# `legacyPackages.buildDAndroidTests`: a D package's unittests as one Android# executable per ABI, pushed to a device or emulator with adb. A device has no# runner executable, so the binary runs its tests in sparkles:test-harness's# fallback: one at a time, with plain-text lines (test-runner process-model# §7, open issue O16).## Raw ldc2, as in build-d-android-lib.nix: dub cannot drive a cross build, so# this derivation writes the `dub_test_root` module dub would generate (every# module under `testedDirs`, `package.d` excepted, as dub omits it) and hands it# to `-i` together with the shim's registration module.{inputs,lib, ... }:let# Whether this system can build Android at all (./host.nix).androidHost=import./host.nix{inheritinputs;};in{perSystem={config,pkgs,system, ...}:letndk=config.legacyPackages.androidNdk;sources=config.legacyPackages.sparklesSources;# The in-binary half of the runner (shim, harness, attributes) and# sparkles:base's closure.runnerDirs=sources.libsClosure["base"]++["libs/test-runner/src""libs/test-harness/src""libs/test-attributes/src""libs/profiling/src""libs/profiling-counters/src"];inlib.optionalAttrs(androidHostsystem).supported{legacyPackages.buildDAndroidTests={pname,# The executable's name on the device.exeName,# Source directories (repo-relative) whose every module the runner# tests: the package's `src`, and its test tree if it has one.testedDirs,# Further `-I` directories (repo-relative): the package's in-tree# dependency closure. The runner's own closure is added here.srcDirs ? [],# The dub `versions` of the configuration being mirrored, including# the `Have_*` set of its dependency graph.versions ? [],# dub-registry dependencies: `{ name; src; }` with `src` the# registry zip (a flake input). Their own unittests are not built.dubDeps ? [],# Link the tests as `lib<exeName>.so` beside a launcher named# `exeName` that `dlopen`s it, as a NativeActivity loads an app's# library. Code whose behaviour depends on living in a loaded# module (druntime's TLS ranges) is only exercised this way.sharedLibrary ? false,# Further link arguments for one ABI's target (`t`, an entry of# `androidNdk.targets`): the static C libraries a configuration links.extraLinkFlags ? (_t: []),description ? "D unittests as Android executables, per ABI",}:letimportDirs=lib.unique(testedDirs++srcDirs++runnerDirs);linkFlags=ifsharedLibrarythen"-shared -L${ndk.pageAlignFlags}"else"-L-pie";output=t: ifsharedLibrarythen"lib${exeName}-${t.abi}.so"else"${exeName}-${t.abi}";inpkgs.stdenv.mkDerivation{inheritpname;version="0.1.0";src=sources.sourceForimportDirs;nativeBuildInputs=[(androidHostsystem).ldcAndroidpkgs.unzip];buildPhase=''runHook preBuild${lib.concatMapStrings(d: '' mkdir -p dub-imports/${d.name} (cd dub-imports/${d.name} && unzip -q ${d.src}) '')dubDeps}modules=$(fordirin${toStringtestedDirs}; do (cd"$dir"&&find . -name'*.d' ! -name package.d)done|sed's|^\./||; s|\.d$||; s|/|.|g'| sort) {echo'module dub_test_root;'formin$modules; doecho"static import $m;"; doneecho'alias allModules = imported!"std.meta".AliasSeq!('formin$modules; doecho" $m,"; doneecho');'echo'void main() {}' } > dub_test_root.d${lib.concatMapStrings(t: '' # A dependency's code is compiled once, without its unittests, and # linked: excluded from `-i`, its non-template symbols would stay # unresolved, which a `.so` cannot be loaded with.${lib.concatMapStrings(d: '' ldc2 -mtriple=${t.triple} -relocation-model=pic -O1 -preview=in \${ndk.dPrefixMapFlags} \ -lib -of=dub-imports/${d.name}-${t.abi}.a \ $(find dub-imports/${d.name}/*/source -name '*.d') '')dubDeps} ldc2 -mtriple=${t.triple} -relocation-model=pic -O1 \${ndk.dPrefixMapFlags} \ -preview=in -preview=dip1000 -unittest -checkaction=context -allinst \${toString(map(v: "-d-version=${v}")versions)} \${toString(map(dir: "-I=${dir}")importDirs)} \${toString(map(d: ''-I="$(echo dub-imports/${d.name}/*/source)" -i=-${d.name}'')dubDeps)} \ -i dub_test_root.d \ libs/test-runner/src/sparkles/test_runner/register.d \${toString(map(d: "dub-imports/${d.name}-${t.abi}.a")dubDeps)} \${toString(extraLinkFlagst)} \ -link-defaultlib-shared=false ${linkFlags} -L-llog -L-lm -L-ldl \ -L-Wl,--warn-unresolved-symbols \ -of=${outputt}${lib.optionalStringsharedLibrary''${t.cc} -O2 ${ndk.cPrefixMapFlags} -o ${exeName}-${t.abi}${./dlopen-launcher.c} -ldl ''} '')(lib.attrValuesndk.targets)}runHook postBuild '';installPhase=''runHook preInstall${lib.concatMapStrings(t: '' install -Dm755 ${exeName}-${t.abi} $out/bin/${t.abi}/${exeName}${lib.optionalStringsharedLibrary'' install -Dm755 ${outputt} $out/bin/${t.abi}/lib${exeName}.so ''} '')(lib.attrValuesndk.targets)}runHook postInstall '';dontStrip=true;disallowedReferences=ndk.toolchainPaths;meta={inheritdescription;platforms=(androidHostsystem).platforms;};};};}