zig package manager - build glfw cmake project
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.idea/
|
||||
zig-out/
|
||||
zig-cache/
|
47
README.md
Normal file
47
README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
Fetch GLFW via experimental zig package manager `build.zig.zon`
|
||||
|
||||
Invoke CMake commands to build and install GLFW to zig-cache
|
||||
|
||||
Add Zig executable which includes and links to GLFW.
|
||||
|
||||
|
||||
Drawing inspiration from CMake ExternalProject.
|
||||
|
||||
----
|
||||
|
||||
Ideally there would be some mechanism like `b.addCMakeProject` with options to:
|
||||
|
||||
- provide CMake CLI or CACHE_ARGS
|
||||
- infer source/config/install dirs
|
||||
- infer installed libraries and linkage (pkgconfig?)
|
||||
- inf
|
||||
- set CMAKE_CXX_COMPILER to `zig c++`
|
||||
- set CMAKE_C_COMPILER to `zig c`
|
||||
- Infer modules/artifacts from installed targets.
|
||||
- inspect/evaluate `glfw3Targets.cmake` and produce a module/artifact for each cmake target
|
||||
|
||||
---
|
||||
|
||||
I imagine usage like:
|
||||
|
||||
```zig
|
||||
const glfw = b.dependency("glfw", .{
|
||||
// automatically use zig for CMAKE_C_COMPILER, CMAKE_CXX_COMPILER
|
||||
.target = target, // pass on as compiler flags
|
||||
.optimize = optimize, // convert to CMAKE_BUILD_TYPE
|
||||
.cache_args = .{
|
||||
"GLFW_BUILD_SHARED_LIBS:BOOL=OFF",
|
||||
"GLFW_BUILD_WAYLAND:BOOL=OFF",
|
||||
"GLFW_BUILD_X11:BOOL=ON",
|
||||
"GLFW_BUILD_TESTS:BOOL=OFF",
|
||||
},
|
||||
});
|
||||
|
||||
exe.addModule(
|
||||
"glfw", // the name of the @import which defers to @cImport.
|
||||
glfw.module("glfw"), // the name of the cmake target
|
||||
);
|
||||
exe.linkLibrary(
|
||||
glfw.artifact("glfw"), // the name of the cmake target
|
||||
);
|
||||
```
|
64
build.zig
Normal file
64
build.zig
Normal file
@@ -0,0 +1,64 @@
|
||||
const std = @import("std");
|
||||
|
||||
const CMake = struct {};
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const glfw = b.dependency("glfw", .{});
|
||||
|
||||
const source_dir = glfw.builder.build_root.path orelse unreachable;
|
||||
|
||||
const basename = std.fs.path.basename(glfw.builder.install_prefix);
|
||||
const build_dir = b.cache_root.join(b.allocator, &.{ "cmake", basename }) catch unreachable;
|
||||
const install_dir = b.cache_root.join(b.allocator, &.{ "cmake-i", basename }) catch unreachable;
|
||||
|
||||
const config_glfw = glfw.builder.addSystemCommand(&.{
|
||||
"cmake",
|
||||
"-S",
|
||||
source_dir,
|
||||
"-B",
|
||||
build_dir,
|
||||
std.fmt.allocPrint(b.allocator, "-DCMAKE_INSTALL_PREFIX:PATH={s}", .{install_dir}) catch unreachable,
|
||||
"-DCMAKE_INSTALL_MESSAGE=LAZY",
|
||||
"-DCMAKE_MESSAGE_LOG_LEVEL=WARNING",
|
||||
"-DCMAKE_BUILD_TYPE=Release",
|
||||
"-DGLFW_BUILD_WAYLAND=OFF",
|
||||
"-DGLFW_BUILD_X11=ON",
|
||||
"-DBUILD_SHARED_LIBS=OFF",
|
||||
"-DGLFW_BUILD_TESTS=OFF",
|
||||
"-DGLFW_BUILD_DOCS=OFF",
|
||||
"-DGLFW_INSTALL=ON",
|
||||
});
|
||||
|
||||
const build_glfw = glfw.builder.addSystemCommand(&.{
|
||||
"cmake", "--build", build_dir, "-j", "--", "--quiet",
|
||||
});
|
||||
build_glfw.step.dependOn(&config_glfw.step);
|
||||
|
||||
const install_glfw = glfw.builder.addSystemCommand(&.{ "cmake", "--install", build_dir });
|
||||
install_glfw.step.dependOn(&build_glfw.step);
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "learnzig",
|
||||
.root_source_file = .{ .path = "src/main.zig" },
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
b.installArtifact(exe);
|
||||
exe.step.dependOn(&install_glfw.step);
|
||||
exe.addLibraryPath(.{ .path = b.fmt("{s}/lib", .{install_dir}) });
|
||||
exe.addIncludePath(.{ .path = b.fmt("{s}/include", .{install_dir}) });
|
||||
exe.linkSystemLibrary("glfw3");
|
||||
exe.linkSystemLibrary("rt");
|
||||
exe.linkSystemLibrary("m");
|
||||
exe.linkSystemLibrary("dl");
|
||||
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
}
|
15
build.zig.zon
Normal file
15
build.zig.zon
Normal file
@@ -0,0 +1,15 @@
|
||||
.{
|
||||
.name = "learnzig",
|
||||
.version = "0.0.0",
|
||||
|
||||
.dependencies = .{
|
||||
.glfw = .{
|
||||
.url = "https://github.com/glfw/glfw/archive/refs/tags/3.4.tar.gz",
|
||||
.hash = "1220c46ebcac607065f942193b82a21b87213fca4b0d50c83461f8f5e22955fcb88f",
|
||||
},
|
||||
},
|
||||
|
||||
.paths = .{
|
||||
"",
|
||||
},
|
||||
}
|
25
src/main.zig
Normal file
25
src/main.zig
Normal file
@@ -0,0 +1,25 @@
|
||||
const std = @import("std");
|
||||
|
||||
const c = @cImport({
|
||||
@cInclude("GLFW/glfw3.h");
|
||||
});
|
||||
|
||||
pub fn main() !void {
|
||||
if (c.glfwInit() == 0) {
|
||||
@panic("GLFW Initialization failed");
|
||||
}
|
||||
defer c.glfwTerminate();
|
||||
|
||||
// todo error callback
|
||||
|
||||
const win = c.glfwCreateWindow(640, 480, "Hello Zig!", null, null);
|
||||
if (win == null) {
|
||||
@panic("GLFW window creation failed");
|
||||
}
|
||||
defer c.glfwDestroyWindow(win);
|
||||
|
||||
while (c.glfwWindowShouldClose(win) == 0) {
|
||||
c.glfwPollEvents();
|
||||
c.glfwSwapBuffers(win);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user