nandi/frqpublic Fork 0
43cc3f0376f08659dea357cd994b3b465d00d9aa
Commits
Clone
git clone https://git.rickub.com/nandi/frq.git
git clone ssh://git@rickub.com/nandi/frq.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

BUCK · 214 lines · 10.7 KBPython Blame HistoryRaw
Build the APK with buck2, from pins rather than from a second checkout a71ef8f nandi 18d ago1# The APK, as a graph rather than a script.
2#
Write the build in babashka, and pin the babashka 34832a8 nandi 18d ago3# Every step the old build-apk script ran in sequence is a target here, so a change
Build the APK with buck2, from pins rather than from a second checkout a71ef8f nandi 18d ago4# to one screen rebuilds the boot image and repackages, and touches neither the
5# Rust nor the Java. The two native halves come from jolt-native — libvidya out
6# of its buck2 graph, the glue compiled by the NDK its scripts/ pin — and the
7# rest is this repo's.
8#
9# The machine-specific paths are read from .buckconfig.local, which the `buck`
10# recipe in the justfile writes. Nothing here is found by looking around the
11# machine; if a path is missing the recipe says which.
Make the worktree its own buck root, and the glue an input ae207b8 nandi 18d ago12load(":defs.bzl", "glue", "libvidya")
Build the APK with buck2, from pins rather than from a second checkout a71ef8f nandi 18d ago13
14_JOLT_NATIVE = read_root_config("frq", "jolt_native", "")
15_ANDROID_HOME = read_root_config("frq", "android_home", "")
16_CHEZ = read_root_config("frq", "chez_android", "")
17_OPENSSL = read_root_config("frq", "openssl_android", "")
18
19_API = "28"
20_TOOLS = _ANDROID_HOME + "/build-tools/36.0.0"
21_ANDROID_JAR = _ANDROID_HOME + "/platforms/android-36/android.jar"
22# `$(...)` in a genrule cmd is a buck macro, not the shell's substitution, so
23# everything this file runs at command time uses backticks.
24# This repo's own, not the other checkout's: an APK build needs a clang and an
25# llvm-objcopy whether or not jolt-native is on the machine. Both resolve the
26# NDK through scripts/android-ndk.dotslash, downloading it once.
27_NDK_BIN = "ndk=`$(location //scripts:android-ndk-bin)`"
28_CC = "$(location //scripts:android-cc)"
29
30# An absolute path for $OUT, so a command may cd without losing it. buck has
31# already made the parent directory. Spelled out step by step because backticks
32# do not nest.
33# Empty when a checkout answers for it, so the script's own lookup wins.
34# The archive's top directory is what strip_prefix took off, so `src` is
35# directly inside it. Absolute, because the script is handed this and does its
36# own directory changing.
37_GLIMMER_VIDYA = "" if read_root_config("frq", "libvidya", "pinned") == "checkout" else \
38 "GLIMMER_VIDYA=`realpath $(location toolchains//dist:glimmer-vidya)/src` "
39
Make the worktree its own buck root, and the glue an input ae207b8 nandi 18d ago40# jolt_main.c and the ABI's headers, as targets — see defs.bzl for which of
41# the two sources answers, and why neither may be a bare path.
42_GLUE_C = "`realpath $(location :glue-c)`"
43_GLUE_INCLUDE = "`realpath $(location :glue-include)`"
Build the APK with buck2, from pins rather than from a second checkout a71ef8f nandi 18d ago44
45_ABS_OUT = 'd=`dirname "$OUT"` && b=`basename "$OUT"` && d=`cd "$d" && pwd` && out="$d/$b"'
46
47# Somewhere to assemble in. Removed by the command that made it, on success;
48# buck's own scratch space is not this.
49_TMP = 'tmp=`mktemp -d`'
50
Write the build in babashka, and pin the babashka 34832a8 nandi 18d ago51# A babashka script, run in place: it loads scripts/frq/paths.clj beside it and
52# resolves its own interpreter through scripts/bb, the DotSlash pin. Referenced
53# rather than copied for exactly that reason — the tree around it is part of it.
Build the APK with buck2, from pins rather than from a second checkout a71ef8f nandi 18d ago54export_file(
Write the build in babashka, and pin the babashka 34832a8 nandi 18d ago55 name = "build-jolt-boot.bb",
Build the APK with buck2, from pins rather than from a second checkout a71ef8f nandi 18d ago56 mode = "reference",
57)
58
59# --- the UI half ------------------------------------------------------------
60# Two ways in, and both make the library's bytes an input rather than a
61# command buck would cache forever:
62#
63# pinned the .so out of jolt-native's release, fetched by digest. What a
64# build gets by default, and what makes an APK buildable with no
65# jolt-native checkout and no NDK anywhere on the machine.
66# checkout the .so a sibling jolt-native just built, staged into this cell
67# by the `buck` recipe. Anyone working on both repos at once
68# builds what they are editing.
69#
70# The recipe decides which, by whether that checkout exists, and says so here.
71libvidya(name = "libvidya")
72
Make the worktree its own buck root, and the glue an input ae207b8 nandi 18d ago73glue(c_name = "glue-c", include_name = "glue-include")
74
Build the APK with buck2, from pins rather than from a second checkout a71ef8f nandi 18d ago75# --- the Jolt half ----------------------------------------------------------
76# The boot image: frq's Scheme, cross-compiled to arm64 by Chez. The sources
77# are an input so that editing one rebuilds this; the compile itself reads them
78# through the deps.edn the script writes.
79# The sources here are frq's own. The other two roots the image is compiled
80# from — glimmer, out of the jolt cache, and glimmer-vidya, out of jolt-native
81# — are outside this cell, and so are Chez's cross boot files and the pinned
82# jolt itself. None of them can be an input, so the `buck` recipe hashes them
83# and writes the digest below; naming it in the command is what puts them in
84# this action's identity.
85# jolt reaches this action as a manifest and a fetcher rather than as a
86# program: both are inputs, so nothing here depends on what is installed where
87# the compile runs, and a remote worker resolves the same pin — by the same
88# digest — from the same place this machine would.
89genrule(
90 name = "jolt-boot",
91 out = "boot",
92 srcs = ["//:jolt-sources"],
93 cmd = "# sources outside this cell: " + read_root_config("frq", "boot_stamp", "unknown") + "\n" +
94 "DOTSLASH=$(location toolchains//dist:dotslash)/dotslash " +
95 "JOLT_MANIFEST=$(location //scripts:jolt) " +
96 # The one source root the script cannot find for itself when there
97 # is no jolt-native checkout. An input, so a new release recompiles
98 # the image.
99 _GLIMMER_VIDYA +
Write the build in babashka, and pin the babashka 34832a8 nandi 18d ago100 "$(location :build-jolt-boot.bb) \"$OUT\" >&2",
Build the APK with buck2, from pins rather than from a second checkout a71ef8f nandi 18d ago101)
102
103# The image travels as a blob in an object file's data section; the
104# _binary_jolt_boot_{start,end} symbols jolt_main.c reads are named after the
105# input *path*, which is why this copies the file somewhere it can be called
106# exactly `jolt.boot` before converting it.
107genrule(
108 name = "jolt-boot-obj",
109 out = "jolt_boot.o",
110 cmd = _ABS_OUT + " && " +
111 _TMP + " && " + _NDK_BIN + " && " +
112 "cp $(location :jolt-boot)/jolt.boot \"$tmp/jolt.boot\" && " +
113 "( cd \"$tmp\" && \"$ndk\"/llvm-objcopy " +
114 "--input-target=binary --output-target=elf64-littleaarch64 " +
115 "--binary-architecture=aarch64 jolt.boot jolt_boot.o ) && " +
116 "cp \"$tmp/jolt_boot.o\" \"$out\" && rm -rf \"$tmp\"",
117)
118
119# The glue: jolt-native's android/jolt_main.c over the boot image, linked
120# against libvidya by name. --no-undefined is what makes a symbol the Scheme
121# side registers but the ABI no longer exports a build failure here rather than
122# a crash on the phone.
123genrule(
124 name = "libjoltapp",
125 out = "libjoltapp.so",
126 cmd = _ABS_OUT + " && lib=`mktemp -d` && " +
127 "cp $(location :libvidya) \"$lib/libvidya.so\" && " +
128 _CC + " -shared -fPIC -O2 -o \"$out\" " +
129 _GLUE_C + " " +
130 "$(location :jolt-boot-obj) " +
131 "-I$(location :jolt-boot) " +
132 "-I" + _GLUE_INCLUDE + " " +
133 "-L\"$lib\" " +
134 _CHEZ + "/tarm64le/boot/tarm64le/libkernel.a " +
135 _CHEZ + "/lz4/lib/liblz4.a " +
136 "-lvidya -landroid -llog -lz -ldl -lm -Wl,--no-undefined && " +
137 "rm -rf \"$lib\"",
138)
139
140# --- the Java half ----------------------------------------------------------
141# One class: the photo chooser's result has to land somewhere, and native code
142# is not somewhere. android.jar on the class path is where every android.* type
143# comes from; the JDK's own java.* is what is left.
144genrule(
145 name = "classes-dex",
146 out = "classes.dex",
147 srcs = glob(["java/**/*.java"]),
148 cmd = _ABS_OUT + " && " + _TMP + " && " +
149 "javac --release 17 --class-path " + _ANDROID_JAR + " -d \"$tmp/classes\" $SRCS && " +
150 "classes=`find \"$tmp/classes\" -name '*.class'` && " +
151 _TOOLS + "/d8 --min-api " + _API + " --output \"$tmp\" $classes && " +
152 "cp \"$tmp/classes.dex\" \"$out\" && rm -rf \"$tmp\"",
153)
154
155# --- the package ------------------------------------------------------------
156# The libraries are stored rather than deflated: the loader maps them straight
157# out of the APK. The dex is read rather than mapped, so it may as well
158# compress. OpenSSL travels with the app because the platform's own is not ours
159# to load — an app's linker namespace refuses /system/lib64/libssl.so, and
160# without one there is no TLS on the phone at all.
161genrule(
162 name = "apk-unsigned",
163 out = "frq-unsigned.apk",
164 srcs = ["AndroidManifest.xml"],
165 cmd = _ABS_OUT + " && stage=`mktemp -d` && " +
166 "mkdir -p \"$stage/lib/arm64-v8a\" && " +
167 "cp $(location :libvidya) \"$stage/lib/arm64-v8a/libvidya.so\" && " +
168 "cp $(location :libjoltapp) \"$stage/lib/arm64-v8a/libjoltapp.so\" && " +
169 "cp " + _OPENSSL + "/libssl.so " + _OPENSSL + "/libcrypto.so \"$stage/lib/arm64-v8a/\" && " +
170 "cp $(location :classes-dex) \"$stage/classes.dex\" && " +
171 _TOOLS + "/aapt2 link -o \"$out\" -I " + _ANDROID_JAR + " " +
172 "--manifest $SRCS --min-sdk-version " + _API + " --target-sdk-version 36 " +
173 "--version-code 1 --version-name 0.1.0 >&2 && " +
174 "( cd \"$stage\" && zip -q -0 \"$out\" lib/arm64-v8a/libvidya.so " +
175 "lib/arm64-v8a/libjoltapp.so lib/arm64-v8a/libssl.so lib/arm64-v8a/libcrypto.so && " +
176 "zip -q \"$out\" classes.dex ) && rm -rf \"$stage\"",
177)
178
179# Aligned and signed with the debug key, which is generated on first use the
180# way the SDK's own tools generate it.
181genrule(
182 name = "apk",
183 out = "frq.apk",
184 cmd = _ABS_OUT + " && " + _TMP + " && " +
185 "ks=\"$HOME/.android/debug.keystore\" && " +
186 "if [ ! -f \"$ks\" ]; then mkdir -p \"$HOME/.android\" && " +
187 "keytool -genkeypair -v -keystore \"$ks\" -storepass android -keypass android " +
188 "-alias androiddebugkey -keyalg RSA -keysize 2048 -validity 10000 " +
189 "-dname 'CN=Android Debug,O=Android,C=US' >&2; fi && " +
190 _TOOLS + "/zipalign -f -p 4 $(location :apk-unsigned) \"$tmp/aligned.apk\" && " +
191 _TOOLS + "/apksigner sign --ks \"$ks\" --ks-key-alias androiddebugkey " +
192 "--ks-pass pass:android --key-pass pass:android --out \"$out\" \"$tmp/aligned.apk\" && " +
193 _TOOLS + "/apksigner verify \"$out\" >/dev/null && rm -rf \"$tmp\"",
194 visibility = ["PUBLIC"],
195)
196
197# Does the pin resolve where the action runs, and does what it resolves to run
198# there? Nothing depends on this; it is here to be asked by hand.
199#
200# buck2 build //android:jolt-fetch-check --remote-only --materializations=all \
201# -c build.execution_platforms=root//platforms:remote -c "parser...->root//platforms:remote"
202#
203# It is what found the glibc floor: jolt needs 2.38, and the rbe-ubuntu22-04
204# image this platform used to name ships 2.35, so the fetch succeeded and the
205# binary would not start. The answer was the newer image, not a different pin.
206genrule(
207 name = "jolt-fetch-check",
208 out = "report",
209 cmd = "jolt=`$(location toolchains//dist:dotslash)/dotslash -- fetch $(location //scripts:jolt)`; " +
210 "{ echo \"fetched: $jolt\"; " +
211 "cat /etc/os-release 2>/dev/null | head -1 || true; " +
212 "ldd --version 2>&1 | head -1 || true; " +
213 "echo '--- run ---'; \"$jolt\" --version; echo \"rc=$?\"; } > \"$OUT\" 2>&1 || true",
214)