all: build: organize build files and reduce unreachables (#567)

This commit is contained in:
Ali Chraghi 2022-09-25 20:32:51 +03:30 committed by GitHub
parent cc301493ca
commit fcb82345d4
Failed to generate hash of commit
10 changed files with 434 additions and 419 deletions

337
build.zig
View file

@ -1,32 +1,50 @@
const std = @import("std");
const builtin = @import("builtin");
const gpu_sdk = @import("libs/gpu/sdk.zig");
const gpu_dawn_sdk = @import("libs/gpu-dawn/sdk.zig");
const system_sdk = @import("libs/glfw/system_sdk.zig");
const sysaudio_sdk = @import("libs/sysaudio/sdk.zig");
const glfw = @import("libs/glfw/build.zig");
const ecs = @import("libs/ecs/build.zig");
const freetype = @import("libs/freetype/build.zig");
const basisu = @import("libs/basisu/build.zig");
const sysjs = @import("libs/sysjs/build.zig");
const gamemode = @import("libs/gamemode/build.zig");
const Pkg = std.build.Pkg;
const gpu_dawn = gpu_dawn_sdk.Sdk(.{
const wasmserve = @import("tools/wasmserve/wasmserve.zig");
const gpu_dawn = @import("libs/gpu-dawn/sdk.zig").Sdk(.{
.glfw = glfw,
.glfw_include_dir = "glfw/upstream/glfw/include",
.system_sdk = system_sdk,
});
const gpu = gpu_sdk.Sdk(.{
const gpu = @import("libs/gpu/sdk.zig").Sdk(.{
.glfw = glfw,
.gpu_dawn = gpu_dawn,
});
const sysaudio = sysaudio_sdk.Sdk(.{
const sysaudio = @import("libs/sysaudio/sdk.zig").Sdk(.{
.system_sdk = system_sdk,
.sysjs = sysjs,
});
const CrossTarget = std.zig.CrossTarget;
const Builder = std.build.Builder;
const Pkg = std.build.Pkg;
pub fn build(b: *std.build.Builder) void {
pub const pkg = Pkg{
.name = "mach",
.source = .{ .path = thisDir() ++ "/src/main.zig" },
.dependencies = &.{ gpu.pkg, ecs.pkg, sysaudio.pkg },
};
pub const Options = struct {
glfw_options: glfw.Options = .{},
gpu_dawn_options: gpu_dawn.Options = .{},
sysaudio_options: sysaudio.Options = .{},
pub fn gpuOptions(options: Options) gpu.Options {
return .{
.glfw_options = options.glfw_options,
.gpu_dawn_options = options.gpu_dawn_options,
};
}
};
pub fn build(b: *Builder) !void {
const mode = b.standardReleaseOptions();
const target = b.standardTargetOptions(.{});
@ -47,13 +65,12 @@ pub fn build(b: *std.build.Builder) void {
const sysaudio_test_step = b.step("test-sysaudio", "Run sysaudio library tests");
const mach_test_step = b.step("test-mach", "Run Mach Core library tests");
glfw_test_step.dependOn(&glfw.testStep(b, mode, target).step);
gpu_test_step.dependOn(&gpu.testStep(b, mode, target, options.gpuOptions()).step);
glfw_test_step.dependOn(&(try glfw.testStep(b, mode, target)).step);
gpu_test_step.dependOn(&(try gpu.testStep(b, mode, target, options.gpuOptions())).step);
freetype_test_step.dependOn(&freetype.testStep(b, mode, target).step);
// TODO(self-hosted) uncomment this
// ecs_test_step.dependOn(&ecs.testStep(b, mode, target).step);
if (target.isNativeOs())
basisu_test_step.dependOn(&basisu.testStep(b, mode, target).step);
basisu_test_step.dependOn(&basisu.testStep(b, mode, target).step);
sysaudio_test_step.dependOn(&sysaudio.testStep(b, mode, target).step);
mach_test_step.dependOn(&testStep(b, mode, target).step);
@ -61,13 +78,17 @@ pub fn build(b: *std.build.Builder) void {
all_tests_step.dependOn(gpu_test_step);
// TODO(self-hosted) uncomment this
// all_tests_step.dependOn(ecs_test_step);
if (target.isNativeOs())
all_tests_step.dependOn(basisu_test_step);
all_tests_step.dependOn(basisu_test_step);
all_tests_step.dependOn(freetype_test_step);
all_tests_step.dependOn(sysaudio_test_step);
all_tests_step.dependOn(mach_test_step);
const shaderexp_app = App.init(
// TODO: we need a way to test wasm stuff
// const sysjs_test_step = b.step( "test-sysjs", "Run sysjs library tests");
// sysjs_test_step.dependOn(&sysjs.testStep(b, mode, target).step);
// all_tests_step.dependOn(sysjs_test_step);
const shaderexp_app = try App.init(
b,
.{
.name = "shaderexp",
@ -76,106 +97,81 @@ pub fn build(b: *std.build.Builder) void {
},
);
shaderexp_app.setBuildMode(mode);
shaderexp_app.link(options);
try shaderexp_app.link(options);
shaderexp_app.install();
const shaderexp_compile_step = b.step("shaderexp", "Compile shaderexp");
shaderexp_compile_step.dependOn(&shaderexp_app.getInstallStep().?.step);
const shaderexp_run_cmd = shaderexp_app.run();
const shaderexp_run_cmd = try shaderexp_app.run();
shaderexp_run_cmd.dependOn(&shaderexp_app.getInstallStep().?.step);
const shaderexp_run_step = b.step("run-shaderexp", "Run shaderexp");
shaderexp_run_step.dependOn(shaderexp_run_cmd);
// compiles the `libmach` shared library
const lib = b.addSharedLibrary("mach", "src/platform/libmach.zig", .unversioned);
lib.setTarget(target);
lib.setBuildMode(mode);
lib.main_pkg_path = "src/";
const app_pkg = std.build.Pkg{
.name = "app",
.source = .{ .path = "src/platform/libmach.zig" },
};
lib.addPackage(app_pkg);
lib.addPackage(gpu.pkg);
lib.addPackage(glfw.pkg);
lib.addPackage(sysaudio.pkg);
if (target.toTarget().os.tag == .linux)
lib.addPackage(gamemode.pkg);
glfw.link(b, lib, options.glfw_options);
gpu.link(b, lib, options.gpuOptions());
gamemode.link(lib);
lib.setOutputDir("./libmach/build");
lib.install();
// Compiles the `libmach` shared library
const shared_lib = try buildSharedLib(b, mode, target, options);
shared_lib.install();
}
// TODO: we need a way to test wasm stuff
// const sysjs_test_step = b.step( "test-sysjs", "Run sysjs library tests");
// sysjs_test_step.dependOn(&sysjs.testStep(b, mode, target).step);
// all_tests_step.dependOn(sysjs_test_step);
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
ensureGit(b.allocator);
ensureDependencySubmodule(b.allocator, "examples/libs/zmath") catch unreachable;
ensureDependencySubmodule(b.allocator, "examples/libs/zigimg") catch unreachable;
ensureDependencySubmodule(b.allocator, "examples/gkurve/assets") catch unreachable;
ensureDependencySubmodule(b.allocator, "examples/image-blur/assets") catch unreachable;
ensureDependencySubmodule(b.allocator, "examples/textured-cube/assets") catch unreachable;
ensureDependencySubmodule(b.allocator, "examples/cubemap/assets") catch unreachable;
inline for ([_]ExampleDefinition{
try ensureExamplesDependencySubmodules(b.allocator);
inline for ([_]struct {
name: []const u8,
deps: []const Pkg = &.{},
std_platform_only: bool = false,
has_assets: bool = false,
}{
.{ .name = "triangle" },
.{ .name = "triangle-msaa" },
.{ .name = "boids" },
.{ .name = "rotating-cube", .packages = &[_]Pkg{Packages.zmath} },
.{ .name = "pixel-post-process", .packages = &[_]Pkg{Packages.zmath} },
.{ .name = "two-cubes", .packages = &[_]Pkg{Packages.zmath} },
.{ .name = "instanced-cube", .packages = &[_]Pkg{Packages.zmath} },
.{ .name = "advanced-gen-texture-light", .packages = &[_]Pkg{Packages.zmath} },
.{ .name = "fractal-cube", .packages = &[_]Pkg{Packages.zmath} },
.{ .name = "textured-cube", .packages = &[_]Pkg{ Packages.zmath, Packages.zigimg }, .has_assets = true },
.{ .name = "rotating-cube", .deps = &.{Packages.zmath} },
.{ .name = "pixel-post-process", .deps = &.{Packages.zmath} },
.{ .name = "two-cubes", .deps = &.{Packages.zmath} },
.{ .name = "instanced-cube", .deps = &.{Packages.zmath} },
.{ .name = "advanced-gen-texture-light", .deps = &.{Packages.zmath} },
.{ .name = "fractal-cube", .deps = &.{Packages.zmath} },
.{ .name = "textured-cube", .deps = &.{ Packages.zmath, Packages.zigimg }, .has_assets = true },
// TODO(self-hosted) uncomment this
// .{ .name = "ecs-app", .packages = &[_]Pkg{} },
.{ .name = "image-blur", .packages = &[_]Pkg{Packages.zigimg}, .has_assets = true },
.{ .name = "cubemap", .packages = &[_]Pkg{ Packages.zmath, Packages.zigimg }, .has_assets = true },
.{ .name = "map-async", .packages = &[_]Pkg{} },
.{ .name = "sysaudio", .packages = &[_]Pkg{} },
// NOTE: examples with std_platform_only should be placed at last
.{ .name = "gkurve", .packages = &[_]Pkg{ Packages.zmath, Packages.zigimg, freetype.pkg }, .std_platform_only = true, .has_assets = true },
// .{ .name = "ecs-app", .deps = &.{} },
.{ .name = "image-blur", .deps = &.{Packages.zigimg}, .has_assets = true },
.{ .name = "cubemap", .deps = &.{ Packages.zmath, Packages.zigimg }, .has_assets = true },
.{ .name = "map-async", .deps = &.{} },
.{ .name = "sysaudio", .deps = &.{} },
.{ .name = "gkurve", .deps = &.{ Packages.zmath, Packages.zigimg, freetype.pkg }, .std_platform_only = true, .has_assets = true },
}) |example| {
// FIXME: this is workaround for a problem that some examples (having the std_platform_only=true field) as
// well as zigimg uses IO which is not supported in freestanding environments. So break out of this loop
// as soon as any such examples is found. This does means that any example which works on wasm should be
// FIXME: this is workaround for a problem that some examples
// (having the std_platform_only=true field) as well as zigimg
// uses IO which is not supported in freestanding environments.
// So break out of this loop as soon as any such examples is found.
// This does means that any example which works on wasm should be
// placed before those who dont.
if (example.std_platform_only)
if (target.toTarget().cpu.arch == .wasm32)
if (target.getCpuArch() == .wasm32)
break;
const example_app = App.init(
const example_app = try App.init(
b,
.{
.name = "example-" ++ example.name,
.src = "examples/" ++ example.name ++ "/main.zig",
.target = target,
.deps = example.packages,
.deps = example.deps,
.res_dirs = if (example.has_assets) &.{"examples/" ++ example.name ++ "/assets"} else null,
.watch_paths = &.{"examples/" ++ example.name},
},
);
example_app.setBuildMode(mode);
inline for (example.packages) |p| {
inline for (example.deps) |p| {
if (std.mem.eql(u8, p.name, freetype.pkg.name))
freetype.link(example_app.b, example_app.step, .{});
}
example_app.link(options);
try example_app.link(options);
example_app.install();
const example_compile_step = b.step("example-" ++ example.name, "Compile '" ++ example.name ++ "' example");
example_compile_step.dependOn(&example_app.getInstallStep().?.step);
const example_run_cmd = example_app.run();
example_run_cmd.dependOn(&example_app.getInstallStep().?.step);
const example_run_cmd = try example_app.run();
example_run_cmd.dependOn(example_compile_step);
const example_run_step = b.step("run-example-" ++ example.name, "Run '" ++ example.name ++ "' example");
example_run_step.dependOn(example_run_cmd);
}
@ -184,7 +180,19 @@ pub fn build(b: *std.build.Builder) void {
compile_all.dependOn(b.getInstallStep());
}
fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep {
const Packages = struct {
// Declared here because submodule may not be cloned at the time build.zig runs.
const zmath = Pkg{
.name = "zmath",
.source = .{ .path = "examples/libs/zmath/src/zmath.zig" },
};
const zigimg = Pkg{
.name = "zigimg",
.source = .{ .path = "examples/libs/zigimg/zigimg.zig" },
};
};
fn testStep(b: *Builder, mode: std.builtin.Mode, target: CrossTarget) *std.build.RunStep {
const main_tests = b.addTestExe("mach-tests", "src/main.zig");
main_tests.setBuildMode(mode);
main_tests.setTarget(target);
@ -196,48 +204,45 @@ fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.Cross
return main_tests.run();
}
pub const Options = struct {
glfw_options: glfw.Options = .{},
gpu_dawn_options: gpu_dawn.Options = .{},
sysaudio_options: sysaudio.Options = .{},
pub fn gpuOptions(options: Options) gpu.Options {
return .{
.glfw_options = options.glfw_options,
.gpu_dawn_options = options.gpu_dawn_options,
};
fn buildSharedLib(b: *Builder, mode: std.builtin.Mode, target: CrossTarget, options: Options) !*std.build.LibExeObjStep {
const lib = b.addSharedLibrary("mach", "src/platform/libmach.zig", .unversioned);
lib.setTarget(target);
lib.setBuildMode(mode);
lib.main_pkg_path = "src/";
const app_pkg = Pkg{
.name = "app",
.source = .{ .path = "src/platform/libmach.zig" },
};
lib.addPackage(app_pkg);
lib.addPackage(glfw.pkg);
lib.addPackage(gpu.pkg);
lib.addPackage(sysaudio.pkg);
if (target.isLinux()) {
lib.addPackage(gamemode.pkg);
gamemode.link(lib);
}
};
const ExampleDefinition = struct {
name: []const u8,
packages: []const Pkg = &[_]Pkg{},
std_platform_only: bool = false,
has_assets: bool = false,
};
const Packages = struct {
// Declared here because submodule may not be cloned at the time build.zig runs.
const zmath = std.build.Pkg{
.name = "zmath",
.source = .{ .path = "examples/libs/zmath/src/zmath.zig" },
};
const zigimg = std.build.Pkg{
.name = "zigimg",
.source = .{ .path = "examples/libs/zigimg/zigimg.zig" },
};
};
try glfw.link(b, lib, options.glfw_options);
try gpu.link(b, lib, options.gpuOptions());
lib.setOutputDir("libmach/build");
return lib;
}
const web_install_dir = std.build.InstallDir{ .custom = "www" };
pub const App = struct {
b: *std.build.Builder,
b: *Builder,
name: []const u8,
step: *std.build.LibExeObjStep,
platform: Platform,
res_dirs: ?[]const []const u8,
watch_paths: ?[]const []const u8,
pub const InitError = std.zig.system.NativeTargetInfo.DetectError;
pub const LinkError = glfw.LinkError;
pub const RunError = error{
ParsingIpFailed,
} || wasmserve.Error || std.fmt.ParseIntError;
pub const Platform = enum {
native,
web,
@ -248,28 +253,28 @@ pub const App = struct {
}
};
pub fn init(b: *std.build.Builder, options: struct {
pub fn init(b: *Builder, options: struct {
name: []const u8,
src: []const u8,
target: std.zig.CrossTarget,
target: CrossTarget,
deps: ?[]const Pkg = null,
res_dirs: ?[]const []const u8 = null,
watch_paths: ?[]const []const u8 = null,
}) App {
const target = (std.zig.system.NativeTargetInfo.detect(options.target) catch unreachable).target;
}) InitError!App {
const target = (try std.zig.system.NativeTargetInfo.detect(options.target)).target;
const platform = Platform.fromTarget(target);
var deps = std.ArrayList(std.build.Pkg).init(b.allocator);
deps.append(pkg) catch unreachable;
deps.append(gpu.pkg) catch unreachable;
deps.append(sysaudio.pkg) catch unreachable;
var deps = std.ArrayList(Pkg).init(b.allocator);
try deps.append(pkg);
try deps.append(gpu.pkg);
try deps.append(sysaudio.pkg);
switch (platform) {
.native => deps.append(glfw.pkg) catch unreachable,
.web => deps.append(sysjs.pkg) catch unreachable,
.native => try deps.append(glfw.pkg),
.web => try deps.append(sysjs.pkg),
}
if (options.deps) |app_deps| deps.appendSlice(app_deps) catch unreachable;
if (options.deps) |app_deps| try deps.appendSlice(app_deps);
const app_pkg = std.build.Pkg{
const app_pkg = Pkg{
.name = "app",
.source = .{ .path = options.src },
.dependencies = deps.toOwnedSlice(),
@ -310,6 +315,16 @@ pub const App = struct {
};
}
pub fn link(app: *const App, options: Options) LinkError!void {
if (app.platform != .web) {
try glfw.link(app.b, app.step, options.glfw_options);
gpu.link(app.b, app.step, options.gpuOptions()) catch return error.FailedToLinkGPU;
if (app.step.target.isLinux())
gamemode.link(app.step);
}
sysaudio.link(app.b, app.step, options.sysaudio_options);
}
pub fn install(app: *const App) void {
app.step.install();
@ -356,14 +371,28 @@ pub const App = struct {
}
}
pub fn link(app: *const App, options: Options) void {
if (app.platform != .web) {
glfw.link(app.b, app.step, options.glfw_options);
gpu.link(app.b, app.step, options.gpuOptions());
if (app.step.target.isLinux())
gamemode.link(app.step);
pub fn run(app: *const App) RunError!*std.build.Step {
if (app.platform == .web) {
const address = std.process.getEnvVarOwned(app.b.allocator, "MACH_ADDRESS") catch try app.b.allocator.dupe(u8, "127.0.0.1");
const port = std.process.getEnvVarOwned(app.b.allocator, "MACH_PORT") catch try app.b.allocator.dupe(u8, "8080");
const address_parsed = std.net.Address.parseIp4(address, try std.fmt.parseInt(u16, port, 10)) catch return error.ParsingIpFailed;
const install_step_name = if (std.mem.startsWith(u8, app.step.name, "example-"))
app.step.name
else
null;
const serve_step = try wasmserve.serve(
app.step,
.{
.install_step_name = install_step_name,
.install_dir = web_install_dir,
.watch_paths = app.watch_paths,
.listen_address = address_parsed,
},
);
return &serve_step.step;
} else {
return &app.step.run().step;
}
sysaudio.link(app.b, app.step, options.sysaudio_options);
}
pub fn setBuildMode(app: *const App, mode: std.builtin.Mode) void {
@ -373,41 +402,17 @@ pub const App = struct {
pub fn getInstallStep(app: *const App) ?*std.build.InstallArtifactStep {
return app.step.install_step;
}
pub fn run(app: *const App) *std.build.Step {
if (app.platform == .web) {
const address = std.process.getEnvVarOwned(app.b.allocator, "MACH_ADDRESS") catch app.b.allocator.dupe(u8, "127.0.0.1") catch unreachable;
const port = std.process.getEnvVarOwned(app.b.allocator, "MACH_PORT") catch app.b.allocator.dupe(u8, "8080") catch unreachable;
const address_parsed = std.net.Address.parseIp4(address, std.fmt.parseInt(u16, port, 10) catch unreachable) catch unreachable;
const wasmserve = @import("tools/wasmserve/wasmserve.zig");
const install_step_name = if (std.mem.startsWith(u8, app.step.name, "example-"))
app.step.name
else
null;
const serve_step = wasmserve.serve(
app.step,
.{
.install_step_name = install_step_name,
.install_dir = web_install_dir,
.watch_paths = app.watch_paths,
.listen_address = address_parsed,
},
) catch unreachable;
return &serve_step.step;
} else {
return &app.step.run().step;
}
}
};
pub const pkg = std.build.Pkg{
.name = "mach",
.source = .{ .path = thisDir() ++ "/src/main.zig" },
.dependencies = &.{ gpu.pkg, ecs.pkg, sysaudio.pkg },
};
fn thisDir() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
fn ensureExamplesDependencySubmodules(allocator: std.mem.Allocator) !void {
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
ensureGit(allocator);
try ensureDependencySubmodule(allocator, "examples/libs/zmath");
try ensureDependencySubmodule(allocator, "examples/libs/zigimg");
try ensureDependencySubmodule(allocator, "examples/gkurve/assets");
try ensureDependencySubmodule(allocator, "examples/image-blur/assets");
try ensureDependencySubmodule(allocator, "examples/textured-cube/assets");
try ensureDependencySubmodule(allocator, "examples/cubemap/assets");
}
fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !void {
@ -424,11 +429,9 @@ fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !vo
}
fn ensureGit(allocator: std.mem.Allocator) void {
const argv = &[_][]const u8{ "git", "--version" };
const result = std.ChildProcess.exec(.{
.allocator = allocator,
.argv = argv,
.cwd = ".",
.argv = &.{ "git", "--version" },
}) catch { // e.g. FileNotFound
std.log.err("mach: error: 'git --version' failed. Is git not installed?", .{});
std.process.exit(1);
@ -442,3 +445,7 @@ fn ensureGit(allocator: std.mem.Allocator) void {
std.process.exit(1);
}
}
fn thisDir() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}