glfw: update to latest Zig build API
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
f88f26f663
commit
14790d5b1e
4 changed files with 47 additions and 38 deletions
|
@ -56,9 +56,9 @@ Then in your `build.zig` add:
|
||||||
...
|
...
|
||||||
const glfw = @import("libs/mach-glfw/build.zig");
|
const glfw = @import("libs/mach-glfw/build.zig");
|
||||||
|
|
||||||
pub fn build(b: *Builder) !void {
|
pub fn build(b: *Build) !void {
|
||||||
...
|
...
|
||||||
exe.addPackage(glfw.pkg);
|
exe.addModule("glfw", glfw.module(b));
|
||||||
try glfw.link(b, exe, .{});
|
try glfw.link(b, exe, .{});
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -82,10 +82,10 @@ Then in your `build.zig` add:
|
||||||
const pkgs = @import("deps.zig").pkgs;
|
const pkgs = @import("deps.zig").pkgs;
|
||||||
const glfw = @import("build-glfw");
|
const glfw = @import("build-glfw");
|
||||||
|
|
||||||
pub fn build(b: *Builder) !void {
|
pub fn build(b: *Build) !void {
|
||||||
...
|
...
|
||||||
|
|
||||||
exe.addPackage(pkgs.glfw);
|
exe.addModule("glfw", pkgs.glfw);
|
||||||
try glfw.link(b, exe, .{});
|
try glfw.link(b, exe, .{});
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
63
build.zig
63
build.zig
|
@ -1,31 +1,41 @@
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Builder = std.build.Builder;
|
const Build = std.Build;
|
||||||
|
|
||||||
const system_sdk = @import("system_sdk.zig");
|
const system_sdk = @import("system_sdk.zig");
|
||||||
|
|
||||||
pub fn build(b: *Builder) !void {
|
pub fn build(b: *Build) !void {
|
||||||
const mode = b.standardReleaseOptions();
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
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(&(try testStep(b, mode, target)).step);
|
test_step.dependOn(&(try testStep(b, optimize, target)).step);
|
||||||
test_step.dependOn(&(try testStepShared(b, mode, target)).step);
|
test_step.dependOn(&(try testStepShared(b, optimize, target)).step);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) !*std.build.RunStep {
|
pub fn testStep(b: *Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) !*std.build.RunStep {
|
||||||
const main_tests = b.addTestExe("glfw-tests", sdkPath("/src/main.zig"));
|
const main_tests = b.addTest(.{
|
||||||
main_tests.setBuildMode(mode);
|
.name = "glfw-tests",
|
||||||
main_tests.setTarget(target);
|
.kind = .test_exe,
|
||||||
|
.root_source_file = .{ .path = sdkPath("/src/main.zig") },
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
try link(b, main_tests, .{});
|
try link(b, main_tests, .{});
|
||||||
main_tests.install();
|
main_tests.install();
|
||||||
return main_tests.run();
|
return main_tests.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn testStepShared(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) !*std.build.RunStep {
|
fn testStepShared(b: *Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) !*std.build.RunStep {
|
||||||
const main_tests = b.addTestExe("glfw-tests-shared", sdkPath("/src/main.zig"));
|
const main_tests = b.addTest(.{
|
||||||
main_tests.setBuildMode(mode);
|
.name = "glfw-tests-shared",
|
||||||
main_tests.setTarget(target);
|
.kind = .test_exe,
|
||||||
|
.root_source_file = .{ .path = sdkPath("/src/main.zig") },
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
try link(b, main_tests, .{ .shared = true });
|
try link(b, main_tests, .{ .shared = true });
|
||||||
main_tests.install();
|
main_tests.install();
|
||||||
return main_tests.run();
|
return main_tests.run();
|
||||||
|
@ -59,14 +69,15 @@ pub const Options = struct {
|
||||||
install_libs: bool = false,
|
install_libs: bool = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const pkg = std.build.Pkg{
|
pub fn module(b: *std.Build) *std.build.Module {
|
||||||
.name = "glfw",
|
return b.createModule(.{
|
||||||
.source = .{ .path = sdkPath("/src/main.zig") },
|
.source_file = .{ .path = sdkPath("/src/main.zig") },
|
||||||
};
|
});
|
||||||
|
}
|
||||||
|
|
||||||
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: *Build, step: *std.build.CompileStep, options: Options) LinkError!void {
|
||||||
const lib = try buildLibrary(b, step.build_mode, step.target, options);
|
const lib = try buildLibrary(b, step.optimize, step.target, options);
|
||||||
step.linkLibrary(lib);
|
step.linkLibrary(lib);
|
||||||
addGLFWIncludes(step);
|
addGLFWIncludes(step);
|
||||||
if (options.shared) {
|
if (options.shared) {
|
||||||
|
@ -78,16 +89,14 @@ pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) LinkE
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const BuildError = error{CannotEnsureDependency} || std.mem.Allocator.Error;
|
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 {
|
fn buildLibrary(b: *Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget, options: Options) BuildError!*std.build.CompileStep {
|
||||||
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
|
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
|
||||||
ensureDependencySubmodule(b.allocator, "upstream") catch return error.CannotEnsureDependency;
|
ensureDependencySubmodule(b.allocator, "upstream") catch return error.CannotEnsureDependency;
|
||||||
|
|
||||||
const lib = if (options.shared)
|
const lib = if (options.shared)
|
||||||
b.addSharedLibrary("glfw", null, .unversioned)
|
b.addSharedLibrary(.{ .name = "glfw", .target = target, .optimize = optimize })
|
||||||
else
|
else
|
||||||
b.addStaticLibrary("glfw", null);
|
b.addStaticLibrary(.{ .name = "glfw", .target = target, .optimize = optimize });
|
||||||
lib.setBuildMode(mode);
|
|
||||||
lib.setTarget(target);
|
|
||||||
|
|
||||||
if (options.shared)
|
if (options.shared)
|
||||||
lib.defineCMacro("_GLFW_BUILD_DLL", null);
|
lib.defineCMacro("_GLFW_BUILD_DLL", null);
|
||||||
|
@ -102,12 +111,12 @@ fn buildLibrary(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget
|
||||||
return lib;
|
return lib;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn addGLFWIncludes(step: *std.build.LibExeObjStep) void {
|
fn addGLFWIncludes(step: *std.build.CompileStep) void {
|
||||||
step.addIncludePath(sdkPath("/upstream/glfw/include"));
|
step.addIncludePath(sdkPath("/upstream/glfw/include"));
|
||||||
step.addIncludePath(sdkPath("/upstream/vulkan_headers/include"));
|
step.addIncludePath(sdkPath("/upstream/vulkan_headers/include"));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn addGLFWSources(b: *Builder, lib: *std.build.LibExeObjStep, options: Options) std.mem.Allocator.Error!void {
|
fn addGLFWSources(b: *Build, lib: *std.build.CompileStep, options: Options) std.mem.Allocator.Error!void {
|
||||||
const include_glfw_src = comptime "-I" ++ sdkPath("/upstream/glfw/src");
|
const include_glfw_src = comptime "-I" ++ sdkPath("/upstream/glfw/src");
|
||||||
switch (lib.target_info.target.os.tag) {
|
switch (lib.target_info.target.os.tag) {
|
||||||
.windows => lib.addCSourceFiles(&.{
|
.windows => lib.addCSourceFiles(&.{
|
||||||
|
@ -147,7 +156,7 @@ fn addGLFWSources(b: *Builder, lib: *std.build.LibExeObjStep, options: Options)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn linkGLFWDependencies(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void {
|
fn linkGLFWDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
|
||||||
step.linkLibC();
|
step.linkLibC();
|
||||||
system_sdk.include(b, step, options.system_sdk);
|
system_sdk.include(b, step, options.system_sdk);
|
||||||
switch (step.target_info.target.os.tag) {
|
switch (step.target_info.target.os.tag) {
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const zig_builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
|
|
||||||
const debug_mode = (zig_builtin.mode == .Debug);
|
const is_debug = builtin.mode == .Debug;
|
||||||
var glfw_initialized = if (debug_mode) false else @as(void, {});
|
var glfw_initialized = if (is_debug) false else @as(void, {});
|
||||||
pub inline fn toggleInitialized() void {
|
pub inline fn toggleInitialized() void {
|
||||||
if (debug_mode) glfw_initialized = !glfw_initialized;
|
if (is_debug) glfw_initialized = !glfw_initialized;
|
||||||
}
|
}
|
||||||
pub inline fn assertInitialized() void {
|
pub inline fn assertInitialized() void {
|
||||||
if (debug_mode) std.debug.assert(glfw_initialized);
|
if (is_debug) std.debug.assert(glfw_initialized);
|
||||||
}
|
}
|
||||||
pub inline fn assumeInitialized() void {
|
pub inline fn assumeInitialized() void {
|
||||||
glfw_initialized = true;
|
glfw_initialized = true;
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
//! version: Mar 4, 2022
|
//! version: Mar 4, 2022
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Builder = std.build.Builder;
|
const Build = std.Build;
|
||||||
|
|
||||||
pub const Options = struct {
|
pub const Options = struct {
|
||||||
pub const Sdk = struct {
|
pub const Sdk = struct {
|
||||||
|
@ -123,7 +123,7 @@ pub const Options = struct {
|
||||||
set_sysroot: bool = false,
|
set_sysroot: bool = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn include(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void {
|
pub fn include(b: *Build, step: *std.build.CompileStep, options: Options) void {
|
||||||
const target = step.target_info.target;
|
const target = step.target_info.target;
|
||||||
// var best_sdk: ?Options.Sdk = null;
|
// var best_sdk: ?Options.Sdk = null;
|
||||||
for (options.sdk_list) |sdk| {
|
for (options.sdk_list) |sdk| {
|
||||||
|
|
Loading…
Add table
Reference in a new issue