This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.py
123 lines (93 loc) · 3 KB
/
build.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
import glob
import shutil
import subprocess
import os
import sys
def err(message: str):
print(f"Error: {message}")
sys.exit(1)
# Get the target region from the command line
if len(sys.argv) < 2:
err("Did not specify a target region.")
region = sys.argv[1]
# Check if CodeWarrior and Kamek are present in the toolchain
if not os.path.exists("deps/CodeWarrior/mwcceppc.exe"):
err("CodeWarrior compiler not found.")
if not os.path.exists("deps/Kamek/Kamek.exe"):
err("Kamek linker not found.")
# Define the compiler options and command
compiler_options = [
"-i .",
"-I-",
"-i include",
"-nodefaults",
"-proc gekko",
"-Cpp_exceptions off",
"-enum int",
"-O4,s",
"-fp hard",
"-func_align 4",
"-str pool",
"-sdata 0",
"-sdata2 0",
f"-D{region}",
"-DGEKKO",
"-DMTX_USE_PS",
"-MMD",
"-rtti off",
"-c",
"-o"
]
assembler_options = [
"-i .",
"-I-",
"-i include",
"-proc gekko",
"-c",
f"-D{region}",
"-o"
]
command = "deps\CodeWarrior\mwcceppc.exe " + " ".join(compiler_options)
asm_cmd = "deps\CodeWarrior\mwasmeppc.exe " + " ".join(assembler_options)
# Clean the entire build folder first if it exists
if os.path.exists("build"):
shutil.rmtree("build", ignore_errors=True)
# Fetch all source C++ files that need to be compiled
tasks = list()
asm_tasks = list()
for root, dirs, files in os.walk("source"):
for file in files:
if file.endswith(".cpp"):
source_path = os.path.join(root, file)
build_path = source_path.replace("source", "build").replace(".cpp", ".o")
os.makedirs(os.path.dirname(build_path), exist_ok=True)
tasks.append((source_path, build_path))
elif file.endswith(".s"):
source_path = os.path.join(root, file)
build_path = source_path.replace("source", "build").replace(".s", ".o")
os.makedirs(os.path.dirname(build_path), exist_ok=True)
asm_tasks.append((source_path, build_path))
if len(tasks) < 1:
err("No C++ files to compile!")
# Process all compile tasks
for task in tasks:
source_path, build_path = task
print(f"Compiling {source_path}...")
if subprocess.call(f"{command} {build_path} {source_path}", shell=True) != 0:
err("Compiler error.")
for a_task in asm_tasks:
source_path, build_path = a_task
print(f"Assembling {source_path}...")
if subprocess.call(f"{asm_cmd} {build_path} {source_path}", shell=True) != 0:
err("Assembler error.")
# Link all object files and create the CustomCode binary
print("Linking...")
object_files = " ".join([task[1] for task in tasks])
asm_obj_files = " ".join([a_task[1] for a_task in asm_tasks])
kamek_cmd = f"deps\Kamek\Kamek.exe {object_files} {asm_obj_files} -externals=symbols/{region}.txt -output-kamek=CustomCode_{region}.bin"
if subprocess.call(kamek_cmd, shell=True) != 0:
err("Linking failed.")
# Remove all useless "d" files
for f in glob.glob("*.d"):
os.remove(f)
print("Done!")