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 相关文章推荐
一个小示例告诉你Python语言的优雅之处
Jul 04 Python
Pycharm远程调试openstack的方法
Nov 21 Python
Python实现读写INI配置文件的方法示例
Jun 09 Python
python连接mongodb密码认证实例
Oct 16 Python
浅谈python连续赋值可能引发的错误
Nov 10 Python
Python3的unicode编码转换成中文的问题及解决方案
Dec 10 Python
python 中不同包 类 方法 之间的调用详解
Mar 09 Python
PyCharm 在Windows的有用快捷键详解
Apr 07 Python
Python ini文件常用操作方法解析
Apr 26 Python
python3+PyQt5+Qt Designer实现界面可视化
Jun 10 Python
Django REST framework 限流功能的使用
Jun 24 Python
Python四款GUI图形界面库介绍
Jun 05 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 日期和时间的处理-郑阿奇(续)
2011/07/04 PHP
PHP 基于文件头的文件类型验证类函数
2012/05/01 PHP
php类中private属性继承问题分析
2012/11/01 PHP
smarty基础之拼接字符串的详解
2013/06/18 PHP
php防止sql注入示例分析和几种常见攻击正则表达式
2014/01/12 PHP
cakephp2.X多表联合查询join及使用分页查询的方法
2017/02/23 PHP
360搜索引擎自动收录php改写方案
2018/04/28 PHP
javascript自适应宽度的瀑布流实现思路
2013/02/20 Javascript
js怎么覆盖原有方法实现重写
2014/09/04 Javascript
jquery模拟alert的弹窗插件
2015/07/31 Javascript
js点击返回跳转到指定页面实现过程
2020/08/20 Javascript
自己动手封装一个React Native多级联动
2018/09/19 Javascript
原生JS实现旋转轮播图+文字内容切换效果【附源码】
2018/09/29 Javascript
Vue在 Nuxt.js 中重定向 404 页面的方法
2019/04/23 Javascript
[02:06]DOTA2肉山黑名单魔法终结者 敌法师中文配音鉴赏
2013/06/17 DOTA
[04:40]2016个国际邀请赛中国区预选赛场地——华西村观战指南
2016/06/25 DOTA
python网络编程学习笔记(九):数据库客户端 DB-API
2014/06/09 Python
深入理解NumPy简明教程---数组1
2016/12/17 Python
基于Django的python验证码(实例讲解)
2017/10/23 Python
python使用pyqt写带界面工具的示例代码
2017/10/23 Python
numpy中索引和切片详解
2017/12/15 Python
使用python读取.text文件特定行的数据方法
2019/01/28 Python
python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)
2019/08/28 Python
解决python -m pip install --upgrade pip 升级不成功问题
2020/03/05 Python
python如何控制进程或者线程的个数
2020/10/16 Python
HTML5开发动态音频图的实现
2020/07/02 HTML / CSS
香蕉共和国Banana Republic官网:美国GAP旗下偏贵族风格服饰品牌
2016/11/21 全球购物
经营理念口号
2014/06/21 职场文书
会计专业毕业生自荐书
2014/06/25 职场文书
高中学生会竞选演讲稿
2014/08/25 职场文书
2014年党风廉政工作总结
2014/12/03 职场文书
2014年小学图书室工作总结
2014/12/09 职场文书
2014小学语文教学工作总结
2014/12/17 职场文书
公司业务员管理制度
2015/08/05 职场文书
2015年行政管理人员工作总结
2015/10/15 职场文书
Python中的turtle画箭头,矩形,五角星
2022/03/16 Python