diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5e49137..b8ccd8c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2602,20 +2602,58 @@ jobs: || { echo "FAIL: mcpp run exited $rc with the project's entries, and the program exits 7"; exit 1; } echo "ok: the simulator bundle carries the project's entries, and the simulator ran it and returned 7" - # NO dist-apk STEP ON THIS HOST, AND THE REASON IS THE ENGINE. The NDK, - # the build tools, the platform, the JDK, the Kotlin compiler and coursier - # all publish a macOS table, and `tests/apk-consumer` was packed here once - # (0.11.0's first run): `mcpp build --target x86_64-linux-android` failed - # at the application's own link with - # - # ld64.lld: error: unknown argument '-soname' - # - # before any dist-apk step ran. On a macOS host mcpp 2026.9.14.2 composes - # the link line in its macOS branch, which states the target triple only - # for an Apple row, so `-fuse-ld=lld` picks the Mach-O flavour - # (mcpp-community/mcpp#647, E3). The steps return when an engine links - # that row; the Windows NDK has no libc++ module surface, so that host - # has no Android row at all. + # dist-apk ON A HOST THAT IS NOT LINUX (0.24.0). Every tool the member + # runs is a native program (`aapt2`, `zipalign`) or a jar on the JDK's + # `java`, and its helper is a Java source file, so no step needs a shell + # and a Windows host packs what a Linux host does. On Windows the NDK's + # libc++ module sources come from `xim:android-ndk-libcxx-modules`, + # which `xim:android-ndk`'s windows entry depends on. The criteria are the + # level-0 step's on the Linux job, plus a level-1 package whose dex holds + # the activity: that is the helper's `run-and-stamp`, `run-d8` and + # `copy-then-jar` all having run. + - name: dist-apk packs a signed APK on this host, level 0 and level 1 + if: ${{ !cancelled() }} + working-directory: tests/apk-consumer + shell: bash + run: | + set -e + T=x86_64-linux-android + check() { # ... + apk="$1"; shift + "$JDK/bin/java" -jar "$BT/lib/apksigner.jar" verify --print-certs "$apk" > verify.log 2>&1 || true + grep -q 'CN=Android Debug' verify.log \ + || { cat verify.log; echo "FAIL: $apk is not signed with the debug key"; exit 1; } + "$JDK/bin/jar" tf "$apk" | tr -d '\r' > listing.log + for f in "$@"; do + grep -qxF "$f" listing.log || { cat listing.log; echo "FAIL: $apk does not list $f"; exit 1; } + done + "$BT/aapt2" dump badging "$apk" 2>/dev/null | tr -d '\r' > badging.log || true + grep -q "^package: name='" badging.log \ + || { cat badging.log; echo "FAIL: aapt2 could not read $apk"; exit 1; } + } + + unset APK_CONSUMER_LEVEL1 + "$MCPP" build --target "$T" + "$MCPP" pack --pkg-format apk --target "$T" | tee pack.log + BT=$(find "$MCPP_HOME/registry/data/xpkgs/xim-x-android-build-tools" -mindepth 1 -maxdepth 1 -type d | head -1) + JDK=$(find "$MCPP_HOME/registry/data/xpkgs/xim-x-jdk-temurin" -mindepth 1 -maxdepth 1 -type d | head -1) + apk=$(find target -name 'apk-consumer.apk' | head -1) + test -n "$apk" || { cat pack.log; echo "FAIL: no apk-consumer.apk"; exit 1; } + check "$apk" "lib/x86_64/libapk-consumer.so" "assets/mcpp-run.json" \ + "AndroidManifest.xml" "assets/generated/res.bin" + echo "ok: level 0 -- a signed, aapt2-readable APK with the native library and the deployed asset" + + export APK_CONSUMER_LEVEL1=1 + "$MCPP" pack --pkg-format apk --target "$T" | tee pack1.log + apk=$(find target -name 'apk-consumer.apk' | head -1) + check "$apk" "classes.dex" "lib/x86_64/libapk-consumer.so" + "$BT/dexdump" -l plain "$(find target -name classes.dex -path '*dist-apk*' | head -1)" > dexdump.log 2>&1 \ + || { cat dexdump.log; echo "FAIL: dexdump could not read the dex"; exit 1; } + for c in MainActivity GeneratedHelper; do + grep -q "org.mcpp.apkconsumer.$c;" dexdump.log \ + || { echo "FAIL: the dex does not hold org.mcpp.apkconsumer.$c"; exit 1; } + done + echo "ok: level 1 -- javac, d8 and the helper ran on this host, and the dex holds the activity and the generated class" - name: the rule declared its own compiler working-directory: tests/spirv-consumer diff --git a/README.md b/README.md index d3a4bd0..81dc845 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ module name the member declares, and configures it there. ```toml [build-dependencies.mcpp] -plugins = { version = "0.23.1", features = ["rules-spirv"], host-module = true } +plugins = { version = "0.24.0", features = ["rules-spirv"], host-module = true } ``` ```cpp diff --git a/dist/apk.cppm b/dist/apk.cppm index fb0c9f7..fb725b0 100644 --- a/dist/apk.cppm +++ b/dist/apk.cppm @@ -337,6 +337,218 @@ inline bool is_dir(const std::string& p) { return !p.empty() && fs::is_directory(p, ec); } +// THE HOST, NOT THE TARGET: every tool here runs on the machine that builds. +// A Windows host names its programs `.exe` and separates a class path +// with `;`. +inline bool host_is_windows() { + return std::string_view(mcpp::host()).find("windows") != std::string_view::npos; +} +inline std::string host_program(const fs::path& dir, std::string_view name) { + return (dir / (std::string(name) + (host_is_windows() ? ".exe" : ""))).string(); +} +inline const char* class_path_separator() { return host_is_windows() ? ";" : ":"; } + +// THE PIPELINE'S HELPER, ONE PROGRAM FOR EVERY HOST (0.24.0). Four steps need +// more than one command or a file set known only when they run; until 0.24.0 +// each was a `#!/bin/sh` script, which a Windows host cannot start. The JDK +// this member already requires runs a single Java source file directly +// (`java DistApk.java ...`, JEP 330, JDK 11), so the helper is that +// file, and its commands do what the scripts did: +// +// copy-then-jar [--dex ] [--stored ] ... +// Copies the linked archive and adds the staged `lib/`, `assets/` and +// the dex with `jar`: aapt2 has no flag for native libraries. +// `--dex ` adds every `classes*.dex` d8 wrote there: a package +// whose classes pass the 64K-method limit of one dex gets +// `classes2.dex` and on, and which of them exist is known only when d8 +// has run. `--stored ` adds an entry uncompressed, which +// native libraries loaded in place must be. +// run-and-stamp ... +// Runs the command, then touches the stamp: javac's and kotlinc's own +// output sets are not knowable at plan time. +// run-d8 ... --inputs ... --options ... +// `d8` DOES NOT ACCEPT A DIRECTORY, MEASURED (2026-09-12, d8 9.2.4, +// `xim:android-build-tools` 37.0.0): `d8 --output ` +// fails in the tool itself, `Unsupported source file type`, one frame +// into `BaseCommand$Builder.addProgramFiles`. So a directory is +// expanded to its `.class` files here, at command time; an archive +// (a JAR, an AAR's classes.jar, the Kotlin stdlib) is passed as it is. +// Earlier dex files are removed first, so a package that shrinks below +// the multidex limit does not keep a stale `classes2.dex`. +// make-module +// THE BASE MODULE OF AN APP BUNDLE. `aapt2 link --proto-format` writes +// the manifest at the archive's root beside `resources.pb` and `res/`; +// bundletool reads a module whose manifest is under `manifest/`, whose +// dex is under `dex/` and whose native libraries and assets are under +// `lib/` and `assets/`. A level-0 package has no dex, which the fourth +// argument states as `-`: an empty argument does not survive the +// action's argv. +// +// A failing tool's exit status is the helper's, and the tool's own output +// reaches the build's, as it did through `set -e`. +inline constexpr std::string_view helper_source = R"java(// mcpp.dist.apk helper. Do not edit. +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.nio.file.attribute.FileTime; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class DistApk { + static final class Failure extends RuntimeException { + final int status; + Failure(int status) { super(null, null, false, false); this.status = status; } + } + + static Failure fail(String message) { + System.err.println("DistApk: " + message); + return new Failure(1); + } + + static void run(List command, Path directory) throws IOException, InterruptedException { + ProcessBuilder builder = new ProcessBuilder(command).inheritIO(); + if (directory != null) builder.directory(directory.toFile()); + int status = builder.start().waitFor(); + if (status != 0) throw new Failure(status); + } + + static List list(String... items) { return new ArrayList<>(Arrays.asList(items)); } + + static List dexFiles(Path directory) throws IOException { + try (Stream files = Files.list(directory)) { + return files.map(f -> f.getFileName().toString()) + .filter(n -> n.startsWith("classes") && n.endsWith(".dex")) + .sorted().collect(Collectors.toList()); + } + } + + static void copyTree(Path from, Path to) throws IOException { + try (Stream files = Files.walk(from)) { + for (Path f : files.sorted().collect(Collectors.toList())) { + Path target = to.resolve(from.relativize(f).toString()); + if (Files.isDirectory(f)) Files.createDirectories(target); + else Files.copy(f, target, StandardCopyOption.REPLACE_EXISTING); + } + } + } + + static void deleteTree(Path root) throws IOException { + if (!Files.exists(root)) return; + try (Stream files = Files.walk(root)) { + for (Path f : files.sorted((a, b) -> b.compareTo(a)).collect(Collectors.toList())) Files.delete(f); + } + } + + static void copyThenJar(List a) throws Exception { + if (a.size() < 3) throw fail("copy-then-jar ..."); + Path dst = Paths.get(a.get(1)).toAbsolutePath(); + String jar = a.get(2); + int i = 3; + String dex = null, storedDir = null, stored = null; + if (i + 1 < a.size() && a.get(i).equals("--dex")) { dex = a.get(i + 1); i += 2; } + if (i + 2 < a.size() && a.get(i).equals("--stored")) { storedDir = a.get(i + 1); stored = a.get(i + 2); i += 3; } + Files.copy(Paths.get(a.get(0)), dst, StandardCopyOption.REPLACE_EXISTING); + if (stored != null) { + run(list(jar, "--update", "--no-compress", "--file", dst.toString(), "-C", storedDir, stored), null); + } + if (i < a.size()) { + List update = list(jar, "uf", dst.toString()); + update.addAll(a.subList(i, a.size())); + run(update, null); + } + if (dex != null) { + List names = dexFiles(Paths.get(dex)); + if (names.isEmpty()) throw fail("no classes*.dex in " + dex); + List update = list(jar, "uf", dst.toString()); + update.addAll(names); + run(update, Paths.get(dex)); + } + } + + static void runAndStamp(List a) throws Exception { + if (a.size() < 2) throw fail("run-and-stamp ..."); + run(a.subList(1, a.size()), null); + Path stamp = Paths.get(a.get(0)); + if (stamp.getParent() != null) Files.createDirectories(stamp.getParent()); + if (Files.exists(stamp)) Files.setLastModifiedTime(stamp, FileTime.fromMillis(System.currentTimeMillis())); + else Files.createFile(stamp); + } + + static void runD8(List a) throws Exception { + int inputsAt = a.indexOf("--inputs"), optionsAt = a.indexOf("--options"); + if (inputsAt < 1 || optionsAt < inputsAt) throw fail("run-d8 ... --inputs ... --options