|
| 1 | +import sys |
| 2 | +import json |
| 3 | +import os |
| 4 | +from distutils.version import LooseVersion |
| 5 | + |
| 6 | +build_file = "build.property" |
| 7 | +required_version = {} |
| 8 | + |
| 9 | +# cntoolkit, cnnl |
| 10 | +modules = ["cntoolkit", "cnnl"] |
| 11 | +# NEUWARE_HOME = env_vars["NEUWARE_HOME"] |
| 12 | +env_vars = dict(os.environ) |
| 13 | + |
| 14 | + |
| 15 | +def get_build_requires(print_mode=1): |
| 16 | + global required_version |
| 17 | + with open(build_file) as build_property: |
| 18 | + data = json.load(build_property) |
| 19 | + required_version = data["build_requires"] |
| 20 | + for key in modules: |
| 21 | + if print_mode == 1: |
| 22 | + print( |
| 23 | + "%s %s %s" |
| 24 | + % (key, required_version[key][0], required_version[key][1]) |
| 25 | + ) |
| 26 | + required_version[key] = required_version[key][1].split("-")[0] |
| 27 | + |
| 28 | + |
| 29 | +def check_cntoolkit(): |
| 30 | + toolkit_ver_path = env_vars["NEUWARE_HOME"] + "/version.txt" |
| 31 | + if not os.path.exists(toolkit_ver_path): |
| 32 | + print("Not found toolkit") |
| 33 | + exit(2) |
| 34 | + |
| 35 | + # check cntoolkit |
| 36 | + with open(toolkit_ver_path) as tk_f: |
| 37 | + data = tk_f.readlines() |
| 38 | + for line in data: |
| 39 | + if "Neuware Version" in line: |
| 40 | + cur_tk_ver = line.strip("\n").split(" ")[-1] |
| 41 | + if LooseVersion(required_version["cntoolkit"]) > LooseVersion( |
| 42 | + cur_tk_ver |
| 43 | + ): |
| 44 | + print( |
| 45 | + "The version of cntoolkit must be at least " |
| 46 | + + required_version["cntoolkit"] |
| 47 | + + ", but local version is " |
| 48 | + + cur_tk_ver |
| 49 | + ) |
| 50 | + exit(1) |
| 51 | + |
| 52 | + |
| 53 | +def check_cnnl(): |
| 54 | + cnnl_ver_pre = env_vars["NEUWARE_HOME"] + "/lib64/" |
| 55 | + if not os.path.exists(cnnl_ver_pre): |
| 56 | + print("Not found cnnl") |
| 57 | + exit(2) |
| 58 | + |
| 59 | + # check cnnl |
| 60 | + for filePath in os.listdir(cnnl_ver_pre): |
| 61 | + if "libcnnl.so." in filePath: |
| 62 | + tmp = filePath.split(".") |
| 63 | + if len(tmp) > 3: |
| 64 | + cur_cnnl_ver = filePath[11:] |
| 65 | + if LooseVersion(required_version["cnnl"]) > LooseVersion(cur_cnnl_ver): |
| 66 | + print( |
| 67 | + "The version of cnnl must be at least " |
| 68 | + + required_version["cnnl"] |
| 69 | + + ", but local version is " |
| 70 | + + cur_cnnl_ver |
| 71 | + ) |
| 72 | + exit(1) |
| 73 | + |
| 74 | + |
| 75 | +def check_driver(): |
| 76 | + sys_out = os.popen("cnmon version").readline() |
| 77 | + if len(sys_out) == 0: |
| 78 | + print("Warning: not found cnmon.") |
| 79 | + print("If compilation failed, please check driver version") |
| 80 | + return |
| 81 | + |
| 82 | + sys_out = sys_out.strip("\n").split(":")[-1] |
| 83 | + if LooseVersion(required_version["driver"]) > LooseVersion(sys_out): |
| 84 | + print( |
| 85 | + "The version of driver must be at least " |
| 86 | + + required_version["driver"] |
| 87 | + + ", but local version is " |
| 88 | + + sys_out |
| 89 | + ) |
| 90 | + exit(1) |
| 91 | + |
| 92 | + |
| 93 | +def check_protoc(): |
| 94 | + sys_out = os.popen("protoc --version").readline() |
| 95 | + if len(sys_out) == 0: |
| 96 | + print("Not found protoc") |
| 97 | + exit(2) |
| 98 | + |
| 99 | + sys_out = sys_out.strip("\n").split(" ")[-1] |
| 100 | + if LooseVersion(required_version["protoc"]) < LooseVersion(sys_out): |
| 101 | + print( |
| 102 | + "The version of protoc must be at most " |
| 103 | + + required_version["protoc"] |
| 104 | + + ", but local version is " |
| 105 | + + sys_out |
| 106 | + ) |
| 107 | + exit(1) |
| 108 | + |
| 109 | + |
| 110 | +def check_libxml2(): |
| 111 | + sys_out = os.popen("xml2-config --version").readline() |
| 112 | + if len(sys_out) == 0: |
| 113 | + print("Not found libxml2") |
| 114 | + exit(2) |
| 115 | + |
| 116 | + sys_out = sys_out.strip("\n") |
| 117 | + if LooseVersion(required_version["libxml2"]) > LooseVersion(sys_out): |
| 118 | + print( |
| 119 | + "The version of libxml2 must be at least " |
| 120 | + + required_version["libxml2"] |
| 121 | + + ", but local version is " |
| 122 | + + sys_out |
| 123 | + ) |
| 124 | + exit(1) |
| 125 | + |
| 126 | + |
| 127 | +def check_eigen3(): |
| 128 | + if os.path.exists("/usr/local/include/eigen3/Eigen/src/Core/util/Macros.h"): |
| 129 | + h_file = open("/usr/local/include/eigen3/Eigen/src/Core/util/Macros.h") |
| 130 | + elif os.path.exists("/usr/include/eigen3/Eigen/src/Core/util/Macros.h"): |
| 131 | + h_file = open("/usr/include/eigen3/Eigen/src/Core/util/Macros.h") |
| 132 | + else: |
| 133 | + print("Not found eigen3") |
| 134 | + exit(2) |
| 135 | + |
| 136 | + line = h_file.readline() |
| 137 | + eigen_ver = "" |
| 138 | + while len(line) > 0: |
| 139 | + if "Eigen version and basic defaults" in line: |
| 140 | + line = h_file.readline() |
| 141 | + line = h_file.readline() |
| 142 | + eigen_ver = h_file.readline()[28:-1] |
| 143 | + eigen_ver += "." + h_file.readline()[28:-1] |
| 144 | + eigen_ver += "." + h_file.readline()[28:-1] |
| 145 | + break |
| 146 | + line = h_file.readline() |
| 147 | + |
| 148 | + if LooseVersion(required_version["eigen3"]) > LooseVersion(eigen_ver): |
| 149 | + print( |
| 150 | + "The version of eigen3 must be at least " |
| 151 | + + required_version["eigen3"] |
| 152 | + + ", but local version is " |
| 153 | + + eigen_ver |
| 154 | + ) |
| 155 | + exit(1) |
| 156 | + |
| 157 | + |
| 158 | +def check_build_requires(): |
| 159 | + get_build_requires(0) |
| 160 | + check_cntoolkit() |
| 161 | + check_cnnl() |
| 162 | + check_driver() |
| 163 | + check_protoc() |
| 164 | + check_libxml2() |
| 165 | + check_eigen3() |
| 166 | + |
| 167 | + |
| 168 | +argvs = sys.argv[1:] |
| 169 | +if len(argvs) == 1: |
| 170 | + eval(argvs[0])() |
| 171 | +elif len(argvs) == 2: |
| 172 | + eval(argvs[0])(argvs[1]) |
| 173 | +elif len(argvs) == 3: |
| 174 | + eval(argvs[0])(argvs[1], argvs[2]) |
| 175 | +else: |
| 176 | + exit(3) |
0 commit comments