mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-10-06 01:46:33 +00:00
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:
52
build.zig
52
build.zig
@@ -159,7 +159,7 @@ pub fn build(b: *std.Build) !void {
|
|||||||
"If not specified, git will be used. This must be a semantic version.",
|
"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)
|
try std.SemanticVersion.parse(v)
|
||||||
else version: {
|
else version: {
|
||||||
const vsn = try Version.detect(b);
|
const vsn = try Version.detect(b);
|
||||||
@@ -211,19 +211,8 @@ pub fn build(b: *std.Build) !void {
|
|||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
}) else null;
|
}) 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
|
// Exe
|
||||||
if (exe_) |exe| {
|
if (exe_) |exe| {
|
||||||
exe.root_module.addOptions("build_options", exe_options);
|
|
||||||
|
|
||||||
// Add the shared dependencies
|
// Add the shared dependencies
|
||||||
_ = try addDeps(b, exe, config);
|
_ = try addDeps(b, exe, config);
|
||||||
|
|
||||||
@@ -428,7 +417,7 @@ pub fn build(b: *std.Build) !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Documenation
|
// Documenation
|
||||||
if (emit_docs) buildDocumentation(b, version);
|
if (emit_docs) buildDocumentation(b, config.version);
|
||||||
|
|
||||||
// App (Linux)
|
// App (Linux)
|
||||||
if (target.result.os.tag == .linux and config.app_runtime != .none) {
|
if (target.result.os.tag == .linux and config.app_runtime != .none) {
|
||||||
@@ -464,7 +453,6 @@ pub fn build(b: *std.Build) !void {
|
|||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
.target = target,
|
.target = target,
|
||||||
});
|
});
|
||||||
lib.root_module.addOptions("build_options", exe_options);
|
|
||||||
_ = try addDeps(b, lib, config);
|
_ = try addDeps(b, lib, config);
|
||||||
|
|
||||||
const lib_install = b.addInstallLibFile(
|
const lib_install = b.addInstallLibFile(
|
||||||
@@ -482,7 +470,6 @@ pub fn build(b: *std.Build) !void {
|
|||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
.target = target,
|
.target = target,
|
||||||
});
|
});
|
||||||
lib.root_module.addOptions("build_options", exe_options);
|
|
||||||
_ = try addDeps(b, lib, config);
|
_ = try addDeps(b, lib, config);
|
||||||
|
|
||||||
const lib_install = b.addInstallLibFile(
|
const lib_install = b.addInstallLibFile(
|
||||||
@@ -510,7 +497,6 @@ pub fn build(b: *std.Build) !void {
|
|||||||
b,
|
b,
|
||||||
optimize,
|
optimize,
|
||||||
config,
|
config,
|
||||||
exe_options,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Add our library to zig-out
|
// Add our library to zig-out
|
||||||
@@ -526,7 +512,6 @@ pub fn build(b: *std.Build) !void {
|
|||||||
null,
|
null,
|
||||||
optimize,
|
optimize,
|
||||||
config,
|
config,
|
||||||
exe_options,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Add our library to zig-out
|
// Add our library to zig-out
|
||||||
@@ -542,7 +527,6 @@ pub fn build(b: *std.Build) !void {
|
|||||||
.simulator,
|
.simulator,
|
||||||
optimize,
|
optimize,
|
||||||
config,
|
config,
|
||||||
exe_options,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Add our library to zig-out
|
// 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.
|
// Modify our build configuration for wasm builds.
|
||||||
const wasm_config: BuildConfig = config: {
|
const wasm_config: BuildConfig = config: {
|
||||||
var copy = config;
|
var copy = config;
|
||||||
@@ -616,26 +605,19 @@ pub fn build(b: *std.Build) !void {
|
|||||||
// Backends that are fixed for wasm
|
// Backends that are fixed for wasm
|
||||||
copy.font_backend = .web_canvas;
|
copy.font_backend = .web_canvas;
|
||||||
|
|
||||||
|
// Wasm-specific options
|
||||||
|
copy.wasm_shared = wasm_shared;
|
||||||
|
copy.wasm_target = wasm_target;
|
||||||
|
|
||||||
break :config copy;
|
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(.{
|
const wasm = b.addSharedLibrary(.{
|
||||||
.name = "ghostty-wasm",
|
.name = "ghostty-wasm",
|
||||||
.root_source_file = .{ .path = "src/main_wasm.zig" },
|
.root_source_file = .{ .path = "src/main_wasm.zig" },
|
||||||
.target = b.resolveTargetQuery(wasm_crosstarget),
|
.target = b.resolveTargetQuery(wasm_crosstarget),
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
wasm.root_module.addOptions("build_options", exe_options);
|
|
||||||
|
|
||||||
// So that we can use web workers with our wasm binary
|
// So that we can use web workers with our wasm binary
|
||||||
wasm.import_memory = true;
|
wasm.import_memory = true;
|
||||||
@@ -666,7 +648,6 @@ pub fn build(b: *std.Build) !void {
|
|||||||
.target = b.resolveTargetQuery(wasm_crosstarget),
|
.target = b.resolveTargetQuery(wasm_crosstarget),
|
||||||
});
|
});
|
||||||
|
|
||||||
main_test.root_module.addOptions("build_options", exe_options);
|
|
||||||
_ = try addDeps(b, main_test, wasm_config);
|
_ = try addDeps(b, main_test, wasm_config);
|
||||||
test_step.dependOn(&main_test.step);
|
test_step.dependOn(&main_test.step);
|
||||||
}
|
}
|
||||||
@@ -709,7 +690,6 @@ pub fn build(b: *std.Build) !void {
|
|||||||
copy.static = true;
|
copy.static = true;
|
||||||
break :config copy;
|
break :config copy;
|
||||||
});
|
});
|
||||||
main_test.root_module.addOptions("build_options", exe_options);
|
|
||||||
|
|
||||||
const test_run = b.addRunArtifact(main_test);
|
const test_run = b.addRunArtifact(main_test);
|
||||||
test_step.dependOn(&test_run.step);
|
test_step.dependOn(&test_run.step);
|
||||||
@@ -755,7 +735,6 @@ fn createMacOSLib(
|
|||||||
b: *std.Build,
|
b: *std.Build,
|
||||||
optimize: std.builtin.OptimizeMode,
|
optimize: std.builtin.OptimizeMode,
|
||||||
config: BuildConfig,
|
config: BuildConfig,
|
||||||
exe_options: *std.Build.Step.Options,
|
|
||||||
) !struct { *std.Build.Step, std.Build.LazyPath } {
|
) !struct { *std.Build.Step, std.Build.LazyPath } {
|
||||||
// Modify our build configuration for macOS builds.
|
// Modify our build configuration for macOS builds.
|
||||||
const macos_config: BuildConfig = config: {
|
const macos_config: BuildConfig = config: {
|
||||||
@@ -781,7 +760,6 @@ fn createMacOSLib(
|
|||||||
});
|
});
|
||||||
lib.bundle_compiler_rt = true;
|
lib.bundle_compiler_rt = true;
|
||||||
lib.linkLibC();
|
lib.linkLibC();
|
||||||
lib.root_module.addOptions("build_options", exe_options);
|
|
||||||
|
|
||||||
// Create a single static lib with all our dependencies merged
|
// Create a single static lib with all our dependencies merged
|
||||||
var lib_list = try addDeps(b, lib, macos_config);
|
var lib_list = try addDeps(b, lib, macos_config);
|
||||||
@@ -810,7 +788,6 @@ fn createMacOSLib(
|
|||||||
});
|
});
|
||||||
lib.bundle_compiler_rt = true;
|
lib.bundle_compiler_rt = true;
|
||||||
lib.linkLibC();
|
lib.linkLibC();
|
||||||
lib.root_module.addOptions("build_options", exe_options);
|
|
||||||
|
|
||||||
// Create a single static lib with all our dependencies merged
|
// Create a single static lib with all our dependencies merged
|
||||||
var lib_list = try addDeps(b, lib, macos_config);
|
var lib_list = try addDeps(b, lib, macos_config);
|
||||||
@@ -847,7 +824,6 @@ fn createIOSLib(
|
|||||||
abi: ?std.Target.Abi,
|
abi: ?std.Target.Abi,
|
||||||
optimize: std.builtin.OptimizeMode,
|
optimize: std.builtin.OptimizeMode,
|
||||||
config: BuildConfig,
|
config: BuildConfig,
|
||||||
exe_options: *std.Build.Step.Options,
|
|
||||||
) !struct { *std.Build.Step, std.Build.LazyPath } {
|
) !struct { *std.Build.Step, std.Build.LazyPath } {
|
||||||
const ios_config: BuildConfig = config: {
|
const ios_config: BuildConfig = config: {
|
||||||
var copy = config;
|
var copy = config;
|
||||||
@@ -868,7 +844,6 @@ fn createIOSLib(
|
|||||||
});
|
});
|
||||||
lib.bundle_compiler_rt = true;
|
lib.bundle_compiler_rt = true;
|
||||||
lib.linkLibC();
|
lib.linkLibC();
|
||||||
lib.root_module.addOptions("build_options", exe_options);
|
|
||||||
|
|
||||||
// Create a single static lib with all our dependencies merged
|
// Create a single static lib with all our dependencies merged
|
||||||
var lib_list = try addDeps(b, lib, ios_config);
|
var lib_list = try addDeps(b, lib, ios_config);
|
||||||
@@ -895,6 +870,11 @@ fn addDeps(
|
|||||||
step: *std.Build.Step.Compile,
|
step: *std.Build.Step.Compile,
|
||||||
config: BuildConfig,
|
config: BuildConfig,
|
||||||
) !LazyPathList {
|
) !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);
|
var static_libs = LazyPathList.init(b.allocator);
|
||||||
errdefer static_libs.deinit();
|
errdefer static_libs.deinit();
|
||||||
|
|
||||||
|
@@ -9,6 +9,7 @@ const assert = std.debug.assert;
|
|||||||
const apprt = @import("apprt.zig");
|
const apprt = @import("apprt.zig");
|
||||||
const font = @import("font/main.zig");
|
const font = @import("font/main.zig");
|
||||||
const rendererpkg = @import("renderer.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
|
/// The build configuratin options. This may not be all available options
|
||||||
/// to `zig build` but it contains all the options that the Ghostty source
|
/// 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
|
/// between options, make it easy to copy and mutate options for different
|
||||||
/// build types, etc.
|
/// build types, etc.
|
||||||
pub const BuildConfig = struct {
|
pub const BuildConfig = struct {
|
||||||
|
version: std.SemanticVersion = .{ .major = 0, .minor = 0, .patch = 0 },
|
||||||
static: bool = false,
|
static: bool = false,
|
||||||
flatpak: bool = false,
|
flatpak: bool = false,
|
||||||
libadwaita: bool = false,
|
libadwaita: bool = false,
|
||||||
@@ -25,8 +27,14 @@ pub const BuildConfig = struct {
|
|||||||
renderer: rendererpkg.Impl = .opengl,
|
renderer: rendererpkg.Impl = .opengl,
|
||||||
font_backend: font.Backend = .freetype,
|
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.
|
/// 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
|
// We need to break these down individual because addOption doesn't
|
||||||
// support all types.
|
// support all types.
|
||||||
step.addOption(bool, "flatpak", self.flatpak);
|
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(apprt.Runtime, "app_runtime", self.app_runtime);
|
||||||
step.addOption(font.Backend, "font_backend", self.font_backend);
|
step.addOption(font.Backend, "font_backend", self.font_backend);
|
||||||
step.addOption(rendererpkg.Impl, "renderer", self.renderer);
|
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
|
/// 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.
|
/// to see what is and isn't available.
|
||||||
pub fn fromOptions() BuildConfig {
|
pub fn fromOptions() BuildConfig {
|
||||||
return .{
|
return .{
|
||||||
|
.version = options.app_version,
|
||||||
.flatpak = options.flatpak,
|
.flatpak = options.flatpak,
|
||||||
.libadwaita = options.libadwaita,
|
.libadwaita = options.libadwaita,
|
||||||
.app_runtime = std.meta.stringToEnum(apprt.Runtime, @tagName(options.app_runtime)).?,
|
.app_runtime = std.meta.stringToEnum(apprt.Runtime, @tagName(options.app_runtime)).?,
|
||||||
.font_backend = std.meta.stringToEnum(font.Backend, @tagName(options.font_backend)).?,
|
.font_backend = std.meta.stringToEnum(font.Backend, @tagName(options.font_backend)).?,
|
||||||
.renderer = std.meta.stringToEnum(rendererpkg.Impl, @tagName(options.renderer)).?,
|
.renderer = std.meta.stringToEnum(rendererpkg.Impl, @tagName(options.renderer)).?,
|
||||||
|
.wasm_target = std.meta.stringToEnum(WasmTarget, @tagName(options.wasm_target)).?,
|
||||||
|
.wasm_shared = options.wasm_shared,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user