|
| 1 | +import yaml |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import platform |
| 6 | +import atexit |
| 7 | +import subprocess |
| 8 | + |
| 9 | +SOURCE_DIR = "source" |
| 10 | +BUILD_DIR = "build" |
| 11 | + |
| 12 | +# read config file (depends.yaml) |
| 13 | +config = None |
| 14 | +with open("depends.yaml") as f: |
| 15 | + try: |
| 16 | + config = yaml.load(f) |
| 17 | + except yaml.YAMLError as e: |
| 18 | + print e |
| 19 | + |
| 20 | +assert config is not None |
| 21 | +print config |
| 22 | + |
| 23 | +# log.txt |
| 24 | +log = open("log.txt", "w", 0) |
| 25 | +@atexit.register |
| 26 | +def close_log(): |
| 27 | + log.flush() |
| 28 | + log.close() |
| 29 | + |
| 30 | +class Logger(object): |
| 31 | + def __init__(self): |
| 32 | + # backup old stdout |
| 33 | + self.terminal = sys.stdout |
| 34 | + |
| 35 | + def write(self, messgae): |
| 36 | + self.terminal.write(messgae) |
| 37 | + log and log.write(messgae) |
| 38 | + |
| 39 | +logger = Logger() |
| 40 | +sys.stdout = logger |
| 41 | +sys.stderr = logger |
| 42 | + |
| 43 | +# check os |
| 44 | +os_name = platform.system().lower() |
| 45 | +BUILD_OS = [v for k, v in {"win": "win", "darwin": "osx", "linux" : "linux"}.items() if k in os_name][0] |
| 46 | +print "Build OS: ", BUILD_OS |
| 47 | + |
| 48 | + |
| 49 | +def create_dir(dir_name): |
| 50 | + if not os.path.exists(dir_name): |
| 51 | + os.mkdir(dir_name) |
| 52 | + |
| 53 | +def remove_dir(dir_name): |
| 54 | + if os.path.exists(dir_name): |
| 55 | + shutil.rmtree(dir_name) |
| 56 | + |
| 57 | +def create_empty_dir(dir_name): |
| 58 | + remove_dir(dir_name) |
| 59 | + create_dir(dir_name) |
| 60 | + |
| 61 | +# get real path |
| 62 | +SOURCE_DIR = os.path.realpath(config.get("source", "source")) |
| 63 | +BUILD_DIR = os.path.realpath(config.get("build", "build")) |
| 64 | +print 'Source Dir: ', SOURCE_DIR |
| 65 | +print 'Build Dir:', BUILD_DIR |
| 66 | + |
| 67 | +create_dir(SOURCE_DIR) |
| 68 | +create_dir(BUILD_DIR) |
| 69 | + |
| 70 | + |
| 71 | +def run_cmd(cmd): |
| 72 | + print '-- run -- ', cmd |
| 73 | + poll_code = None |
| 74 | + try: |
| 75 | + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=0) |
| 76 | + while True: |
| 77 | + output = p.stdout.readline() |
| 78 | + if output: |
| 79 | + print output, |
| 80 | + poll_code = p.poll() |
| 81 | + if poll_code is not None: |
| 82 | + break |
| 83 | + except Exception, e: |
| 84 | + p.terminate() |
| 85 | + print e |
| 86 | + finally: |
| 87 | + # flush, counldn't block here |
| 88 | + p.stdout.flush() |
| 89 | + output = p.stdout.read() |
| 90 | + if output: |
| 91 | + print output, |
| 92 | + assert poll_code == 0, 'run cmd failed, code %s!'%poll_code |
| 93 | + |
| 94 | + |
| 95 | +class Builder(object): |
| 96 | + def __init__(self, target=None): |
| 97 | + self.ninja = None |
| 98 | + |
| 99 | + def build_ninja(self): |
| 100 | + source_dir = os.path.join(SOURCE_DIR, "ninja") |
| 101 | + build_dir = os.path.join(BUILD_DIR, "ninja") |
| 102 | + |
| 103 | + suffix = ".exe" if BUILD_OS == "win" else "" |
| 104 | + self.ninja = os.path.join(build_dir, "ninja%s"%(suffix, )) |
| 105 | + if os.path.exists(self.ninja): |
| 106 | + print '================== Ninja Alredy Build, Skipped... ==================' |
| 107 | + return |
| 108 | + |
| 109 | + create_empty_dir(build_dir) |
| 110 | + |
| 111 | + cmd = "cd %s && python %s/configure.py --bootstrap"%(build_dir, source_dir) |
| 112 | + run_cmd(cmd) |
| 113 | + |
| 114 | + def clean(self): |
| 115 | + remove_dir(BUILD_DIR) |
| 116 | + |
| 117 | + def build_one_lib(self, lib_name, cmake_dir, cmake_args=None): |
| 118 | + source_dir = os.path.join(SOURCE_DIR, lib_name) |
| 119 | + if not os.path.exists(source_dir): |
| 120 | + source_dir = os.path.join(SOURCE_DIR, "..", lib_name) |
| 121 | + if not os.path.exists(source_dir): |
| 122 | + print 'error', lib_name, 'not exist' |
| 123 | + return |
| 124 | + |
| 125 | + build_dir = os.path.join(BUILD_DIR, lib_name) |
| 126 | + install_dir = os.path.realpath(config.get("install", "install")) |
| 127 | + |
| 128 | + create_empty_dir(build_dir) |
| 129 | + |
| 130 | + now_path = os.path.realpath(".") |
| 131 | + os.chdir(build_dir) |
| 132 | + |
| 133 | + print '================== BUILD %s BEGIN =================='%(lib_name.upper(), ) |
| 134 | + final_cmake_args = [] |
| 135 | + final_cmake_args.extend(config.get("cmake_args", [])) |
| 136 | + |
| 137 | + if cmake_args: |
| 138 | + final_cmake_args.extend(cmake_args) |
| 139 | + final_cmake_args.append("-DCMAKE_INSTALL_PREFIX=%s"%(install_dir,)) |
| 140 | + final_cmake_args.append("-DCMAKE_MAKE_PROGRAM=%s"%self.ninja) |
| 141 | + |
| 142 | + cmake_arg_str = " ".join(final_cmake_args) |
| 143 | + cmd = " ".join(["cmake -G Ninja", cmake_arg_str, "%s/%s"%(source_dir, cmake_dir)]) |
| 144 | + |
| 145 | + run_cmd(cmd) |
| 146 | + run_cmd(self.ninja) |
| 147 | + run_cmd(self.ninja + " install") |
| 148 | + print '================== BUILD %s END =================='%(lib_name.upper(), ) |
| 149 | + |
| 150 | + os.chdir(now_path) |
| 151 | + |
| 152 | + |
| 153 | + def build_libs(self, sp_name=None): |
| 154 | + for name, attr in config["depends"].items(): |
| 155 | + if name == "ninja" or (sp_name and sp_name != name): |
| 156 | + continue |
| 157 | + self.build_one_lib(name, "", attr.get('cmake_args')) |
| 158 | + |
| 159 | + |
| 160 | + def download_libs(self): |
| 161 | + for name, attr in config["depends"].items(): |
| 162 | + git_url = attr.get("git", None) |
| 163 | + if git_url: |
| 164 | + source_path = os.path.join(SOURCE_DIR, name) |
| 165 | + if not os.path.exists(source_path): |
| 166 | + run_cmd("cd %s && git clone %s %s"%(SOURCE_DIR, git_url, name)) |
| 167 | + if attr.get('tag'): |
| 168 | + run_cmd("cd %s && git checkout %s"%(source_path, attr.get("tag"))) |
| 169 | + |
| 170 | + |
| 171 | + def work(self): |
| 172 | + self.download_libs() |
| 173 | + self.build_ninja() |
| 174 | + self.build_libs() |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == "__main__": |
| 178 | + if len(sys.argv) > 1: |
| 179 | + if sys.argv[1] == "clean": |
| 180 | + Builder().clean() |
| 181 | + if sys.argv[1] == "build": |
| 182 | + b = Builder() |
| 183 | + b.build_ninja() |
| 184 | + b.build_libs(sys.argv[2]) |
| 185 | + exit(0) |
| 186 | + |
| 187 | + Builder().work() |
0 commit comments