Skip to content

Instantly share code, notes, and snippets.

@mikdusan
Last active February 25, 2021 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikdusan/a4617066c986d46bd415916f6d0b2263 to your computer and use it in GitHub Desktop.
Save mikdusan/a4617066c986d46bd415916f6d0b2263 to your computer and use it in GitHub Desktop.
tryImport musings
// A future version of zig is self-hosting and has a low-latency backend
// for x86_64 and arch64.
//
// A variant of zig may be built with LLVM-backend and this is our
// use-case: build zig w/ LLVM-backend.
// start point:
// → zig binary installation `/opt/zig/{bin,lib}`
// → zig source git worktree `/project/zig`
// EXISTING: @import
`zig build -Denable-llvm ...`
// build.zig source code
{
const llvm = @import("llvm");
}
ERROR: package "llvm" not found
// NEW: @tryImport
`zig build -Denable-llvm=true ...`
// build.zig source code
const enable_llvm = b.option(bool, "enable-llvm", "Enable LLVM-backend") orelse false;
{
if (enable_llvm) {
if (@tryImport("llvm")) |llvm| {
// we have victory
b.addLibraries(llvm.getLibrariesToLink());
b.addSources("src/backends/llvm/main.zig");
...
} else {
// PACKAGE NOT FOUND!
// → find `llvm.json`
// → fetch → `/project/zig/_package/llvm`
// → verify cksum
// → extract → `/project/zig/_extract/llvm`
// → config + make install → `/project/zig/_install/llvm`
exec("zig build -Denable-llvm=true ...");
}
}
}
// build.exe runs...
// → llvm: fetch, verify, extract, config, make install
...
`cmake -DCMAKE_INSTALL_PREFIX=/project/zig/_install/llvm ...`
`make -j8 install`
`zig build -Denable-llvm=true ...`
// build.exe runs a second time...
// → zig c++ src/backends/llvm/wrapper.cpp ...
// → zig build-exe sources.zig +llvm_backend_sources.zig wrapper.o
// → artifact: zig.exe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment