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)
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法