build: create new build options per compile step

This is going to let us put more object-specific config into the options
rather than affecting every object build per build.
This commit is contained in:
Mitchell Hashimoto
2024-02-04 19:40:59 -08:00
parent 95d855b225
commit c2b3f99faa
2 changed files with 40 additions and 37 deletions

View File

@ -159,7 +159,7 @@ pub fn build(b: *std.Build) !void {
"If not specified, git will be used. This must be a semantic version.",
);
const version: std.SemanticVersion = if (version_string) |v|
config.version = if (version_string) |v|
try std.SemanticVersion.parse(v)
else version: {
const vsn = try Version.detect(b);
@ -211,19 +211,8 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
}) else null;
const exe_options = b.addOptions();
config.addOptions(exe_options);
exe_options.addOption(std.SemanticVersion, "app_version", version);
exe_options.addOption([:0]const u8, "app_version_string", try std.fmt.allocPrintZ(
b.allocator,
"{}",
.{version},
));
// Exe
if (exe_) |exe| {
exe.root_module.addOptions("build_options", exe_options);
// Add the shared dependencies
_ = try addDeps(b, exe, config);
@ -428,7 +417,7 @@ pub fn build(b: *std.Build) !void {
}
// Documenation
if (emit_docs) buildDocumentation(b, version);
if (emit_docs) buildDocumentation(b, config.version);
// App (Linux)
if (target.result.os.tag == .linux and config.app_runtime != .none) {
@ -464,7 +453,6 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
.target = target,
});
lib.root_module.addOptions("build_options", exe_options);
_ = try addDeps(b, lib, config);
const lib_install = b.addInstallLibFile(
@ -482,7 +470,6 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
.target = target,
});
lib.root_module.addOptions("build_options", exe_options);
_ = try addDeps(b, lib, config);
const lib_install = b.addInstallLibFile(
@ -510,7 +497,6 @@ pub fn build(b: *std.Build) !void {
b,
optimize,
config,
exe_options,
);
// Add our library to zig-out
@ -526,7 +512,6 @@ pub fn build(b: *std.Build) !void {
null,
optimize,
config,
exe_options,
);
// Add our library to zig-out
@ -542,7 +527,6 @@ pub fn build(b: *std.Build) !void {
.simulator,
optimize,
config,
exe_options,
);
// Add our library to zig-out
@ -605,6 +589,11 @@ pub fn build(b: *std.Build) !void {
}),
};
// Whether we're using wasm shared memory. Some behaviors change.
// For now we require this but I wanted to make the code handle both
// up front.
const wasm_shared: bool = true;
// Modify our build configuration for wasm builds.
const wasm_config: BuildConfig = config: {
var copy = config;
@ -616,26 +605,19 @@ pub fn build(b: *std.Build) !void {
// Backends that are fixed for wasm
copy.font_backend = .web_canvas;
// Wasm-specific options
copy.wasm_shared = wasm_shared;
copy.wasm_target = wasm_target;
break :config copy;
};
// Whether we're using wasm shared memory. Some behaviors change.
// For now we require this but I wanted to make the code handle both
// up front.
const wasm_shared: bool = true;
exe_options.addOption(bool, "wasm_shared", wasm_shared);
// We want to support alternate wasm targets in the future (i.e.
// server side) so we have this now although its hardcoded.
exe_options.addOption(WasmTarget, "wasm_target", wasm_target);
const wasm = b.addSharedLibrary(.{
.name = "ghostty-wasm",
.root_source_file = .{ .path = "src/main_wasm.zig" },
.target = b.resolveTargetQuery(wasm_crosstarget),
.optimize = optimize,
});
wasm.root_module.addOptions("build_options", exe_options);
// So that we can use web workers with our wasm binary
wasm.import_memory = true;
@ -666,7 +648,6 @@ pub fn build(b: *std.Build) !void {
.target = b.resolveTargetQuery(wasm_crosstarget),
});
main_test.root_module.addOptions("build_options", exe_options);
_ = try addDeps(b, main_test, wasm_config);
test_step.dependOn(&main_test.step);
}
@ -709,7 +690,6 @@ pub fn build(b: *std.Build) !void {
copy.static = true;
break :config copy;
});
main_test.root_module.addOptions("build_options", exe_options);
const test_run = b.addRunArtifact(main_test);
test_step.dependOn(&test_run.step);
@ -755,7 +735,6 @@ fn createMacOSLib(
b: *std.Build,
optimize: std.builtin.OptimizeMode,
config: BuildConfig,
exe_options: *std.Build.Step.Options,
) !struct { *std.Build.Step, std.Build.LazyPath } {
// Modify our build configuration for macOS builds.
const macos_config: BuildConfig = config: {
@ -781,7 +760,6 @@ fn createMacOSLib(
});
lib.bundle_compiler_rt = true;
lib.linkLibC();
lib.root_module.addOptions("build_options", exe_options);
// Create a single static lib with all our dependencies merged
var lib_list = try addDeps(b, lib, macos_config);
@ -810,7 +788,6 @@ fn createMacOSLib(
});
lib.bundle_compiler_rt = true;
lib.linkLibC();
lib.root_module.addOptions("build_options", exe_options);
// Create a single static lib with all our dependencies merged
var lib_list = try addDeps(b, lib, macos_config);
@ -847,7 +824,6 @@ fn createIOSLib(
abi: ?std.Target.Abi,
optimize: std.builtin.OptimizeMode,
config: BuildConfig,
exe_options: *std.Build.Step.Options,
) !struct { *std.Build.Step, std.Build.LazyPath } {
const ios_config: BuildConfig = config: {
var copy = config;
@ -868,7 +844,6 @@ fn createIOSLib(
});
lib.bundle_compiler_rt = true;
lib.linkLibC();
lib.root_module.addOptions("build_options", exe_options);
// Create a single static lib with all our dependencies merged
var lib_list = try addDeps(b, lib, ios_config);
@ -895,6 +870,11 @@ fn addDeps(
step: *std.Build.Step.Compile,
config: BuildConfig,
) !LazyPathList {
// All object targets get access to a standard build_options module
const exe_options = b.addOptions();
try config.addOptions(exe_options);
step.root_module.addOptions("build_options", exe_options);
var static_libs = LazyPathList.init(b.allocator);
errdefer static_libs.deinit();

View File

@ -9,6 +9,7 @@ const assert = std.debug.assert;
const apprt = @import("apprt.zig");
const font = @import("font/main.zig");
const rendererpkg = @import("renderer.zig");
const WasmTarget = @import("os/wasm/target.zig").Target;
/// The build configuratin options. This may not be all available options
/// to `zig build` but it contains all the options that the Ghostty source
@ -18,6 +19,7 @@ const rendererpkg = @import("renderer.zig");
/// between options, make it easy to copy and mutate options for different
/// build types, etc.
pub const BuildConfig = struct {
version: std.SemanticVersion = .{ .major = 0, .minor = 0, .patch = 0 },
static: bool = false,
flatpak: bool = false,
libadwaita: bool = false,
@ -25,8 +27,14 @@ pub const BuildConfig = struct {
renderer: rendererpkg.Impl = .opengl,
font_backend: font.Backend = .freetype,
/// The target runtime for the wasm build and whether to use wasm shared
/// memory or not. These are both legacy wasm-specific options that we
/// will probably have to revisit when we get back to work on wasm.
wasm_target: WasmTarget = .browser,
wasm_shared: bool = true,
/// Configure the build options with our values.
pub fn addOptions(self: BuildConfig, step: *std.Build.Step.Options) void {
pub fn addOptions(self: BuildConfig, step: *std.Build.Step.Options) !void {
// We need to break these down individual because addOption doesn't
// support all types.
step.addOption(bool, "flatpak", self.flatpak);
@ -34,6 +42,18 @@ pub const BuildConfig = struct {
step.addOption(apprt.Runtime, "app_runtime", self.app_runtime);
step.addOption(font.Backend, "font_backend", self.font_backend);
step.addOption(rendererpkg.Impl, "renderer", self.renderer);
step.addOption(WasmTarget, "wasm_target", self.wasm_target);
step.addOption(bool, "wasm_shared", self.wasm_shared);
// Our version. We also add the string version so we don't need
// to do any allocations at runtime.
var buf: [64]u8 = undefined;
step.addOption(std.SemanticVersion, "app_version", self.version);
step.addOption([:0]const u8, "app_version_string", try std.fmt.bufPrintZ(
&buf,
"{}",
.{self.version},
));
}
/// Rehydrate our BuildConfig from the comptime options. Note that not all
@ -41,11 +61,14 @@ pub const BuildConfig = struct {
/// to see what is and isn't available.
pub fn fromOptions() BuildConfig {
return .{
.version = options.app_version,
.flatpak = options.flatpak,
.libadwaita = options.libadwaita,
.app_runtime = std.meta.stringToEnum(apprt.Runtime, @tagName(options.app_runtime)).?,
.font_backend = std.meta.stringToEnum(font.Backend, @tagName(options.font_backend)).?,
.renderer = std.meta.stringToEnum(rendererpkg.Impl, @tagName(options.renderer)).?,
.wasm_target = std.meta.stringToEnum(WasmTarget, @tagName(options.wasm_target)).?,
.wasm_shared = options.wasm_shared,
};
}
};