-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmakefile.py
More file actions
410 lines (332 loc) · 14.6 KB
/
makefile.py
File metadata and controls
410 lines (332 loc) · 14.6 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import time
from datetime import datetime, timezone
import os
import zipfile
import subprocess
import argparse
from typing import List, Set
from tabulate import tabulate
from src.common import app_version
import re
import sys
cur_dir = os.path.abspath(os.path.dirname(__file__))
external = f"--add-data={os.path.join(cur_dir, 'src', 'external')};src\\external"
print(f"External path: {external}")
# Path to your .NET DLLs
dotnet_dlls = [
("Datamodel.NET.dll", "src\\external"),
("ValveKeyValue.dll", "src\\external"),
("ValvePak.dll", "src\\external"),
("ValveResourceFormat.dll", "src\\external"),
("ZstdSharp.dll", "src\\external"),
("K4os.Compression.LZ4.dll", "src\\external"),
("SharpGLTF.Toolkit.dll", "src\\external"),
("SharpZstd.Interop.dll", "src\\external"),
("SkiaSharp.dll", "src\\external"),
("System.IO.Hashing.dll", "src\\external"),
("TinyBCSharp.dll", "src\\external"),
("TinyEXR.NET.dll", "src\\external")
]
# Create a runtimeconfig.json for the bundled .NET runtime
def generate_runtime_config(target_dir):
config = {
"runtimeOptions": {
"tfm": "net9.0",
"frameworks": [
{
"name": "Microsoft.WindowsDesktop.App",
"version": "9.0.0"
},
{
"name": "Microsoft.NETCore.App",
"version": "9.0.0"
}
]
}
}
import json
os.makedirs(target_dir, exist_ok=True)
with open(os.path.join(target_dir, 'Hammer5Tools.runtimeconfig.json'), 'w') as f:
json.dump(config, f, indent=2)
def get_dotnet_runtime_data():
"""Finds .NET 9.0 runtime files on the system to bundle them."""
import glob
dotnet_root = os.environ.get("DOTNET_ROOT", r"C:\Program Files\dotnet")
if not os.path.exists(dotnet_root):
return []
shared_path = os.path.join(dotnet_root, "shared")
results = []
# Find latest 9.0 versions
for framework in ["Microsoft.NETCore.App", "Microsoft.WindowsDesktop.App"]:
fw_path = os.path.join(shared_path, framework)
if os.path.exists(fw_path):
versions = [v for v in os.listdir(fw_path) if v.startswith("9.0")]
if versions:
latest = sorted(versions, key=lambda x: [int(i) for i in x.split('.')])[-1]
src = os.path.join(fw_path, latest)
dst = f"dotnet/shared/{framework}/{latest}"
results.append(f"--add-data={src};{dst}")
# Find host fxr
host_fxr_path = os.path.join(dotnet_root, "host", "fxr")
if os.path.exists(host_fxr_path):
versions = os.listdir(host_fxr_path)
if versions:
latest = sorted(versions, key=lambda x: [int(i) for i in x.split('.')])[-1]
src = os.path.join(host_fxr_path, latest)
dst = f"dotnet/host/fxr/{latest}"
results.append(f"--add-data={src};{dst}")
# Main host files
for dll in ["hostfxr.dll", "hostpolicy.dll"]:
dll_path = os.path.join(dotnet_root, dll)
if os.path.exists(dll_path):
results.append(f"--add-data={dll_path};dotnet")
return results
# Path to your .NET DLLs
def print_elapsed_time(stage_name: str, start_time: float) -> None:
"""Prints the elapsed time for a given stage."""
elapsed_time = time.time() - start_time
print(f"{stage_name} took {elapsed_time:.2f} seconds")
def kill_process(process_name: str) -> None:
"""Kills a process by its name."""
subprocess.run(
["taskkill", "/F", "/IM", process_name],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
def find_pycparser_tables():
"""
Return (lextab_path, yacctab_path) if the pre-generated pycparser parser
tables exist, or None if they are absent (pycparser >= 3.0 generates them
lazily at runtime, so absence is normal).
Search order:
1. Local .venv / venv
2. pycparser's own package directory (global installs, CI)
3. Current working directory (generated by _generate_pycparser_tables)
"""
try:
import pycparser as _pycparser
pycparser_pkg_dir = os.path.dirname(_pycparser.__file__)
except ImportError:
pycparser_pkg_dir = None
candidates = [
os.path.join(cur_dir, '.venv', 'Lib', 'site-packages', 'pycparser'),
os.path.join(cur_dir, 'venv', 'Lib', 'site-packages', 'pycparser'),
]
if pycparser_pkg_dir:
candidates.append(pycparser_pkg_dir)
candidates.append(cur_dir) # fallback: generated into cwd
for base in candidates:
lextab = os.path.join(base, 'lextab.py')
yacctab = os.path.join(base, 'yacctab.py')
if os.path.isfile(lextab) and os.path.isfile(yacctab):
return lextab, yacctab
return None
def _generate_pycparser_tables():
"""
Force pycparser to generate lextab.py and yacctab.py by instantiating
its C parser with outputdir set to cur_dir so the files land somewhere
predictable regardless of whether the package dir is writable.
"""
try:
try:
import pycparser.ply.lex as lex
import pycparser.ply.yacc as yacc
except ImportError:
import ply.lex as lex
import ply.yacc as yacc
# Monkey-patch outputdir so tables land in cur_dir
_orig_lex = lex.lex
_orig_yacc = yacc.yacc
def _lex(*a, **kw):
kw.setdefault('outputdir', cur_dir)
return _orig_lex(*a, **kw)
def _yacc(*a, **kw):
kw.setdefault('outputdir', cur_dir)
return _orig_yacc(*a, **kw)
lex.lex = _lex
yacc.yacc = _yacc
try:
from pycparser import c_parser
c_parser.CParser()
print("pycparser tables generated.")
finally:
lex.lex = _orig_lex
yacc.yacc = _orig_yacc
except Exception as e:
print(f"Warning: could not pre-generate pycparser tables: {e}")
def build_cpp(project: str, src_dir: str, output_name: str) -> None:
"""Builds a C++ project using CMake."""
build_dir = os.path.join(cur_dir, f"build_cpp_{project}")
os.makedirs(build_dir, exist_ok=True)
try:
# Let CMake choose the best generator for the system
subprocess.run(["cmake", "-S", src_dir, "-B", build_dir,
"-DCMAKE_BUILD_TYPE=Release"], check=True)
subprocess.run(["cmake", "--build", build_dir, "--config", "Release"], check=True)
# Copy exe to hammer5tools/
# CMake might put the exe in build_dir or build_dir/Release (for MSVC)
src_exe = os.path.join(build_dir, f"{output_name}.exe")
if not os.path.exists(src_exe):
src_exe = os.path.join(build_dir, "Release", f"{output_name}.exe")
if not os.path.exists(src_exe):
raise FileNotFoundError(f"Could not find built executable at {src_exe}")
dst_exe = os.path.join(cur_dir, "Hammer5Tools", f"{output_name}.exe")
os.makedirs(os.path.dirname(dst_exe), exist_ok=True)
import shutil
shutil.copy2(src_exe, dst_exe)
print(f"Successfully built and copied {output_name}.exe")
except subprocess.CalledProcessError as e:
print(f"Error building C++ project {project}: {e}")
raise
def build_app_pyinstaller(fast=False, channel='stable') -> None:
"""Builds the Python application using PyInstaller."""
# Try to locate pre-generated pycparser tables; generate them if absent.
tables = find_pycparser_tables()
if tables is None:
_generate_pycparser_tables()
tables = find_pycparser_tables()
runtime_config_dir = os.path.join(cur_dir, 'src', 'external', 'dotnet')
runtime_config_path = os.path.join(runtime_config_dir, 'Hammer5Tools.runtimeconfig.json')
if channel == 'stable':
generate_runtime_config(runtime_config_dir)
pyinstaller_cmd = [
sys.executable, '-m', 'PyInstaller',
'--name=Hammer5Tools_Core',
'--contents-directory=app',
'--noupx',
'--distpath=out_hammer5tools',
'--hidden-import=vpk',
'--collect-all=velopack',
'--noconfirm',
'--onedir',
'--windowed',
'--paths=.;src',
'--hidden-import=resources_rc',
'--hidden-import=widgets',
'--collect-all=src',
'--collect-all=keyvalues3',
'--collect-submodules=pycparser',
'--hidden-import=PySide6.QtNetwork',
'--hidden-import=PySide6.QtMultimedia',
'--hidden-import=PySide6.QtMultimediaWidgets',
'--hidden-import=cffi',
'--collect-submodules=cffi',
'--hidden-import=clr_loader',
'--collect-submodules=clr_loader',
'--optimize=0',
'--icon=src/appicon.ico',
'--add-data=src/appicon.ico;.',
'--add-data=src/images;images/',
'--add-data=src/styles;styles/',
'--add-data=Hammer5Tools;defaults/',
'--exclude-module=PyQt5',
'--exclude-module=numba',
'--exclude-module=scipy',
'--exclude-module=pandas',
'--exclude-module=tabulate',
external,
*( [f'--add-binary=src{os.sep}external{os.sep}{dll};external{os.sep}{dll}' for dll, _ in dotnet_dlls] if channel == 'stable' else [] ),
*( get_dotnet_runtime_data() if channel == 'stable' else [] ),
f'--add-data={runtime_config_path};dotnet' if channel == 'stable' and os.path.exists(runtime_config_path) else '',
'src/main.py'
]
pyinstaller_cmd = [arg for arg in pyinstaller_cmd if arg]
if tables:
lextab_path, yacctab_path = tables
pyinstaller_cmd += [
f'--add-data={lextab_path};pycparser',
f'--add-data={yacctab_path};pycparser',
]
subprocess.run(pyinstaller_cmd, check=True)
def build_hammer5_tools(fast=False, channel='stable') -> None:
# Phase 0: cleanup moved to main() for thread safety
build_app_pyinstaller(fast=fast, channel=channel)
# Final distribution folder
bundle_root = os.path.join(cur_dir, 'Hammer5Tools')
# Safe cleanup: only remove build artifacts, keep data folders
for item in ['app', '_internal']:
path = os.path.join(bundle_root, item)
if os.path.exists(path):
if os.path.isdir(path): shutil.rmtree(path)
else: os.remove(path)
if not os.path.exists(bundle_root):
os.makedirs(bundle_root)
# Flatten the PyInstaller output: move contents from out_hammer5tools/Hammer5Tools_Core/ up to Hammer5Tools/
pyi_output = os.path.join(cur_dir, 'out_hammer5tools', 'Hammer5Tools_Core')
if os.path.exists(pyi_output):
import shutil
for item in os.listdir(pyi_output):
src = os.path.join(pyi_output, item)
dst = os.path.join(bundle_root, item)
# Remove old versions if they exist
if os.path.exists(dst):
if os.path.isdir(dst): shutil.rmtree(dst)
else: os.remove(dst)
shutil.move(src, dst)
shutil.rmtree(os.path.join(cur_dir, 'out_hammer5tools'))
# Ensure data folders are present in bundle_root (they should be if it's the source folder)
template_dir = os.path.join(cur_dir, 'Hammer5Tools')
data_folders = ['Hotkeys', 'Presets', 'SmartPropEditor', 'SoundEventEditor']
for folder in data_folders:
src = os.path.join(template_dir, folder)
dst = os.path.join(bundle_root, folder)
if os.path.exists(src) and src != dst:
if os.path.exists(dst): shutil.rmtree(dst)
shutil.copytree(src, dst)
print(f"Copied data folder {folder} to bundle root")
elif not os.path.exists(src):
print(f"Warning: data folder {folder} missing from template")
def main() -> None:
"""Main function to parse arguments and execute build and packaging tasks."""
parser = argparse.ArgumentParser(description="Build Hammer 5 Tools for Velopack.")
parser.add_argument('--build-all', action='store_true', help="Build Hammer 5 Tools.")
parser.add_argument('--build-app', action='store_true', help="Build only Hammer 5 Tools.")
parser.add_argument('--fast', action='store_true', help="Use 0 level optimization.")
channel_group = parser.add_mutually_exclusive_group()
channel_group.add_argument('--stable', action='store_true', help="Build stable release (default).")
channel_group.add_argument('--dev', action='store_true', help="Build dev release.")
args = parser.parse_args()
channel = 'dev' if args.dev else 'stable'
overall_start_time = time.time()
stage_start_time = time.time()
# Kill processes
for p in ["Hammer5Tools.exe", "fileedit.exe"]:
kill_process(p)
print_elapsed_time("Kill processes", stage_start_time)
results = []
try:
if args.build_all:
stage_start_time = time.time()
# Phase 0: Cleanup
bundle_root = os.path.join(cur_dir, 'Hammer5Tools')
if os.path.exists(bundle_root):
import shutil
for item in ['app', 'Hammer5Tools.exe', 'Hammer5Tools_Core.exe', 'fileedit.exe', '_internal']:
path = os.path.join(bundle_root, item)
if os.path.exists(path):
if os.path.isdir(path): shutil.rmtree(path)
else: os.remove(path)
# 1. Build Python Core
build_hammer5_tools(fast=args.fast, channel=channel)
# 2. Build C++ Launcher
build_cpp("Hammer5Tools", os.path.join(cur_dir, "launcher"), "Hammer5Tools")
# Verify Launcher exists
launcher_path = os.path.join(bundle_root, "Hammer5Tools.exe")
if not os.path.exists(launcher_path):
print(f"FATAL ERROR: Launcher was not found at {launcher_path}")
sys.exit(1)
elapsed_time = time.time() - stage_start_time
results.append(["Sequential Build (Core + Launcher)", f"{elapsed_time:.2f} seconds"])
elif args.build_app:
stage_start_time = time.time()
build_hammer5_tools(fast=args.fast, channel=channel)
elapsed_time = time.time() - stage_start_time
results.append(["Hammer 5 Tools Build (Python)", f"{elapsed_time:.2f} seconds"])
except subprocess.CalledProcessError as e:
print(f"Error during build: {e}")
return
overall_elapsed_time = time.time() - overall_start_time
results.append(["Overall process", f"{overall_elapsed_time:.2f} seconds"])
print(tabulate(results, headers=["Stage", "Elapsed Time"], tablefmt="grid"))
if __name__ == "__main__":
main()