Skip to content

Commit

Permalink
adapt build system to zig master
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Ji committed Jan 5, 2025
1 parent 9956299 commit ce63cdf
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 239 deletions.
97 changes: 36 additions & 61 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -107,101 +107,76 @@ pub const BuildOptions = struct {
pub fn createDesktopApp(
b: *Build,
name: []const u8,
root_file: []const u8,
game_root: []const u8,
target: ResolvedTarget,
optimize: std.builtin.Mode,
opt: BuildOptions,
) *Build.Step.Compile {
assert(target.result.os.tag == .windows or target.result.os.tag == .linux or target.result.os.tag == .macos);

// Initialize jok
const jok = initJok(b, target, optimize, opt);

// Initialize sdl2 sdk
const jokmod = getJokModule(b, target, optimize, opt);
const sdk = CrossSDL.init(b);

// Create executable
// Create root module
const builder = getJokBuilder(b, opt);
const exe = builder.addExecutable(.{
.name = name,
const root = b.createModule(.{
.root_source_file = builder.path("src/entrypoints/app.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("jok", jok.module);
exe.root_module.addImport("game", builder.createModule(.{
.root_source_file = b.path(root_file),
const game = builder.createModule(.{
.root_source_file = b.path(game_root),
.imports = &.{
.{ .name = "jok", .module = jok.module },
.{ .name = "jok", .module = jokmod },
},
}));
exe.linkLibrary(jok.lib);
});
root.addImport("jok", jokmod);
root.addImport("game", game);

// Create executable
const exe = builder.addExecutable(.{
.name = name,
.root_module = root,
});
sdk.link(exe, .dynamic);

return exe;
}

// Create jok module and library
fn initJok(
// Create jok Module
fn getJokModule(
b: *Build,
target: ResolvedTarget,
optimize: std.builtin.Mode,
opt: BuildOptions,
) struct {
module: *Build.Module,
lib: *Build.Step.Compile,
} {
// Create module
) *Build.Module {
const builder = getJokBuilder(b, opt);
const bos = builder.addOptions();
bos.addOption(bool, "no_audio", opt.no_audio);
bos.addOption(bool, "use_cp", opt.use_cp);
bos.addOption(bool, "use_nfd", opt.use_nfd);
const module = builder.createModule(.{
const mod = builder.createModule(.{
.root_source_file = builder.path("src/jok.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "build_options", .module = bos.createModule() },
},
});

// Create library
const lib = builder.addStaticLibrary(.{
.name = "jok",
.root_source_file = builder.path("src/jok.zig"),
.target = target,
.optimize = optimize,
});
injectVendorLibraries(builder, lib, target, optimize, opt);

return .{ .module = module, .lib = lib };
}

// Compile vendor libraries into artifact
fn injectVendorLibraries(
b: *Build,
bin: *Build.Step.Compile,
target: ResolvedTarget,
optimize: std.builtin.Mode,
opt: BuildOptions,
) void {
@import("src/vendor/system_sdk/build.zig").inject(b, bin, target, optimize, b.path("src/vendor/system_sdk"));
@import("src/vendor/imgui/build.zig").inject(b, bin, target, optimize, b.path("src/vendor/imgui"));
@import("src/vendor/physfs/build.zig").inject(b, bin, target, optimize, b.path("src/vendor/physfs"));
@import("src/vendor/sdl/build.zig").inject(b, bin, target, optimize, b.path("src/vendor/sdl"));
@import("src/vendor/stb/build.zig").inject(b, bin, target, optimize, b.path("src/vendor/stb"));
@import("src/vendor/svg/build.zig").inject(b, bin, target, optimize, b.path("src/vendor/svg"));
@import("src/vendor/zmath/build.zig").inject(b, bin, target, optimize, b.path("src/vendor/zmath"));
@import("src/vendor/zmesh/build.zig").inject(b, bin, target, optimize, b.path("src/vendor/zmesh"));
@import("src/vendor/znoise/build.zig").inject(b, bin, target, optimize, b.path("src/vendor/znoise"));
if (!opt.no_audio) {
@import("src/vendor/zaudio/build.zig").inject(b, bin, target, optimize, b.path("src/vendor/zaudio"));
}
if (opt.use_cp) {
@import("src/vendor/chipmunk/build.zig").inject(b, bin, target, optimize, b.path("src/vendor/chipmunk"));
}
if (opt.use_nfd) {
@import("src/vendor/nfd/build.zig").inject(b, bin, target, optimize, b.path("src/vendor/nfd"));
}
@import("src/vendor/system_sdk/build.zig").inject(mod, builder.path("src/vendor/system_sdk"));
@import("src/vendor/imgui/build.zig").inject(mod, builder.path("src/vendor/imgui"));
@import("src/vendor/physfs/build.zig").inject(mod, builder.path("src/vendor/physfs"));
@import("src/vendor/sdl/build.zig").inject(mod, builder.path("src/vendor/sdl"));
@import("src/vendor/stb/build.zig").inject(mod, builder.path("src/vendor/stb"));
@import("src/vendor/svg/build.zig").inject(mod, builder.path("src/vendor/svg"));
@import("src/vendor/zmath/build.zig").inject(mod, builder.path("src/vendor/zmath"));
@import("src/vendor/zmesh/build.zig").inject(mod, builder.path("src/vendor/zmesh"));
@import("src/vendor/znoise/build.zig").inject(mod, builder.path("src/vendor/znoise"));
if (!opt.no_audio) @import("src/vendor/zaudio/build.zig").inject(mod, builder.path("src/vendor/zaudio"));
if (opt.use_cp) @import("src/vendor/chipmunk/build.zig").inject(mod, builder.path("src/vendor/chipmunk"));
if (opt.use_nfd) @import("src/vendor/nfd/build.zig").inject(mod, builder.path("src/vendor/nfd"));

return mod;
}

// Get jok's own builder from project's
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = "jok",
.version = "0.30.1",
.version = "0.31.0",
.paths = .{
"README.md",
"build.zig",
Expand Down
14 changes: 4 additions & 10 deletions src/vendor/chipmunk/build.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
const std = @import("std");

pub fn inject(
b: *std.Build,
bin: *std.Build.Step.Compile,
_: std.Build.ResolvedTarget,
optimize: std.builtin.Mode,
dir: std.Build.LazyPath,
) void {
bin.addIncludePath(dir.path(b, "c/include"));
bin.addCSourceFiles(.{
pub fn inject(mod: *std.Build.Module, dir: std.Build.LazyPath) void {
mod.addIncludePath(dir.path(mod.owner, "c/include"));
mod.addCSourceFiles(.{
.root = dir,
.files = &.{
"c/src/chipmunk.c",
Expand Down Expand Up @@ -45,7 +39,7 @@ pub fn inject(
"c/src/cpSweep1D.c",
},
.flags = &.{
if (optimize == .Debug) "" else "-DNDEBUG",
if (mod.optimize.? == .Debug) "" else "-DNDEBUG",
"-DCP_USE_DOUBLES=0",
"-Wno-return-type-c-linkage",
"-fno-sanitize=undefined",
Expand Down
34 changes: 14 additions & 20 deletions src/vendor/imgui/build.zig
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
const std = @import("std");

pub fn inject(
b: *std.Build,
bin: *std.Build.Step.Compile,
target: std.Build.ResolvedTarget,
_: std.builtin.Mode,
dir: std.Build.LazyPath,
) void {
pub fn inject(mod: *std.Build.Module, dir: std.Build.LazyPath) void {
const cflags = &.{
"-fno-sanitize=undefined",
"-Wno-elaborated-enum-base",
"-Wno-error=date-time",
};

const emscripten = target.result.os.tag == .emscripten;
const emscripten = mod.resolved_target.?.result.os.tag == .emscripten;
if (emscripten) {
bin.root_module.addCMacro("__EMSCRIPTEN__", "1");
bin.root_module.addCMacro("__EMSCRIPTEN_major__", "3");
bin.root_module.addCMacro("__EMSCRIPTEN_minor__", "1");
bin.root_module.stack_protector = false;
mod.addCMacro("__EMSCRIPTEN__", "1");
mod.addCMacro("__EMSCRIPTEN_major__", "3");
mod.addCMacro("__EMSCRIPTEN_minor__", "1");
mod.stack_protector = false;
} else {
bin.linkLibC();
if (target.result.abi != .msvc) bin.linkLibCpp();
mod.link_libc = true;
if (mod.resolved_target.?.result.abi != .msvc) mod.link_libcpp = true;
}

bin.addIncludePath(dir.path(b, "c/imgui"));
bin.addIncludePath(dir.path(b, "c/implot"));
bin.addIncludePath(dir.path(b, "c/imguizmo"));
bin.addIncludePath(dir.path(b, "c/node_editor"));
bin.addIncludePath(dir.path(b, "c/impl"));
bin.addCSourceFiles(.{
mod.addIncludePath(dir.path(mod.owner, "c/imgui"));
mod.addIncludePath(dir.path(mod.owner, "c/implot"));
mod.addIncludePath(dir.path(mod.owner, "c/imguizmo"));
mod.addIncludePath(dir.path(mod.owner, "c/node_editor"));
mod.addIncludePath(dir.path(mod.owner, "c/impl"));
mod.addCSourceFiles(.{
.root = dir,
.files = &.{
"c/imgui/imgui.cpp",
Expand Down
56 changes: 25 additions & 31 deletions src/vendor/nfd/build.zig
Original file line number Diff line number Diff line change
@@ -1,49 +1,43 @@
const std = @import("std");

pub fn inject(
b: *std.Build,
bin: *std.Build.Step.Compile,
target: std.Build.ResolvedTarget,
_: std.builtin.Mode,
dir: std.Build.LazyPath,
) void {
pub fn inject(mod: *std.Build.Module, dir: std.Build.LazyPath) void {
const cflags = &.{
"-Wno-return-type-c-linkage",
"-fno-sanitize=undefined",
};

if (bin.rootModuleTarget().os.tag == .windows) {
bin.linkSystemLibrary("shell32");
bin.linkSystemLibrary("ole32");
bin.linkSystemLibrary("uuid"); // needed by MinGW
} else if (target.result.os.tag == .macos) {
bin.linkFramework("AppKit");
} else if (bin.rootModuleTarget().os.tag == .linux) {
bin.linkSystemLibrary("atk-1.0");
bin.linkSystemLibrary("gdk-3");
bin.linkSystemLibrary("gtk-3");
bin.linkSystemLibrary("glib-2.0");
bin.linkSystemLibrary("gobject-2.0");
if (mod.resolved_target.?.result.os.tag == .windows) {
mod.linkSystemLibrary("shell32", .{});
mod.linkSystemLibrary("ole32", .{});
mod.linkSystemLibrary("uuid", .{}); // needed by MinGW
} else if (mod.resolved_target.?.result.os.tag == .macos) {
mod.linkFramework("AppKit", .{});
} else if (mod.resolved_target.?.result.os.tag == .linux) {
mod.linkSystemLibrary("atk-1.0", .{});
mod.linkSystemLibrary("gdk-3", .{});
mod.linkSystemLibrary("gtk-3", .{});
mod.linkSystemLibrary("glib-2.0", .{});
mod.linkSystemLibrary("gobject-2.0", .{});
} else unreachable;

bin.addIncludePath(dir.path(b, "c/include"));
bin.addCSourceFile(.{
.file = dir.path(b, "c/nfd_common.c"),
mod.addIncludePath(dir.path(mod.owner, "c/include"));
mod.addCSourceFile(.{
.file = dir.path(mod.owner, "c/nfd_common.c"),
.flags = cflags,
});
if (target.result.os.tag == .windows) {
bin.addCSourceFile(.{
.file = dir.path(b, "c/nfd_win.cpp"),
if (mod.resolved_target.?.result.os.tag == .windows) {
mod.addCSourceFile(.{
.file = dir.path(mod.owner, "c/nfd_win.cpp"),
.flags = cflags,
});
} else if (target.result.os.tag == .macos) {
bin.addCSourceFile(.{
.file = dir.path(b, "c/nfd_cocoa.m"),
} else if (mod.resolved_target.?.result.os.tag == .macos) {
mod.addCSourceFile(.{
.file = dir.path(mod.owner, "c/nfd_cocoa.m"),
.flags = cflags,
});
} else if (target.result.os.tag == .linux) {
bin.addCSourceFile(.{
.file = dir.path(b, "c/nfd_gtk.c"),
} else if (mod.resolved_target.?.result.os.tag == .linux) {
mod.addCSourceFile(.{
.file = dir.path(mod.owner, "c/nfd_gtk.c"),
.flags = cflags,
});
} else unreachable;
Expand Down
30 changes: 12 additions & 18 deletions src/vendor/physfs/build.zig
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
const std = @import("std");

pub fn inject(
_: *std.Build,
bin: *std.Build.Step.Compile,
target: std.Build.ResolvedTarget,
_: std.builtin.Mode,
dir: std.Build.LazyPath,
) void {
pub fn inject(mod: *std.Build.Module, dir: std.Build.LazyPath) void {
const cflags = &.{
"-Wno-return-type-c-linkage",
"-fno-sanitize=undefined",
};

bin.addCSourceFiles(.{
mod.addCSourceFiles(.{
.root = dir,
.files = &.{
"c/physfs.c",
Expand Down Expand Up @@ -43,21 +37,21 @@ pub fn inject(
},
.flags = cflags,
});
if (target.result.os.tag == .windows) {
bin.linkSystemLibrary("advapi32");
bin.linkSystemLibrary("shell32");
} else if (target.result.os.tag == .macos) {
bin.addCSourceFiles(.{
if (mod.resolved_target.?.result.os.tag == .windows) {
mod.linkSystemLibrary("advapi32", .{});
mod.linkSystemLibrary("shell32", .{});
} else if (mod.resolved_target.?.result.os.tag == .macos) {
mod.addCSourceFiles(.{
.root = dir,
.files = &.{
"c/physfs_platform_apple.m",
},
.flags = cflags,
});
bin.linkSystemLibrary("objc");
bin.linkFramework("IOKit");
bin.linkFramework("Foundation");
} else if (target.result.os.tag == .linux) {
bin.linkSystemLibrary("pthread");
mod.linkSystemLibrary("objc", .{});
mod.linkFramework("IOKit", .{});
mod.linkFramework("Foundation", .{});
} else if (mod.resolved_target.?.result.os.tag == .linux) {
mod.linkSystemLibrary("pthread", .{});
} else unreachable;
}
16 changes: 5 additions & 11 deletions src/vendor/sdl/build.zig
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
const std = @import("std");

pub fn inject(
b: *std.Build,
bin: *std.Build.Step.Compile,
target: std.Build.ResolvedTarget,
_: std.builtin.Mode,
dir: std.Build.LazyPath,
) void {
switch (target.result.os.tag) {
pub fn inject(mod: *std.Build.Module, dir: std.Build.LazyPath) void {
switch (mod.resolved_target.?.result.os.tag) {
.windows => {
bin.addIncludePath(dir.path(b, "c/windows"));
mod.addIncludePath(dir.path(mod.owner, "c/windows"));
},
.macos => {
bin.addIncludePath(dir.path(b, "c/macos"));
mod.addIncludePath(dir.path(mod.owner, "c/macos"));
},
.linux => {
bin.addIncludePath(dir.path(b, "c/linux"));
mod.addIncludePath(dir.path(mod.owner, "c/linux"));
},
else => unreachable,
}
Expand Down
12 changes: 3 additions & 9 deletions src/vendor/stb/build.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
const std = @import("std");

pub fn inject(
b: *std.Build,
bin: *std.Build.Step.Compile,
_: std.Build.ResolvedTarget,
_: std.builtin.Mode,
dir: std.Build.LazyPath,
) void {
bin.addCSourceFile(.{
.file = dir.path(b, "c/stb_wrapper.c"),
pub fn inject(mod: *std.Build.Module, dir: std.Build.LazyPath) void {
mod.addCSourceFile(.{
.file = dir.path(mod.owner, "c/stb_wrapper.c"),
.flags = &.{
"-Wno-return-type-c-linkage",
"-fno-sanitize=undefined",
Expand Down
Loading

0 comments on commit ce63cdf

Please sign in to comment.