|
| 1 | +// The image this member binds is built here, from image/twice.c, as a library |
| 2 | +// published as an image is built anywhere: clang for <arch>-unknown-none-elf, |
| 3 | +// the notes `okld note-source' lays down, ld.lld, `okld note'. Then |
| 4 | +// openkal-bind reads include/twice.h and generates the binding. |
| 5 | +#include <cstdio> |
| 6 | +#include <cstdlib> |
| 7 | +import std; |
| 8 | +import mcpp; |
| 9 | +import mcpp.mcpplibs.openkal_bind; |
| 10 | + |
| 11 | +namespace fs = std::filesystem; |
| 12 | + |
| 13 | +namespace { |
| 14 | +bool windows() { return std::string_view(mcpp::host()).find("windows") != std::string_view::npos; } |
| 15 | +std::string q(const fs::path& p) { return "\"" + p.generic_string() + "\""; } |
| 16 | +int run(std::string cmd) { |
| 17 | + if (windows()) cmd = "\"" + cmd + "\""; |
| 18 | + std::fflush(stdout); |
| 19 | + return std::system(cmd.c_str()); |
| 20 | +} |
| 21 | +} |
| 22 | + |
| 23 | +int main() { |
| 24 | + const fs::path root = mcpp::manifest_dir(); |
| 25 | + const fs::path llvm = mcpp::xpkg_dir("xim", "llvm"); |
| 26 | + const fs::path okld = mcpp::dep_bin("openkal-tools", "okld"); |
| 27 | + const std::string arch = mcpp::target_arch(); |
| 28 | + const std::string exe = windows() ? ".exe" : ""; |
| 29 | + if (llvm.empty() || okld.empty()) { |
| 30 | + std::fprintf(stderr, "error: openkal-bind-tests needs xim:llvm and openkal-tools' okld\n"); |
| 31 | + return 1; |
| 32 | + } |
| 33 | + const fs::path out = fs::path(mcpp::out_dir()) / "lib" / arch; |
| 34 | + fs::create_directories(out); |
| 35 | + const fs::path clang = llvm / "bin" / ("clang" + exe); |
| 36 | + // The ELF linker: `ld.lld', or, where the payload ships another name of |
| 37 | + // the same driver (the Windows archive has `lld-link.exe' alone), that |
| 38 | + // driver told which flavour to be. |
| 39 | + std::string lld; |
| 40 | + for (const char* name : { "ld.lld", "lld", "lld-link" }) |
| 41 | + if (const fs::path p = llvm / "bin" / (std::string(name) + exe); lld.empty() && fs::exists(p)) |
| 42 | + lld = q(p) + (std::string_view(name) == "ld.lld" ? "" : " -flavor gnu"); |
| 43 | + if (lld.empty()) { |
| 44 | + std::fprintf(stderr, "error: openkal-bind-tests: no lld in %s\n", (llvm / "bin").generic_string().c_str()); |
| 45 | + return 1; |
| 46 | + } |
| 47 | + const fs::path note = out / "note.S", image = out / "libtwice.okso"; |
| 48 | + std::string flags = std::format("--no-default-config --target={}-unknown-none-elf -ffreestanding -nostdlibinc -fPIC " |
| 49 | + "-fvisibility=hidden -femulated-tls -fasynchronous-unwind-tables -O2", arch); |
| 50 | + if (arch == "aarch64") flags += " -ffixed-x18 -mno-outline-atomics"; |
| 51 | + mcpp::rerun_if_changed((root / "image" / "twice.c").generic_string().c_str()); |
| 52 | + if (run(std::format("{} note-source > {}", q(okld), q(note))) != 0 |
| 53 | + || run(std::format("{} {} -c {} -o {}", q(clang), flags, q(root / "image" / "twice.c"), q(out / "twice.o"))) != 0 |
| 54 | + || run(std::format("{} {} -c {} -o {}", q(clang), flags, q(note), q(out / "note.o"))) != 0 |
| 55 | + || run(std::format("{} -shared -z max-page-size=65536 -z now -z noexecstack --hash-style=both -soname libtwice.okso -o {} {} {}", |
| 56 | + lld, q(image), q(out / "twice.o"), q(out / "note.o"))) != 0 |
| 57 | + || run(std::format("{} note {}", q(okld), q(image))) != 0) { |
| 58 | + std::fprintf(stderr, "error: openkal-bind-tests: the image was not built\n"); |
| 59 | + return 1; |
| 60 | + } |
| 61 | + return mcpp::mcpplibs::openkal_bind::bind({ |
| 62 | + .header = "include/twice.h", |
| 63 | + .image = image.generic_string(), |
| 64 | + }); |
| 65 | +} |
0 commit comments