all: build: fix sdkPath for relative @src.file / fix autocompletion with ZLS / IDEs (#661)

* all: build: fix sdkPath for relative @src.file

Prior to this commit, the build system heavily assumed that the result
`@src.file` would always be absolute, but this is no longer
guaranteed, likely due to there being no such thing as an "absolute
path" in WASI.

It appears that for normal invocations of `zig build`, it is safe to
assume that `@src.file` is absolute. However, when ZLS uses a custom
`build_runner.zig` to collect build configuration, `@src.file` is
actually relative to the current working directory, at least on my
system. For a while, this led to ZLS completions breaking entirely,
but presently it actually causes ZLS to crash!

The solution is not as simple as using relative `sdkPath` results
as-is, because the build system may attempt to resolve these paths
relative to build root, when the paths are actually relative to the
current working directory.

This leads to a sticky situation: the current working directory is a
runtime concept, but `@src.file` is resolved at compile time. However,
it appears that the build runner does not change current working
directory in between compilation and execution, so it is probably safe
to calculate `sdkPath` using runtime current working directory.

Still, this requires major changes with how `sdkPath` works, since
runtime computation and allocations are required. So pretty much
anything that relied on `sdkPath` being comptime-known has been
refactored in this commit.

The most severe result of this is that, for example, `gpu.pkg` can no
longer be a comptime-known constant: it has to be a runtime function
that takes a `*Builder` and returns a `Pkg`.

This commit deals with usages of `*.pkg` and `sdkPath` within Mach
itself, but projects that depend on Mach such as `mach-examples` will
almost certainly require changes as well.

* all: update README to reflect change in pkg usage

For details on updating your code to use this version, see: 88b1106953

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
Co-authored-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Lue 2023-01-02 03:23:46 -05:00 committed by GitHub
parent f5d9b1ee57
commit a1fe671db8
Failed to generate hash of commit
17 changed files with 931 additions and 331 deletions

127
build.zig
View file

@ -27,11 +27,27 @@ const CrossTarget = std.zig.CrossTarget;
const Builder = std.build.Builder; const Builder = std.build.Builder;
const Pkg = std.build.Pkg; const Pkg = std.build.Pkg;
pub const pkg = Pkg{ var cached_pkg: ?Pkg = null;
.name = "mach",
.source = .{ .path = sdkPath("/src/main.zig") }, pub fn pkg(b: *Builder) Pkg {
.dependencies = &.{ gpu.pkg, ecs.pkg, sysaudio.pkg, earcut.pkg }, if (cached_pkg == null) {
}; const dependencies = b.allocator.create([4]Pkg) catch unreachable;
dependencies.* = .{
gpu.pkg(b),
ecs.pkg(b),
sysaudio.pkg(b),
earcut.pkg(b),
};
cached_pkg = .{
.name = "mach",
.source = .{ .path = sdkPath(b, "/src/main.zig") },
.dependencies = dependencies,
};
}
return cached_pkg.?;
}
pub const Options = struct { pub const Options = struct {
glfw_options: glfw.Options = .{}, glfw_options: glfw.Options = .{},
@ -124,11 +140,12 @@ fn testStep(b: *Builder, mode: std.builtin.Mode, target: CrossTarget) *std.build
const main_tests = b.addTestExe("mach-tests", "src/main.zig"); const main_tests = b.addTestExe("mach-tests", "src/main.zig");
main_tests.setBuildMode(mode); main_tests.setBuildMode(mode);
main_tests.setTarget(target); main_tests.setTarget(target);
for (pkg.dependencies.?) |dependency| {
for (pkg(b).dependencies.?) |dependency| {
main_tests.addPackage(dependency); main_tests.addPackage(dependency);
} }
main_tests.addPackage(freetype.pkg); main_tests.addPackage(freetype.pkg(b));
freetype.link(b, main_tests, .{}); freetype.link(b, main_tests, .{});
main_tests.install(); main_tests.install();
@ -146,11 +163,11 @@ fn buildSharedLib(b: *Builder, mode: std.builtin.Mode, target: CrossTarget, opti
.source = .{ .path = "src/platform/libmach.zig" }, .source = .{ .path = "src/platform/libmach.zig" },
}; };
lib.addPackage(app_pkg); lib.addPackage(app_pkg);
lib.addPackage(glfw.pkg); lib.addPackage(glfw.pkg(b));
lib.addPackage(gpu.pkg); lib.addPackage(gpu.pkg(b));
lib.addPackage(sysaudio.pkg); lib.addPackage(sysaudio.pkg(b));
if (target.isLinux()) { if (target.isLinux()) {
lib.addPackage(gamemode.pkg); lib.addPackage(gamemode.pkg(b));
gamemode.link(lib); gamemode.link(lib);
} }
try glfw.link(b, lib, options.glfw_options); try glfw.link(b, lib, options.glfw_options);
@ -208,14 +225,14 @@ pub const App = struct {
const platform = Platform.fromTarget(target); const platform = Platform.fromTarget(target);
var deps = std.ArrayList(Pkg).init(b.allocator); var deps = std.ArrayList(Pkg).init(b.allocator);
try deps.append(pkg); try deps.append(pkg(b));
try deps.append(gpu.pkg); try deps.append(gpu.pkg(b));
try deps.append(sysaudio.pkg); try deps.append(sysaudio.pkg(b));
switch (platform) { switch (platform) {
.native => try deps.append(glfw.pkg), .native => try deps.append(glfw.pkg(b)),
.web => try deps.append(sysjs.pkg), .web => try deps.append(sysjs.pkg(b)),
} }
if (options.use_freetype) |_| try deps.append(freetype.pkg); if (options.use_freetype) |_| try deps.append(freetype.pkg(b));
if (options.deps) |app_deps| try deps.appendSlice(app_deps); if (options.deps) |app_deps| try deps.appendSlice(app_deps);
const app_pkg = Pkg{ const app_pkg = Pkg{
@ -226,26 +243,26 @@ pub const App = struct {
const step = blk: { const step = blk: {
if (platform == .web) { if (platform == .web) {
const lib = b.addSharedLibrary(options.name, sdkPath("/src/platform/wasm.zig"), .unversioned); const lib = b.addSharedLibrary(options.name, sdkPath(b, "/src/platform/wasm.zig"), .unversioned);
lib.addPackage(gpu.pkg); lib.addPackage(gpu.pkg(b));
lib.addPackage(sysaudio.pkg); lib.addPackage(sysaudio.pkg(b));
lib.addPackage(sysjs.pkg); lib.addPackage(sysjs.pkg(b));
break :blk lib; break :blk lib;
} else { } else {
const exe = b.addExecutable(options.name, sdkPath("/src/platform/native.zig")); const exe = b.addExecutable(options.name, sdkPath(b, "/src/platform/native.zig"));
exe.addPackage(gpu.pkg); exe.addPackage(gpu.pkg(b));
exe.addPackage(sysaudio.pkg); exe.addPackage(sysaudio.pkg(b));
exe.addPackage(glfw.pkg); exe.addPackage(glfw.pkg(b));
if (target.os.tag == .linux) if (target.os.tag == .linux)
exe.addPackage(gamemode.pkg); exe.addPackage(gamemode.pkg(b));
break :blk exe; break :blk exe;
} }
}; };
step.main_pkg_path = sdkPath("/src"); step.main_pkg_path = sdkPath(b, "/src");
step.addPackage(app_pkg); step.addPackage(app_pkg);
step.setTarget(options.target); step.setTarget(options.target);
step.setBuildMode(options.mode); step.setBuildMode(options.mode);
@ -287,14 +304,14 @@ pub const App = struct {
inline for (.{ "/src/platform/mach.js", "/libs/sysjs/src/mach-sysjs.js" }) |js| { inline for (.{ "/src/platform/mach.js", "/libs/sysjs/src/mach-sysjs.js" }) |js| {
const install_js = app.b.addInstallFileWithDir( const install_js = app.b.addInstallFileWithDir(
.{ .path = sdkPath(js) }, .{ .path = sdkPath(app.b, js) },
web_install_dir, web_install_dir,
std.fs.path.basename(js), std.fs.path.basename(js),
); );
app.getInstallStep().?.step.dependOn(&install_js.step); app.getInstallStep().?.step.dependOn(&install_js.step);
} }
const html_generator = app.b.addExecutable("html-generator", sdkPath("/tools/html-generator/main.zig")); const html_generator = app.b.addExecutable("html-generator", sdkPath(app.b, "/tools/html-generator/main.zig"));
const run_html_generator = html_generator.run(); const run_html_generator = html_generator.run();
const html_file_name = std.mem.concat( const html_file_name = std.mem.concat(
app.b.allocator, app.b.allocator,
@ -346,10 +363,50 @@ pub const App = struct {
} }
}; };
fn sdkPath(comptime suffix: []const u8) []const u8 { const unresolved_dir = (struct {
if (suffix[0] != '/') @compileError("suffix must be an absolute path"); inline fn unresolvedDir() []const u8 {
return comptime blk: { return comptime std.fs.path.dirname(@src().file) orelse ".";
const root_dir = std.fs.path.dirname(@src().file) orelse "."; }
break :blk root_dir ++ suffix; }).unresolvedDir();
};
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir;
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
return cached_dir.*.?;
}
inline fn sdkPath(b: *Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
inline fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
return sdkPathInternal(allocator, suffix.len, suffix[0..suffix.len].*);
}
fn sdkPathInternal(allocator: std.mem.Allocator, comptime len: usize, comptime suffix: [len]u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir ++ @as([]const u8, &suffix);
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
}
return cached_dir.*.?;
} }

View file

@ -20,7 +20,7 @@ const foobar = @import("libs/mach-foobar/build.zig");
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
... ...
exe.addPackage(foobar.pkg); exe.addPackage(foobar.pkg(b));
foobar.link(b, exe, .{}); foobar.link(b, exe, .{});
} }
``` ```

View file

@ -1,14 +1,21 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
const basisu_root = sdkPath("/upstream/basisu"); const basisu_root = "/upstream/basisu";
pub const pkg = std.build.Pkg{ var cached_pkg: ?std.build.Pkg = null;
.name = "basisu",
.source = .{ pub fn pkg(b: *Builder) std.build.Pkg {
.path = "src/main.zig", if (cached_pkg == null) {
}, cached_pkg = .{
}; .name = "basisu",
.source = .{ .path = sdkPath(b, "/src/main.zig") },
.dependencies = &.{},
};
}
return cached_pkg.?;
}
pub const Options = struct { pub const Options = struct {
encoder: ?EncoderOptions, encoder: ?EncoderOptions,
@ -32,10 +39,10 @@ pub fn build(b: *Builder) void {
} }
pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep {
const main_tests = b.addTestExe("basisu-tests", sdkPath("/src/main.zig")); const main_tests = b.addTestExe("basisu-tests", sdkPath(b, "/src/main.zig"));
main_tests.setBuildMode(mode); main_tests.setBuildMode(mode);
main_tests.setTarget(target); main_tests.setTarget(target);
main_tests.main_pkg_path = sdkPath("/"); main_tests.main_pkg_path = sdkPath(b, "/");
link(b, main_tests, target, .{ link(b, main_tests, target, .{
.encoder = .{}, .encoder = .{},
.transcoder = .{}, .transcoder = .{},
@ -47,13 +54,13 @@ 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 { pub fn link(b: *Builder, step: *std.build.LibExeObjStep, target: std.zig.CrossTarget, options: Options) void {
if (options.encoder) |encoder_options| { if (options.encoder) |encoder_options| {
step.linkLibrary(buildEncoder(b, target, encoder_options)); step.linkLibrary(buildEncoder(b, target, encoder_options));
step.addCSourceFile(sdkPath("/src/encoder/wrapper.cpp"), &.{}); step.addCSourceFile(sdkPath(b, "/src/encoder/wrapper.cpp"), &.{});
step.addIncludePath(basisu_root ++ "/encoder"); step.addIncludePath(sdkPath(b, basisu_root ++ "/encoder"));
} }
if (options.transcoder) |transcoder_options| { if (options.transcoder) |transcoder_options| {
step.linkLibrary(buildTranscoder(b, target, transcoder_options)); step.linkLibrary(buildTranscoder(b, target, transcoder_options));
step.addCSourceFile(sdkPath("/src/transcoder/wrapper.cpp"), &.{}); step.addCSourceFile(sdkPath(b, "/src/transcoder/wrapper.cpp"), &.{});
step.addIncludePath(basisu_root ++ "/transcoder"); step.addIncludePath(sdkPath(b, basisu_root ++ "/transcoder"));
} }
} }
@ -64,10 +71,11 @@ pub fn buildEncoder(b: *Builder, target: std.zig.CrossTarget, options: EncoderOp
const encoder = b.addStaticLibrary("basisu-encoder", null); const encoder = b.addStaticLibrary("basisu-encoder", null);
encoder.setTarget(target); encoder.setTarget(target);
encoder.linkLibCpp(); encoder.linkLibCpp();
encoder.addCSourceFiles(
encoder_sources, inline for (encoder_sources) |encoder_source| {
&.{}, encoder.addCSourceFile(sdkPath(b, encoder_source), &.{});
); }
encoder.defineCMacro("BASISU_FORCE_DEVEL_MESSAGES", "0"); encoder.defineCMacro("BASISU_FORCE_DEVEL_MESSAGES", "0");
encoder.defineCMacro("BASISD_SUPPORT_KTX2_ZSTD", "0"); encoder.defineCMacro("BASISD_SUPPORT_KTX2_ZSTD", "0");
@ -83,10 +91,11 @@ pub fn buildTranscoder(b: *Builder, target: std.zig.CrossTarget, options: Transc
const transcoder = b.addStaticLibrary("basisu-transcoder", null); const transcoder = b.addStaticLibrary("basisu-transcoder", null);
transcoder.setTarget(target); transcoder.setTarget(target);
transcoder.linkLibCpp(); transcoder.linkLibCpp();
transcoder.addCSourceFiles(
transcoder_sources, inline for (transcoder_sources) |transcoder_source| {
&.{}, transcoder.addCSourceFile(sdkPath(b, transcoder_source), &.{});
); }
transcoder.defineCMacro("BASISU_FORCE_DEVEL_MESSAGES", "0"); transcoder.defineCMacro("BASISU_FORCE_DEVEL_MESSAGES", "0");
transcoder.defineCMacro("BASISD_SUPPORT_KTX2_ZSTD", "0"); transcoder.defineCMacro("BASISD_SUPPORT_KTX2_ZSTD", "0");
@ -101,19 +110,59 @@ fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !vo
if (std.mem.eql(u8, no_ensure_submodules, "true")) return; if (std.mem.eql(u8, no_ensure_submodules, "true")) return;
} else |_| {} } else |_| {}
var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator); var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator);
child.cwd = sdkPath("/"); child.cwd = sdkPathAllocator(allocator, "/");
child.stderr = std.io.getStdErr(); child.stderr = std.io.getStdErr();
child.stdout = std.io.getStdOut(); child.stdout = std.io.getStdOut();
_ = try child.spawnAndWait(); _ = try child.spawnAndWait();
} }
fn sdkPath(comptime suffix: []const u8) []const u8 { const unresolved_dir = (struct {
inline fn unresolvedDir() []const u8 {
return comptime std.fs.path.dirname(@src().file) orelse ".";
}
}).unresolvedDir();
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir;
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
return cached_dir.*.?;
}
inline fn sdkPath(b: *Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
inline fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
return sdkPathInternal(allocator, suffix.len, suffix[0..suffix.len].*);
}
fn sdkPathInternal(allocator: std.mem.Allocator, comptime len: usize, comptime suffix: [len]u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path"); if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse "."; if (comptime unresolved_dir[0] == '/') {
break :blk root_dir ++ suffix; return unresolved_dir ++ @as([]const u8, &suffix);
}; }
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
}
return cached_dir.*.?;
} }
const transcoder_sources = &[_][]const u8{ const transcoder_sources = &[_][]const u8{

View file

@ -32,7 +32,7 @@ const earcut = @import("libs/mach-earcut/build.zig");
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
... ...
exe.addPackage(earcut.pkg); exe.addPackage(earcut.pkg(b));
} }
``` ```

