Skip to content

Commit

Permalink
mach pt.1, segfaults the compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Mar 6, 2024
1 parent 1c5704b commit 6837fc8
Show file tree
Hide file tree
Showing 23 changed files with 917 additions and 7,093 deletions.
255 changes: 104 additions & 151 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,183 +1,133 @@
const std = @import("std");
const zmath = @import("libs/zmath/build.zig");
const bass = @import("libs/zig-bass/build.zig");
const wgpu = @import("wgpu.zig");
const cimgui = @import("libs/cimgui/build.zig");
const iconv = @import("libs/iconv-zig/build.zig");
const ImageProcessor = @import("image_processor.zig");
const builtin = @import("builtin");
const mach_core = @import("mach_core");

pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const exe = b.addExecutable(.{
.name = "ztyping",
.root_source_file = .{ .path = "game/main.zig" },
const mach_core_dep = b.dependency("mach_core", .{
.target = target,
.optimize = optimize,
});

b.installArtifact(exe);

//Link libc and libc++, libc++ for wgpu
exe.linkLibC();
exe.linkLibCpp();

if (target.isDarwin()) {
exe.addIncludePath(.{ .path = root_path ++ "libs/system-sdk/macos12/usr/include" });
exe.addFrameworkPath(.{ .path = root_path ++ "libs/system-sdk/macos12/System/Library/Frameworks" });
exe.addLibraryPath(.{ .path = root_path ++ "libs/system-sdk/macos12/usr/lib" });

if (!builtin.target.isDarwin()) {
b.sysroot = root_path ++ "libs/system-sdk/macos12";
}

exe.linkFramework("Metal");
exe.linkFramework("QuartzCore");
exe.linkFramework("Foundation");

exe.linkSystemLibrary("objc");
}

exe.addCSourceFile(.{ .file = .{ .path = root_path ++ "libs/stb/impl_stb_truetype.c" }, .flags = &.{} });
exe.addIncludePath(.{ .path = root_path ++ "libs/stb" });

if (target.isLinux() and !target.isNative()) {
exe.addIncludePath(.{ .path = root_path ++ "libs/system-sdk/linux/include" });
exe.addLibraryPath(.{ .path = b.fmt(root_path ++ "libs/system-sdk/linux/lib/{s}", .{try target.linuxTriple(b.allocator)}) });
}

{ //zig-bass
const zig_bass = b.addModule("bass", .{ .source_file = .{ .path = root_path ++ "libs/zig-bass/src/bass.zig" } });
exe.addModule("bass", zig_bass);

bass.linkBass(exe);
bass.installBass(b, target);

exe.addIncludePath(.{ .path = root_path ++ "libs/zig-bass/src" });
} //zig-bass

{ //zmath
const zmath_pkg = zmath.package(b, target, optimize, .{
.options = .{ .enable_cross_platform_determinism = true },
});
const zig_bass = b.addModule("bass", .{
.root_source_file = .{ .path = root_path ++ "libs/zig-bass/src/bass.zig" },
});

zmath_pkg.link(exe);
} //zmath
const stb_truetype = b.addModule("stb_truetype", .{
.optimize = optimize,
.target = target,
.root_source_file = .{ .path = "game/stb_truetype.zig" },
});

//SDL
stb_truetype.addCSourceFile(.{ .file = .{ .path = root_path ++ "libs/stb/impl_stb_truetype.c" }, .flags = &.{} });
stb_truetype.addIncludePath(.{ .path = root_path ++ "libs/stb" });

const sdl_pkg = b.dependency("SDL2", .{
const zigimg_module = b.dependency("zigimg", .{}).module("zigimg");
const app = try mach_core.App.init(b, mach_core_dep.builder, .{
.name = "ztyping",
.src = "game/app.zig",
.target = target,
.optimize = optimize,
.osx_sdk_path = @as([]const u8, root_path ++ "libs/system-sdk/macos12"),
.linux_sdk_path = @as([]const u8, root_path ++ "libs/system-sdk/linux"),
.disable_audio = true,
.disable_render = true,
.disable_joystick = true,
// .disable_video_sub_implementations = true,
.deps = &[_]std.Build.Module.Import{
.{
.name = "mach",
.module = b.dependency("mach", .{
.target = target,
.optimize = optimize,
}).module("mach"),
},
.{
.name = "zigimg",
.module = zigimg_module,
},
.{
.name = "bass",
.module = zig_bass,
},
.{
.name = "stb_truetype",
.module = stb_truetype,
},
},
});
const sdl_lib = sdl_pkg.artifact("SDL2");

//Link SDL
exe.linkLibrary(sdl_lib);
//Install the SDL headers
exe.installLibraryHeaders(sdl_lib);

//Add all the SDL C macros to our executable too
try exe.c_macros.appendSlice(sdl_lib.c_macros.items);
const app_module = app.compile.root_module.import_table.get("app").?;

//SDL
app_module.resolved_target = target;

{ //wgpu
const wgpu_from_source = b.option(bool, "wgpu_from_source", "Compile WGPU from source") orelse false;
bass.linkBass(app_module);
bass.installBass(b, target.result);

if (wgpu_from_source) {
const wgpu_lib = try wgpu.create_wgpu(b, target, optimize);
app.compile.addIncludePath(.{ .path = root_path ++ "libs/zig-bass/src" });

if (target.getOsTag() == .windows) {
@panic("TODO"); //we need to force dynamic linking here.
} else {
exe.addObjectFile(.{ .path = wgpu_lib });
}
} else {
var wgpu_bin_path = std.ArrayList(u8).init(b.allocator);
// if (target.isDarwin()) {
// exe.addIncludePath(.{ .path = root_path ++ "libs/system-sdk/macos12/usr/include" });
// exe.addFrameworkPath(.{ .path = root_path ++ "libs/system-sdk/macos12/System/Library/Frameworks" });
// exe.addLibraryPath(.{ .path = root_path ++ "libs/system-sdk/macos12/usr/lib" });

try wgpu_bin_path.appendSlice(root_path ++ "libs/wgpu-native-bin/");
// if (!builtin.target.isDarwin()) {
// b.sysroot = root_path ++ "libs/system-sdk/macos12";
// }

try wgpu_bin_path.appendSlice(@tagName(target.getOsTag()));
try wgpu_bin_path.append('-');
try wgpu_bin_path.appendSlice(@tagName(target.getCpuArch()));
// exe.linkFramework("Metal");
// exe.linkFramework("QuartzCore");
// exe.linkFramework("Foundation");

const wgpu_bin_path_slice = try wgpu_bin_path.toOwnedSlice();
// exe.linkSystemLibrary("objc");
// }

exe.addLibraryPath(.{ .path = wgpu_bin_path_slice });
exe.linkSystemLibrary("wgpu_native");
// if (target.isLinux() and !target.isNative()) {
// exe.addIncludePath(.{ .path = root_path ++ "libs/system-sdk/linux/include" });
// exe.addLibraryPath(.{ .path = b.fmt(root_path ++ "libs/system-sdk/linux/lib/{s}", .{try target.linuxTriple(b.allocator)}) });
// }

if (target.isWindows()) {
b.installFile(try std.mem.concat(b.allocator, u8, &.{ wgpu_bin_path_slice, "/wgpu_native.dll" }), "bin/wgpu_native.dll");
}
}

//On windows we need to link these libraries for wgpu to work
if (target.getOsTag() == .windows) {
exe.linkSystemLibrary("ws2_32");
exe.linkSystemLibrary("userenv");
exe.linkSystemLibrary("bcrypt");
exe.linkSystemLibrary("d3dcompiler_47");
}

exe.addIncludePath(.{ .path = "libs/wgpu-native/ffi" });
} //wgpu

{ //cimgui
const cimgui_lib = try cimgui.create_cimgui(b, target, optimize);
{ //zig-bass
} //zig-bass

cimgui_lib.installLibraryHeaders(sdl_lib);
cimgui_lib.linkLibrary(sdl_lib);
try cimgui_lib.lib_paths.appendSlice(sdl_lib.lib_paths.items);
try cimgui_lib.include_dirs.appendSlice(sdl_lib.include_dirs.items);
// { //cimgui
// const cimgui_lib = try cimgui.create_cimgui(b, target, optimize);

exe.addIncludePath(.{ .path = "libs/cimgui/" });
// cimgui_lib.installLibraryHeaders(sdl_lib);
// cimgui_lib.linkLibrary(sdl_lib);
// try cimgui_lib.lib_paths.appendSlice(sdl_lib.lib_paths.items);
// try cimgui_lib.include_dirs.appendSlice(sdl_lib.include_dirs.items);

exe.linkLibrary(cimgui_lib);
} //cimgui
// exe.addIncludePath(.{ .path = "libs/cimgui/" });

//zigimg
const zigimg_pkg = b.dependency("zigimg", .{
.target = target,
.optimize = optimize,
});
const zigimg_module = zigimg_pkg.module("zigimg");
exe.addModule("zigimg", zigimg_module);
//zigimg
// exe.linkLibrary(cimgui_lib);
// } //cimgui

{ //iconv
const iconv_lib = iconv.createIconv(b, target, optimize);

exe.linkLibrary(iconv_lib);
app_module.linkLibrary(iconv_lib);

//On MacOS, we need to link iconv manually, on other platforms its part of libc (Linux) or by ourselves through win_iconv (Windows)
if (target.getOsTag() == .macos) {
exe.linkSystemLibrary("iconv");
if (target.result.os.tag == .macos) {
app_module.linkSystemLibrary("iconv", .{});
}
} //iconv

{ //process assets
var process_assets_exe = b.addExecutable(.{
.name = "asset_processor",
.root_source_file = .{ .path = root_path ++ "image_processor.zig" },
.target = b.host,
});

process_assets_exe.addModule("zigimg", zigimg_module);
process_assets_exe.root_module.addImport("zigimg", zigimg_module);
var run_process_assets = b.addRunArtifact(process_assets_exe);
run_process_assets.addArg(root_path);

const process_images_step = b.step("Process images", "Process image files into a QOI texture atlas");
process_images_step.dependOn(&run_process_assets.step);

exe.step.dependOn(process_images_step);
app.compile.step.dependOn(process_images_step);
} //process assets

const tempo = b.addExecutable(.{
Expand All @@ -188,25 +138,33 @@ pub fn build(b: *std.Build) !void {
});
b.installArtifact(tempo);

const clap = b.dependency("clap", .{});
const fumen_compile = b.addExecutable(.{
.name = "fumen_compile",
.root_source_file = .{ .path = root_path ++ "util/fumen_compile.zig" },
.target = target,
.optimize = optimize,
});
fumen_compile.addModule("clap", clap.module("clap"));
b.installArtifact(fumen_compile);

exe.addModule("fumen_compiler", b.addModule("fumen_compiler", .{ .source_file = .{ .path = root_path ++ "util/fumen_compile.zig" } }));

const fumen_compile_cmd = b.addRunArtifact(fumen_compile);
fumen_compile_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
fumen_compile_cmd.addArgs(args);
}
const fumen_compile_step = b.step("fumen_compile", "Run the fumen compiler");
fumen_compile_step.dependOn(&fumen_compile_cmd.step);
// const clap = b.dependency("clap", .{});
// const fumen_compile = b.addExecutable(.{
// .name = "fumen_compile",
// .root_source_file = .{ .path = root_path ++ "util/fumen_compile.zig" },
// .target = target,
// .optimize = optimize,
// });
// fumen_compile.root_module.addImport("clap", clap.module("clap"));
// b.installArtifact(fumen_compile);

app.compile.root_module.addImport(
"fumen_compiler",
b.addModule(
"fumen_compiler",
.{
.root_source_file = .{ .path = root_path ++ "util/fumen_compile.zig" },
},
),
);

// const fumen_compile_cmd = b.addRunArtifact(fumen_compile);
// fumen_compile_cmd.step.dependOn(b.getInstallStep());
// if (b.args) |args| {
// fumen_compile_cmd.addArgs(args);
// }
// const fumen_compile_step = b.step("fumen_compile", "Run the fumen compiler");
// fumen_compile_step.dependOn(&fumen_compile_cmd.step);

const tempo_cmd = b.addRunArtifact(tempo);
tempo_cmd.step.dependOn(b.getInstallStep());
Expand All @@ -216,13 +174,8 @@ pub fn build(b: *std.Build) !void {
const tempo_step = b.step("tempo", "");
tempo_step.dependOn(&tempo_cmd.step);

const run_game_cmd = b.addRunArtifact(exe);
run_game_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_game_cmd.addArgs(args);
}
const run_game_step = b.step("run", "Run the app");
run_game_step.dependOn(&run_game_cmd.step);
const run_step = b.step("run", "Run the app");
run_step.dependOn(&app.run.step);
}

fn root() []const u8 {
Expand Down
22 changes: 13 additions & 9 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@
.name = "ztyping",
.version = "0.0.0",
.dependencies = .{
.SDL2 = .{
.url = "https://github.com/Beyley/SDL/archive/f254788cd768adac006a25d7f1fe1e726596d9fd.tar.gz",
.hash = "1220f430383c1d7c198901c99dcd0b10cce8b3a34e77e60026f4c55ecf548e6d1ecc",
.mach_core = .{
.url = "https://pkg.machengine.org/mach-core/6a62bcc90e0d072d632788a6575d77942bd09a19.tar.gz",
.hash = "12209d39954fcda0be158461c10f64d14d5c7d097bd6d26785b332d75ffefa7dd7a0",
},
.zigimg = .{
.url = "https://github.com/zigimg/zigimg/archive/bb4dd9854c64cec6d810e1f9fa858ec308ac260c.tar.gz",
.hash = "12201813d83afa600cdbd68bbe5daf90c4b05edea8a99bded98217472760b425ec96",
.mach = .{
.url = "https://pkg.machengine.org/mach/d4cd79440ea16bf59156cd57707e5833acb8e1b5.tar.gz",
.hash = "12204662d0dc8a74270b1b219ad1b41e63568f7ae532a243a5fc38b1a95f58f756ae",
},
.clap = .{
.url = "https://github.com/Hejsil/zig-clap/archive/f49b94700e0761b7514abdca0e4f0e7f3f938a93.tar.gz",
.hash = "1220f48518ce22882e102255ed3bcdb7aeeb4891f50b2cdd3bd74b5b2e24d3149ba2",
.zigimg = .{
.url = "https://github.com/Beyley/zigimg/archive/d54b946d4b33cf1a18c31ab8ce6a1b4e4f7c2d38.tar.gz",
.hash = "1220a1ac633472925863401189e4de1cbf441736dd100c17c19a837ee0f31632d647",
},
// .clap = .{
// .url = "https://github.com/Hejsil/zig-clap/archive/f49b94700e0761b7514abdca0e4f0e7f3f938a93.tar.gz",
// .hash = "1220f48518ce22882e102255ed3bcdb7aeeb4891f50b2cdd3bd74b5b2e24d3149ba2",
// },
},
.paths = .{
"game/",
Expand Down
Loading

0 comments on commit 6837fc8

Please sign in to comment.