• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import os
2import argparse
3
4SRC_DIRS = ["src", "crt", "ldso"]
5
6def file_search(path, target_type):
7    base_srcs = []
8    if not os.path.isdir(path):
9        return base_srcs
10    src_list = os.listdir(path)
11    for src in src_list:
12        cur_path = os.path.join(path, src)
13        if os.path.isdir(cur_path):
14            # base_srcs += file_search(cur_path, target_type)
15            continue
16        elif target_type == "header":
17            if (cur_path.endswith(".h")):
18                base_srcs.append(cur_path)
19        elif target_type == "src":
20            if (cur_path.endswith(".c")):
21                base_srcs.append(cur_path)
22    return base_srcs
23
24def arch_file_search(path, arch):
25    arch_srcs = []
26    target_path = path + "/" + arch
27    if os.path.isdir(target_path):
28        src_list = os.listdir(target_path)
29        for src in src_list:
30            cur_path = os.path.join(target_path, src)
31            if os.path.isdir(cur_path):
32                # arch_srcs += file_search(cur_path, target_type)
33                continue
34            elif (cur_path.endswith(".c") or cur_path.endswith(".s") or
35                  cur_path.endswith(".S")):
36                arch_srcs.append(cur_path)
37    return arch_srcs
38
39def get_base_srcs(path):
40    base_srcs = []
41    for src_dir in SRC_DIRS:
42        src_path = path + "/" + src_dir
43        src_list = os.listdir(src_path)
44        for src in src_list:
45            cur_path = os.path.join(src_path, src)
46            if os.path.isdir(cur_path) and src_dir == "src":
47                base_srcs += file_search(cur_path, "src")
48            else:
49                if (cur_path.endswith(".c")):
50                    base_srcs.append(cur_path)
51    base_srcs.sort()
52    return base_srcs
53
54def get_arch_srcs(path, arch):
55    arch_srcs = []
56    for src_dir in SRC_DIRS:
57        if src_dir == "src":
58            src_list = os.listdir(path + "/" + src_dir)
59            for src in src_list:
60                cur_path = os.path.join(path + "/" + src_dir, src)
61                arch_srcs += arch_file_search(cur_path, arch)
62        elif os.path.isdir(path + "/" + src_dir + "/" + arch):
63            src_path = path + "/" + src_dir + "/" + arch
64            src_list = os.listdir(src_path)
65            for src in src_list:
66                cur_path = os.path.join(src_path, src)
67                if os.path.isdir(cur_path):
68                    continue
69                else:
70                    if (cur_path.endswith(".c") or cur_path.endswith(".s") or
71                        cur_path.endswith(".S")):
72                        arch_srcs.append(cur_path)
73    arch_srcs.sort()
74    return arch_srcs
75
76def change_subfix(name):
77    new_name = ""
78    if name.endswith(".s"):
79        new_name = name.replace(".s", ".c")
80    elif name.endswith(".S"):
81        new_name = name.replace(".S", ".c")
82    else:
83        return name
84    return new_name
85
86def replace_srcs_with_arch(base_srcs, arch_srcs, arch):
87    target_srcs = base_srcs
88    arch_removed = []
89    for src in arch_srcs:
90        fake_name = change_subfix(src.replace("/" + arch, ""))
91        if fake_name in target_srcs:
92            target_srcs.remove(fake_name)
93            arch_removed.append(src)
94    return target_srcs, arch_removed
95
96def get_libc_srcs(target_srcs, relative_path):
97    libc_srcs = []
98    for src in target_srcs:
99        clean_path_src = src.replace(relative_path, "")
100        if clean_path_src.startswith("/src/") or clean_path_src.startswith("/compat/time32/"):
101            libc_srcs.append(src)
102    return libc_srcs
103
104def get_ldso_srcs(target_srcs, relative_path):
105    ldso_srcs = []
106    for src in target_srcs:
107        clean_path_src = src.replace(relative_path, "")
108        if clean_path_src.startswith("/ldso/"):
109            ldso_srcs.append(src)
110    return ldso_srcs
111
112def get_crt_srcs(target_srcs, relative_path):
113    crt_srcs = []
114    for src in target_srcs:
115        clean_path_src = src.replace(relative_path, "")
116        if clean_path_src.startswith("/crt/"):
117            crt_srcs.append(src)
118    return crt_srcs
119
120def get_header_dirs(path, arch):
121    header_dirs = {
122        path + "/arch/" + arch + "/bits",
123        path + "/arch/generic/bits",
124        path + "/include"
125    }
126    return header_dirs
127
128def get_generated_header(path):
129    generated_headers = [
130        path + "/include/bits/alltypes.h",
131        path + "/include/bits/syscall.h",
132    ]
133
134def get_all_header(header_dirs):
135    all_header = []
136    for dir in header_dirs:
137        file_list = os.listdir(dir)
138        for file in file_list:
139            cur_path = os.path.join(dir, file)
140            if os.path.isdir(cur_path):
141                all_header += file_search(cur_path, "header")
142            else:
143                if (cur_path.endswith(".h")):
144                    all_header.append(cur_path)
145    all_header.sort()
146    return all_header
147
148def rm_dup_header(all_header_files, arch):
149    header_files = all_header_files
150    header_to_rm = []
151    for file in header_files:
152        if "/arch/generic/bits" in file:
153            substitute_file = file.replace("/arch/generic/bits", "/arch/"+ arch +"/bits")
154            if substitute_file in header_files:
155                header_to_rm.append(file)
156
157    return  [i for i in header_files if i not in header_to_rm]
158
159def print_outputs(outputs):
160    for out in outputs:
161        print(out)
162        for content in outputs[out]:
163            print("    " + content)
164
165def search_for_files(path, arch):
166    if arch == "arm":
167        SRC_DIRS.append("compat/time32")
168
169    base_srcs = get_base_srcs(path)
170    arch_srcs = get_arch_srcs(path, arch)
171    target_srcs, arch_removed = replace_srcs_with_arch(base_srcs, arch_srcs, arch)
172    target_srcs += arch_srcs
173
174    libc_srcs = get_libc_srcs(target_srcs, path)
175    ldso_srcs = get_ldso_srcs(target_srcs, path)
176    crt_srcs = get_crt_srcs(target_srcs, path)
177
178    all_header_files = get_all_header(get_header_dirs(path, arch))
179    header_files = rm_dup_header(all_header_files, arch)
180
181    outputs = {
182        "arch": arch_srcs,
183        "arch_rm": arch_removed,
184        "libc": libc_srcs,
185        "ldso": ldso_srcs,
186        "crt": crt_srcs,
187        "header": header_files,
188    }
189    # print_outputs(outputs)
190    return libc_srcs, ldso_srcs, crt_srcs, header_files
191
192def main():
193    parser = argparse.ArgumentParser(description=__doc__)
194
195    parser.add_argument('--path',
196                        required = True,
197                        help = 'The path of musl source files')
198    parser.add_argument('--arch',
199                        required = True,
200                        help = 'The targeting architecture')
201    args = parser.parse_args()
202
203    return search_for_files(args.path, args.arch)
204
205
206if __name__ == "__main__":
207    main()
208