View file

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder;
pub fn build(b: *std.build.Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const mode = b.standardReleaseOptions();
const lib = b.addStaticLibrary("earcut", "src/main.zig"); const lib = b.addStaticLibrary("earcut", "src/main.zig");
lib.setBuildMode(mode); lib.setBuildMode(mode);
@ -12,14 +13,67 @@ pub fn build(b: *std.build.Builder) void {
const test_step = b.step("test", "Run library tests"); const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step); test_step.dependOn(&main_tests.step);
_ = pkg; _ = pkg(b);
} }
pub const pkg = std.build.Pkg{ var cached_pkg: ?std.build.Pkg = null;
.name = "earcut",
.source = .{ .path = thisDir() ++ "/src/main.zig" },
};
fn thisDir() []const u8 { pub fn pkg(b: *Builder) std.build.Pkg {
return std.fs.path.dirname(@src().file) orelse "."; if (cached_pkg == null) {
cached_pkg = .{
.name = "earcut",
.source = .{ .path = sdkPath(b, "/src/main.zig") },
.dependencies = &.{},
};
}
return cached_pkg.?;
}
const unresolved_dir = (struct {
inline fn unresolvedDir() []const u8 {
return comptime std.fs.path.dirname(@src().file) orelse ".";
}
}).unresolvedDir();
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir;
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
return cached_dir.*.?;
}
inline fn sdkPath(b: *Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
inline fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
return sdkPathInternal(allocator, suffix.len, suffix[0..suffix.len].*);
}
fn sdkPathInternal(allocator: std.mem.Allocator, comptime len: usize, comptime suffix: [len]u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir ++ @as([]const u8, &suffix);
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
}
return cached_dir.*.?;
} }

View file

@ -1,30 +1,79 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder;
pub const pkg = std.build.Pkg{ var cached_pkg: ?std.build.Pkg = null;
.name = "ecs",
.source = .{ .path = sdkPath("/src/main.zig") },
.dependencies = &[_]std.build.Pkg{},
};
pub fn build(b: *std.build.Builder) void { pub fn pkg(b: *Builder) std.build.Pkg {
if (cached_pkg == null) {
cached_pkg = .{
.name = "ecs",
.source = .{ .path = sdkPath(b, "/src/main.zig") },
.dependencies = &.{},
};
}
return cached_pkg.?;
}
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const mode = b.standardReleaseOptions();
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const test_step = b.step("test", "Run library tests"); const test_step = b.step("test", "Run library tests");
test_step.dependOn(&testStep(b, mode, target).step); test_step.dependOn(&testStep(b, mode, target).step);
} }
pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep {
const main_tests = b.addTestExe("ecs-tests", sdkPath("/src/main.zig")); const main_tests = b.addTestExe("ecs-tests", sdkPath(b, "/src/main.zig"));
main_tests.setBuildMode(mode); main_tests.setBuildMode(mode);
main_tests.setTarget(target); main_tests.setTarget(target);
main_tests.install(); main_tests.install();
return main_tests.run(); return main_tests.run();
} }
fn sdkPath(comptime suffix: []const u8) []const u8 { const unresolved_dir = (struct {
if (suffix[0] != '/') @compileError("suffix must be an absolute path"); inline fn unresolvedDir() []const u8 {
return comptime blk: { return comptime std.fs.path.dirname(@src().file) orelse ".";
const root_dir = std.fs.path.dirname(@src().file) orelse "."; }
break :blk root_dir ++ suffix; }).unresolvedDir();
};
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir;
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
return cached_dir.*.?;
}
inline fn sdkPath(b: *Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
inline fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
return sdkPathInternal(allocator, suffix.len, suffix[0..suffix.len].*);
}
fn sdkPathInternal(allocator: std.mem.Allocator, comptime len: usize, comptime suffix: [len]u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir ++ @as([]const u8, &suffix);
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
}
return cached_dir.*.?;
} }

View file

@ -28,7 +28,7 @@ const freetype = @import("libs/mach-freetype/build.zig");
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
... ...
exe.addPackage(freetype.pkg); exe.addPackage(freetype.pkg(b));
freetype.link(b, exe, .{}); freetype.link(b, exe, .{});
// use this option if you are including zlib separately // use this option if you are including zlib separately
@ -46,7 +46,7 @@ freetype.link(b, exe, .{ .harfbuzz = .{} });
You can also optionally build brotli compression (for WOFF2 font support): You can also optionally build brotli compression (for WOFF2 font support):
```zig ```zig
exe.addPackage(freetype.pkg); exe.addPackage(freetype.pkg(b));
freetype.link(b, exe, .{ .freetype = .{ .brotli = true } }); freetype.link(b, exe, .{ .freetype = .{ .brotli = true } });
``` ```

View file

