|
| 1 | +#!/usr/bin/env rdmd |
| 2 | +/** |
| 3 | +Cross-compile AArch64 runnable tests and execute them under qemu-aarch64. |
| 4 | +
|
| 5 | +Can be run directly or via test runner: |
| 6 | +``` |
| 7 | +rdmd compiler/test/dshell/arm_cross.d |
| 8 | +compiler/test/run.d arm |
| 9 | +``` |
| 10 | +Or via the test runner: |
| 11 | +Install prerequisites: |
| 12 | +
|
| 13 | +Debian/Ubuntu: |
| 14 | +``` |
| 15 | +sudo apt install qemu-user clang lld gcc-aarch64-linux-gnu |
| 16 | +``` |
| 17 | +
|
| 18 | +Arch Linux: |
| 19 | +``` |
| 20 | +sudo pacman -S qemu-user clang lld aarch64-linux-gnu-gcc |
| 21 | +``` |
| 22 | +*/ |
| 23 | +module arm_cross; |
| 24 | + |
| 25 | +import std.array : join; |
| 26 | +import std.file : exists, mkdirRecurse, remove, tempDir; |
| 27 | +import std.file : fileWrite = write; |
| 28 | +import std.path; |
| 29 | +import std.process; |
| 30 | +import std.stdio; |
| 31 | + |
| 32 | +// When run via run.d the DMD env var is set; otherwise fall back to the built binary. |
| 33 | +string dmd() |
| 34 | +{ |
| 35 | + import tools.paths : dmdPath; |
| 36 | + |
| 37 | + auto env = environment.get("DMD"); |
| 38 | + return env ? env : dmdPath; |
| 39 | +} |
| 40 | + |
| 41 | +int main() |
| 42 | +{ |
| 43 | + foreach (tool; ["qemu-aarch64", "clang", "ld.lld"]) |
| 44 | + { |
| 45 | + if (!toolExists(tool)) |
| 46 | + { |
| 47 | + writeln("Skipping arm_cross: '", tool, "' not found in PATH"); |
| 48 | + return 0; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + immutable scriptDir = __FILE_FULL_PATH__.dirName; |
| 53 | + immutable testDir = scriptDir.buildPath("..", "runnable"); |
| 54 | + immutable drImport = scriptDir.buildPath("..", "..", "druntime", "import"); |
| 55 | + immutable outDir = tempDir.buildPath("arm_cross_tests"); |
| 56 | + |
| 57 | + if (!outDir.exists) |
| 58 | + outDir.mkdirRecurse; |
| 59 | + |
| 60 | + int result = 0; |
| 61 | + foreach (testName; [ |
| 62 | + "ai", |
| 63 | + "arm", |
| 64 | + "bcraii", |
| 65 | + "bcraii2", |
| 66 | + "opcolon", |
| 67 | + "powinline", |
| 68 | + "test18472", |
| 69 | + "test21416", |
| 70 | + "test24884", |
| 71 | + ]) |
| 72 | + result |= runTest(outDir, testDir, drImport, testName); |
| 73 | + return result; |
| 74 | +} |
| 75 | + |
| 76 | +int runTest(string outDir, string testDir, string drImport, string testName) |
| 77 | +{ |
| 78 | + immutable armO = buildPath(outDir, testName ~ ".o"); |
| 79 | + immutable armExe = buildPath(outDir, testName); |
| 80 | + immutable armSrc = buildPath(testDir, testName ~ ".d"); |
| 81 | + |
| 82 | + writefln("--- %s (AArch64) ---", testName); |
| 83 | + |
| 84 | + if (run([ |
| 85 | + dmd, "-marm64", "-betterC", "-c", armSrc, "-I" ~ drImport, |
| 86 | + "-of=" ~ armO |
| 87 | + ]) != 0) |
| 88 | + return 1; |
| 89 | + |
| 90 | + if (run([ |
| 91 | + "clang", "--target=aarch64-linux-gnu", "-fuse-ld=lld", "-static", |
| 92 | + armO, "-o", armExe |
| 93 | + ]) != 0) |
| 94 | + return 1; |
| 95 | + |
| 96 | + return run(["qemu-aarch64", armExe]); |
| 97 | +} |
| 98 | + |
| 99 | +int run(string[] args) |
| 100 | +{ |
| 101 | + writeln("+ ", args.join(" ")); |
| 102 | + stdout.flush(); |
| 103 | + return spawnProcess(args).wait; |
| 104 | +} |
| 105 | + |
| 106 | +bool toolExists(string program) |
| 107 | +{ |
| 108 | + return execute(["which", program]).status == 0; |
| 109 | +} |
0 commit comments