From 82e10f4f2857e1c211755a11cec1081a8412b38b Mon Sep 17 00:00:00 2001 From: Ali Chraghi <63465728+alichraghi@users.noreply.github.com> Date: Thu, 29 Sep 2022 19:11:46 +0330 Subject: [PATCH] all: build: thisDir improvements (#570) * build:all: thisDir improvements more performant output, usage code reducement and compileError for wrong usage * glfw: update deprecated code --- build.zig | 22 ++++--- libs/basisu/build.zig | 24 ++++---- libs/ecs/build.zig | 12 ++-- libs/freetype/build.zig | 32 +++++----- libs/freetype/src/main.zig | 20 ++++--- libs/gamemode/build.zig | 12 ++-- libs/glfw/build.zig | 54 +++++++++-------- libs/glfw/src/Window.zig | 2 +- libs/gpu-dawn/sdk.zig | 108 ++++++++++++++++++---------------- libs/gpu/sdk.zig | 16 +++-- libs/sysaudio/sdk.zig | 20 ++++--- libs/sysjs/build.zig | 12 ++-- tools/wasmserve/wasmserve.zig | 10 +++- 13 files changed, 196 insertions(+), 148 deletions(-) diff --git a/build.zig b/build.zig index a2593ae0..dac557ac 100644 --- a/build.zig +++ b/build.zig @@ -27,7 +27,7 @@ const Pkg = std.build.Pkg; pub const pkg = Pkg{ .name = "mach", - .source = .{ .path = thisDir() ++ "/src/main.zig" }, + .source = .{ .path = sdkPath("/src/main.zig") }, .dependencies = &.{ gpu.pkg, ecs.pkg, sysaudio.pkg }, }; @@ -282,14 +282,14 @@ pub const App = struct { const step = blk: { if (platform == .web) { - const lib = b.addSharedLibrary(options.name, (comptime thisDir()) ++ "/src/platform/wasm.zig", .unversioned); + const lib = b.addSharedLibrary(options.name, sdkPath("/src/platform/wasm.zig"), .unversioned); lib.addPackage(gpu.pkg); lib.addPackage(sysaudio.pkg); lib.addPackage(sysjs.pkg); break :blk lib; } else { - const exe = b.addExecutable(options.name, (comptime thisDir()) ++ "/src/platform/native.zig"); + const exe = b.addExecutable(options.name, sdkPath("/src/platform/native.zig")); exe.addPackage(gpu.pkg); exe.addPackage(sysaudio.pkg); exe.addPackage(glfw.pkg); @@ -301,7 +301,7 @@ pub const App = struct { } }; - step.main_pkg_path = (comptime thisDir()) ++ "/src"; + step.main_pkg_path = sdkPath("/src"); step.addPackage(app_pkg); step.setTarget(options.target); @@ -336,14 +336,14 @@ pub const App = struct { inline for (.{ "/src/platform/mach.js", "/libs/sysjs/src/mach-sysjs.js" }) |js| { const install_js = app.b.addInstallFileWithDir( - .{ .path = (comptime thisDir()) ++ js }, + .{ .path = sdkPath(js) }, web_install_dir, std.fs.path.basename(js), ); app.getInstallStep().?.step.dependOn(&install_js.step); } - const html_generator = app.b.addExecutable("html-generator", (comptime thisDir()) ++ "/tools/html-generator/main.zig"); + const html_generator = app.b.addExecutable("html-generator", sdkPath("/tools/html-generator/main.zig")); const run_html_generator = html_generator.run(); const html_file_name = std.mem.concat( app.b.allocator, @@ -421,7 +421,7 @@ fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !vo if (std.mem.eql(u8, no_ensure_submodules, "true")) return; } else |_| {} var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator); - child.cwd = (comptime thisDir()); + child.cwd = sdkPath("/"); child.stderr = std.io.getStdErr(); child.stdout = std.io.getStdOut(); @@ -446,6 +446,10 @@ fn ensureGit(allocator: std.mem.Allocator) void { } } -fn thisDir() []const u8 { - return std.fs.path.dirname(@src().file) orelse "."; +fn sdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; } diff --git a/libs/basisu/build.zig b/libs/basisu/build.zig index c050f9a9..fe28a1d0 100644 --- a/libs/basisu/build.zig +++ b/libs/basisu/build.zig @@ -1,7 +1,7 @@ const std = @import("std"); const Builder = std.build.Builder; -const basisu_root = thisDir() ++ "/upstream/basisu"; +const basisu_root = sdkPath("/upstream/basisu"); pub const pkg = std.build.Pkg{ .name = "basisu", @@ -32,10 +32,10 @@ pub fn build(b: *Builder) void { } pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { - const main_tests = b.addTestExe("basisu-tests", comptime thisDir() ++ "/src/main.zig"); + const main_tests = b.addTestExe("basisu-tests", sdkPath("/src/main.zig")); main_tests.setBuildMode(mode); main_tests.setTarget(target); - main_tests.main_pkg_path = thisDir(); + main_tests.main_pkg_path = sdkPath("/"); link(b, main_tests, target, .{ .encoder = .{}, .transcoder = .{}, @@ -47,12 +47,12 @@ pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget pub fn link(b: *Builder, step: *std.build.LibExeObjStep, target: std.zig.CrossTarget, options: Options) void { if (options.encoder) |encoder_options| { step.linkLibrary(buildEncoder(b, target, encoder_options)); - step.addCSourceFile(comptime thisDir() ++ "/src/encoder/wrapper.cpp", &.{}); + step.addCSourceFile(sdkPath("/src/encoder/wrapper.cpp"), &.{}); step.addIncludePath(basisu_root ++ "/encoder"); } if (options.transcoder) |transcoder_options| { step.linkLibrary(buildTranscoder(b, target, transcoder_options)); - step.addCSourceFile(comptime thisDir() ++ "/src/transcoder/wrapper.cpp", &.{}); + step.addCSourceFile(sdkPath("/src/transcoder/wrapper.cpp"), &.{}); step.addIncludePath(basisu_root ++ "/transcoder"); } } @@ -95,23 +95,27 @@ pub fn buildTranscoder(b: *Builder, target: std.zig.CrossTarget, options: Transc return transcoder; } -fn thisDir() []const u8 { - return std.fs.path.dirname(@src().file) orelse "."; -} - fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !void { if (std.process.getEnvVarOwned(allocator, "NO_ENSURE_SUBMODULES")) |no_ensure_submodules| { defer allocator.free(no_ensure_submodules); if (std.mem.eql(u8, no_ensure_submodules, "true")) return; } else |_| {} var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator); - child.cwd = (comptime thisDir()); + child.cwd = sdkPath("/"); child.stderr = std.io.getStdErr(); child.stdout = std.io.getStdOut(); _ = try child.spawnAndWait(); } +fn sdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; +} + const transcoder_sources = &[_][]const u8{ basisu_root ++ "/transcoder/basisu_transcoder.cpp", }; diff --git a/libs/ecs/build.zig b/libs/ecs/build.zig index dbf04b0e..296f45b8 100644 --- a/libs/ecs/build.zig +++ b/libs/ecs/build.zig @@ -2,7 +2,7 @@ const std = @import("std"); pub const pkg = std.build.Pkg{ .name = "ecs", - .source = .{ .path = thisDir() ++ "/src/main.zig" }, + .source = .{ .path = sdkPath("/src/main.zig") }, .dependencies = &[_]std.build.Pkg{}, }; @@ -14,13 +14,17 @@ pub fn build(b: *std.build.Builder) void { } pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { - const main_tests = b.addTestExe("ecs-tests", (comptime thisDir()) ++ "/src/main.zig"); + const main_tests = b.addTestExe("ecs-tests", sdkPath("/src/main.zig")); main_tests.setBuildMode(mode); main_tests.setTarget(target); main_tests.install(); return main_tests.run(); } -fn thisDir() []const u8 { - return std.fs.path.dirname(@src().file) orelse "."; +fn sdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; } diff --git a/libs/freetype/build.zig b/libs/freetype/build.zig index dedfb793..9ab258a4 100644 --- a/libs/freetype/build.zig +++ b/libs/freetype/build.zig @@ -1,21 +1,21 @@ const std = @import("std"); const Builder = std.build.Builder; -const ft_root = thisDir() ++ "/upstream/freetype"; +const ft_root = sdkPath("/upstream/freetype"); const ft_include_path = ft_root ++ "/include"; -const hb_root = thisDir() ++ "/upstream/harfbuzz"; +const hb_root = sdkPath("/upstream/harfbuzz"); const hb_include_path = hb_root ++ "/src"; -const brotli_root = thisDir() ++ "/upstream/brotli"; +const brotli_root = sdkPath("/upstream/brotli"); pub const pkg = std.build.Pkg{ .name = "freetype", - .source = .{ .path = thisDir() ++ "/src/main.zig" }, + .source = .{ .path = sdkPath("/src/main.zig") }, .dependencies = &.{}, }; pub const harfbuzz_pkg = std.build.Pkg{ .name = "harfbuzz", - .source = .{ .path = thisDir() ++ "/src/harfbuzz/main.zig" }, + .source = .{ .path = sdkPath("/src/harfbuzz/main.zig") }, .dependencies = &.{pkg}, }; @@ -70,7 +70,7 @@ pub fn build(b: *std.build.Builder) !void { } pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { - const main_tests = b.addTestExe("freetype-tests", (comptime thisDir()) ++ "/src/main.zig"); + const main_tests = b.addTestExe("freetype-tests", sdkPath("/src/main.zig")); main_tests.setBuildMode(mode); main_tests.setTarget(target); main_tests.addPackage(pkg); @@ -80,10 +80,10 @@ pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget }, .harfbuzz = .{}, }); - main_tests.main_pkg_path = (comptime thisDir()); + main_tests.main_pkg_path = sdkPath("/"); main_tests.install(); - const harfbuzz_tests = b.addTestExe("harfbuzz-tests", (comptime thisDir()) ++ "/src/harfbuzz/main.zig"); + const harfbuzz_tests = b.addTestExe("harfbuzz-tests", sdkPath("/src/harfbuzz/main.zig")); harfbuzz_tests.setBuildMode(mode); harfbuzz_tests.setTarget(target); harfbuzz_tests.addPackage(pkg); @@ -93,7 +93,7 @@ pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget }, .harfbuzz = .{}, }); - harfbuzz_tests.main_pkg_path = (comptime thisDir()); + harfbuzz_tests.main_pkg_path = sdkPath("/"); harfbuzz_tests.install(); const main_tests_run = main_tests.run(); @@ -190,23 +190,27 @@ fn buildBrotli(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) return lib; } -fn thisDir() []const u8 { - return std.fs.path.dirname(@src().file) orelse "."; -} - fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !void { if (std.process.getEnvVarOwned(allocator, "NO_ENSURE_SUBMODULES")) |no_ensure_submodules| { defer allocator.free(no_ensure_submodules); if (std.mem.eql(u8, no_ensure_submodules, "true")) return; } else |_| {} var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator); - child.cwd = (comptime thisDir()); + child.cwd = sdkPath("/"); child.stderr = std.io.getStdErr(); child.stdout = std.io.getStdOut(); _ = try child.spawnAndWait(); } +fn sdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; +} + const freetype_base_sources = &[_][]const u8{ ft_root ++ "/src/autofit/autofit.c", ft_root ++ "/src/base/ftbase.c", diff --git a/libs/freetype/src/main.zig b/libs/freetype/src/main.zig index dc207a1b..db58fec8 100644 --- a/libs/freetype/src/main.zig +++ b/libs/freetype/src/main.zig @@ -14,8 +14,12 @@ const std = @import("std"); const testing = std.testing; const ft = @import("freetype.zig"); -fn thisDir() []const u8 { - return std.fs.path.dirname(@src().file) orelse "."; +fn sdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; } test { @@ -30,8 +34,8 @@ test { std.testing.refAllDeclsRecursive(@import("computations.zig")); } -const firasans_font_path = thisDir() ++ "/../upstream/assets/FiraSans-Regular.ttf"; -const firasans_font_data = @embedFile(thisDir() ++ "/../upstream/assets/FiraSans-Regular.ttf"); +const firasans_font_path = sdkPath("/../upstream/assets/FiraSans-Regular.ttf"); +const firasans_font_data = @embedFile(sdkPath("/../upstream/assets/FiraSans-Regular.ttf")); test "create face from file" { const lib = try ft.Library.init(); @@ -63,14 +67,14 @@ test "load glyph" { test "attach file" { const lib = try ft.Library.init(); - const face = try lib.createFace(comptime thisDir() ++ "/../upstream/assets/DejaVuSans.pfb", 0); - try face.attachFile(comptime thisDir() ++ "/../upstream/assets/DejaVuSans.pfm"); + const face = try lib.createFace(sdkPath("/../upstream/assets/DejaVuSans.pfb"), 0); + try face.attachFile(sdkPath("/../upstream/assets/DejaVuSans.pfm")); } test "attach from memory" { const lib = try ft.Library.init(); - const face = try lib.createFace(comptime thisDir() ++ "/../upstream/assets/DejaVuSans.pfb", 0); - const file = @embedFile(comptime thisDir() ++ "/../upstream/assets/DejaVuSans.pfm"); + const face = try lib.createFace(sdkPath("/../upstream/assets/DejaVuSans.pfb"), 0); + const file = @embedFile(sdkPath("/../upstream/assets/DejaVuSans.pfm")); try face.attachMemory(file); } diff --git a/libs/gamemode/build.zig b/libs/gamemode/build.zig index d7963a4d..03c21d4e 100644 --- a/libs/gamemode/build.zig +++ b/libs/gamemode/build.zig @@ -2,13 +2,17 @@ const std = @import("std"); pub const pkg = std.build.Pkg{ .name = "gamemode", - .source = .{ .path = thisDir() ++ "/gamemode.zig" }, + .source = .{ .path = sdkPath("/gamemode.zig") }, }; pub fn link(step: *std.build.LibExeObjStep) void { - step.addIncludePath(comptime thisDir() ++ "/upstream/include"); + step.addIncludePath(sdkPath("/upstream/include")); } -fn thisDir() []const u8 { - return std.fs.path.dirname(@src().file) orelse "."; +fn sdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; } diff --git a/libs/glfw/build.zig b/libs/glfw/build.zig index 18e711ef..7a866743 100644 --- a/libs/glfw/build.zig +++ b/libs/glfw/build.zig @@ -4,17 +4,17 @@ const Builder = std.build.Builder; const system_sdk = @import("system_sdk.zig"); -pub fn build(b: *Builder) void { +pub fn build(b: *Builder) !void { const mode = b.standardReleaseOptions(); const target = b.standardTargetOptions(.{}); const test_step = b.step("test", "Run library tests"); - test_step.dependOn(&testStep(b, mode, target).step); - test_step.dependOn(&testStepShared(b, mode, target).step); + test_step.dependOn(&(try testStep(b, mode, target)).step); + test_step.dependOn(&(try testStepShared(b, mode, target)).step); } pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) !*std.build.RunStep { - const main_tests = b.addTestExe("glfw-tests", thisDir() ++ "/src/main.zig"); + const main_tests = b.addTestExe("glfw-tests", sdkPath("/src/main.zig")); main_tests.setBuildMode(mode); main_tests.setTarget(target); try link(b, main_tests, .{}); @@ -23,7 +23,7 @@ pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget } fn testStepShared(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) !*std.build.RunStep { - const main_tests = b.addTestExe("glfw-tests-shared", thisDir() ++ "/src/main.zig"); + const main_tests = b.addTestExe("glfw-tests-shared", sdkPath("/src/main.zig")); main_tests.setBuildMode(mode); main_tests.setTarget(target); try link(b, main_tests, .{ .shared = true }); @@ -66,7 +66,7 @@ pub const Options = struct { pub const pkg = std.build.Pkg{ .name = "glfw", - .source = .{ .path = thisDir() ++ "/src/main.zig" }, + .source = .{ .path = sdkPath("/src/main.zig") }, }; // TODO(self-hosted): HACK: workaround https://github.com/ziglang/zig/issues/12784 @@ -74,9 +74,9 @@ pub const pkg = std.build.Pkg{ // Extracted from a build using stage1 from zig-cache/ (`cimport/c_darwin_native.zig`) // Then find+replace `= ?fn` -> `= ?*const fn` fn cimportWorkaround() void { - const dest_dir = std.fs.cwd().openDir(thisDir() ++ "/src", .{}) catch unreachable; - const cn_path = thisDir() ++ "/src/cimport/" ++ if (builtin.os.tag == .macos) "c_darwin_native.zig" else "c_normal_native.zig"; - std.fs.cwd().copyFile(cn_path, dest_dir, thisDir() ++ "/src/c_native.zig", .{}) catch unreachable; + const dest_dir = std.fs.cwd().openDir(sdkPath("/src"), .{}) catch unreachable; + const cn_path = sdkPath("/src/cimport/" ++ if (builtin.os.tag == .macos) "c_darwin_native.zig" else "c_normal_native.zig"); + std.fs.cwd().copyFile(cn_path, dest_dir, sdkPath("/src/c_native.zig"), .{}) catch unreachable; } pub const LinkError = error{FailedToLinkGPU} || BuildError; @@ -120,21 +120,21 @@ fn buildLibrary(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget } fn addGLFWIncludes(step: *std.build.LibExeObjStep) void { - step.addIncludePath(thisDir() ++ "/upstream/glfw/include"); - step.addIncludePath(thisDir() ++ "/upstream/vulkan_headers/include"); + step.addIncludePath(sdkPath("/upstream/glfw/include")); + step.addIncludePath(sdkPath("/upstream/vulkan_headers/include")); } fn addGLFWSources(b: *Builder, lib: *std.build.LibExeObjStep, options: Options) std.mem.Allocator.Error!void { - const include_glfw_src = "-I" ++ thisDir() ++ "/upstream/glfw/src"; + const include_glfw_src = comptime "-I" ++ sdkPath("/upstream/glfw/src"); switch (lib.target_info.target.os.tag) { .windows => lib.addCSourceFiles(&.{ - thisDir() ++ "/src/sources_all.c", - thisDir() ++ "/src/sources_windows.c", + sdkPath("/src/sources_all.c"), + sdkPath("/src/sources_windows.c"), }, &.{ "-D_GLFW_WIN32", include_glfw_src }), .macos => lib.addCSourceFiles(&.{ - thisDir() ++ "/src/sources_all.c", - thisDir() ++ "/src/sources_macos.m", - thisDir() ++ "/src/sources_macos.c", + sdkPath("/src/sources_all.c"), + sdkPath("/src/sources_macos.m"), + sdkPath("/src/sources_macos.c"), }, &.{ "-D_GLFW_COCOA", include_glfw_src }), else => { // TODO(future): for now, Linux can't be built with musl: @@ -145,17 +145,17 @@ fn addGLFWSources(b: *Builder, lib: *std.build.LibExeObjStep, options: Options) // ``` var sources = std.ArrayList([]const u8).init(b.allocator); var flags = std.ArrayList([]const u8).init(b.allocator); - try sources.append(thisDir() ++ "/src/sources_all.c"); - try sources.append(thisDir() ++ "/src/sources_linux.c"); + try sources.append(sdkPath("/src/sources_all.c")); + try sources.append(sdkPath("/src/sources_linux.c")); if (options.x11) { - try sources.append(thisDir() ++ "/src/sources_linux_x11.c"); + try sources.append(sdkPath("/src/sources_linux_x11.c")); try flags.append("-D_GLFW_X11"); } if (options.wayland) { - try sources.append(thisDir() ++ "/src/sources_linux_wayland.c"); + try sources.append(sdkPath("/src/sources_linux_wayland.c")); try flags.append("-D_GLFW_WAYLAND"); } - try flags.append("-I" ++ thisDir() ++ "/upstream/glfw/src"); + try flags.append(comptime "-I" ++ sdkPath("/upstream/glfw/src")); // TODO(upstream): glfw can't compile on clang15 without this flag try flags.append("-Wno-implicit-function-declaration"); @@ -209,13 +209,17 @@ fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !vo if (std.mem.eql(u8, no_ensure_submodules, "true")) return; } else |_| {} var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator); - child.cwd = thisDir(); + child.cwd = sdkPath("/"); child.stderr = std.io.getStdErr(); child.stdout = std.io.getStdOut(); _ = try child.spawnAndWait(); } -inline fn thisDir() []const u8 { - return comptime std.fs.path.dirname(@src().file) orelse "."; +fn sdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; } diff --git a/libs/glfw/src/Window.zig b/libs/glfw/src/Window.zig index 3912317f..0f6f4651 100644 --- a/libs/glfw/src/Window.zig +++ b/libs/glfw/src/Window.zig @@ -2420,7 +2420,7 @@ pub inline fn setDropCallback(self: Window, comptime callback: ?fn (window: Wind inline fn hint(h: Hint, value: anytype) void { internal_debug.assertInitialized(); const value_type = @TypeOf(value); - const value_type_info: std.builtin.TypeInfo = @typeInfo(value_type); + const value_type_info: std.builtin.Type = @typeInfo(value_type); switch (value_type_info) { .Int, .ComptimeInt => { diff --git a/libs/gpu-dawn/sdk.zig b/libs/gpu-dawn/sdk.zig index a0eb4908..8bc4b850 100644 --- a/libs/gpu-dawn/sdk.zig +++ b/libs/gpu-dawn/sdk.zig @@ -91,9 +91,9 @@ pub fn Sdk(comptime deps: anytype) type { fn linkFromSource(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !void { try ensureSubmodules(b.allocator); - step.addIncludePath(comptime thisDir() ++ "/libs/dawn/out/Debug/gen/include"); - step.addIncludePath(comptime thisDir() ++ "/libs/dawn/include"); - step.addIncludePath(comptime thisDir() ++ "/src/dawn"); + step.addIncludePath(sdkPath("/libs/dawn/out/Debug/gen/include")); + step.addIncludePath(sdkPath("/libs/dawn/include")); + step.addIncludePath(sdkPath("/src/dawn")); if (options.separate_libs) { const lib_mach_dawn_native = try buildLibMachDawnNative(b, step, options); @@ -130,7 +130,7 @@ pub fn Sdk(comptime deps: anytype) type { return; } - const main_abs = comptime thisDir() ++ "/src/dawn/dummy.zig"; + const main_abs = sdkPath("/src/dawn/dummy.zig"); const lib_dawn = b.addStaticLibrary("dawn", main_abs); lib_dawn.setBuildMode(if (options.debug) .Debug else .ReleaseFast); lib_dawn.setTarget(step.target); @@ -156,7 +156,7 @@ pub fn Sdk(comptime deps: anytype) type { return; } var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", "--recursive" }, allocator); - child.cwd = comptime thisDir(); + child.cwd = sdkPath("/"); child.stderr = std.io.getStdErr(); child.stdout = std.io.getStdOut(); _ = try child.spawnAndWait(); @@ -241,7 +241,7 @@ pub fn Sdk(comptime deps: anytype) type { step.linkLibCpp(); step.addIncludePath(include_dir); - step.addIncludePath(comptime thisDir() ++ "/src/dawn"); + step.addIncludePath(sdkPath("/src/dawn")); if (options.linux_window_manager != null and options.linux_window_manager.? == .X11) { step.linkSystemLibraryName("X11"); @@ -438,7 +438,7 @@ pub fn Sdk(comptime deps: anytype) type { const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = &.{ "git", "branch", branch, "--contains", commit }, - .cwd = comptime thisDir(), + .cwd = sdkPath("/"), }); defer { allocator.free(result.stdout); @@ -451,7 +451,7 @@ pub fn Sdk(comptime deps: anytype) type { const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = &.{ "git", "rev-parse", "HEAD" }, - .cwd = comptime thisDir(), + .cwd = sdkPath("/"), }); defer allocator.free(result.stderr); if (result.stdout.len > 0) return result.stdout[0 .. result.stdout.len - 1]; // trim newline @@ -462,7 +462,7 @@ pub fn Sdk(comptime deps: anytype) type { const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = &.{ "git", "clone", repository, dir }, - .cwd = comptime thisDir(), + .cwd = sdkPath("/"), }); defer { allocator.free(result.stdout); @@ -480,7 +480,7 @@ pub fn Sdk(comptime deps: anytype) type { std.ChildProcess.init(&.{ "curl", "--insecure", "-L", "-o", target_file, url }, allocator) else std.ChildProcess.init(&.{ "curl", "-L", "-o", target_file, url }, allocator); - child.cwd = comptime thisDir(); + child.cwd = sdkPath("/"); child.stderr = std.io.getStdErr(); child.stdout = std.io.getStdOut(); _ = try child.spawnAndWait(); @@ -490,7 +490,7 @@ pub fn Sdk(comptime deps: anytype) type { const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = &.{ "curl", "--version" }, - .cwd = comptime thisDir(), + .cwd = sdkPath("/"), }) catch { // e.g. FileNotFound std.log.err("mach: error: 'curl --version' failed. Is curl not installed?", .{}); std.process.exit(1); @@ -512,7 +512,7 @@ pub fn Sdk(comptime deps: anytype) type { fn buildLibMachDawnNative(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { const lib = if (!options.separate_libs) step else blk: { - const main_abs = comptime thisDir() ++ "/src/dawn/dummy.zig"; + const main_abs = sdkPath("/src/dawn/dummy.zig"); const separate_lib = b.addStaticLibrary("dawn-native-mach", main_abs); separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); separate_lib.setTarget(step.target); @@ -548,7 +548,7 @@ pub fn Sdk(comptime deps: anytype) type { // Builds common sources; derived from src/common/BUILD.gn fn buildLibDawnCommon(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { const lib = if (!options.separate_libs) step else blk: { - const main_abs = comptime thisDir() ++ "/src/dawn/dummy.zig"; + const main_abs = sdkPath("/src/dawn/dummy.zig"); const separate_lib = b.addStaticLibrary("dawn-common", main_abs); separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); separate_lib.setTarget(step.target); @@ -583,11 +583,11 @@ pub fn Sdk(comptime deps: anytype) type { // TODO(build-system): pass system SDK options through deps.system_sdk.include(b, lib, .{}); lib.linkFramework("Foundation"); - const abs_path = comptime thisDir() ++ "/libs/dawn/src/dawn/common/SystemUtils_mac.mm"; + const abs_path = sdkPath("/libs/dawn/src/dawn/common/SystemUtils_mac.mm"); try cpp_sources.append(abs_path); } if (step.target_info.target.os.tag == .windows) { - const abs_path = comptime thisDir() ++ "/libs/dawn/src/dawn/common/WindowsUtils.cpp"; + const abs_path = sdkPath("/libs/dawn/src/dawn/common/WindowsUtils.cpp"); try cpp_sources.append(abs_path); } @@ -601,7 +601,7 @@ pub fn Sdk(comptime deps: anytype) type { // Build dawn platform sources; derived from src/dawn/platform/BUILD.gn fn buildLibDawnPlatform(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { const lib = if (!options.separate_libs) step else blk: { - const main_abs = comptime thisDir() ++ "/src/dawn/dummy.zig"; + const main_abs = sdkPath("/src/dawn/dummy.zig"); const separate_lib = b.addStaticLibrary("dawn-platform", main_abs); separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); separate_lib.setTarget(step.target); @@ -626,7 +626,7 @@ pub fn Sdk(comptime deps: anytype) type { "src/dawn/platform/WorkerThread.cpp", "src/dawn/platform/tracing/EventTracer.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } @@ -673,7 +673,7 @@ pub fn Sdk(comptime deps: anytype) type { // Builds dawn native sources; derived from src/dawn/native/BUILD.gn fn buildLibDawnNative(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { const lib = if (!options.separate_libs) step else blk: { - const main_abs = comptime thisDir() ++ "/src/dawn/dummy.zig"; + const main_abs = sdkPath("/src/dawn/dummy.zig"); const separate_lib = b.addStaticLibrary("dawn-native", main_abs); separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); separate_lib.setTarget(step.target); @@ -750,7 +750,7 @@ pub fn Sdk(comptime deps: anytype) type { inline for ([_][]const u8{ "src/dawn/mingw_helpers.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/" ++ path; + const abs_path = sdkPath("/" ++ path); try cpp_sources.append(abs_path); } @@ -786,7 +786,7 @@ pub fn Sdk(comptime deps: anytype) type { inline for ([_][]const u8{ "src/dawn/native/XlibXcbFunctions.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } } @@ -794,7 +794,7 @@ pub fn Sdk(comptime deps: anytype) type { inline for ([_][]const u8{ "src/dawn/native/null/DeviceNull.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } @@ -802,7 +802,7 @@ pub fn Sdk(comptime deps: anytype) type { inline for ([_][]const u8{ "src/dawn/native/SpirvValidation.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } } @@ -832,7 +832,7 @@ pub fn Sdk(comptime deps: anytype) type { "src/dawn/native/vulkan/external_memory/MemoryServiceOpaqueFD.cpp", "src/dawn/native/vulkan/external_semaphore/SemaphoreServiceFD.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } } else if (step.target_info.target.os.tag == .fuchsia) { @@ -840,7 +840,7 @@ pub fn Sdk(comptime deps: anytype) type { "src/dawn/native/vulkan/external_memory/MemoryServiceZirconHandle.cpp", "src/dawn/native/vulkan/external_semaphore/SemaphoreServiceZirconHandle.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } } else { @@ -848,7 +848,7 @@ pub fn Sdk(comptime deps: anytype) type { "src/dawn/native/vulkan/external_memory/MemoryServiceNull.cpp", "src/dawn/native/vulkan/external_semaphore/SemaphoreServiceNull.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } } @@ -896,7 +896,7 @@ pub fn Sdk(comptime deps: anytype) type { inline for ([_][]const u8{ "src/dawn/native/null/NullBackend.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } @@ -904,7 +904,7 @@ pub fn Sdk(comptime deps: anytype) type { inline for ([_][]const u8{ "src/dawn/native/d3d12/D3D12Backend.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } } @@ -912,7 +912,7 @@ pub fn Sdk(comptime deps: anytype) type { inline for ([_][]const u8{ "src/dawn/native/opengl/OpenGLBackend.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } } @@ -920,7 +920,7 @@ pub fn Sdk(comptime deps: anytype) type { inline for ([_][]const u8{ "src/dawn/native/vulkan/VulkanBackend.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } // TODO(build-system): vulkan @@ -944,7 +944,7 @@ pub fn Sdk(comptime deps: anytype) type { // Builds tint sources; derived from src/tint/BUILD.gn fn buildLibTint(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { const lib = if (!options.separate_libs) step else blk: { - const main_abs = comptime thisDir() ++ "/src/dawn/dummy.zig"; + const main_abs = sdkPath("/src/dawn/dummy.zig"); const separate_lib = b.addStaticLibrary("tint", main_abs); separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); separate_lib.setTarget(step.target); @@ -1000,9 +1000,9 @@ pub fn Sdk(comptime deps: anytype) type { var cpp_sources = std.ArrayList([]const u8).init(b.allocator); switch (step.target_info.target.os.tag) { - .windows => try cpp_sources.append(comptime thisDir() ++ "/libs/dawn/src/tint/diagnostic/printer_windows.cc"), - .linux => try cpp_sources.append(comptime thisDir() ++ "/libs/dawn/src/tint/diagnostic/printer_linux.cc"), - else => try cpp_sources.append(comptime thisDir() ++ "/libs/dawn/src/tint/diagnostic/printer_other.cc"), + .windows => try cpp_sources.append(sdkPath("/libs/dawn/src/tint/diagnostic/printer_windows.cc")), + .linux => try cpp_sources.append(sdkPath("/libs/dawn/src/tint/diagnostic/printer_linux.cc")), + else => try cpp_sources.append(sdkPath("/libs/dawn/src/tint/diagnostic/printer_other.cc")), } // libtint_sem_src @@ -1092,7 +1092,7 @@ pub fn Sdk(comptime deps: anytype) type { // Builds third_party/vulkan-deps/spirv-tools sources; derived from third_party/vulkan-deps/spirv-tools/src/BUILD.gn fn buildLibSPIRVTools(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { const lib = if (!options.separate_libs) step else blk: { - const main_abs = comptime thisDir() ++ "/src/dawn/dummy.zig"; + const main_abs = sdkPath("/src/dawn/dummy.zig"); const separate_lib = b.addStaticLibrary("spirv-tools", main_abs); separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); separate_lib.setTarget(step.target); @@ -1160,7 +1160,7 @@ pub fn Sdk(comptime deps: anytype) type { // fn buildLibAbseilCpp(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { const lib = if (!options.separate_libs) step else blk: { - const main_abs = comptime thisDir() ++ "/src/dawn/dummy.zig"; + const main_abs = sdkPath("/src/dawn/dummy.zig"); const separate_lib = b.addStaticLibrary("abseil-cpp-common", main_abs); separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); separate_lib.setTarget(step.target); @@ -1222,7 +1222,7 @@ pub fn Sdk(comptime deps: anytype) type { // Buids dawn wire sources; derived from src/dawn/wire/BUILD.gn fn buildLibDawnWire(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { const lib = if (!options.separate_libs) step else blk: { - const main_abs = comptime thisDir() ++ "/src/dawn/dummy.zig"; + const main_abs = sdkPath("/src/dawn/dummy.zig"); const separate_lib = b.addStaticLibrary("dawn-wire", main_abs); separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); separate_lib.setTarget(step.target); @@ -1259,7 +1259,7 @@ pub fn Sdk(comptime deps: anytype) type { // Builds dawn utils sources; derived from src/dawn/utils/BUILD.gn fn buildLibDawnUtils(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { const lib = if (!options.separate_libs) step else blk: { - const main_abs = comptime thisDir() ++ "/src/dawn/dummy.zig"; + const main_abs = sdkPath("/src/dawn/dummy.zig"); const separate_lib = b.addStaticLibrary("dawn-utils", main_abs); separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); separate_lib.setTarget(step.target); @@ -1284,7 +1284,7 @@ pub fn Sdk(comptime deps: anytype) type { "src/dawn/utils/BackendBinding.cpp", "src/dawn/utils/NullBinding.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } @@ -1292,7 +1292,7 @@ pub fn Sdk(comptime deps: anytype) type { inline for ([_][]const u8{ "src/dawn/utils/D3D12Binding.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } try flags.appendSlice(dawn_d3d12_flags); @@ -1301,7 +1301,7 @@ pub fn Sdk(comptime deps: anytype) type { inline for ([_][]const u8{ "src/dawn/utils/MetalBinding.mm", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } } @@ -1310,7 +1310,7 @@ pub fn Sdk(comptime deps: anytype) type { inline for ([_][]const u8{ "src/dawn/utils/OpenGLBinding.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } } @@ -1319,7 +1319,7 @@ pub fn Sdk(comptime deps: anytype) type { inline for ([_][]const u8{ "src/dawn/utils/VulkanBinding.cpp", }) |path| { - const abs_path = comptime thisDir() ++ "/libs/dawn/" ++ path; + const abs_path = sdkPath("/libs/dawn/" ++ path); try cpp_sources.append(abs_path); } } @@ -1334,7 +1334,7 @@ pub fn Sdk(comptime deps: anytype) type { // Buids dxcompiler sources; derived from libs/DirectXShaderCompiler/CMakeLists.txt fn buildLibDxcompiler(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { const lib = if (!options.separate_libs) step else blk: { - const main_abs = comptime thisDir() ++ "/src/dawn/dummy.zig"; + const main_abs = sdkPath("/src/dawn/dummy.zig"); const separate_lib = b.addStaticLibrary("dxcompiler", main_abs); separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); separate_lib.setTarget(step.target); @@ -1432,14 +1432,6 @@ pub fn Sdk(comptime deps: anytype) type { return lib; } - fn include(comptime rel: []const u8) []const u8 { - return "-I" ++ (comptime thisDir()) ++ "/" ++ rel; - } - - fn thisDir() []const u8 { - return std.fs.path.dirname(@src().file) orelse "."; - } - fn appendLangScannedSources( b: *Builder, step: *std.build.LibExeObjStep, @@ -1503,7 +1495,7 @@ pub fn Sdk(comptime deps: anytype) type { excluding: []const []const u8, excluding_contains: []const []const u8, ) !void { - const abs_dir = try std.fs.path.join(b.allocator, &.{ comptime thisDir(), rel_dir }); + const abs_dir = try std.fs.path.join(b.allocator, &.{ sdkPath("/"), rel_dir }); defer b.allocator.free(abs_dir); var dir = try std.fs.openIterableDirAbsolute(abs_dir, .{}); defer dir.close(); @@ -1541,5 +1533,17 @@ pub fn Sdk(comptime deps: anytype) type { try dst.append(abs_path); } } + + fn include(comptime rel: []const u8) []const u8 { + return comptime "-I" ++ sdkPath("/" ++ rel); + } + + fn sdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; + } }; } diff --git a/libs/gpu/sdk.zig b/libs/gpu/sdk.zig index e906a5c4..8dece138 100644 --- a/libs/gpu/sdk.zig +++ b/libs/gpu/sdk.zig @@ -3,7 +3,7 @@ const std = @import("std"); pub fn Sdk(comptime deps: anytype) type { return struct { pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: Options) !*std.build.RunStep { - const main_tests = b.addTestExe("gpu-tests", (comptime thisDir()) ++ "/src/main.zig"); + const main_tests = b.addTestExe("gpu-tests", sdkPath("/src/main.zig")); main_tests.setBuildMode(mode); main_tests.setTarget(target); try link(b, main_tests, options); @@ -18,7 +18,7 @@ pub fn Sdk(comptime deps: anytype) type { pub const pkg = std.build.Pkg{ .name = "gpu", - .source = .{ .path = thisDir() ++ "/src/main.zig" }, + .source = .{ .path = sdkPath("/src/main.zig") }, .dependencies = &.{deps.glfw.pkg}, }; @@ -26,13 +26,17 @@ pub fn Sdk(comptime deps: anytype) type { if (step.target.toTarget().cpu.arch != .wasm32) { try deps.glfw.link(b, step, options.glfw_options); try deps.gpu_dawn.link(b, step, options.gpu_dawn_options); - step.addCSourceFile((comptime thisDir()) ++ "/src/mach_dawn.cpp", &.{"-std=c++17"}); - step.addIncludePath((comptime thisDir()) ++ "/src"); + step.addCSourceFile(sdkPath("/src/mach_dawn.cpp"), &.{"-std=c++17"}); + step.addIncludePath(sdkPath("/src")); } } - fn thisDir() []const u8 { - return std.fs.path.dirname(@src().file) orelse "."; + fn sdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; } }; } diff --git a/libs/sysaudio/sdk.zig b/libs/sysaudio/sdk.zig index 35105547..0b2cb285 100644 --- a/libs/sysaudio/sdk.zig +++ b/libs/sysaudio/sdk.zig @@ -2,17 +2,17 @@ const std = @import("std"); pub fn Sdk(comptime deps: anytype) type { return struct { - const soundio_path = thisDir() ++ "/upstream/soundio"; + const soundio_path = sdkPath("/upstream/soundio"); pub const pkg = std.build.Pkg{ .name = "sysaudio", - .source = .{ .path = thisDir() ++ "/src/main.zig" }, + .source = .{ .path = sdkPath("/src/main.zig") }, .dependencies = &.{ deps.sysjs.pkg, soundio_pkg }, }; pub const soundio_pkg = std.build.Pkg{ .name = "soundio", - .source = .{ .path = thisDir() ++ "/soundio/main.zig" }, + .source = .{ .path = sdkPath("/soundio/main.zig") }, }; pub const Options = struct { @@ -20,13 +20,13 @@ pub fn Sdk(comptime deps: anytype) type { }; pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { - const soundio_tests = b.addTestExe("soundio-tests", (comptime thisDir()) ++ "/soundio/main.zig"); + const soundio_tests = b.addTestExe("soundio-tests", sdkPath("/soundio/main.zig")); soundio_tests.setBuildMode(mode); soundio_tests.setTarget(target); link(b, soundio_tests, .{}); soundio_tests.install(); - const main_tests = b.addTestExe("sysaudio-tests", (comptime thisDir()) ++ "/src/main.zig"); + const main_tests = b.addTestExe("sysaudio-tests", sdkPath("/src/main.zig")); main_tests.setBuildMode(mode); main_tests.setTarget(target); main_tests.addPackage(soundio_pkg); @@ -104,15 +104,19 @@ pub fn Sdk(comptime deps: anytype) type { if (std.mem.eql(u8, no_ensure_submodules, "true")) return; } else |_| {} var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator); - child.cwd = (comptime thisDir()); + child.cwd = sdkPath("/"); child.stderr = std.io.getStdErr(); child.stdout = std.io.getStdOut(); _ = try child.spawnAndWait(); } - fn thisDir() []const u8 { - return std.fs.path.dirname(@src().file) orelse "."; + fn sdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; } const soundio_sources = &[_][]const u8{ diff --git a/libs/sysjs/build.zig b/libs/sysjs/build.zig index 74fa2dc2..eb5bcd60 100644 --- a/libs/sysjs/build.zig +++ b/libs/sysjs/build.zig @@ -8,7 +8,7 @@ pub fn build(b: *std.build.Builder) void { } pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { - const main_tests = b.addTestExe("sysjs-tests", thisDir() ++ "/src/main.zig"); + const main_tests = b.addTestExe("sysjs-tests", sdkPath("/src/main.zig")); main_tests.setBuildMode(mode); main_tests.setTarget(target); return main_tests.run(); @@ -16,10 +16,14 @@ pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.C pub const pkg = std.build.Pkg{ .name = "sysjs", - .source = .{ .path = thisDir() ++ "/src/main.zig" }, + .source = .{ .path = sdkPath("/src/main.zig") }, .dependencies = &[_]std.build.Pkg{}, }; -fn thisDir() []const u8 { - return std.fs.path.dirname(@src().file) orelse "."; +fn sdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; } diff --git a/tools/wasmserve/wasmserve.zig b/tools/wasmserve/wasmserve.zig index 0f236aeb..4715c727 100644 --- a/tools/wasmserve/wasmserve.zig +++ b/tools/wasmserve/wasmserve.zig @@ -5,7 +5,7 @@ const mem = std.mem; const fs = std.fs; const build = std.build; -const www_dir_path = thisDir() ++ "/www"; +const www_dir_path = sdkPath("/www"); const buffer_size = 2048; const esc = struct { pub const reset = "\x1b[0m"; @@ -334,6 +334,10 @@ fn logErr(err: anyerror, src: std.builtin.SourceLocation) void { } } -fn thisDir() []const u8 { - return fs.path.dirname(@src().file) orelse "."; +fn sdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; }