Python实现生成简单的Makefile文件代码示例


Posted in Python onMarch 10, 2015

在linux下写几个测试程序,还要一行行的输入g++命令进行编译,当经常改测试代码的时候,那一次次的敲(或者一次次的上线箭头选)也感觉不爽,不如make来的快。用Makefile的好处就不用多说了,这里我写了个脚本,其功能是自动搜索当前目录(不包括子目录)下的“.c”文件生成Makefile文件。

代码在这里,功能有限(适用于单个文件是一个独立的测试代码的情况),需要的朋友可以稍作修改以满足需求。

#! /usr/bin/python

'''

 File      : genMakefile.py

 Author    : Mike

 E-Mail    : Mike_Zhang@live.com

'''

import os
def genMakefileStr(dir,surfix = '.c'):

    msg = ''

    msg = msg + 'CC = gcc' + '\n'

    msg = msg +  'CFLAGS = -g -O2 -Wall' + '\n\n'

    

    fList = []

    for dirPath,dirNames,fileNames in os.walk(dir):

        for file in fileNames:

            name,extension = os.path.splitext(file)

            if extension == surfix:

                fList.append(name)

        break # only search the current directory

    str1 = 'all:\n'

    str2 = ''

    str3 = 'clean:\n'

    for f in fList:

        str1 = str1 + '\tmake ' + f + '\n'

        str2 = ('%s%s:%s.o\n') % (str2,f,f)

        str2 = ('%s\t$(CC) -o %s %s.o\n\n') % (str2,f,f)

        str3 = ('%s\trm -f %s\n') % (str3,f)

    str3 = str3 + '\trm -f *.o\n'

    strClean = '.c.o:\n\t$(CC) $(CFLAGS) -c -o $*.o $<\n'

    msg = ('%s%s\n%s\n%s\n%s') % (msg,str1,str2,str3,strClean) 

    #print 'msg : \n'

    #print msg

    return msg
if __name__ == '__main__':

    str = genMakefileStr('.','.c')

    file = open("Makefile","w")

    file.write(str)

    file.close()

    print str

运行效果如下(示例):

# ./genMakefile.py          

CC = gcc

CFLAGS = -g -O2 -Wall
all:

        make pfun1

        make pfun2
pfun1:pfun1.o

        $(CC) -o pfun1 pfun1.o
pfun2:pfun2.o

        $(CC) -o pfun2 pfun2.o


clean:

        rm -f pfun1

        rm -f pfun2

        rm -f *.o
.c.o:

        $(CC) $(CFLAGS) -c -o $*.o $<

运行脚本后进行make即可。

附:

感觉上面的那个脚本用着不方便,随后修改修改,代码如下:

#! /usr/bin/python

'''

  File      : genMakefile.py

  Author    : Mike

  E-Mail    : Mike_Zhang@live.com

'''

import os,sys

 

surfix = ['.c','.cpp']
def genMakefileStr(dir):

    msg = ''

    msg = msg + 'CC = g++ ' + '\n'

    msg = msg +  'CFLAGS = -g -O2 -Wall' + '\n\n'

    

    fList = []

    for dirPath,dirNames,fileNames in os.walk(dir):

        for file in fileNames:

            name,extension = os.path.splitext(file)

            if surfix.count(extension) > 0:

                fList.append(name)

        break # only search the current directory

    str1 = 'all:\n'

    str2 = ''

    str3 = 'clean:\n'

    for f in fList:

        str1 = str1 + '\tmake ' + f + '\n'

        str2 = ('%s%s:%s.o\n') % (str2,f,f)

        str2 = ('%s\t$(CC) -o %s %s.o\n\n') % (str2,f,f)

        str3 = ('%s\trm -f %s\n') % (str3,f)

    str3 = str3 + '\trm -f *.o\n'

    strClean = '.c.cpp.o:\n\t$(CC) $(CFLAGS) -c -o $*.o $<\n'

    msg = ('%s%s\n%s\n%s\n%s') % (msg,str1,str2,str3,strClean) 

    #print 'msg : \n'

    #print msg

    return msg

 

if __name__ == '__main__':

    for arg in sys.argv[1:]:

        print arg

    str = genMakefileStr(arg)

    if arg[-1] == '/':arg = arg[:-1]

    file = open(arg+"/Makefile","w")

    file.write(str)

    file.close()

    print str

