all: build: organize build files and reduce unreachables (#567)
This commit is contained in:
parent
cc301493ca
commit
fcb82345d4
10 changed files with 434 additions and 419 deletions
|
|
@ -13,20 +13,20 @@ pub fn build(b: *Builder) void {
|
|||
test_step.dependOn(&testStepShared(b, mode, target).step);
|
||||
}
|
||||
|
||||
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", thisDir() ++ "/src/main.zig");
|
||||
main_tests.setBuildMode(mode);
|
||||
main_tests.setTarget(target);
|
||||
link(b, main_tests, .{});
|
||||
try link(b, main_tests, .{});
|
||||
main_tests.install();
|
||||
return main_tests.run();
|
||||
}
|
||||
|
||||
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", thisDir() ++ "/src/main.zig");
|
||||
main_tests.setBuildMode(mode);
|
||||
main_tests.setTarget(target);
|
||||
link(b, main_tests, .{ .shared = true });
|
||||
try link(b, main_tests, .{ .shared = true });
|
||||
main_tests.install();
|
||||
return main_tests.run();
|
||||
}
|
||||
|
|
@ -79,10 +79,11 @@ fn cimportWorkaround() void {
|
|||
std.fs.cwd().copyFile(cn_path, dest_dir, thisDir() ++ "/src/c_native.zig", .{}) catch unreachable;
|
||||
}
|
||||
|
||||
pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void {
|
||||
pub const LinkError = error{FailedToLinkGPU} || BuildError;
|
||||
pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) LinkError!void {
|
||||
cimportWorkaround();
|
||||
|
||||
const lib = buildLibrary(b, step.build_mode, step.target, options);
|
||||
const lib = try buildLibrary(b, step.build_mode, step.target, options);
|
||||
step.linkLibrary(lib);
|
||||
addGLFWIncludes(step);
|
||||
if (options.shared) {
|
||||
|
|
@ -93,9 +94,10 @@ pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void
|
|||
}
|
||||
}
|
||||
|
||||
fn buildLibrary(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: Options) *std.build.LibExeObjStep {
|
||||
pub const BuildError = error{CannotEnsureDependency} || std.mem.Allocator.Error;
|
||||
fn buildLibrary(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: Options) BuildError!*std.build.LibExeObjStep {
|
||||
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
|
||||
ensureDependencySubmodule(b.allocator, "upstream") catch unreachable;
|
||||
ensureDependencySubmodule(b.allocator, "upstream") catch return error.CannotEnsureDependency;
|
||||
|
||||
const lib = if (options.shared)
|
||||
b.addSharedLibrary("glfw", null, .unversioned)
|
||||
|
|
@ -108,7 +110,7 @@ fn buildLibrary(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget
|
|||
lib.defineCMacro("_GLFW_BUILD_DLL", null);
|
||||
|
||||
addGLFWIncludes(lib);
|
||||
addGLFWSources(b, lib, options);
|
||||
try addGLFWSources(b, lib, options);
|
||||
linkGLFWDependencies(b, lib, options);
|
||||
|
||||
if (options.install_libs)
|
||||
|
|
@ -122,7 +124,7 @@ fn addGLFWIncludes(step: *std.build.LibExeObjStep) void {
|
|||
step.addIncludePath(thisDir() ++ "/upstream/vulkan_headers/include");
|
||||
}
|
||||
|
||||
fn addGLFWSources(b: *Builder, lib: *std.build.LibExeObjStep, options: Options) void {
|
||||
fn addGLFWSources(b: *Builder, lib: *std.build.LibExeObjStep, options: Options) std.mem.Allocator.Error!void {
|
||||
const include_glfw_src = "-I" ++ thisDir() ++ "/upstream/glfw/src";
|
||||
switch (lib.target_info.target.os.tag) {
|
||||
.windows => lib.addCSourceFiles(&.{
|
||||
|
|
@ -143,42 +145,25 @@ 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);
|
||||
sources.append(thisDir() ++ "/src/sources_all.c") catch unreachable;
|
||||
sources.append(thisDir() ++ "/src/sources_linux.c") catch unreachable;
|
||||
try sources.append(thisDir() ++ "/src/sources_all.c");
|
||||
try sources.append(thisDir() ++ "/src/sources_linux.c");
|
||||
if (options.x11) {
|
||||
sources.append(thisDir() ++ "/src/sources_linux_x11.c") catch unreachable;
|
||||
flags.append("-D_GLFW_X11") catch unreachable;
|
||||
try sources.append(thisDir() ++ "/src/sources_linux_x11.c");
|
||||
try flags.append("-D_GLFW_X11");
|
||||
}
|
||||
if (options.wayland) {
|
||||
sources.append(thisDir() ++ "/src/sources_linux_wayland.c") catch unreachable;
|
||||
flags.append("-D_GLFW_WAYLAND") catch unreachable;
|
||||
try sources.append(thisDir() ++ "/src/sources_linux_wayland.c");
|
||||
try flags.append("-D_GLFW_WAYLAND");
|
||||
}
|
||||
flags.append("-I" ++ thisDir() ++ "/upstream/glfw/src") catch unreachable;
|
||||
try flags.append("-I" ++ thisDir() ++ "/upstream/glfw/src");
|
||||
// TODO(upstream): glfw can't compile on clang15 without this flag
|
||||
flags.append("-Wno-implicit-function-declaration") catch unreachable;
|
||||
try flags.append("-Wno-implicit-function-declaration");
|
||||
|
||||
lib.addCSourceFiles(sources.items, flags.items);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
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 = thisDir();
|
||||
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 linkGLFWDependencies(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void {
|
||||
step.linkLibC();
|
||||
system_sdk.include(b, step, options.system_sdk);
|
||||
|
|
@ -217,3 +202,20 @@ fn linkGLFWDependencies(b: *Builder, step: *std.build.LibExeObjStep, options: Op
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
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 = thisDir();
|
||||
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 ".";
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue