| Build the APK with buck2, from pins rather than from a second checkout a71ef8f nandi 19d ago | 1 | # Where libvidya comes from, which is a question with two answers. |
| 2 | # |
| 3 | # A BUCK file cannot branch — `if` outside a `def` is not this dialect — and |
| 4 | # this is not a `select` either: it does not vary by configuration but by |
| 5 | # whether a checkout is sitting next to this one. So it lives in a macro. |
| 6 | def libvidya(name): |
| 7 | """The Android libvidya: a sibling checkout's, or the pinned release's. |
| 8 | |
| 9 | Either way the library's bytes are an input to what reads them, which is |
| 10 | the part that matters — an action that shelled out to build it would have |
| 11 | nothing to invalidate on and would serve the same stale object forever. |
| 12 | """ |
| 13 | if native.read_root_config("frq", "libvidya", "pinned") == "checkout": |
| 14 | # Staged by the `buck` recipe out of the sibling jolt-native, so that |
| 15 | # anyone working on both repos at once builds what they are editing. |
| 16 | native.export_file( |
| 17 | name = name, |
| 18 | src = "prebuilt/arm64-v8a/libvidya.so", |
| 19 | mode = "reference", |
| 20 | ) |
| 21 | else: |
| 22 | # The release, fetched by digest. This is what makes an APK buildable |
| 23 | # with no jolt-native checkout and no NDK anywhere on the machine. |
| 24 | native.genrule( |
| 25 | name = name, |
| 26 | out = "libvidya.so", |
| 27 | cmd = "cp $(location toolchains//dist:libvidya-android)/libvidya.so \"$OUT\"", |
| 28 | ) |
| Make the worktree its own buck root, and the glue an input ae207b8 nandi 18d ago | 29 | |
| 30 | def glue(c_name, include_name): |
| 31 | """jolt_main.c and the ABI's headers, from wherever libvidya came from. |
| 32 | |
| 33 | Targets rather than paths, and that is the point: naming a checkout's file |
| 34 | by absolute path leaves buck with nothing to notice when it changes, so |
| 35 | editing the glue rebuilt nothing and the APK kept the old object. It has to |
| 36 | be an input. |
| 37 | """ |
| 38 | if native.read_root_config("frq", "libvidya", "pinned") == "checkout": |
| 39 | # Staged by the `buck` recipe, because a cell cannot reach outside its |
| 40 | # own root. |
| 41 | native.export_file( |
| 42 | name = c_name, |
| 43 | src = "prebuilt/glue/android/jolt_main.c", |
| 44 | mode = "reference", |
| 45 | ) |
| 46 | # A genrule rather than a filegroup: a filegroup keeps each file at |
| 47 | # its own path inside the output, so `-I` would have to name the |
| 48 | # staging directory again. This hands back a directory of headers. |
| 49 | native.genrule( |
| 50 | name = include_name, |
| 51 | out = "include", |
| 52 | srcs = native.glob(["prebuilt/glue/include/*.h"]), |
| 53 | cmd = "mkdir -p \"$OUT\" && cp $SRCS \"$OUT\"/", |
| 54 | ) |
| 55 | else: |
| 56 | native.genrule( |
| 57 | name = c_name, |
| 58 | out = "jolt_main.c", |
| 59 | cmd = "cp $(location toolchains//dist:android-glue)/android/jolt_main.c \"$OUT\"", |
| 60 | ) |
| 61 | native.genrule( |
| 62 | name = include_name, |
| 63 | out = "include", |
| 64 | cmd = "cp -r $(location toolchains//dist:android-glue)/include \"$OUT\"", |
| 65 | ) |