build glfw

This commit is contained in:
David Allemang
2024-07-03 11:34:13 -04:00
parent 8a9bbee536
commit f0e7be542b
6 changed files with 115 additions and 71 deletions

View File

@@ -1,25 +1,30 @@
const std = @import("std"); const std = @import("std");
const vkgen = @import("vulkan-zig"); const vkgen = @import("vulkan-zig");
const glfw_util = @import("glfw");
pub fn build(b: *std.Build) void { pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const glfw_dep = b.dependency("glfw", .{});
glfw_util.install(glfw_dep, b);
const vk = b.dependency("vulkan-zig", .{ const vk = b.dependency("vulkan-zig", .{
.registry = @as([]const u8, b.pathFromRoot("reg/vk.xml")), .registry = @as([]const u8, b.pathFromRoot("reg/vk.xml")),
}); });
const vkmod = vk.module("vulkan-zig");
const cimgui = b.dependency("cimgui", .{}); const cimgui = b.dependency("cimgui", .{});
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "scratchzig", .name = "zig-glfw-vulkan",
.root_source_file = b.path("src/main.zig"), .root_source_file = b.path("src/main.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
exe.root_module.addImport("vk", vk.module("vulkan-zig"));
exe.root_module.addImport("cimgui", cimgui.module("cimgui")); exe.root_module.addImport("cimgui", cimgui.module("cimgui"));
glfw_util.link(glfw_dep, exe);
b.installArtifact(exe);
const shaders = vkgen.ShaderCompileStep.create( const shaders = vkgen.ShaderCompileStep.create(
b, b,
@@ -30,18 +35,6 @@ pub fn build(b: *std.Build) void {
shaders.add("triangle_frag", "src/shaders/triangle.frag", .{}); shaders.add("triangle_frag", "src/shaders/triangle.frag", .{});
exe.root_module.addImport("shaders", shaders.getModule()); 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); const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep()); 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"); const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step); 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);
} }

View File

@@ -1,5 +1,5 @@
.{ .{
.name = "scratchzig", .name = "zig-glfw-vulkan",
.version = "0.0.0", .version = "0.0.0",
.dependencies = .{ .dependencies = .{
@@ -7,6 +7,9 @@
.url = "https://github.com/Snektron/vulkan-zig/archive/f2c2e0ff80374563357cc4fe72bf7d8a2c956824.tar.gz", .url = "https://github.com/Snektron/vulkan-zig/archive/f2c2e0ff80374563357cc4fe72bf7d8a2c956824.tar.gz",
.hash = "1220cf0972c6fe05437c1a8689b955084385eb7ca1f8c14010d49ca5a89570a5d90d", .hash = "1220cf0972c6fe05437c1a8689b955084385eb7ca1f8c14010d49ca5a89570a5d90d",
}, },
.glfw = .{
.path = "glfw",
},
.cimgui = .{ .cimgui = .{
.path = "cimgui", .path = "cimgui",
}, },

View File

@@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const vkgen = @import("vulkan-zig"); const vkgen = @import("vulkan-zig");
const glfw_util = @import("glfw");
pub fn build(b: *std.Build) !void { pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
@@ -7,6 +8,7 @@ pub fn build(b: *std.Build) !void {
const imgui_dep = b.dependency("imgui", .{}); const imgui_dep = b.dependency("imgui", .{});
const cimgui_dep = b.dependency("cimgui", .{}); const cimgui_dep = b.dependency("cimgui", .{});
const glfw_dep = b.dependency("glfw", .{});
const luajit = try b.findProgram(&.{"luajit"}, &.{}); 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.h"), "cimgui.h");
cimgui.installHeader(copy.getDirectory().path(b, "cimgui_impl.h"), "cimgui_impl.h"); cimgui.installHeader(copy.getDirectory().path(b, "cimgui_impl.h"), "cimgui_impl.h");
glfw_util.link(glfw_dep, cimgui);
// todo separate impls into different shared libraries for easier linkage
cimgui.linkSystemLibrary2("glfw3", .{
.needed = true,
.preferred_link_mode = .static,
.use_pkg_config = .force,
});
b.installArtifact(cimgui); b.installArtifact(cimgui);

View File

@@ -11,6 +11,9 @@
.url = "https://github.com/ocornut/imgui/archive/refs/tags/v1.90.8-docking.tar.gz", .url = "https://github.com/ocornut/imgui/archive/refs/tags/v1.90.8-docking.tar.gz",
.hash = "122065151b97161e25abb71c9df2fd9fba42aaca8c33d689a480b883d82411c8fabe", .hash = "122065151b97161e25abb71c9df2fd9fba42aaca8c33d689a480b883d82411c8fabe",
}, },
.glfw = .{
.path = "../glfw",
}
}, },
.paths = .{ .paths = .{

82
glfw/build.zig Normal file
View File

@@ -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);
}

14
glfw/build.zig.zon Normal file
View File

@@ -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",
},
}