1#!/usr/bin/env python
2import os, sys, re, codecs, shutil, platform
3
4# read params from file if exists
5params = sys.argv[1:]
6if os.path.isfile(sys.argv[1]):
7    with codecs.open(sys.argv[1], 'r', 'UTF-8') as fh:
8        params = fh.read().split("\n")
9    # appdend user-defined args
10    if len(sys.argv) > 2:
11        params += sys.argv[2:]
12
13# choose those arguments we wanted, and discard others
14key_value = {}
15pattern = re.compile(r'--(.+?)=(.*)')
16for arg in params:
17    arg = arg.strip()
18    if arg.startswith("--"):
19        match = pattern.match(arg)
20        if match:
21            key = match.group(1)
22            value = match.group(2)
23            if key in ["toolchain", "lib", "target", "cflag", "cxxflag", "asmflag", "ldflag", "cpu", "macro_list", "global_inc", "solution_dir", "comp_name"]:
24                key_value[key] = value
25            else:
26                print("ignore %s=%s" %(key, value))
27
28# format string
29for key in ["cflag", "cxxflag", "asmflag", "ldflag"]:
30    if key in key_value.keys():
31        key_value[key] = key_value[key].replace("#", " ")
32        # escape the string: "aos.map" -> \"aos.map\"
33        # but keep \"ALIBABA IOT\"
34        value = ""
35        ch_last = ""
36        for ch in key_value[key]:
37            if (ch == '"') and (ch_last != '\\'):
38                value += '\\'
39            value += ch
40            ch_last = ch
41        # escape the string: \"aos.map\" -> \\\"aos.map\\\"
42        key_value[key] = value.replace('\\\"', '\\\\\\\"')
43
44for key in ["macro_list", "global_inc"]:
45    if key in key_value.keys():
46        # escape the string: \"aos.map\" -> "aos.map"
47        value = key_value[key].replace("#", " ")
48        key_value[key] = value.replace('\\\"', '\"')
49
50# strip "
51for key in ["toolchain", "lib", "target", "solution_dir"]:
52    if key in key_value.keys():
53        key_value[key] = key_value[key].strip('"')
54
55print("the script is %s" % sys.argv[0])
56print("current dir is %s" % os.getcwd())
57for key in key_value.keys():
58    print("%s is %s" %(key, key_value[key]))
59
60# =======================================================
61# do our work
62
63# format file path in windows
64if platform.system() == "Windows":
65    for key in ["toolchain", "macro_list", "global_inc"]:
66        if key in key_value.keys():
67            key_value[key] = key_value[key].replace("\\", "\\\\")
68
69# update flags
70key_value["cflag"] += " --specs=nosys.specs"
71key_value["cxxflag"] += " --specs=nosys.specs"
72key_value["ldflag"] += " --specs=nosys.specs"
73
74# update add_params_from_aostools.cmake with macro and include path
75# and call cmake to build
76comp_path = os.path.dirname(sys.argv[0])
77
78macro_path = "macro_defines.h"
79f = open(macro_path, 'w')
80data_list = key_value["macro_list"].split('-D')
81
82for item in data_list:
83    k_list = item.split('=')
84    if len(k_list) < 2:
85        continue
86    head = "#ifndef " + k_list[0] + "\n"
87    f.write(head)
88    data = item.replace('=', "    ")
89    f.write("#define "+ data + "\n")
90    f.write("#endif\n\n")
91
92f.close()
93
94build_cmd = '%s-gcc %s -imacros %s -E -D__ARM__  -D__ALIGN__=4 -P %s/_haas1000_alios.c -o %s/_haas1000_alios.lds' % (key_value["toolchain"], key_value["cflag"], macro_path, comp_path, comp_path)
95print(build_cmd)
96ret = os.system(build_cmd) >> 8
97if ret != 0:
98    exit(ret)
99
100# =======================================================
101
102# result
103print("run external script success")
104exit(0)
105