| Build the APK with buck2, from pins rather than from a second checkout a71ef8f nandi 18d ago | 1 | # The APK, as a graph rather than a script. |
| 2 | # |
| 3 | # Every step the old build-apk.sh ran in sequence is a target here, so a change |
| 4 | # 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 ago | 12 | load(":defs.bzl", "glue", "libvidya") |
| Build the APK with buck2, from pins rather than from a second checkout a71ef8f nandi 18d ago | 13 | |
| 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 ago | 40 | # 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 ago | 44 | |
| 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 | |
| 51 | export_file( |
| 52 | name = "build-jolt-boot.sh", |
| 53 | mode = "reference", |
| 54 | ) |
| 55 | |
| 56 | # --- the UI half ------------------------------------------------------------ |
| 57 | # Two ways in, and both make the library's bytes an input rather than a |
| 58 | # command buck would cache forever: |
| 59 | # |
| 60 | # pinned the .so out of jolt-native's release, fetched by digest. What a |
| 61 | # build gets by default, and what makes an APK buildable with no |
| 62 | # jolt-native checkout and no NDK anywhere on the machine. |
| 63 | # checkout the .so a sibling jolt-native just built, staged into this cell |
| 64 | # by the `buck` recipe. Anyone working on both repos at once |
| 65 | # builds what they are editing. |
| 66 | # |
| 67 | # The recipe decides which, by whether that checkout exists, and says so here. |
| 68 | libvidya(name = "libvidya") |
| 69 | |
| Make the worktree its own buck root, and the glue an input ae207b8 nandi 18d ago | 70 | glue(c_name = "glue-c", include_name = "glue-include") |
| 71 | |
| Build the APK with buck2, from pins rather than from a second checkout a71ef8f nandi 18d ago | 72 | # --- the Jolt half ---------------------------------------------------------- |
| 73 | # The boot image: frq's Scheme, cross-compiled to arm64 by Chez. The sources |
| 74 | # are an input so that editing one rebuilds this; the compile itself reads them |
| 75 | # through the deps.edn the script writes. |
| 76 | # The sources here are frq's own. The other two roots the image is compiled |
| 77 | # from — glimmer, out of the jolt cache, and glimmer-vidya, out of jolt-native |
| 78 | # — are outside this cell, and so are Chez's cross boot files and the pinned |
| 79 | # jolt itself. None of them can be an input, so the `buck` recipe hashes them |
| 80 | # and writes the digest below; naming it in the command is what puts them in |
| 81 | # this action's identity. |
| 82 | # jolt reaches this action as a manifest and a fetcher rather than as a |
| 83 | # program: both are inputs, so nothing here depends on what is installed where |
| 84 | # the compile runs, and a remote worker resolves the same pin — by the same |
| 85 | # digest — from the same place this machine would. |
| 86 | genrule( |
| 87 | name = "jolt-boot", |
| 88 | out = "boot", |
| 89 | srcs = ["//:jolt-sources"], |
| 90 | cmd = "# sources outside this cell: " + read_root_config("frq", "boot_stamp", "unknown") + "\n" + |
| 91 | "DOTSLASH=$(location toolchains//dist:dotslash)/dotslash " + |
| 92 | "JOLT_MANIFEST=$(location //scripts:jolt) " + |
| 93 | # The one source root the script cannot find for itself when there |
| 94 | # is no jolt-native checkout. An input, so a new release recompiles |
| 95 | # the image. |
| 96 | _GLIMMER_VIDYA + |
| 97 | "$(location :build-jolt-boot.sh) \"$OUT\" >&2", |
| 98 | ) |
| 99 | |
| 100 | # The image travels as a blob in an object file's data section; the |
| 101 | # _binary_jolt_boot_{start,end} symbols jolt_main.c reads are named after the |
| 102 | # input *path*, which is why this copies the file somewhere it can be called |
| 103 | # exactly `jolt.boot` before converting it. |
| 104 | genrule( |
| 105 | name = "jolt-boot-obj", |
| 106 | out = "jolt_boot.o", |
| 107 | cmd = _ABS_OUT + " && " + |
| 108 | _TMP + " && " + _NDK_BIN + " && " + |
| 109 | "cp $(location :jolt-boot)/jolt.boot \"$tmp/jolt.boot\" && " + |
| 110 | "( cd \"$tmp\" && \"$ndk\"/llvm-objcopy " + |
| 111 | "--input-target=binary --output-target=elf64-littleaarch64 " + |
| 112 | "--binary-architecture=aarch64 jolt.boot jolt_boot.o ) && " + |
| 113 | "cp \"$tmp/jolt_boot.o\" \"$out\" && rm -rf \"$tmp\"", |
| 114 | ) |
| 115 | |
| 116 | # The glue: jolt-native's android/jolt_main.c over the boot image, linked |
| 117 | # against libvidya by name. --no-undefined is what makes a symbol the Scheme |
| 118 | # side registers but the ABI no longer exports a build failure here rather than |
| 119 | # a crash on the phone. |
| 120 | genrule( |
| 121 | name = "libjoltapp", |
| 122 | out = "libjoltapp.so", |
| 123 | cmd = _ABS_OUT + " && lib=`mktemp -d` && " + |
| 124 | "cp $(location :libvidya) \"$lib/libvidya.so\" && " + |
| 125 | _CC + " -shared -fPIC -O2 -o \"$out\" " + |
| 126 | _GLUE_C + " " + |
| 127 | "$(location :jolt-boot-obj) " + |
| 128 | "-I$(location :jolt-boot) " + |
| 129 | "-I" + _GLUE_INCLUDE + " " + |
| 130 | "-L\"$lib\" " + |
| 131 | _CHEZ + "/tarm64le/boot/tarm64le/libkernel.a " + |
| 132 | _CHEZ + "/lz4/lib/liblz4.a " + |
| 133 | "-lvidya -landroid -llog -lz -ldl -lm -Wl,--no-undefined && " + |
| 134 | "rm -rf \"$lib\"", |
| 135 | ) |
| 136 | |
| 137 | # --- the Java half ---------------------------------------------------------- |
| 138 | # One class: the photo chooser's result has to land somewhere, and native code |
| 139 | # is not somewhere. android.jar on the class path is where every android.* type |
| 140 | # comes from; the JDK's own java.* is what is left. |
| 141 | genrule( |
| 142 | name = "classes-dex", |
| 143 | out = "classes.dex", |
| 144 | srcs = glob(["java/**/*.java"]), |
| 145 | cmd = _ABS_OUT + " && " + _TMP + " && " + |
| 146 | "javac --release 17 --class-path " + _ANDROID_JAR + " -d \"$tmp/classes\" $SRCS && " + |
| 147 | "classes=`find \"$tmp/classes\" -name '*.class'` && " + |
| 148 | _TOOLS + "/d8 --min-api " + _API + " --output \"$tmp\" $classes && " + |
| 149 | "cp \"$tmp/classes.dex\" \"$out\" && rm -rf \"$tmp\"", |
| 150 | ) |
| 151 | |
| 152 | # --- the package ------------------------------------------------------------ |
| 153 | # The libraries are stored rather than deflated: the loader maps them straight |
| 154 | # out of the APK. The dex is read rather than mapped, so it may as well |
| 155 | # compress. OpenSSL travels with the app because the platform's own is not ours |
| 156 | # to load — an app's linker namespace refuses /system/lib64/libssl.so, and |
| 157 | # without one there is no TLS on the phone at all. |
| 158 | genrule( |
| 159 | name = "apk-unsigned", |
| 160 | out = "frq-unsigned.apk", |
| 161 | srcs = ["AndroidManifest.xml"], |
| 162 | cmd = _ABS_OUT + " && stage=`mktemp -d` && " + |
| 163 | "mkdir -p \"$stage/lib/arm64-v8a\" && " + |
| 164 | "cp $(location :libvidya) \"$stage/lib/arm64-v8a/libvidya.so\" && " + |
| 165 | "cp $(location :libjoltapp) \"$stage/lib/arm64-v8a/libjoltapp.so\" && " + |
| 166 | "cp " + _OPENSSL + "/libssl.so " + _OPENSSL + "/libcrypto.so \"$stage/lib/arm64-v8a/\" && " + |
| 167 | "cp $(location :classes-dex) \"$stage/classes.dex\" && " + |
| 168 | _TOOLS + "/aapt2 link -o \"$out\" -I " + _ANDROID_JAR + " " + |
| 169 | "--manifest $SRCS --min-sdk-version " + _API + " --target-sdk-version 36 " + |
| 170 | "--version-code 1 --version-name 0.1.0 >&2 && " + |
| 171 | "( cd \"$stage\" && zip -q -0 \"$out\" lib/arm64-v8a/libvidya.so " + |
| 172 | "lib/arm64-v8a/libjoltapp.so lib/arm64-v8a/libssl.so lib/arm64-v8a/libcrypto.so && " + |
| 173 | "zip -q \"$out\" classes.dex ) && rm -rf \"$stage\"", |
| 174 | ) |
| 175 | |
| 176 | # Aligned and signed with the debug key, which is generated on first use the |
| 177 | # way the SDK's own tools generate it. |
| 178 | genrule( |
| 179 | name = "apk", |
| 180 | out = "frq.apk", |
| 181 | cmd = _ABS_OUT + " && " + _TMP + " && " + |
| 182 | "ks=\"$HOME/.android/debug.keystore\" && " + |
| 183 | "if [ ! -f \"$ks\" ]; then mkdir -p \"$HOME/.android\" && " + |
| 184 | "keytool -genkeypair -v -keystore \"$ks\" -storepass android -keypass android " + |
| 185 | "-alias androiddebugkey -keyalg RSA -keysize 2048 -validity 10000 " + |
| 186 | "-dname 'CN=Android Debug,O=Android,C=US' >&2; fi && " + |
| 187 | _TOOLS + "/zipalign -f -p 4 $(location :apk-unsigned) \"$tmp/aligned.apk\" && " + |
| 188 | _TOOLS + "/apksigner sign --ks \"$ks\" --ks-key-alias androiddebugkey " + |
| 189 | "--ks-pass pass:android --key-pass pass:android --out \"$out\" \"$tmp/aligned.apk\" && " + |
| 190 | _TOOLS + "/apksigner verify \"$out\" >/dev/null && rm -rf \"$tmp\"", |
| 191 | visibility = ["PUBLIC"], |
| 192 | ) |
| 193 | |
| 194 | # Does the pin resolve where the action runs, and does what it resolves to run |
| 195 | # there? Nothing depends on this; it is here to be asked by hand. |
| 196 | # |
| 197 | # buck2 build //android:jolt-fetch-check --remote-only --materializations=all \ |
| 198 | # -c build.execution_platforms=root//platforms:remote -c "parser...->root//platforms:remote" |
| 199 | # |
| 200 | # It is what found the glibc floor: jolt needs 2.38, and the rbe-ubuntu22-04 |
| 201 | # image this platform used to name ships 2.35, so the fetch succeeded and the |
| 202 | # binary would not start. The answer was the newer image, not a different pin. |
| 203 | genrule( |
| 204 | name = "jolt-fetch-check", |
| 205 | out = "report", |
| 206 | cmd = "jolt=`$(location toolchains//dist:dotslash)/dotslash -- fetch $(location //scripts:jolt)`; " + |
| 207 | "{ echo \"fetched: $jolt\"; " + |
| 208 | "cat /etc/os-release 2>/dev/null | head -1 || true; " + |
| 209 | "ldd --version 2>&1 | head -1 || true; " + |
| 210 | "echo '--- run ---'; \"$jolt\" --version; echo \"rc=$?\"; } > \"$OUT\" 2>&1 || true", |
| 211 | ) |