@ -1,23 +1,43 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
const ft_root = sdkPath("/upstream/freetype"); const ft_root = "/upstream/freetype";
const ft_include_path = ft_root ++ "/include"; const ft_include_path = ft_root ++ "/include";
const hb_root = sdkPath("/upstream/harfbuzz"); const hb_root = "/upstream/harfbuzz";
const hb_include_path = hb_root ++ "/src"; const hb_include_path = hb_root ++ "/src";
const brotli_root = sdkPath("/upstream/brotli"); const brotli_root = "/upstream/brotli";
pub const pkg = std.build.Pkg{ var cached_pkg: ?std.build.Pkg = null;
.name = "freetype", var cached_harfbuzz_pkg: ?std.build.Pkg = null;
.source = .{ .path = sdkPath("/src/main.zig") },
.dependencies = &.{},
};
pub const harfbuzz_pkg = std.build.Pkg{ pub fn pkg(b: *Builder) std.build.Pkg {
.name = "harfbuzz", if (cached_pkg == null) {
.source = .{ .path = sdkPath("/src/harfbuzz/main.zig") }, cached_pkg = .{
.dependencies = &.{pkg}, .name = "freetype",
}; .source = .{ .path = sdkPath(b, "/src/main.zig") },
.dependencies = &.{},
};
}
return cached_pkg.?;
}
pub fn harfbuzz_pkg(b: *Builder) std.build.Pkg {
if (cached_harfbuzz_pkg == null) {
const dependencies = b.allocator.create([1]std.build.Pkg) catch unreachable;
dependencies.* = .{
pkg(b),
};
cached_harfbuzz_pkg = .{
.name = "harfbuzz",
.source = .{ .path = sdkPath(b, "/src/harfbuzz/main.zig") },
.dependencies = dependencies,
};
}
return cached_harfbuzz_pkg.?;
}
pub const Options = struct { pub const Options = struct {
freetype: FreetypeOptions = .{}, freetype: FreetypeOptions = .{},
@ -51,7 +71,7 @@ pub fn build(b: *std.build.Builder) !void {
const example_exe = b.addExecutable("example-" ++ example, "examples/" ++ example ++ ".zig"); const example_exe = b.addExecutable("example-" ++ example, "examples/" ++ example ++ ".zig");
example_exe.setBuildMode(mode); example_exe.setBuildMode(mode);
example_exe.setTarget(target); example_exe.setTarget(target);
example_exe.addPackage(pkg); example_exe.addPackage(pkg(b));
link(b, example_exe, .{}); link(b, example_exe, .{});
@ -71,30 +91,30 @@ pub fn build(b: *std.build.Builder) !void {
} }
pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep {
const main_tests = b.addTestExe("freetype-tests", sdkPath("/src/main.zig")); const main_tests = b.addTestExe("freetype-tests", sdkPath(b, "/src/main.zig"));
main_tests.setBuildMode(mode); main_tests.setBuildMode(mode);
main_tests.setTarget(target); main_tests.setTarget(target);
main_tests.addPackage(pkg); main_tests.addPackage(pkg(b));
link(b, main_tests, .{ link(b, main_tests, .{
.freetype = .{ .freetype = .{
.brotli = true, .brotli = true,
}, },
.harfbuzz = .{}, .harfbuzz = .{},
}); });
main_tests.main_pkg_path = sdkPath("/"); main_tests.main_pkg_path = sdkPath(b, "/");
main_tests.install(); main_tests.install();
const harfbuzz_tests = b.addTestExe("harfbuzz-tests", sdkPath("/src/harfbuzz/main.zig")); const harfbuzz_tests = b.addTestExe("harfbuzz-tests", sdkPath(b, "/src/harfbuzz/main.zig"));
harfbuzz_tests.setBuildMode(mode); harfbuzz_tests.setBuildMode(mode);
harfbuzz_tests.setTarget(target); harfbuzz_tests.setTarget(target);
harfbuzz_tests.addPackage(pkg); harfbuzz_tests.addPackage(pkg(b));
link(b, harfbuzz_tests, .{ link(b, harfbuzz_tests, .{
.freetype = .{ .freetype = .{
.brotli = true, .brotli = true,
}, },
.harfbuzz = .{}, .harfbuzz = .{},
}); });
harfbuzz_tests.main_pkg_path = sdkPath("/"); harfbuzz_tests.main_pkg_path = sdkPath(b, "/");
harfbuzz_tests.install(); harfbuzz_tests.install();
const main_tests_run = main_tests.run(); const main_tests_run = main_tests.run();
@ -111,7 +131,7 @@ pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void
pub fn linkFreetype(b: *Builder, step: *std.build.LibExeObjStep, options: FreetypeOptions) void { pub fn linkFreetype(b: *Builder, step: *std.build.LibExeObjStep, options: FreetypeOptions) void {
const ft_lib = buildFreetype(b, step.build_mode, step.target, options); const ft_lib = buildFreetype(b, step.build_mode, step.target, options);
step.linkLibrary(ft_lib); step.linkLibrary(ft_lib);
step.addIncludePath(ft_include_path); step.addIncludePath(sdkPath(b, ft_include_path));
if (options.brotli) { if (options.brotli) {
const brotli_lib = buildBrotli(b, step.build_mode, step.target); const brotli_lib = buildBrotli(b, step.build_mode, step.target);
if (options.install_libs) if (options.install_libs)
@ -123,7 +143,7 @@ pub fn linkFreetype(b: *Builder, step: *std.build.LibExeObjStep, options: Freety
pub fn linkHarfbuzz(b: *Builder, step: *std.build.LibExeObjStep, options: HarfbuzzOptions) void { pub fn linkHarfbuzz(b: *Builder, step: *std.build.LibExeObjStep, options: HarfbuzzOptions) void {
const hb_lib = buildHarfbuzz(b, step.build_mode, step.target, options); const hb_lib = buildHarfbuzz(b, step.build_mode, step.target, options);
step.linkLibrary(hb_lib); step.linkLibrary(hb_lib);
step.addIncludePath(hb_include_path); step.addIncludePath(sdkPath(b, hb_include_path));
} }
pub fn buildFreetype(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: FreetypeOptions) *std.build.LibExeObjStep { pub fn buildFreetype(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: FreetypeOptions) *std.build.LibExeObjStep {
@ -138,7 +158,7 @@ pub fn buildFreetype(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossT
lib.setBuildMode(mode); lib.setBuildMode(mode);
lib.setTarget(target); lib.setTarget(target);
lib.linkLibC(); lib.linkLibC();
lib.addIncludePath(ft_include_path); lib.addIncludePath(sdkPath(b, ft_include_path));
if (options.config_path) |path| if (options.config_path) |path|
lib.addIncludePath(path); lib.addIncludePath(path);
@ -149,20 +169,23 @@ pub fn buildFreetype(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossT
const target_info = (std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target; const target_info = (std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target;
if (target_info.os.tag == .windows) { if (target_info.os.tag == .windows) {
lib.addCSourceFile(ft_root ++ "/builds/windows/ftsystem.c", &.{}); lib.addCSourceFile(sdkPath(b, ft_root ++ "/builds/windows/ftsystem.c"), &.{});
lib.addCSourceFile(ft_root ++ "/builds/windows/ftdebug.c", &.{}); lib.addCSourceFile(sdkPath(b, ft_root ++ "/builds/windows/ftdebug.c"), &.{});
} else { } else {
lib.addCSourceFile(ft_root ++ "/src/base/ftsystem.c", &.{}); lib.addCSourceFile(sdkPath(b, ft_root ++ "/src/base/ftsystem.c"), &.{});
lib.addCSourceFile(ft_root ++ "/src/base/ftdebug.c", &.{}); lib.addCSourceFile(sdkPath(b, ft_root ++ "/src/base/ftdebug.c"), &.{});
} }
if (target_info.os.tag.isBSD() or target_info.os.tag == .linux) { if (target_info.os.tag.isBSD() or target_info.os.tag == .linux) {
lib.defineCMacro("HAVE_UNISTD_H", "1"); lib.defineCMacro("HAVE_UNISTD_H", "1");
lib.defineCMacro("HAVE_FCNTL_H", "1"); lib.defineCMacro("HAVE_FCNTL_H", "1");
lib.addCSourceFile(ft_root ++ "/builds/unix/ftsystem.c", &.{}); lib.addCSourceFile(sdkPath(b, ft_root ++ "/builds/unix/ftsystem.c"), &.{});
if (target_info.os.tag == .macos) if (target_info.os.tag == .macos)
lib.addCSourceFile(ft_root ++ "/src/base/ftmac.c", &.{}); lib.addCSourceFile(sdkPath(b, ft_root ++ "/src/base/ftmac.c"), &.{});
}
inline for (freetype_base_sources) |ft_source| {
lib.addCSourceFile(sdkPath(b, ft_source), &.{});
} }
lib.addCSourceFiles(freetype_base_sources, &.{});
if (options.install_libs) if (options.install_libs)
lib.install(); lib.install();
@ -170,13 +193,13 @@ pub fn buildFreetype(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossT
} }
pub fn buildHarfbuzz(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: HarfbuzzOptions) *std.build.LibExeObjStep { pub fn buildHarfbuzz(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: HarfbuzzOptions) *std.build.LibExeObjStep {
const main_abs = hb_root ++ "/src/harfbuzz.cc"; const main_abs = sdkPath(b, hb_root ++ "/src/harfbuzz.cc");
const lib = b.addStaticLibrary("harfbuzz", main_abs); const lib = b.addStaticLibrary("harfbuzz", main_abs);
lib.setBuildMode(mode); lib.setBuildMode(mode);
lib.setTarget(target); lib.setTarget(target);
lib.linkLibCpp(); lib.linkLibCpp();
lib.addIncludePath(hb_include_path); lib.addIncludePath(sdkPath(b, hb_include_path));
lib.addIncludePath(ft_include_path); lib.addIncludePath(sdkPath(b, ft_include_path));
lib.defineCMacro("HAVE_FREETYPE", "1"); lib.defineCMacro("HAVE_FREETYPE", "1");
if (options.install_libs) if (options.install_libs)
@ -189,8 +212,12 @@ fn buildBrotli(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget)
lib.setBuildMode(mode); lib.setBuildMode(mode);
lib.setTarget(target); lib.setTarget(target);
lib.linkLibC(); lib.linkLibC();
lib.addIncludePath(brotli_root ++ "/include"); lib.addIncludePath(sdkPath(b, brotli_root ++ "/include"));
lib.addCSourceFiles(brotli_base_sources, &.{});
inline for (brotli_base_sources) |brotli_source| {
lib.addCSourceFile(sdkPath(b, brotli_source), &.{});
}
return lib; return lib;
} }
@ -200,19 +227,59 @@ fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !vo
if (std.mem.eql(u8, no_ensure_submodules, "true")) return; if (std.mem.eql(u8, no_ensure_submodules, "true")) return;
} else |_| {} } else |_| {}
var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator); var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator);
child.cwd = sdkPath("/"); child.cwd = sdkPathAllocator(allocator, "/");
child.stderr = std.io.getStdErr(); child.stderr = std.io.getStdErr();
child.stdout = std.io.getStdOut(); child.stdout = std.io.getStdOut();
_ = try child.spawnAndWait(); _ = try child.spawnAndWait();
} }
fn sdkPath(comptime suffix: []const u8) []const u8 { const unresolved_dir = (struct {
inline fn unresolvedDir() []const u8 {
return comptime std.fs.path.dirname(@src().file) orelse ".";
}
}).unresolvedDir();
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir;
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
return cached_dir.*.?;
}
inline fn sdkPath(b: *Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
inline fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
return sdkPathInternal(allocator, suffix.len, suffix[0..suffix.len].*);
}
fn sdkPathInternal(allocator: std.mem.Allocator, comptime len: usize, comptime suffix: [len]u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path"); if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse "."; if (comptime unresolved_dir[0] == '/') {
break :blk root_dir ++ suffix; return unresolved_dir ++ @as([]const u8, &suffix);
}; }
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
}
return cached_dir.*.?;
} }
const freetype_base_sources = &[_][]const u8{ const freetype_base_sources = &[_][]const u8{

View file

@ -1,18 +1,70 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder;
pub const pkg = std.build.Pkg{ pub fn build(_: *Builder) void {}
.name = "gamemode",
.source = .{ .path = sdkPath("/gamemode.zig") }, var cached_pkg: ?std.build.Pkg = null;
};
pub fn pkg(b: *Builder) std.build.Pkg {
if (cached_pkg == null) {
cached_pkg = .{
.name = "gamemode",
.source = .{ .path = sdkPath(b, "/src/main.zig") },
.dependencies = &.{},
};
}
return cached_pkg.?;
}
pub fn link(step: *std.build.LibExeObjStep) void { pub fn link(step: *std.build.LibExeObjStep) void {
step.addIncludePath(sdkPath("/upstream/include")); step.addIncludePath(sdkPath(step.builder, "/upstream/include"));
} }
fn sdkPath(comptime suffix: []const u8) []const u8 { const unresolved_dir = (struct {
if (suffix[0] != '/') @compileError("suffix must be an absolute path"); inline fn unresolvedDir() []const u8 {
return comptime blk: { return comptime std.fs.path.dirname(@src().file) orelse ".";
const root_dir = std.fs.path.dirname(@src().file) orelse "."; }
break :blk root_dir ++ suffix; }).unresolvedDir();
};
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir;
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
return cached_dir.*.?;
}
inline fn sdkPath(b: *Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
inline fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
return sdkPathInternal(allocator, suffix.len, suffix[0..suffix.len].*);
}
fn sdkPathInternal(allocator: std.mem.Allocator, comptime len: usize, comptime suffix: [len]u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir ++ @as([]const u8, &suffix);
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
}
return cached_dir.*.?;
} }

View file

@ -59,7 +59,7 @@ const glfw = @import("libs/mach-glfw/build.zig");
pub fn build(b: *Builder) !void { pub fn build(b: *Builder) !void {
... ...
exe.addPackage(glfw.pkg); exe.addPackage(glfw.pkg(b));
try glfw.link(b, exe, .{}); try glfw.link(b, exe, .{});
} }
``` ```

View file

@ -14,7 +14,7 @@ pub fn build(b: *Builder) !void {
} }
pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) !*std.build.RunStep { pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) !*std.build.RunStep {
const main_tests = b.addTestExe("glfw-tests", sdkPath("/src/main.zig")); const main_tests = b.addTestExe("glfw-tests", sdkPath(b, "/src/main.zig"));
main_tests.setBuildMode(mode); main_tests.setBuildMode(mode);
main_tests.setTarget(target); main_tests.setTarget(target);
try link(b, main_tests, .{}); 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 { fn testStepShared(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) !*std.build.RunStep {
const main_tests = b.addTestExe("glfw-tests-shared", sdkPath("/src/main.zig")); const main_tests = b.addTestExe("glfw-tests-shared", sdkPath(b, "/src/main.zig"));
main_tests.setBuildMode(mode); main_tests.setBuildMode(mode);
main_tests.setTarget(target); main_tests.setTarget(target);
try link(b, main_tests, .{ .shared = true }); try link(b, main_tests, .{ .shared = true });
@ -64,10 +64,19 @@ pub const Options = struct {
install_libs: bool = false, install_libs: bool = false,
}; };
pub const pkg = std.build.Pkg{ var cached_pkg: ?std.build.Pkg = null;
.name = "glfw",
.source = .{ .path = sdkPath("/src/main.zig") }, pub fn pkg(b: *Builder) std.build.Pkg {
}; if (cached_pkg == null) {
cached_pkg = .{
.name = "glfw",
.source = .{ .path = sdkPath(b, "/src/main.zig") },
.dependencies = &.{},
};
}
return cached_pkg.?;
}
pub const LinkError = error{FailedToLinkGPU} || BuildError; pub const LinkError = error{FailedToLinkGPU} || BuildError;
pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) LinkError!void { pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) LinkError!void {
@ -108,21 +117,21 @@ fn buildLibrary(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget
} }
fn addGLFWIncludes(step: *std.build.LibExeObjStep) void { fn addGLFWIncludes(step: *std.build.LibExeObjStep) void {
step.addIncludePath(sdkPath("/upstream/glfw/include")); step.addIncludePath(sdkPath(step.builder, "/upstream/glfw/include"));
step.addIncludePath(sdkPath("/upstream/vulkan_headers/include")); step.addIncludePath(sdkPath(step.builder, "/upstream/vulkan_headers/include"));
} }
fn addGLFWSources(b: *Builder, lib: *std.build.LibExeObjStep, options: Options) std.mem.Allocator.Error!void { fn addGLFWSources(b: *Builder, lib: *std.build.LibExeObjStep, options: Options) std.mem.Allocator.Error!void {
const include_glfw_src = comptime "-I" ++ sdkPath("/upstream/glfw/src"); const include_glfw_src = try std.mem.concat(b.allocator, u8, &.{ "-I", sdkPath(b, "/upstream/glfw/src") });
switch (lib.target_info.target.os.tag) { switch (lib.target_info.target.os.tag) {
.windows => lib.addCSourceFiles(&.{ .windows => lib.addCSourceFiles(&.{
sdkPath("/src/sources_all.c"), sdkPath(b, "/src/sources_all.c"),
sdkPath("/src/sources_windows.c"), sdkPath(b, "/src/sources_windows.c"),
}, &.{ "-D_GLFW_WIN32", include_glfw_src }), }, &.{ "-D_GLFW_WIN32", include_glfw_src }),
.macos => lib.addCSourceFiles(&.{ .macos => lib.addCSourceFiles(&.{
sdkPath("/src/sources_all.c"), sdkPath(b, "/src/sources_all.c"),
sdkPath("/src/sources_macos.m"), sdkPath(b, "/src/sources_macos.m"),
sdkPath("/src/sources_macos.c"), sdkPath(b, "/src/sources_macos.c"),
}, &.{ "-D_GLFW_COCOA", include_glfw_src }), }, &.{ "-D_GLFW_COCOA", include_glfw_src }),
else => { else => {
// TODO(future): for now, Linux can't be built with musl: // TODO(future): for now, Linux can't be built with musl:
@ -133,17 +142,17 @@ fn addGLFWSources(b: *Builder, lib: *std.build.LibExeObjStep, options: Options)
// ``` // ```
var sources = std.ArrayList([]const u8).init(b.allocator); var sources = std.ArrayList([]const u8).init(b.allocator);
var flags = std.ArrayList([]const u8).init(b.allocator); var flags = std.ArrayList([]const u8).init(b.allocator);
try sources.append(sdkPath("/src/sources_all.c")); try sources.append(sdkPath(b, "/src/sources_all.c"));
try sources.append(sdkPath("/src/sources_linux.c")); try sources.append(sdkPath(b, "/src/sources_linux.c"));
if (options.x11) { if (options.x11) {
try sources.append(sdkPath("/src/sources_linux_x11.c")); try sources.append(sdkPath(b, "/src/sources_linux_x11.c"));
try flags.append("-D_GLFW_X11"); try flags.append("-D_GLFW_X11");
} }
if (options.wayland) { if (options.wayland) {
try sources.append(sdkPath("/src/sources_linux_wayland.c")); try sources.append(sdkPath(b, "/src/sources_linux_wayland.c"));
try flags.append("-D_GLFW_WAYLAND"); try flags.append("-D_GLFW_WAYLAND");
} }
try flags.append(comptime "-I" ++ sdkPath("/upstream/glfw/src")); try flags.append(include_glfw_src);
// TODO(upstream): glfw can't compile on clang15 without this flag // TODO(upstream): glfw can't compile on clang15 without this flag
try flags.append("-Wno-implicit-function-declaration"); try flags.append("-Wno-implicit-function-declaration");
@ -197,17 +206,57 @@ fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !vo
if (std.mem.eql(u8, no_ensure_submodules, "true")) return; if (std.mem.eql(u8, no_ensure_submodules, "true")) return;
} else |_| {} } else |_| {}
var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator); var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator);
child.cwd = sdkPath("/"); child.cwd = sdkPathAllocator(allocator, "/");
child.stderr = std.io.getStdErr(); child.stderr = std.io.getStdErr();
child.stdout = std.io.getStdOut(); child.stdout = std.io.getStdOut();
_ = try child.spawnAndWait(); _ = try child.spawnAndWait();
} }
fn sdkPath(comptime suffix: []const u8) []const u8 { const unresolved_dir = (struct {
if (suffix[0] != '/') @compileError("suffix must be an absolute path"); inline fn unresolvedDir() []const u8 {
return comptime blk: { return comptime std.fs.path.dirname(@src().file) orelse ".";
const root_dir = std.fs.path.dirname(@src().file) orelse "."; }
break :blk root_dir ++ suffix; }).unresolvedDir();
};
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir;
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
return cached_dir.*.?;
}
inline fn sdkPath(b: *Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
inline fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
return sdkPathInternal(allocator, suffix.len, suffix[0..suffix.len].*);
}
fn sdkPathInternal(allocator: std.mem.Allocator, comptime len: usize, comptime suffix: [len]u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir ++ @as([]const u8, &suffix);
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
}
return cached_dir.*.?;
} }

