makefile太难了,学不会,自己用Python写个简单的构建工具
来源:互联网 发布:windows informix 编辑:程序博客网 时间:2024/04/29 13:46
makefile 太复杂了,学的不精。自己用Python写一个构建工具,经过简单测试,可用
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import argparse import os import commands import re # .c对应的.o文件 def obj_file_path(srcpath): return srcpath[:-1] + 'o' # 检测.[hc]是否比.o文件新 def is_newer_than(src, obj): srcmtime = os.stat(src).st_mtime if os.path.exists(obj): objmtime = os.stat(obj).st_mtime else: objmtime = 0 return srcmtime > objmtime # 检查是否需要重新编译.c文件 def need_compile_src(srcpath): output = commands.getoutput('{} -MM {}'.format(cc, srcpath)) hlist = output.split(':')[1].strip().split(' ') hfiles = [h for h in hlist if h.endswith('.h')] objpath = obj_file_path(srcpath) # 检查依赖的头文件是否更新了 for h in hfiles: if is_newer_than(h, objpath): return True # 检查源文件是否更新了 if is_newer_than(srcpath, objpath): return True # 检查配置文件是否更新了 cfgpath = os.path.abspath('buildconfig') execpath = os.path.abspath(execname) if is_newer_than(cfgpath, execpath): return True return False # target build def build(): # compile needlink = False objfiles = [] for srcfile in srcfiles: objfile = obj_file_path(srcfile) objfiles.append(objfile) if need_compile_src(srcfile): needlink = True cccmd = '{} {} -c {} -o {}'.format(cc, cflags, srcfile, objfile) if not quiet: print(cccmd) if os.system(cccmd): sys.exit(1) # link if needlink: dependency = '' for objfile in objfiles: dependency += (' ' + objfile) execpath = os.path.abspath(execname); ldcmd = '{} {} {} -o {}'.format(cc, ldflags, dependency, execpath) if not quiet: print(ldcmd) os.system(ldcmd) def remove_file(filepath): if os.path.exists(filepath): if not quiet: print('rm {}'.format(filepath)) os.remove(filepath) # target clean def clean(): # remove obj files for srcfile in srcfiles: objfile = obj_file_path(srcfile) remove_file(objfile) # remove exec file execpath = os.path.abspath(execname); remove_file(execpath) # generate the buildconfig file def gen_buildconfig(): with open('buildconfig', 'w') as cfgfile: cfgfile.writelines('''######## buildconfig ######## CC=cc # eg. cc/gcc/clang CFLAGS=-g LDFLAGS= EXECNAME=main ''') # parse command line opthons def args_parser(): parser = argparse.ArgumentParser() parser.add_argument('-c', '--config', action='store_true', help='create the buildconfig file') parser.add_argument('-q', '--quiet', action='store_true', help='quiet') parser.add_argument('target', nargs='?', choices=['build', 'clean'], default='build', help='build or clean.') return parser # parse buildconfig file def parse_build_config(): variables = dict() cfgpath = os.path.abspath('buildconfig'); if not os.path.exists(cfgpath): print('先创建一个buildconfig文件') parser.print_help() sys.exit(2) with open(cfgpath) as cfgfile: pattern = re.compile(r'#.*$') for line in cfgfile: line = pattern.sub('', line) line = line.strip() pair = line.split('=') if len(pair) == 2: variables[pair[0].strip()] = pair[1].strip() return variables # find all src files def src_file_list(srcdir, ext): srcfiles = [] for dirpath, dirnames, filenames in os.walk(srcdir): for filename in filenames: fileext = os.path.splitext(filename)[1] if fileext == '.' + ext: srcpath = os.path.join(dirpath, filename) srcfiles.append(srcpath) return srcfiles # main entry if __name__ == '__main__': parser = args_parser() # parse args args = parser.parse_args() quiet = args.quiet config = args.config if config: gen_buildconfig() sys.exit(0) # parse variables variables = parse_build_config(); cc = variables.setdefault('CC', 'cc') cflags = variables.setdefault('CFLAGS', '-g') ldflags = variables.setdefault('LDFLAGS', '') execname = variables.setdefault('EXECNAME', 'main') # src files srcfiles = src_file_list(os.getcwd(), 'c') # make target if args.target == 'build': build() elif args.target == 'clean': clean()
使用演示
开启树莓派探索模式
0 0
- makefile太难了,学不会,自己用Python写个简单的构建工具
- 学了2天的Python,自己写了个简单的爬虫,可是爬虫有什么用呢?
- 写个简单的makefile
- leetcode:Combinations (太难了,递归,不会写)
- 郁闷,发现自己太不会应变了。。。
- 刚学ui,自己写了个霓虹灯,我觉的我写的这个比比百度上简单多了!!!
- EVE 居然是用 python 写的!!太不可思议了
- 自己写了个魔兽显血改键工具
- 自己写了个简单的供求系统
- 写自己的第一个makefile
- 晒晒自己学了这段时间的C语言自己写的简单的程序
- 自己写一个简单通用的Makefile
- 自己写一个简单通用的Makefile
- 闲来无事,将自己收集到的工具类封装了下,做了一个lib,写了个简单的mvp,初学者可以看看
- 自己用c#写的俄罗斯方块,基本上实现了,就是算法太笨了,呵呵
- 学了几个月vc++,只会看些简单的程序,自己还是不会编写,很是郁闷呀
- 学了一周的安卓,写了个简单的 种子搜索器app
- iScroll太臃肿,自己写了个,发出来供有需要的人,压缩后3.8KB,支持滚动回弹。
- 使用Spring的@Scheduled实现定时任务
- 查看Linux服务器上的Redis版本号
- c++定义命名空间
- java 中的 wait()方法和 sleep()方法的区别
- openssl之BIO系列之21---Base64类型的BIO
- makefile太难了,学不会,自己用Python写个简单的构建工具
- selenium启动的firefox开启新的匿名的profiles
- AFNetWorking设置请求超时时间
- SQL Alias(别名) 高级教程
- 十步完全理解SQL
- poj并查集之畅通工程
- OSG中相交测试模块工作流程及原理
- 实用linux命令实例
- SQL JOIN 高级教程