From d8e9593024eae05d987fc193b776dae2623f2add Mon Sep 17 00:00:00 2001 From: bfredl Date: Tue, 2 Jun 2026 21:47:52 +0200 Subject: [PATCH] feat(build.zig): easier cross-complilation invocation of cross-compilation changed, see news.txt blurb --- BUILD.md | 16 ++++++++++++++-- build.zig | 22 ++++++++++++---------- runtime/doc/news.txt | 13 +++++++++++++ src/gen/gen_steps.zig | 3 ++- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/BUILD.md b/BUILD.md index 4fc32a17f3..4edde31454 100644 --- a/BUILD.md +++ b/BUILD.md @@ -564,6 +564,7 @@ Note that the C++ compiler is explicitly set so that it can be found when the de + `zig build functionaltest` to run all functionaltests + `zig build functionaltest -- test/functional/autocmd/bufenter_spec.lua` to run the tests in one file + `zig build unittest` to run all unittests + + `zig build oldtest` to run all oldtests #### Using system dependencies @@ -575,8 +576,19 @@ See the `prepare` function of [this `PKGBUILD`](https://git.sr.ht/~chinmay/nvim_ ## Cross-compiling -Cross-compilation is not supported, but we collect notes here for reference (improvements welcome). +Cross-compilation is not fully supported, but we collect notes here for reference (improvements welcome). Also relevant for webassembly (WASM) build. -- Set `NVIM_HOST_PRG` so that the docs and tags generation works without +- cmake: Set `NVIM_HOST_PRG` so that the docs and tags generation works without depending on the target binary. + +- zig build: often is enabled by just setting -Dtarget, e.g. from a linux host + + zig build -Dtarget=aarch64-macos + + will automatically compilp a host lua for use during build. The + "-Dhost={target_string}" option can be used to override the platform for + running binaries during compile-time. use "-Dhost=native" to force + cross-compiling or "-Dhost=" (empty string) to assume that target binaries + can run on the host during the build process (e.g. if target is x86 on a + x86_64 system, or if emulation set up with binfmt or similar) diff --git a/build.zig b/build.zig index 01c6660bb8..3e7f285214 100644 --- a/build.zig +++ b/build.zig @@ -58,7 +58,12 @@ pub fn build(b: *std.Build) !void { const modern_unix = is_darwin or os_tag.isBSD() or is_linux; const is_wasm = t.cpu.arch == .wasm32; - const cross_compiling = b.option(bool, "cross", "cross compile") orelse is_wasm; + const h = b.graph.host.result; + const host = b.option([]const u8, "host", "target for host ") orelse + if (target.query.isNative() or h.cpu.arch == t.cpu.arch and h.os.tag == t.os.tag) "" else "native"; + const cross_compiling = host.len > 0; + const target_host = if (cross_compiling) b.resolveTargetQuery(try std.Build.parseTargetQuery(.{ .arch_os_abi = host })) else target; + const emscripten_sysroot = b.option([]const u8, "emscripten-sysroot", "path to emscripten sysroot"); const emscripten_include = if (emscripten_sysroot) |s| std.Build.LazyPath{ .cwd_relative = b.pathJoin(&.{ s, "include" }) } @@ -77,9 +82,6 @@ pub fn build(b: *std.Build) !void { break :blk null; } else null; - // TODO(bfredl): option to set nlua0 target explicitly when cross compiling? - const target_host = if (cross_compiling) b.graph.host else target; - // without cross_compiling we like to reuse libluv etc at the same optimize level const optimize_host = if (cross_compiling) .ReleaseSafe else optimize; @@ -282,13 +284,12 @@ pub fn build(b: *std.Build) !void { const version_lua = gen_config.add("nvim_version.lua", lua_version_info(b)); - var config_str = b.fmt("zig build -Doptimize={s}", .{@tagName(optimize)}); + // Note: these represent the actually resolved target and host platforms, which are often + // more verbose than what needs to be passed in (often -Dhost=native is auto-decteted) + // TODO(bfredl): we could include stuff like specific cpu features and os version but eh + var config_str = b.fmt("zig build -Doptimize={s} -Dtarget={s}", .{ @tagName(optimize), try t.linuxTriple(b.graph.arena) }); if (cross_compiling) { - config_str = b.fmt("{s} -Dcross -Dtarget={s} (host: {s})", .{ - config_str, - try t.linuxTriple(b.allocator), - try b.graph.host.result.linuxTriple(b.allocator), - }); + config_str = b.fmt("{s} -Dhost={s}", .{ config_str, try target_host.result.linuxTriple(b.graph.arena) }); } const versiondef_step = b.addConfigHeader(.{ @@ -463,6 +464,7 @@ pub fn build(b: *std.Build) !void { &api_headers, versiondef_git, version_lua, + !cross_compiling, ); const test_config_step = b.addWriteFiles(); diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 7541f116d0..a9303b11ad 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -131,6 +131,19 @@ BUILD • Building using "zig build" requires zig 0.16.x. +• zig build: "-Dcross" option was removed. Often cross-compilation is now +detected, so e.g. from a linux host + + zig build -Dtarget=aarch64-macos + +will automatically compile a host Lua for use during build. + +The new "-Dhost={target_string}" option can be used to override the used host. +use "-Dhost=native" to force cross-compiling or "-Dhost=" (empty string) to +assume that target binaries can run on the host during the build process (e.g. +if target is x86 on a x86_64 system, or if emulation set up with binfmt or +similar) + DEFAULTS • todo diff --git a/src/gen/gen_steps.zig b/src/gen/gen_steps.zig index 782dd701a3..4b002742f1 100644 --- a/src/gen/gen_steps.zig +++ b/src/gen/gen_steps.zig @@ -11,6 +11,7 @@ pub fn nvim_gen_sources( api_headers: *std.ArrayList(LazyPath), versiondef_git: LazyPath, version_lua: LazyPath, + precompile: bool, ) !struct { *std.Build.Step.WriteFile, LazyPath } { const gen_headers = b.addWriteFiles(); @@ -64,7 +65,7 @@ pub fn nvim_gen_sources( { const gen_step = b.addRunArtifact(nlua0); gen_step.addFileArg(b.path("src/gen/gen_char_blob.lua")); - gen_step.addArg("-c"); + if (precompile) gen_step.addArg("-c"); _ = gen_header(b, gen_step, "lua/vim_module.generated.h", gen_headers); // NB: vim._init_packages and vim.inspect must be be first and second ones // respectively, otherwise --luamod-dev won't work properly.