build-release.py 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. #!/usr/bin/env python3
  2. """
  3. This script is shared between SDL2, SDL3, and all satellite libraries.
  4. Don't specialize this script for doing project-specific modifications.
  5. Rather, modify release-info.json.
  6. """
  7. import argparse
  8. import collections
  9. import dataclasses
  10. from collections.abc import Callable
  11. import contextlib
  12. import datetime
  13. import fnmatch
  14. import glob
  15. import io
  16. import json
  17. import logging
  18. import multiprocessing
  19. import os
  20. from pathlib import Path
  21. import platform
  22. import re
  23. import shlex
  24. import shutil
  25. import subprocess
  26. import sys
  27. import tarfile
  28. import tempfile
  29. import textwrap
  30. import typing
  31. import zipfile
  32. logger = logging.getLogger(__name__)
  33. GIT_HASH_FILENAME = ".git-hash"
  34. REVISION_TXT = "REVISION.txt"
  35. def safe_isotime_to_datetime(str_isotime: str) -> datetime.datetime:
  36. try:
  37. return datetime.datetime.fromisoformat(str_isotime)
  38. except ValueError:
  39. pass
  40. logger.warning("Invalid iso time: %s", str_isotime)
  41. if str_isotime[-6:-5] in ("+", "-"):
  42. # Commits can have isotime with invalid timezone offset (e.g. "2021-07-04T20:01:40+32:00")
  43. modified_str_isotime = str_isotime[:-6] + "+00:00"
  44. try:
  45. return datetime.datetime.fromisoformat(modified_str_isotime)
  46. except ValueError:
  47. pass
  48. raise ValueError(f"Invalid isotime: {str_isotime}")
  49. def arc_join(*parts: list[str]) -> str:
  50. assert all(p[:1] != "/" and p[-1:] != "/" for p in parts), f"None of {parts} may start or end with '/'"
  51. return "/".join(p for p in parts if p)
  52. @dataclasses.dataclass(frozen=True)
  53. class VsArchPlatformConfig:
  54. arch: str
  55. configuration: str
  56. platform: str
  57. def extra_context(self):
  58. return {
  59. "ARCH": self.arch,
  60. "CONFIGURATION": self.configuration,
  61. "PLATFORM": self.platform,
  62. }
  63. @contextlib.contextmanager
  64. def chdir(path):
  65. original_cwd = os.getcwd()
  66. try:
  67. os.chdir(path)
  68. yield
  69. finally:
  70. os.chdir(original_cwd)
  71. class Executer:
  72. def __init__(self, root: Path, dry: bool=False):
  73. self.root = root
  74. self.dry = dry
  75. def run(self, cmd, cwd=None, env=None):
  76. logger.info("Executing args=%r", cmd)
  77. sys.stdout.flush()
  78. if not self.dry:
  79. subprocess.check_call(cmd, cwd=cwd or self.root, env=env, text=True)
  80. def check_output(self, cmd, cwd=None, dry_out=None, env=None, text=True):
  81. logger.info("Executing args=%r", cmd)
  82. sys.stdout.flush()
  83. if self.dry:
  84. return dry_out
  85. return subprocess.check_output(cmd, cwd=cwd or self.root, env=env, text=text)
  86. class SectionPrinter:
  87. @contextlib.contextmanager
  88. def group(self, title: str):
  89. print(f"{title}:")
  90. yield
  91. class GitHubSectionPrinter(SectionPrinter):
  92. def __init__(self):
  93. super().__init__()
  94. self.in_group = False
  95. @contextlib.contextmanager
  96. def group(self, title: str):
  97. print(f"::group::{title}")
  98. assert not self.in_group, "Can enter a group only once"
  99. self.in_group = True
  100. yield
  101. self.in_group = False
  102. print("::endgroup::")
  103. class VisualStudio:
  104. def __init__(self, executer: Executer, year: typing.Optional[str]=None):
  105. self.executer = executer
  106. self.vsdevcmd = self.find_vsdevcmd(year)
  107. self.msbuild = self.find_msbuild()
  108. @property
  109. def dry(self) -> bool:
  110. return self.executer.dry
  111. VS_YEAR_TO_VERSION = {
  112. "2022": 17,
  113. "2019": 16,
  114. "2017": 15,
  115. "2015": 14,
  116. "2013": 12,
  117. }
  118. def find_vsdevcmd(self, year: typing.Optional[str]=None) -> typing.Optional[Path]:
  119. vswhere_spec = ["-latest"]
  120. if year is not None:
  121. try:
  122. version = self.VS_YEAR_TO_VERSION[year]
  123. except KeyError:
  124. logger.error("Invalid Visual Studio year")
  125. return None
  126. vswhere_spec.extend(["-version", f"[{version},{version+1})"])
  127. vswhere_cmd = ["vswhere"] + vswhere_spec + ["-property", "installationPath"]
  128. vs_install_path = Path(self.executer.check_output(vswhere_cmd, dry_out="/tmp").strip())
  129. logger.info("VS install_path = %s", vs_install_path)
  130. assert vs_install_path.is_dir(), "VS installation path does not exist"
  131. vsdevcmd_path = vs_install_path / "Common7/Tools/vsdevcmd.bat"
  132. logger.info("vsdevcmd path = %s", vsdevcmd_path)
  133. if self.dry:
  134. vsdevcmd_path.parent.mkdir(parents=True, exist_ok=True)
  135. vsdevcmd_path.touch(exist_ok=True)
  136. assert vsdevcmd_path.is_file(), "vsdevcmd.bat batch file does not exist"
  137. return vsdevcmd_path
  138. def find_msbuild(self) -> typing.Optional[Path]:
  139. vswhere_cmd = ["vswhere", "-latest", "-requires", "Microsoft.Component.MSBuild", "-find", r"MSBuild\**\Bin\MSBuild.exe"]
  140. msbuild_path = Path(self.executer.check_output(vswhere_cmd, dry_out="/tmp/MSBuild.exe").strip())
  141. logger.info("MSBuild path = %s", msbuild_path)
  142. if self.dry:
  143. msbuild_path.parent.mkdir(parents=True, exist_ok=True)
  144. msbuild_path.touch(exist_ok=True)
  145. assert msbuild_path.is_file(), "MSBuild.exe does not exist"
  146. return msbuild_path
  147. def build(self, arch_platform: VsArchPlatformConfig, projects: list[Path]):
  148. assert projects, "Need at least one project to build"
  149. vsdev_cmd_str = f"\"{self.vsdevcmd}\" -arch={arch_platform.arch}"
  150. msbuild_cmd_str = " && ".join([f"\"{self.msbuild}\" \"{project}\" /m /p:BuildInParallel=true /p:Platform={arch_platform.platform} /p:Configuration={arch_platform.configuration}" for project in projects])
  151. bat_contents = f"{vsdev_cmd_str} && {msbuild_cmd_str}\n"
  152. bat_path = Path(tempfile.gettempdir()) / "cmd.bat"
  153. with bat_path.open("w") as f:
  154. f.write(bat_contents)
  155. logger.info("Running cmd.exe script (%s): %s", bat_path, bat_contents)
  156. cmd = ["cmd.exe", "/D", "/E:ON", "/V:OFF", "/S", "/C", f"CALL {str(bat_path)}"]
  157. self.executer.run(cmd)
  158. class Archiver:
  159. def __init__(self, zip_path: typing.Optional[Path]=None, tgz_path: typing.Optional[Path]=None, txz_path: typing.Optional[Path]=None):
  160. self._zip_files = []
  161. self._tar_files = []
  162. self._added_files = set()
  163. if zip_path:
  164. self._zip_files.append(zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED))
  165. if tgz_path:
  166. self._tar_files.append(tarfile.open(tgz_path, "w:gz"))
  167. if txz_path:
  168. self._tar_files.append(tarfile.open(txz_path, "w:xz"))
  169. @property
  170. def added_files(self) -> set[str]:
  171. return self._added_files
  172. def add_file_data(self, arcpath: str, data: bytes, mode: int, time: datetime.datetime):
  173. for zf in self._zip_files:
  174. file_data_time = (time.year, time.month, time.day, time.hour, time.minute, time.second)
  175. zip_info = zipfile.ZipInfo(filename=arcpath, date_time=file_data_time)
  176. zip_info.external_attr = mode << 16
  177. zip_info.compress_type = zipfile.ZIP_DEFLATED
  178. zf.writestr(zip_info, data=data)
  179. for tf in self._tar_files:
  180. tar_info = tarfile.TarInfo(arcpath)
  181. tar_info.type = tarfile.REGTYPE
  182. tar_info.mode = mode
  183. tar_info.size = len(data)
  184. tar_info.mtime = int(time.timestamp())
  185. tf.addfile(tar_info, fileobj=io.BytesIO(data))
  186. self._added_files.add(arcpath)
  187. def add_symlink(self, arcpath: str, target: str, time: datetime.datetime, files_for_zip):
  188. logger.debug("Adding symlink (target=%r) -> %s", target, arcpath)
  189. for zf in self._zip_files:
  190. file_data_time = (time.year, time.month, time.day, time.hour, time.minute, time.second)
  191. for f in files_for_zip:
  192. zip_info = zipfile.ZipInfo(filename=f["arcpath"], date_time=file_data_time)
  193. zip_info.external_attr = f["mode"] << 16
  194. zip_info.compress_type = zipfile.ZIP_DEFLATED
  195. zf.writestr(zip_info, data=f["data"])
  196. for tf in self._tar_files:
  197. tar_info = tarfile.TarInfo(arcpath)
  198. tar_info.type = tarfile.SYMTYPE
  199. tar_info.mode = 0o777
  200. tar_info.mtime = int(time.timestamp())
  201. tar_info.linkname = target
  202. tf.addfile(tar_info)
  203. self._added_files.update(f["arcpath"] for f in files_for_zip)
  204. def add_git_hash(self, arcdir: str, commit: str, time: datetime.datetime):
  205. arcpath = arc_join(arcdir, GIT_HASH_FILENAME)
  206. data = f"{commit}\n".encode()
  207. self.add_file_data(arcpath=arcpath, data=data, mode=0o100644, time=time)
  208. def add_file_path(self, arcpath: str, path: Path):
  209. assert path.is_file(), f"{path} should be a file"
  210. logger.debug("Adding %s -> %s", path, arcpath)
  211. for zf in self._zip_files:
  212. zf.write(path, arcname=arcpath)
  213. for tf in self._tar_files:
  214. tf.add(path, arcname=arcpath)
  215. def add_file_directory(self, arcdirpath: str, dirpath: Path):
  216. assert dirpath.is_dir()
  217. if arcdirpath and arcdirpath[-1:] != "/":
  218. arcdirpath += "/"
  219. for f in dirpath.iterdir():
  220. if f.is_file():
  221. arcpath = f"{arcdirpath}{f.name}"
  222. logger.debug("Adding %s to %s", f, arcpath)
  223. self.add_file_path(arcpath=arcpath, path=f)
  224. def close(self):
  225. # Archiver is intentionally made invalid after this function
  226. del self._zip_files
  227. self._zip_files = None
  228. del self._tar_files
  229. self._tar_files = None
  230. def __enter__(self):
  231. return self
  232. def __exit__(self, type, value, traceback):
  233. self.close()
  234. class NodeInArchive:
  235. def __init__(self, arcpath: str, path: typing.Optional[Path]=None, data: typing.Optional[bytes]=None, mode: typing.Optional[int]=None, symtarget: typing.Optional[str]=None, time: typing.Optional[datetime.datetime]=None, directory: bool=False):
  236. self.arcpath = arcpath
  237. self.path = path
  238. self.data = data
  239. self.mode = mode
  240. self.symtarget = symtarget
  241. self.time = time
  242. self.directory = directory
  243. @classmethod
  244. def from_fs(cls, arcpath: str, path: Path, mode: int=0o100644, time: typing.Optional[datetime.datetime]=None) -> "NodeInArchive":
  245. if time is None:
  246. time = datetime.datetime.fromtimestamp(os.stat(path).st_mtime)
  247. return cls(arcpath=arcpath, path=path, mode=mode)
  248. @classmethod
  249. def from_data(cls, arcpath: str, data: bytes, time: datetime.datetime) -> "NodeInArchive":
  250. return cls(arcpath=arcpath, data=data, time=time, mode=0o100644)
  251. @classmethod
  252. def from_text(cls, arcpath: str, text: str, time: datetime.datetime) -> "NodeInArchive":
  253. return cls.from_data(arcpath=arcpath, data=text.encode(), time=time)
  254. @classmethod
  255. def from_symlink(cls, arcpath: str, symtarget: str) -> "NodeInArchive":
  256. return cls(arcpath=arcpath, symtarget=symtarget)
  257. @classmethod
  258. def from_directory(cls, arcpath: str) -> "NodeInArchive":
  259. return cls(arcpath=arcpath, directory=True)
  260. def __repr__(self) -> str:
  261. return f"<{type(self).__name__}:arcpath={self.arcpath},path='{str(self.path)}',len(data)={len(self.data) if self.data else 'n/a'},directory={self.directory},symtarget={self.symtarget}>"
  262. def configure_file(path: Path, context: dict[str, str]) -> bytes:
  263. text = path.read_text()
  264. return configure_text(text, context=context).encode()
  265. def configure_text(text: str, context: dict[str, str]) -> str:
  266. original_text = text
  267. for txt, repl in context.items():
  268. text = text.replace(f"@<@{txt}@>@", repl)
  269. success = all(thing not in text for thing in ("@<@", "@>@"))
  270. if not success:
  271. raise ValueError(f"Failed to configure {repr(original_text)}")
  272. return text
  273. def configure_text_list(text_list: list[str], context: dict[str, str]) -> list[str]:
  274. return [configure_text(text=e, context=context) for e in text_list]
  275. class ArchiveFileTree:
  276. def __init__(self):
  277. self._tree: dict[str, NodeInArchive] = {}
  278. def add_file(self, file: NodeInArchive):
  279. self._tree[file.arcpath] = file
  280. def get_latest_mod_time(self) -> datetime.datetime:
  281. return max(item.time for item in self._tree.values() if item.time)
  282. def add_to_archiver(self, archive_base: str, archiver: Archiver):
  283. remaining_symlinks = set()
  284. added_files = dict()
  285. def calculate_symlink_target(s: NodeInArchive) -> str:
  286. dest_dir = os.path.dirname(s.arcpath)
  287. if dest_dir:
  288. dest_dir += "/"
  289. target = dest_dir + s.symtarget
  290. while True:
  291. new_target, n = re.subn(r"([^/]+/+[.]{2}/)", "", target)
  292. target = new_target
  293. if not n:
  294. break
  295. return target
  296. # Add files in first pass
  297. for arcpath, node in self._tree.items():
  298. assert node is not None, f"{arcpath} -> node"
  299. if node.data is not None:
  300. archiver.add_file_data(arcpath=arc_join(archive_base, arcpath), data=node.data, time=node.time, mode=node.mode)
  301. assert node.arcpath is not None, f"{node=}"
  302. added_files[node.arcpath] = node
  303. elif node.path is not None:
  304. archiver.add_file_path(arcpath=arc_join(archive_base, arcpath), path=node.path)
  305. assert node.arcpath is not None, f"{node=}"
  306. added_files[node.arcpath] = node
  307. elif node.symtarget is not None:
  308. remaining_symlinks.add(node)
  309. elif node.directory:
  310. pass
  311. else:
  312. raise ValueError(f"Invalid Archive Node: {repr(node)}")
  313. assert None not in added_files
  314. # Resolve symlinks in second pass: zipfile does not support symlinks, so add files to zip archive
  315. while True:
  316. if not remaining_symlinks:
  317. break
  318. symlinks_this_time = set()
  319. extra_added_files = {}
  320. for symlink in remaining_symlinks:
  321. symlink_files_for_zip = {}
  322. symlink_target_path = calculate_symlink_target(symlink)
  323. if symlink_target_path in added_files:
  324. symlink_files_for_zip[symlink.arcpath] = added_files[symlink_target_path]
  325. else:
  326. symlink_target_path_slash = symlink_target_path + "/"
  327. for added_file in added_files:
  328. if added_file.startswith(symlink_target_path_slash):
  329. path_in_symlink = symlink.arcpath + "/" + added_file.removeprefix(symlink_target_path_slash)
  330. symlink_files_for_zip[path_in_symlink] = added_files[added_file]
  331. if symlink_files_for_zip:
  332. symlinks_this_time.add(symlink)
  333. extra_added_files.update(symlink_files_for_zip)
  334. files_for_zip = [{"arcpath": f"{archive_base}/{sym_path}", "data": sym_info.data, "mode": sym_info.mode} for sym_path, sym_info in symlink_files_for_zip.items()]
  335. archiver.add_symlink(arcpath=f"{archive_base}/{symlink.arcpath}", target=symlink.symtarget, time=symlink.time, files_for_zip=files_for_zip)
  336. # if not symlinks_this_time:
  337. # logger.info("files added: %r", set(path for path in added_files.keys()))
  338. assert symlinks_this_time, f"No targets found for symlinks: {remaining_symlinks}"
  339. remaining_symlinks.difference_update(symlinks_this_time)
  340. added_files.update(extra_added_files)
  341. def add_directory_tree(self, arc_dir: str, path: Path, time: datetime.datetime):
  342. assert path.is_dir()
  343. for files_dir, _, filenames in os.walk(path):
  344. files_dir_path = Path(files_dir)
  345. rel_files_path = files_dir_path.relative_to(path)
  346. for filename in filenames:
  347. self.add_file(NodeInArchive.from_fs(arcpath=arc_join(arc_dir, str(rel_files_path), filename), path=files_dir_path / filename, time=time))
  348. def _add_files_recursively(self, arc_dir: str, paths: list[Path], time: datetime.datetime):
  349. logger.debug(f"_add_files_recursively({arc_dir=} {paths=})")
  350. for path in paths:
  351. arcpath = arc_join(arc_dir, path.name)
  352. if path.is_file():
  353. logger.debug("Adding %s as %s", path, arcpath)
  354. self.add_file(NodeInArchive.from_fs(arcpath=arcpath, path=path, time=time))
  355. elif path.is_dir():
  356. self._add_files_recursively(arc_dir=arc_join(arc_dir, path.name), paths=list(path.iterdir()), time=time)
  357. else:
  358. raise ValueError(f"Unsupported file type to add recursively: {path}")
  359. def add_file_mapping(self, arc_dir: str, file_mapping: dict[str, list[str]], file_mapping_root: Path, context: dict[str, str], time: datetime.datetime):
  360. for meta_rel_destdir, meta_file_globs in file_mapping.items():
  361. rel_destdir = configure_text(meta_rel_destdir, context=context)
  362. assert "@" not in rel_destdir, f"archive destination should not contain an @ after configuration ({repr(meta_rel_destdir)}->{repr(rel_destdir)})"
  363. for meta_file_glob in meta_file_globs:
  364. file_glob = configure_text(meta_file_glob, context=context)
  365. assert "@" not in rel_destdir, f"archive glob should not contain an @ after configuration ({repr(meta_file_glob)}->{repr(file_glob)})"
  366. if ":" in file_glob:
  367. original_path, new_filename = file_glob.rsplit(":", 1)
  368. assert ":" not in original_path, f"Too many ':' in {repr(file_glob)}"
  369. assert "/" not in new_filename, f"New filename cannot contain a '/' in {repr(file_glob)}"
  370. path = file_mapping_root / original_path
  371. arcpath = arc_join(arc_dir, rel_destdir, new_filename)
  372. if path.suffix == ".in":
  373. data = configure_file(path, context=context)
  374. logger.debug("Adding processed %s -> %s", path, arcpath)
  375. self.add_file(NodeInArchive.from_data(arcpath=arcpath, data=data, time=time))
  376. else:
  377. logger.debug("Adding %s -> %s", path, arcpath)
  378. self.add_file(NodeInArchive.from_fs(arcpath=arcpath, path=path, time=time))
  379. else:
  380. relative_file_paths = glob.glob(file_glob, root_dir=file_mapping_root)
  381. assert relative_file_paths, f"Glob '{file_glob}' does not match any file"
  382. self._add_files_recursively(arc_dir=arc_join(arc_dir, rel_destdir), paths=[file_mapping_root / p for p in relative_file_paths], time=time)
  383. class SourceCollector:
  384. # TreeItem = collections.namedtuple("TreeItem", ("path", "mode", "data", "symtarget", "directory", "time"))
  385. def __init__(self, root: Path, commit: str, filter: typing.Optional[Callable[[str], bool]], executer: Executer):
  386. self.root = root
  387. self.commit = commit
  388. self.filter = filter
  389. self.executer = executer
  390. def get_archive_file_tree(self) -> ArchiveFileTree:
  391. git_archive_args = ["git", "archive", "--format=tar.gz", self.commit, "-o", "/dev/stdout"]
  392. logger.info("Executing args=%r", git_archive_args)
  393. contents_tgz = subprocess.check_output(git_archive_args, cwd=self.root, text=False)
  394. tar_archive = tarfile.open(fileobj=io.BytesIO(contents_tgz), mode="r:gz")
  395. filenames = tuple(m.name for m in tar_archive if (m.isfile() or m.issym()))
  396. file_times = self._get_file_times(paths=filenames)
  397. git_contents = ArchiveFileTree()
  398. for ti in tar_archive:
  399. if self.filter and not self.filter(ti.name):
  400. continue
  401. data = None
  402. symtarget = None
  403. directory = False
  404. file_time = None
  405. if ti.isfile():
  406. contents_file = tar_archive.extractfile(ti.name)
  407. data = contents_file.read()
  408. file_time = file_times[ti.name]
  409. elif ti.issym():
  410. symtarget = ti.linkname
  411. file_time = file_times[ti.name]
  412. elif ti.isdir():
  413. directory = True
  414. else:
  415. raise ValueError(f"{ti.name}: unknown type")
  416. node = NodeInArchive(arcpath=ti.name, data=data, mode=ti.mode, symtarget=symtarget, time=file_time, directory=directory)
  417. git_contents.add_file(node)
  418. return git_contents
  419. def _get_file_times(self, paths: tuple[str, ...]) -> dict[str, datetime.datetime]:
  420. dry_out = textwrap.dedent("""\
  421. time=2024-03-14T15:40:25-07:00
  422. M\tCMakeLists.txt
  423. """)
  424. git_log_out = self.executer.check_output(["git", "log", "--name-status", '--pretty=time=%cI', self.commit], dry_out=dry_out, cwd=self.root).splitlines(keepends=False)
  425. current_time = None
  426. set_paths = set(paths)
  427. path_times: dict[str, datetime.datetime] = {}
  428. for line in git_log_out:
  429. if not line:
  430. continue
  431. if line.startswith("time="):
  432. current_time = safe_isotime_to_datetime(line.removeprefix("time="))
  433. continue
  434. mod_type, file_paths = line.split(maxsplit=1)
  435. assert current_time is not None
  436. for file_path in file_paths.split("\t"):
  437. if file_path in set_paths and file_path not in path_times:
  438. path_times[file_path] = current_time
  439. # FIXME: find out why some files are not shown in "git log"
  440. # assert set(path_times.keys()) == set_paths
  441. if set(path_times.keys()) != set_paths:
  442. found_times = set(path_times.keys())
  443. paths_without_times = set_paths.difference(found_times)
  444. logger.warning("No times found for these paths: %s", paths_without_times)
  445. max_time = max(time for time in path_times.values())
  446. for path in paths_without_times:
  447. path_times[path] = max_time
  448. return path_times
  449. class Releaser:
  450. def __init__(self, release_info: dict, commit: str, revision: str, root: Path, dist_path: Path, section_printer: SectionPrinter, executer: Executer, cmake_generator: str, deps_path: Path, overwrite: bool, github: bool, fast: bool):
  451. self.release_info = release_info
  452. self.project = release_info["name"]
  453. self.version = self.extract_sdl_version(root=root, release_info=release_info)
  454. self.root = root
  455. self.commit = commit
  456. self.revision = revision
  457. self.dist_path = dist_path
  458. self.section_printer = section_printer
  459. self.executer = executer
  460. self.cmake_generator = cmake_generator
  461. self.cpu_count = multiprocessing.cpu_count()
  462. self.deps_path = deps_path
  463. self.overwrite = overwrite
  464. self.github = github
  465. self.fast = fast
  466. self.arc_time = datetime.datetime.now()
  467. self.artifacts: dict[str, Path] = {}
  468. def get_context(self, extra_context: typing.Optional[dict[str, str]]=None) -> dict[str, str]:
  469. ctx = {
  470. "PROJECT_NAME": self.project,
  471. "PROJECT_VERSION": self.version,
  472. "PROJECT_COMMIT": self.commit,
  473. "PROJECT_REVISION": self.revision,
  474. "PROJECT_ROOT": str(self.root),
  475. }
  476. if extra_context:
  477. ctx.update(extra_context)
  478. return ctx
  479. @property
  480. def dry(self) -> bool:
  481. return self.executer.dry
  482. def prepare(self):
  483. logger.debug("Creating dist folder")
  484. self.dist_path.mkdir(parents=True, exist_ok=True)
  485. @classmethod
  486. def _path_filter(cls, path: str) -> bool:
  487. if ".gitmodules" in path:
  488. return True
  489. if path.startswith(".git"):
  490. return False
  491. return True
  492. @classmethod
  493. def _external_repo_path_filter(cls, path: str) -> bool:
  494. if not cls._path_filter(path):
  495. return False
  496. if path.startswith("test/") or path.startswith("tests/"):
  497. return False
  498. return True
  499. def create_source_archives(self) -> None:
  500. source_collector = SourceCollector(root=self.root, commit=self.commit, executer=self.executer, filter=self._path_filter)
  501. print(f"Collecting sources of {self.project}...")
  502. archive_tree = source_collector.get_archive_file_tree()
  503. latest_mod_time = archive_tree.get_latest_mod_time()
  504. archive_tree.add_file(NodeInArchive.from_text(arcpath=REVISION_TXT, text=f"{self.revision}\n", time=latest_mod_time))
  505. archive_tree.add_file(NodeInArchive.from_text(arcpath=f"{GIT_HASH_FILENAME}", text=f"{self.commit}\n", time=latest_mod_time))
  506. archive_tree.add_file_mapping(arc_dir="", file_mapping=self.release_info["source"].get("files", {}), file_mapping_root=self.root, context=self.get_context(), time=latest_mod_time)
  507. archive_base = f"{self.project}-{self.version}"
  508. zip_path = self.dist_path / f"{archive_base}.zip"
  509. tgz_path = self.dist_path / f"{archive_base}.tar.gz"
  510. txz_path = self.dist_path / f"{archive_base}.tar.xz"
  511. logger.info("Creating zip/tgz/txz source archives ...")
  512. if self.dry:
  513. zip_path.touch()
  514. tgz_path.touch()
  515. txz_path.touch()
  516. else:
  517. with Archiver(zip_path=zip_path, tgz_path=tgz_path, txz_path=txz_path) as archiver:
  518. print(f"Adding source files of {self.project}...")
  519. archive_tree.add_to_archiver(archive_base=archive_base, archiver=archiver)
  520. for extra_repo in self.release_info["source"].get("extra-repos", []):
  521. extra_repo_root = self.root / extra_repo
  522. assert (extra_repo_root / ".git").exists(), f"{extra_repo_root} must be a git repo"
  523. extra_repo_commit = self.executer.check_output(["git", "rev-parse", "HEAD"], dry_out=f"gitsha-extra-repo-{extra_repo}", cwd=extra_repo_root).strip()
  524. extra_repo_source_collector = SourceCollector(root=extra_repo_root, commit=extra_repo_commit, executer=self.executer, filter=self._external_repo_path_filter)
  525. print(f"Collecting sources of {extra_repo} ...")
  526. extra_repo_archive_tree = extra_repo_source_collector.get_archive_file_tree()
  527. print(f"Adding source files of {extra_repo} ...")
  528. extra_repo_archive_tree.add_to_archiver(archive_base=f"{archive_base}/{extra_repo}", archiver=archiver)
  529. for file in self.release_info["source"]["checks"]:
  530. assert f"{archive_base}/{file}" in archiver.added_files, f"'{archive_base}/{file}' must exist"
  531. logger.info("... done")
  532. self.artifacts["src-zip"] = zip_path
  533. self.artifacts["src-tar-gz"] = tgz_path
  534. self.artifacts["src-tar-xz"] = txz_path
  535. if not self.dry:
  536. with tgz_path.open("r+b") as f:
  537. # Zero the embedded timestamp in the gzip'ed tarball
  538. f.seek(4, 0)
  539. f.write(b"\x00\x00\x00\x00")
  540. def create_dmg(self, configuration: str="Release") -> None:
  541. dmg_in = self.root / self.release_info["dmg"]["path"]
  542. xcode_project = self.root / self.release_info["dmg"]["project"]
  543. assert xcode_project.is_dir(), f"{xcode_project} must be a directory"
  544. assert (xcode_project / "project.pbxproj").is_file, f"{xcode_project} must contain project.pbxproj"
  545. if not self.fast:
  546. dmg_in.unlink(missing_ok=True)
  547. build_xcconfig = self.release_info["dmg"].get("build-xcconfig")
  548. if build_xcconfig:
  549. shutil.copy(self.root / build_xcconfig, xcode_project.parent / "build.xcconfig")
  550. xcode_scheme = self.release_info["dmg"].get("scheme")
  551. xcode_target = self.release_info["dmg"].get("target")
  552. assert xcode_scheme or xcode_target, "dmg needs scheme or target"
  553. assert not (xcode_scheme and xcode_target), "dmg cannot have both scheme and target set"
  554. if xcode_scheme:
  555. scheme_or_target = "-scheme"
  556. target_like = xcode_scheme
  557. else:
  558. scheme_or_target = "-target"
  559. target_like = xcode_target
  560. self.executer.run(["xcodebuild", "ONLY_ACTIVE_ARCH=NO", "-project", xcode_project, scheme_or_target, target_like, "-configuration", configuration])
  561. if self.dry:
  562. dmg_in.parent.mkdir(parents=True, exist_ok=True)
  563. dmg_in.touch()
  564. assert dmg_in.is_file(), f"{self.project}.dmg was not created by xcodebuild"
  565. dmg_out = self.dist_path / f"{self.project}-{self.version}.dmg"
  566. shutil.copy(dmg_in, dmg_out)
  567. self.artifacts["dmg"] = dmg_out
  568. @property
  569. def git_hash_data(self) -> bytes:
  570. return f"{self.commit}\n".encode()
  571. def create_mingw_archives(self) -> None:
  572. build_type = "Release"
  573. build_parent_dir = self.root / "build-mingw"
  574. ARCH_TO_GNU_ARCH = {
  575. # "arm64": "aarch64",
  576. "x86": "i686",
  577. "x64": "x86_64",
  578. }
  579. ARCH_TO_TRIPLET = {
  580. # "arm64": "aarch64-w64-mingw32",
  581. "x86": "i686-w64-mingw32",
  582. "x64": "x86_64-w64-mingw32",
  583. }
  584. new_env = dict(os.environ)
  585. cmake_prefix_paths = []
  586. mingw_deps_path = self.deps_path / "mingw-deps"
  587. if "dependencies" in self.release_info["mingw"]:
  588. shutil.rmtree(mingw_deps_path, ignore_errors=True)
  589. mingw_deps_path.mkdir()
  590. for triplet in ARCH_TO_TRIPLET.values():
  591. (mingw_deps_path / triplet).mkdir()
  592. def extract_filter(member: tarfile.TarInfo, path: str, /):
  593. if member.name.startswith("SDL"):
  594. member.name = "/".join(Path(member.name).parts[1:])
  595. return member
  596. for dep in self.release_info.get("dependencies", {}):
  597. extract_path = mingw_deps_path / f"extract-{dep}"
  598. extract_path.mkdir()
  599. with chdir(extract_path):
  600. tar_path = self.deps_path / glob.glob(self.release_info["mingw"]["dependencies"][dep]["artifact"], root_dir=self.deps_path)[0]
  601. logger.info("Extracting %s to %s", tar_path, mingw_deps_path)
  602. assert tar_path.suffix in (".gz", ".xz")
  603. with tarfile.open(tar_path, mode=f"r:{tar_path.suffix.strip('.')}") as tarf:
  604. tarf.extractall(filter=extract_filter)
  605. for arch, triplet in ARCH_TO_TRIPLET.items():
  606. install_cmd = self.release_info["mingw"]["dependencies"][dep]["install-command"]
  607. extra_configure_data = {
  608. "ARCH": ARCH_TO_GNU_ARCH[arch],
  609. "TRIPLET": triplet,
  610. "PREFIX": str(mingw_deps_path / triplet),
  611. }
  612. install_cmd = configure_text(install_cmd, context=self.get_context(extra_configure_data))
  613. self.executer.run(shlex.split(install_cmd), cwd=str(extract_path))
  614. dep_binpath = mingw_deps_path / triplet / "bin"
  615. assert dep_binpath.is_dir(), f"{dep_binpath} for PATH should exist"
  616. dep_pkgconfig = mingw_deps_path / triplet / "lib/pkgconfig"
  617. assert dep_pkgconfig.is_dir(), f"{dep_pkgconfig} for PKG_CONFIG_PATH should exist"
  618. new_env["PATH"] = os.pathsep.join([str(dep_binpath), new_env["PATH"]])
  619. new_env["PKG_CONFIG_PATH"] = str(dep_pkgconfig)
  620. cmake_prefix_paths.append(mingw_deps_path)
  621. new_env["CFLAGS"] = f"-O2 -ffile-prefix-map={self.root}=/src/{self.project}"
  622. new_env["CXXFLAGS"] = f"-O2 -ffile-prefix-map={self.root}=/src/{self.project}"
  623. assert any(system in self.release_info["mingw"] for system in ("autotools", "cmake"))
  624. assert not all(system in self.release_info["mingw"] for system in ("autotools", "cmake"))
  625. mingw_archs = set()
  626. arc_root = f"{self.project}-{self.version}"
  627. archive_file_tree = ArchiveFileTree()
  628. if "autotools" in self.release_info["mingw"]:
  629. for arch in self.release_info["mingw"]["autotools"]["archs"]:
  630. triplet = ARCH_TO_TRIPLET[arch]
  631. new_env["CC"] = f"{triplet}-gcc"
  632. new_env["CXX"] = f"{triplet}-g++"
  633. new_env["RC"] = f"{triplet}-windres"
  634. assert arch not in mingw_archs
  635. mingw_archs.add(arch)
  636. build_path = build_parent_dir / f"build-{triplet}"
  637. install_path = build_parent_dir / f"install-{triplet}"
  638. shutil.rmtree(install_path, ignore_errors=True)
  639. build_path.mkdir(parents=True, exist_ok=True)
  640. context = self.get_context({
  641. "ARCH": arch,
  642. "DEP_PREFIX": str(mingw_deps_path / triplet),
  643. })
  644. extra_args = configure_text_list(text_list=self.release_info["mingw"]["autotools"]["args"], context=context)
  645. with self.section_printer.group(f"Configuring MinGW {triplet} (autotools)"):
  646. assert "@" not in " ".join(extra_args), f"@ should not be present in extra arguments ({extra_args})"
  647. self.executer.run([
  648. self.root / "configure",
  649. f"--prefix={install_path}",
  650. f"--includedir=${{prefix}}/include",
  651. f"--libdir=${{prefix}}/lib",
  652. f"--bindir=${{prefix}}/bin",
  653. f"--exec-prefix=${{prefix}}/bin",
  654. f"--host={triplet}",
  655. f"--build=x86_64-none-linux-gnu",
  656. "CFLAGS=-O2",
  657. "CXXFLAGS=-O2",
  658. "LDFLAGS=-Wl,-s",
  659. ] + extra_args, cwd=build_path, env=new_env)
  660. with self.section_printer.group(f"Build MinGW {triplet} (autotools)"):
  661. self.executer.run(["make", "V=1", f"-j{self.cpu_count}"], cwd=build_path, env=new_env)
  662. with self.section_printer.group(f"Install MinGW {triplet} (autotools)"):
  663. self.executer.run(["make", "install"], cwd=build_path, env=new_env)
  664. archive_file_tree.add_directory_tree(arc_dir=arc_join(arc_root, triplet), path=install_path, time=self.arc_time)
  665. print("Recording arch-dependent extra files for MinGW development archive ...")
  666. extra_context = {
  667. "TRIPLET": ARCH_TO_TRIPLET[arch],
  668. }
  669. archive_file_tree.add_file_mapping(arc_dir=arc_root, file_mapping=self.release_info["mingw"]["autotools"].get("files", {}), file_mapping_root=self.root, context=self.get_context(extra_context=extra_context), time=self.arc_time)
  670. if "cmake" in self.release_info["mingw"]:
  671. assert self.release_info["mingw"]["cmake"]["shared-static"] in ("args", "both")
  672. for arch in self.release_info["mingw"]["cmake"]["archs"]:
  673. triplet = ARCH_TO_TRIPLET[arch]
  674. new_env["CC"] = f"{triplet}-gcc"
  675. new_env["CXX"] = f"{triplet}-g++"
  676. new_env["RC"] = f"{triplet}-windres"
  677. assert arch not in mingw_archs
  678. mingw_archs.add(arch)
  679. context = self.get_context({
  680. "ARCH": arch,
  681. "DEP_PREFIX": str(mingw_deps_path / triplet),
  682. })
  683. extra_args = configure_text_list(text_list=self.release_info["mingw"]["cmake"]["args"], context=context)
  684. build_path = build_parent_dir / f"build-{triplet}"
  685. install_path = build_parent_dir / f"install-{triplet}"
  686. shutil.rmtree(install_path, ignore_errors=True)
  687. build_path.mkdir(parents=True, exist_ok=True)
  688. if self.release_info["mingw"]["cmake"]["shared-static"] == "args":
  689. args_for_shared_static = ([], )
  690. elif self.release_info["mingw"]["cmake"]["shared-static"] == "both":
  691. args_for_shared_static = (["-DBUILD_SHARED_LIBS=ON"], ["-DBUILD_SHARED_LIBS=OFF"])
  692. for arg_for_shared_static in args_for_shared_static:
  693. with self.section_printer.group(f"Configuring MinGW {triplet} (CMake)"):
  694. assert "@" not in " ".join(extra_args), f"@ should not be present in extra arguments ({extra_args})"
  695. self.executer.run([
  696. f"cmake",
  697. f"-S", str(self.root), "-B", str(build_path),
  698. f"-DCMAKE_BUILD_TYPE={build_type}",
  699. f'''-DCMAKE_C_FLAGS="-ffile-prefix-map={self.root}=/src/{self.project}"''',
  700. f'''-DCMAKE_CXX_FLAGS="-ffile-prefix-map={self.root}=/src/{self.project}"''',
  701. f"-DCMAKE_PREFIX_PATH={mingw_deps_path / triplet}",
  702. f"-DCMAKE_INSTALL_PREFIX={install_path}",
  703. f"-DCMAKE_INSTALL_INCLUDEDIR=include",
  704. f"-DCMAKE_INSTALL_LIBDIR=lib",
  705. f"-DCMAKE_INSTALL_BINDIR=bin",
  706. f"-DCMAKE_INSTALL_DATAROOTDIR=share",
  707. f"-DCMAKE_TOOLCHAIN_FILE={self.root}/build-scripts/cmake-toolchain-mingw64-{ARCH_TO_GNU_ARCH[arch]}.cmake",
  708. f"-G{self.cmake_generator}",
  709. ] + extra_args + ([] if self.fast else ["--fresh"]) + arg_for_shared_static, cwd=build_path, env=new_env)
  710. with self.section_printer.group(f"Build MinGW {triplet} (CMake)"):
  711. self.executer.run(["cmake", "--build", str(build_path), "--verbose", "--config", build_type], cwd=build_path, env=new_env)
  712. with self.section_printer.group(f"Install MinGW {triplet} (CMake)"):
  713. self.executer.run(["cmake", "--install", str(build_path)], cwd=build_path, env=new_env)
  714. archive_file_tree.add_directory_tree(arc_dir=arc_join(arc_root, triplet), path=install_path, time=self.arc_time)
  715. print("Recording arch-dependent extra files for MinGW development archive ...")
  716. extra_context = {
  717. "TRIPLET": ARCH_TO_TRIPLET[arch],
  718. }
  719. archive_file_tree.add_file_mapping(arc_dir=arc_root, file_mapping=self.release_info["mingw"]["cmake"].get("files", {}), file_mapping_root=self.root, context=self.get_context(extra_context=extra_context), time=self.arc_time)
  720. print("... done")
  721. print("Recording extra files for MinGW development archive ...")
  722. archive_file_tree.add_file_mapping(arc_dir=arc_root, file_mapping=self.release_info["mingw"].get("files", {}), file_mapping_root=self.root, context=self.get_context(), time=self.arc_time)
  723. print("... done")
  724. print("Creating zip/tgz/txz development archives ...")
  725. zip_path = self.dist_path / f"{self.project}-devel-{self.version}-mingw.zip"
  726. tgz_path = self.dist_path / f"{self.project}-devel-{self.version}-mingw.tar.gz"
  727. txz_path = self.dist_path / f"{self.project}-devel-{self.version}-mingw.tar.xz"
  728. with Archiver(zip_path=zip_path, tgz_path=tgz_path, txz_path=txz_path) as archiver:
  729. archive_file_tree.add_to_archiver(archive_base="", archiver=archiver)
  730. archiver.add_git_hash(arcdir=arc_root, commit=self.commit, time=self.arc_time)
  731. print("... done")
  732. self.artifacts["mingw-devel-zip"] = zip_path
  733. self.artifacts["mingw-devel-tar-gz"] = tgz_path
  734. self.artifacts["mingw-devel-tar-xz"] = txz_path
  735. def _detect_android_api(self, android_home: str) -> typing.Optional[int]:
  736. platform_dirs = list(Path(p) for p in glob.glob(f"{android_home}/platforms/android-*"))
  737. re_platform = re.compile("android-([0-9]+)")
  738. platform_versions = []
  739. for platform_dir in platform_dirs:
  740. logger.debug("Found Android Platform SDK: %s", platform_dir)
  741. if m:= re_platform.match(platform_dir.name):
  742. platform_versions.append(int(m.group(1)))
  743. platform_versions.sort()
  744. logger.info("Available platform versions: %s", platform_versions)
  745. platform_versions = list(filter(lambda v: v >= self._android_api_minimum, platform_versions))
  746. logger.info("Valid platform versions (>=%d): %s", self._android_api_minimum, platform_versions)
  747. if not platform_versions:
  748. return None
  749. android_api = platform_versions[0]
  750. logger.info("Selected API version %d", android_api)
  751. return android_api
  752. def _get_prefab_json_text(self) -> str:
  753. return textwrap.dedent(f"""\
  754. {{
  755. "schema_version": 2,
  756. "name": "{self.project}",
  757. "version": "{self.version}",
  758. "dependencies": []
  759. }}
  760. """)
  761. def _get_prefab_module_json_text(self, library_name: typing.Optional[str], export_libraries: list[str]) -> str:
  762. for lib in export_libraries:
  763. assert isinstance(lib, str), f"{lib} must be a string"
  764. module_json_dict = {
  765. "export_libraries": export_libraries,
  766. }
  767. if library_name:
  768. module_json_dict["library_name"] = f"lib{library_name}"
  769. return json.dumps(module_json_dict, indent=4)
  770. @property
  771. def _android_api_minimum(self):
  772. return self.release_info["android"]["api-minimum"]
  773. @property
  774. def _android_api_target(self):
  775. return self.release_info["android"]["api-target"]
  776. @property
  777. def _android_ndk_minimum(self):
  778. return self.release_info["android"]["ndk-minimum"]
  779. def _get_prefab_abi_json_text(self, abi: str, cpp: bool, shared: bool) -> str:
  780. abi_json_dict = {
  781. "abi": abi,
  782. "api": self._android_api_minimum,
  783. "ndk": self._android_ndk_minimum,
  784. "stl": "c++_shared" if cpp else "none",
  785. "static": not shared,
  786. }
  787. return json.dumps(abi_json_dict, indent=4)
  788. def _get_android_manifest_text(self) -> str:
  789. return textwrap.dedent(f"""\
  790. <manifest
  791. xmlns:android="http://schemas.android.com/apk/res/android"
  792. package="org.libsdl.android.{self.project}" android:versionCode="1"
  793. android:versionName="1.0">
  794. <uses-sdk android:minSdkVersion="{self._android_api_minimum}"
  795. android:targetSdkVersion="{self._android_api_target}" />
  796. </manifest>
  797. """)
  798. def create_android_archives(self, android_api: int, android_home: Path, android_ndk_home: Path) -> None:
  799. cmake_toolchain_file = Path(android_ndk_home) / "build/cmake/android.toolchain.cmake"
  800. if not cmake_toolchain_file.exists():
  801. logger.error("CMake toolchain file does not exist (%s)", cmake_toolchain_file)
  802. raise SystemExit(1)
  803. aar_path = self.dist_path / f"{self.project}-{self.version}.aar"
  804. android_abis = self.release_info["android"]["abis"]
  805. java_jars_added = False
  806. module_data_added = False
  807. android_deps_path = self.deps_path / "android-deps"
  808. shutil.rmtree(android_deps_path, ignore_errors=True)
  809. for dep, depinfo in self.release_info["android"].get("dependencies", {}).items():
  810. android_aar = self.deps_path / glob.glob(depinfo["artifact"], root_dir=self.deps_path)[0]
  811. with self.section_printer.group(f"Extracting Android dependency {dep} ({android_aar.name})"):
  812. self.executer.run([sys.executable, str(android_aar), "-o", str(android_deps_path)])
  813. for module_name, module_info in self.release_info["android"]["modules"].items():
  814. assert "type" in module_info and module_info["type"] in ("interface", "library"), f"module {module_name} must have a valid type"
  815. archive_file_tree = ArchiveFileTree()
  816. for android_abi in android_abis:
  817. with self.section_printer.group(f"Building for Android {android_api} {android_abi}"):
  818. build_dir = self.root / "build-android" / f"{android_abi}-build"
  819. install_dir = self.root / "install-android" / f"{android_abi}-install"
  820. shutil.rmtree(install_dir, ignore_errors=True)
  821. assert not install_dir.is_dir(), f"{install_dir} should not exist prior to build"
  822. build_type = "Release"
  823. cmake_args = [
  824. "cmake",
  825. "-S", str(self.root),
  826. "-B", str(build_dir),
  827. f'''-DCMAKE_C_FLAGS="-ffile-prefix-map={self.root}=/src/{self.project}"''',
  828. f'''-DCMAKE_CXX_FLAGS="-ffile-prefix-map={self.root}=/src/{self.project}"''',
  829. f"-DCMAKE_TOOLCHAIN_FILE={cmake_toolchain_file}",
  830. f"-DCMAKE_PREFIX_PATH={str(android_deps_path)}",
  831. f"-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH",
  832. f"-DANDROID_HOME={android_home}",
  833. f"-DANDROID_PLATFORM={android_api}",
  834. f"-DANDROID_ABI={android_abi}",
  835. "-DCMAKE_POSITION_INDEPENDENT_CODE=ON",
  836. f"-DCMAKE_INSTALL_PREFIX={install_dir}",
  837. "-DCMAKE_INSTALL_INCLUDEDIR=include ",
  838. "-DCMAKE_INSTALL_LIBDIR=lib",
  839. "-DCMAKE_INSTALL_DATAROOTDIR=share",
  840. f"-DCMAKE_BUILD_TYPE={build_type}",
  841. f"-G{self.cmake_generator}",
  842. ] + self.release_info["android"]["cmake"]["args"] + ([] if self.fast else ["--fresh"])
  843. build_args = [
  844. "cmake",
  845. "--build", str(build_dir),
  846. "--verbose",
  847. "--config", build_type,
  848. ]
  849. install_args = [
  850. "cmake",
  851. "--install", str(build_dir),
  852. "--config", build_type,
  853. ]
  854. self.executer.run(cmake_args)
  855. self.executer.run(build_args)
  856. self.executer.run(install_args)
  857. for module_name, module_info in self.release_info["android"]["modules"].items():
  858. arcdir_prefab_module = f"prefab/modules/{module_name}"
  859. if module_info["type"] == "library":
  860. library = install_dir / module_info["library"]
  861. assert library.suffix in (".so", ".a")
  862. assert library.is_file(), f"CMake should have built library '{library}' for module {module_name}"
  863. arcdir_prefab_libs = f"{arcdir_prefab_module}/libs/android.{android_abi}"
  864. archive_file_tree.add_file(NodeInArchive.from_fs(arcpath=f"{arcdir_prefab_libs}/{library.name}", path=library, time=self.arc_time))
  865. archive_file_tree.add_file(NodeInArchive.from_text(arcpath=f"{arcdir_prefab_libs}/abi.json", text=self._get_prefab_abi_json_text(abi=android_abi, cpp=False, shared=library.suffix == ".so"), time=self.arc_time))
  866. if not module_data_added:
  867. library_name = None
  868. if module_info["type"] == "library":
  869. library_name = Path(module_info["library"]).stem.removeprefix("lib")
  870. export_libraries = module_info.get("export-libraries", [])
  871. archive_file_tree.add_file(NodeInArchive.from_text(arcpath=arc_join(arcdir_prefab_module, "module.json"), text=self._get_prefab_module_json_text(library_name=library_name, export_libraries=export_libraries), time=self.arc_time))
  872. arcdir_prefab_include = f"prefab/modules/{module_name}/include"
  873. if "includes" in module_info:
  874. archive_file_tree.add_file_mapping(arc_dir=arcdir_prefab_include, file_mapping=module_info["includes"], file_mapping_root=install_dir, context=self.get_context(), time=self.arc_time)
  875. else:
  876. archive_file_tree.add_file(NodeInArchive.from_text(arcpath=arc_join(arcdir_prefab_include, ".keep"), text="\n", time=self.arc_time))
  877. module_data_added = True
  878. if not java_jars_added:
  879. java_jars_added = True
  880. if "jars" in self.release_info["android"]:
  881. classes_jar_path = install_dir / configure_text(text=self.release_info["android"]["jars"]["classes"], context=self.get_context())
  882. sources_jar_path = install_dir / configure_text(text=self.release_info["android"]["jars"]["sources"], context=self.get_context())
  883. doc_jar_path = install_dir / configure_text(text=self.release_info["android"]["jars"]["doc"], context=self.get_context())
  884. assert classes_jar_path.is_file(), f"CMake should have compiled the java sources and archived them into a JAR ({classes_jar_path})"
  885. assert sources_jar_path.is_file(), f"CMake should have archived the java sources into a JAR ({sources_jar_path})"
  886. assert doc_jar_path.is_file(), f"CMake should have archived javadoc into a JAR ({doc_jar_path})"
  887. archive_file_tree.add_file(NodeInArchive.from_fs(arcpath="classes.jar", path=classes_jar_path, time=self.arc_time))
  888. archive_file_tree.add_file(NodeInArchive.from_fs(arcpath="classes-sources.jar", path=sources_jar_path, time=self.arc_time))
  889. archive_file_tree.add_file(NodeInArchive.from_fs(arcpath="classes-doc.jar", path=doc_jar_path, time=self.arc_time))
  890. assert ("jars" in self.release_info["android"] and java_jars_added) or "jars" not in self.release_info["android"], "Must have archived java JAR archives"
  891. archive_file_tree.add_file_mapping(arc_dir="", file_mapping=self.release_info["android"].get("files", {}), file_mapping_root=self.root, context=self.get_context(), time=self.arc_time)
  892. archive_file_tree.add_file(NodeInArchive.from_text(arcpath="prefab/prefab.json", text=self._get_prefab_json_text(), time=self.arc_time))
  893. archive_file_tree.add_file(NodeInArchive.from_text(arcpath="AndroidManifest.xml", text=self._get_android_manifest_text(), time=self.arc_time))
  894. with Archiver(zip_path=aar_path) as archiver:
  895. archive_file_tree.add_to_archiver(archive_base="", archiver=archiver)
  896. archiver.add_git_hash(arcdir="", commit=self.commit, time=self.arc_time)
  897. self.artifacts[f"android-aar"] = aar_path
  898. def download_dependencies(self):
  899. shutil.rmtree(self.deps_path, ignore_errors=True)
  900. self.deps_path.mkdir(parents=True)
  901. if self.github:
  902. with open(os.environ["GITHUB_OUTPUT"], "a") as f:
  903. f.write(f"dep-path={self.deps_path.absolute()}\n")
  904. for dep, depinfo in self.release_info.get("dependencies", {}).items():
  905. startswith = depinfo["startswith"]
  906. dep_repo = depinfo["repo"]
  907. # FIXME: dropped "--exclude-pre-releases"
  908. dep_string_data = self.executer.check_output(["gh", "-R", dep_repo, "release", "list", "--exclude-drafts", "--json", "name,createdAt,tagName", "--jq", f'[.[]|select(.name|startswith("{startswith}"))]|max_by(.createdAt)']).strip()
  909. dep_data = json.loads(dep_string_data)
  910. dep_tag = dep_data["tagName"]
  911. dep_version = dep_data["name"]
  912. logger.info("Download dependency %s version %s (tag=%s) ", dep, dep_version, dep_tag)
  913. self.executer.run(["gh", "-R", dep_repo, "release", "download", dep_tag], cwd=self.deps_path)
  914. if self.github:
  915. with open(os.environ["GITHUB_OUTPUT"], "a") as f:
  916. f.write(f"dep-{dep.lower()}-version={dep_version}\n")
  917. def verify_dependencies(self):
  918. for dep, depinfo in self.release_info.get("dependencies", {}).items():
  919. if "mingw" in self.release_info:
  920. mingw_matches = glob.glob(self.release_info["mingw"]["dependencies"][dep]["artifact"], root_dir=self.deps_path)
  921. assert len(mingw_matches) == 1, f"Exactly one archive matches mingw {dep} dependency: {mingw_matches}"
  922. if "dmg" in self.release_info:
  923. dmg_matches = glob.glob(self.release_info["dmg"]["dependencies"][dep]["artifact"], root_dir=self.deps_path)
  924. assert len(dmg_matches) == 1, f"Exactly one archive matches dmg {dep} dependency: {dmg_matches}"
  925. if "msvc" in self.release_info:
  926. msvc_matches = glob.glob(self.release_info["msvc"]["dependencies"][dep]["artifact"], root_dir=self.deps_path)
  927. assert len(msvc_matches) == 1, f"Exactly one archive matches msvc {dep} dependency: {msvc_matches}"
  928. if "android" in self.release_info:
  929. android_matches = glob.glob(self.release_info["android"]["dependencies"][dep]["artifact"], root_dir=self.deps_path)
  930. assert len(android_matches) == 1, f"Exactly one archive matches msvc {dep} dependency: {msvc_matches}"
  931. @staticmethod
  932. def _arch_to_vs_platform(arch: str, configuration: str="Release") -> VsArchPlatformConfig:
  933. ARCH_TO_VS_PLATFORM = {
  934. "x86": VsArchPlatformConfig(arch="x86", platform="Win32", configuration=configuration),
  935. "x64": VsArchPlatformConfig(arch="x64", platform="x64", configuration=configuration),
  936. "arm64": VsArchPlatformConfig(arch="arm64", platform="ARM64", configuration=configuration),
  937. }
  938. return ARCH_TO_VS_PLATFORM[arch]
  939. def build_msvc(self):
  940. with self.section_printer.group("Find Visual Studio"):
  941. vs = VisualStudio(executer=self.executer)
  942. for arch in self.release_info["msvc"].get("msbuild", {}).get("archs", []):
  943. self._build_msvc_msbuild(arch_platform=self._arch_to_vs_platform(arch=arch), vs=vs)
  944. if "cmake" in self.release_info["msvc"]:
  945. deps_path = self.root / "msvc-deps"
  946. shutil.rmtree(deps_path, ignore_errors=True)
  947. dep_roots = []
  948. for dep, depinfo in self.release_info["msvc"].get("dependencies", {}).items():
  949. dep_extract_path = deps_path / f"extract-{dep}"
  950. msvc_zip = self.deps_path / glob.glob(depinfo["artifact"], root_dir=self.deps_path)[0]
  951. with zipfile.ZipFile(msvc_zip, "r") as zf:
  952. zf.extractall(dep_extract_path)
  953. contents_msvc_zip = glob.glob(str(dep_extract_path / "*"))
  954. assert len(contents_msvc_zip) == 1, f"There must be exactly one root item in the root directory of {dep}"
  955. dep_roots.append(contents_msvc_zip[0])
  956. for arch in self.release_info["msvc"].get("cmake", {}).get("archs", []):
  957. self._build_msvc_cmake(arch_platform=self._arch_to_vs_platform(arch=arch), dep_roots=dep_roots)
  958. with self.section_printer.group("Create SDL VC development zip"):
  959. self._build_msvc_devel()
  960. def _build_msvc_msbuild(self, arch_platform: VsArchPlatformConfig, vs: VisualStudio):
  961. platform_context = self.get_context(arch_platform.extra_context())
  962. for dep, depinfo in self.release_info["msvc"].get("dependencies", {}).items():
  963. msvc_zip = self.deps_path / glob.glob(depinfo["artifact"], root_dir=self.deps_path)[0]
  964. src_globs = [configure_text(instr["src"], context=platform_context) for instr in depinfo["copy"]]
  965. with zipfile.ZipFile(msvc_zip, "r") as zf:
  966. for member in zf.namelist():
  967. member_path = "/".join(Path(member).parts[1:])
  968. for src_i, src_glob in enumerate(src_globs):
  969. if fnmatch.fnmatch(member_path, src_glob):
  970. dst = (self.root / configure_text(depinfo["copy"][src_i]["dst"], context=platform_context)).resolve() / Path(member_path).name
  971. zip_data = zf.read(member)
  972. if dst.exists():
  973. identical = False
  974. if dst.is_file():
  975. orig_bytes = dst.read_bytes()
  976. if orig_bytes == zip_data:
  977. identical = True
  978. if not identical:
  979. logger.warning("Extracting dependency %s, will cause %s to be overwritten", dep, dst)
  980. if not self.overwrite:
  981. raise RuntimeError("Run with --overwrite to allow overwriting")
  982. logger.debug("Extracting %s -> %s", member, dst)
  983. dst.parent.mkdir(exist_ok=True, parents=True)
  984. dst.write_bytes(zip_data)
  985. prebuilt_paths = set(self.root / full_prebuilt_path for prebuilt_path in self.release_info["msvc"]["msbuild"].get("prebuilt", []) for full_prebuilt_path in glob.glob(configure_text(prebuilt_path, context=platform_context), root_dir=self.root))
  986. msbuild_paths = set(self.root / configure_text(f, context=platform_context) for file_mapping in (self.release_info["msvc"]["msbuild"]["files-lib"], self.release_info["msvc"]["msbuild"]["files-devel"]) for files_list in file_mapping.values() for f in files_list)
  987. assert prebuilt_paths.issubset(msbuild_paths), f"msvc.msbuild.prebuilt must be a subset of (msvc.msbuild.files-lib, msvc.msbuild.files-devel)"
  988. built_paths = msbuild_paths.difference(prebuilt_paths)
  989. logger.info("MSbuild builds these files, to be included in the package: %s", built_paths)
  990. if not self.fast:
  991. for b in built_paths:
  992. b.unlink(missing_ok=True)
  993. rel_projects: list[str] = self.release_info["msvc"]["msbuild"]["projects"]
  994. projects = list(self.root / p for p in rel_projects)
  995. directory_build_props_src_relpath = self.release_info["msvc"]["msbuild"].get("directory-build-props")
  996. for project in projects:
  997. dir_b_props = project.parent / "Directory.Build.props"
  998. dir_b_props.unlink(missing_ok = True)
  999. if directory_build_props_src_relpath:
  1000. src = self.root / directory_build_props_src_relpath
  1001. logger.debug("Copying %s -> %s", src, dir_b_props)
  1002. shutil.copy(src=src, dst=dir_b_props)
  1003. with self.section_printer.group(f"Build {arch_platform.arch} VS binary"):
  1004. vs.build(arch_platform=arch_platform, projects=projects)
  1005. if self.dry:
  1006. for b in built_paths:
  1007. b.parent.mkdir(parents=True, exist_ok=True)
  1008. b.touch()
  1009. for b in built_paths:
  1010. assert b.is_file(), f"{b} has not been created"
  1011. b.parent.mkdir(parents=True, exist_ok=True)
  1012. b.touch()
  1013. zip_path = self.dist_path / f"{self.project}-{self.version}-win32-{arch_platform.arch}.zip"
  1014. zip_path.unlink(missing_ok=True)
  1015. logger.info("Collecting files...")
  1016. archive_file_tree = ArchiveFileTree()
  1017. archive_file_tree.add_file_mapping(arc_dir="", file_mapping=self.release_info["msvc"]["msbuild"]["files-lib"], file_mapping_root=self.root, context=platform_context, time=self.arc_time)
  1018. archive_file_tree.add_file_mapping(arc_dir="", file_mapping=self.release_info["msvc"]["files-lib"], file_mapping_root=self.root, context=platform_context, time=self.arc_time)
  1019. logger.info("Writing to %s", zip_path)
  1020. with Archiver(zip_path=zip_path) as archiver:
  1021. arc_root = f""
  1022. archive_file_tree.add_to_archiver(archive_base=arc_root, archiver=archiver)
  1023. archiver.add_git_hash(arcdir=arc_root, commit=self.commit, time=self.arc_time)
  1024. self.artifacts[f"VC-{arch_platform.arch}"] = zip_path
  1025. for p in built_paths:
  1026. assert p.is_file(), f"{p} should exist"
  1027. def _arch_platform_to_build_path(self, arch_platform: VsArchPlatformConfig) -> Path:
  1028. return self.root / f"build-vs-{arch_platform.arch}"
  1029. def _arch_platform_to_install_path(self, arch_platform: VsArchPlatformConfig) -> Path:
  1030. return self._arch_platform_to_build_path(arch_platform) / "prefix"
  1031. def _build_msvc_cmake(self, arch_platform: VsArchPlatformConfig, dep_roots: list[Path]):
  1032. build_path = self._arch_platform_to_build_path(arch_platform)
  1033. install_path = self._arch_platform_to_install_path(arch_platform)
  1034. platform_context = self.get_context(extra_context=arch_platform.extra_context())
  1035. build_type = "Release"
  1036. built_paths = set(install_path / configure_text(f, context=platform_context) for file_mapping in (self.release_info["msvc"]["cmake"]["files-lib"], self.release_info["msvc"]["cmake"]["files-devel"]) for files_list in file_mapping.values() for f in files_list)
  1037. logger.info("CMake builds these files, to be included in the package: %s", built_paths)
  1038. if not self.fast:
  1039. for b in built_paths:
  1040. b.unlink(missing_ok=True)
  1041. shutil.rmtree(install_path, ignore_errors=True)
  1042. build_path.mkdir(parents=True, exist_ok=True)
  1043. with self.section_printer.group(f"Configure VC CMake project for {arch_platform.arch}"):
  1044. self.executer.run([
  1045. "cmake", "-S", str(self.root), "-B", str(build_path),
  1046. "-A", arch_platform.platform,
  1047. "-DCMAKE_INSTALL_BINDIR=bin",
  1048. "-DCMAKE_INSTALL_DATAROOTDIR=share",
  1049. "-DCMAKE_INSTALL_INCLUDEDIR=include",
  1050. "-DCMAKE_INSTALL_LIBDIR=lib",
  1051. f"-DCMAKE_BUILD_TYPE={build_type}",
  1052. f"-DCMAKE_INSTALL_PREFIX={install_path}",
  1053. # MSVC debug information format flags are selected by an abstraction
  1054. "-DCMAKE_POLICY_DEFAULT_CMP0141=NEW",
  1055. # MSVC debug information format
  1056. "-DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=ProgramDatabase",
  1057. # Linker flags for executables
  1058. "-DCMAKE_EXE_LINKER_FLAGS=-INCREMENTAL:NO -DEBUG -OPT:REF -OPT:ICF",
  1059. # Linker flag for shared libraries
  1060. "-DCMAKE_SHARED_LINKER_FLAGS=-INCREMENTAL:NO -DEBUG -OPT:REF -OPT:ICF",
  1061. # MSVC runtime library flags are selected by an abstraction
  1062. "-DCMAKE_POLICY_DEFAULT_CMP0091=NEW",
  1063. # Use statically linked runtime (-MT) (ideally, should be "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  1064. "-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded",
  1065. f"-DCMAKE_PREFIX_PATH={';'.join(str(s) for s in dep_roots)}",
  1066. ] + self.release_info["msvc"]["cmake"]["args"] + ([] if self.fast else ["--fresh"]))
  1067. with self.section_printer.group(f"Build VC CMake project for {arch_platform.arch}"):
  1068. self.executer.run(["cmake", "--build", str(build_path), "--verbose", "--config", build_type])
  1069. with self.section_printer.group(f"Install VC CMake project for {arch_platform.arch}"):
  1070. self.executer.run(["cmake", "--install", str(build_path), "--config", build_type])
  1071. if self.dry:
  1072. for b in built_paths:
  1073. b.parent.mkdir(parents=True, exist_ok=True)
  1074. b.touch()
  1075. zip_path = self.dist_path / f"{self.project}-{self.version}-win32-{arch_platform.arch}.zip"
  1076. zip_path.unlink(missing_ok=True)
  1077. logger.info("Collecting files...")
  1078. archive_file_tree = ArchiveFileTree()
  1079. archive_file_tree.add_file_mapping(arc_dir="", file_mapping=self.release_info["msvc"]["cmake"]["files-lib"], file_mapping_root=install_path, context=platform_context, time=self.arc_time)
  1080. archive_file_tree.add_file_mapping(arc_dir="", file_mapping=self.release_info["msvc"]["files-lib"], file_mapping_root=self.root, context=self.get_context(), time=self.arc_time)
  1081. logger.info("Creating %s", zip_path)
  1082. with Archiver(zip_path=zip_path) as archiver:
  1083. arc_root = f""
  1084. archive_file_tree.add_to_archiver(archive_base=arc_root, archiver=archiver)
  1085. archiver.add_git_hash(arcdir=arc_root, commit=self.commit, time=self.arc_time)
  1086. for p in built_paths:
  1087. assert p.is_file(), f"{p} should exist"
  1088. def _build_msvc_devel(self) -> None:
  1089. zip_path = self.dist_path / f"{self.project}-devel-{self.version}-VC.zip"
  1090. arc_root = f"{self.project}-{self.version}"
  1091. def copy_files_devel(ctx):
  1092. archive_file_tree.add_file_mapping(arc_dir=arc_root, file_mapping=self.release_info["msvc"]["files-devel"], file_mapping_root=self.root, context=ctx, time=self.arc_time)
  1093. logger.info("Collecting files...")
  1094. archive_file_tree = ArchiveFileTree()
  1095. if "msbuild" in self.release_info["msvc"]:
  1096. for arch in self.release_info["msvc"]["msbuild"]["archs"]:
  1097. arch_platform = self._arch_to_vs_platform(arch=arch)
  1098. platform_context = self.get_context(arch_platform.extra_context())
  1099. archive_file_tree.add_file_mapping(arc_dir=arc_root, file_mapping=self.release_info["msvc"]["msbuild"]["files-devel"], file_mapping_root=self.root, context=platform_context, time=self.arc_time)
  1100. copy_files_devel(ctx=platform_context)
  1101. if "cmake" in self.release_info["msvc"]:
  1102. for arch in self.release_info["msvc"]["cmake"]["archs"]:
  1103. arch_platform = self._arch_to_vs_platform(arch=arch)
  1104. platform_context = self.get_context(arch_platform.extra_context())
  1105. archive_file_tree.add_file_mapping(arc_dir=arc_root, file_mapping=self.release_info["msvc"]["cmake"]["files-devel"], file_mapping_root=self._arch_platform_to_install_path(arch_platform), context=platform_context, time=self.arc_time)
  1106. copy_files_devel(ctx=platform_context)
  1107. with Archiver(zip_path=zip_path) as archiver:
  1108. archive_file_tree.add_to_archiver(archive_base="", archiver=archiver)
  1109. archiver.add_git_hash(arcdir=arc_root, commit=self.commit, time=self.arc_time)
  1110. self.artifacts["VC-devel"] = zip_path
  1111. @classmethod
  1112. def extract_sdl_version(cls, root: Path, release_info: dict) -> str:
  1113. with open(root / release_info["version"]["file"], "r") as f:
  1114. text = f.read()
  1115. major = next(re.finditer(release_info["version"]["re_major"], text, flags=re.M)).group(1)
  1116. minor = next(re.finditer(release_info["version"]["re_minor"], text, flags=re.M)).group(1)
  1117. micro = next(re.finditer(release_info["version"]["re_micro"], text, flags=re.M)).group(1)
  1118. return f"{major}.{minor}.{micro}"
  1119. def main(argv=None) -> int:
  1120. if sys.version_info < (3, 11):
  1121. logger.error("This script needs at least python 3.11")
  1122. return 1
  1123. parser = argparse.ArgumentParser(allow_abbrev=False, description="Create SDL release artifacts")
  1124. parser.add_argument("--root", metavar="DIR", type=Path, default=Path(__file__).absolute().parents[1], help="Root of project")
  1125. parser.add_argument("--release-info", metavar="JSON", dest="path_release_info", type=Path, default=Path(__file__).absolute().parent / "release-info.json", help="Path of release-info.json")
  1126. parser.add_argument("--dependency-folder", metavar="FOLDER", dest="deps_path", type=Path, default="deps", help="Directory containing pre-built archives of dependencies (will be removed when downloading archives)")
  1127. parser.add_argument("--out", "-o", metavar="DIR", dest="dist_path", type=Path, default="dist", help="Output directory")
  1128. parser.add_argument("--github", action="store_true", help="Script is running on a GitHub runner")
  1129. parser.add_argument("--commit", default="HEAD", help="Git commit/tag of which a release should be created")
  1130. parser.add_argument("--actions", choices=["download", "source", "android", "mingw", "msvc", "dmg"], required=True, nargs="+", dest="actions", help="What to do?")
  1131. parser.set_defaults(loglevel=logging.INFO)
  1132. parser.add_argument('--vs-year', dest="vs_year", help="Visual Studio year")
  1133. parser.add_argument('--android-api', type=int, dest="android_api", help="Android API version")
  1134. parser.add_argument('--android-home', dest="android_home", default=os.environ.get("ANDROID_HOME"), help="Android Home folder")
  1135. parser.add_argument('--android-ndk-home', dest="android_ndk_home", default=os.environ.get("ANDROID_NDK_HOME"), help="Android NDK Home folder")
  1136. parser.add_argument('--cmake-generator', dest="cmake_generator", default="Ninja", help="CMake Generator")
  1137. parser.add_argument('--debug', action='store_const', const=logging.DEBUG, dest="loglevel", help="Print script debug information")
  1138. parser.add_argument('--dry-run', action='store_true', dest="dry", help="Don't execute anything")
  1139. parser.add_argument('--force', action='store_true', dest="force", help="Ignore a non-clean git tree")
  1140. parser.add_argument('--overwrite', action='store_true', dest="overwrite", help="Allow potentially overwriting other projects")
  1141. parser.add_argument('--fast', action='store_true', dest="fast", help="Don't do a rebuild")
  1142. args = parser.parse_args(argv)
  1143. logging.basicConfig(level=args.loglevel, format='[%(levelname)s] %(message)s')
  1144. args.deps_path = args.deps_path.absolute()
  1145. args.dist_path = args.dist_path.absolute()
  1146. args.root = args.root.absolute()
  1147. args.dist_path = args.dist_path.absolute()
  1148. if args.dry:
  1149. args.dist_path = args.dist_path / "dry"
  1150. if args.github:
  1151. section_printer: SectionPrinter = GitHubSectionPrinter()
  1152. else:
  1153. section_printer = SectionPrinter()
  1154. if args.github and "GITHUB_OUTPUT" not in os.environ:
  1155. os.environ["GITHUB_OUTPUT"] = "/tmp/github_output.txt"
  1156. executer = Executer(root=args.root, dry=args.dry)
  1157. root_git_hash_path = args.root / GIT_HASH_FILENAME
  1158. root_is_maybe_archive = root_git_hash_path.is_file()
  1159. if root_is_maybe_archive:
  1160. logger.warning("%s detected: Building from archive", GIT_HASH_FILENAME)
  1161. archive_commit = root_git_hash_path.read_text().strip()
  1162. if args.commit != archive_commit:
  1163. logger.warning("Commit argument is %s, but archive commit is %s. Using %s.", args.commit, archive_commit, archive_commit)
  1164. args.commit = archive_commit
  1165. revision = (args.root / REVISION_TXT).read_text().strip()
  1166. else:
  1167. args.commit = executer.check_output(["git", "rev-parse", args.commit], dry_out="e5812a9fd2cda317b503325a702ba3c1c37861d9").strip()
  1168. revision = executer.check_output(["git", "describe", "--always", "--tags", "--long", args.commit], dry_out="preview-3.1.3-96-g9512f2144").strip()
  1169. logger.info("Using commit %s", args.commit)
  1170. try:
  1171. with args.path_release_info.open() as f:
  1172. release_info = json.load(f)
  1173. except FileNotFoundError:
  1174. logger.error(f"Could not find {args.path_release_info}")
  1175. releaser = Releaser(
  1176. release_info=release_info,
  1177. commit=args.commit,
  1178. revision=revision,
  1179. root=args.root,
  1180. dist_path=args.dist_path,
  1181. executer=executer,
  1182. section_printer=section_printer,
  1183. cmake_generator=args.cmake_generator,
  1184. deps_path=args.deps_path,
  1185. overwrite=args.overwrite,
  1186. github=args.github,
  1187. fast=args.fast,
  1188. )
  1189. if root_is_maybe_archive:
  1190. logger.warning("Building from archive. Skipping clean git tree check.")
  1191. else:
  1192. porcelain_status = executer.check_output(["git", "status", "--ignored", "--porcelain"], dry_out="\n").strip()
  1193. if porcelain_status:
  1194. print(porcelain_status)
  1195. logger.warning("The tree is dirty! Do not publish any generated artifacts!")
  1196. if not args.force:
  1197. raise Exception("The git repo contains modified and/or non-committed files. Run with --force to ignore.")
  1198. if args.fast:
  1199. logger.warning("Doing fast build! Do not publish generated artifacts!")
  1200. with section_printer.group("Arguments"):
  1201. print(f"project = {releaser.project}")
  1202. print(f"version = {releaser.version}")
  1203. print(f"revision = {revision}")
  1204. print(f"commit = {args.commit}")
  1205. print(f"out = {args.dist_path}")
  1206. print(f"actions = {args.actions}")
  1207. print(f"dry = {args.dry}")
  1208. print(f"force = {args.force}")
  1209. print(f"overwrite = {args.overwrite}")
  1210. print(f"cmake_generator = {args.cmake_generator}")
  1211. releaser.prepare()
  1212. if "download" in args.actions:
  1213. releaser.download_dependencies()
  1214. if set(args.actions).intersection({"msvc", "mingw", "android"}):
  1215. print("Verifying presence of dependencies (run 'download' action to download) ...")
  1216. releaser.verify_dependencies()
  1217. print("... done")
  1218. if "source" in args.actions:
  1219. if root_is_maybe_archive:
  1220. raise Exception("Cannot build source archive from source archive")
  1221. with section_printer.group("Create source archives"):
  1222. releaser.create_source_archives()
  1223. if "dmg" in args.actions:
  1224. if platform.system() != "Darwin" and not args.dry:
  1225. parser.error("framework artifact(s) can only be built on Darwin")
  1226. releaser.create_dmg()
  1227. if "msvc" in args.actions:
  1228. if platform.system() != "Windows" and not args.dry:
  1229. parser.error("msvc artifact(s) can only be built on Windows")
  1230. releaser.build_msvc()
  1231. if "mingw" in args.actions:
  1232. releaser.create_mingw_archives()
  1233. if "android" in args.actions:
  1234. if args.android_home is None or not Path(args.android_home).is_dir():
  1235. parser.error("Invalid $ANDROID_HOME or --android-home: must be a directory containing the Android SDK")
  1236. if args.android_ndk_home is None or not Path(args.android_ndk_home).is_dir():
  1237. parser.error("Invalid $ANDROID_NDK_HOME or --android_ndk_home: must be a directory containing the Android NDK")
  1238. if args.android_api is None:
  1239. with section_printer.group("Detect Android APIS"):
  1240. args.android_api = releaser._detect_android_api(android_home=args.android_home)
  1241. if args.android_api is None or not (Path(args.android_home) / f"platforms/android-{args.android_api}").is_dir():
  1242. parser.error("Invalid --android-api, and/or could not be detected")
  1243. with section_printer.group("Android arguments"):
  1244. print(f"android_home = {args.android_home}")
  1245. print(f"android_ndk_home = {args.android_ndk_home}")
  1246. print(f"android_api = {args.android_api}")
  1247. releaser.create_android_archives(
  1248. android_api=args.android_api,
  1249. android_home=args.android_home,
  1250. android_ndk_home=args.android_ndk_home,
  1251. )
  1252. with section_printer.group("Summary"):
  1253. print(f"artifacts = {releaser.artifacts}")
  1254. if args.github:
  1255. with open(os.environ["GITHUB_OUTPUT"], "a") as f:
  1256. f.write(f"project={releaser.project}\n")
  1257. f.write(f"version={releaser.version}\n")
  1258. for k, v in releaser.artifacts.items():
  1259. f.write(f"{k}={v.name}\n")
  1260. return 0
  1261. if __name__ == "__main__":
  1262. raise SystemExit(main())