diff --git a/tools/build.py b/tools/build.py index ed8f4dc..589d8ca 100755 --- a/tools/build.py +++ b/tools/build.py @@ -69,14 +69,45 @@ def use_docker(): return os.environ.get("JIELI_DOCKER", "0" if native else "1") == "1" +_BUILDER = None +_BUILDER_LOCK = __import__("threading").Lock() + + +def builder(): + """one long-lived toolchain container per source tree (starting a container per tool call cost + most of the build); it stops by itself after an hour without a build""" + global _BUILDER + with _BUILDER_LOCK: # the loader and the app build in parallel: start it once + return _builder_start() + + +def _builder_start(): + global _BUILDER + if _BUILDER: + return _BUILDER + name = "jieli-" + hashlib.sha1(str(SRC).encode()).hexdigest()[:10] + st = subprocess.run(["docker", "inspect", "-f", "{{.State.Running}}", name], capture_output=True, text=True) + if st.stdout.strip() != "true": + subprocess.run(["docker", "rm", "-f", name], capture_output=True) + r = subprocess.run(["docker", "run", "-d", "--rm", "--name", name, "--platform", "linux/amd64", + "-v", f"{SRC}:/work", "-v", f"{toolchain()}:/opt/jieli:ro", "-w", "/work", + DOCKER_IMAGE, "sh", "-c", "sleep 3600"], capture_output=True, text=True) + if r.returncode: + sys.stderr.write(r.stderr) + raise SystemExit("build: cannot start the toolchain container") + else: # an hour from now again + subprocess.run(["docker", "exec", "-d", name, "sh", "-c", "sleep 3600 && kill 1"], capture_output=True) + _BUILDER = name + return name + + def tc(tool, *args): """run a toolchain binary (pi32v2/bin/..., common/bin/...) with cwd SRC; paths relative to SRC""" rel = [str(Path(a).resolve().relative_to(SRC)) if isinstance(a, Path) else a for a in args] if tool == "cc": # the toolchain's cc wrapper needs python3; call clang directly tool, rel = "pi32v2/bin/clang", ["-target", "pi32v2", *rel] if use_docker(): - cmd = ["docker", "run", "--rm", "--platform", "linux/amd64", "-v", f"{SRC}:/work", - "-v", f"{toolchain()}:/opt/jieli:ro", "-w", "/work", DOCKER_IMAGE, f"/opt/jieli/{tool}", *rel] + cmd = ["docker", "exec", "-w", "/work", builder(), f"/opt/jieli/{tool}", *rel] else: cmd = [str(toolchain() / tool), *rel] r = subprocess.run(cmd, cwd=SRC, capture_output=True, text=True) diff --git a/tools/publish_pages.sh b/tools/publish_pages.sh index 1e5234e..7e2f188 100755 --- a/tools/publish_pages.sh +++ b/tools/publish_pages.sh @@ -19,4 +19,7 @@ git add -A git commit -q -m "Site for X0X $V (from $SHA)" git push -q -f "$REMOTE" gh-pages rm -rf .git +# ask GitHub Pages to build it now: a push alone has left the first deploy stuck on "building" +REPO="$(printf %s "$REMOTE" | sed -E 's#(git@github.com:|https://github.com/)##; s#\.git$##')" +gh api -X POST "repos/$REPO/pages/builds" >/dev/null 2>&1 || echo "(could not ask GitHub Pages to build: check the repository's Pages settings)" echo "pushed the site for $V to gh-pages"