From ccd6eee5feb35d04ac24ac37395d155792dd448e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 1 Oct 2023 17:30:11 -0700 Subject: [PATCH] progress on glfw --- build.zig | 57 ++++++++++++++++++++++++++++++++++++++------------- build.zig.zon | 16 +++++++++++++++ 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/build.zig b/build.zig index af2522974..ab1847d15 100644 --- a/build.zig +++ b/build.zig @@ -14,7 +14,6 @@ const LipoStep = @import("src/build/LipoStep.zig"); const XCFrameworkStep = @import("src/build/XCFrameworkStep.zig"); const Version = @import("src/build/Version.zig"); -const glfw = @import("vendor/mach-glfw/build.zig"); const system_sdk = @import("vendor/mach-glfw/system_sdk.zig"); // Do a comptime Zig version requirement. The required Zig version is @@ -659,6 +658,10 @@ fn addDeps( .target = step.target, .optimize = step.optimize, }); + const mach_glfw_dep = b.dependency("mach_glfw", .{ + .target = step.target, + .optimize = step.optimize, + }); const libpng_dep = b.dependency("libpng", .{ .target = step.target, .optimize = step.optimize, @@ -722,7 +725,7 @@ fn addDeps( // We always require the system SDK so that our system headers are available. // This makes things like `os/log.h` available for cross-compiling. - system_sdk.include(b, step, .{}); + try addSystemSDK(b, step); // We always need the Zig packages // TODO: This can't be the right way to use the new Zig modules system, @@ -810,26 +813,29 @@ fn addDeps( // get access to glib for dbus. if (flatpak) step.linkSystemLibrary2("gtk4", dynamic_link_opts); + // We may link GLFW below + const glfw_dep = b.dependency("glfw", .{ + .target = step.target, + .optimize = step.optimize, + .x11 = step.target.isLinux(), + .wayland = step.target.isLinux(), + .metal = step.target.isDarwin(), + }); + switch (app_runtime) { .none => {}, .glfw => { - step.addModule("glfw", glfw.module(b)); - const glfw_opts: glfw.Options = .{ - .metal = step.target.isDarwin(), - .opengl = false, - }; - try glfw.link(b, step, glfw_opts); + step.addModule("glfw", mach_glfw_dep.module("mach-glfw")); + step.linkLibrary(mach_glfw_dep.artifact("mach-glfw")); + step.linkLibrary(glfw_dep.artifact("glfw")); }, .gtk => { // We need glfw for GTK because we use GLFW to get DPI. - step.addModule("glfw", glfw.module(b)); - const glfw_opts: glfw.Options = .{ - .metal = step.target.isDarwin(), - .opengl = false, - }; - try glfw.link(b, step, glfw_opts); + step.addModule("glfw", mach_glfw_dep.module("mach-glfw")); + step.linkLibrary(mach_glfw_dep.artifact("mach-glfw")); + step.linkLibrary(glfw_dep.artifact("glfw")); step.linkSystemLibrary2("gtk4", dynamic_link_opts); }, @@ -839,6 +845,29 @@ fn addDeps( return static_libs; } +/// Adds the proper system headers for the target. +fn addSystemSDK( + b: *std.Build, + step: *std.Build.CompileStep, +) !void { + if (step.target.isDarwin()) { + system_sdk.include(b, step, .{}); + try @import("apple_sdk").addPaths(b, step); + } + + // GLFW requires these on all platforms so we just add them here. It + // doesn't hurt to add them if we don't use GLFW since they're all + // namespaced. + step.linkLibrary(b.dependency("vulkan_headers", .{ + .target = step.target, + .optimize = step.optimize, + }).artifact("vulkan-headers")); + // step.linkLibrary(b.dependency("x11_headers", .{ + // .target = step.target, + // .optimize = step.optimize, + // }).artifact("x11-headers")); +} + fn benchSteps( b: *std.Build, target: std.zig.CrossTarget, diff --git a/build.zig.zon b/build.zig.zon index 287218f8d..961ff84f5 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -7,6 +7,10 @@ .url = "https://github.com/mitchellh/libxev/archive/ecbc161a5dace26a1fd47e494f8be2cfd051cbfb.tar.gz", .hash = "1220f34357168affd9aab1a3fcafcaff093c44beb75ce1d4d4b75490e90729221771", }, + .mach_glfw = .{ + .url = "https://github.com/hexops/mach-glfw/archive/321efd4065b57e31d8ab0bce720852c1d680d443.tar.gz", + .hash = "122002e355cf42b8d257efc95229c9ee6be4cca189c1718f86179cb7c21225beeb75", + }, .zig_objc = .{ .url = "https://github.com/mitchellh/zig-objc/archive/01c16aaeeb674622f4a0e715aeeb16c8ce0bc30e.tar.gz", .hash = "1220f9c919a1171ecf5d097a0d0349e8a9a01b45afa41006dd7eb0afa6243a5fa21f", @@ -16,6 +20,11 @@ .hash = "1220319b42fbc0116f3f198343256018e9f1da9483cef259201afe4ebab0ce0d8f6a", }, + .glfw = .{ + .url = "https://pkg.machengine.org/glfw/92abc63294d389c8f2a546686b6c072930b029e0.tar.gz", + .hash = "12205a6ce386007b97e22ef4b78a4f68c98af2cd6e1fde82589191fda79b9de54d0d", + }, + // C libs .fontconfig = .{ .path = "./pkg/fontconfig" }, .freetype = .{ .path = "./pkg/freetype" }, @@ -26,5 +35,12 @@ .tracy = .{ .path = "./pkg/tracy" }, .utf8proc = .{ .path = "./pkg/utf8proc" }, .zlib = .{ .path = "./pkg/zlib" }, + + // System headers + .apple_sdk = .{ .path = "./pkg/apple-sdk" }, + .vulkan_headers = .{ + .url = "https://pkg.machengine.org/vulkan-headers/fc495148a910ac7817ce0ec2d5948231806f2ac0.tar.gz", + .hash = "12209aeba80369fa8638b82179b47e6742adb98a3a5b01bc518565504a922baad3e4", + }, }, }