Ship a self-contained Windows GUI with game shortcuts and application icons
This commit is contained in:
1 parent
329a1ad4df
commit
3ea497d38a
16 files changed
+589
-58
No files matched your search
+67
-6
@@ -38,19 +38,70 @@ def check_binary(binary, platform):
|
||||
raise ValueError(f"{binary} is not an x86_64 {platform} executable")
|
||||
|
||||
|
||||
def package_windows(binary, work):
|
||||
def windows_inventory(payload, include):
|
||||
from windows_deps import safe_basename
|
||||
|
||||
paths = sorted(payload.rglob("*"), key=lambda p: p.relative_to(payload).as_posix().lower())
|
||||
files, directories, seen = [], [], set()
|
||||
for path in paths:
|
||||
relative = path.relative_to(payload)
|
||||
for part in relative.parts:
|
||||
safe_basename(part)
|
||||
key = relative.as_posix().lower()
|
||||
if key in seen or key == "uninstall.exe":
|
||||
raise ValueError(f"Conflicting Windows payload filename: {relative}")
|
||||
seen.add(key)
|
||||
if path.is_symlink():
|
||||
raise ValueError(f"Symlink in Windows payload: {relative}")
|
||||
if path.is_dir():
|
||||
directories.append(relative)
|
||||
elif path.is_file():
|
||||
files.append(relative)
|
||||
else:
|
||||
raise ValueError(f"Not a regular payload file: {relative}")
|
||||
install = ["!macro InstallPayload"]
|
||||
previous = None
|
||||
for path in files:
|
||||
parent = "\\".join(path.parts[:-1])
|
||||
destination = "$INSTDIR" + ("\\" + parent if parent else "")
|
||||
if destination != previous:
|
||||
install.append(f' SetOutPath "{destination}"')
|
||||
previous = destination
|
||||
source = "\\".join(path.parts)
|
||||
install.append(f' File "payload\\{source}"')
|
||||
install.extend([' SetOutPath "$INSTDIR"', "!macroend", "", "!macro UninstallPayload"])
|
||||
for path in files:
|
||||
name = "\\".join(path.parts)
|
||||
install.append(f' Delete "$INSTDIR\\{name}"')
|
||||
for path in sorted(directories, key=lambda p: (-len(p.parts), p.as_posix().lower())):
|
||||
name = "\\".join(path.parts)
|
||||
install.append(f' RMDir "$INSTDIR\\{name}"')
|
||||
install.append("!macroend")
|
||||
include.write_text("\n".join(install) + "\n", encoding="utf-8")
|
||||
return files
|
||||
|
||||
|
||||
def package_windows(binary, work, dll_dirs=(), require_static_crt=False):
|
||||
from windows_deps import resolve_dependencies, safe_basename
|
||||
|
||||
safe_basename(NAME)
|
||||
safe_basename(VERSION)
|
||||
dependencies = resolve_dependencies(binary, dll_dirs, require_static_crt)
|
||||
stem = f"{NAME}-{VERSION}-windows-x86_64"
|
||||
payload = work / "payload"
|
||||
(payload / "licenses").mkdir(parents=True)
|
||||
shutil.copyfile(binary, payload / f"{NAME}.exe")
|
||||
for name, source in dependencies.items():
|
||||
shutil.copyfile(source, payload / name)
|
||||
for name, source in NOTICES.items():
|
||||
shutil.copyfile(source, payload / "licenses" / name)
|
||||
|
||||
# Freeze one validated inventory for the archive and both NSIS macros.
|
||||
inventory = windows_inventory(payload, work / "payload-files.nsh")
|
||||
archive = work / f"{stem}.zip"
|
||||
with zipfile.ZipFile(archive, "w", zipfile.ZIP_DEFLATED) as bundle:
|
||||
for path in sorted(payload.rglob("*")):
|
||||
if path.is_file():
|
||||
bundle.write(path, path.relative_to(payload))
|
||||
for relative in inventory:
|
||||
bundle.write(payload / relative, relative.as_posix())
|
||||
|
||||
makensis = shutil.which("makensis")
|
||||
if not makensis and os.name == "nt":
|
||||
@@ -63,6 +114,7 @@ def package_windows(binary, work):
|
||||
if not makensis:
|
||||
raise ValueError("Install NSIS 3 and make makensis available")
|
||||
shutil.copyfile(ROOT / "packaging/windows/installer.nsi", work / "installer.nsi")
|
||||
shutil.copyfile(ROOT / "assets/icon.ico", work / "app.ico")
|
||||
define = "/D" if os.name == "nt" else "-D"
|
||||
installer = work / f"{stem}-setup.exe"
|
||||
subprocess.run(
|
||||
@@ -137,14 +189,23 @@ def main():
|
||||
parser.add_argument("platform", choices=("windows", "linux"))
|
||||
parser.add_argument("binary", type=Path)
|
||||
parser.add_argument("--output", type=Path, default=ROOT / "target/dist")
|
||||
parser.add_argument("--dll-dir", type=Path, action="append", default=[], metavar="DIR",
|
||||
help="Windows DLL source directory (repeatable; no PATH search)")
|
||||
parser.add_argument("--require-static-crt", action="store_true",
|
||||
help="Reject direct/delay CRT imports in the Windows executable")
|
||||
args = parser.parse_args()
|
||||
if args.platform != "windows" and (args.dll_dir or args.require_static_crt):
|
||||
parser.error("--dll-dir and --require-static-crt apply only to Windows")
|
||||
binary = args.binary.resolve()
|
||||
output = args.output.resolve()
|
||||
check_binary(binary, args.platform)
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
with tempfile.TemporaryDirectory(dir=output) as temp:
|
||||
package = package_windows if args.platform == "windows" else package_linux
|
||||
for artifact in package(binary, Path(temp)):
|
||||
if args.platform == "windows":
|
||||
artifacts = package_windows(binary, Path(temp), args.dll_dir, args.require_static_crt)
|
||||
else:
|
||||
artifacts = package_linux(binary, Path(temp))
|
||||
for artifact in artifacts:
|
||||
destination = output / artifact.name
|
||||
artifact.replace(destination)
|
||||
print(destination)
|
||||
|
||||
Reference in new issue
Block a user