2021-07-06 20:53:10 -07:00
|
|
|
const std = @import("std");
|
|
|
|
const Builder = std.build.Builder;
|
|
|
|
|
2021-10-30 21:24:15 -07:00
|
|
|
const system_sdk = @import("system_sdk.zig");
|
|
|
|
|
2021-07-06 20:53:10 -07:00
|
|
|
pub fn build(b: *Builder) void {
|
2022-07-20 16:31:57 +04:30
|
|
|
const mode = b.standardReleaseOptions();
|
2022-07-19 14:35:28 +04:30
|
|
|
const test_step = b.step("test", "Run library tests");
|
2022-07-20 16:31:57 +04:30
|
|
|
test_step.dependOn(&testStep(b, mode).step);
|
2022-07-19 14:35:28 +04:30
|
|
|
}
|
2021-07-06 20:53:10 -07:00
|
|
|
|
2022-07-20 16:31:57 +04:30
|
|
|
pub fn testStep(b: *Builder, mode: std.builtin.Mode) *std.build.LibExeObjStep {
|
2022-07-19 14:35:28 +04:30
|
|
|
const main_tests = b.addTest(thisDir() ++ "/src/main.zig");
|
2022-07-20 16:31:57 +04:30
|
|
|
main_tests.setBuildMode(mode);
|
2021-07-06 20:53:10 -07:00
|
|
|
link(b, main_tests, .{});
|
2022-07-19 14:35:28 +04:30
|
|
|
return main_tests;
|
2021-07-06 20:53:10 -07:00
|
|
|
}
|
|
|
|
|
2021-07-10 01:54:16 -07:00
|
|
|
pub const LinuxWindowManager = enum {
|
|
|
|
X11,
|
|
|
|
Wayland,
|
|
|
|
};
|
2021-07-06 20:53:10 -07:00
|
|
|
|
|
|
|
pub const Options = struct {
|
2021-07-16 11:52:38 -07:00
|
|
|
/// Not supported on macOS.
|
2021-07-10 01:54:16 -07:00
|
|
|
vulkan: bool = true,
|
|
|
|
|
2021-07-16 11:52:38 -07:00
|
|
|
/// Only respected on macOS.
|
2021-07-10 01:54:16 -07:00
|
|
|
metal: bool = true,
|
|
|
|
|
2021-07-16 11:52:38 -07:00
|
|
|
/// Deprecated on macOS.
|
2021-07-10 01:54:16 -07:00
|
|
|
opengl: bool = false,
|
|
|
|
|
2021-10-16 16:53:15 -07:00
|
|
|
/// Not supported on macOS. GLES v3.2 only, currently.
|
2021-07-10 01:54:16 -07:00
|
|
|
gles: bool = false,
|
|
|
|
|
2021-07-16 11:52:38 -07:00
|
|
|
/// Only respected on Linux.
|
2022-06-11 17:18:42 -07:00
|
|
|
x11: bool = true,
|
|
|
|
|
|
|
|
/// Only respected on Linux.
|
2022-06-28 12:39:24 +02:00
|
|
|
wayland: bool = true,
|
2021-10-30 21:47:48 -07:00
|
|
|
|
|
|
|
/// System SDK options.
|
|
|
|
system_sdk: system_sdk.Options = .{},
|
2021-07-06 20:53:10 -07:00
|
|
|
};
|
|
|
|
|
2022-04-06 05:40:28 +01:00
|
|
|
pub const pkg = std.build.Pkg{
|
2022-04-04 22:39:58 -07:00
|
|
|
.name = "glfw",
|
2022-05-27 15:47:44 +02:00
|
|
|
.source = .{ .path = thisDir() ++ "/src/main.zig" },
|
2022-04-04 22:39:58 -07:00
|
|
|
};
|
|
|
|
|
2021-07-06 20:53:10 -07:00
|
|
|
pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void {
|
2021-10-24 13:50:14 -07:00
|
|
|
const lib = buildLibrary(b, step, options);
|
|
|
|
step.linkLibrary(lib);
|
|
|
|
linkGLFWDependencies(b, step, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn buildLibrary(b: *Builder, step: *std.build.LibExeObjStep, options: Options) *std.build.LibExeObjStep {
|
2022-06-11 12:05:39 -07:00
|
|
|
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
|
|
|
|
ensureDependencySubmodule(b.allocator, "upstream") catch unreachable;
|
|
|
|
|
2022-06-24 19:12:45 +02:00
|
|
|
const main_abs = std.fs.path.join(b.allocator, &.{ (comptime thisDir()), "src/main.zig" }) catch unreachable;
|
2021-10-24 13:50:14 -07:00
|
|
|
const lib = b.addStaticLibrary("glfw", main_abs);
|
|
|
|
lib.setBuildMode(step.build_mode);
|
|
|
|
lib.setTarget(step.target);
|
|
|
|
|
2021-11-30 11:29:56 -07:00
|
|
|
// TODO(build-system): pass system SDK options through
|
2021-10-30 21:24:15 -07:00
|
|
|
system_sdk.include(b, step, .{});
|
2021-07-06 20:53:10 -07:00
|
|
|
const target = (std.zig.system.NativeTargetInfo.detect(b.allocator, step.target) catch unreachable).target;
|
2022-06-24 19:12:45 +02:00
|
|
|
const include_glfw_src = "-I" ++ (comptime thisDir()) ++ "/upstream/glfw/src";
|
2021-07-06 20:53:10 -07:00
|
|
|
switch (target.os.tag) {
|
2022-06-11 15:44:27 -07:00
|
|
|
.windows => lib.addCSourceFiles(&.{
|
2022-06-24 19:12:45 +02:00
|
|
|
(comptime thisDir()) ++ "/src/sources_all.c",
|
|
|
|
(comptime thisDir()) ++ "/src/sources_windows.c",
|
2022-06-11 15:44:27 -07:00
|
|
|
}, &.{ "-D_GLFW_WIN32", include_glfw_src }),
|
2021-12-10 04:41:39 -07:00
|
|
|
.macos => lib.addCSourceFiles(&.{
|
2022-06-24 19:12:45 +02:00
|
|
|
(comptime thisDir()) ++ "/src/sources_all.c",
|
|
|
|
(comptime thisDir()) ++ "/src/sources_macos.m",
|
|
|
|
(comptime thisDir()) ++ "/src/sources_macos.c",
|
2021-12-10 04:41:39 -07:00
|
|
|
}, &.{ "-D_GLFW_COCOA", include_glfw_src }),
|
2021-07-06 20:53:10 -07:00
|
|
|
else => {
|
2021-10-16 18:04:10 -07:00
|
|
|
// TODO(future): for now, Linux must be built with glibc, not musl:
|
2021-07-29 19:11:08 -07:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// ld.lld: error: cannot create a copy relocation for symbol stderr
|
|
|
|
// thread 2004762 panic: attempt to unwrap error: LLDReportedFailure
|
|
|
|
// ```
|
|
|
|
step.target.abi = .gnu;
|
2021-10-24 13:50:14 -07:00
|
|
|
lib.setTarget(step.target);
|
2021-07-29 19:11:08 -07:00
|
|
|
|
2021-12-10 04:41:39 -07:00
|
|
|
var sources = std.ArrayList([]const u8).init(b.allocator);
|
2022-06-11 17:18:42 -07:00
|
|
|
var flags = std.ArrayList([]const u8).init(b.allocator);
|
2022-06-24 19:12:45 +02:00
|
|
|
sources.append((comptime thisDir()) ++ "/src/sources_all.c") catch unreachable;
|
|
|
|
sources.append((comptime thisDir()) ++ "/src/sources_linux.c") catch unreachable;
|
2022-06-11 17:18:42 -07:00
|
|
|
if (options.x11) {
|
2022-06-24 19:12:45 +02:00
|
|
|
sources.append((comptime thisDir()) ++ "/src/sources_linux_x11.c") catch unreachable;
|
2022-06-11 17:18:42 -07:00
|
|
|
flags.append("-D_GLFW_X11") catch unreachable;
|
2021-07-10 11:25:04 -07:00
|
|
|
}
|
2022-06-11 17:18:42 -07:00
|
|
|
if (options.wayland) {
|
2022-06-24 19:12:45 +02:00
|
|
|
sources.append((comptime thisDir()) ++ "/src/sources_linux_wayland.c") catch unreachable;
|
2022-06-11 17:18:42 -07:00
|
|
|
flags.append("-D_GLFW_WAYLAND") catch unreachable;
|
|
|
|
}
|
2022-06-24 19:12:45 +02:00
|
|
|
flags.append("-I" ++ (comptime thisDir()) ++ "/upstream/glfw/src") catch unreachable;
|
2022-06-11 17:18:42 -07:00
|
|
|
|
|
|
|
lib.addCSourceFiles(sources.items, flags.items);
|
2021-07-06 20:53:10 -07:00
|
|
|
},
|
|
|
|
}
|
2021-10-24 13:50:14 -07:00
|
|
|
linkGLFWDependencies(b, lib, options);
|
|
|
|
lib.install();
|
|
|
|
return lib;
|
2021-07-06 20:53:10 -07:00
|
|
|
}
|
|
|
|
|
2022-06-11 12:05:39 -07:00
|
|
|
fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !void {
|
|
|
|
if (std.process.getEnvVarOwned(allocator, "NO_ENSURE_SUBMODULES")) |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);
|
2022-06-24 19:12:45 +02:00
|
|
|
child.cwd = (comptime thisDir());
|
2022-06-11 12:05:39 -07:00
|
|
|
child.stderr = std.io.getStdErr();
|
|
|
|
child.stdout = std.io.getStdOut();
|
|
|
|
|
|
|
|
_ = try child.spawnAndWait();
|
|
|
|
}
|
|
|
|
|
2021-07-10 01:54:16 -07:00
|
|
|
fn thisDir() []const u8 {
|
|
|
|
return std.fs.path.dirname(@src().file) orelse ".";
|
|
|
|
}
|
|
|
|
|
2021-10-24 13:50:14 -07:00
|
|
|
fn linkGLFWDependencies(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void {
|
2022-06-24 19:12:45 +02:00
|
|
|
const include_dir = std.fs.path.join(b.allocator, &.{ (comptime thisDir()), "upstream/glfw/include" }) catch unreachable;
|
2021-07-06 20:53:10 -07:00
|
|
|
defer b.allocator.free(include_dir);
|
|
|
|
step.addIncludeDir(include_dir);
|
|
|
|
|
2022-06-24 19:12:45 +02:00
|
|
|
const vulkan_include_dir = std.fs.path.join(b.allocator, &.{ (comptime thisDir()), "upstream/vulkan_headers/include" }) catch unreachable;
|
2021-10-16 17:30:56 -07:00
|
|
|
defer b.allocator.free(vulkan_include_dir);
|
|
|
|
step.addIncludeDir(vulkan_include_dir);
|
|
|
|
|
2021-07-26 01:09:19 -07:00
|
|
|
step.linkLibC();
|
2021-11-30 11:29:56 -07:00
|
|
|
// TODO(build-system): pass system SDK options through
|
2021-10-30 21:24:15 -07:00
|
|
|
system_sdk.include(b, step, .{});
|
2021-07-06 20:53:10 -07:00
|
|
|
const target = (std.zig.system.NativeTargetInfo.detect(b.allocator, step.target) catch unreachable).target;
|
|
|
|
switch (target.os.tag) {
|
2021-07-10 01:36:15 -07:00
|
|
|
.windows => {
|
2022-04-12 21:08:30 +02:00
|
|
|
step.linkSystemLibraryName("gdi32");
|
|
|
|
step.linkSystemLibraryName("user32");
|
|
|
|
step.linkSystemLibraryName("shell32");
|
2021-07-25 22:30:20 -07:00
|
|
|
if (options.opengl) {
|
2022-04-12 21:08:30 +02:00
|
|
|
step.linkSystemLibraryName("opengl32");
|
2021-07-25 22:30:20 -07:00
|
|
|
}
|
|
|
|
if (options.gles) {
|
2022-04-12 21:08:30 +02:00
|
|
|
step.linkSystemLibraryName("GLESv3");
|
2021-07-25 22:30:20 -07:00
|
|
|
}
|
2021-07-10 01:36:15 -07:00
|
|
|
},
|
2021-07-06 20:53:10 -07:00
|
|
|
.macos => {
|
|
|
|
step.linkFramework("IOKit");
|
|
|
|
step.linkFramework("CoreFoundation");
|
2021-07-26 01:12:00 -07:00
|
|
|
if (options.metal) {
|
|
|
|
step.linkFramework("Metal");
|
|
|
|
}
|
2021-07-10 01:54:16 -07:00
|
|
|
if (options.opengl) {
|
2021-07-06 20:53:10 -07:00
|
|
|
step.linkFramework("OpenGL");
|
|
|
|
}
|
2022-04-12 21:08:30 +02:00
|
|
|
step.linkSystemLibraryName("objc");
|
2021-11-05 12:20:24 -07:00
|
|
|
step.linkFramework("AppKit");
|
2021-11-26 22:17:42 -07:00
|
|
|
step.linkFramework("CoreServices");
|
|
|
|
step.linkFramework("CoreGraphics");
|
|
|
|
step.linkFramework("Foundation");
|
2021-07-06 20:53:10 -07:00
|
|
|
},
|
|
|
|
else => {
|
|
|
|
// Assume Linux-like
|
2022-06-28 12:39:24 +02:00
|
|
|
if (options.wayland) {
|
|
|
|
step.defineCMacro("WL_MARSHAL_FLAG_DESTROY", null);
|
|
|
|
}
|
2022-06-11 17:18:42 -07:00
|
|
|
if (options.x11) {
|
|
|
|
step.linkSystemLibraryName("X11");
|
|
|
|
step.linkSystemLibraryName("xcb");
|
|
|
|
step.linkSystemLibraryName("Xau");
|
|
|
|
step.linkSystemLibraryName("Xdmcp");
|
2021-07-26 01:29:32 -07:00
|
|
|
}
|
2021-07-29 19:11:08 -07:00
|
|
|
// Note: no need to link against vulkan, GLFW finds it dynamically at runtime.
|
|
|
|
// https://www.glfw.org/docs/3.3/vulkan_guide.html#vulkan_loader
|
2021-07-26 01:29:32 -07:00
|
|
|
if (options.opengl) {
|
2022-04-12 21:08:30 +02:00
|
|
|
step.linkSystemLibraryName("GL");
|
2021-07-26 01:29:32 -07:00
|
|
|
}
|
|
|
|
if (options.gles) {
|
2022-04-12 21:08:30 +02:00
|
|
|
step.linkSystemLibraryName("GLESv3");
|
2021-07-26 01:29:32 -07:00
|
|
|
}
|
2021-07-06 20:53:10 -07:00
|
|
|
},
|
|
|
|
}
|
2021-07-10 01:01:51 -07:00
|
|
|
}
|