commit 4f9188ad4ddc7e626f2b607237ee250284f28475
parent c6abeb3ac27bdaa4ca17ab49f422a9318860c754
Author: Jan Pobříslo <ccx@te2000.cz>
Date: Wed, 7 Jun 2023 13:39:17 +0200
Prototype package generator in Python (to be rewritten later)
Diffstat:
5 files changed, 126 insertions(+), 0 deletions(-)
diff --git a/genpkg.py b/genpkg.py
@@ -0,0 +1,62 @@
+#!/usr/bin/env python3
+import os.path
+import hashlib
+from pathlib import Path
+
+import jinja2
+import yaml
+
+
+class Main:
+ def __init__(self, out_dir="newpkg", template_dir="templates"):
+ self.out_dir = Path(out_dir)
+ self.template_dir = Path(template_dir)
+ self.env = jinja2.Environment(
+ loader = jinja2.FileSystemLoader(template_dir),
+ autoescape = False,
+ )
+ self.env.globals["pkg_sha256"] = self.pkg_sha256
+ self.rendered = {}
+ self.rendering = []
+
+ def load_vars_yaml(self, fname="vars.yaml"):
+ with open(fname) as f:
+ self.env.globals.update(yaml.safe_load(f))
+
+ def pkg_sha256(self, name):
+ return hashlib.sha256(self.render_pkg(name)).hexdigest()
+
+ def render_pkg(self, name):
+ if name in self.rendered:
+ return self.rendered[name]
+ if name in self.rendering:
+ raise RuntimeError("circular dependency: %r", self.rendering)
+ t = self.env.get_template("pkg/" + name)
+ self.rendering.append(name)
+ self.rendered[name] = bytes(t.render().encode('utf8'))
+ lastname = self.rendering.pop()
+ assert name == lastname
+ return self.rendered[name]
+
+ def list_packages(self):
+ for tplname in self.env.list_templates():
+ if not tplname.startswith("pkg/"):
+ continue
+ if "/." in tplname:
+ continue
+ yield tplname[4:]
+
+ def render_all(self):
+ for pkgname in self.list_packages()
+ print("%s:\t%s" % (pkgname, self.pkg_sha256(pkgname)))
+
+
+if __name__ == '__main__':
+ from pprint import pprint as pp
+ m = Main()
+ m.load_vars_yaml()
+ pp(m.env.list_templates(filter_func = lambda name: "/." not in name))
+ m.render_all()
+ os.write(1, b"\n\n---\n")
+ os.write(1, m.rendered["musl-cross-make:bootstrap"])
+ os.write(1, b"---\n")
diff --git a/templates/base b/templates/base
@@ -0,0 +1,3 @@
+{{ shebang }}
+{%- block body %}
+{% endblock %}
diff --git a/templates/genlinks b/templates/genlinks
@@ -0,0 +1,43 @@
+{% extends "base" %}
+{%- block body -%}
+{%- block script %}
+{% endblock %}
+cd "$pthbs_destdir/{{versions}}/$pthbs_package"
+find -type d -o -print | awk -F/ '
+BEGIN {
+{% block genlinks_begin %}
+ x["./bin/{{triplet}}-g++"]=1
+ x["./bin/{{triplet}}-gcc"]=1
+{% endblock %}
+}
+
+function r1(s) {
+ sub("^[.]/[^/]*", ".", s)
+ return s
+}
+function s1(repl, s) {
+ sub("^[.]/[^/]*", "./"repl, s)
+ return s
+}
+function link(src) {
+ x[$0]=0
+ printf "%s\t%s\n", $0, src
+}
+$1!="."{exit 1}
+
+$2 == "share" && $3 ~ /^(info|man)$/ { link(r1($0)); next }
+$2 == "bin" { link(s1("command", $0)); next }
+$2 == "include" { link($0); next }
+$2 == "lib" { link($0); next }
+
+END {
+ for(fname in x) { printf "DEBUG: x[\"%s\"]=\"%s\"\n", fname, x[fname] >"/dev/stderr" }
+ for(fname in x) {
+ if(x[fname]) {
+ printf "ERROR: missing expected file \"%s\"\n", fname >"/dev/stderr"
+ exit 3
+ }
+ }
+}' >.install-links.new
+mv .install-links.new .install-links
+{% endblock %}
diff --git a/templates/pkg/musl-cross-make:bootstrap b/templates/pkg/musl-cross-make:bootstrap
@@ -0,0 +1,15 @@
+{% extends "genlinks" %}
+{%- block script %}
+#+*
+#@git:fe915821b652a7fa37b34a596f47d8e20bc72338:musl-cross-make
+
+: ${JOBS:=1}
+export TARGET={{triplet}}
+printf '%s\n' >musl-cross-make/config.mak "OUTPUT=${pthbs_destdir%/}{{versions}}/$pthbs_package"
+make -C musl-cross-make -j$JOBS -l$(1+$JOBS) || exit $?
+make -C musl-cross-make install || exit $?
+{% endblock %}
+{% block genlinks_begin %}
+ x["./bin/{{triplet}}-g++"]=1
+ x["./bin/{{triplet}}-gcc"]=1
+{% endblock %}
diff --git a/vars.yaml b/vars.yaml
@@ -0,0 +1,3 @@
+shebang: "#!/usr/bin/env pthbs-build"
+triplet: x86_64-linux-musl
+versions: /home/ccx/versions