| import os |
| import subprocess |
| import re |
| import sys |
| import platform |
| from sys import argv |
| |
| file_out_path = "" |
| editor_file_path = [os.path.join("./interface/service/nvram"),os.path.join( "./custom/protocol/common/ps")] |
| info_file_path = os.path.join(file_out_path, "bin", "log") |
| inc_file_path = os.path.join(file_out_path, "tmp") |
| editor_file = [] |
| def_file = [] |
| lid_stu_map = {} |
| |
| def nvram_gen_open_file(file_path, file_name, mode="r"): |
| file_pathname = os.path.join(file_path, file_name) |
| if mode == "r": |
| # file not exist. |
| if not os.access(file_pathname, os.F_OK): |
| printf("error: %s not exist." % file_pathname) |
| sys.exit(2) |
| # file not read. |
| if not os.access(file_pathname, os.R_OK): |
| printf("error: %s exist, but it cannot be read." % file_pathname) |
| sys.exit(2) |
| elif mode == "w+": |
| # floder not write. |
| if not os.access(file_path, os.F_OK): |
| printf("error: folder no exist: %s" % file_path) |
| sys.exit(2) |
| try: |
| fd = open(file_pathname, mode) |
| except: |
| printf("error: open file error.") |
| printf("file_pathname: %s , mode: %s" % (file_pathname, mode)) |
| raise |
| |
| return fd |
| |
| def printf(strs, file=sys.stdout, end="\n", flush=False): |
| strs_val = strs + end |
| file.write(strs_val) |
| if flush: |
| file.flush() |
| |
| |
| def gen_editor_data_item(): |
| """ parse editor file name, create and write nvram_editor_data_item.c """ |
| global editor_file |
| """ list containing the names of the entries in the directory given by path. """ |
| for editor in editor_file_path: |
| if not os.access(editor, os.F_OK): |
| printf("error: %s not exist." % editor) |
| sys.exit(2) |
| if not os.access(editor, os.R_OK): |
| printf("error: %s exist, but it cannot be read." % editor) |
| sys.exit(2) |
| dirs = os.listdir(editor) |
| """ check filename, find editor files name saved to list. """ |
| editor_pat = re.compile("nvram_editor\.h$") |
| def_pat = re.compile("nvram_def\.h$") |
| for item in dirs: |
| if editor_pat.search(item): |
| editor_file.append(item) |
| if def_pat.search(item): |
| def_file.append(item) |
| |
| editor_file.append("nvram_editor_data_item.h") |
| editor_file.sort() |
| """ write include file to file. """ |
| fd = nvram_gen_open_file(file_out_path, "nvram_editor_data_item.c", "w+") |
| for item in editor_file: |
| printf("#include \"%s\"" % item, file=fd, flush=True) |
| |
| fd.close() |
| |
| |
| def gen_gcc_option(): |
| """ parse macro in info.log, save to list, write macro to gcc_option.h """ |
| define_data = [] |
| """ parse macro in info.log """ |
| fd = nvram_gen_open_file(info_file_path, "info.log", "r") |
| for each_line in fd: |
| if each_line.isspace(): |
| continue |
| if each_line.count("[ COMMON INCLUDE PATH ]") != 0: |
| break |
| if (each_line.count("[") != 0) or (each_line.count("]") != 0): |
| continue |
| item = each_line.replace("=", " ") |
| item = item.strip() # remove trailing characters "\n \r \t" |
| define_data.append(item) |
| fd.close() |
| # add compile option for nvram gen |
| define_data.append("__NVRAM_INTERNAL_LID_DESCRIPTION_ENABLE__") |
| define_data.append("__NV_GEN_FULL_LID__") |
| |
| """ write define macro to file. """ |
| fd = nvram_gen_open_file(file_out_path, "gcc_option.h", "w+") |
| printf("#ifndef __GCC_OPTION_H__", file=fd, flush=True) |
| printf("#define __GCC_OPTION_H__", file=fd, flush=True) |
| printf("", file=fd, flush=True) |
| |
| for item in define_data: |
| printf("#define %s" % item, file=fd, flush=True) |
| |
| printf("", file=fd, flush=True) |
| printf("#endif", file=fd, flush=True) |
| fd.close() |
| |
| def find_include_path_win(gcc, out_path): |
| """ create gcc preprocessor command and run (win)""" |
| # create gcc preprocess command |
| gcc_command = "\"./" + gcc + "\"" + " -DGEN_FOR_PC -E -imacros gcc_option.h -fno-strict-aliasing -fshort-enums" |
| |
| fd = nvram_gen_open_file(inc_file_path, "~inc.tmp.tmp", "r") |
| for each_line in fd: |
| item = each_line.strip() # remove trailing characters "\n \r \t" |
| if os.path.exists(item): # check existing path |
| gcc_command = gcc_command + " -I " + item |
| fd.close() |
| |
| fd_com = nvram_gen_open_file(file_out_path, "gcc_com.mak", "w+") |
| printf("all:", file=fd_com) |
| 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) |
| fd_com.close() |
| |
| printf("===============win gcc file build======================") |
| printf("\".\\tools\\MSYS\\bin\\make.exe\" -f "+out_path+"/nvram_auto_gen/gcc_com.mak") |
| 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) |
| outs, errs = p.communicate() |
| printf("gcc run log:") |
| if p.returncode != 0: # run fail |
| printf("gcc run fail! ") |
| printf("exit code = %d" % p.returncode) |
| printf("outs:") |
| printf(str(outs)) |
| printf("errs:") |
| printf(str(errs)) |
| else: |
| printf("gcc run pass! ") |
| printf("outs:") |
| printf(str(outs)) |
| |
| def find_include_path(): |
| """ create gcc preprocessor command and run """ |
| # create gcc preprocess command |
| option_path = os.path.join(file_out_path, "gcc_option.h") |
| gcc_command = "gcc -DGEN_FOR_PC -E -C -imacros {} -fno-strict-aliasing -fshort-enums".format(option_path) |
| |
| fd = nvram_gen_open_file(inc_file_path, "~inc.tmp.tmp", "r") |
| for each_line in fd: |
| item = each_line.strip() # remove trailing characters "\n \r \t" |
| if os.path.exists(item): # check existing path |
| gcc_command = gcc_command + " -I " + item |
| fd.close() |
| gcc_command = gcc_command + " " + file_out_path + "/nvram_editor_data_item.c" + " -o " + file_out_path + "/nvram_editor_data_item.i" |
| |
| # shell run : gcc preprocess |
| printf("===============linux gcc build======================") |
| # printf("gcc command = %s" % gcc_command) |
| p = subprocess.Popen(gcc_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| outs, errs = p.communicate() |
| printf("gcc run log:") |
| if p.returncode != 0: # run fail |
| printf("gcc run fail! ") |
| printf("exit code = %d" % p.returncode) |
| printf("outs:") |
| printf(outs) |
| printf("errs:") |
| printf(errs) |
| else: |
| printf("gcc run pass! ") |
| printf("outs:") |
| printf(outs) |
| |
| |
| def parse_lid_stu_map(): |
| """ parse lid struct name in nvram_editor_data_item.i, save to lid_stu_map dict """ |
| global lid_stu_map |
| fd = nvram_gen_open_file(file_out_path, "nvram_editor_data_item.i", "r") |
| strs = fd.read() |
| fd.close() |
| |
| """ all non-overlapping matches, return string list """ |
| """ |
| string format example: |
| LID_BIT "000" NVRAM_EF_STATIC_APPLMN_LID nvram_ef_static_applmn_struct * |
| or |
| LID_BIT MULTIPLE_LID "000" NVRAM_EF_EL1_MPRADJTBL_LID nvrm_el1_mqr_struct * |
| """ |
| re_list = re.findall("LID_BIT[\s(MULTIPLE_LID)]*\"[0-9]{3}\"\s*NVRAM_EF_\w*_LID\s*\w*\s*\*", strs) |
| |
| for item in re_list: |
| strs = re.sub("LID_BIT[\s(MULTIPLE_LID)]*\"[0-9]{3}\"\s*", "", item, count=1) |
| strs = re.sub("\s*\*", "", strs, count=1) |
| dict = re.split("\s*", strs) |
| lid_stu_map[dict[1]] = dict[0] # add to dict |
| # for i in lid_stu_map.items(): |
| # printf(i) |
| |
| |
| def gen_lid_stu_file(): |
| """ gen nvram_lid_stu_tmp.h file """ |
| fd = nvram_gen_open_file(file_out_path, "nvram_lid_stu_tmp.h", "w+") |
| |
| printf("#ifndef __NVRAM_LID_STU_TMP_H__", file=fd, flush=True) |
| printf("#define __NVRAM_LID_STU_TMP_H__", file=fd, flush=True) |
| printf("", file=fd, flush=True) |
| printf("#include \"%s\"" % "nvram_group_editor.h" , file=fd, flush=True) |
| printf("#include \"%s\"" % "nvram_group_def.h", file=fd, flush=True) |
| printf("", file=fd, flush=True) |
| printf("/*\n * Variables\n */", file=fd, flush=True) |
| printf("", file=fd, flush=True) |
| for item in lid_stu_map.keys(): |
| printf("static ", file=fd, end="", flush=True) |
| printf("%s " % item, file=fd, end="", flush=True) |
| printf("%s" % item + "_var", file=fd, end="", flush=True) |
| printf(" = {0};", file=fd, end="\n", flush=True) |
| printf("", file=fd, flush=True) |
| printf("#endif", file=fd, flush=True) |
| |
| |
| def main(out_path, gcc): |
| global file_out_path |
| global info_file_path |
| global inc_file_path |
| file_out_path = os.path.join(out_path, "nvram_auto_gen") |
| info_file_path = os.path.join(out_path, "bin", "log") |
| inc_file_path = os.path.join(out_path, "tmp") |
| printf("file_out_path = %s" % file_out_path) |
| printf("info_file_path = %s" % info_file_path) |
| printf("inc_file_path = %s" % inc_file_path) |
| |
| printf("run gen_editor_data_item()") |
| # gen_editor_data_item() |
| editor_file = os.path.join(file_out_path, "nvram_editor_data_item.c") |
| with open(editor_file, "w") as fd: |
| fd.write('#include "nvram_group_editor.h"') |
| printf("run gen_gcc_option()") |
| gen_gcc_option() |
| printf("run find_include_path()") |
| if platform.system() == "Windows": # windows |
| find_include_path_win(gcc, out_path) |
| elif platform.system() == "Linux": # linux |
| find_include_path() |
| else: |
| printf("platform error: platform is not windows or linux!") |
| printf("platform.system() = %s" % platform.system()) |
| sys.exit(1) |
| printf("run parse_lid_stu_map()") |
| parse_lid_stu_map() |
| printf("run gen_lid_stu_file()") |
| gen_lid_stu_file() |
| |
| |
| if __name__ == '__main__': |
| printf("out path = %s" % argv[1]) |
| printf("gcc = %s" % argv[2]) |
| # printf("platfrom = %s" % argv[3]) |
| # if len(argv) != 4: |
| # printf("command error:") |
| # printf("Example command: startup.py ./build/MT6885_SP/NLWCTG gcc platfrom" ) |
| #else: |
| main(argv[1], argv[2]) |
| |
| |
| |