Created
November 24, 2013 07:48
-
-
Save maliubiao/7624551 to your computer and use it in GitHub Desktop.
build a c project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python | |
| import os | |
| import subprocess | |
| import cStringIO | |
| TYPEC = 1 << 1 | |
| TYPECXX = 1 <<2 | |
| project = { | |
| "src": ["signal.c"], | |
| "includes": [], | |
| "libraries": [], | |
| "cflags": ["-gdwarf-4"], | |
| "target": "signal", | |
| "type": TYPEC | |
| } | |
| commands = [] | |
| TOOLS = { | |
| "compiler": "gcc", | |
| "linker": "ld" | |
| } | |
| def no_key_error(dict, keys): | |
| for key in keys: | |
| if key not in dict: | |
| print "KeyError, %s" % key | |
| exit(1) | |
| def add_executable(project): | |
| required = ["src", "target", "type"] | |
| no_key_error(project, required) | |
| if project["type"] == TYPEC: | |
| TOOLS["compiler"] = "gcc" | |
| elif project["type"] == TYPECXX: | |
| TOOLS["compiler"] = "g++" | |
| else: | |
| print "unable to find compiler" | |
| cmd = cStringIO.StringIO() | |
| cmd.write(TOOLS["compiler"]+" ") | |
| if "cflags" in project: | |
| for f in project["cflags"]: | |
| cmd.write(f + " ") | |
| if "includes" in project: | |
| for i in project["includes"]: | |
| cmd.write("-I %s " % i) | |
| for s in project["src"]: | |
| cmd.write(s + " ") | |
| cmd.write("-o %s " % project["target"]) | |
| commands.append((project["target"], cmd.getvalue())) | |
| cmd.close() | |
| def build_project(project): | |
| add_executable(project) | |
| for cmd in commands: | |
| print "building %s" % cmd[0] | |
| subprocess.call(cmd[1], shell=True) | |
| if __name__ == "__main__": | |
| build_project(project) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment