Difficult-Rocket/nuitka_build.py

143 lines
6.1 KiB
Python
Raw Permalink Normal View History

2023-06-09 15:10:15 +08:00
# -------------------------------
# Difficult Rocket
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
import os
2023-06-09 16:57:26 +08:00
import sys
import time
2023-06-10 15:10:44 +08:00
import shutil
import tomlkit
import platform
2023-06-10 16:30:44 +08:00
import traceback
2023-06-09 15:10:15 +08:00
import subprocess
2023-06-10 15:10:44 +08:00
2023-06-11 15:19:23 +08:00
from typing import Tuple, Dict
2023-06-10 15:10:44 +08:00
from pathlib import Path
2023-06-09 15:10:15 +08:00
from libs.utils import nuitka
2023-06-11 15:19:23 +08:00
2023-06-09 15:10:15 +08:00
if __name__ == '__main__':
2023-06-10 14:45:51 +08:00
compiler = nuitka.CompilerHelper()
2023-06-09 15:10:15 +08:00
2023-06-09 16:57:26 +08:00
# 修改 python 执行文件 为 运行时的 python
2023-06-10 15:52:49 +08:00
compiler.python_cmd = sys.executable
2023-06-09 16:57:26 +08:00
2023-06-14 21:18:04 +08:00
compiler.xml_path = Path(f"./build/compile_data-{time.time()}.xml")
2023-06-09 17:04:17 +08:00
# 检测 --github 参数
2023-06-10 15:10:44 +08:00
is_github = False
2023-06-09 17:04:17 +08:00
if '--github' in sys.argv:
2023-06-10 15:10:44 +08:00
is_github = True
2023-06-09 17:04:17 +08:00
compiler.use_ccache = False
2023-06-14 21:18:04 +08:00
compiler.show_memory = False
2023-06-10 15:10:44 +08:00
compiler.show_progress = False
compiler.output_path = Path('./build/github')
2023-06-10 15:54:36 +08:00
compiler.python_cmd = 'python'
2023-06-14 21:18:04 +08:00
compiler.save_xml = False
2023-06-09 17:04:17 +08:00
2023-06-15 00:30:29 +08:00
# 检测 --xml 参数
if '--xml' in sys.argv:
compiler.save_xml = True
sys.argv.remove('--xml')
# 检测 --output xx 参数
if '--output' in sys.argv:
# 输入的是输出目录
2023-08-19 20:15:35 +08:00
out_path = sys.argv[sys.argv.index('--output') + 1]
compiler.output_path = Path(out_path)
sys.argv.remove('--output')
2023-08-19 20:15:35 +08:00
sys.argv.remove(out_path)
2023-06-11 15:19:23 +08:00
# 检测 --no-pyglet-opt 参数
pyglet_optimizations = True
2023-06-11 15:19:23 +08:00
if pyglet_optimizations and '--no-pyglet-opt' not in sys.argv:
compiler.no_follow_import += [f'pyglet.app.{x}' for x in ['win32', 'xlib', 'cocoa']]
compiler.no_follow_import += [f'pyglet.input.{x}' for x in ['win32', 'linux', 'macos']]
compiler.no_follow_import += [f'pyglet.libs.{x}' for x in ['win32', 'x11', 'wayland', 'darwin', 'egl', 'headless']]
compiler.no_follow_import += [f'pyglet.window.{x}' for x in ['win32', 'xlib', 'cocoa', 'headless']]
compiler.no_follow_import += [f'pyglet.canvas.{x}' for x in ['win32', 'xlib', 'xlib_vidmoderstore', 'cocoa', 'headless']]
compiler.no_follow_import += [f'pyglet.gl.{x}' for x in ['win32', 'xlib', 'cocoa', 'headless']]
mult_plat_libs = ['app', 'input', 'libs', 'window', 'canvas', 'gl']
if platform.system() == "Windows":
2023-06-11 15:19:23 +08:00
for lib in mult_plat_libs:
compiler.no_follow_import.remove(f'pyglet.{lib}.win32')
elif platform.system() == "Linux":
2023-06-11 15:19:23 +08:00
for lib in mult_plat_libs:
for name in ('xlib', 'x11', 'wayland', 'egl'):
if f'pyglet.{lib}.{name}' in compiler.no_follow_import:
compiler.no_follow_import.remove(f'pyglet.{lib}.{name}')
compiler.no_follow_import.remove('pyglet.canvas.xlib_vidmoderstore')
elif platform.system() == "Darwin":
for lib in mult_plat_libs:
for name in ('cocoa', 'darwin', 'macos'):
if f'pyglet.{lib}.{name}' in compiler.no_follow_import:
compiler.no_follow_import.remove(f'pyglet.{lib}.{name}')
if is_github:
from pprint import pprint
pprint(compiler.option())
2023-07-01 00:51:26 +08:00
else:
compiler.output_path = Path(f'./build/nuitka-{platform.system().lower()}')
2023-08-20 21:42:38 +08:00
print(compiler.as_markdown())
2023-06-09 15:40:30 +08:00
print(compiler.gen_subprocess_cmd())
2023-06-09 15:10:15 +08:00
2023-06-09 16:57:26 +08:00
# 确认是否需要编译
# 如果包含 -y 参数 则直接编译
2023-06-10 15:43:30 +08:00
if (('-y' or '-n') not in sys.argv) and (not is_github):
2023-06-09 16:57:26 +08:00
while (do_compile := input('Do you want to compile this file? (y/n) ')) not in ['y', 'n']:
pass
elif '-y' in sys.argv:
do_compile = 'y'
2023-06-10 15:10:44 +08:00
elif is_github:
do_compile = 'y'
2023-06-09 16:57:26 +08:00
else:
do_compile = 'n'
if do_compile == 'y':
# 编译
2023-06-10 16:10:53 +08:00
time.sleep(1) # 等待 1s
2023-06-09 16:57:26 +08:00
start_time = time.time_ns()
subprocess.run(compiler.gen_subprocess_cmd())
print('Compile Done!')
2023-06-09 17:05:14 +08:00
print(f'Compile Time: {time.time_ns() - start_time} ns ({(time.time_ns() - start_time) / 1000_000_000} s)')
2023-06-10 15:10:44 +08:00
if is_github:
2023-06-11 15:19:23 +08:00
# 去除无用字体文件 (其实现在也不会打包字体文件了 因为 git lfs 没宽带了)
2023-06-10 16:30:44 +08:00
try:
2023-06-10 16:42:54 +08:00
shutil.rmtree(compiler.output_path / 'DR.dist/libs/fonts' / 'Fira_Code', ignore_errors=True)
shutil.rmtree(compiler.output_path / 'DR.dist/libs/fonts' / 'scientifica', ignore_errors=True)
shutil.rmtree(compiler.output_path / 'DR.dist/libs/fonts' / 'HarmonyOS_Sans' / 'HarmonyOS_Sans_Condensed', ignore_errors=True)
shutil.rmtree(compiler.output_path / 'DR.dist/libs/fonts' / 'HarmonyOS_Sans' / 'HarmonyOS_Sans', ignore_errors=True)
os.remove(compiler.output_path / 'DR.dist/libs/fonts' / 'Monocraft.otf')
os.remove(compiler.output_path / 'DR.dist/libs/fonts' / 'SmileySans-Oblique.ttf')
2023-06-10 16:30:44 +08:00
except Exception:
traceback.print_exc()
2023-06-10 16:10:53 +08:00
print('Remove Useless Files Done!')
2023-06-11 15:19:23 +08:00
else:
dist_dir_size = 0
2023-06-11 15:19:23 +08:00
dist_file_size: Dict[str, Tuple[int, float]] = {}
for path, sub_paths, sub_files in os.walk(compiler.output_path / 'DR.dist'):
for file in sub_files:
file_path = os.path.join(path, file)
dist_dir_size += os.path.getsize(file_path)
2023-06-11 15:19:23 +08:00
# 排除不需要记录的文件
2023-07-16 00:21:31 +08:00
if any(x in file_path for x in ('config', 'libs', 'assets')):
2023-06-11 15:19:23 +08:00
continue
dist_file_size[file_path] = (os.path.getsize(file_path), os.path.getsize(file_path) / 1024 / 1024)
compile_data = {'compile_time_ns': time.time_ns() - start_time,
'compile_time_s': (time.time_ns() - start_time) / 1000_000_000,
'dist_size': dist_dir_size,
'dist_size_mb': dist_dir_size / 1024 / 1024,
2023-06-11 15:19:23 +08:00
'compiler_data': compiler.str_option(),
'dist_file_size': dist_file_size}
2023-06-16 00:16:09 +08:00
with open(compiler.output_path / f'../compile_data-{time.time()}.toml', 'w', encoding='utf-8') as compile_data_file:
tomlkit.dump(compile_data, compile_data_file)
2023-06-10 16:11:17 +08:00
sys.exit(0)