Python实现生成简单的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 $
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 $
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 .
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
相关文章
相关视频
专题推荐
-
独孤九贱-php全栈开发教程
全栈 170W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
-
玉女心经-web前端开发教程
入门 80W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
-
天龙八部-实战开发教程
实战 120W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论