WKern
Loading...
Searching...
No Matches
brules.py
Go to the documentation of this file.
13
14import os
15import subprocess
16import shutil
17import sys
18from bfiles.files import * # Source file lists and structure
19from bfiles.toolsflags import * # Compiler/linker tools and flags
20from pathlib import Path
21from concurrent.futures import ThreadPoolExecutor, as_completed
22
23
31 os.chdir(Path(__file__).resolve().parents[1])
32 print("Working from:", os.getcwd())
33 for file in NASMSRCS:
34 obj = "objs/" + file.replace(".asm", ".o")
35 src_time = os.path.getmtime(file)
36 obj_time = os.path.getmtime(obj) if os.path.exists(obj) else -1
37
38 if src_time <= obj_time:
39 continue
40
41 print(f"Assembling {file}")
42 try:
43 subprocess.run([AS, *ASFLAGS, file, "-o", obj], check=True)
44 except subprocess.CalledProcessError as e:
45 print(f"Failed with code {e.returncode}", file=sys.stderr)
46 sys.exit(1)
47
48
53def compile_file(file):
54 obj = "objs/" + file.replace(".c", ".o")
55 src_time = os.path.getmtime(file)
56 obj_time = os.path.getmtime(obj) if os.path.exists(obj) else -1
57
58 if src_time <= obj_time:
59 return "0" # Indicates no work needed
60
61 print(f"Compiling {file}")
62 try:
63 subprocess.run([CC, *CCFLAGS, "-c", file, "-o", obj], check=True)
64 return f"Compiled {file}"
65 except subprocess.CalledProcessError as e:
66 return f"Failed {file} with code {e.returncode}"
67
68
74 os.chdir(Path(__file__).resolve().parents[1])
75 print("Working from:", os.getcwd())
76
77 with ThreadPoolExecutor(max_workers=8) as executor:
78 futures = {executor.submit(compile_file, f): f for f in SRCS}
79
80 for future in as_completed(futures):
81 result = future.result()
82 if result.startswith("Failed"):
83 print(result, file=sys.stderr)
84 sys.exit(1)
85 if result != "0":
86 print(result)
87
88
96 os.chdir(Path(__file__).resolve().parents[1])
97 os.makedirs("objs/modules", exist_ok=True)
98
99 for modname, files in MODULES.items():
100 obj_files = [f"objs/{src.replace('.c', '.o')}" for src in files]
101 output = f"objs/modules/{modname}.o"
102
103 if os.path.exists(output):
104 out_time = os.path.getmtime(output)
105 if all(os.path.exists(f) and os.path.getmtime(f) <= out_time for f in obj_files):
106 print(f"Module {modname} is up to date")
107 continue
108
109 print(f"Linking module: {modname}")
110 try:
111 subprocess.run(["i686-elf-ld", "-r", "-o", output, *obj_files], check=True)
112 except subprocess.CalledProcessError as e:
113 print(f"Error linking module {modname}: {e}", file=sys.stderr)
114 sys.exit(1)
115
116
121def link():
122 os.chdir(Path(__file__).resolve().parents[1])
123 print("Working from:", os.getcwd())
124
125 if os.path.exists(OUT):
126 out_time = os.path.getmtime(OUT)
127 if all(os.path.exists(obj) and os.path.getmtime(obj) <= out_time for obj in OBJS):
128 print("Kernel is up to date")
129 return
130
131 print("Linking kernel")
132 try:
133 subprocess.run(["i686-elf-ld", *LDFLAGS, "-T", "link.ld", "-o", OUT, *OBJS], check=True)
134 except subprocess.CalledProcessError as e:
135 print(f"Final link failed: {e}", file=sys.stderr)
136 sys.exit(1)
137
138
143def iso():
144 os.chdir(Path(__file__).resolve().parents[1])
145 src_time = os.path.getmtime("kernel.elf")
146 obj_time = os.path.getmtime(ISO) if os.path.exists(ISO) else -1
147 if src_time <= obj_time:
148 return "0" # ISO is already up to date
149
150 print("Working from:", os.getcwd())
151 print("Making ISO")
152 shutil.copy(GRUBCFG, GRUBCFGTARG)
153 shutil.copy(OUT, OUTARG)
154 subprocess.run(
155 [GRUB, "-o", ISO, "iso"],
156 stdout=subprocess.DEVNULL,
157 stderr=subprocess.DEVNULL
158 )
compilec()
Compiles all C sources in parallel from SRCS.
Definition brules.py:73
link()
Links the final kernel ELF binary from object files.
Definition brules.py:121
partial_link()
Performs partial linking on modules from MODULES.
Definition brules.py:95
compileasm()
Compiles all NASM assembly files in NASMSRCS.
Definition brules.py:30
iso()
Generates a bootable ISO using GRUB and kernel ELF.
Definition brules.py:143
compile_file(file)
Compile a single C file if out of date.
Definition brules.py:53