-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigure.py
executable file
·189 lines (145 loc) · 5.17 KB
/
configure.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
from pathlib import Path
import subprocess
import sys
import hashlib
import time
# This file is used to generate a Makefile for the project.
CC = "i686-elf-gcc"
CFLAGS = []
CFLAGS.append("-ffreestanding")
CFLAGS.append("-flto")
CFLAGS.append("-I.")
CFLAGS.append("-nostdlib")
CFLAGS.append("-Ofast")
CFLAGS.append("-pedantic")
CFLAGS.append("-std=c99")
CFLAGS.append("-Wall")
CFLAGS.append("-Wextra")
CXX = "i686-elf-g++"
CXXFLAGS = []
CXXFLAGS.append("-ffreestanding")
CXXFLAGS.append("-flto")
CXXFLAGS.append("-fno-exceptions")
CXXFLAGS.append("-fno-rtti")
CXXFLAGS.append("-I.")
CXXFLAGS.append("-Ofast")
CXXFLAGS.append("-pedantic")
CXXFLAGS.append("-std=c++17")
CXXFLAGS.append("-Wall")
CXXFLAGS.append("-Wextra")
print(f"""
# Kernel Makefile
# Generated by configure.py at {time.strftime("%Y-%m-%d %H:%M:%S")}
# Do not edit this file manually.
CC = {CC}
CXX = {CXX}
CFLAGS = {' '.join(CFLAGS)}
CXXFLAGS = {' '.join(CXXFLAGS)}
all: leonardo.bin tar-drive.tar
.PHONY: all
""")
def sha256(s: str) -> str:
return hashlib.sha256(s.encode("utf-8")).hexdigest()
def relative(p: Path) -> Path:
return p.resolve().relative_to(Path.cwd())
OBJ_FILES = []
def get_c_dependencies(file: Path) -> list[str]:
"""Get the dependencies of a C file."""
res = subprocess.run([CC, *CFLAGS, "-M", file], capture_output=True)
if res.returncode != 0:
print("Error getting dependencies for ", file)
print(res.stderr.decode("utf-8"))
sys.exit(1)
deps = res.stdout.decode("utf-8").split(" ")[2:]
deps = [d.strip() for d in deps]
deps = [d for d in deps if d != "\\"]
# Filter absolute
deps = [d for d in deps if not d.startswith("/")]
deps = [str(relative(Path(d).resolve())) for d in deps]
return deps
c_files = []
c_files += Path("libk").glob("**/*.c")
for c_file in c_files:
c_file = relative(c_file)
path = str(c_file)
OBJ_FILES.append(f"build/{sha256(path)}")
print(f"build/{sha256(path)}", ":", path, " ".join(get_c_dependencies(c_file)))
print(f"\t$(CC) $(CFLAGS) -c {path} -o $@")
def get_cpp_dependencies(file: Path) -> list[str]:
"""Get the dependencies of a C++ file."""
res = subprocess.run([CXX, *CXXFLAGS, "-M", file], capture_output=True)
if res.returncode != 0:
print("Error getting dependencies for ", file)
print(res.stderr.decode("utf-8"))
sys.exit(1)
deps = res.stdout.decode("utf-8").split(" ")[2:]
deps = [d.strip() for d in deps]
deps = [d for d in deps if d != "\\"]
# Filter absolute
deps = [d for d in deps if not d.startswith("/")]
deps = [str(relative(Path(d).resolve())) for d in deps]
return deps
cpp_files = []
cpp_files += Path("arch").glob("**/*.cpp")
cpp_files += Path("kernel").glob("**/*.cpp")
cpp_files += Path("libk").glob("**/*.cpp")
cpp_files += [Path("kernel.cpp")]
for cpp_file in cpp_files:
cpp_file = relative(cpp_file)
path = str(cpp_file)
OBJ_FILES.append(f"build/{sha256(path)}")
print(f"build/{sha256(path)}", ":", path, " ".join(get_cpp_dependencies(cpp_file)))
print(f"\t$(CXX) -c {path} -o build/{sha256(path)} $(CXXFLAGS)")
nasm_files = []
nasm_files += Path("arch").glob("**/*.asm")
nasm_files += [Path("boot.asm")]
for nasm_file in nasm_files:
nasm_file = relative(nasm_file)
path = str(nasm_file)
OBJ_FILES.append(f"build/{sha256(path)}")
print(f"build/{sha256(path)}", ":", path)
print(f"\tnasm -felf32 {path} -o build/{sha256(path)}")
gas_files = []
gas_files += Path("kernel").glob("**/*.s")
for gas_file in gas_files:
gas_file = relative(gas_file)
path = str(gas_file)
OBJ_FILES.append(f"build/{sha256(path)}")
print(f"build/{sha256(path)}", ":", path)
print(f"\ti686-elf-as {path} -o build/{sha256(path)}")
no_flto_c_files = []
no_flto_c_files += Path("no_flto_c").glob("**/*.c")
for c_file in no_flto_c_files:
c_file = relative(c_file)
path = str(c_file)
flags = CFLAGS.copy()
flags.remove("-flto")
OBJ_FILES.append(f"build/{sha256(path)}")
print(f"build/{sha256(path)}", ":", path, " ".join(get_c_dependencies(c_file)))
print(f"\t{CC} -c {path} -o build/{sha256(path)} {' '.join(flags)}")
no_flto_cpp_files = []
no_flto_cpp_files += Path("no_flto_cpp").glob("**/*.cpp")
for cpp_file in no_flto_cpp_files:
cpp_file = relative(cpp_file)
path = str(cpp_file)
flags = CXXFLAGS.copy()
flags.remove("-flto")
OBJ_FILES.append(f"build/{sha256(path)}")
print(f"build/{sha256(path)}", ":", path, " ".join(get_cpp_dependencies(cpp_file)))
print(f"\t{CXX} -c {path} -o build/{sha256(path)} {' '.join(flags)}")
print(f"leonardo.bin: {' '.join(OBJ_FILES)}")
print(f"\t{CC} -T linker.ld -o leonardo.bin {' '.join(CFLAGS)} {' '.join(OBJ_FILES)}")
OBJ_FILES.append("leonardo.bin")
# tar drive
tar_files = []
for f in Path("tar-drive").glob("**/*"):
tar_files.append(relative(f))
print(f"tar-drive.tar: {' '.join([str(x) for x in tar_files])}")
print(f"\ttar -cvf tar-drive.tar -C tar-drive {' '.join([str(f.relative_to(Path('tar-drive'))) for f in tar_files])}")
OBJ_FILES.append("tar-drive.tar")
# Clean rule
print("clean:")
for obj_file in OBJ_FILES:
print(f"\trm -f {obj_file}")
print(".PHONY: clean")