Repository navigation
Expand file tree
/
Copy pathcompiler-script.py
More file actions
executable file
·104 lines (87 loc) · 3.15 KB
/
Copy pathcompiler-script.py
File metadata and controls
executable file
·104 lines (87 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python3
#Author: Lro.Steven.aq (DustyFeather)
#License: MIT
import subprocess
import os
import re
#definations:
cc = "gcc"
level = 0
output = "./target/main"
additional_library_search_path = "."
#${LIBRARY_PATH}
stdand_library_path = "/usr/lib/gcc/x86_64-linux-gnu/15/:/usr/lib/gcc/x86_64-linux-gnu/15/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/15/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/15/../../../:/lib/:/usr/lib/"
library_dependence = ""
#enddefinations
def load_configuration_file(file_path: str) ->None:
global level, output, additional_library_search_path, library_dependence
with open(file_path,"rb") as f:
content = f.read().decode("utf-8")
for line in content.split("\n"):
res = re.search(r"\s*?ProjectName\s*?=\s*?(.*)",line)
if res:
project_name = res.group(1).strip()
output = f"./target/{project_name}"
print(f"output: {output}")
res = re.search(r"\s*?OptLevel\s*?=\s*?(\d)",line)
if res:
opt_level = res.group(1).strip()
level = int(opt_level)
print(f"level: {level}")
res = re.search(r"\s*?LibrarySearchPath\s*?=\s*?(.*)",line)
if res:
library_search_path = res.group(1).strip()
additional_library_search_path = library_search_path
print(f"addi: {additional_library_search_path}")
res = re.search(r"\s*?LibraryDependence\s*?=\s*?(.*)",line)
if res:
_library_dependence = res.group(1).strip()
library_dependence = _library_dependence
print(f"library dep: {library_dependence}")
def get_cfiles() ->list[str]:
cfiles = []
cwd = os.path.abspath(__file__ + "/..")
#
os.chdir(cwd)
#
files = os.listdir(cwd)
for file in files:
if file.endswith(".c"):
cfiles.append(file)
return cfiles
def generate_command(cfiles :list[str]) ->list[str]:
command = [
f"{cc}",
f"-O{level}",
f"-o",
f"{output}",
f"-L{additional_library_search_path}",
f"-L{stdand_library_path}"
]
if library_dependence != "" and library_dependence != "\"\"":
command.append(f"-l{library_dependence}")
# for i in cfiles:
# command.append(f"{i}")
command.extend(cfiles)
# return f"""{cc} -O{level} -o {output} {_cfiles}"""
return command
def execuate_command(command :list[str]) ->None:
result = subprocess.run(command, check=True, capture_output=True, text=True)
print(result.stdout)
print(result.stderr)
def main():
# 是否有配置文件?是否使用缺省值?
if os.path.exists(os.path.abspath(__file__ + "/../compiler-script.configuration")):
load_configuration_file("./compiler-script.configuration")
else:
# use default settings
pass
cfiles = get_cfiles()
#depend on dir: ./target
if not os.path.exists("./target"):
os.mkdir("./target")
command = generate_command(cfiles)
print(command)
execuate_command(command)
if __name__ == "__main__":
main()