Difficult-Rocket/libs/utils/nuitka.py

156 lines
5.7 KiB
Python
Raw Normal View History

2023-05-14 02:08:31 +08:00
# -------------------------------
# Difficult Rocket
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
# 用于使用 nuitka 构建 DR
import platform
import traceback
from pathlib import Path
from typing import List, Tuple, Optional
2023-05-14 02:08:31 +08:00
2023-05-28 01:03:46 +08:00
from Difficult_Rocket.api.types import Options, Version
2023-05-14 02:08:31 +08:00
2023-06-10 14:45:51 +08:00
class CompilerHelper(Options):
name = 'Nuitka Compiler Helper'
2023-05-14 02:08:31 +08:00
2023-06-09 15:40:30 +08:00
output_path: Path = Path("./build/nuitka-win")
2023-05-14 02:08:31 +08:00
src_file: Path = Path('DR.py')
2023-06-09 16:57:26 +08:00
python_cmd: str = 'python'
2023-05-14 02:08:31 +08:00
# 以下为 nuitka 的参数
2023-05-27 00:55:27 +08:00
use_lto: bool = False # --lto=yes (no is faster)
use_clang: bool = True # --clang
use_msvc: bool = True # --msvc=latest
use_mingw: bool = False # --mingw64
standalone: bool = True # --standalone
2023-06-09 17:03:35 +08:00
use_ccache: bool = True # not --disable-ccache
enable_console: bool = True # --enable-console / --disable-console
2023-06-09 15:40:30 +08:00
show_progress: bool = True # --show-progress
show_memory: bool = False # --show-memory
2023-06-15 00:30:29 +08:00
save_xml: bool = False # --xml
2023-06-14 21:18:04 +08:00
xml_path: Path = Path('build/compile_data.xml')
2023-06-09 15:40:30 +08:00
2023-06-09 17:03:35 +08:00
download_confirm: bool = True # --assume-yes-for-download
2023-05-14 02:08:31 +08:00
company_name: str = 'tool-shenjack-workshop'
product_name: str = 'Difficult-Rocket'
file_version: Version
product_version: Version
file_description: str = 'Difficult Rocket' # --file-description
copy_right: str = 'Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com' # --copyright
2023-06-09 15:40:30 +08:00
2023-05-14 02:08:31 +08:00
icon_path: Path = Path('textures/icon.png')
2023-06-09 15:40:30 +08:00
2023-06-09 16:57:26 +08:00
follow_import: List[str] = ['pyglet']
2023-06-09 15:40:30 +08:00
no_follow_import: List[str] = ['objprint', 'pillow', 'PIL', 'cffi', 'pydoc', 'numpy']
2023-06-09 17:03:35 +08:00
include_data_dir: List[Tuple[str, str]] = [('./libs/fonts', './libs/fonts'),
('./textures', './textures'),
('./configs', './configs')]
2023-06-09 16:57:26 +08:00
include_packages: List[str] = ['Difficult_Rocket.api']
2023-05-14 02:08:31 +08:00
2023-06-16 00:06:43 +08:00
enable_plugin: List[str] = [] # --enable-plugin=xxx,xxx
disable_plugin: List[str] = [] # --disable-plugin=xxx,xxx
2023-05-14 02:08:31 +08:00
def init(self, **kwargs) -> None:
# 非 windows 平台不使用 msvc
if platform.system() != 'Windows':
self.use_msvc = False
2023-05-27 00:55:27 +08:00
self.use_mingw = False
else:
self.use_mingw = self.use_mingw and not self.use_msvc
# Windows 平台下使用 msvc 时不使用 mingw
2023-05-14 02:08:31 +08:00
def load_file(self) -> bool:
try:
from Difficult_Rocket import DR_runtime
self.product_version = DR_runtime.DR_version
self.file_version = DR_runtime.Build_version
return True
except ImportError:
traceback.print_exc()
return False
2023-06-09 15:08:08 +08:00
def __str__(self):
return self.as_markdown()
def as_markdown(self, longest: Optional[int] = None) -> str:
front = super().as_markdown(longest)
gen_cmd = self.gen_subprocess_cmd()
return f"{front}\n\n```bash\n{' '.join(gen_cmd)}\n```"
2023-05-14 02:08:31 +08:00
def gen_subprocess_cmd(self) -> List[str]:
2023-06-09 16:57:26 +08:00
cmd_list = [self.python_cmd, '-m', 'nuitka']
2023-05-14 02:08:31 +08:00
# macos 和 非 macos icon 参数不同
2023-05-27 00:50:14 +08:00
icon_cmd = ""
2023-05-14 02:08:31 +08:00
if platform.system() == 'Darwin':
icon_cmd = f"--macos-app-icon={self.icon_path.absolute()}"
cmd_list.append(f"--macos-app-version={self.product_version}")
2023-05-14 02:08:31 +08:00
elif platform.system() == 'Windows':
icon_cmd = f"--windows-icon-from-ico={self.icon_path.absolute()}"
elif platform.system() == 'Linux':
icon_cmd = f"--linux-icon={self.icon_path.absolute()}"
2023-05-27 00:50:14 +08:00
if self.use_lto:
cmd_list.append('--lto=yes')
2023-05-14 02:08:31 +08:00
else:
2023-05-27 00:50:14 +08:00
cmd_list.append('--lto=no')
2023-06-09 17:03:35 +08:00
if self.enable_console:
cmd_list.append('--enable-console')
else:
cmd_list.append('--disable-console')
2023-05-27 00:50:14 +08:00
if self.use_clang:
cmd_list.append('--clang')
if self.use_msvc:
cmd_list.append('--msvc=latest')
if self.standalone:
cmd_list.append('--standalone')
2023-06-09 17:03:35 +08:00
if not self.use_ccache:
cmd_list.append('--disable-ccache')
2023-06-09 15:40:30 +08:00
if self.show_progress:
cmd_list.append('--show-progress')
if self.show_memory:
cmd_list.append('--show-memory')
2023-06-09 17:03:35 +08:00
if self.download_confirm:
cmd_list.append('--assume-yes-for-download')
2023-06-14 21:18:04 +08:00
if self.save_xml:
cmd_list.append(f'--xml={self.xml_path.absolute()}')
2023-06-09 15:40:30 +08:00
cmd_list.append(f"--output-dir={self.output_path.absolute()}")
2023-05-27 00:50:14 +08:00
2023-05-27 00:55:27 +08:00
cmd_list.append(f"--company-name={self.company_name}")
cmd_list.append(f"--product-name={self.product_name}")
cmd_list.append(f"--file-version={self.file_version}")
cmd_list.append(f"--product-version={self.product_version}")
cmd_list.append(f"--file-description={self.file_description}")
cmd_list.append(f"--copyright={self.copy_right}")
2023-06-09 15:40:30 +08:00
2023-06-10 16:25:47 +08:00
if icon_cmd:
cmd_list.append(icon_cmd)
2023-06-09 15:40:30 +08:00
2023-06-16 00:14:01 +08:00
if self.include_data_dir:
cmd_list += [f"--include-data-dir={src}={dst}" for src, dst in self.include_data_dir]
if self.include_packages:
cmd_list += [f"--include-package={package}" for package in self.include_packages]
if self.follow_import:
cmd_list.append(f"--follow-import-to={','.join(self.follow_import)}")
if self.no_follow_import:
cmd_list.append(f"--nofollow-import-to={','.join(self.no_follow_import)}")
if self.enable_plugin:
cmd_list.append(f"--enable-plugin={','.join(self.enable_plugin)}")
if self.disable_plugin:
cmd_list.append(f"--disable-plugin={','.join(self.disable_plugin)}")
2023-06-16 00:06:43 +08:00
2023-06-09 15:40:30 +08:00
cmd_list.append(f"{self.src_file}")
2023-05-27 00:50:14 +08:00
return cmd_list