Browse Source

update zig build to latest master (#2910)

also, adds package manager support
star-tek-mb 2 years ago
parent
commit
ff70a04bf5
3 changed files with 47 additions and 20 deletions
  1. 18 0
      build.zig
  2. 18 16
      examples/build.zig
  3. 11 4
      src/build.zig

+ 18 - 0
build.zig

@@ -0,0 +1,18 @@
+const std = @import("std");
+const raylib = @import("src/build.zig");
+
+pub fn build(b: *std.Build) void {
+    // Standard target options allows the person running `zig build` to choose
+    // what target to build for. Here we do not override the defaults, which
+    // means any target is allowed, and the default is native. Other options
+    // for restricting supported target set are available.
+    const target = b.standardTargetOptions(.{});
+    // Standard optimization options allow the person running `zig build` to select
+    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
+    // set a preferred release mode, allowing the user to decide how to optimize.
+    const optimize = b.standardOptimizeOption(.{});
+
+    const lib = raylib.addRaylib(b, target, optimize);
+    lib.installHeader("src/raylib.h", "raylib.h");
+    lib.install();
+}

+ 18 - 16
examples/build.zig

@@ -1,15 +1,11 @@
 const std = @import("std");
 const std = @import("std");
 const builtin = @import("builtin");
 const builtin = @import("builtin");
 
 
-fn add_module(comptime module: []const u8, b: *std.build.Builder, target: std.zig.CrossTarget) !*std.build.Step {
+fn add_module(comptime module: []const u8, b: *std.build.Builder, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode) !*std.build.Step {
     if (target.getOsTag() == .emscripten) {
     if (target.getOsTag() == .emscripten) {
         @panic("Emscripten building via Zig unsupported");
         @panic("Emscripten building via Zig unsupported");
     }
     }
 
 
-    // Standard release options allow the person running `zig build` to select
-    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
-    const mode = b.standardReleaseOptions();
-
     const all = b.step(module, "All " ++ module ++ " examples");
     const all = b.step(module, "All " ++ module ++ " examples");
     const dir = try std.fs.cwd().openIterableDir(module, .{});
     const dir = try std.fs.cwd().openIterableDir(module, .{});
     var iter = dir.iterate();
     var iter = dir.iterate();
@@ -22,10 +18,12 @@ fn add_module(comptime module: []const u8, b: *std.build.Builder, target: std.zi
         // zig's mingw headers do not include pthread.h
         // zig's mingw headers do not include pthread.h
         if (std.mem.eql(u8, "core_loading_thread", name) and target.getOsTag() == .windows) continue;
         if (std.mem.eql(u8, "core_loading_thread", name) and target.getOsTag() == .windows) continue;
 
 
-        const exe = b.addExecutable(name, null);
+        const exe = b.addExecutable(.{
+            .name = name,
+            .target = target,
+            .optimize = optimize,
+        });
         exe.addCSourceFile(path, &[_][]const u8{});
         exe.addCSourceFile(path, &[_][]const u8{});
-        exe.setTarget(target);
-        exe.setBuildMode(mode);
         exe.linkLibC();
         exe.linkLibC();
         exe.addObjectFile(switch (target.getOsTag()) {
         exe.addObjectFile(switch (target.getOsTag()) {
             .windows => "../src/raylib.lib",
             .windows => "../src/raylib.lib",
@@ -89,15 +87,19 @@ pub fn build(b: *std.build.Builder) !void {
     // means any target is allowed, and the default is native. Other options
     // means any target is allowed, and the default is native. Other options
     // for restricting supported target set are available.
     // for restricting supported target set are available.
     const target = b.standardTargetOptions(.{});
     const target = b.standardTargetOptions(.{});
+    // Standard optimization options allow the person running `zig build` to select
+    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
+    // set a preferred release mode, allowing the user to decide how to optimize.
+    const optimize = b.standardOptimizeOption(.{});
 
 
     const all = b.getInstallStep();
     const all = b.getInstallStep();
 
 
-    all.dependOn(try add_module("audio", b, target));
-    all.dependOn(try add_module("core", b, target));
-    all.dependOn(try add_module("models", b, target));
-    all.dependOn(try add_module("others", b, target));
-    all.dependOn(try add_module("shaders", b, target));
-    all.dependOn(try add_module("shapes", b, target));
-    all.dependOn(try add_module("text", b, target));
-    all.dependOn(try add_module("textures", b, target));
+    all.dependOn(try add_module("audio", b, target, optimize));
+    all.dependOn(try add_module("core", b, target, optimize));
+    all.dependOn(try add_module("models", b, target, optimize));
+    all.dependOn(try add_module("others", b, target, optimize));
+    all.dependOn(try add_module("shaders", b, target, optimize));
+    all.dependOn(try add_module("shapes", b, target, optimize));
+    all.dependOn(try add_module("text", b, target, optimize));
+    all.dependOn(try add_module("textures", b, target, optimize));
 }
 }

+ 11 - 4
src/build.zig

@@ -1,6 +1,6 @@
 const std = @import("std");
 const std = @import("std");
 
 
-pub fn addRaylib(b: *std.build.Builder, target: std.zig.CrossTarget) *std.build.LibExeObjStep {
+pub fn addRaylib(b: *std.build.Builder, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode) *std.build.LibExeObjStep {
     const raylib_flags = &[_][]const u8{
     const raylib_flags = &[_][]const u8{
         "-std=gnu99",
         "-std=gnu99",
         "-D_GNU_SOURCE",
         "-D_GNU_SOURCE",
@@ -8,8 +8,11 @@ pub fn addRaylib(b: *std.build.Builder, target: std.zig.CrossTarget) *std.build.
         "-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/1891
         "-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/1891
     };
     };
 
 
-    const raylib = b.addStaticLibrary("raylib", null);
-    raylib.setTarget(target);
+    const raylib = b.addStaticLibrary(.{
+        .name = "raylib",
+        .target = target,
+        .optimize = optimize,
+    });
     raylib.linkLibC();
     raylib.linkLibC();
 
 
     raylib.addIncludePath(srcdir ++ "/external/glfw/include");
     raylib.addIncludePath(srcdir ++ "/external/glfw/include");
@@ -106,8 +109,12 @@ pub fn build(b: *std.build.Builder) void {
     // means any target is allowed, and the default is native. Other options
     // means any target is allowed, and the default is native. Other options
     // for restricting supported target set are available.
     // for restricting supported target set are available.
     const target = b.standardTargetOptions(.{});
     const target = b.standardTargetOptions(.{});
+    // Standard optimization options allow the person running `zig build` to select
+    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
+    // set a preferred release mode, allowing the user to decide how to optimize.
+    const optimize = b.standardOptimizeOption(.{});
 
 
-    const lib = addRaylib(b, target);
+    const lib = addRaylib(b, target, optimize);
     lib.setOutputDir(srcdir);
     lib.setOutputDir(srcdir);
     lib.install();
     lib.install();
 }
 }