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