您的位置:首页 > 编程语言 > Python开发

python-简单的makefile生成程序-没有注释

2015-12-16 20:30 519 查看
build.py

#!/usr/bin/python
# -*- coding:utf-8 -*-
import os, sys, threading, re

class R():
def __init__(self):
pass
newline = "\n"
subMakefile = '''all:
\t$(CC) $(CFLAGS) $(INC) -c *.c $(LIBS)
clean:
\trm -f *.o
'''
mainMakefile = '''all:$(SUBTASK)
\t$(CC) $(CFLAGS) $(INC) -c *.c $(LIBS)
\t$(CC) -o $(APP) *.o $(SUBOBJS) $(LIBS)
clean:
\trm -f *.o $(APP)
'''

class MakeTool(threading.Thread):
def __init__(self, app = "hello", dir = ".", cc = "gcc"):
threading.Thread.__init__(self)
self.dir = dir
self.app = app
self.cc = cc
def hasCFile(self, dirName):
r = re.compile(".*.\.c$")
for fileName in os.listdir(dirName):
m = r.search(fileName)
if m:
#                print fileName
return True
return False
def run(self):
head = "export CC=" + self.cc + R.newline
head += "export MAKE=make -w" + R.newline
if "mipsel-linux-gcc" == self.cc:
head += "export INC=-I. -I.. -I/root/RT288x_SDK/source/user/openssl-0.9.8e/include" + R.newline
head += "export LIBS=-L/root/RT288x_SDK/source/romfs/lib -lpthread -lssl -lcrypto" + R.newline
elif "gcc" == self.cc:
head += "export INC=-I. -I.." + R.newline
head += "export LIBS=-lpthread -lssl -lcrypto" + R.newline
head += "APP=" + self.app + R.newline
predef = "-D_TEST"
head += "export CFLAGS=" + predef + R.newline
subObjs = "SUBOBJS="
subMakeAll = "SUBTASK:" + R.newline
subClean = ""
hasDir = False
if not os.path.isdir(self.dir):
return
for root, dirs, files in os.walk(self.dir):
for dir in dirs:
fileName = root + "/" + dir
if self.hasCFile(fileName):
#                    print "dirname", fileName
subObjs += fileName + "/*.o "
subMakeAll += "\t$(MAKE) -C " + fileName + R.newline
subClean += "\t$(MAKE) -C " + fileName + " clean" + R.newline
file = open(fileName + "/Makefile", "w")
file.write(R.subMakefile)
file.close()
hasDir = True
if not hasDir:
subMakeAll = ""
s = head + subObjs + R.newline + subMakeAll + R.newline + R.mainMakefile + subClean
print s
print R.subMakefile
file = open(self.dir + "/Makefile", "w")
file.write(s)
file.close()

def help():
s = '''
python autoconfig.py [mips|gcc]
'''
print s

def main(argv):
appName = os.path.basename(os.path.abspath("."))
if len(argv) == 1:
tool = MakeTool(appName, ".", "mipsel-linux-gcc")
tool.run()
elif len(argv) == 2:
if "gcc" == argv[1]:
tool = MakeTool(appName, ".", "gcc")
tool.run()
elif "mips" == argv[1] or "mipsel" == argv[1] or "mipsel-linux-gcc" == argv[1]:
tool = MakeTool(appName, ".", "mipsel-linux-gcc")
tool.run()
else:
help()
else:
help()
os.system("kill -9 " + str(os.getpid())) #杀掉进程

if __name__ == "__main__":
main(sys.argv)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  makefile python