把文件genMakefile.py改名为genMakefile,复制到/usr/local/bin下,以后在需要的目录里面执行如下命令即可:

genMakefile .

Python 相关文章推荐
Python2.6版本中实现字典推导 PEP 274(Dict Comprehensions)
Apr 28 Python
Python 网页解析HTMLParse的实例详解
Aug 10 Python
Python 模拟登陆的两种实现方法
Aug 10 Python
Python使用django框架实现多人在线匿名聊天的小程序
Nov 29 Python
Python tkinter label 更新方法
Oct 11 Python
Python实现批量执行同目录下的py文件方法
Jan 11 Python
Python pycharm 同时加载多个项目的方法
Jan 17 Python
Python django框架开发发布会签到系统(web开发)
Feb 12 Python
python实现拼图小游戏
Feb 22 Python
Django用户身份验证完成示例代码
Apr 03 Python
Mac PyCharm中的.gitignore 安装设置教程
Apr 16 Python
python自动获取微信公众号最新文章的实现代码
Jul 15 Python
Python和GO语言实现的消息摘要算法示例
Mar 10 #Python
Windows和Linux下使用Python访问SqlServer的方法介绍
Mar 10 #Python
Python脚本实现代码行数统计代码分享
Mar 10 #Python
Windows系统配置python脚本开机启动的3种方法分享
Mar 10 #Python
Python自动化构建工具scons使用入门笔记
Mar 10 #Python
Python操作CouchDB数据库简单示例
Mar 10 #Python
Python性能优化技巧
Mar 09 #Python
You might like
php 移除数组重复元素的一点说明
2008/11/27 PHP
php下统计用户在线时间的一种尝试
2010/08/26 PHP
PHPWind与Discuz截取字符函数substrs与cutstr性能比较
2011/12/05 PHP
php类的扩展和继承用法实例
2015/06/20 PHP
php邮箱地址正则表达式验证
2015/11/13 PHP
PHP使用strtotime获取上个月、下个月、本月的日期
2015/12/30 PHP
PHP的PDO大对象(LOBs)
2019/01/27 PHP
Vagrant(WSL)+PHPStorm+Xdebu 断点调试环境搭建
2019/12/13 PHP
常见效果实现之返回顶部(结合淡入、淡出、减速滚动)
2012/01/04 Javascript
jQuery实现响应浏览器缩放大小并改变背景颜色
2014/10/31 Javascript
JQuery中绑定事件(bind())和移除事件(unbind())
2015/02/27 Javascript
JQuery插入DOM节点的方法
2015/06/11 Javascript
Node.js开发第三方微信公众平台
2017/06/05 Javascript
jQuery简单绑定单个事件的方法示例
2017/06/10 jQuery
React-Native左右联动List的示例代码
2017/09/21 Javascript
vue 添加vux的代码讲解
2017/11/30 Javascript
vue项目base64字符串转图片的实现代码
2018/07/13 Javascript
vue实现的双向数据绑定操作示例
2018/12/04 Javascript
使用Vue开发自己的Chrome扩展程序过程详解
2019/06/21 Javascript
Vue实现可移动水平时间轴
2020/06/29 Javascript
[15:58]DOTA2国际邀请赛采访专栏:Tongfu.Sansheng&KingJ,DK.rOtk
2013/08/08 DOTA
Python smallseg分词用法实例分析
2015/05/28 Python
python cs架构实现简单文件传输
2020/03/20 Python
Python字典的基本用法实例分析【创建、增加、获取、修改、删除】
2019/03/05 Python
python dataframe NaN处理方式
2019/12/26 Python
Python实现括号匹配方法详解
2020/02/10 Python
简单掌握CSS3将文字描边及填充文字颜色的方法
2016/03/07 HTML / CSS
CSS3 实现时间轴动画
2020/11/25 HTML / CSS
打造经典复古风格的品牌:Alice + Olivia(爱丽丝+奥利维亚)
2016/09/07 全球购物
美国单身专业人士在线约会网站:EliteSingles
2019/03/19 全球购物
国贸专业个人求职信分享
2013/12/04 职场文书
农村婚礼主持词
2014/03/13 职场文书
撤诉书怎么写
2015/05/19 职场文书
变长双向rnn的正确使用姿势教学
2021/05/31 Python
MySQL中的布尔值,怎么存储false或true
2021/06/04 MySQL
python Tkinter模块使用方法详解
2022/04/07 Python