View file

@ -90,14 +90,14 @@ pub fn Sdk(comptime deps: anytype) type {
fn linkFromSource(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !void { fn linkFromSource(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !void {
// branch: generated-2022-08-06 // branch: generated-2022-08-06
try ensureGitRepoCloned(b.allocator, "https://github.com/hexops/dawn", "0b704c4acae154ec8d4be7615d18a489f270f6c0", sdkPath("/libs/dawn")); try ensureGitRepoCloned(b.allocator, "https://github.com/hexops/dawn", "0b704c4acae154ec8d4be7615d18a489f270f6c0", sdkPath(b, "/libs/dawn"));
// branch: mach // branch: mach
try ensureGitRepoCloned(b.allocator, "https://github.com/hexops/DirectXShaderCompiler", "cff9a6f0b7f961748b822e1d313a7205dfdecf9d", sdkPath("/libs/DirectXShaderCompiler")); try ensureGitRepoCloned(b.allocator, "https://github.com/hexops/DirectXShaderCompiler", "cff9a6f0b7f961748b822e1d313a7205dfdecf9d", sdkPath(b, "/libs/DirectXShaderCompiler"));
step.addIncludePath(sdkPath("/libs/dawn/out/Debug/gen/include")); step.addIncludePath(sdkPath(b, "/libs/dawn/out/Debug/gen/include"));
step.addIncludePath(sdkPath("/libs/dawn/include")); step.addIncludePath(sdkPath(b, "/libs/dawn/include"));
step.addIncludePath(sdkPath("/src/dawn")); step.addIncludePath(sdkPath(b, "/src/dawn"));
if (options.separate_libs) { if (options.separate_libs) {
const lib_mach_dawn_native = try buildLibMachDawnNative(b, step, options); const lib_mach_dawn_native = try buildLibMachDawnNative(b, step, options);
@ -176,7 +176,7 @@ pub fn Sdk(comptime deps: anytype) type {
error.FileNotFound => { error.FileNotFound => {
std.log.info("cloning required dependency..\ngit clone {s} {s}..\n", .{ clone_url, dir }); std.log.info("cloning required dependency..\ngit clone {s} {s}..\n", .{ clone_url, dir });
try exec(allocator, &[_][]const u8{ "git", "clone", "-c", "core.longpaths=true", clone_url, dir }, sdkPath("/")); try exec(allocator, &[_][]const u8{ "git", "clone", "-c", "core.longpaths=true", clone_url, dir }, sdkPathAllocator(allocator, "/"));
try exec(allocator, &[_][]const u8{ "git", "reset", "--quiet", "--hard", revision }, dir); try exec(allocator, &[_][]const u8{ "git", "reset", "--quiet", "--hard", revision }, dir);
try exec(allocator, &[_][]const u8{ "git", "submodule", "update", "--init", "--recursive" }, dir); try exec(allocator, &[_][]const u8{ "git", "submodule", "update", "--init", "--recursive" }, dir);
return; return;
@ -297,7 +297,7 @@ pub fn Sdk(comptime deps: anytype) type {
step.linkLibCpp(); step.linkLibCpp();
step.addIncludePath(include_dir); step.addIncludePath(include_dir);
step.addIncludePath(sdkPath("/src/dawn")); step.addIncludePath(sdkPath(b, "/src/dawn"));
if (options.linux_window_manager != null and options.linux_window_manager.? == .X11) { if (options.linux_window_manager != null and options.linux_window_manager.? == .X11) {
step.linkSystemLibraryName("X11"); step.linkSystemLibraryName("X11");
@ -494,7 +494,7 @@ pub fn Sdk(comptime deps: anytype) type {
const result = try std.ChildProcess.exec(.{ const result = try std.ChildProcess.exec(.{
.allocator = allocator, .allocator = allocator,
.argv = &.{ "git", "branch", branch, "--contains", commit }, .argv = &.{ "git", "branch", branch, "--contains", commit },
.cwd = sdkPath("/"), .cwd = sdkPathAllocator(allocator, "/"),
}); });
defer { defer {
allocator.free(result.stdout); allocator.free(result.stdout);
@ -507,7 +507,7 @@ pub fn Sdk(comptime deps: anytype) type {
const result = try std.ChildProcess.exec(.{ const result = try std.ChildProcess.exec(.{
.allocator = allocator, .allocator = allocator,
.argv = &.{ "git", "rev-parse", "HEAD" }, .argv = &.{ "git", "rev-parse", "HEAD" },
.cwd = sdkPath("/"), .cwd = sdkPathAllocator(allocator, "/"),
}); });
defer allocator.free(result.stderr); defer allocator.free(result.stderr);
if (result.stdout.len > 0) return result.stdout[0 .. result.stdout.len - 1]; // trim newline if (result.stdout.len > 0) return result.stdout[0 .. result.stdout.len - 1]; // trim newline
@ -518,7 +518,7 @@ pub fn Sdk(comptime deps: anytype) type {
const result = try std.ChildProcess.exec(.{ const result = try std.ChildProcess.exec(.{
.allocator = allocator, .allocator = allocator,
.argv = &.{ "git", "clone", repository, dir }, .argv = &.{ "git", "clone", repository, dir },
.cwd = sdkPath("/"), .cwd = sdkPathAllocator(allocator, "/"),
}); });
defer { defer {
allocator.free(result.stdout); allocator.free(result.stdout);
@ -536,7 +536,7 @@ pub fn Sdk(comptime deps: anytype) type {
std.ChildProcess.init(&.{ "curl", "--insecure", "-L", "-o", target_file, url }, allocator) std.ChildProcess.init(&.{ "curl", "--insecure", "-L", "-o", target_file, url }, allocator)
else else
std.ChildProcess.init(&.{ "curl", "-L", "-o", target_file, url }, allocator); std.ChildProcess.init(&.{ "curl", "-L", "-o", target_file, url }, allocator);
child.cwd = sdkPath("/"); child.cwd = sdkPathAllocator(allocator, "/");
child.stderr = std.io.getStdErr(); child.stderr = std.io.getStdErr();
child.stdout = std.io.getStdOut(); child.stdout = std.io.getStdOut();
_ = try child.spawnAndWait(); _ = try child.spawnAndWait();
@ -546,7 +546,7 @@ pub fn Sdk(comptime deps: anytype) type {
const result = std.ChildProcess.exec(.{ const result = std.ChildProcess.exec(.{
.allocator = allocator, .allocator = allocator,
.argv = &.{ "curl", "--version" }, .argv = &.{ "curl", "--version" },
.cwd = sdkPath("/"), .cwd = sdkPathAllocator(allocator, "/"),
}) catch { // e.g. FileNotFound }) catch { // e.g. FileNotFound
std.log.err("mach: error: 'curl --version' failed. Is curl not installed?", .{}); std.log.err("mach: error: 'curl --version' failed. Is curl not installed?", .{});
std.process.exit(1); std.process.exit(1);
@ -586,11 +586,11 @@ pub fn Sdk(comptime deps: anytype) type {
try options.appendFlags(&cpp_flags, false, true); try options.appendFlags(&cpp_flags, false, true);
try appendDawnEnableBackendTypeFlags(&cpp_flags, options); try appendDawnEnableBackendTypeFlags(&cpp_flags, options);
try cpp_flags.appendSlice(&.{ try cpp_flags.appendSlice(&.{
include(deps.glfw_include_dir), include(b, deps.glfw_include_dir),
include("libs/dawn/out/Debug/gen/include"), include(b, "libs/dawn/out/Debug/gen/include"),
include("libs/dawn/out/Debug/gen/src"), include(b, "libs/dawn/out/Debug/gen/src"),
include("libs/dawn/include"), include(b, "libs/dawn/include"),
include("libs/dawn/src"), include(b, "libs/dawn/src"),
}); });
if (step.target_info.target.os.tag == .windows) { if (step.target_info.target.os.tag == .windows) {
try cpp_flags.appendSlice(&.{ try cpp_flags.appendSlice(&.{
@ -618,9 +618,9 @@ pub fn Sdk(comptime deps: anytype) type {
var flags = std.ArrayList([]const u8).init(b.allocator); var flags = std.ArrayList([]const u8).init(b.allocator);
try flags.appendSlice(&.{ try flags.appendSlice(&.{
include("libs/dawn/src"), include(b, "libs/dawn/src"),
include("libs/dawn/out/Debug/gen/include"), include(b, "libs/dawn/out/Debug/gen/include"),
include("libs/dawn/out/Debug/gen/src"), include(b, "libs/dawn/out/Debug/gen/src"),
}); });
try appendLangScannedSources(b, lib, options, .{ try appendLangScannedSources(b, lib, options, .{
.rel_dirs = &.{ .rel_dirs = &.{
@ -641,11 +641,11 @@ pub fn Sdk(comptime deps: anytype) type {
// TODO(build-system): pass system SDK options through // TODO(build-system): pass system SDK options through
deps.system_sdk.include(b, lib, .{}); deps.system_sdk.include(b, lib, .{});
lib.linkFramework("Foundation"); lib.linkFramework("Foundation");
const abs_path = sdkPath("/libs/dawn/src/dawn/common/SystemUtils_mac.mm"); const abs_path = sdkPath(b, "/libs/dawn/src/dawn/common/SystemUtils_mac.mm");
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
if (step.target_info.target.os.tag == .windows) { if (step.target_info.target.os.tag == .windows) {
const abs_path = sdkPath("/libs/dawn/src/dawn/common/WindowsUtils.cpp"); const abs_path = sdkPath(b, "/libs/dawn/src/dawn/common/WindowsUtils.cpp");
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
@ -673,10 +673,10 @@ pub fn Sdk(comptime deps: anytype) type {
var cpp_flags = std.ArrayList([]const u8).init(b.allocator); var cpp_flags = std.ArrayList([]const u8).init(b.allocator);
try options.appendFlags(&cpp_flags, false, true); try options.appendFlags(&cpp_flags, false, true);
try cpp_flags.appendSlice(&.{ try cpp_flags.appendSlice(&.{
include("libs/dawn/src"), include(b, "libs/dawn/src"),
include("libs/dawn/include"), include(b, "libs/dawn/include"),
include("libs/dawn/out/Debug/gen/include"), include(b, "libs/dawn/out/Debug/gen/include"),
}); });
var cpp_sources = std.ArrayList([]const u8).init(b.allocator); var cpp_sources = std.ArrayList([]const u8).init(b.allocator);
@ -685,7 +685,7 @@ pub fn Sdk(comptime deps: anytype) type {
"src/dawn/platform/WorkerThread.cpp", "src/dawn/platform/WorkerThread.cpp",
"src/dawn/platform/tracing/EventTracer.cpp", "src/dawn/platform/tracing/EventTracer.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
@ -747,12 +747,12 @@ pub fn Sdk(comptime deps: anytype) type {
var flags = std.ArrayList([]const u8).init(b.allocator); var flags = std.ArrayList([]const u8).init(b.allocator);
try appendDawnEnableBackendTypeFlags(&flags, options); try appendDawnEnableBackendTypeFlags(&flags, options);
try flags.appendSlice(&.{ try flags.appendSlice(&.{
include("libs/dawn"), include(b, "libs/dawn"),
include("libs/dawn/src"), include(b, "libs/dawn/src"),
include("libs/dawn/include"), include(b, "libs/dawn/include"),
include("libs/dawn/third_party/vulkan-deps/spirv-tools/src/include"), include(b, "libs/dawn/third_party/vulkan-deps/spirv-tools/src/include"),
include("libs/dawn/third_party/abseil-cpp"), include(b, "libs/dawn/third_party/abseil-cpp"),
include("libs/dawn/third_party/khronos"), include(b, "libs/dawn/third_party/khronos"),
// TODO(build-system): make these optional // TODO(build-system): make these optional
"-DTINT_BUILD_SPV_READER=1", "-DTINT_BUILD_SPV_READER=1",
@ -763,12 +763,12 @@ pub fn Sdk(comptime deps: anytype) type {
"-DTINT_BUILD_HLSL_WRITER=1", "-DTINT_BUILD_HLSL_WRITER=1",
"-DTINT_BUILD_GLSL_WRITER=1", "-DTINT_BUILD_GLSL_WRITER=1",
include("libs/dawn/"), include(b, "libs/dawn/"),
include("libs/dawn/include/tint"), include(b, "libs/dawn/include/tint"),
include("libs/dawn/third_party/vulkan-deps/vulkan-tools/src/"), include(b, "libs/dawn/third_party/vulkan-deps/vulkan-tools/src/"),
include("libs/dawn/out/Debug/gen/include"), include(b, "libs/dawn/out/Debug/gen/include"),
include("libs/dawn/out/Debug/gen/src"), include(b, "libs/dawn/out/Debug/gen/src"),
}); });
if (options.d3d12.?) try flags.appendSlice(dawn_d3d12_flags); if (options.d3d12.?) try flags.appendSlice(dawn_d3d12_flags);
@ -811,7 +811,7 @@ pub fn Sdk(comptime deps: anytype) type {
inline for ([_][]const u8{ inline for ([_][]const u8{
"src/dawn/mingw_helpers.cpp", "src/dawn/mingw_helpers.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/" ++ path); const abs_path = sdkPath(b, "/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
@ -847,7 +847,7 @@ pub fn Sdk(comptime deps: anytype) type {
inline for ([_][]const u8{ inline for ([_][]const u8{
"src/dawn/native/XlibXcbFunctions.cpp", "src/dawn/native/XlibXcbFunctions.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
} }
@ -855,7 +855,7 @@ pub fn Sdk(comptime deps: anytype) type {
inline for ([_][]const u8{ inline for ([_][]const u8{
"src/dawn/native/null/DeviceNull.cpp", "src/dawn/native/null/DeviceNull.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
@ -863,7 +863,7 @@ pub fn Sdk(comptime deps: anytype) type {
inline for ([_][]const u8{ inline for ([_][]const u8{
"src/dawn/native/SpirvValidation.cpp", "src/dawn/native/SpirvValidation.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
} }
@ -893,7 +893,7 @@ pub fn Sdk(comptime deps: anytype) type {
"src/dawn/native/vulkan/external_memory/MemoryServiceOpaqueFD.cpp", "src/dawn/native/vulkan/external_memory/MemoryServiceOpaqueFD.cpp",
"src/dawn/native/vulkan/external_semaphore/SemaphoreServiceFD.cpp", "src/dawn/native/vulkan/external_semaphore/SemaphoreServiceFD.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
} else if (step.target_info.target.os.tag == .fuchsia) { } else if (step.target_info.target.os.tag == .fuchsia) {
@ -901,7 +901,7 @@ pub fn Sdk(comptime deps: anytype) type {
"src/dawn/native/vulkan/external_memory/MemoryServiceZirconHandle.cpp", "src/dawn/native/vulkan/external_memory/MemoryServiceZirconHandle.cpp",
"src/dawn/native/vulkan/external_semaphore/SemaphoreServiceZirconHandle.cpp", "src/dawn/native/vulkan/external_semaphore/SemaphoreServiceZirconHandle.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
} else { } else {
@ -909,7 +909,7 @@ pub fn Sdk(comptime deps: anytype) type {
"src/dawn/native/vulkan/external_memory/MemoryServiceNull.cpp", "src/dawn/native/vulkan/external_memory/MemoryServiceNull.cpp",
"src/dawn/native/vulkan/external_semaphore/SemaphoreServiceNull.cpp", "src/dawn/native/vulkan/external_semaphore/SemaphoreServiceNull.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
} }
@ -957,7 +957,7 @@ pub fn Sdk(comptime deps: anytype) type {
inline for ([_][]const u8{ inline for ([_][]const u8{
"src/dawn/native/null/NullBackend.cpp", "src/dawn/native/null/NullBackend.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
@ -965,7 +965,7 @@ pub fn Sdk(comptime deps: anytype) type {
inline for ([_][]const u8{ inline for ([_][]const u8{
"src/dawn/native/d3d12/D3D12Backend.cpp", "src/dawn/native/d3d12/D3D12Backend.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
} }
@ -973,7 +973,7 @@ pub fn Sdk(comptime deps: anytype) type {
inline for ([_][]const u8{ inline for ([_][]const u8{
"src/dawn/native/opengl/OpenGLBackend.cpp", "src/dawn/native/opengl/OpenGLBackend.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
} }
@ -981,7 +981,7 @@ pub fn Sdk(comptime deps: anytype) type {
inline for ([_][]const u8{ inline for ([_][]const u8{
"src/dawn/native/vulkan/VulkanBackend.cpp", "src/dawn/native/vulkan/VulkanBackend.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
// TODO(build-system): vulkan // TODO(build-system): vulkan
@ -1027,17 +1027,17 @@ pub fn Sdk(comptime deps: anytype) type {
"-DTINT_BUILD_HLSL_WRITER=1", "-DTINT_BUILD_HLSL_WRITER=1",
"-DTINT_BUILD_GLSL_WRITER=1", "-DTINT_BUILD_GLSL_WRITER=1",
include("libs/dawn/"), include(b, "libs/dawn/"),
include("libs/dawn/include/tint"), include(b, "libs/dawn/include/tint"),
// Required for TINT_BUILD_SPV_READER=1 and TINT_BUILD_SPV_WRITER=1, if specified // Required for TINT_BUILD_SPV_READER=1 and TINT_BUILD_SPV_WRITER=1, if specified
include("libs/dawn/third_party/vulkan-deps"), include(b, "libs/dawn/third_party/vulkan-deps"),
include("libs/dawn/third_party/vulkan-deps/spirv-tools/src"), include(b, "libs/dawn/third_party/vulkan-deps/spirv-tools/src"),
include("libs/dawn/third_party/vulkan-deps/spirv-tools/src/include"), include(b, "libs/dawn/third_party/vulkan-deps/spirv-tools/src/include"),
include("libs/dawn/third_party/vulkan-deps/spirv-headers/src/include"), include(b, "libs/dawn/third_party/vulkan-deps/spirv-headers/src/include"),
include("libs/dawn/out/Debug/gen/third_party/vulkan-deps/spirv-tools/src"), include(b, "libs/dawn/out/Debug/gen/third_party/vulkan-deps/spirv-tools/src"),
include("libs/dawn/out/Debug/gen/third_party/vulkan-deps/spirv-tools/src/include"), include(b, "libs/dawn/out/Debug/gen/third_party/vulkan-deps/spirv-tools/src/include"),
include("libs/dawn/include"), include(b, "libs/dawn/include"),
}); });
// libtint_core_all_src // libtint_core_all_src
@ -1062,9 +1062,9 @@ pub fn Sdk(comptime deps: anytype) type {
var cpp_sources = std.ArrayList([]const u8).init(b.allocator); var cpp_sources = std.ArrayList([]const u8).init(b.allocator);
switch (step.target_info.target.os.tag) { switch (step.target_info.target.os.tag) {
.windows => try cpp_sources.append(sdkPath("/libs/dawn/src/tint/diagnostic/printer_windows.cc")), .windows => try cpp_sources.append(sdkPath(b, "/libs/dawn/src/tint/diagnostic/printer_windows.cc")),
.linux => try cpp_sources.append(sdkPath("/libs/dawn/src/tint/diagnostic/printer_linux.cc")), .linux => try cpp_sources.append(sdkPath(b, "/libs/dawn/src/tint/diagnostic/printer_linux.cc")),
else => try cpp_sources.append(sdkPath("/libs/dawn/src/tint/diagnostic/printer_other.cc")), else => try cpp_sources.append(sdkPath(b, "/libs/dawn/src/tint/diagnostic/printer_other.cc")),
} }
// libtint_sem_src // libtint_sem_src
@ -1167,13 +1167,13 @@ pub fn Sdk(comptime deps: anytype) type {
var flags = std.ArrayList([]const u8).init(b.allocator); var flags = std.ArrayList([]const u8).init(b.allocator);
try flags.appendSlice(&.{ try flags.appendSlice(&.{
include("libs/dawn"), include(b, "libs/dawn"),
include("libs/dawn/third_party/vulkan-deps/spirv-tools/src"), include(b, "libs/dawn/third_party/vulkan-deps/spirv-tools/src"),
include("libs/dawn/third_party/vulkan-deps/spirv-tools/src/include"), include(b, "libs/dawn/third_party/vulkan-deps/spirv-tools/src/include"),
include("libs/dawn/third_party/vulkan-deps/spirv-headers/src/include"), include(b, "libs/dawn/third_party/vulkan-deps/spirv-headers/src/include"),
include("libs/dawn/out/Debug/gen/third_party/vulkan-deps/spirv-tools/src"), include(b, "libs/dawn/out/Debug/gen/third_party/vulkan-deps/spirv-tools/src"),
include("libs/dawn/out/Debug/gen/third_party/vulkan-deps/spirv-tools/src/include"), include(b, "libs/dawn/out/Debug/gen/third_party/vulkan-deps/spirv-tools/src/include"),
include("libs/dawn/third_party/vulkan-deps/spirv-headers/src/include/spirv/unified1"), include(b, "libs/dawn/third_party/vulkan-deps/spirv-headers/src/include/spirv/unified1"),
}); });
// spvtools // spvtools
@ -1241,8 +1241,8 @@ pub fn Sdk(comptime deps: anytype) type {
var flags = std.ArrayList([]const u8).init(b.allocator); var flags = std.ArrayList([]const u8).init(b.allocator);
try flags.appendSlice(&.{ try flags.appendSlice(&.{
include("libs/dawn"), include(b, "libs/dawn"),
include("libs/dawn/third_party/abseil-cpp"), include(b, "libs/dawn/third_party/abseil-cpp"),
}); });
if (target.os.tag == .windows) try flags.appendSlice(&.{ if (target.os.tag == .windows) try flags.appendSlice(&.{
"-DABSL_FORCE_THREAD_IDENTITY_MODE=2", "-DABSL_FORCE_THREAD_IDENTITY_MODE=2",
@ -1250,7 +1250,7 @@ pub fn Sdk(comptime deps: anytype) type {
"-DD3D10_ARBITRARY_HEADER_ORDERING", "-DD3D10_ARBITRARY_HEADER_ORDERING",
"-D_CRT_SECURE_NO_WARNINGS", "-D_CRT_SECURE_NO_WARNINGS",
"-DNOMINMAX", "-DNOMINMAX",
include("src/dawn/zig_mingw_pthread"), include(b, "src/dawn/zig_mingw_pthread"),
}); });
// absl // absl
@ -1299,11 +1299,11 @@ pub fn Sdk(comptime deps: anytype) type {
var flags = std.ArrayList([]const u8).init(b.allocator); var flags = std.ArrayList([]const u8).init(b.allocator);
try flags.appendSlice(&.{ try flags.appendSlice(&.{
include("libs/dawn"), include(b, "libs/dawn"),
include("libs/dawn/src"), include(b, "libs/dawn/src"),
include("libs/dawn/include"), include(b, "libs/dawn/include"),
include("libs/dawn/out/Debug/gen/include"), include(b, "libs/dawn/out/Debug/gen/include"),
include("libs/dawn/out/Debug/gen/src"), include(b, "libs/dawn/out/Debug/gen/src"),
}); });
try appendLangScannedSources(b, lib, options, .{ try appendLangScannedSources(b, lib, options, .{
@ -1339,10 +1339,10 @@ pub fn Sdk(comptime deps: anytype) type {
var flags = std.ArrayList([]const u8).init(b.allocator); var flags = std.ArrayList([]const u8).init(b.allocator);
try appendDawnEnableBackendTypeFlags(&flags, options); try appendDawnEnableBackendTypeFlags(&flags, options);
try flags.appendSlice(&.{ try flags.appendSlice(&.{
include(deps.glfw_include_dir), include(b, deps.glfw_include_dir),
include("libs/dawn/src"), include(b, "libs/dawn/src"),
include("libs/dawn/include"), include(b, "libs/dawn/include"),
include("libs/dawn/out/Debug/gen/include"), include(b, "libs/dawn/out/Debug/gen/include"),
}); });
var cpp_sources = std.ArrayList([]const u8).init(b.allocator); var cpp_sources = std.ArrayList([]const u8).init(b.allocator);
@ -1350,7 +1350,7 @@ pub fn Sdk(comptime deps: anytype) type {
"src/dawn/utils/BackendBinding.cpp", "src/dawn/utils/BackendBinding.cpp",
"src/dawn/utils/NullBinding.cpp", "src/dawn/utils/NullBinding.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
@ -1358,7 +1358,7 @@ pub fn Sdk(comptime deps: anytype) type {
inline for ([_][]const u8{ inline for ([_][]const u8{
"src/dawn/utils/D3D12Binding.cpp", "src/dawn/utils/D3D12Binding.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
try flags.appendSlice(dawn_d3d12_flags); try flags.appendSlice(dawn_d3d12_flags);
@ -1367,7 +1367,7 @@ pub fn Sdk(comptime deps: anytype) type {
inline for ([_][]const u8{ inline for ([_][]const u8{
"src/dawn/utils/MetalBinding.mm", "src/dawn/utils/MetalBinding.mm",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
} }
@ -1376,7 +1376,7 @@ pub fn Sdk(comptime deps: anytype) type {
inline for ([_][]const u8{ inline for ([_][]const u8{
"src/dawn/utils/OpenGLBinding.cpp", "src/dawn/utils/OpenGLBinding.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
} }
@ -1385,7 +1385,7 @@ pub fn Sdk(comptime deps: anytype) type {
inline for ([_][]const u8{ inline for ([_][]const u8{
"src/dawn/utils/VulkanBinding.cpp", "src/dawn/utils/VulkanBinding.cpp",
}) |path| { }) |path| {
const abs_path = sdkPath("/libs/dawn/" ++ path); const abs_path = sdkPath(b, "/libs/dawn/" ++ path);
try cpp_sources.append(abs_path); try cpp_sources.append(abs_path);
} }
} }
@ -1420,13 +1420,13 @@ pub fn Sdk(comptime deps: anytype) type {
var flags = std.ArrayList([]const u8).init(b.allocator); var flags = std.ArrayList([]const u8).init(b.allocator);
try flags.appendSlice(&.{ try flags.appendSlice(&.{
include("libs/"), include(b, "libs/"),
include("libs/DirectXShaderCompiler/include/llvm/llvm_assert"), include(b, "libs/DirectXShaderCompiler/include/llvm/llvm_assert"),
include("libs/DirectXShaderCompiler/include"), include(b, "libs/DirectXShaderCompiler/include"),
include("libs/DirectXShaderCompiler/build/include"), include(b, "libs/DirectXShaderCompiler/build/include"),
include("libs/DirectXShaderCompiler/build/lib/HLSL"), include(b, "libs/DirectXShaderCompiler/build/lib/HLSL"),
include("libs/DirectXShaderCompiler/build/lib/DxilPIXPasses"), include(b, "libs/DirectXShaderCompiler/build/lib/DxilPIXPasses"),
include("libs/DirectXShaderCompiler/build/include"), include(b, "libs/DirectXShaderCompiler/build/include"),
"-DUNREFERENCED_PARAMETER(x)=", "-DUNREFERENCED_PARAMETER(x)=",
"-Wno-inconsistent-missing-override", "-Wno-inconsistent-missing-override",
"-Wno-missing-exception-spec", "-Wno-missing-exception-spec",
@ -1562,7 +1562,7 @@ pub fn Sdk(comptime deps: anytype) type {
excluding: []const []const u8, excluding: []const []const u8,
excluding_contains: []const []const u8, excluding_contains: []const []const u8,
) !void { ) !void {
const abs_dir = try std.fs.path.join(b.allocator, &.{ sdkPath("/"), rel_dir }); const abs_dir = try std.fs.path.join(b.allocator, &.{ sdkPath(b, "/"), rel_dir });
defer b.allocator.free(abs_dir); defer b.allocator.free(abs_dir);
var dir = try std.fs.openIterableDirAbsolute(abs_dir, .{}); var dir = try std.fs.openIterableDirAbsolute(abs_dir, .{});
defer dir.close(); defer dir.close();
@ -1601,16 +1601,34 @@ pub fn Sdk(comptime deps: anytype) type {
} }
} }
fn include(comptime rel: []const u8) []const u8 { var this_dir: ?[]const u8 = null;
return comptime "-I" ++ sdkPath("/" ++ rel);
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (this_dir == null) {
const unresolved_dir = comptime std.fs.path.dirname(@src().file) orelse ".";
if (comptime unresolved_dir[0] == '/') {
this_dir = unresolved_dir;
} else {
this_dir = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
}
return this_dir.?;
} }
fn sdkPath(comptime suffix: []const u8) []const u8 { fn sdkPath(b: *Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path"); if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse "."; return std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
break :blk root_dir ++ suffix; }
};
fn include(b: *Builder, comptime rel: []const u8) []const u8 {
return std.mem.concat(b.allocator, u8, &.{ "-I", sdkPath(b, "/" ++ rel) }) catch unreachable;
} }
}; };
} }

View file

@ -1,9 +1,10 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder;
pub fn Sdk(comptime deps: anytype) type { pub fn Sdk(comptime deps: anytype) type {
return struct { return struct {
pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: Options) !*std.build.RunStep { pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: Options) !*std.build.RunStep {
const main_tests = b.addTestExe("gpu-tests", sdkPath("/src/main.zig")); const main_tests = b.addTestExe("gpu-tests", sdkPath(b, "/src/main.zig"));
main_tests.setBuildMode(mode); main_tests.setBuildMode(mode);
main_tests.setTarget(target); main_tests.setTarget(target);
try link(b, main_tests, options); try link(b, main_tests, options);
@ -16,27 +17,58 @@ pub fn Sdk(comptime deps: anytype) type {
gpu_dawn_options: deps.gpu_dawn.Options = .{}, gpu_dawn_options: deps.gpu_dawn.Options = .{},
}; };
pub const pkg = std.build.Pkg{ var cached_pkg: ?std.build.Pkg = null;
.name = "gpu",
.source = .{ .path = sdkPath("/src/main.zig") }, pub fn pkg(b: *Builder) std.build.Pkg {
.dependencies = &.{deps.glfw.pkg}, if (cached_pkg == null) {
}; const dependencies = b.allocator.create([1]std.build.Pkg) catch unreachable;
dependencies.* = .{
deps.glfw.pkg(b),
};
cached_pkg = .{
.name = "gpu",
.source = .{ .path = sdkPath(b, "/src/main.zig") },
.dependencies = dependencies,
};
}
return cached_pkg.?;
}
pub fn link(b: *std.build.Builder, step: *std.build.LibExeObjStep, options: Options) !void { pub fn link(b: *std.build.Builder, step: *std.build.LibExeObjStep, options: Options) !void {
if (step.target.toTarget().cpu.arch != .wasm32) { if (step.target.toTarget().cpu.arch != .wasm32) {
try deps.glfw.link(b, step, options.glfw_options); try deps.glfw.link(b, step, options.glfw_options);
try deps.gpu_dawn.link(b, step, options.gpu_dawn_options); try deps.gpu_dawn.link(b, step, options.gpu_dawn_options);
step.addCSourceFile(sdkPath("/src/mach_dawn.cpp"), &.{"-std=c++17"}); step.addCSourceFile(sdkPath(b, "/src/mach_dawn.cpp"), &.{"-std=c++17"});
step.addIncludePath(sdkPath("/src")); step.addIncludePath(sdkPath(b, "/src"));
} }
} }
fn sdkPath(comptime suffix: []const u8) []const u8 { var this_dir: ?[]const u8 = null;
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (this_dir == null) {
const unresolved_dir = comptime std.fs.path.dirname(@src().file) orelse ".";
if (comptime unresolved_dir[0] == '/') {
this_dir = unresolved_dir;
} else {
this_dir = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
}
return this_dir.?;
}
fn sdkPath(b: *Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path"); if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse "."; return std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
break :blk root_dir ++ suffix;
};
} }
}; };
} }

View file

@ -1,18 +1,28 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder;
pub const pkg = std.build.Pkg{ var cached_pkg: ?std.build.Pkg = null;
.name = "model3d",
.source = .{ .path = sdkPath("/src/main.zig") },
};
pub fn build(b: *std.build.Builder) void { pub fn pkg(b: *Builder) std.build.Pkg {
if (cached_pkg == null) {
cached_pkg = .{
.name = "model3d",
.source = .{ .path = sdkPath(b, "/src/main.zig") },
.dependencies = &.{},
};
}
return cached_pkg.?;
}
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const mode = b.standardReleaseOptions();
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const test_step = b.step("test", "Run library tests"); const test_step = b.step("test", "Run library tests");
test_step.dependOn(&testStep(b, mode, target).step); test_step.dependOn(&testStep(b, mode, target).step);
} }
pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep {
const main_tests = b.addTestExe("model3d-tests", "src/main.zig"); const main_tests = b.addTestExe("model3d-tests", "src/main.zig");
main_tests.setBuildMode(mode); main_tests.setBuildMode(mode);
main_tests.setTarget(target); main_tests.setTarget(target);
@ -21,21 +31,61 @@ pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.C
return main_tests.run(); return main_tests.run();
} }
pub fn link(b: *std.build.Builder, step: *std.build.LibExeObjStep, target: std.zig.CrossTarget) void { pub fn link(b: *Builder, step: *std.build.LibExeObjStep, target: std.zig.CrossTarget) void {
const lib = b.addStaticLibrarySource("model3d", null); const lib = b.addStaticLibrarySource("model3d", null);
lib.setTarget(target); lib.setTarget(target);
// Note: model3d needs unaligned accesses, which are safe on all modern architectures. // Note: model3d needs unaligned accesses, which are safe on all modern architectures.
// See https://gitlab.com/bztsrc/model3d/-/issues/19 // See https://gitlab.com/bztsrc/model3d/-/issues/19
lib.addCSourceFile(sdkPath("/src/c/m3d.c"), &.{ "-std=c89", "-fno-sanitize=alignment" }); lib.addCSourceFile(sdkPath(b, "/src/c/m3d.c"), &.{ "-std=c89", "-fno-sanitize=alignment" });
lib.linkLibC(); lib.linkLibC();
step.addIncludePath(sdkPath("/src/c/")); step.addIncludePath(sdkPath(b, "/src/c/"));
step.linkLibrary(lib); step.linkLibrary(lib);
} }
fn sdkPath(comptime suffix: []const u8) []const u8 { const unresolved_dir = (struct {
if (suffix[0] != '/') @compileError("suffix must be an absolute path"); inline fn unresolvedDir() []const u8 {
return comptime blk: { return comptime std.fs.path.dirname(@src().file) orelse ".";
const root_dir = std.fs.path.dirname(@src().file) orelse "."; }
break :blk root_dir ++ suffix; }).unresolvedDir();
};
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir;
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
return cached_dir.*.?;
}
inline fn sdkPath(b: *Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
inline fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
return sdkPathInternal(allocator, suffix.len, suffix[0..suffix.len].*);
}
fn sdkPathInternal(allocator: std.mem.Allocator, comptime len: usize, comptime suffix: [len]u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir ++ @as([]const u8, &suffix);
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
}
return cached_dir.*.?;
} }

View file

@ -1,12 +1,26 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder;
pub fn Sdk(comptime deps: anytype) type { pub fn Sdk(comptime deps: anytype) type {
return struct { return struct {
pub const pkg = std.build.Pkg{ var cached_pkg: ?std.build.Pkg = null;
.name = "sysaudio",
.source = .{ .path = sdkPath("/src/main.zig") }, pub fn pkg(b: *Builder) std.build.Pkg {
.dependencies = &.{deps.sysjs.pkg}, if (cached_pkg == null) {
}; const dependencies = b.allocator.create([1]std.build.Pkg) catch unreachable;
dependencies.* = .{
deps.sysjs.pkg(b),
};
cached_pkg = .{
.name = "sysaudio",
.source = .{ .path = sdkPath(b, "/src/main.zig") },
.dependencies = dependencies,
};
}
return cached_pkg.?;
}
pub const Options = struct { pub const Options = struct {
install_libs: bool = false, install_libs: bool = false,
@ -15,8 +29,8 @@ pub fn Sdk(comptime deps: anytype) type {
system_sdk: deps.system_sdk.Options = .{}, system_sdk: deps.system_sdk.Options = .{},
}; };
pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep {
const main_tests = b.addTestExe("sysaudio-tests", sdkPath("/src/main.zig")); const main_tests = b.addTestExe("sysaudio-tests", sdkPath(b, "/src/main.zig"));
main_tests.setBuildMode(mode); main_tests.setBuildMode(mode);
main_tests.setTarget(target); main_tests.setTarget(target);
link(b, main_tests, .{}); link(b, main_tests, .{});
@ -24,7 +38,7 @@ pub fn Sdk(comptime deps: anytype) type {
return main_tests.run(); return main_tests.run();
} }
pub fn link(b: *std.build.Builder, step: *std.build.LibExeObjStep, options: Options) void { pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void {
if (step.target.toTarget().cpu.arch != .wasm32) { if (step.target.toTarget().cpu.arch != .wasm32) {
// TODO(build-system): pass system SDK options through // TODO(build-system): pass system SDK options through
deps.system_sdk.include(b, step, .{}); deps.system_sdk.include(b, step, .{});
@ -50,19 +64,37 @@ pub fn Sdk(comptime deps: anytype) type {
if (std.mem.eql(u8, no_ensure_submodules, "true")) return; if (std.mem.eql(u8, no_ensure_submodules, "true")) return;
} else |_| {} } else |_| {}
var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator); var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator);
child.cwd = sdkPath("/"); child.cwd = sdkPathAllocator(allocator, "/");
child.stderr = std.io.getStdErr(); child.stderr = std.io.getStdErr();
child.stdout = std.io.getStdOut(); child.stdout = std.io.getStdOut();
_ = try child.spawnAndWait(); _ = try child.spawnAndWait();
} }
fn sdkPath(comptime suffix: []const u8) []const u8 { var this_dir: ?[]const u8 = null;
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (this_dir == null) {
const unresolved_dir = comptime std.fs.path.dirname(@src().file) orelse ".";
if (comptime unresolved_dir[0] == '/') {
this_dir = unresolved_dir;
} else {
this_dir = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
}
return this_dir.?;
}
fn sdkPath(b: *Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path"); if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse "."; return std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
break :blk root_dir ++ suffix;
};
} }
}; };
} }

View file

@ -1,29 +1,78 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder;
pub fn build(b: *std.build.Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const mode = b.standardReleaseOptions();
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const test_step = b.step("test", "Run library tests"); const test_step = b.step("test", "Run library tests");
test_step.dependOn(&testStep(b, mode, target).step); test_step.dependOn(&testStep(b, mode, target).step);
} }
pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep {
const main_tests = b.addTestExe("sysjs-tests", sdkPath("/src/main.zig")); const main_tests = b.addTestExe("sysjs-tests", sdkPath(b, "/src/main.zig"));
main_tests.setBuildMode(mode); main_tests.setBuildMode(mode);
main_tests.setTarget(target); main_tests.setTarget(target);
return main_tests.run(); return main_tests.run();
} }
pub const pkg = std.build.Pkg{ var cached_pkg: ?std.build.Pkg = null;
.name = "sysjs",
.source = .{ .path = sdkPath("/src/main.zig") },
.dependencies = &[_]std.build.Pkg{},
};
fn sdkPath(comptime suffix: []const u8) []const u8 { pub fn pkg(b: *Builder) std.build.Pkg {
if (suffix[0] != '/') @compileError("suffix must be an absolute path"); if (cached_pkg == null) {
return comptime blk: { cached_pkg = .{
const root_dir = std.fs.path.dirname(@src().file) orelse "."; .name = "sysjs",
break :blk root_dir ++ suffix; .source = .{ .path = sdkPath(b, "/src/main.zig") },
}; .dependencies = &.{},
};
}
return cached_pkg.?;
}
const unresolved_dir = (struct {
inline fn unresolvedDir() []const u8 {
return comptime std.fs.path.dirname(@src().file) orelse ".";
}
}).unresolvedDir();
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir;
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
return cached_dir.*.?;
}
inline fn sdkPath(b: *Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
inline fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
return sdkPathInternal(allocator, suffix.len, suffix[0..suffix.len].*);
}
fn sdkPathInternal(allocator: std.mem.Allocator, comptime len: usize, comptime suffix: [len]u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir ++ @as([]const u8, &suffix);
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
}
return cached_dir.*.?;
} }

View file

@ -6,7 +6,7 @@ const mem = std.mem;
const fs = std.fs; const fs = std.fs;
const build = std.build; const build = std.build;
const www_dir_path = sdkPath("/www"); const www_dir_path = "/www";
const buffer_size = 2048; const buffer_size = 2048;
const esc = struct { const esc = struct {
pub const reset = "\x1b[0m"; pub const reset = "\x1b[0m";
@ -75,11 +75,13 @@ const Wasmserve = struct {
self.compile(); self.compile();
std.debug.assert(mem.eql(u8, fs.path.extension(self.exe_step.out_filename), ".wasm")); std.debug.assert(mem.eql(u8, fs.path.extension(self.exe_step.out_filename), ".wasm"));
var www_dir = try fs.cwd().openIterableDir(www_dir_path, .{}); const resolved_www_dir_path = sdkPathAllocator(step.dependencies.allocator, www_dir_path);
var www_dir = try fs.cwd().openIterableDir(resolved_www_dir_path, .{});
defer www_dir.close(); defer www_dir.close();
var www_dir_iter = www_dir.iterate(); var www_dir_iter = www_dir.iterate();
while (try www_dir_iter.next()) |file| { while (try www_dir_iter.next()) |file| {
const path = try fs.path.join(self.b.allocator, &.{ www_dir_path, file.name }); const path = try fs.path.join(self.b.allocator, &.{ resolved_www_dir_path, file.name });
defer self.b.allocator.free(path); defer self.b.allocator.free(path);
const install_www = self.b.addInstallFileWithDir( const install_www = self.b.addInstallFileWithDir(
.{ .path = path }, .{ .path = path },
@ -326,12 +328,52 @@ fn logErr(err: anyerror, src: std.builtin.SourceLocation) void {
} }
} }
fn sdkPath(comptime suffix: []const u8) []const u8 { const unresolved_dir = (struct {
inline fn unresolvedDir() []const u8 {
return comptime std.fs.path.dirname(@src().file) orelse ".";
}
}).unresolvedDir();
fn thisDir(allocator: std.mem.Allocator) []const u8 {
if (comptime unresolved_dir[0] == '/') {
return unresolved_dir;
}
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.cwd().realpathAlloc(allocator, unresolved_dir) catch unreachable;
}
return cached_dir.*.?;
}
inline fn sdkPath(b: *build.Builder, comptime suffix: []const u8) []const u8 {
return sdkPathAllocator(b.allocator, suffix);
}
inline fn sdkPathAllocator(allocator: std.mem.Allocator, comptime suffix: []const u8) []const u8 {
return sdkPathInternal(allocator, suffix.len, suffix[0..suffix.len].*);
}
fn sdkPathInternal(allocator: std.mem.Allocator, comptime len: usize, comptime suffix: [len]u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path"); if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse "."; if (comptime unresolved_dir[0] == '/') {
break :blk root_dir ++ suffix; return unresolved_dir ++ @as([]const u8, &suffix);
}; }
const cached_dir = &(struct {
var cached_dir: ?[]const u8 = null;
}).cached_dir;
if (cached_dir.* == null) {
cached_dir.* = std.fs.path.resolve(allocator, &.{ thisDir(allocator), suffix[1..] }) catch unreachable;
}
return cached_dir.*.?;
} }
// copied from LibExeObjStep.make() // copied from LibExeObjStep.make()