| 1 | """Rust 앱과 작업자를 교차 빌드해 Windows 개발용 설치 파일을 생성함.""" |
| 2 | |
| 3 | import argparse |
| 4 | import hashlib |
| 5 | import importlib.util |
| 6 | import json |
| 7 | import os |
| 8 | from pathlib import Path |
| 9 | import shutil |
| 10 | import struct |
| 11 | import subprocess |
| 12 | |
| 13 | ROOT = Path(__file__).resolve().parents[3] |
| 14 | TARGET = "x86_64-pc-windows-gnu" |
| 15 | SPEC = importlib.util.spec_from_file_location("licenses", ROOT / "packaging/licenses.py") |
| 16 | LICENSES = importlib.util.module_from_spec(SPEC) |
| 17 | SPEC.loader.exec_module(LICENSES) |
| 18 | |
| 19 | |
| 20 | def pe_machine(path): |
| 21 | with Path(path).open("rb") as stream: |
| 22 | header = stream.read(64) |
| 23 | if len(header) != 64 or header[:2] != b"MZ": |
| 24 | raise ValueError("PE 실행 파일이 아님") |
| 25 | stream.seek(struct.unpack_from("<I", header, 60)[0]) |
| 26 | signature = stream.read(6) |
| 27 | if len(signature) != 6 or signature[:4] != b"PE\0\0": |
| 28 | raise ValueError("PE 헤더가 잘못됨") |
| 29 | return struct.unpack_from("<H", signature, 4)[0] |
| 30 | |
| 31 | |
| 32 | def build_binary(manifest, binary, target_dir, profile, features=(), target=TARGET): |
| 33 | manifest = Path(manifest).resolve(strict=True) |
| 34 | commit = subprocess.check_output( |
| 35 | ["git", "-C", str(manifest.parent), "rev-parse", "HEAD"], text=True |
| 36 | ).strip() |
| 37 | dirty = subprocess.check_output( |
| 38 | ["git", "-C", str(manifest.parent), "status", "--porcelain"], text=True |
| 39 | ) |
| 40 | if dirty.strip(): |
| 41 | raise ValueError("소스에 커밋하지 않은 변경이 있음") |
| 42 | # 작업 복사본 간 mtime 기반 캐시 혼합을 방지함. |
| 43 | target_dir = target_dir / binary / commit |
| 44 | command = ["cargo", "+1.99.1", "build", "--locked", "--manifest-path", str(manifest), |
| 45 | "--target", target, "--target-dir", str(target_dir), "--profile", profile, |
| 46 | "--bin", binary] |
| 47 | if features: |
| 48 | command += ["--features", ",".join(features)] |
| 49 | environment = dict(os.environ) |
| 50 | linker = environment.get("CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER") |
| 51 | linker = linker or shutil.which("x86_64-w64-mingw32-gcc") or shutil.which("x86_64-w64-mingw32-gcc-posix") |
| 52 | if target == TARGET and (not linker or not shutil.which(linker)): |
| 53 | raise ValueError("MinGW-w64 교차 링커를 찾지 못함") |
| 54 | if target == TARGET: |
| 55 | environment["CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER"] = linker |
| 56 | subprocess.run(command, env=environment, check=True) |
| 57 | output = target_dir / target / ("debug" if profile == "dev" else profile) / (binary + (".exe" if target == TARGET else "")) |
| 58 | if target == TARGET and pe_machine(output) != 0x8664: |
| 59 | raise ValueError("x64 Windows 실행 파일이 아님") |
| 60 | current_commit = subprocess.check_output( |
| 61 | ["git", "-C", str(manifest.parent), "rev-parse", "HEAD"], text=True |
| 62 | ).strip() |
| 63 | dirty = subprocess.check_output( |
| 64 | ["git", "-C", str(manifest.parent), "status", "--porcelain"], text=True |
| 65 | ) |
| 66 | if current_commit != commit or dirty.strip(): |
| 67 | raise ValueError("빌드 중 소스가 바뀜") |
| 68 | return output, {"commit": commit, "sha256": hashlib.sha256(output.read_bytes()).hexdigest(), |
| 69 | "binary": binary, "features": list(features)} |
| 70 | |
| 71 | |
| 72 | def build(args): |
| 73 | output = Path(args.output).resolve() |
| 74 | output.mkdir(parents=True, exist_ok=False) |
| 75 | target_dir = Path(args.target_dir).resolve() |
| 76 | records = {} |
| 77 | payload = output / "payload" |
| 78 | payload.mkdir() |
| 79 | LICENSES.generate([args.app_manifest, args.worker_manifest], payload / "THIRD-PARTY-LICENSES.txt") |
| 80 | for name, manifest, binary, features in ( |
| 81 | ("Zipelo.exe", args.app_manifest, "zipelo-ui-memory-app", ("ui",)), |
| 82 | ("zipelo-archive-worker.exe", args.worker_manifest, "zipelo-archive-worker", ()), |
| 83 | ): |
| 84 | source, records[name] = build_binary(manifest, binary, target_dir, args.profile, features) |
| 85 | shutil.copyfile(source, payload / name) |
| 86 | shutil.copyfile(ROOT / "LICENSE", payload / "LICENSE") |
| 87 | records = {"development_only": True, "windows_execution_verified": False, |
| 88 | "profile": args.profile, "target": TARGET, "files": records} |
| 89 | (payload / "BUILD-INFO.json").write_text(json.dumps(records, indent=4) + "\n", encoding="utf-8") |
| 90 | shutil.copyfile(Path(__file__).with_name("installer.nsi"), output / "installer.nsi") |
| 91 | subprocess.run(["makensis", "-WX", "installer.nsi"], cwd=output, check=True) |
| 92 | installer = output / "Zipelo-0.1.0-dev-windows-x64-setup.exe" |
| 93 | if pe_machine(installer) not in (0x14C, 0x8664): |
| 94 | raise ValueError("NSIS 설치 실행 파일이 아님") |
| 95 | print(installer) |
| 96 | print("SHA-256 " + hashlib.sha256(installer.read_bytes()).hexdigest()) |
| 97 | |
| 98 | |
| 99 | def main(): |
| 100 | parser = argparse.ArgumentParser(description=__doc__) |
| 101 | parser.add_argument("--app-manifest", required=True) |
| 102 | parser.add_argument("--worker-manifest", required=True) |
| 103 | parser.add_argument("--output", required=True) |
| 104 | parser.add_argument("--target-dir", required=True) |
| 105 | parser.add_argument("--profile", choices=("dev", "release"), default="release") |
| 106 | build(parser.parse_args()) |
| 107 | |
| 108 | |
| 109 | if __name__ == "__main__": |
| 110 | main() |