From f0e7be542b7b77d4f3b144ea649aa0741953bc86 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Wed, 3 Jul 2024 11:34:13 -0400 Subject: [PATCH] build glfw --- build.zig | 70 +++++-------------------------------- build.zig.zon | 7 ++-- cimgui/build.zig | 10 ++---- cimgui/build.zig.zon | 3 ++ glfw/build.zig | 82 ++++++++++++++++++++++++++++++++++++++++++++ glfw/build.zig.zon | 14 ++++++++ 6 files changed, 115 insertions(+), 71 deletions(-) create mode 100644 glfw/build.zig create mode 100644 glfw/build.zig.zon diff --git a/build.zig b/build.zig index c6d8fbb..b2f4fb4 100644 --- a/build.zig +++ b/build.zig @@ -1,25 +1,30 @@ const std = @import("std"); const vkgen = @import("vulkan-zig"); +const glfw_util = @import("glfw"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); - const optimize = b.standardOptimizeOption(.{}); + const glfw_dep = b.dependency("glfw", .{}); + glfw_util.install(glfw_dep, b); + const vk = b.dependency("vulkan-zig", .{ .registry = @as([]const u8, b.pathFromRoot("reg/vk.xml")), }); - const vkmod = vk.module("vulkan-zig"); const cimgui = b.dependency("cimgui", .{}); const exe = b.addExecutable(.{ - .name = "scratchzig", + .name = "zig-glfw-vulkan", .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); + exe.root_module.addImport("vk", vk.module("vulkan-zig")); exe.root_module.addImport("cimgui", cimgui.module("cimgui")); + glfw_util.link(glfw_dep, exe); + b.installArtifact(exe); const shaders = vkgen.ShaderCompileStep.create( b, @@ -30,18 +35,6 @@ pub fn build(b: *std.Build) void { shaders.add("triangle_frag", "src/shaders/triangle.frag", .{}); exe.root_module.addImport("shaders", shaders.getModule()); - // this requires PKG_CONFIG_PATH to be set. something like: - // ~/.local/lib/pkgconfig/ - exe.linkSystemLibrary2("glfw3", .{ - .needed = true, - .preferred_link_mode = .static, - .use_pkg_config = .force, - }); - exe.linkLibC(); - exe.root_module.addImport("vk", vkmod); - - b.installArtifact(exe); - const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); @@ -51,51 +44,4 @@ pub fn build(b: *std.Build) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - - const exe_unit_tests = b.addTest(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - }); - exe_unit_tests.linkSystemLibrary2("glfw3", .{ - .needed = true, - .preferred_link_mode = .static, - .use_pkg_config = .force, - }); - exe_unit_tests.linkLibC(); - const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); - - const dsa_unit_tests = b.addTest(.{ - .name = "dsa.zig tests", - .root_source_file = b.path("src/dsa.zig"), - .target = target, - .optimize = optimize, - }); - const run_dsa_unit_tests = b.addRunArtifact(dsa_unit_tests); - - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&run_exe_unit_tests.step); - test_step.dependOn(&run_dsa_unit_tests.step); - - const inspect = b.addExecutable(.{ - .name = "vkinspect", - .root_source_file = b.path("src/inspect.zig"), - .target = target, - .optimize = optimize, - }); - inspect.linkSystemLibrary2("vulkan", .{ - .needed = true, - .preferred_link_mode = .dynamic, - }); - inspect.linkSystemLibrary2("glfw3", .{ - .needed = true, - .preferred_link_mode = .static, - .use_pkg_config = .force, - }); - exe_unit_tests.linkLibC(); - inspect.root_module.addImport("vk", vkmod); - inspect.linkLibC(); - const run_inspect = b.addRunArtifact(inspect); - const inspect_step = b.step("vki", "Vulkan Inspect"); - inspect_step.dependOn(&run_inspect.step); } diff --git a/build.zig.zon b/build.zig.zon index 6942b2e..eb9a305 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,5 +1,5 @@ .{ - .name = "scratchzig", + .name = "zig-glfw-vulkan", .version = "0.0.0", .dependencies = .{ @@ -7,8 +7,11 @@ .url = "https://github.com/Snektron/vulkan-zig/archive/f2c2e0ff80374563357cc4fe72bf7d8a2c956824.tar.gz", .hash = "1220cf0972c6fe05437c1a8689b955084385eb7ca1f8c14010d49ca5a89570a5d90d", }, + .glfw = .{ + .path = "glfw", + }, .cimgui = .{ - .path="cimgui", + .path = "cimgui", }, }, diff --git a/cimgui/build.zig b/cimgui/build.zig index 11349b1..19072a6 100644 --- a/cimgui/build.zig +++ b/cimgui/build.zig @@ -1,5 +1,6 @@ const std = @import("std"); const vkgen = @import("vulkan-zig"); +const glfw_util = @import("glfw"); pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); @@ -7,6 +8,7 @@ pub fn build(b: *std.Build) !void { const imgui_dep = b.dependency("imgui", .{}); const cimgui_dep = b.dependency("cimgui", .{}); + const glfw_dep = b.dependency("glfw", .{}); const luajit = try b.findProgram(&.{"luajit"}, &.{}); @@ -74,13 +76,7 @@ pub fn build(b: *std.Build) !void { }); cimgui.installHeader(copy.getDirectory().path(b, "cimgui.h"), "cimgui.h"); cimgui.installHeader(copy.getDirectory().path(b, "cimgui_impl.h"), "cimgui_impl.h"); - - // todo separate impls into different shared libraries for easier linkage - cimgui.linkSystemLibrary2("glfw3", .{ - .needed = true, - .preferred_link_mode = .static, - .use_pkg_config = .force, - }); + glfw_util.link(glfw_dep, cimgui); b.installArtifact(cimgui); diff --git a/cimgui/build.zig.zon b/cimgui/build.zig.zon index 935c753..e14b49c 100644 --- a/cimgui/build.zig.zon +++ b/cimgui/build.zig.zon @@ -11,6 +11,9 @@ .url = "https://github.com/ocornut/imgui/archive/refs/tags/v1.90.8-docking.tar.gz", .hash = "122065151b97161e25abb71c9df2fd9fba42aaca8c33d689a480b883d82411c8fabe", }, + .glfw = .{ + .path = "../glfw", + } }, .paths = .{ diff --git a/glfw/build.zig b/glfw/build.zig new file mode 100644 index 0000000..f399342 --- /dev/null +++ b/glfw/build.zig @@ -0,0 +1,82 @@ +const std = @import("std"); + +pub fn link(dep: *std.Build.Dependency, compile: *std.Build.Step.Compile) void { + compile.step.dependOn(dep.builder.getInstallStep()); + compile.linkLibC(); + compile.root_module.addLibraryPath(.{ .cwd_relative = dep.builder.getInstallPath(.lib, "") }); + compile.root_module.addIncludePath(.{ .cwd_relative = dep.builder.getInstallPath(.header, "") }); + compile.root_module.addRPathSpecial("$ORIGIN/../lib"); + + compile.root_module.linkSystemLibrary("glfw", .{}); + compile.root_module.linkSystemLibrary("rt", .{}); + compile.root_module.linkSystemLibrary("m", .{}); + compile.root_module.linkSystemLibrary("dl", .{}); +} + +pub fn install(dep: *std.Build.Dependency, owner: *std.Build) void { + const install_libs = owner.addInstallDirectory(.{ + .include_extensions = &.{ ".so", ".dll" }, + .install_subdir = "", + .install_dir = .lib, + .source_dir = .{ + .cwd_relative = dep.builder.getInstallPath(.lib, ""), + }, + }); + install_libs.step.dependOn(dep.builder.getInstallStep()); + owner.getInstallStep().dependOn(&install_libs.step); +} + +pub fn build(b: *std.Build) void { + // todo target into toolchain file + // const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const cmake = b.findProgram(&.{"cmake"}, &.{}) catch @panic("missing cmake"); + + const cmake_build_type = switch (optimize) { + .ReleaseFast => "Release", + .ReleaseSafe => "RelWithDebInfo", + .ReleaseSmall => "MinSizeRel", + .Debug => "Debug", + }; + + const config_step = b.addSystemCommand(&.{ + cmake, + "-DBUILD_SHARED_LIBS=ON", + b.fmt("-DCMAKE_BUILD_TYPE={s}", .{cmake_build_type}), + b.fmt("-DCMAKE_INSTALL_PREFIX={s}", .{b.getInstallPath(.prefix, "")}), + "-DGLFW_BUILD_DOCS=OFF", + "-DGLFW_BUILD_EXAMPLES=OFF", + "-DGLFW_BUILD_TESTS=OFF", + "-DGLFW_BUILD_X11=ON", // todo arg + "-DGLFW_BUILD_WAYLAND=OFF", // todo arg + "-DGLFW_INSTALL=ON", + }); + // config_step.setEnvironmentVariable("CC", b.fmt("{s} cc", .{b.graph.zig_exe})); + // config_step.setEnvironmentVariable("CXX", b.fmt("{s} c++", .{b.graph.zig_exe})); + config_step.addArg("-S"); + config_step.addDirectoryArg(b.dependency("glfw_real", .{}).path("")); + config_step.addArg("-B"); + const build_dir = config_step.addOutputDirectoryArg("glfw_build"); + + _ = config_step.captureStdOut(); + config_step.has_side_effects = true; + + const build_step = b.addSystemCommand(&.{cmake}); + build_step.step.dependOn(&config_step.step); + build_step.addArg("--build"); + build_step.addDirectoryArg(build_dir); + + _ = build_step.captureStdOut(); + build_step.has_side_effects = true; + + const install_step = b.addSystemCommand(&.{cmake}); + install_step.step.dependOn(&build_step.step); + install_step.addArg("--install"); + install_step.addDirectoryArg(build_dir); + + _ = install_step.captureStdOut(); + install_step.has_side_effects = true; + + b.getInstallStep().dependOn(&install_step.step); +} diff --git a/glfw/build.zig.zon b/glfw/build.zig.zon new file mode 100644 index 0000000..e5e7776 --- /dev/null +++ b/glfw/build.zig.zon @@ -0,0 +1,14 @@ +.{ + .name = "glfw", + .version = "3.4.0", + .dependencies = .{ + .glfw_real = .{ + .url = "https://github.com/glfw/glfw/releases/download/3.4/glfw-3.4.zip", + .hash = "1220625fa7ce79733c6889844cb02ea1f6e4b81b46a3fabacec181714879947f4abd", + }, + }, + .paths = .{ + "build.zig", + "build.zig.zon", + }, +}