yu.dong | c33b307 | 2024-08-21 23:14:49 -0700 | [diff] [blame^] | 1 | import os |
| 2 | import subprocess |
| 3 | import re |
| 4 | import sys |
| 5 | import platform |
| 6 | from sys import argv |
| 7 | |
| 8 | file_out_path = "" |
| 9 | editor_file_path = [os.path.join("./interface/service/nvram"),os.path.join( "./custom/protocol/common/ps")] |
| 10 | info_file_path = os.path.join(file_out_path, "bin", "log") |
| 11 | inc_file_path = os.path.join(file_out_path, "tmp") |
| 12 | editor_file = [] |
| 13 | def_file = [] |
| 14 | lid_stu_map = {} |
| 15 | |
| 16 | def nvram_gen_open_file(file_path, file_name, mode="r"): |
| 17 | file_pathname = os.path.join(file_path, file_name) |
| 18 | if mode == "r": |
| 19 | # file not exist. |
| 20 | if not os.access(file_pathname, os.F_OK): |
| 21 | printf("error: %s not exist." % file_pathname) |
| 22 | sys.exit(2) |
| 23 | # file not read. |
| 24 | if not os.access(file_pathname, os.R_OK): |
| 25 | printf("error: %s exist, but it cannot be read." % file_pathname) |
| 26 | sys.exit(2) |
| 27 | elif mode == "w+": |
| 28 | # floder not write. |
| 29 | if not os.access(file_path, os.F_OK): |
| 30 | printf("error: folder no exist: %s" % file_path) |
| 31 | sys.exit(2) |
| 32 | try: |
| 33 | fd = open(file_pathname, mode) |
| 34 | except: |
| 35 | printf("error: open file error.") |
| 36 | printf("file_pathname: %s , mode: %s" % (file_pathname, mode)) |
| 37 | raise |
| 38 | |
| 39 | return fd |
| 40 | |
| 41 | def printf(strs, file=sys.stdout, end="\n", flush=False): |
| 42 | strs_val = strs + end |
| 43 | file.write(strs_val) |
| 44 | if flush: |
| 45 | file.flush() |
| 46 | |
| 47 | |
| 48 | def gen_editor_data_item(): |
| 49 | """ parse editor file name, create and write nvram_editor_data_item.c """ |
| 50 | global editor_file |
| 51 | """ list containing the names of the entries in the directory given by path. """ |
| 52 | for editor in editor_file_path: |
| 53 | if not os.access(editor, os.F_OK): |
| 54 | printf("error: %s not exist." % editor) |
| 55 | sys.exit(2) |
| 56 | if not os.access(editor, os.R_OK): |
| 57 | printf("error: %s exist, but it cannot be read." % editor) |
| 58 | sys.exit(2) |
| 59 | dirs = os.listdir(editor) |
| 60 | """ check filename, find editor files name saved to list. """ |
| 61 | editor_pat = re.compile("nvram_editor\.h$") |
| 62 | def_pat = re.compile("nvram_def\.h$") |
| 63 | for item in dirs: |
| 64 | if editor_pat.search(item): |
| 65 | editor_file.append(item) |
| 66 | if def_pat.search(item): |
| 67 | def_file.append(item) |
| 68 | |
| 69 | editor_file.append("nvram_editor_data_item.h") |
| 70 | editor_file.sort() |
| 71 | """ write include file to file. """ |
| 72 | fd = nvram_gen_open_file(file_out_path, "nvram_editor_data_item.c", "w+") |
| 73 | for item in editor_file: |
| 74 | printf("#include \"%s\"" % item, file=fd, flush=True) |
| 75 | |
| 76 | fd.close() |
| 77 | |
| 78 | |
| 79 | def gen_gcc_option(): |
| 80 | """ parse macro in info.log, save to list, write macro to gcc_option.h """ |
| 81 | define_data = [] |
| 82 | """ parse macro in info.log """ |
| 83 | fd = nvram_gen_open_file(info_file_path, "info.log", "r") |
| 84 | for each_line in fd: |
| 85 | if each_line.isspace(): |
| 86 | continue |
| 87 | if each_line.count("[ COMMON INCLUDE PATH ]") != 0: |
| 88 | break |
| 89 | if (each_line.count("[") != 0) or (each_line.count("]") != 0): |
| 90 | continue |
| 91 | item = each_line.replace("=", " ") |
| 92 | item = item.strip() # remove trailing characters "\n \r \t" |
| 93 | define_data.append(item) |
| 94 | fd.close() |
| 95 | # add compile option for nvram gen |
| 96 | define_data.append("__NVRAM_INTERNAL_LID_DESCRIPTION_ENABLE__") |
| 97 | define_data.append("__NV_GEN_FULL_LID__") |
| 98 | |
| 99 | """ write define macro to file. """ |
| 100 | fd = nvram_gen_open_file(file_out_path, "gcc_option.h", "w+") |
| 101 | printf("#ifndef __GCC_OPTION_H__", file=fd, flush=True) |
| 102 | printf("#define __GCC_OPTION_H__", file=fd, flush=True) |
| 103 | printf("", file=fd, flush=True) |
| 104 | |
| 105 | for item in define_data: |
| 106 | printf("#define %s" % item, file=fd, flush=True) |
| 107 | |
| 108 | printf("", file=fd, flush=True) |
| 109 | printf("#endif", file=fd, flush=True) |
| 110 | fd.close() |
| 111 | |
| 112 | def find_include_path_win(gcc, out_path): |
| 113 | """ create gcc preprocessor command and run (win)""" |
| 114 | # create gcc preprocess command |
| 115 | gcc_command = "\"./" + gcc + "\"" + " -DGEN_FOR_PC -E -imacros gcc_option.h -fno-strict-aliasing -fshort-enums" |
| 116 | |
| 117 | fd = nvram_gen_open_file(inc_file_path, "~inc.tmp.tmp", "r") |
| 118 | for each_line in fd: |
| 119 | item = each_line.strip() # remove trailing characters "\n \r \t" |
| 120 | if os.path.exists(item): # check existing path |
| 121 | gcc_command = gcc_command + " -I " + item |
| 122 | fd.close() |
| 123 | |
| 124 | fd_com = nvram_gen_open_file(file_out_path, "gcc_com.mak", "w+") |
| 125 | printf("all:", file=fd_com) |
| 126 | printf("\t%s" % gcc_command+" "+out_path+"/nvram_auto_gen/nvram_editor_data_item.c"+" -o "+out_path+"/nvram_auto_gen/nvram_editor_data_item.i", file=fd_com) |
| 127 | fd_com.close() |
| 128 | |
| 129 | printf("===============win gcc file build======================") |
| 130 | printf("\".\\tools\\MSYS\\bin\\make.exe\" -f "+out_path+"/nvram_auto_gen/gcc_com.mak") |
| 131 | p = subprocess.Popen("\".\\tools\\MSYS\\bin\\make.exe\" -f "+out_path+"/nvram_auto_gen/gcc_com.mak", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 132 | outs, errs = p.communicate() |
| 133 | printf("gcc run log:") |
| 134 | if p.returncode != 0: # run fail |
| 135 | printf("gcc run fail! ") |
| 136 | printf("exit code = %d" % p.returncode) |
| 137 | printf("outs:") |
| 138 | printf(str(outs)) |
| 139 | printf("errs:") |
| 140 | printf(str(errs)) |
| 141 | else: |
| 142 | printf("gcc run pass! ") |
| 143 | printf("outs:") |
| 144 | printf(str(outs)) |
| 145 | |
| 146 | def find_include_path(): |
| 147 | """ create gcc preprocessor command and run """ |
| 148 | # create gcc preprocess command |
| 149 | option_path = os.path.join(file_out_path, "gcc_option.h") |
| 150 | gcc_command = "gcc -DGEN_FOR_PC -E -C -imacros {} -fno-strict-aliasing -fshort-enums".format(option_path) |
| 151 | |
| 152 | fd = nvram_gen_open_file(inc_file_path, "~inc.tmp.tmp", "r") |
| 153 | for each_line in fd: |
| 154 | item = each_line.strip() # remove trailing characters "\n \r \t" |
| 155 | if os.path.exists(item): # check existing path |
| 156 | gcc_command = gcc_command + " -I " + item |
| 157 | fd.close() |
| 158 | gcc_command = gcc_command + " " + file_out_path + "/nvram_editor_data_item.c" + " -o " + file_out_path + "/nvram_editor_data_item.i" |
| 159 | |
| 160 | # shell run : gcc preprocess |
| 161 | printf("===============linux gcc build======================") |
| 162 | # printf("gcc command = %s" % gcc_command) |
| 163 | p = subprocess.Popen(gcc_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 164 | outs, errs = p.communicate() |
| 165 | printf("gcc run log:") |
| 166 | if p.returncode != 0: # run fail |
| 167 | printf("gcc run fail! ") |
| 168 | printf("exit code = %d" % p.returncode) |
| 169 | printf("outs:") |
| 170 | printf(outs) |
| 171 | printf("errs:") |
| 172 | printf(errs) |
| 173 | else: |
| 174 | printf("gcc run pass! ") |
| 175 | printf("outs:") |
| 176 | printf(outs) |
| 177 | |
| 178 | |
| 179 | def parse_lid_stu_map(): |
| 180 | """ parse lid struct name in nvram_editor_data_item.i, save to lid_stu_map dict """ |
| 181 | global lid_stu_map |
| 182 | fd = nvram_gen_open_file(file_out_path, "nvram_editor_data_item.i", "r") |
| 183 | strs = fd.read() |
| 184 | fd.close() |
| 185 | |
| 186 | """ all non-overlapping matches, return string list """ |
| 187 | """ |
| 188 | string format example: |
| 189 | LID_BIT "000" NVRAM_EF_STATIC_APPLMN_LID nvram_ef_static_applmn_struct * |
| 190 | or |
| 191 | LID_BIT MULTIPLE_LID "000" NVRAM_EF_EL1_MPRADJTBL_LID nvrm_el1_mqr_struct * |
| 192 | """ |
| 193 | re_list = re.findall("LID_BIT[\s(MULTIPLE_LID)]*\"[0-9]{3}\"\s*NVRAM_EF_\w*_LID\s*\w*\s*\*", strs) |
| 194 | |
| 195 | for item in re_list: |
| 196 | strs = re.sub("LID_BIT[\s(MULTIPLE_LID)]*\"[0-9]{3}\"\s*", "", item, count=1) |
| 197 | strs = re.sub("\s*\*", "", strs, count=1) |
| 198 | dict = re.split("\s*", strs) |
| 199 | lid_stu_map[dict[1]] = dict[0] # add to dict |
| 200 | # for i in lid_stu_map.items(): |
| 201 | # printf(i) |
| 202 | |
| 203 | |
| 204 | def gen_lid_stu_file(): |
| 205 | """ gen nvram_lid_stu_tmp.h file """ |
| 206 | fd = nvram_gen_open_file(file_out_path, "nvram_lid_stu_tmp.h", "w+") |
| 207 | |
| 208 | printf("#ifndef __NVRAM_LID_STU_TMP_H__", file=fd, flush=True) |
| 209 | printf("#define __NVRAM_LID_STU_TMP_H__", file=fd, flush=True) |
| 210 | printf("", file=fd, flush=True) |
| 211 | printf("#include \"%s\"" % "nvram_group_editor.h" , file=fd, flush=True) |
| 212 | printf("#include \"%s\"" % "nvram_group_def.h", file=fd, flush=True) |
| 213 | printf("", file=fd, flush=True) |
| 214 | printf("/*\n * Variables\n */", file=fd, flush=True) |
| 215 | printf("", file=fd, flush=True) |
| 216 | for item in lid_stu_map.keys(): |
| 217 | printf("static ", file=fd, end="", flush=True) |
| 218 | printf("%s " % item, file=fd, end="", flush=True) |
| 219 | printf("%s" % item + "_var", file=fd, end="", flush=True) |
| 220 | printf(" = {0};", file=fd, end="\n", flush=True) |
| 221 | printf("", file=fd, flush=True) |
| 222 | printf("#endif", file=fd, flush=True) |
| 223 | |
| 224 | |
| 225 | def main(out_path, gcc): |
| 226 | global file_out_path |
| 227 | global info_file_path |
| 228 | global inc_file_path |
| 229 | file_out_path = os.path.join(out_path, "nvram_auto_gen") |
| 230 | info_file_path = os.path.join(out_path, "bin", "log") |
| 231 | inc_file_path = os.path.join(out_path, "tmp") |
| 232 | printf("file_out_path = %s" % file_out_path) |
| 233 | printf("info_file_path = %s" % info_file_path) |
| 234 | printf("inc_file_path = %s" % inc_file_path) |
| 235 | |
| 236 | printf("run gen_editor_data_item()") |
| 237 | # gen_editor_data_item() |
| 238 | editor_file = os.path.join(file_out_path, "nvram_editor_data_item.c") |
| 239 | with open(editor_file, "w") as fd: |
| 240 | fd.write('#include "nvram_group_editor.h"') |
| 241 | printf("run gen_gcc_option()") |
| 242 | gen_gcc_option() |
| 243 | printf("run find_include_path()") |
| 244 | if platform.system() == "Windows": # windows |
| 245 | find_include_path_win(gcc, out_path) |
| 246 | elif platform.system() == "Linux": # linux |
| 247 | find_include_path() |
| 248 | else: |
| 249 | printf("platform error: platform is not windows or linux!") |
| 250 | printf("platform.system() = %s" % platform.system()) |
| 251 | sys.exit(1) |
| 252 | printf("run parse_lid_stu_map()") |
| 253 | parse_lid_stu_map() |
| 254 | printf("run gen_lid_stu_file()") |
| 255 | gen_lid_stu_file() |
| 256 | |
| 257 | |
| 258 | if __name__ == '__main__': |
| 259 | printf("out path = %s" % argv[1]) |
| 260 | printf("gcc = %s" % argv[2]) |
| 261 | # printf("platfrom = %s" % argv[3]) |
| 262 | # if len(argv) != 4: |
| 263 | # printf("command error:") |
| 264 | # printf("Example command: startup.py ./build/MT6885_SP/NLWCTG gcc platfrom" ) |
| 265 | #else: |
| 266 | main(argv[1], argv[2]) |
| 267 | |
| 268 | |
| 269 | |