pthbs

Packaging Through Hashed Build Scripts
git clone https://ccx.te2000.cz/git/pthbs
Log | Files | Refs | Submodules | README

commit cca30e14c31a024fc786ee063e8fd93586b5254e
parent 11ea5b59e538a193c79c62bc6a36faa1ab6ed297
Author: ccx <ccx@te2000.cz>
Date:   Thu, 17 Oct 2024 03:38:28 +0000

Use and export inherited variables

Diffstat:
Mcommand/pthbs-build | 67+++++++++++++++++++++++++++++++++++++------------------------------
Mcommand/pthbs-download | 10+++++++---
Mcommand/pthbs-makegen | 34++++++++++++++++++----------------
Mcommand/pthbs-makegen-downloads | 4++--
Mcommand/pthbs-namedenv | 7+++++--
Mpthbs.mk | 31++++++++++++++++++++-----------
Rgen-downloadlist-entry -> util/gen-downloadlist-entry | 0
Rgen-downloadlist-entry-from-sha512 -> util/gen-downloadlist-entry-from-sha512 | 0
8 files changed, 89 insertions(+), 64 deletions(-)

diff --git a/command/pthbs-build b/command/pthbs-build @@ -1,29 +1,35 @@ #!/bin/sh bsh=$(sha256sum $1) || exit $? bsh=${bsh%% *} -basedir=$(dirname "$(dirname "$(realpath "$0")")") -if ! test -d "$basedir"; then - printf '%s\n' "Error: could not determine basedir" - exit 1 +if test -z "$pthbs_workdir"; then + printf '%s\n' >&2 "$0: fatal: pthbs_workdir env var undefined or empty" + exit 100 +fi +if test -z "$pthbs_versions"; then + printf '%s\n' >&2 "$0: fatal: pthbs_versions env var undefined or empty" + exit 100 fi -mkdir -p "$basedir/work/builddir.$$" || exit $? -workdir=$(realpath "$basedir/work/builddir.$$") +if test -z "$pthbs_pkgdir"; then + printf '%s\n' >&2 "$0: fatal: pthbs_pkgdir env var undefined or empty" + exit 100 +fi +if test -z "$pthbs_cache"; then + printf '%s\n' >&2 "$0: fatal: pthbs_cache env var undefined or empty" + exit 100 +fi + +mkdir -p "$pthbs_workdir/builddir.$$" || exit $? +workdir=$(realpath "$pthbs_workdir/builddir.$$") if ! test -d "$workdir"; then printf '%s\n' "Error: could not determine workdir" exit 1 fi + script=$(realpath "$1") if ! test -f "$script"; then printf '%s\n' "Error: could not determine script path" exit 1 fi -if test -z "$pthbs_versions"; then - case $(id -u) in - (0) pthbs_versions=/versions ;; - (*) pthbs_versions=$HOME/versions ;; - esac - export pthbs_versions -fi pthbs_package=${1##*/} pthbs_package=${pthbs_package%%:*}.$bsh @@ -52,8 +58,8 @@ END { } ' "$1" )" -if test -f "make/package.sha256.${bsh}.env"; then - envfile="make/package.sha256.${bsh}.env" +if test -f "$pthbs_cache/make/package.sha256.${bsh}.env"; then + envfile="$pthbs_cache/make/package.sha256.${bsh}.env" envhash=$(pthbs-getenvhash "$envfile") || exit $? export pthbs_build_environment=/versions/env.$envhash if $setting_set_path; then @@ -79,7 +85,6 @@ case $(id -u) in esac env \ - basedir="$basedir" \ workdir="$workdir" \ script="$script" \ envdir="$pthbs_build_environment" \ @@ -119,14 +124,18 @@ function fatal(msg) { exit 1 } +function qlink(rel) { + return q(ENVIRON["pthbs_cache"] "/link/" rel) +} + function at_git(commit_id, dstdir){ print "mkdir -p "q(dstdir) - print "(cd "q(ENVIRON["basedir"]"/sources/by-commit/"commit_id)" && git archive --format=tar "q(commit_id)" ) | tar --no-same-owner --no-same-permissions -xC "q(dstdir) + print "(cd "qlink("git-commit-sha1/"commit_id)" && git archive --format=tar "q(commit_id)" ) | tar --no-same-owner --no-same-permissions -xC "q(dstdir) } function at_untar(extra_opts, hash_type, file_hash, dstdir){ print "mkdir -p "q(dstdir) - print "tar -x "extra_opts" -C "q(dstdir)" -f "q(ENVIRON["basedir"]"/files/by-"hash_type"/"file_hash) + print "tar -x "extra_opts" -C "q(dstdir)" -f "qlink("files-"hash_type"/"file_hash) } function at_filehash(hash_type, file_hash, dst, dstdir){ @@ -135,7 +144,7 @@ function at_filehash(hash_type, file_hash, dst, dstdir){ sub("/[^/]*$", "", dstdir) print "mkdir -p "q(dstdir) } - print "cp -Lp "q(ENVIRON["basedir"]"/files/by-"hash_type"/"file_hash)" "q(dst) + print "cp -Lp "qlink("file-"hash_type"/"file_hash)" "q(dst) } /^#!/ { next } @@ -194,28 +203,26 @@ function at_filehash(hash_type, file_hash, dst, dstdir){ sandbox_cmd=sandbox_cmd " -m " q("allow/read+"dirname(ENVIRON["envdir"])"/***") sandbox_cmd=sandbox_cmd " -munshare/user:1" sandbox_cmd=sandbox_cmd " -munshare/mount:1" - sandbox_cmd=sandbox_cmd " -mbind+" q(ENVIRON["basedir"]"/work/bin:/bin:ro,nosuid,nodev") + sandbox_cmd=sandbox_cmd " -mbind+" q(ENVIRON["pthbs_workdir"]"/bin:/bin:ro,nosuid,nodev") sandbox_cmd=sandbox_cmd " -m " q("allow/read+/bin/***") sandbox_cmd=sandbox_cmd " -munshare/net:1 -munshare/ipc:1" } else if(sandbox_mode == "userns") { sandbox_cmd=" "q(ENVIRON["basedir"]"/ns_sandbox.py")" --mode=userns" - sandbox_cmd=sandbox_cmd" --vars="q(ENVIRON["basedir"]"/vars.yaml") - sandbox_cmd=sandbox_cmd" --extra-mount=tmpfs:"q(ENVIRON["basedir"]"/work") - sandbox_cmd=sandbox_cmd" --extra-mount=ro_bind:"q(ENVIRON["basedir"]"/packages:"ENVIRON["basedir"]"/packages") + sandbox_cmd=sandbox_cmd" --extra-mount=tmpfs:"q(ENVIRON["pthbs_workdir"]) + sandbox_cmd=sandbox_cmd" --extra-mount=ro_bind:"q(ENVIRON["pthbs_pkgdir"]":"ENVIRON["pthbs_pkgdir"]) sandbox_cmd=sandbox_cmd" --extra-mount=rw_bind:"q(ENVIRON["workdir"]":"ENVIRON["workdir"]) sandbox_cmd=sandbox_cmd" --extra-mount=rw_bind:"q(ENVIRON["workdir"]"/.tmp:/tmp") - sandbox_cmd=sandbox_cmd" -- "q(ENVIRON["basedir"]"/work/root") + sandbox_cmd=sandbox_cmd" -- "q(ENVIRON["pthbs_workdir"]"/root") printf "%s\n", "mkdir -p "q(ENVIRON["workdir"]"/.tmp") } else if(sandbox_mode == "root") { - sandbox_cmd=" "q(ENVIRON["basedir"]"/work/venv/bin/python")" "q(ENVIRON["basedir"]"/ns_sandbox.py")" --mode=root" + sandbox_cmd=" "q(ENVIRON["pthbs_workdir"]"/venv/bin/python")" "q(ENVIRON["basedir"]"/ns_sandbox.py")" --mode=root" sandbox_cmd=sandbox_cmd" --untar="q(ENVIRON["basedir"]"/root.tar") - sandbox_cmd=sandbox_cmd" --vars="q(ENVIRON["basedir"]"/vars.yaml") sandbox_cmd=sandbox_cmd" --chdir="q(ENVIRON["workdir"]) - sandbox_cmd=sandbox_cmd" --extra-mount=tmpfs:"q(ENVIRON["basedir"]"/work") - sandbox_cmd=sandbox_cmd" --extra-mount=ro_bind:"q(ENVIRON["basedir"]"/packages:"ENVIRON["basedir"]"/packages") + sandbox_cmd=sandbox_cmd" --extra-mount=tmpfs:"q(ENVIRON["pthbs_workdir"]) + sandbox_cmd=sandbox_cmd" --extra-mount=ro_bind:"q(ENVIRON["pthbs_pkgdir"]":"ENVIRON["pthbs_pkgdir"]) sandbox_cmd=sandbox_cmd" --extra-mount=rw_bind:"q(ENVIRON["workdir"]":"ENVIRON["workdir"]) sandbox_cmd=sandbox_cmd" --extra-mount=rw_bind:"q(ENVIRON["workdir"]"/.tmp:/tmp") - sandbox_cmd=sandbox_cmd" -- "q(ENVIRON["basedir"]"/work/root") + sandbox_cmd=sandbox_cmd" -- "q(ENVIRON["pthbs_workdir"]"/root") printf "%s\n", "mkdir -p "q(ENVIRON["workdir"]"/.tmp") } else { fatal("unrecognized sanbox_mode " sandbox_mode) @@ -256,7 +263,7 @@ if test -z "$JOBS"; then fi fi -logdir=work/logs/$(date '+%Y-%m-%d-%H%M%S')-$pthbs_package +logdir=$pthbs_workdir/logs/$(date '+%Y-%m-%d-%H%M%S')-$pthbs_package ret=0 if test -n "$pthbs_xtrace"; then pthbs_xtrace=-x; set -x; fi trap 'trap - INT' INT diff --git a/command/pthbs-download b/command/pthbs-download @@ -4,7 +4,11 @@ checksum_format=$1 checksum=$2 size=$3 shift 3 || exit -filename=downloads/$checksum_format/$checksum +if test -z "$pthbs_cache"; then + printf '%s\n' >&2 "$0: fatal: pthbs_cache env var undefined or empty" + exit 100 +fi +filename=$pthbs_cache/downloads/$checksum_format/$checksum case $checksum_format in (md5);; @@ -27,7 +31,7 @@ check() { (*) return 1;; esac if printf '%s %s\n' "$checksum" "$filename" | ${checksum_format}sum -c -; then - ln -sf "../../$filename" files/by-sha256 || exit 1 + ln -sf "../../downloads/$checksum_format/$checksum" "$pthbs_cache/link/file-$checksum_format/" || exit 1 return 0 fi rm -v "$filename" @@ -36,7 +40,7 @@ check() { test -z "$pthbs_xtrace" || set -x -mkdir -p "downloads/$checksum_format" || exit $? +mkdir -p "$pthbs_cache/downloads/$checksum_format" || exit $? if check; then exit 0 diff --git a/command/pthbs-makegen b/command/pthbs-makegen @@ -2,15 +2,17 @@ #set -x bsh=$(sha256sum $1) || exit $? bsh=${bsh%% *} -basedir=$(dirname "$(dirname "$(realpath "$0")")") -workdir=$basedir/work/builddir.$$ +if test -z "$pthbs_workdir"; then + printf '%s\n' >&2 "$0: fatal: pthbs_workdir env var undefined or empty" + exit 100 +fi +workdir=$pthbs_workdir/builddir.$$ script=$(realpath "$1") pthbs_package=${1##*/} pthbs_package=${pthbs_package%%:*}.$bsh exec env \ - basedir="$basedir" \ workdir="$workdir" \ script="$script" \ scriptname="${script##*/}" \ @@ -48,7 +50,7 @@ function have_file(hash_type, file_hash) { if(!((hash_type, file_hash) in downloadable_hashes)) { return } - deps[++dep_count] = "make/file."hash_type"."file_hash".downloaded" + deps[++dep_count] = "$(cache)/make/file."hash_type"."file_hash".downloaded" } function have_envdep(package) { @@ -57,14 +59,14 @@ function have_envdep(package) { } env[++env_count] = package package_hash = substr(package, 64, RSTART+1) - envdep[env_count] = "$(VERSIONS)/"package"/.install-links" + envdep[env_count] = "$(versions)/"package"/.install-links" } function make_envfile( n, envfile, envhash) { if(!env_count) { fatal("environment is empty") } - envfile = "make/package.sha256."ENVIRON["bsh"]".env" + envfile = "$(cache)/make/package.sha256."ENVIRON["bsh"]".env" for(n=1; n<=env_count; n++) { print env[n] >envfile } @@ -76,19 +78,19 @@ function make_envfile( n, envfile, envhash) { if(match(envhash, "^[0-9a-f]{64}$") == 0) { fatal("received invalid envhash: "q(envhash)) } - deps[++dep_count] = "$(VERSIONS)/env."envhash"/.pthbs-env" + deps[++dep_count] = "$(versions)/env."envhash"/.pthbs-env" printf "ifndef %s\n", "env_"envhash"_defined" - printf "%s:", "$(VERSIONS)/env."envhash"/.pthbs-env" + printf "%s:", "$(versions)/env."envhash"/.pthbs-env" for(n=1; n<=env_count; n++) { printf " %s", envdep[n] } - printf "\n\t@%s", "printf "q("Creating environment for %s => %s\\n")" "q(FILENAME)" "q("$(VERSIONS)/env."envhash) - printf "\n\t%s", "if test -e "q("$(VERSIONS)/env."envhash)"; then rm -r "q("$(VERSIONS)/env."envhash)"; fi" - printf "\n\t%s", "mkdir -p "q("$(VERSIONS)/env."envhash"/env") + printf "\n\t@%s", "printf "q("Creating environment for %s => %s\\n")" "q(FILENAME)" "q("$(versions)/env."envhash) + printf "\n\t%s", "if test -e "q("$(versions)/env."envhash)"; then rm -r "q("$(versions)/env."envhash)"; fi" + printf "\n\t%s", "mkdir -p "q("$(versions)/env."envhash"/env") for(n=1; n<=env_count; n++) { - printf "\n\t%s", "pthbs-link "q("$(VERSIONS)/"env[n])" "q("$(VERSIONS)/env."envhash) + printf "\n\t%s", "pthbs-link "q("$(versions)/"env[n])" "q("$(versions)/env."envhash) } - printf "\n\t%s", "pthbs-enter-gen "q("$(VERSIONS)/env."envhash) + printf "\n\t%s", "pthbs-enter-gen "q("$(versions)/env."envhash) printf "\n\t%s\n\n", "sort -u <"q(envfile)" >"q("$@") printf "%s\n", "env_"envhash"_defined="ENVIRON["scriptname"] printf "%s\n", "endif" @@ -134,7 +136,7 @@ function make_envfile( n, envfile, envhash) { if(env_count) { make_envfile() } - printf "%s", "$(VERSIONS)/"ENVIRON["package"]"/.install-links:" + printf "%s", "$(versions)/"ENVIRON["package"]"/.install-links:" for(n=1; n<=dep_count; n++) { printf " %s", deps[n] } @@ -150,8 +152,8 @@ END{ if(has_body) { exit 0 } fatal("no build script present") } - env_installdir = "$(VERSIONS)/env." make_envfile() - printf "%s: %s %s\n", ENVIRON["scriptname"], env_installdir"/.pthbs-env", "namedenv/.exists" + env_installdir = "$(versions)/env." make_envfile() + printf "%s: %s %s\n", ENVIRON["scriptname"], env_installdir"/.pthbs-env", "$(cache)/namedenv/.exists" printf "\t%s\n\n", "pthbs-namedenv "q(env_installdir)" "q(envname) printf ".PHONY: %s\n", ENVIRON["scriptname"] } diff --git a/command/pthbs-makegen-downloads b/command/pthbs-makegen-downloads @@ -16,8 +16,8 @@ function fatal(msg) { /^#/ { next } /^[0-9a-f]{64} [0-9]+ /{ - printf "%s:\n", "make/file."format"."$1".downloaded" - printf "\tpthbs-download sha256" + printf "%s:\n", "$(cache)/make/file."format"."$1".downloaded" + printf "\tpthbs-download "format for(n=1; n<=NF; n++){ printf " %s", q($n) } diff --git a/command/pthbs-namedenv b/command/pthbs-namedenv @@ -1,7 +1,10 @@ #!/bin/sh set -e if test -n "$pthbs_xtrace"; then set -x; fi -basedir=$(dirname "$(dirname "$(realpath "$0")")") +if test -z "$pthbs_cache"; then + printf '%s\n' >&2 "$0: fatal: pthbs_cache env var undefined or empty" + exit 100 +fi if test $# != 2; then printf '%s\n' >&2 'usage: pthbs-namedenv /path/to/env.hash name' exit 2 @@ -24,7 +27,7 @@ case ${env_installdir##*/} in ;; esac -bd_link="$basedir/namedenv/$env_name" +bd_link="$pthbs_cache/namedenv/$env_name" v_link_base="${env_installdir%/*}/namedenv.$env_name" printf "%s => %s\n" "$v_link_base" "${env_installdir##*/}" if test -L "$bd_link"; then diff --git a/pthbs.mk b/pthbs.mk @@ -1,12 +1,21 @@ # default to homedir if not root -VERSIONS:=$(patsubst /root/%,/%,$(HOME)/versions) - -export PATH:=$(CURDIR)/command:$(PATH) -export pthbs_versions:=$(VERSIONS) - -default: default.environment userspace.environment containers.environment mdevd.environment - -$(VERSIONS)/environment.%/.env: +versions?=$(patsubst /root/%,/%,$(HOME)/versions) +pthbs?=. +packages?=packages +work?=work +cache?=cache +index?=index + +export PATH:=$(abspath $(pthbs))/command:$(PATH) +export pthbs_versions:=$(versions) +export pthbs_pkgdir:=$(abspath $(packages)) +export pthbs_workdir:=$(abspath $(work)) +export pthbs_cache:=$(abspath $(cache)) + +no_default: + @echo "pthbs has no default target to build" + +$(versions)/environment.%/.env: @echo mkdir "$$(dirname '$@')" @echo touch '$@' @@ -15,11 +24,11 @@ $(VERSIONS)/environment.%/.env: mkdir -p '$*' touch '$@' -make/package.%.mk: packages/% make/.exists command/pthbs-makegen - pthbs-makegen 'packages/$*' >'$@.new' +$(cache)/make/package.%.mk: $(packages)/% $(cache)/make/.exists $(pthbs)/command/pthbs-makegen + pthbs-makegen '$(packages)/$*' >'$@.new' mv '$@.new' '$@' -make/downloads.%.mk: downloadlist.% make/.exists command/pthbs-makegen-downloads +$(cache)/make/downloads.%.mk: downloadlist.% $(cache)/make/.exists $(pthbs)/command/pthbs-makegen-downloads pthbs-makegen-downloads '$*' 'downloadlist.$*' >'$@.new' mv '$@.new' '$@' diff --git a/gen-downloadlist-entry b/util/gen-downloadlist-entry diff --git a/gen-downloadlist-entry-from-sha512 b/util/gen-downloadlist-entry-from-sha512