update build.zig API usage

This commit is contained in:
Andrew Kelley 2023-01-30 21:39:43 -07:00
parent 71ff60f126
commit 73cf7b6429
80 changed files with 905 additions and 494 deletions

View File

@ -23,7 +23,7 @@ pub fn build(b: *Builder) !void {
} }
break :t b.standardTargetOptions(.{ .default_target = default_target }); break :t b.standardTargetOptions(.{ .default_target = default_target });
}; };
const mode: std.builtin.Mode = if (release) switch (target.getCpuArch()) { const optimize: std.builtin.OptimizeMode = if (release) switch (target.getCpuArch()) {
.wasm32 => .ReleaseSmall, .wasm32 => .ReleaseSmall,
else => .ReleaseFast, else => .ReleaseFast,
} else .Debug; } else .Debug;
@ -33,7 +33,12 @@ pub fn build(b: *Builder) !void {
const test_step = b.step("test", "Run all the tests"); const test_step = b.step("test", "Run all the tests");
const docgen_exe = b.addExecutable("docgen", "doc/docgen.zig"); const docgen_exe = b.addExecutable(.{
.name = "docgen",
.root_source_file = .{ .path = "doc/docgen.zig" },
.target = .{},
.optimize = .Debug,
});
docgen_exe.single_threaded = single_threaded; docgen_exe.single_threaded = single_threaded;
const rel_zig_exe = try fs.path.relative(b.allocator, b.build_root, b.zig_exe); const rel_zig_exe = try fs.path.relative(b.allocator, b.build_root, b.zig_exe);
@ -53,10 +58,12 @@ pub fn build(b: *Builder) !void {
const docs_step = b.step("docs", "Build documentation"); const docs_step = b.step("docs", "Build documentation");
docs_step.dependOn(&docgen_cmd.step); docs_step.dependOn(&docgen_cmd.step);
const test_cases = b.addTest("src/test.zig"); const test_cases = b.addTest(.{
.root_source_file = .{ .path = "src/test.zig" },
.optimize = optimize,
});
test_cases.main_pkg_path = "."; test_cases.main_pkg_path = ".";
test_cases.stack_size = stack_size; test_cases.stack_size = stack_size;
test_cases.setBuildMode(mode);
test_cases.single_threaded = single_threaded; test_cases.single_threaded = single_threaded;
const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"}); const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});
@ -149,17 +156,15 @@ pub fn build(b: *Builder) !void {
const mem_leak_frames: u32 = b.option(u32, "mem-leak-frames", "How many stack frames to print when a memory leak occurs. Tests get 2x this amount.") orelse blk: { const mem_leak_frames: u32 = b.option(u32, "mem-leak-frames", "How many stack frames to print when a memory leak occurs. Tests get 2x this amount.") orelse blk: {
if (strip == true) break :blk @as(u32, 0); if (strip == true) break :blk @as(u32, 0);
if (mode != .Debug) break :blk 0; if (optimize != .Debug) break :blk 0;
break :blk 4; break :blk 4;
}; };
const exe = addCompilerStep(b); const exe = addCompilerStep(b, optimize, target);
exe.strip = strip; exe.strip = strip;
exe.sanitize_thread = sanitize_thread; exe.sanitize_thread = sanitize_thread;
exe.build_id = b.option(bool, "build-id", "Include a build id note") orelse false; exe.build_id = b.option(bool, "build-id", "Include a build id note") orelse false;
exe.install(); exe.install();
exe.setBuildMode(mode);
exe.setTarget(target);
const compile_step = b.step("compile", "Build the self-hosted compiler"); const compile_step = b.step("compile", "Build the self-hosted compiler");
compile_step.dependOn(&exe.step); compile_step.dependOn(&exe.step);
@ -195,7 +200,7 @@ pub fn build(b: *Builder) !void {
test_cases.linkLibC(); test_cases.linkLibC();
} }
const is_debug = mode == .Debug; const is_debug = optimize == .Debug;
const enable_logging = b.option(bool, "log", "Enable debug logging with --debug-log") orelse is_debug; const enable_logging = b.option(bool, "log", "Enable debug logging with --debug-log") orelse is_debug;
const enable_link_snapshots = b.option(bool, "link-snapshot", "Whether to enable linker state snapshots") orelse false; const enable_link_snapshots = b.option(bool, "link-snapshot", "Whether to enable linker state snapshots") orelse false;
@ -360,25 +365,25 @@ pub fn build(b: *Builder) !void {
test_step.dependOn(test_cases_step); test_step.dependOn(test_cases_step);
} }
var chosen_modes: [4]builtin.Mode = undefined; var chosen_opt_modes_buf: [4]builtin.Mode = undefined;
var chosen_mode_index: usize = 0; var chosen_mode_index: usize = 0;
if (!skip_debug) { if (!skip_debug) {
chosen_modes[chosen_mode_index] = builtin.Mode.Debug; chosen_opt_modes_buf[chosen_mode_index] = builtin.Mode.Debug;
chosen_mode_index += 1; chosen_mode_index += 1;
} }
if (!skip_release_safe) { if (!skip_release_safe) {
chosen_modes[chosen_mode_index] = builtin.Mode.ReleaseSafe; chosen_opt_modes_buf[chosen_mode_index] = builtin.Mode.ReleaseSafe;
chosen_mode_index += 1; chosen_mode_index += 1;
} }
if (!skip_release_fast) { if (!skip_release_fast) {
chosen_modes[chosen_mode_index] = builtin.Mode.ReleaseFast; chosen_opt_modes_buf[chosen_mode_index] = builtin.Mode.ReleaseFast;
chosen_mode_index += 1; chosen_mode_index += 1;
} }
if (!skip_release_small) { if (!skip_release_small) {
chosen_modes[chosen_mode_index] = builtin.Mode.ReleaseSmall; chosen_opt_modes_buf[chosen_mode_index] = builtin.Mode.ReleaseSmall;
chosen_mode_index += 1; chosen_mode_index += 1;
} }
const modes = chosen_modes[0..chosen_mode_index]; const optimization_modes = chosen_opt_modes_buf[0..chosen_mode_index];
// run stage1 `zig fmt` on this build.zig file just to make sure it works // run stage1 `zig fmt` on this build.zig file just to make sure it works
test_step.dependOn(&fmt_build_zig.step); test_step.dependOn(&fmt_build_zig.step);
@ -391,7 +396,7 @@ pub fn build(b: *Builder) !void {
"test/behavior.zig", "test/behavior.zig",
"behavior", "behavior",
"Run the behavior tests", "Run the behavior tests",
modes, optimization_modes,
skip_single_threaded, skip_single_threaded,
skip_non_native, skip_non_native,
skip_libc, skip_libc,
@ -405,7 +410,7 @@ pub fn build(b: *Builder) !void {
"lib/compiler_rt.zig", "lib/compiler_rt.zig",
"compiler-rt", "compiler-rt",
"Run the compiler_rt tests", "Run the compiler_rt tests",
modes, optimization_modes,
true, // skip_single_threaded true, // skip_single_threaded
skip_non_native, skip_non_native,
true, // skip_libc true, // skip_libc
@ -419,7 +424,7 @@ pub fn build(b: *Builder) !void {
"lib/c.zig", "lib/c.zig",
"universal-libc", "universal-libc",
"Run the universal libc tests", "Run the universal libc tests",
modes, optimization_modes,
true, // skip_single_threaded true, // skip_single_threaded
skip_non_native, skip_non_native,
true, // skip_libc true, // skip_libc
@ -427,11 +432,11 @@ pub fn build(b: *Builder) !void {
skip_stage2_tests or true, // TODO get these all passing skip_stage2_tests or true, // TODO get these all passing
)); ));
test_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes)); test_step.dependOn(tests.addCompareOutputTests(b, test_filter, optimization_modes));
test_step.dependOn(tests.addStandaloneTests( test_step.dependOn(tests.addStandaloneTests(
b, b,
test_filter, test_filter,
modes, optimization_modes,
skip_non_native, skip_non_native,
enable_macos_sdk, enable_macos_sdk,
target, target,
@ -444,10 +449,10 @@ pub fn build(b: *Builder) !void {
enable_symlinks_windows, enable_symlinks_windows,
)); ));
test_step.dependOn(tests.addCAbiTests(b, skip_non_native, skip_release)); test_step.dependOn(tests.addCAbiTests(b, skip_non_native, skip_release));
test_step.dependOn(tests.addLinkTests(b, test_filter, modes, enable_macos_sdk, skip_stage2_tests, enable_symlinks_windows)); test_step.dependOn(tests.addLinkTests(b, test_filter, optimization_modes, enable_macos_sdk, skip_stage2_tests, enable_symlinks_windows));
test_step.dependOn(tests.addStackTraceTests(b, test_filter, modes)); test_step.dependOn(tests.addStackTraceTests(b, test_filter, optimization_modes));
test_step.dependOn(tests.addCliTests(b, test_filter, modes)); test_step.dependOn(tests.addCliTests(b, test_filter, optimization_modes));
test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter, modes)); test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter, optimization_modes));
test_step.dependOn(tests.addTranslateCTests(b, test_filter)); test_step.dependOn(tests.addTranslateCTests(b, test_filter));
if (!skip_run_translated_c) { if (!skip_run_translated_c) {
test_step.dependOn(tests.addRunTranslatedCTests(b, test_filter, target)); test_step.dependOn(tests.addRunTranslatedCTests(b, test_filter, target));
@ -461,7 +466,7 @@ pub fn build(b: *Builder) !void {
"lib/std/std.zig", "lib/std/std.zig",
"std", "std",
"Run the standard library tests", "Run the standard library tests",
modes, optimization_modes,
skip_single_threaded, skip_single_threaded,
skip_non_native, skip_non_native,
skip_libc, skip_libc,
@ -481,9 +486,7 @@ fn addWasiUpdateStep(b: *Builder, version: [:0]const u8) !void {
}; };
target.cpu_features_add.addFeature(@enumToInt(std.Target.wasm.Feature.bulk_memory)); target.cpu_features_add.addFeature(@enumToInt(std.Target.wasm.Feature.bulk_memory));
const exe = addCompilerStep(b); const exe = addCompilerStep(b, .ReleaseSmall, target);
exe.setBuildMode(.ReleaseSmall);
exe.setTarget(target);
const exe_options = b.addOptions(); const exe_options = b.addOptions();
exe.addOptions("build_options", exe_options); exe.addOptions("build_options", exe_options);
@ -510,8 +513,17 @@ fn addWasiUpdateStep(b: *Builder, version: [:0]const u8) !void {
update_zig1_step.dependOn(&run_opt.step); update_zig1_step.dependOn(&run_opt.step);
} }
fn addCompilerStep(b: *Builder) *std.build.LibExeObjStep { fn addCompilerStep(
const exe = b.addExecutable("zig", "src/main.zig"); b: *Builder,
optimize: std.builtin.OptimizeMode,
target: std.zig.CrossTarget,
) *std.build.LibExeObjStep {
const exe = b.addExecutable(.{
.name = "zig",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
exe.stack_size = stack_size; exe.stack_size = stack_size;
return exe; return exe;
} }

View File

@ -9531,8 +9531,12 @@ fn foo(comptime T: type, ptr: *T) T {
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const exe = b.addExecutable("example", "example.zig"); const optimize = b.standardOptimizeOption(.{});
exe.setBuildMode(b.standardReleaseOptions()); const exe = b.addExecutable(.{
.name = "example",
.root_source_file = .{ .path = "example.zig" },
.optimize = optimize,
});
b.default_step.dependOn(&exe.step); b.default_step.dependOn(&exe.step);
} }
{#code_end#} {#code_end#}
@ -10558,11 +10562,14 @@ pub fn build(b: *Builder) void {
// Standard release options allow the person running `zig build` to select // Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable("example", "src/main.zig"); const exe = b.addExecutable(.{
exe.setTarget(target); .name = "example",
exe.setBuildMode(mode); .root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
exe.install(); exe.install();
const run_cmd = exe.run(); const run_cmd = exe.run();
@ -10584,13 +10591,18 @@ pub fn build(b: *Builder) void {
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const lib = b.addStaticLibrary("example", "src/main.zig"); const lib = b.addStaticLibrary(.{
lib.setBuildMode(mode); .name = "example",
.root_source_file = .{ .path = "src/main.zig" },
.optimize = optimize,
});
lib.install(); lib.install();
var main_tests = b.addTest("src/main.zig"); const main_tests = b.addTest(.{
main_tests.setBuildMode(mode); .root_source_file = .{ .path = "src/main.zig" },
.optimize = optimize,
});
const test_step = b.step("test", "Run library tests"); const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step); test_step.dependOn(&main_tests.step);
@ -10954,7 +10966,9 @@ const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0)); const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0));
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
});
exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"}); exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"});
exe.linkLibrary(lib); exe.linkLibrary(lib);
exe.linkSystemLibrary("c"); exe.linkSystemLibrary("c");
@ -11016,7 +11030,9 @@ const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const obj = b.addObject("base64", "base64.zig"); const obj = b.addObject("base64", "base64.zig");
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
});
exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"}); exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"});
exe.addObject(obj); exe.addObject(obj);
exe.linkSystemLibrary("c"); exe.linkSystemLibrary("c");

View File

