team-ljyzipelo

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