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
|
#!/usr/bin/env python
# SPDX-FileCopyrightText: © 2026 ZeldaRET
# SPDX-License-Identifier: CC0-1.0
#
# Create a single-file armips assembler from an upstream commit, for MIPS only.
# Changing the target commit may require updating
# - armips.diff
# - the included file list
# - the line filters in filter_lines
#
import contextlib, io, os
ARMIPS_URL = "https://github.com/kingcom/armips"
ARMIPS_COMMIT = "9029577f6448c78dadcffbc876d2b8cbcfc6da9b"
ARMIPS_MAJOR_VER = "v0.11"
ARMIPS_PATCH = "armips.diff"
ARMIPS_OUTPUT = "armips.cpp"
file_list = (
"Util/FileSystem.h",
"ext/tinyformat/tinyformat.h",
"Core/Types.h",
"Core/Types.cpp",
"Commands/CAssemblerCommand.h",
"Core/Expression.h",
"Parser/Tokenizer.h",
"Core/ExpressionFunctionHandler.h",
"Core/ExpressionFunctions.h",
"Core/SymbolData.h",
"Util/Util.h",
"Util/FileClasses.h",
"Util/ByteArray.h",
"Core/FileManager.h",
"Core/ELF/ElfTypes.h",
"Core/ELF/ElfFile.h",
"Core/ELF/ElfRelocator.h",
"Archs/Architecture.h",
"Archs/MIPS/Mips.h",
"Archs/MIPS/MipsOpcodes.h",
"Archs/MIPS/CMipsInstruction.h",
"Util/EncodingTable.h",
"Core/Misc.h",
"Core/Assembler.h",
"Core/SymbolTable.h",
"Core/Common.h",
"Core/Allocations.h",
"Core/Allocations.cpp",
"Parser/DirectivesParser.h",
"Archs/MIPS/MipsMacros.h",
"Archs/MIPS/MipsParser.h",
"Archs/MIPS/CMipsInstruction.cpp",
"Archs/MIPS/MipsExpressionFunctions.h",
"Archs/MIPS/MipsElfRelocator.h",
"Archs/MIPS/Mips.cpp",
"Archs/MIPS/MipsElfFile.h",
"Util/CRC.h",
"Archs/MIPS/MipsElfFile.cpp",
"Commands/CommandSequence.h",
"Parser/Parser.h",
"Archs/MIPS/MipsElfRelocator.cpp",
"Archs/MIPS/MipsExpressionFunctions.cpp",
"Archs/MIPS/MipsMacros.cpp",
"Archs/MIPS/MipsOpcodes.cpp",
"Parser/ExpressionParser.h",
"Core/ExpressionFunctionHandler.cpp",
"Archs/MIPS/PsxRelocator.h",
"Commands/CDirectiveFile.h",
"Archs/MIPS/MipsParser.cpp",
"Archs/MIPS/PsxRelocator.cpp",
"Archs/Architecture.cpp",
"Commands/CAssemblerCommand.cpp",
"Commands/CAssemblerLabel.h",
"Commands/CAssemblerLabel.cpp",
"Commands/CDirectiveArea.h",
"Commands/CDirectiveArea.cpp",
"Commands/CDirectiveConditional.h",
"Commands/CDirectiveConditional.cpp",
"Commands/CDirectiveData.h",
"Commands/CDirectiveData.cpp",
"Commands/CDirectiveFile.cpp",
"Commands/CDirectiveMessage.h",
"Commands/CDirectiveMessage.cpp",
"Commands/CommandSequence.cpp",
"Parser/DirectivesParser.cpp",
"Parser/ExpressionParser.cpp",
"Parser/Parser.cpp",
"Parser/Tokenizer.cpp",
"Util/ByteArray.cpp",
"Util/CRC.cpp",
"Util/EncodingTable.cpp",
"Util/FileClasses.cpp",
"Util/Util.cpp",
"Main/CommandLineInterface.h",
"Main/CommandLineInterface.cpp",
"Core/ELF/ElfFile.cpp",
"Core/ELF/ElfRelocator.cpp",
"Core/Assembler.cpp",
"Core/Common.cpp",
"Core/Expression.cpp",
"Core/ExpressionFunctions.cpp",
"Core/FileManager.cpp",
"Core/Misc.cpp",
"Core/SymbolData.cpp",
"Core/SymbolTable.cpp",
"Main/main.cpp",
)
file_header = \
f"""// == AUTOMATICALLY GENERATED by armips_gen.py, do not edit directly ==
// armips assembler {ARMIPS_MAJOR_VER} (commit {ARMIPS_COMMIT})
// {ARMIPS_URL}
// To simplify compilation, all files have been concatenated into one.
// MIPS only, other architectures not included.\n\n"""
@contextlib.contextmanager
def working_dir(x: str):
old = os.getcwd()
os.chdir(x)
try:
yield
finally:
os.chdir(old)
def filter_lines(line: str):
blacklist = (
"#pragma once",
"#include \"",
"#include <tinyformat.h>"
)
return not any(s in line for s in blacklist)
def cat_file(fout: io.TextIOBase, fin_name: str):
with open(fin_name) as fin:
lines = fin.readlines()
lines = map(str.rstrip, filter(filter_lines, lines))
fout.write("\n".join(lines) + "\n")
def combine_armips(fout_name: str, armips_path: str):
with open(fout_name, "w") as fout:
fout.write(file_header)
fout.write("/*\n")
cat_file(fout, os.path.join(armips_path, "LICENSE.txt"))
fout.write("*/\n\n")
fout.write("#define ARMIPS_USE_STD_FILESYSTEM\n")
for f in file_list:
fout.write(f"\n#pragma region \"File: {f}\"\n")
cat_file(fout, os.path.join(armips_path, f))
def main():
with working_dir(os.path.dirname(os.path.realpath(__file__))):
temp_dir = "armips_clone"
# Acquire the base armips commit
os.system(f"rm -rf {temp_dir}")
os.system(f"git init {temp_dir} 2>/dev/null")
with working_dir(temp_dir):
os.system(f"git remote add origin {ARMIPS_URL}.git")
os.system(f"git fetch origin --depth 1 {ARMIPS_COMMIT}")
os.system(f"git checkout FETCH_HEAD 2>/dev/null")
# Apply patch
os.system(f"git apply ../{ARMIPS_PATCH}")
# Concat all included files
print(f"Creating {ARMIPS_OUTPUT}")
combine_armips(ARMIPS_OUTPUT, os.path.expanduser(temp_dir))
os.system(f"rm -rf {temp_dir}")
if __name__ == "__main__":
main()
|