@ -1,5 +1,8 @@
const std = @import("std"); const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose // Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which // what target to build for. Here we do not override the defaults, which
@ -7,28 +10,58 @@ pub fn build(b: *std.build.Builder) void {
// for restricting supported target set are available. // for restricting supported target set are available.
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select // Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
const mode = b.standardReleaseOptions(); // set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption();
const exe = b.addExecutable("$", "src/main.zig"); const exe = b.addExecutable(.{
exe.setTarget(target); .name = "$",
exe.setBuildMode(mode); // In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
exe.install(); exe.install();
// This *creates* a RunStep in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = exe.run(); const run_cmd = exe.run();
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| { if (b.args) |args| {
run_cmd.addArgs(args); run_cmd.addArgs(args);
} }
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app"); const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
const exe_tests = b.addTest("src/main.zig"); // Creates a step for unit testing.
exe_tests.setTarget(target); const exe_tests = b.addTest(.{
exe_tests.setBuildMode(mode); .root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests"); const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step); test_step.dependOn(&exe_tests.step);
} }

View File

@ -1,17 +1,44 @@
const std = @import("std"); const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.build.Builder) void {
// Standard release options allow the person running `zig build` to select // Standard target options allows the person running `zig build` to choose
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. // what target to build for. Here we do not override the defaults, which
const mode = b.standardReleaseOptions(); // means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
const lib = b.addStaticLibrary("$", "src/main.zig"); // Standard optimization options allow the person running `zig build` to select
lib.setBuildMode(mode); // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption();
const lib = b.addStaticLibrary(.{
.name = "$",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
lib.install(); lib.install();
const main_tests = b.addTest("src/main.zig"); // Creates a step for unit testing.
main_tests.setBuildMode(mode); const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build test`
// This will evaluate the `test` step rather than the default, which is "install".
const test_step = b.step("test", "Run library tests"); const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step); test_step.dependOn(&main_tests.step);
} }

View File

@ -420,10 +420,10 @@ pub const Builder = struct {
pub const ExecutableOptions = struct { pub const ExecutableOptions = struct {
name: []const u8, name: []const u8,
root_source_file: ?FileSource, root_source_file: ?FileSource = null,
version: ?std.builtin.Version = null, version: ?std.builtin.Version = null,
target: CrossTarget, target: CrossTarget = .{},
optimize: std.builtin.Mode, optimize: std.builtin.Mode = .Debug,
linkage: ?LibExeObjStep.Linkage = null, linkage: ?LibExeObjStep.Linkage = null,
}; };
@ -436,13 +436,12 @@ pub const Builder = struct {
.optimize = options.optimize, .optimize = options.optimize,
.kind = .exe, .kind = .exe,
.linkage = options.linkage, .linkage = options.linkage,
.version = options.version,
}); });
} }
pub const ObjectOptions = struct { pub const ObjectOptions = struct {
name: []const u8, name: []const u8,
root_source_file: ?FileSource, root_source_file: ?FileSource = null,
target: CrossTarget, target: CrossTarget,
optimize: std.builtin.Mode, optimize: std.builtin.Mode,
}; };
@ -459,7 +458,7 @@ pub const Builder = struct {
pub const SharedLibraryOptions = struct { pub const SharedLibraryOptions = struct {
name: []const u8, name: []const u8,
root_source_file: ?FileSource, root_source_file: ?FileSource = null,
version: ?std.builtin.Version = null, version: ?std.builtin.Version = null,
target: CrossTarget, target: CrossTarget,
optimize: std.builtin.Mode, optimize: std.builtin.Mode,
@ -501,8 +500,8 @@ pub const Builder = struct {
name: []const u8 = "test", name: []const u8 = "test",
kind: LibExeObjStep.Kind = .@"test", kind: LibExeObjStep.Kind = .@"test",
root_source_file: FileSource, root_source_file: FileSource,
target: CrossTarget, target: CrossTarget = .{},
optimize: std.builtin.Mode, optimize: std.builtin.Mode = .Debug,
version: ?std.builtin.Version = null, version: ?std.builtin.Version = null,
}; };
@ -630,8 +629,8 @@ pub const Builder = struct {
return FmtStep.create(self, paths); return FmtStep.create(self, paths);
} }
pub fn addTranslateC(self: *Builder, source: FileSource) *TranslateCStep { pub fn addTranslateC(self: *Builder, options: TranslateCStep.Options) *TranslateCStep {
return TranslateCStep.create(self, source.dupe(self)); return TranslateCStep.create(self, options);
} }
pub fn make(self: *Builder, step_names: []const []const u8) !void { pub fn make(self: *Builder, step_names: []const []const u8) !void {

View File

@ -19,11 +19,19 @@ include_dirs: std.ArrayList([]const u8),
c_macros: std.ArrayList([]const u8), c_macros: std.ArrayList([]const u8),
output_dir: ?[]const u8, output_dir: ?[]const u8,
out_basename: []const u8, out_basename: []const u8,
target: CrossTarget = CrossTarget{}, target: CrossTarget,
optimize: std.builtin.OptimizeMode,
output_file: build.GeneratedFile, output_file: build.GeneratedFile,
pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep { pub const Options = struct {
source_file: build.FileSource,
target: CrossTarget,
optimize: std.builtin.OptimizeMode,
};
pub fn create(builder: *Builder, options: Options) *TranslateCStep {
const self = builder.allocator.create(TranslateCStep) catch unreachable; const self = builder.allocator.create(TranslateCStep) catch unreachable;
const source = options.source_file.dupe(builder);
self.* = TranslateCStep{ self.* = TranslateCStep{
.step = Step.init(.translate_c, "translate-c", builder.allocator, make), .step = Step.init(.translate_c, "translate-c", builder.allocator, make),
.builder = builder, .builder = builder,
@ -32,19 +40,32 @@ pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep {
.c_macros = std.ArrayList([]const u8).init(builder.allocator), .c_macros = std.ArrayList([]const u8).init(builder.allocator),
.output_dir = null, .output_dir = null,
.out_basename = undefined, .out_basename = undefined,
.target = options.target,
.optimize = options.optimize,
.output_file = build.GeneratedFile{ .step = &self.step }, .output_file = build.GeneratedFile{ .step = &self.step },
}; };
source.addStepDependencies(&self.step); source.addStepDependencies(&self.step);
return self; return self;
} }
pub fn setTarget(self: *TranslateCStep, target: CrossTarget) void { pub const AddExecutableOptions = struct {
self.target = target; name: ?[]const u8 = null,
} version: ?std.builtin.Version = null,
target: ?CrossTarget = null,
optimize: ?std.builtin.Mode = null,
linkage: ?LibExeObjStep.Linkage = null,
};
/// Creates a step to build an executable from the translated source. /// Creates a step to build an executable from the translated source.
pub fn addExecutable(self: *TranslateCStep) *LibExeObjStep { pub fn addExecutable(self: *TranslateCStep, options: AddExecutableOptions) *LibExeObjStep {
return self.builder.addExecutableSource("translated_c", build.FileSource{ .generated = &self.output_file }); return self.builder.addExecutable(.{
.root_source_file = .{ .generated = &self.output_file },
.name = options.name orelse "translated_c",
.version = options.version,
.target = options.target orelse self.target,
.optimize = options.optimize orelse self.optimize,
.linkage = options.linkage,
});
} }
pub fn addIncludeDir(self: *TranslateCStep, include_dir: []const u8) void { pub fn addIncludeDir(self: *TranslateCStep, include_dir: []const u8) void {
@ -82,6 +103,11 @@ fn make(step: *Step) !void {
try argv_list.append(try self.target.zigTriple(self.builder.allocator)); try argv_list.append(try self.target.zigTriple(self.builder.allocator));
} }
switch (self.optimize) {
.Debug => {}, // Skip since it's the default.
else => try argv_list.append(self.builder.fmt("-O{s}", .{@tagName(self.optimize)})),
}
for (self.include_dirs.items) |include_dir| { for (self.include_dirs.items) |include_dir| {
try argv_list.append("-I"); try argv_list.append("-I");
try argv_list.append(include_dir); try argv_list.append(include_dir);

View File

@ -131,13 +131,16 @@ pub const CodeModel = enum {
/// This data structure is used by the Zig language code generation and /// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation. /// therefore must be kept in sync with the compiler implementation.
pub const Mode = enum { pub const OptimizeMode = enum {
Debug, Debug,
ReleaseSafe, ReleaseSafe,
ReleaseFast, ReleaseFast,
ReleaseSmall, ReleaseSmall,
}; };
/// Deprecated; use OptimizeMode.
pub const Mode = OptimizeMode;
/// This data structure is used by the Zig language code generation and /// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation. /// therefore must be kept in sync with the compiler implementation.
pub const CallingConvention = enum { pub const CallingConvention = enum {

View File

@ -3596,7 +3596,8 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
man.hash.addOptionalBytes(options.sysroot); man.hash.addOptionalBytes(options.sysroot);
try man.addOptionalFile(options.entitlements); try man.addOptionalFile(options.entitlements);
// We don't actually care whether it's a cache hit or miss; we just need the digest and the lock. // We don't actually care whether it's a cache hit or miss; we just
// need the digest and the lock.
_ = try man.hit(); _ = try man.hit();
digest = man.final(); digest = man.final();
@ -4177,9 +4178,11 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
log.debug("failed to save linking hash digest file: {s}", .{@errorName(err)}); log.debug("failed to save linking hash digest file: {s}", .{@errorName(err)});
}; };
// Again failure here only means an unnecessary cache miss. // Again failure here only means an unnecessary cache miss.
if (man.have_exclusive_lock) {
man.writeManifest() catch |err| { man.writeManifest() catch |err| {
log.debug("failed to write cache manifest when linking: {s}", .{@errorName(err)}); log.debug("failed to write cache manifest when linking: {s}", .{@errorName(err)});
}; };
}
// We hang on to this lock so that the output file path can be used without // We hang on to this lock so that the output file path can be used without
// other processes clobbering it. // other processes clobbering it.
macho_file.base.lock = man.toOwnedLock(); macho_file.base.lock = man.toOwnedLock();

View File

@ -1,12 +1,15 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
const exe = b.addExecutable("bss", "main.zig"); const exe = b.addExecutable(.{
.name = "bss",
.root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
});
b.default_step.dependOn(&exe.step); b.default_step.dependOn(&exe.step);
exe.setBuildMode(mode);
const run = exe.run(); const run = exe.run();
run.expectStdOutEqual("0, 1, 0\n"); run.expectStdOutEqual("0, 1, 0\n");

View File

@ -1,14 +1,19 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const lib_a = b.addStaticLibrary("a", null); const lib_a = b.addStaticLibrary(.{
.name = "a",
.optimize = optimize,
.target = .{},
});
lib_a.addCSourceFiles(&.{ "c.c", "a.c", "b.c" }, &.{"-fcommon"}); lib_a.addCSourceFiles(&.{ "c.c", "a.c", "b.c" }, &.{"-fcommon"});
lib_a.setBuildMode(mode);
const test_exe = b.addTest("main.zig"); const test_exe = b.addTest(.{
test_exe.setBuildMode(mode); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
});
test_exe.linkLibrary(lib_a); test_exe.linkLibrary(lib_a);
const test_step = b.step("test", "Test it"); const test_step = b.step("test", "Test it");

View File

@ -1,14 +1,21 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const lib_a = b.addStaticLibrary("a", null); const lib_a = b.addStaticLibrary(.{
.name = "a",
.optimize = optimize,
.target = target,
});
lib_a.addCSourceFiles(&.{"a.c"}, &.{"-fcommon"}); lib_a.addCSourceFiles(&.{"a.c"}, &.{"-fcommon"});
lib_a.setBuildMode(mode);
const test_exe = b.addTest("main.zig"); const test_exe = b.addTest(.{
test_exe.setBuildMode(mode); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = target,
});
test_exe.linkLibrary(lib_a); test_exe.linkLibrary(lib_a);
const test_step = b.step("test", "Test it"); const test_step = b.step("test", "Test it");

View File

@ -1,20 +1,30 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const lib_a = b.addStaticLibrary("a", null); const lib_a = b.addStaticLibrary(.{
.name = "a",
.optimize = optimize,
.target = target,
});
lib_a.addCSourceFile("a.c", &[_][]const u8{}); lib_a.addCSourceFile("a.c", &[_][]const u8{});
lib_a.setBuildMode(mode);
lib_a.addIncludePath("."); lib_a.addIncludePath(".");
const lib_b = b.addStaticLibrary("b", null); const lib_b = b.addStaticLibrary(.{
.name = "b",
.optimize = optimize,
.target = target,
});
lib_b.addCSourceFile("b.c", &[_][]const u8{}); lib_b.addCSourceFile("b.c", &[_][]const u8{});
lib_b.setBuildMode(mode);
lib_b.addIncludePath("."); lib_b.addIncludePath(".");
const test_exe = b.addTest("main.zig"); const test_exe = b.addTest(.{
test_exe.setBuildMode(mode); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = target,
});
test_exe.linkLibrary(lib_a); test_exe.linkLibrary(lib_a);
test_exe.linkLibrary(lib_b); test_exe.linkLibrary(lib_b);
test_exe.addIncludePath("."); test_exe.addIncludePath(".");

View File

@ -2,7 +2,7 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const target_info = std.zig.system.NativeTargetInfo.detect(target) catch unreachable; const target_info = std.zig.system.NativeTargetInfo.detect(target) catch unreachable;
@ -11,7 +11,10 @@ pub fn build(b: *Builder) void {
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
.optimize = optimize,
});
b.default_step.dependOn(&exe.step); b.default_step.dependOn(&exe.step);
exe.addIncludePath(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/include" }) catch unreachable); exe.addIncludePath(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/include" }) catch unreachable);
exe.addIncludePath(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/include/c++/v1" }) catch unreachable); exe.addIncludePath(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/include/c++/v1" }) catch unreachable);
@ -20,7 +23,6 @@ pub fn build(b: *Builder) void {
"-nostdinc++", "-nostdinc++",
}); });
exe.addObjectFile(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/lib/libc++.tbd" }) catch unreachable); exe.addObjectFile(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/lib/libc++.tbd" }) catch unreachable);
exe.setBuildMode(mode);
const run_cmd = exe.run(); const run_cmd = exe.run();
run_cmd.expectStdErrEqual("x: 5\n"); run_cmd.expectStdErrEqual("x: 5\n");

View File

@ -3,14 +3,17 @@ const Builder = std.build.Builder;
const LibExeObjectStep = std.build.LibExeObjStep; const LibExeObjectStep = std.build.LibExeObjStep;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
const exe = b.addExecutable("test", "main.zig"); const exe = b.addExecutable(.{
exe.setBuildMode(mode); .name = "test",
exe.setTarget(target); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = target,
});
const run = exe.runEmulatable(); const run = exe.runEmulatable();
test_step.dependOn(&run.step); test_step.dependOn(&run.step);

View File

@ -3,7 +3,7 @@ const Builder = std.build.Builder;
const LibExeObjectStep = std.build.LibExeObjStep; const LibExeObjectStep = std.build.LibExeObjStep;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
@ -11,7 +11,7 @@ pub fn build(b: *Builder) void {
{ {
// Without -dead_strip, we expect `iAmUnused` symbol present // Without -dead_strip, we expect `iAmUnused` symbol present
const exe = createScenario(b, mode, target); const exe = createScenario(b, optimize, target);
const check = exe.checkObject(.macho); const check = exe.checkObject(.macho);
check.checkInSymtab(); check.checkInSymtab();
@ -24,7 +24,7 @@ pub fn build(b: *Builder) void {
{ {
// With -dead_strip, no `iAmUnused` symbol should be present // With -dead_strip, no `iAmUnused` symbol should be present
const exe = createScenario(b, mode, target); const exe = createScenario(b, optimize, target);
exe.link_gc_sections = true; exe.link_gc_sections = true;
const check = exe.checkObject(.macho); const check = exe.checkObject(.macho);
@ -37,11 +37,13 @@ pub fn build(b: *Builder) void {
} }
} }
fn createScenario(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *LibExeObjectStep { fn createScenario(b: *Builder, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) *LibExeObjectStep {
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
.optimize = optimize,
.target = target,
});
exe.addCSourceFile("main.c", &[0][]const u8{}); exe.addCSourceFile("main.c", &[0][]const u8{});
exe.setBuildMode(mode);
exe.setTarget(target);
exe.linkLibC(); exe.linkLibC();
return exe; return exe;
} }

View File

@ -3,14 +3,14 @@ const Builder = std.build.Builder;
const LibExeObjectStep = std.build.LibExeObjStep; const LibExeObjectStep = std.build.LibExeObjStep;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
{ {
// Without -dead_strip_dylibs we expect `-la` to include liba.dylib in the final executable // Without -dead_strip_dylibs we expect `-la` to include liba.dylib in the final executable
const exe = createScenario(b, mode); const exe = createScenario(b, optimize);
const check = exe.checkObject(.macho); const check = exe.checkObject(.macho);
check.checkStart("cmd LOAD_DYLIB"); check.checkStart("cmd LOAD_DYLIB");
@ -27,7 +27,7 @@ pub fn build(b: *Builder) void {
{ {
// With -dead_strip_dylibs, we should include liba.dylib as it's unreachable // With -dead_strip_dylibs, we should include liba.dylib as it's unreachable
const exe = createScenario(b, mode); const exe = createScenario(b, optimize);
exe.dead_strip_dylibs = true; exe.dead_strip_dylibs = true;
const run_cmd = exe.run(); const run_cmd = exe.run();
@ -36,10 +36,12 @@ pub fn build(b: *Builder) void {
} }
} }
fn createScenario(b: *Builder, mode: std.builtin.Mode) *LibExeObjectStep { fn createScenario(b: *Builder, optimize: std.builtin.OptimizeMode) *LibExeObjectStep {
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
.optimize = optimize,
});
exe.addCSourceFile("main.c", &[0][]const u8{}); exe.addCSourceFile("main.c", &[0][]const u8{});
exe.setBuildMode(mode);
exe.linkLibC(); exe.linkLibC();
exe.linkFramework("Cocoa"); exe.linkFramework("Cocoa");
return exe; return exe;

View File

@ -2,15 +2,18 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); const dylib = b.addSharedLibrary(.{
dylib.setBuildMode(mode); .name = "a",
dylib.setTarget(target); .version = .{ .major = 1, .minor = 0 },
.optimize = optimize,
.target = target,
});
dylib.addCSourceFile("a.c", &.{}); dylib.addCSourceFile("a.c", &.{});
dylib.linkLibC(); dylib.linkLibC();
dylib.install(); dylib.install();
@ -24,9 +27,11 @@ pub fn build(b: *Builder) void {
test_step.dependOn(&check_dylib.step); test_step.dependOn(&check_dylib.step);
const exe = b.addExecutable("main", null); const exe = b.addExecutable(.{
exe.setTarget(target); .name = "main",
exe.setBuildMode(mode); .optimize = optimize,
.target = target,
});
exe.addCSourceFile("main.c", &.{}); exe.addCSourceFile("main.c", &.{});
exe.linkSystemLibrary("a"); exe.linkSystemLibrary("a");
exe.linkLibC(); exe.linkLibC();

View File

@ -2,17 +2,19 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
.optimize = optimize,
.target = target,
});
exe.addCSourceFile("main.c", &[0][]const u8{}); exe.addCSourceFile("main.c", &[0][]const u8{});
exe.addCSourceFile("empty.c", &[0][]const u8{}); exe.addCSourceFile("empty.c", &[0][]const u8{});
exe.setBuildMode(mode);
exe.setTarget(target);
exe.linkLibC(); exe.linkLibC();
const run_cmd = std.build.EmulatableRunStep.create(b, "run", exe); const run_cmd = std.build.EmulatableRunStep.create(b, "run", exe);

View File

@ -2,14 +2,16 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const exe = b.addExecutable("main", null); const exe = b.addExecutable(.{
exe.setTarget(.{ .os_tag = .macos }); .name = "main",
exe.setBuildMode(mode); .optimize = optimize,
.target = .{ .os_tag = .macos },
});
exe.addCSourceFile("main.c", &.{}); exe.addCSourceFile("main.c", &.{});
exe.linkLibC(); exe.linkLibC();
exe.entry_symbol_name = "_non_main"; exe.entry_symbol_name = "_non_main";

View File

@ -4,14 +4,14 @@ const Builder = std.build.Builder;
const LibExeObjectStep = std.build.LibExeObjStep; const LibExeObjectStep = std.build.LibExeObjStep;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
{ {
// Test -headerpad_max_install_names // Test -headerpad_max_install_names
const exe = simpleExe(b, mode); const exe = simpleExe(b, optimize);
exe.headerpad_max_install_names = true; exe.headerpad_max_install_names = true;
const check = exe.checkObject(.macho); const check = exe.checkObject(.macho);
@ -36,7 +36,7 @@ pub fn build(b: *Builder) void {
{ {
// Test -headerpad // Test -headerpad
const exe = simpleExe(b, mode); const exe = simpleExe(b, optimize);
exe.headerpad_size = 0x10000; exe.headerpad_size = 0x10000;
const check = exe.checkObject(.macho); const check = exe.checkObject(.macho);
@ -52,7 +52,7 @@ pub fn build(b: *Builder) void {
{ {
// Test both flags with -headerpad overriding -headerpad_max_install_names // Test both flags with -headerpad overriding -headerpad_max_install_names
const exe = simpleExe(b, mode); const exe = simpleExe(b, optimize);
exe.headerpad_max_install_names = true; exe.headerpad_max_install_names = true;
exe.headerpad_size = 0x10000; exe.headerpad_size = 0x10000;
@ -69,7 +69,7 @@ pub fn build(b: *Builder) void {
{ {
// Test both flags with -headerpad_max_install_names overriding -headerpad // Test both flags with -headerpad_max_install_names overriding -headerpad
const exe = simpleExe(b, mode); const exe = simpleExe(b, optimize);
exe.headerpad_size = 0x1000; exe.headerpad_size = 0x1000;
exe.headerpad_max_install_names = true; exe.headerpad_max_install_names = true;
@ -94,9 +94,11 @@ pub fn build(b: *Builder) void {
} }
} }
fn simpleExe(b: *Builder, mode: std.builtin.Mode) *LibExeObjectStep { fn simpleExe(b: *Builder, optimize: std.builtin.OptimizeMode) *LibExeObjectStep {
const exe = b.addExecutable("main", null); const exe = b.addExecutable(.{
exe.setBuildMode(mode); .name = "main",
.optimize = optimize,
});
exe.addCSourceFile("main.c", &.{}); exe.addCSourceFile("main.c", &.{});
exe.linkLibC(); exe.linkLibC();
exe.linkFramework("CoreFoundation"); exe.linkFramework("CoreFoundation");

View File

@ -1,15 +1,18 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.build.Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = std.zig.CrossTarget{ .os_tag = .macos }; const target = std.zig.CrossTarget{ .os_tag = .macos };
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const obj = b.addObject("test", "main.zig"); const obj = b.addObject(.{
obj.setBuildMode(mode); .name = "test",
obj.setTarget(target); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = target,
});
const check = obj.checkObject(.macho); const check = obj.checkObject(.macho);
@ -19,7 +22,7 @@ pub fn build(b: *std.build.Builder) void {
check.checkInSymtab(); check.checkInSymtab();
check.checkNext("{*} (__TEXT,__TestFn) external _testFn"); check.checkNext("{*} (__TEXT,__TestFn) external _testFn");
if (mode == .Debug) { if (optimize == .Debug) {
check.checkInSymtab(); check.checkInSymtab();
check.checkNext("{*} (__TEXT,__TestGenFnA) _main.testGenericFn__anon_{*}"); check.checkNext("{*} (__TEXT,__TestGenFnA) _main.testGenericFn__anon_{*}");
} }

View File

@ -3,16 +3,18 @@ const Builder = std.build.Builder;
const LibExeObjectStep = std.build.LibExeObjStep; const LibExeObjectStep = std.build.LibExeObjStep;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
// -dead_strip_dylibs // -dead_strip_dylibs
// -needed_framework Cocoa // -needed_framework Cocoa
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
.optimize = optimize,
});
exe.addCSourceFile("main.c", &[0][]const u8{}); exe.addCSourceFile("main.c", &[0][]const u8{});
exe.setBuildMode(mode);
exe.linkLibC(); exe.linkLibC();
exe.linkFrameworkNeeded("Cocoa"); exe.linkFrameworkNeeded("Cocoa");
exe.dead_strip_dylibs = true; exe.dead_strip_dylibs = true;

View File

@ -3,25 +3,30 @@ const Builder = std.build.Builder;
const LibExeObjectStep = std.build.LibExeObjStep; const LibExeObjectStep = std.build.LibExeObjStep;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); const dylib = b.addSharedLibrary(.{
dylib.setTarget(target); .name = "a",
dylib.setBuildMode(mode); .version = .{ .major = 1, .minor = 0 },
.optimize = optimize,
.target = target,
});
dylib.addCSourceFile("a.c", &.{}); dylib.addCSourceFile("a.c", &.{});
dylib.linkLibC(); dylib.linkLibC();
dylib.install(); dylib.install();
// -dead_strip_dylibs // -dead_strip_dylibs
// -needed-la // -needed-la
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
.optimize = optimize,
.target = target,
});
exe.addCSourceFile("main.c", &[0][]const u8{}); exe.addCSourceFile("main.c", &[0][]const u8{});
exe.setBuildMode(mode);
exe.setTarget(target);
exe.linkLibC(); exe.linkLibC();
exe.linkSystemLibraryNeeded("a"); exe.linkSystemLibraryNeeded("a");
exe.addLibraryPath(b.pathFromRoot("zig-out/lib")); exe.addLibraryPath(b.pathFromRoot("zig-out/lib"));

View File

@ -2,15 +2,17 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
.optimize = optimize,
});
exe.addIncludePath("."); exe.addIncludePath(".");
exe.addCSourceFile("Foo.m", &[0][]const u8{}); exe.addCSourceFile("Foo.m", &[0][]const u8{});
exe.addCSourceFile("test.m", &[0][]const u8{}); exe.addCSourceFile("test.m", &[0][]const u8{});
exe.setBuildMode(mode);
exe.linkLibC(); exe.linkLibC();
// TODO when we figure out how to ship framework stubs for cross-compilation, // TODO when we figure out how to ship framework stubs for cross-compilation,
// populate paths to the sysroot here. // populate paths to the sysroot here.

View File

@ -2,16 +2,18 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
.optimize = optimize,
});
b.default_step.dependOn(&exe.step); b.default_step.dependOn(&exe.step);
exe.addIncludePath("."); exe.addIncludePath(".");
exe.addCSourceFile("Foo.mm", &[0][]const u8{}); exe.addCSourceFile("Foo.mm", &[0][]const u8{});
exe.addCSourceFile("test.mm", &[0][]const u8{}); exe.addCSourceFile("test.mm", &[0][]const u8{});
exe.setBuildMode(mode);
exe.linkLibCpp(); exe.linkLibCpp();
// TODO when we figure out how to ship framework stubs for cross-compilation, // TODO when we figure out how to ship framework stubs for cross-compilation,
// populate paths to the sysroot here. // populate paths to the sysroot here.

View File

@ -2,16 +2,18 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
{ {
const exe = b.addExecutable("pagezero", null); const exe = b.addExecutable(.{
exe.setTarget(target); .name = "pagezero",
exe.setBuildMode(mode); .optimize = optimize,
.target = target,
});
exe.addCSourceFile("main.c", &.{}); exe.addCSourceFile("main.c", &.{});
exe.linkLibC(); exe.linkLibC();
exe.pagezero_size = 0x4000; exe.pagezero_size = 0x4000;
@ -29,9 +31,11 @@ pub fn build(b: *Builder) void {
} }
{ {
const exe = b.addExecutable("no_pagezero", null); const exe = b.addExecutable(.{
exe.setTarget(target); .name = "no_pagezero",
exe.setBuildMode(mode); .optimize = optimize,
.target = target,
});
exe.addCSourceFile("main.c", &.{}); exe.addCSourceFile("main.c", &.{});
exe.linkLibC(); exe.linkLibC();
exe.pagezero_size = 0; exe.pagezero_size = 0;

View File

@ -3,7 +3,7 @@ const Builder = std.build.Builder;
const LibExeObjectStep = std.build.LibExeObjStep; const LibExeObjectStep = std.build.LibExeObjStep;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
@ -11,7 +11,7 @@ pub fn build(b: *Builder) void {
{ {
// -search_dylibs_first // -search_dylibs_first
const exe = createScenario(b, mode, target); const exe = createScenario(b, optimize, target);
exe.search_strategy = .dylibs_first; exe.search_strategy = .dylibs_first;
const check = exe.checkObject(.macho); const check = exe.checkObject(.macho);
@ -26,7 +26,7 @@ pub fn build(b: *Builder) void {
{ {
// -search_paths_first // -search_paths_first
const exe = createScenario(b, mode, target); const exe = createScenario(b, optimize, target);
exe.search_strategy = .paths_first; exe.search_strategy = .paths_first;
const run = std.build.EmulatableRunStep.create(b, "run", exe); const run = std.build.EmulatableRunStep.create(b, "run", exe);
@ -36,10 +36,12 @@ pub fn build(b: *Builder) void {
} }
} }
fn createScenario(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *LibExeObjectStep { fn createScenario(b: *Builder, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) *LibExeObjectStep {
const static = b.addStaticLibrary("a", null); const static = b.addStaticLibrary(.{
static.setTarget(target); .name = "a",
static.setBuildMode(mode); .optimize = optimize,
.target = target,
});
static.addCSourceFile("a.c", &.{}); static.addCSourceFile("a.c", &.{});
static.linkLibC(); static.linkLibC();
static.override_dest_dir = std.build.InstallDir{ static.override_dest_dir = std.build.InstallDir{
@ -47,9 +49,12 @@ fn createScenario(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarg
}; };
static.install(); static.install();
const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); const dylib = b.addSharedLibrary(.{
dylib.setTarget(target); .name = "a",
dylib.setBuildMode(mode); .version = .{ .major = 1, .minor = 0 },
.optimize = optimize,
.target = target,
});
dylib.addCSourceFile("a.c", &.{}); dylib.addCSourceFile("a.c", &.{});
dylib.linkLibC(); dylib.linkLibC();
dylib.override_dest_dir = std.build.InstallDir{ dylib.override_dest_dir = std.build.InstallDir{
@ -57,9 +62,11 @@ fn createScenario(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarg
}; };
dylib.install(); dylib.install();
const exe = b.addExecutable("main", null); const exe = b.addExecutable(.{
exe.setTarget(target); .name = "main",
exe.setBuildMode(mode); .optimize = optimize,
.target = target,
});
exe.addCSourceFile("main.c", &.{}); exe.addCSourceFile("main.c", &.{});
exe.linkSystemLibraryName("a"); exe.linkSystemLibraryName("a");
exe.linkLibC(); exe.linkLibC();

View File

@ -2,15 +2,17 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const exe = b.addExecutable("main", null); const exe = b.addExecutable(.{
exe.setTarget(target); .name = "main",
exe.setBuildMode(mode); .optimize = optimize,
.target = target,
});
exe.addCSourceFile("main.c", &.{}); exe.addCSourceFile("main.c", &.{});
exe.linkLibC(); exe.linkLibC();
exe.stack_size = 0x100000000; exe.stack_size = 0x100000000;

View File

@ -4,15 +4,18 @@ const Builder = std.build.Builder;
const LibExeObjectStep = std.build.LibExeObjStep; const LibExeObjectStep = std.build.LibExeObjStep;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const exe = b.addExecutable("main", "main.zig"); const exe = b.addExecutable(.{
exe.setBuildMode(mode); .name = "main",
exe.setTarget(target); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = target,
});
exe.linkLibC(); exe.linkLibC();
const check_exe = exe.checkObject(.macho); const check_exe = exe.checkObject(.macho);

View File

@ -2,18 +2,23 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const lib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); const lib = b.addSharedLibrary(.{
lib.setBuildMode(mode); .name = "a",
lib.setTarget(target); .version = .{ .major = 1, .minor = 0 },
.optimize = optimize,
.target = target,
});
lib.addCSourceFile("a.c", &.{}); lib.addCSourceFile("a.c", &.{});
lib.linkLibC(); lib.linkLibC();
const test_exe = b.addTest("main.zig"); const test_exe = b.addTest(.{
test_exe.setBuildMode(mode); .root_source_file = .{ .path = "main.zig" },
test_exe.setTarget(target); .optimize = optimize,
.target = target,
});
test_exe.linkLibrary(lib); test_exe.linkLibrary(lib);
test_exe.linkLibC(); test_exe.linkLibC();

View File

@ -4,23 +4,23 @@ const Builder = std.build.Builder;
const LibExeObjectStep = std.build.LibExeObjStep; const LibExeObjectStep = std.build.LibExeObjStep;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
testUnwindInfo(b, test_step, mode, target, false); testUnwindInfo(b, test_step, optimize, target, false);
testUnwindInfo(b, test_step, mode, target, true); testUnwindInfo(b, test_step, optimize, target, true);
} }
fn testUnwindInfo( fn testUnwindInfo(
b: *Builder, b: *Builder,
test_step: *std.build.Step, test_step: *std.build.Step,
mode: std.builtin.Mode, optimize: std.builtin.OptimizeMode,
target: std.zig.CrossTarget, target: std.zig.CrossTarget,
dead_strip: bool, dead_strip: bool,
) void { ) void {
const exe = createScenario(b, mode, target); const exe = createScenario(b, optimize, target);
exe.link_gc_sections = dead_strip; exe.link_gc_sections = dead_strip;
const check = exe.checkObject(.macho); const check = exe.checkObject(.macho);
@ -52,8 +52,12 @@ fn testUnwindInfo(
test_step.dependOn(&run_cmd.step); test_step.dependOn(&run_cmd.step);
} }
fn createScenario(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *LibExeObjectStep { fn createScenario(b: *Builder, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) *LibExeObjectStep {
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
.optimize = optimize,
.target = target,
});
b.default_step.dependOn(&exe.step); b.default_step.dependOn(&exe.step);
exe.addIncludePath("."); exe.addIncludePath(".");
exe.addCSourceFiles(&[_][]const u8{ exe.addCSourceFiles(&[_][]const u8{
@ -61,8 +65,6 @@ fn createScenario(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarg
"simple_string.cpp", "simple_string.cpp",
"simple_string_owner.cpp", "simple_string_owner.cpp",
}, &[0][]const u8{}); }, &[0][]const u8{});
exe.setBuildMode(mode);
exe.setTarget(target);
exe.linkLibCpp(); exe.linkLibCpp();
return exe; return exe;
} }

View File

@ -29,21 +29,21 @@ pub fn build(b: *Builder) void {
fn testUuid( fn testUuid(
b: *Builder, b: *Builder,
test_step: *std.build.Step, test_step: *std.build.Step,
mode: std.builtin.Mode, optimize: std.builtin.OptimizeMode,
target: std.zig.CrossTarget, target: std.zig.CrossTarget,
comptime exp: []const u8, comptime exp: []const u8,
) void { ) void {
// The calculated UUID value is independent of debug info and so it should // The calculated UUID value is independent of debug info and so it should
// stay the same across builds. // stay the same across builds.
{ {
const dylib = simpleDylib(b, mode, target); const dylib = simpleDylib(b, optimize, target);
const check_dylib = dylib.checkObject(.macho); const check_dylib = dylib.checkObject(.macho);
check_dylib.checkStart("cmd UUID"); check_dylib.checkStart("cmd UUID");
check_dylib.checkNext("uuid " ++ exp); check_dylib.checkNext("uuid " ++ exp);
test_step.dependOn(&check_dylib.step); test_step.dependOn(&check_dylib.step);
} }
{ {
const dylib = simpleDylib(b, mode, target); const dylib = simpleDylib(b, optimize, target);
dylib.strip = true; dylib.strip = true;
const check_dylib = dylib.checkObject(.macho); const check_dylib = dylib.checkObject(.macho);
check_dylib.checkStart("cmd UUID"); check_dylib.checkStart("cmd UUID");
@ -52,10 +52,13 @@ fn testUuid(
} }
} }
fn simpleDylib(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *LibExeObjectStep { fn simpleDylib(b: *Builder, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) *LibExeObjectStep {
const dylib = b.addSharedLibrary("test", null, b.version(1, 0, 0)); const dylib = b.addSharedLibrary(.{
dylib.setTarget(target); .name = "test",
dylib.setBuildMode(mode); .version = .{ .major = 1, .minor = 0 },
.optimize = optimize,
.target = target,
});
dylib.addCSourceFile("test.c", &.{}); dylib.addCSourceFile("test.c", &.{});
dylib.linkLibC(); dylib.linkLibC();
return dylib; return dylib;

View File

@ -3,14 +3,16 @@ const Builder = std.build.Builder;
const LibExeObjectStep = std.build.LibExeObjStep; const LibExeObjectStep = std.build.LibExeObjStep;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
.optimize = optimize,
});
exe.addCSourceFile("main.c", &[0][]const u8{}); exe.addCSourceFile("main.c", &[0][]const u8{});
exe.setBuildMode(mode);
exe.linkLibC(); exe.linkLibC();
exe.linkFrameworkWeak("Cocoa"); exe.linkFrameworkWeak("Cocoa");

View File

@ -3,23 +3,28 @@ const Builder = std.build.Builder;
const LibExeObjectStep = std.build.LibExeObjStep; const LibExeObjectStep = std.build.LibExeObjStep;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const target: std.zig.CrossTarget = .{ .os_tag = .macos };
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); const dylib = b.addSharedLibrary(.{
dylib.setTarget(target); .name = "a",
dylib.setBuildMode(mode); .version = .{ .major = 1, .minor = 0, .patch = 0 },
.target = target,
.optimize = optimize,
});
dylib.addCSourceFile("a.c", &.{}); dylib.addCSourceFile("a.c", &.{});
dylib.linkLibC(); dylib.linkLibC();
dylib.install(); dylib.install();
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
.target = target,
.optimize = optimize,
});
exe.addCSourceFile("main.c", &[0][]const u8{}); exe.addCSourceFile("main.c", &[0][]const u8{});
exe.setTarget(target);
exe.setBuildMode(mode);
exe.linkLibC(); exe.linkLibC();
exe.linkSystemLibraryWeak("a"); exe.linkSystemLibraryWeak("a");
exe.addLibraryPath(b.pathFromRoot("zig-out/lib")); exe.addLibraryPath(b.pathFromRoot("zig-out/lib"));

View File

@ -2,16 +2,23 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const lib_a = b.addStaticLibrary("a", null); const lib_a = b.addStaticLibrary(.{
.name = "a",
.optimize = optimize,
.target = target,
});
lib_a.addCSourceFile("a.c", &[_][]const u8{}); lib_a.addCSourceFile("a.c", &[_][]const u8{});
lib_a.setBuildMode(mode);
lib_a.addIncludePath("."); lib_a.addIncludePath(".");
lib_a.install(); lib_a.install();
const test_exe = b.addTest("main.zig"); const test_exe = b.addTest(.{
test_exe.setBuildMode(mode); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = target,
});
test_exe.linkSystemLibrary("a"); // force linking liba.a as -la test_exe.linkSystemLibrary("a"); // force linking liba.a as -la
test_exe.addSystemIncludePath("."); test_exe.addSystemIncludePath(".");
const search_path = std.fs.path.join(b.allocator, &[_][]const u8{ b.install_path, "lib" }) catch unreachable; const search_path = std.fs.path.join(b.allocator, &[_][]const u8{ b.install_path, "lib" }) catch unreachable;

View File

@ -2,16 +2,17 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
// The code in question will pull-in compiler-rt, // The code in question will pull-in compiler-rt,
// and therefore link with its archive file. // and therefore link with its archive file.
const lib = b.addSharedLibrary("main", "main.zig", .unversioned); const lib = b.addSharedLibrary(.{
lib.setBuildMode(mode); .name = "main",
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .root_source_file = .{ .path = "main.zig" },
.optimize = b.standardOptimizeOption(.{}),
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
});
lib.use_llvm = false; lib.use_llvm = false;
lib.use_lld = false; lib.use_lld = false;
lib.strip = false; lib.strip = false;

View File

@ -1,14 +1,18 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.build.Builder) void {
const mode = b.standardReleaseOptions();
// Library with explicitly set cpu features // Library with explicitly set cpu features
const lib = b.addSharedLibrary("lib", "main.zig", .unversioned); const lib = b.addSharedLibrary(.{
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .name = "lib",
lib.target.cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp }; .root_source_file = .{ .path = "main.zig" },
lib.target.cpu_features_add.addFeature(0); // index 0 == atomics (see std.Target.wasm.Features) .optimize = b.standardOptimizeOption(.{}),
lib.setBuildMode(mode); .target = .{
.cpu_arch = .wasm32,
.cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
.cpu_features_add = std.Target.wasm.featureSet(&.{.atomics}),
.os_tag = .freestanding,
},
});
lib.use_llvm = false; lib.use_llvm = false;
lib.use_lld = false; lib.use_lld = false;

View File

@ -2,14 +2,15 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const lib = b.addSharedLibrary("lib", "lib.zig", .unversioned); const lib = b.addSharedLibrary(.{
lib.setBuildMode(mode); .name = "lib",
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .root_source_file = .{ .path = "lib.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
.optimize = b.standardOptimizeOption(.{}),
});
lib.use_llvm = false; lib.use_llvm = false;
lib.use_lld = false; lib.use_lld = false;
lib.strip = false; lib.strip = false;

View File

@ -5,9 +5,12 @@ pub fn build(b: *Builder) void {
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const lib = b.addSharedLibrary("lib", "lib.zig", .unversioned); const lib = b.addSharedLibrary(.{
lib.setBuildMode(.ReleaseSafe); // to make the output deterministic in address positions .name = "lib",
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .root_source_file = .{ .path = "lib.zig" },
.optimize = .ReleaseSafe, // to make the output deterministic in address positions
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
});
lib.use_lld = false; lib.use_lld = false;
lib.export_symbol_names = &.{ "foo", "bar" }; lib.export_symbol_names = &.{ "foo", "bar" };
lib.global_base = 0; // put data section at address 0 to make data symbols easier to parse lib.global_base = 0; // put data section at address 0 to make data symbols easier to parse

View File

@ -1,24 +1,33 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.build.Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const no_export = b.addSharedLibrary("no-export", "main.zig", .unversioned); const no_export = b.addSharedLibrary(.{
no_export.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .name = "no-export",
no_export.setBuildMode(mode); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
});
no_export.use_llvm = false; no_export.use_llvm = false;
no_export.use_lld = false; no_export.use_lld = false;
const dynamic_export = b.addSharedLibrary("dynamic", "main.zig", .unversioned); const dynamic_export = b.addSharedLibrary(.{
dynamic_export.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .name = "dynamic",
dynamic_export.setBuildMode(mode); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
});
dynamic_export.rdynamic = true; dynamic_export.rdynamic = true;
dynamic_export.use_llvm = false; dynamic_export.use_llvm = false;
dynamic_export.use_lld = false; dynamic_export.use_lld = false;
const force_export = b.addSharedLibrary("force", "main.zig", .unversioned); const force_export = b.addSharedLibrary(.{
force_export.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .name = "force",
force_export.setBuildMode(mode); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
});
force_export.export_symbol_names = &.{"foo"}; force_export.export_symbol_names = &.{"foo"};
force_export.use_llvm = false; force_export.use_llvm = false;
force_export.use_lld = false; force_export.use_lld = false;

View File

@ -2,14 +2,15 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const lib = b.addSharedLibrary("lib", "lib.zig", .unversioned); const lib = b.addSharedLibrary(.{
lib.setBuildMode(mode); .name = "lib",
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .root_source_file = .{ .path = "lib.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
.optimize = b.standardOptimizeOption(.{}),
});
lib.import_symbols = true; // import `a` and `b` lib.import_symbols = true; // import `a` and `b`
lib.rdynamic = true; // export `foo` lib.rdynamic = true; // export `foo`
lib.install(); lib.install();

View File

@ -1,10 +1,12 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.build.Builder) void {
const mode = b.standardReleaseOptions(); const exe = b.addExecutable(.{
const exe = b.addExecutable("extern", "main.zig"); .name = "extern",
exe.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .wasi }); .root_source_file = .{ .path = "main.zig" },
exe.setBuildMode(mode); .optimize = b.standardOptimizeOption(.{}),
.target = .{ .cpu_arch = .wasm32, .os_tag = .wasi },
});
exe.addCSourceFile("foo.c", &.{}); exe.addCSourceFile("foo.c", &.{});
exe.use_llvm = false; exe.use_llvm = false;
exe.use_lld = false; exe.use_lld = false;

View File

@ -2,28 +2,37 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const import_table = b.addSharedLibrary("lib", "lib.zig", .unversioned); const import_table = b.addSharedLibrary(.{
import_table.setBuildMode(mode); .name = "lib",
import_table.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .root_source_file = .{ .path = "lib.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
.optimize = optimize,
});
import_table.use_llvm = false; import_table.use_llvm = false;
import_table.use_lld = false; import_table.use_lld = false;
import_table.import_table = true; import_table.import_table = true;
const export_table = b.addSharedLibrary("lib", "lib.zig", .unversioned); const export_table = b.addSharedLibrary(.{
export_table.setBuildMode(mode); .name = "lib",
export_table.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .root_source_file = .{ .path = "lib.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
.optimize = optimize,
});
export_table.use_llvm = false; export_table.use_llvm = false;
export_table.use_lld = false; export_table.use_lld = false;
export_table.export_table = true; export_table.export_table = true;
const regular_table = b.addSharedLibrary("lib", "lib.zig", .unversioned); const regular_table = b.addSharedLibrary(.{
regular_table.setBuildMode(mode); .name = "lib",
regular_table.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .root_source_file = .{ .path = "lib.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
.optimize = optimize,
});
regular_table.use_llvm = false; regular_table.use_llvm = false;
regular_table.use_lld = false; regular_table.use_lld = false;

View File

@ -1,21 +1,32 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.build.Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
// Wasm Object file which we will use to infer the features from // Wasm Object file which we will use to infer the features from
const c_obj = b.addObject("c_obj", null); const c_obj = b.addObject(.{
c_obj.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .name = "c_obj",
c_obj.target.cpu_model = .{ .explicit = &std.Target.wasm.cpu.bleeding_edge }; .optimize = optimize,
.target = .{
.cpu_arch = .wasm32,
.cpu_model = .{ .explicit = &std.Target.wasm.cpu.bleeding_edge },
.os_tag = .freestanding,
},
});
c_obj.addCSourceFile("foo.c", &.{}); c_obj.addCSourceFile("foo.c", &.{});
c_obj.setBuildMode(mode);
// Wasm library that doesn't have any features specified. This will // Wasm library that doesn't have any features specified. This will
// infer its featureset from other linked object files. // infer its featureset from other linked object files.
const lib = b.addSharedLibrary("lib", "main.zig", .unversioned); const lib = b.addSharedLibrary(.{
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .name = "lib",
lib.target.cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp }; .root_source_file = .{ .path = "main.zig" },
lib.setBuildMode(mode); .optimize = optimize,
.target = .{
.cpu_arch = .wasm32,
.cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
.os_tag = .freestanding,
},
});
lib.use_llvm = false; lib.use_llvm = false;
lib.use_lld = false; lib.use_lld = false;
lib.addObject(c_obj); lib.addObject(c_obj);

View File

@ -3,14 +3,15 @@ const builtin = @import("builtin");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const lib = b.addSharedLibrary("lib", "lib.zig", .unversioned); const lib = b.addSharedLibrary(.{
lib.setBuildMode(mode); .name = "lib",
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .root_source_file = .{ .path = "lib.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
.optimize = b.standardOptimizeOption(.{}),
});
lib.use_llvm = false; lib.use_llvm = false;
lib.use_lld = false; lib.use_lld = false;
lib.strip = false; lib.strip = false;

View File

@ -2,14 +2,15 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const lib = b.addSharedLibrary("lib", "lib.zig", .unversioned); const lib = b.addSharedLibrary(.{
lib.setBuildMode(mode); .name = "lib",
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .root_source_file = .{ .path = "lib.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
.optimize = b.standardOptimizeOption(.{}),
});
lib.use_llvm = false; lib.use_llvm = false;
lib.use_lld = false; lib.use_lld = false;
lib.strip = false; lib.strip = false;

View File

@ -2,14 +2,15 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const lib = b.addSharedLibrary("lib", "lib.zig", .unversioned); const lib = b.addSharedLibrary(.{
lib.setBuildMode(mode); .name = "lib",
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .root_source_file = .{ .path = "lib.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
.optimize = b.standardOptimizeOption(.{}),
});
lib.use_llvm = false; lib.use_llvm = false;
lib.use_lld = false; lib.use_lld = false;
lib.strip = false; lib.strip = false;

View File

@ -2,14 +2,15 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const test_step = b.step("test", "Test"); const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
const lib = b.addSharedLibrary("lib", "lib.zig", .unversioned); const lib = b.addSharedLibrary(.{
lib.setBuildMode(mode); .name = "lib",
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); .root_source_file = .{ .path = "lib.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
.optimize = b.standardOptimizeOption(.{}),
});
lib.use_llvm = false; lib.use_llvm = false;
lib.use_lld = false; lib.use_lld = false;
lib.strip = false; lib.strip = false;

View File

@ -6,14 +6,14 @@ const ArrayList = std.ArrayList;
const fmt = std.fmt; const fmt = std.fmt;
const mem = std.mem; const mem = std.mem;
const fs = std.fs; const fs = std.fs;
const Mode = std.builtin.Mode; const OptimizeMode = std.builtin.OptimizeMode;
pub const CompareOutputContext = struct { pub const CompareOutputContext = struct {
b: *build.Builder, b: *build.Builder,
step: *build.Step, step: *build.Step,
test_index: usize, test_index: usize,
test_filter: ?[]const u8, test_filter: ?[]const u8,
modes: []const Mode, optimize_modes: []const OptimizeMode,
const Special = enum { const Special = enum {
None, None,
@ -102,7 +102,11 @@ pub const CompareOutputContext = struct {
if (mem.indexOf(u8, annotated_case_name, filter) == null) return; if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
} }
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
.name = "test",
.target = .{},
.optimize = .Debug,
});
exe.addAssemblyFileSource(write_src.getFileSource(case.sources.items[0].filename).?); exe.addAssemblyFileSource(write_src.getFileSource(case.sources.items[0].filename).?);
const run = exe.run(); const run = exe.run();
@ -113,19 +117,23 @@ pub const CompareOutputContext = struct {
self.step.dependOn(&run.step); self.step.dependOn(&run.step);
}, },
Special.None => { Special.None => {
for (self.modes) |mode| { for (self.optimize_modes) |optimize| {
const annotated_case_name = fmt.allocPrint(self.b.allocator, "{s} {s} ({s})", .{ const annotated_case_name = fmt.allocPrint(self.b.allocator, "{s} {s} ({s})", .{
"compare-output", "compare-output",
case.name, case.name,
@tagName(mode), @tagName(optimize),
}) catch unreachable; }) catch unreachable;
if (self.test_filter) |filter| { if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null) continue; if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
} }
const basename = case.sources.items[0].filename; const basename = case.sources.items[0].filename;
const exe = b.addExecutableSource("test", write_src.getFileSource(basename).?); const exe = b.addExecutable(.{
exe.setBuildMode(mode); .name = "test",
.root_source_file = write_src.getFileSource(basename).?,
.optimize = optimize,
.target = .{},
});
if (case.link_libc) { if (case.link_libc) {
exe.linkSystemLibrary("c"); exe.linkSystemLibrary("c");
} }
@ -139,13 +147,20 @@ pub const CompareOutputContext = struct {
} }
}, },
Special.RuntimeSafety => { Special.RuntimeSafety => {
// TODO iterate over self.optimize_modes and test this in both
// debug and release safe mode
const annotated_case_name = fmt.allocPrint(self.b.allocator, "safety {s}", .{case.name}) catch unreachable; const annotated_case_name = fmt.allocPrint(self.b.allocator, "safety {s}", .{case.name}) catch unreachable;
if (self.test_filter) |filter| { if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null) return; if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
} }
const basename = case.sources.items[0].filename; const basename = case.sources.items[0].filename;
const exe = b.addExecutableSource("test", write_src.getFileSource(basename).?); const exe = b.addExecutable(.{
.name = "test",
.root_source_file = write_src.getFileSource(basename).?,
.target = .{},
.optimize = .Debug,
});
if (case.link_libc) { if (case.link_libc) {
exe.linkSystemLibrary("c"); exe.linkSystemLibrary("c");
} }

View File

@ -85,11 +85,14 @@ pub const RunTranslatedCContext = struct {
for (case.sources.items) |src_file| { for (case.sources.items) |src_file| {
write_src.add(src_file.filename, src_file.source); write_src.add(src_file.filename, src_file.source);
} }
const translate_c = b.addTranslateC(write_src.getFileSource(case.sources.items[0].filename).?); const translate_c = b.addTranslateC(.{
.source_file = write_src.getFileSource(case.sources.items[0].filename).?,
.target = .{},
.optimize = .Debug,
});
translate_c.step.name = b.fmt("{s} translate-c", .{annotated_case_name}); translate_c.step.name = b.fmt("{s} translate-c", .{annotated_case_name});
const exe = translate_c.addExecutable(); const exe = translate_c.addExecutable(.{});
exe.setTarget(self.target);
exe.step.name = b.fmt("{s} build-exe", .{annotated_case_name}); exe.step.name = b.fmt("{s} build-exe", .{annotated_case_name});
exe.linkLibC(); exe.linkLibC();
const run = exe.run(); const run = exe.run();

View File

@ -108,10 +108,13 @@ pub const TranslateCContext = struct {
write_src.add(src_file.filename, src_file.source); write_src.add(src_file.filename, src_file.source);
} }
const translate_c = b.addTranslateC(write_src.getFileSource(case.sources.items[0].filename).?); const translate_c = b.addTranslateC(.{
.source_file = write_src.getFileSource(case.sources.items[0].filename).?,
.target = case.target,
.optimize = .Debug,
});
translate_c.step.name = annotated_case_name; translate_c.step.name = annotated_case_name;
translate_c.setTarget(case.target);
const check_file = translate_c.addCheckFile(case.expected_lines.items); const check_file = translate_c.addCheckFile(case.expected_lines.items);

View File

@ -1,8 +1,10 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const main = b.addTest("main.zig"); const main = b.addTest(.{
main.setBuildMode(b.standardReleaseOptions()); .root_source_file = .{ .path = "main.zig" },
.optimize = b.standardOptimizeOption(.{}),
});
const test_step = b.step("test", "Test it"); const test_step = b.step("test", "Test it");
test_step.dependOn(&main.step); test_step.dependOn(&main.step);

View File

@ -12,23 +12,27 @@ fn isRunnableTarget(t: CrossTarget) bool {
} }
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
const exe_c = b.addExecutable("test_c", null); const exe_c = b.addExecutable(.{
.name = "test_c",
.optimize = optimize,
.target = target,
});
b.default_step.dependOn(&exe_c.step); b.default_step.dependOn(&exe_c.step);
exe_c.addCSourceFile("test.c", &[0][]const u8{}); exe_c.addCSourceFile("test.c", &[0][]const u8{});
exe_c.setBuildMode(mode);
exe_c.setTarget(target);
exe_c.linkLibC(); exe_c.linkLibC();
const exe_cpp = b.addExecutable("test_cpp", null); const exe_cpp = b.addExecutable(.{
.name = "test_cpp",
.optimize = optimize,
.target = target,
});
b.default_step.dependOn(&exe_cpp.step); b.default_step.dependOn(&exe_cpp.step);
exe_cpp.addCSourceFile("test.cpp", &[0][]const u8{}); exe_cpp.addCSourceFile("test.cpp", &[0][]const u8{});
exe_cpp.setBuildMode(mode);
exe_cpp.setTarget(target);
exe_cpp.linkLibCpp(); exe_cpp.linkLibCpp();
switch (target.getOsTag()) { switch (target.getOsTag()) {

View File

@ -1,8 +1,10 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const main = b.addTest("main.zig"); const main = b.addTest(.{
main.setBuildMode(b.standardReleaseOptions()); .root_source_file = .{ .path = "main.zig" },
.optimize = b.standardOptimizeOption(.{}),
});
main.emit_asm = .{ .emit_to = b.pathFromRoot("main.s") }; main.emit_asm = .{ .emit_to = b.pathFromRoot("main.s") };
main.emit_bin = .{ .emit_to = b.pathFromRoot("main") }; main.emit_bin = .{ .emit_to = b.pathFromRoot("main") };

View File

@ -1,8 +1,11 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const main = b.addExecutable("main", "main.zig"); const main = b.addExecutable(.{
main.setBuildMode(b.standardReleaseOptions()); .name = "main",
.root_source_file = .{ .path = "main.zig" },
.optimize = b.standardOptimizeOption(.{}),
});
const run = main.run(); const run = main.run();
run.clearEnvironment(); run.clearEnvironment();

View File

@ -1,16 +1,26 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const obj1 = b.addStaticLibrary("obj1", "obj1.zig"); const obj1 = b.addStaticLibrary(.{
obj1.setBuildMode(mode); .name = "obj1",
.root_source_file = .{ .path = "obj1.zig" },
.optimize = optimize,
.target = .{},
});
const obj2 = b.addStaticLibrary("obj2", "obj2.zig"); const obj2 = b.addStaticLibrary(.{
obj2.setBuildMode(mode); .name = "obj2",
.root_source_file = .{ .path = "obj2.zig" },
.optimize = optimize,
.target = .{},
});
const main = b.addTest("main.zig"); const main = b.addTest(.{
main.setBuildMode(mode); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
});
main.linkLibrary(obj1); main.linkLibrary(obj1);
main.linkLibrary(obj2); main.linkLibrary(obj2);

View File

@ -10,11 +10,14 @@ pub fn build(b: *std.build.Builder) void {
.abi = .gnueabihf, .abi = .gnueabihf,
}; };
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const elf = b.addExecutable("zig-nrf52-blink.elf", "main.zig"); const elf = b.addExecutable(.{
elf.setTarget(target); .name = "zig-nrf52-blink.elf",
elf.setBuildMode(mode); .root_source_file = .{ .path = "main.zig" },
.target = target,
.optimize = optimize,
});
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
b.default_step.dependOn(test_step); b.default_step.dependOn(test_step);

View File

@ -12,11 +12,15 @@ fn isRunnableTarget(t: CrossTarget) bool {
} }
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const exe = b.addExecutable("zigtest", "main.zig"); const exe = b.addExecutable(.{
exe.setBuildMode(mode); .name = "zigtest",
.root_source_file = .{ .path = "main.zig" },
.target = target,
.optimize = optimize,
});
exe.install(); exe.install();
const c_sources = [_][]const u8{ const c_sources = [_][]const u8{
@ -39,7 +43,6 @@ pub fn build(b: *Builder) void {
exe.defineCMacro("QUX", "\"Q\" \"UX\""); exe.defineCMacro("QUX", "\"Q\" \"UX\"");
exe.defineCMacro("QUUX", "\"QU\\\"UX\""); exe.defineCMacro("QUUX", "\"QU\\\"UX\"");
exe.setTarget(target);
b.default_step.dependOn(&exe.step); b.default_step.dependOn(&exe.step);
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");

View File

@ -2,12 +2,15 @@ const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const obj = b.addObject("main", "main.zig"); const obj = b.addObject(.{
obj.setBuildMode(mode); .name = "main",
obj.setTarget(target); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = target,
});
obj.emit_llvm_ir = .{ .emit_to = b.pathFromRoot("main.ll") }; obj.emit_llvm_ir = .{ .emit_to = b.pathFromRoot("main.ll") };
obj.emit_llvm_bc = .{ .emit_to = b.pathFromRoot("main.bc") }; obj.emit_llvm_bc = .{ .emit_to = b.pathFromRoot("main.bc") };
obj.emit_bin = .no_emit; obj.emit_bin = .no_emit;

View File

@ -12,11 +12,15 @@ fn isRunnableTarget(t: CrossTarget) bool {
} }
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const exe = b.addExecutable("main", "main.zig"); const exe = b.addExecutable(.{
exe.setBuildMode(mode); .name = "main",
.root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = target,
});
exe.install(); exe.install();
const c_sources = [_][]const u8{ const c_sources = [_][]const u8{
@ -26,7 +30,6 @@ pub fn build(b: *Builder) void {
exe.addCSourceFiles(&c_sources, &.{}); exe.addCSourceFiles(&c_sources, &.{});
exe.linkLibC(); exe.linkLibC();
exe.setTarget(target);
b.default_step.dependOn(&exe.step); b.default_step.dependOn(&exe.step);
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");

View File

@ -4,13 +4,15 @@ const Builder = std.build.Builder;
const CrossTarget = std.zig.CrossTarget; const CrossTarget = std.zig.CrossTarget;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const obj = b.addObject("main", "main.zig"); const obj = b.addObject(.{
obj.setBuildMode(mode); .name = "main",
.root_source_file = .{ .path = "main.zig" },
obj.setTarget(target); .optimize = optimize,
.target = target,
});
b.default_step.dependOn(&obj.step); b.default_step.dependOn(&obj.step);
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");

View File

@ -1,7 +1,12 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const obj = b.addObject("test", "test.zig"); const obj = b.addObject(.{
.name = "test",
.root_source_file = .{ .path = "test.zig" },
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
});
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");
test_step.dependOn(&obj.step); test_step.dependOn(&obj.step);

View File

@ -6,17 +6,22 @@ pub fn build(b: *Builder) void {
.os_tag = .windows, .os_tag = .windows,
.abi = .msvc, .abi = .msvc,
}; };
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const obj = b.addObject("issue_5825", "main.zig"); const obj = b.addObject(.{
obj.setTarget(target); .name = "issue_5825",
obj.setBuildMode(mode); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = target,
});
const exe = b.addExecutable("issue_5825", null); const exe = b.addExecutable(.{
.name = "issue_5825",
.optimize = optimize,
.target = target,
});
exe.subsystem = .Console; exe.subsystem = .Console;
exe.linkSystemLibrary("kernel32"); exe.linkSystemLibrary("kernel32");
exe.linkSystemLibrary("ntdll"); exe.linkSystemLibrary("ntdll");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.addObject(obj); exe.addObject(obj);
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");

View File

@ -1,10 +1,13 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const exe = b.addExecutable("issue_7030", "main.zig"); const exe = b.addExecutable(.{
exe.setTarget(.{ .name = "issue_7030",
.root_source_file = .{ .path = "main.zig" },
.target = .{
.cpu_arch = .wasm32, .cpu_arch = .wasm32,
.os_tag = .freestanding, .os_tag = .freestanding,
},
}); });
exe.install(); exe.install();
b.default_step.dependOn(&exe.step); b.default_step.dependOn(&exe.step);

View File

@ -1,7 +1,9 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const test_artifact = b.addTest("main.zig"); const test_artifact = b.addTest(.{
.root_source_file = .{ .path = "main.zig" },
});
test_artifact.addIncludePath("a_directory"); test_artifact.addIncludePath("a_directory");
b.default_step.dependOn(&test_artifact.step); b.default_step.dependOn(&test_artifact.step);

View File

@ -8,12 +8,15 @@ pub fn build(b: *std.build.Builder) !void {
.explicit = &std.Target.arm.cpu.arm1176jz_s, .explicit = &std.Target.arm.cpu.arm1176jz_s,
}, },
}; };
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const kernel = b.addExecutable("kernel", "./main.zig"); const kernel = b.addExecutable(.{
.name = "kernel",
.root_source_file = .{ .path = "./main.zig" },
.optimize = optimize,
.target = target,
});
kernel.addObjectFile("./boot.S"); kernel.addObjectFile("./boot.S");
kernel.setLinkerScriptPath(.{ .path = "./linker.ld" }); kernel.setLinkerScriptPath(.{ .path = "./linker.ld" });
kernel.setBuildMode(mode);
kernel.setTarget(target);
kernel.install(); kernel.install();
const test_step = b.step("test", "Test it"); const test_step = b.step("test", "Test it");

View File

@ -1,9 +1,11 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.build.Builder) !void { pub fn build(b: *std.build.Builder) !void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const zip_add = b.addTest("main.zig"); const zip_add = b.addTest(.{
zip_add.setBuildMode(mode); .root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
});
zip_add.addCSourceFile("vendor/kuba-zip/zip.c", &[_][]const u8{ zip_add.addCSourceFile("vendor/kuba-zip/zip.c", &[_][]const u8{
"-std=c99", "-std=c99",
"-fno-sanitize=undefined", "-fno-sanitize=undefined",

View File

@ -1,13 +1,23 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const opts = b.standardReleaseOptions(); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lib = b.addSharedLibrary("add", "add.zig", b.version(1, 0, 0)); const lib = b.addSharedLibrary(.{
lib.setBuildMode(opts); .name = "add",
.root_source_file = .{ .path = "add.zig" },
.version = .{ .major = 1, .minor = 0 },
.optimize = optimize,
.target = target,
});
const main = b.addExecutable("main", "main.zig"); const main = b.addExecutable(.{
main.setBuildMode(opts); .name = "main",
.root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = target,
});
const run = main.run(); const run = main.run();
run.addArtifactArg(lib); run.addArtifactArg(lib);

View File

@ -1,7 +1,9 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const test_exe = b.addTest("a/test.zig"); const test_exe = b.addTest(.{
.root_source_file = .{ .path = "a/test.zig" },
});
test_exe.setMainPkgPath("."); test_exe.setMainPkgPath(".");
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");

View File

@ -12,14 +12,17 @@ fn isRunnableTarget(t: CrossTarget) bool {
} }
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const exe = b.addExecutable("test", "main.zig"); const exe = b.addExecutable(.{
.name = "test",
.root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
.target = target,
});
exe.addCSourceFile("test.c", &[_][]const u8{"-std=c11"}); exe.addCSourceFile("test.c", &[_][]const u8{"-std=c11"});
exe.setBuildMode(mode);
exe.linkLibC(); exe.linkLibC();
exe.setTarget(target);
b.default_step.dependOn(&exe.step); b.default_step.dependOn(&exe.step);
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");

View File

@ -1,9 +1,19 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const obj = b.addObject("base64", "base64.zig"); const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable("test", null); const obj = b.addObject(.{
.name = "base64",
.root_source_file = .{ .path = "base64.zig" },
.optimize = optimize,
.target = .{},
});
const exe = b.addExecutable(.{
.name = "test",
.optimize = optimize,
});
exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"}); exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"});
exe.addObject(obj); exe.addObject(obj);
exe.linkSystemLibrary("c"); exe.linkSystemLibrary("c");

View File

@ -2,11 +2,13 @@ const std = @import("std");
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.build.Builder) void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const main = b.addTest("src/main.zig"); const main = b.addTest(.{
main.setTarget(target); .root_source_file = .{ .path = "src/main.zig" },
main.setBuildMode(mode); .target = target,
.optimize = optimize,
});
const options = b.addOptions(); const options = b.addOptions();
main.addOptions("build_options", options); main.addOptions("build_options", options);

View File

@ -1,8 +1,10 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const main = b.addTest("main.zig"); const main = b.addTest(.{
main.setBuildMode(b.standardReleaseOptions()); .root_source_file = .{ .path = "main.zig" },
.optimize = b.standardOptimizeOption(.{}),
});
main.pie = true; main.pie = true;
const test_step = b.step("test", "Test the program"); const test_step = b.step("test", "Test the program");

View File

@ -1,13 +1,14 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const exe = b.addExecutable("test", "test.zig"); const optimize = b.standardOptimizeOption(.{});
exe.addPackagePath("my_pkg", "pkg.zig");
// This is duplicated to test that you are allowed to call const exe = b.addExecutable(.{
// b.standardReleaseOptions() twice. .name = "test",
exe.setBuildMode(b.standardReleaseOptions()); .root_source_file = .{ .path = "test.zig" },
exe.setBuildMode(b.standardReleaseOptions()); .optimize = optimize,
});
exe.addPackagePath("my_pkg", "pkg.zig");
const run = exe.run(); const run = exe.run();

View File

@ -1,12 +1,21 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0)); const lib = b.addSharedLibrary(.{
lib.setTarget(target); .name = "mathtest",
.root_source_file = .{ .path = "mathtest.zig" },
.version = .{ .major = 1, .minor = 0 },
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable("test", null); const exe = b.addExecutable(.{
exe.setTarget(target); .name = "test",
.target = target,
.optimize = optimize,
});
exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"}); exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"});
exe.linkLibrary(lib); exe.linkLibrary(lib);
exe.linkSystemLibrary("c"); exe.linkSystemLibrary("c");

View File

@ -1,15 +1,20 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const foo = b.addStaticLibrary("foo", null); const foo = b.addStaticLibrary(.{
.name = "foo",
.optimize = optimize,
.target = .{},
});
foo.addCSourceFile("foo.c", &[_][]const u8{}); foo.addCSourceFile("foo.c", &[_][]const u8{});
foo.setBuildMode(mode);
foo.addIncludePath("."); foo.addIncludePath(".");
const test_exe = b.addTest("foo.zig"); const test_exe = b.addTest(.{
test_exe.setBuildMode(mode); .root_source_file = .{ .path = "foo.zig" },
.optimize = optimize,
});
test_exe.linkLibrary(foo); test_exe.linkLibrary(foo);
test_exe.addIncludePath("."); test_exe.addIncludePath(".");

View File

@ -1,7 +1,10 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const test_exe = b.addTestExe("test", "test.zig"); const test_exe = b.addTest(.{
.root_source_file = .{ .path = "test.zig" },
.kind = .test_exe,
});
test_exe.test_runner = "test_runner.zig"; test_exe.test_runner = "test_runner.zig";
const test_run = test_exe.run(); const test_run = test_exe.run();

View File

@ -1,8 +1,10 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const main = b.addTest("main.zig"); const main = b.addTest(.{
main.setBuildMode(b.standardReleaseOptions()); .root_source_file = .{ .path = "main.zig" },
.optimize = b.standardOptimizeOption(.{}),
});
main.addIncludePath("."); main.addIncludePath(".");
const test_step = b.step("test", "Test it"); const test_step = b.step("test", "Test it");

View File

@ -1,13 +1,20 @@
const Builder = @import("std").build.Builder; const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const hello = b.addExecutable("hello", "hello.zig"); const hello = b.addExecutable(.{
hello.setBuildMode(mode); .name = "hello",
.root_source_file = .{ .path = "hello.zig" },
.optimize = optimize,
});
const main = b.addExecutable(.{
.name = "main",
.root_source_file = .{ .path = "main.zig" },
.optimize = optimize,
});
const main = b.addExecutable("main", "main.zig");
main.setBuildMode(mode);
const run = main.run(); const run = main.run();
run.addArtifactArg(hello); run.addArtifactArg(hello);

View File

@ -8,7 +8,7 @@ const fs = std.fs;
const mem = std.mem; const mem = std.mem;
const fmt = std.fmt; const fmt = std.fmt;
const ArrayList = std.ArrayList; const ArrayList = std.ArrayList;
const Mode = std.builtin.Mode; const OptimizeMode = std.builtin.OptimizeMode;
const LibExeObjStep = build.LibExeObjStep; const LibExeObjStep = build.LibExeObjStep;
const Allocator = mem.Allocator; const Allocator = mem.Allocator;
const ExecError = build.Builder.ExecError; const ExecError = build.Builder.ExecError;
@ -30,7 +30,7 @@ pub const CompareOutputContext = @import("src/compare_output.zig").CompareOutput
const TestTarget = struct { const TestTarget = struct {
target: CrossTarget = @as(CrossTarget, .{}), target: CrossTarget = @as(CrossTarget, .{}),
mode: std.builtin.Mode = .Debug, optimize_mode: std.builtin.OptimizeMode = .Debug,
link_libc: bool = false, link_libc: bool = false,
single_threaded: bool = false, single_threaded: bool = false,
disable_native: bool = false, disable_native: bool = false,
@ -423,38 +423,38 @@ const test_targets = blk: {
// Do the release tests last because they take a long time // Do the release tests last because they take a long time
.{ .{
.mode = .ReleaseFast, .optimize_mode = .ReleaseFast,
}, },
.{ .{
.link_libc = true, .link_libc = true,
.mode = .ReleaseFast, .optimize_mode = .ReleaseFast,
}, },
.{ .{
.mode = .ReleaseFast, .optimize_mode = .ReleaseFast,
.single_threaded = true, .single_threaded = true,
}, },
.{ .{
.mode = .ReleaseSafe, .optimize_mode = .ReleaseSafe,
}, },
.{ .{
.link_libc = true, .link_libc = true,
.mode = .ReleaseSafe, .optimize_mode = .ReleaseSafe,
}, },
.{ .{
.mode = .ReleaseSafe, .optimize_mode = .ReleaseSafe,
.single_threaded = true, .single_threaded = true,
}, },
.{ .{
.mode = .ReleaseSmall, .optimize_mode = .ReleaseSmall,
}, },
.{ .{
.link_libc = true, .link_libc = true,
.mode = .ReleaseSmall, .optimize_mode = .ReleaseSmall,
}, },
.{ .{
.mode = .ReleaseSmall, .optimize_mode = .ReleaseSmall,
.single_threaded = true, .single_threaded = true,
}, },
}; };
@ -462,14 +462,14 @@ const test_targets = blk: {
const max_stdout_size = 1 * 1024 * 1024; // 1 MB const max_stdout_size = 1 * 1024 * 1024; // 1 MB
pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8, optimize_modes: []const OptimizeMode) *build.Step {
const cases = b.allocator.create(CompareOutputContext) catch unreachable; const cases = b.allocator.create(CompareOutputContext) catch unreachable;
cases.* = CompareOutputContext{ cases.* = CompareOutputContext{
.b = b, .b = b,
.step = b.step("test-compare-output", "Run the compare output tests"), .step = b.step("test-compare-output", "Run the compare output tests"),
.test_index = 0, .test_index = 0,
.test_filter = test_filter, .test_filter = test_filter,
.modes = modes, .optimize_modes = optimize_modes,
}; };
compare_output.addCases(cases); compare_output.addCases(cases);
@ -477,14 +477,14 @@ pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8, modes:
return cases.step; return cases.step;
} }
pub fn addStackTraceTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { pub fn addStackTraceTests(b: *build.Builder, test_filter: ?[]const u8, optimize_modes: []const OptimizeMode) *build.Step {
const cases = b.allocator.create(StackTracesContext) catch unreachable; const cases = b.allocator.create(StackTracesContext) catch unreachable;
cases.* = StackTracesContext{ cases.* = StackTracesContext{
.b = b, .b = b,
.step = b.step("test-stack-traces", "Run the stack trace tests"), .step = b.step("test-stack-traces", "Run the stack trace tests"),
.test_index = 0, .test_index = 0,
.test_filter = test_filter, .test_filter = test_filter,
.modes = modes, .optimize_modes = optimize_modes,
}; };
stack_traces.addCases(cases); stack_traces.addCases(cases);
@ -495,7 +495,7 @@ pub fn addStackTraceTests(b: *build.Builder, test_filter: ?[]const u8, modes: []
pub fn addStandaloneTests( pub fn addStandaloneTests(
b: *build.Builder, b: *build.Builder,
test_filter: ?[]const u8, test_filter: ?[]const u8,
modes: []const Mode, optimize_modes: []const OptimizeMode,
skip_non_native: bool, skip_non_native: bool,
enable_macos_sdk: bool, enable_macos_sdk: bool,
target: std.zig.CrossTarget, target: std.zig.CrossTarget,
@ -513,7 +513,7 @@ pub fn addStandaloneTests(
.step = b.step("test-standalone", "Run the standalone tests"), .step = b.step("test-standalone", "Run the standalone tests"),
.test_index = 0, .test_index = 0,
.test_filter = test_filter, .test_filter = test_filter,
.modes = modes, .optimize_modes = optimize_modes,
.skip_non_native = skip_non_native, .skip_non_native = skip_non_native,
.enable_macos_sdk = enable_macos_sdk, .enable_macos_sdk = enable_macos_sdk,
.target = target, .target = target,
@ -534,7 +534,7 @@ pub fn addStandaloneTests(
pub fn addLinkTests( pub fn addLinkTests(
b: *build.Builder, b: *build.Builder,
test_filter: ?[]const u8, test_filter: ?[]const u8,
modes: []const Mode, optimize_modes: []const OptimizeMode,
enable_macos_sdk: bool, enable_macos_sdk: bool,
omit_stage2: bool, omit_stage2: bool,
enable_symlinks_windows: bool, enable_symlinks_windows: bool,
@ -545,7 +545,7 @@ pub fn addLinkTests(
.step = b.step("test-link", "Run the linker tests"), .step = b.step("test-link", "Run the linker tests"),
.test_index = 0, .test_index = 0,
.test_filter = test_filter, .test_filter = test_filter,
.modes = modes, .optimize_modes = optimize_modes,
.skip_non_native = true, .skip_non_native = true,
.enable_macos_sdk = enable_macos_sdk, .enable_macos_sdk = enable_macos_sdk,
.target = .{}, .target = .{},
@ -556,12 +556,17 @@ pub fn addLinkTests(
return cases.step; return cases.step;
} }
pub fn addCliTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { pub fn addCliTests(b: *build.Builder, test_filter: ?[]const u8, optimize_modes: []const OptimizeMode) *build.Step {
_ = test_filter; _ = test_filter;
_ = modes; _ = optimize_modes;
const step = b.step("test-cli", "Test the command line interface"); const step = b.step("test-cli", "Test the command line interface");
const exe = b.addExecutable("test-cli", "test/cli.zig"); const exe = b.addExecutable(.{
.name = "test-cli",
.root_source_file = .{ .path = "test/cli.zig" },
.target = .{},
.optimize = .Debug,
});
const run_cmd = exe.run(); const run_cmd = exe.run();
run_cmd.addArgs(&[_][]const u8{ run_cmd.addArgs(&[_][]const u8{
fs.realpathAlloc(b.allocator, b.zig_exe) catch unreachable, fs.realpathAlloc(b.allocator, b.zig_exe) catch unreachable,
@ -572,14 +577,14 @@ pub fn addCliTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const M
return step; return step;
} }
pub fn addAssembleAndLinkTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { pub fn addAssembleAndLinkTests(b: *build.Builder, test_filter: ?[]const u8, optimize_modes: []const OptimizeMode) *build.Step {
const cases = b.allocator.create(CompareOutputContext) catch unreachable; const cases = b.allocator.create(CompareOutputContext) catch unreachable;
cases.* = CompareOutputContext{ cases.* = CompareOutputContext{
.b = b, .b = b,
.step = b.step("test-asm-link", "Run the assemble and link tests"), .step = b.step("test-asm-link", "Run the assemble and link tests"),
.test_index = 0, .test_index = 0,
.test_filter = test_filter, .test_filter = test_filter,
.modes = modes, .optimize_modes = optimize_modes,
}; };
assemble_and_link.addCases(cases); assemble_and_link.addCases(cases);
@ -640,7 +645,7 @@ pub fn addPkgTests(
root_src: []const u8, root_src: []const u8,
name: []const u8, name: []const u8,
desc: []const u8, desc: []const u8,
modes: []const Mode, optimize_modes: []const OptimizeMode,
skip_single_threaded: bool, skip_single_threaded: bool,
skip_non_native: bool, skip_non_native: bool,
skip_libc: bool, skip_libc: bool,
@ -677,8 +682,8 @@ pub fn addPkgTests(
else => if (skip_stage2) continue, else => if (skip_stage2) continue,
}; };
const want_this_mode = for (modes) |m| { const want_this_mode = for (optimize_modes) |m| {
if (m == test_target.mode) break true; if (m == test_target.optimize_mode) break true;
} else false; } else false;
if (!want_this_mode) continue; if (!want_this_mode) continue;
@ -691,21 +696,23 @@ pub fn addPkgTests(
const triple_prefix = test_target.target.zigTriple(b.allocator) catch unreachable; const triple_prefix = test_target.target.zigTriple(b.allocator) catch unreachable;
const these_tests = b.addTest(root_src); const these_tests = b.addTest(.{
.root_source_file = .{ .path = root_src },
.optimize = test_target.optimize_mode,
.target = test_target.target,
});
const single_threaded_txt = if (test_target.single_threaded) "single" else "multi"; const single_threaded_txt = if (test_target.single_threaded) "single" else "multi";
const backend_txt = if (test_target.backend) |backend| @tagName(backend) else "default"; const backend_txt = if (test_target.backend) |backend| @tagName(backend) else "default";
these_tests.setNamePrefix(b.fmt("{s}-{s}-{s}-{s}-{s}-{s} ", .{ these_tests.setNamePrefix(b.fmt("{s}-{s}-{s}-{s}-{s}-{s} ", .{
name, name,
triple_prefix, triple_prefix,
@tagName(test_target.mode), @tagName(test_target.optimize_mode),
libc_prefix, libc_prefix,
single_threaded_txt, single_threaded_txt,
backend_txt, backend_txt,
})); }));
these_tests.single_threaded = test_target.single_threaded; these_tests.single_threaded = test_target.single_threaded;
these_tests.setFilter(test_filter); these_tests.setFilter(test_filter);
these_tests.setBuildMode(test_target.mode);
these_tests.setTarget(test_target.target);
if (test_target.link_libc) { if (test_target.link_libc) {
these_tests.linkSystemLibrary("c"); these_tests.linkSystemLibrary("c");
} }
@ -739,9 +746,9 @@ pub const StackTracesContext = struct {
step: *build.Step, step: *build.Step,
test_index: usize, test_index: usize,
test_filter: ?[]const u8, test_filter: ?[]const u8,
modes: []const Mode, optimize_modes: []const OptimizeMode,
const Expect = [@typeInfo(Mode).Enum.fields.len][]const u8; const Expect = [@typeInfo(OptimizeMode).Enum.fields.len][]const u8;
pub fn addCase(self: *StackTracesContext, config: anytype) void { pub fn addCase(self: *StackTracesContext, config: anytype) void {
if (@hasField(@TypeOf(config), "exclude")) { if (@hasField(@TypeOf(config), "exclude")) {
@ -755,26 +762,26 @@ pub const StackTracesContext = struct {
const exclude_os: []const std.Target.Os.Tag = &config.exclude_os; const exclude_os: []const std.Target.Os.Tag = &config.exclude_os;
for (exclude_os) |os| if (os == builtin.os.tag) return; for (exclude_os) |os| if (os == builtin.os.tag) return;
} }
for (self.modes) |mode| { for (self.optimize_modes) |optimize_mode| {
switch (mode) { switch (optimize_mode) {
.Debug => { .Debug => {
if (@hasField(@TypeOf(config), "Debug")) { if (@hasField(@TypeOf(config), "Debug")) {
self.addExpect(config.name, config.source, mode, config.Debug); self.addExpect(config.name, config.source, optimize_mode, config.Debug);
} }
}, },
.ReleaseSafe => { .ReleaseSafe => {
if (@hasField(@TypeOf(config), "ReleaseSafe")) { if (@hasField(@TypeOf(config), "ReleaseSafe")) {
self.addExpect(config.name, config.source, mode, config.ReleaseSafe); self.addExpect(config.name, config.source, optimize_mode, config.ReleaseSafe);
} }
}, },
.ReleaseFast => { .ReleaseFast => {
if (@hasField(@TypeOf(config), "ReleaseFast")) { if (@hasField(@TypeOf(config), "ReleaseFast")) {
self.addExpect(config.name, config.source, mode, config.ReleaseFast); self.addExpect(config.name, config.source, optimize_mode, config.ReleaseFast);
} }
}, },
.ReleaseSmall => { .ReleaseSmall => {
if (@hasField(@TypeOf(config), "ReleaseSmall")) { if (@hasField(@TypeOf(config), "ReleaseSmall")) {
self.addExpect(config.name, config.source, mode, config.ReleaseSmall); self.addExpect(config.name, config.source, optimize_mode, config.ReleaseSmall);
} }
}, },
} }
@ -785,7 +792,7 @@ pub const StackTracesContext = struct {
self: *StackTracesContext, self: *StackTracesContext,
name: []const u8, name: []const u8,
source: []const u8, source: []const u8,
mode: Mode, optimize_mode: OptimizeMode,
mode_config: anytype, mode_config: anytype,
) void { ) void {
if (@hasField(@TypeOf(mode_config), "exclude")) { if (@hasField(@TypeOf(mode_config), "exclude")) {
@ -803,7 +810,7 @@ pub const StackTracesContext = struct {
const annotated_case_name = fmt.allocPrint(self.b.allocator, "{s} {s} ({s})", .{ const annotated_case_name = fmt.allocPrint(self.b.allocator, "{s} {s} ({s})", .{
"stack-trace", "stack-trace",
name, name,
@tagName(mode), @tagName(optimize_mode),
}) catch unreachable; }) catch unreachable;
if (self.test_filter) |filter| { if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null) return; if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
@ -812,14 +819,18 @@ pub const StackTracesContext = struct {
const b = self.b; const b = self.b;
const src_basename = "source.zig"; const src_basename = "source.zig";
const write_src = b.addWriteFile(src_basename, source); const write_src = b.addWriteFile(src_basename, source);
const exe = b.addExecutableSource("test", write_src.getFileSource(src_basename).?); const exe = b.addExecutable(.{
exe.setBuildMode(mode); .name = "test",
.root_source_file = write_src.getFileSource(src_basename).?,
.optimize = optimize_mode,
.target = .{},
});
const run_and_compare = RunAndCompareStep.create( const run_and_compare = RunAndCompareStep.create(
self, self,
exe, exe,
annotated_case_name, annotated_case_name,
mode, optimize_mode,
mode_config.expect, mode_config.expect,
); );
@ -833,7 +844,7 @@ pub const StackTracesContext = struct {
context: *StackTracesContext, context: *StackTracesContext,
exe: *LibExeObjStep, exe: *LibExeObjStep,
name: []const u8, name: []const u8,
mode: Mode, optimize_mode: OptimizeMode,
expect_output: []const u8, expect_output: []const u8,
test_index: usize, test_index: usize,
@ -841,7 +852,7 @@ pub const StackTracesContext = struct {
context: *StackTracesContext, context: *StackTracesContext,
exe: *LibExeObjStep, exe: *LibExeObjStep,
name: []const u8, name: []const u8,
mode: Mode, optimize_mode: OptimizeMode,
expect_output: []const u8, expect_output: []const u8,
) *RunAndCompareStep { ) *RunAndCompareStep {
const allocator = context.b.allocator; const allocator = context.b.allocator;
@ -851,7 +862,7 @@ pub const StackTracesContext = struct {
.context = context, .context = context,
.exe = exe, .exe = exe,
.name = name, .name = name,
.mode = mode, .optimize_mode = optimize_mode,
.expect_output = expect_output, .expect_output = expect_output,
.test_index = context.test_index, .test_index = context.test_index,
}; };
@ -932,7 +943,7 @@ pub const StackTracesContext = struct {
// process result // process result
// - keep only basename of source file path // - keep only basename of source file path
// - replace address with symbolic string // - replace address with symbolic string
// - replace function name with symbolic string when mode != .Debug // - replace function name with symbolic string when optimize_mode != .Debug
// - skip empty lines // - skip empty lines
const got: []const u8 = got_result: { const got: []const u8 = got_result: {
var buf = ArrayList(u8).init(b.allocator); var buf = ArrayList(u8).init(b.allocator);
@ -968,7 +979,7 @@ pub const StackTracesContext = struct {
// emit substituted line // emit substituted line
try buf.appendSlice(line[pos + 1 .. marks[2] + delims[2].len]); try buf.appendSlice(line[pos + 1 .. marks[2] + delims[2].len]);
try buf.appendSlice(" [address]"); try buf.appendSlice(" [address]");
if (self.mode == .Debug) { if (self.optimize_mode == .Debug) {
// On certain platforms (windows) or possibly depending on how we choose to link main // On certain platforms (windows) or possibly depending on how we choose to link main
// the object file extension may be present so we simply strip any extension. // the object file extension may be present so we simply strip any extension.
if (mem.indexOfScalar(u8, line[marks[4]..marks[5]], '.')) |idot| { if (mem.indexOfScalar(u8, line[marks[4]..marks[5]], '.')) |idot| {
@ -1007,7 +1018,7 @@ pub const StandaloneContext = struct {
step: *build.Step, step: *build.Step,
test_index: usize, test_index: usize,
test_filter: ?[]const u8, test_filter: ?[]const u8,
modes: []const Mode, optimize_modes: []const OptimizeMode,
skip_non_native: bool, skip_non_native: bool,
enable_macos_sdk: bool, enable_macos_sdk: bool,
target: std.zig.CrossTarget, target: std.zig.CrossTarget,
@ -1087,13 +1098,13 @@ pub const StandaloneContext = struct {
} }
} }
const modes = if (features.build_modes) self.modes else &[1]Mode{.Debug}; const optimize_modes = if (features.build_modes) self.optimize_modes else &[1]OptimizeMode{.Debug};
for (modes) |mode| { for (optimize_modes) |optimize_mode| {
const arg = switch (mode) { const arg = switch (optimize_mode) {
.Debug => "", .Debug => "",
.ReleaseFast => "-Drelease-fast", .ReleaseFast => "-Doptimize=ReleaseFast",
.ReleaseSafe => "-Drelease-safe", .ReleaseSafe => "-Doptimize=ReleaseSafe",
.ReleaseSmall => "-Drelease-small", .ReleaseSmall => "-Doptimize=ReleaseSmall",
}; };
const zig_args_base_len = zig_args.items.len; const zig_args_base_len = zig_args.items.len;
if (arg.len > 0) if (arg.len > 0)
@ -1101,7 +1112,7 @@ pub const StandaloneContext = struct {
defer zig_args.resize(zig_args_base_len) catch unreachable; defer zig_args.resize(zig_args_base_len) catch unreachable;
const run_cmd = b.addSystemCommand(zig_args.items); const run_cmd = b.addSystemCommand(zig_args.items);
const log_step = b.addLog("PASS {s} ({s})", .{ annotated_case_name, @tagName(mode) }); const log_step = b.addLog("PASS {s} ({s})", .{ annotated_case_name, @tagName(optimize_mode) });
log_step.step.dependOn(&run_cmd.step); log_step.step.dependOn(&run_cmd.step);
self.step.dependOn(&log_step.step); self.step.dependOn(&log_step.step);
@ -1111,17 +1122,21 @@ pub const StandaloneContext = struct {
pub fn addAllArgs(self: *StandaloneContext, root_src: []const u8, link_libc: bool) void { pub fn addAllArgs(self: *StandaloneContext, root_src: []const u8, link_libc: bool) void {
const b = self.b; const b = self.b;
for (self.modes) |mode| { for (self.optimize_modes) |optimize| {
const annotated_case_name = fmt.allocPrint(self.b.allocator, "build {s} ({s})", .{ const annotated_case_name = fmt.allocPrint(self.b.allocator, "build {s} ({s})", .{
root_src, root_src,
@tagName(mode), @tagName(optimize),
}) catch unreachable; }) catch unreachable;
if (self.test_filter) |filter| { if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null) continue; if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
} }
const exe = b.addExecutable("test", root_src); const exe = b.addExecutable(.{
exe.setBuildMode(mode); .name = "test",
.root_source_file = .{ .path = root_src },
.optimize = optimize,
.target = .{},
});
if (link_libc) { if (link_libc) {
exe.linkSystemLibrary("c"); exe.linkSystemLibrary("c");
} }
@ -1247,8 +1262,8 @@ pub const GenHContext = struct {
pub fn addCase(self: *GenHContext, case: *const TestCase) void { pub fn addCase(self: *GenHContext, case: *const TestCase) void {
const b = self.b; const b = self.b;
const mode = std.builtin.Mode.Debug; const optimize_mode = std.builtin.OptimizeMode.Debug;
const annotated_case_name = fmt.allocPrint(self.b.allocator, "gen-h {s} ({s})", .{ case.name, @tagName(mode) }) catch unreachable; const annotated_case_name = fmt.allocPrint(self.b.allocator, "gen-h {s} ({s})", .{ case.name, @tagName(optimize_mode) }) catch unreachable;
if (self.test_filter) |filter| { if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null) return; if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
} }
@ -1259,7 +1274,7 @@ pub const GenHContext = struct {
} }
const obj = b.addObjectFromWriteFileStep("test", write_src, case.sources.items[0].filename); const obj = b.addObjectFromWriteFileStep("test", write_src, case.sources.items[0].filename);
obj.setBuildMode(mode); obj.setBuildMode(optimize_mode);
const cmp_h = GenHCmpOutputStep.create(self, obj, annotated_case_name, case); const cmp_h = GenHCmpOutputStep.create(self, obj, annotated_case_name, case);
@ -1336,14 +1351,16 @@ const c_abi_targets = [_]CrossTarget{
pub fn addCAbiTests(b: *build.Builder, skip_non_native: bool, skip_release: bool) *build.Step { pub fn addCAbiTests(b: *build.Builder, skip_non_native: bool, skip_release: bool) *build.Step {
const step = b.step("test-c-abi", "Run the C ABI tests"); const step = b.step("test-c-abi", "Run the C ABI tests");
const modes: [2]Mode = .{ .Debug, .ReleaseFast }; const optimize_modes: [2]OptimizeMode = .{ .Debug, .ReleaseFast };
for (modes[0 .. @as(u8, 1) + @boolToInt(!skip_release)]) |mode| for (c_abi_targets) |c_abi_target| { for (optimize_modes[0 .. @as(u8, 1) + @boolToInt(!skip_release)]) |optimize_mode| for (c_abi_targets) |c_abi_target| {
if (skip_non_native and !c_abi_target.isNative()) if (skip_non_native and !c_abi_target.isNative())
continue; continue;
const test_step = b.addTest("test/c_abi/main.zig"); const test_step = b.addTest(.{
test_step.setTarget(c_abi_target); .root_source_file = .{ .path = "test/c_abi/main.zig" },
.optimize = optimize_mode,
});
if (c_abi_target.abi != null and c_abi_target.abi.?.isMusl()) { if (c_abi_target.abi != null and c_abi_target.abi.?.isMusl()) {
// TODO NativeTargetInfo insists on dynamically linking musl // TODO NativeTargetInfo insists on dynamically linking musl
// for some reason? // for some reason?
@ -1351,7 +1368,6 @@ pub fn addCAbiTests(b: *build.Builder, skip_non_native: bool, skip_release: bool
} }
test_step.linkLibC(); test_step.linkLibC();
test_step.addCSourceFile("test/c_abi/cfuncs.c", &.{"-std=c99"}); test_step.addCSourceFile("test/c_abi/cfuncs.c", &.{"-std=c99"});
test_step.setBuildMode(mode);
if (c_abi_target.isWindows() and (c_abi_target.getCpuArch() == .x86 or builtin.target.os.tag == .linux)) { if (c_abi_target.isWindows() and (c_abi_target.getCpuArch() == .x86 or builtin.target.os.tag == .linux)) {
// LTO currently incorrectly strips stdcall name-mangled functions // LTO currently incorrectly strips stdcall name-mangled functions
@ -1363,7 +1379,7 @@ pub fn addCAbiTests(b: *build.Builder, skip_non_native: bool, skip_release: bool
test_step.setNamePrefix(b.fmt("{s}-{s}-{s} ", .{ test_step.setNamePrefix(b.fmt("{s}-{s}-{s} ", .{
"test-c-abi", "test-c-abi",
triple_prefix, triple_prefix,
@tagName(mode), @tagName(optimize_mode),
})); }));
step.dependOn(&test_step.step); step.dependOn(&test_step.step);