Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ pub fn build(b: *std.Build) void {
options.addOption([]const u8, "version", build_zon.version);

// Library module (for use as dependency and test root).
// link_libc is required because wasi.zig / cache.zig / platform.zig use
// `std.c.*` for POSIX operations that std.posix lost in Zig 0.16
// (fsync, mkdirat, unlinkat, renameat, dup, pread/pwrite, futimens, …).
// On Linux the build is strict about this; macOS happens to auto-link
// libc for `extern "c"` decls but both platforms need it.
// link_libc = false post-W46 migration. WASI fd I/O and path-based ops
// now go through `platform.pfd*` helpers (Linux syscalls / Mac libSystem
// auto-link / Win32 kernel32). Env vars come from `std.process.Environ.Map`
// captured in `cli.main` (W46 Phase 1e). The `std.c.*` references that
// survive are all inside `else` branches of `switch (comptime builtin.os.tag)`
// blocks, so they are comptime-pruned on Linux/Windows and still resolve
// to libSystem on Mac.
const mod = b.addModule("zwasm", .{
.root_source_file = b.path("src/types.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libc = false,
});
mod.addOptions("build_options", options);

Expand All @@ -55,7 +57,7 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path("src/cli.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libc = false,
});
cli_mod.addOptions("build_options", options);
const cli = b.addExecutable(.{
Expand All @@ -82,7 +84,7 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path(ex.src),
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libc = false,
});
ex_mod.addImport("zwasm", mod);
const ex_exe = b.addExecutable(.{
Expand All @@ -99,7 +101,7 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path("test/e2e/e2e_runner.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libc = false,
});
e2e_mod.addImport("zwasm", mod);
const e2e = b.addExecutable(.{
Expand All @@ -116,7 +118,7 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path("bench/fib_bench.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libc = false,
});
bench_mod.addImport("zwasm", mod);
const bench = b.addExecutable(.{
Expand All @@ -135,7 +137,7 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path("src/fuzz_loader.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libc = false,
});
fuzz_mod.addImport("zwasm", mod);
const fuzz = b.addExecutable(.{
Expand All @@ -148,7 +150,7 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path("src/fuzz_wat_loader.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libc = false,
});
fuzz_wat_mod.addImport("zwasm", mod);
const fuzz_wat = b.addExecutable(.{
Expand All @@ -162,6 +164,12 @@ pub fn build(b: *std.Build) void {
// Default to ReleaseSafe: Zig 0.15's Debug-mode shared libraries
// crash on Linux x86_64 due to GPA/PIC codegen issues (see #11).
// Users embedding zwasm want optimized code anyway.
//
// C API targets keep link_libc = true: `src/c_api.zig` uses
// `std.heap.c_allocator` as the default backing allocator, which
// requires libc on every platform (Mac libSystem auto-linked, Linux
// glibc/musl, Windows msvcrt). Consumers of libzwasm are C programs
// that always link libc anyway, so this costs them nothing.
const lib_optimize = b.option(bool, "lib-debug", "Build libraries in Debug mode (default: false)") orelse false;
const lib_shared_mod = b.createModule(.{
.root_source_file = b.path("src/c_api.zig"),
Expand Down
7 changes: 7 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const component_mod = @import("component.zig");
const guard_mod = @import("guard.zig");
const jit_mod = vm_mod.jit_mod;
const cache_mod = @import("cache.zig");
const platform = @import("platform.zig");

/// Process-wide Io handle. Populated once at the top of `main` from the
/// `std.process.Init` the compiler-generated entrypoint hands us, and read
Expand All @@ -40,6 +41,12 @@ pub fn main(init: std.process.Init) !void {

cli_io = init.io;

// Expose the process environment to `platform` so subsequent callers
// (`appCacheDir`, `tempDirPath`) can look up HOME / TMPDIR / … without
// going through `std.c.getenv` — a prerequisite for dropping
// `link_libc = true` on Linux (W46 Phase 1e).
platform.setEnvironMap(init.environ_map);

// `init.gpa` is a DebugAllocator-backed allocator in Debug builds
// (leak-checked by start.zig) and the appropriate production allocator
// otherwise. `init.arena` is the process arena — args/environ live there.
Expand Down
Loading
Loading