authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-02 21:51:34-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:18-07:00
log142471fcc46070326526e3976f0150fe734df0b6
tree67d4dec7968cf6d6765cd3bc66524721cf0554d2
parent579f572cf203eda11da7e4e919fdfc12e15f03e2

zig build system: change target, compilation, and module APIs

Introduce the concept of "target query" and "resolved target". A target query is what the user specifies, with some things left to default. A resolved target has the default things discovered and populated. In the future, std.zig.CrossTarget will be rename to std.Target.Query. Introduces `std.Build.resolveTargetQuery` to get from one to the other. The concept of `main_mod_path` is gone, no longer supported. You have to put the root source file at the module root now. * remove deprecated API * update build.zig for the breaking API changes in this branch * move std.Build.Step.Compile.BuildId to std.zig.BuildId * add more options to std.Build.ExecutableOptions, std.Build.ObjectOptions, std.Build.SharedLibraryOptions, std.Build.StaticLibraryOptions, and std.Build.TestOptions. * remove `std.Build.constructCMacro`. There is no use for this API. * deprecate `std.Build.Step.Compile.defineCMacro`. Instead, `std.Build.Module.addCMacro` is provided. - remove `std.Build.Step.Compile.defineCMacroRaw`. * deprecate `std.Build.Step.Compile.linkFrameworkNeeded` - use `std.Build.Module.linkFramework` * deprecate `std.Build.Step.Compile.linkFrameworkWeak` - use `std.Build.Module.linkFramework` * move more logic into `std.Build.Module` * allow `target` and `optimize` to be `null` when creating a Module. Along with other fields, those unspecified options will be inherited from parent `Module` when inserted into an import table. * the `target` field of `addExecutable` is now required. pass `b.host` to get the host target.

122 files changed, 1812 insertions(+), 1338 deletions(-)

build.zig+53-39
......@@ -39,10 +39,10 @@ pub fn build(b: *std.Build) !void {
3939 const docgen_exe = b.addExecutable(.{
4040 .name = "docgen",
4141 .root_source_file = .{ .path = "tools/docgen.zig" },
42 .target = .{},
42 .target = b.host,
4343 .optimize = .Debug,
44 .single_threaded = single_threaded,
4445 });
45 docgen_exe.single_threaded = single_threaded;
4646
4747 const docgen_cmd = b.addRunArtifact(docgen_exe);
4848 docgen_cmd.addArgs(&.{ "--zig", b.zig_exe });
......@@ -89,11 +89,11 @@ pub fn build(b: *std.Build) !void {
8989 const check_case_exe = b.addExecutable(.{
9090 .name = "check-case",
9191 .root_source_file = .{ .path = "test/src/Cases.zig" },
92 .target = b.host,
9293 .optimize = optimize,
93 .main_mod_path = .{ .path = "." },
94 .single_threaded = single_threaded,
9495 });
9596 check_case_exe.stack_size = stack_size;
96 check_case_exe.single_threaded = single_threaded;
9797
9898 const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;
9999 const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false;
......@@ -194,14 +194,18 @@ pub fn build(b: *std.Build) !void {
194194 break :blk 4;
195195 };
196196
197 const exe = addCompilerStep(b, optimize, target);
198 exe.strip = strip;
197 const exe = addCompilerStep(b, .{
198 .optimize = optimize,
199 .target = target,
200 .strip = strip,
201 .sanitize_thread = sanitize_thread,
202 .single_threaded = single_threaded,
203 });
199204 exe.pie = pie;
200 exe.sanitize_thread = sanitize_thread;
201205 exe.entitlements = entitlements;
202206
203207 exe.build_id = b.option(
204 std.Build.Step.Compile.BuildId,
208 std.zig.BuildId,
205209 "build-id",
206210 "Request creation of '.note.gnu.build-id' section",
207211 );
......@@ -217,9 +221,7 @@ pub fn build(b: *std.Build) !void {
217221
218222 test_step.dependOn(&exe.step);
219223
220 exe.single_threaded = single_threaded;
221
222 if (target.isWindows() and target.getAbi() == .gnu) {
224 if (target.target.os.tag == .windows and target.target.abi == .gnu) {
223225 // LTO is currently broken on mingw, this can be removed when it's fixed.
224226 exe.want_lto = false;
225227 check_case_exe.want_lto = false;
......@@ -230,7 +232,7 @@ pub fn build(b: *std.Build) !void {
230232 exe.use_lld = use_llvm;
231233
232234 const exe_options = b.addOptions();
233 exe.addOptions("build_options", exe_options);
235 exe.root_module.addOptions("build_options", exe_options);
234236
235237 exe_options.addOption(u32, "mem_leak_frames", mem_leak_frames);
236238 exe_options.addOption(bool, "skip_non_native", skip_non_native);
......@@ -345,7 +347,7 @@ pub fn build(b: *std.Build) !void {
345347 try addStaticLlvmOptionsToExe(exe);
346348 try addStaticLlvmOptionsToExe(check_case_exe);
347349 }
348 if (target.isWindows()) {
350 if (target.target.os.tag == .windows) {
349351 inline for (.{ exe, check_case_exe }) |artifact| {
350352 artifact.linkSystemLibrary("version");
351353 artifact.linkSystemLibrary("uuid");
......@@ -369,7 +371,7 @@ pub fn build(b: *std.Build) !void {
369371 );
370372
371373 // On mingw, we need to opt into windows 7+ to get some features required by tracy.
372 const tracy_c_flags: []const []const u8 = if (target.isWindows() and target.getAbi() == .gnu)
374 const tracy_c_flags: []const []const u8 = if (target.target.os.tag == .windows and target.target.abi == .gnu)
373375 &[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined", "-D_WIN32_WINNT=0x601" }
374376 else
375377 &[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" };
......@@ -377,11 +379,11 @@ pub fn build(b: *std.Build) !void {
377379 exe.addIncludePath(.{ .cwd_relative = tracy_path });
378380 exe.addCSourceFile(.{ .file = .{ .cwd_relative = client_cpp }, .flags = tracy_c_flags });
379381 if (!enable_llvm) {
380 exe.linkSystemLibraryName("c++");
382 exe.root_module.linkSystemLibrary("c++", .{ .use_pkg_config = .no });
381383 }
382384 exe.linkLibC();
383385
384 if (target.isWindows()) {
386 if (target.target.os.tag == .windows) {
385387 exe.linkSystemLibrary("dbghelp");
386388 exe.linkSystemLibrary("ws2_32");
387389 }
......@@ -390,7 +392,7 @@ pub fn build(b: *std.Build) !void {
390392 const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
391393
392394 const test_cases_options = b.addOptions();
393 check_case_exe.addOptions("build_options", test_cases_options);
395 check_case_exe.root_module.addOptions("build_options", test_cases_options);
394396
395397 test_cases_options.addOption(bool, "enable_tracy", false);
396398 test_cases_options.addOption(bool, "enable_logging", enable_logging);
......@@ -540,16 +542,19 @@ pub fn build(b: *std.Build) !void {
540542fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
541543 const semver = try std.SemanticVersion.parse(version);
542544
543 var target: std.zig.CrossTarget = .{
545 var target_query: std.zig.CrossTarget = .{
544546 .cpu_arch = .wasm32,
545547 .os_tag = .wasi,
546548 };
547 target.cpu_features_add.addFeature(@intFromEnum(std.Target.wasm.Feature.bulk_memory));
549 target_query.cpu_features_add.addFeature(@intFromEnum(std.Target.wasm.Feature.bulk_memory));
548550
549 const exe = addCompilerStep(b, .ReleaseSmall, target);
551 const exe = addCompilerStep(b, .{
552 .optimize = .ReleaseSmall,
553 .target = b.resolveTargetQuery(target_query),
554 });
550555
551556 const exe_options = b.addOptions();
552 exe.addOptions("build_options", exe_options);
557 exe.root_module.addOptions("build_options", exe_options);
553558
554559 exe_options.addOption(u32, "mem_leak_frames", 0);
555560 exe_options.addOption(bool, "have_llvm", false);
......@@ -584,17 +589,24 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
584589 update_zig1_step.dependOn(&copy_zig_h.step);
585590}
586591
587fn addCompilerStep(
588 b: *std.Build,
592const AddCompilerStepOptions = struct {
589593 optimize: std.builtin.OptimizeMode,
590 target: std.zig.CrossTarget,
591) *std.Build.Step.Compile {
594 target: std.Build.ResolvedTarget,
595 strip: ?bool = null,
596 sanitize_thread: ?bool = null,
597 single_threaded: ?bool = null,
598};
599
600fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.Step.Compile {
592601 const exe = b.addExecutable(.{
593602 .name = "zig",
594603 .root_source_file = .{ .path = "src/main.zig" },
595 .target = target,
596 .optimize = optimize,
604 .target = options.target,
605 .optimize = options.optimize,
597606 .max_rss = 7_000_000_000,
607 .strip = options.strip,
608 .sanitize_thread = options.sanitize_thread,
609 .single_threaded = options.single_threaded,
598610 });
599611 exe.stack_size = stack_size;
600612
......@@ -602,15 +614,15 @@ fn addCompilerStep(
602614 aro_options.addOption([]const u8, "version_str", "aro-zig");
603615 const aro_options_module = aro_options.createModule();
604616 const aro_backend = b.createModule(.{
605 .source_file = .{ .path = "deps/aro/backend.zig" },
606 .dependencies = &.{.{
617 .root_source_file = .{ .path = "deps/aro/backend.zig" },
618 .imports = &.{.{
607619 .name = "build_options",
608620 .module = aro_options_module,
609621 }},
610622 });
611623 const aro_module = b.createModule(.{
612 .source_file = .{ .path = "deps/aro/aro.zig" },
613 .dependencies = &.{
624 .root_source_file = .{ .path = "deps/aro/aro.zig" },
625 .imports = &.{
614626 .{
615627 .name = "build_options",
616628 .module = aro_options_module,
......@@ -625,7 +637,7 @@ fn addCompilerStep(
625637 },
626638 });
627639
628 exe.addModule("aro", aro_module);
640 exe.root_module.addImport("aro", aro_module);
629641 return exe;
630642}
631643
......@@ -649,7 +661,7 @@ fn addCmakeCfgOptionsToExe(
649661 exe: *std.Build.Step.Compile,
650662 use_zig_libcxx: bool,
651663) !void {
652 if (exe.target.isDarwin()) {
664 if (exe.rootModuleTarget().isDarwin()) {
653665 // useful for package maintainers
654666 exe.headerpad_max_install_names = true;
655667 }
......@@ -677,8 +689,8 @@ fn addCmakeCfgOptionsToExe(
677689 // against system-provided LLVM, Clang, LLD.
678690 const need_cpp_includes = true;
679691 const static = cfg.llvm_linkage == .static;
680 const lib_suffix = if (static) exe.target.staticLibSuffix()[1..] else exe.target.dynamicLibSuffix()[1..];
681 switch (exe.target.getOsTag()) {
692 const lib_suffix = if (static) exe.rootModuleTarget().staticLibSuffix()[1..] else exe.rootModuleTarget().dynamicLibSuffix()[1..];
693 switch (exe.rootModuleTarget().os.tag) {
682694 .linux => {
683695 // First we try to link against the detected libcxx name. If that doesn't work, we fall
684696 // back to -lc++ and cross our fingers.
......@@ -694,7 +706,7 @@ fn addCmakeCfgOptionsToExe(
694706 exe.linkLibCpp();
695707 },
696708 .windows => {
697 if (exe.target.getAbi() != .msvc) exe.linkLibCpp();
709 if (exe.rootModuleTarget().abi != .msvc) exe.linkLibCpp();
698710 },
699711 .freebsd => {
700712 if (static) {
......@@ -756,12 +768,12 @@ fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void {
756768 exe.linkSystemLibrary("z");
757769 exe.linkSystemLibrary("zstd");
758770
759 if (exe.target.getOs().tag != .windows or exe.target.getAbi() != .msvc) {
771 if (exe.rootModuleTarget().os.tag != .windows or exe.rootModuleTarget().abi != .msvc) {
760772 // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.
761773 exe.linkSystemLibrary("c++");
762774 }
763775
764 if (exe.target.getOs().tag == .windows) {
776 if (exe.rootModuleTarget().os.tag == .windows) {
765777 exe.linkSystemLibrary("version");
766778 exe.linkSystemLibrary("uuid");
767779 exe.linkSystemLibrary("ole32");
......@@ -810,7 +822,9 @@ fn addCMakeLibraryList(exe: *std.Build.Step.Compile, list: []const u8) void {
810822 while (it.next()) |lib| {
811823 if (mem.startsWith(u8, lib, "-l")) {
812824 exe.linkSystemLibrary(lib["-l".len..]);
813 } else if (exe.target.isWindows() and mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib)) {
825 } else if (exe.rootModuleTarget().os.tag == .windows and
826 mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib))
827 {
814828 exe.linkSystemLibrary(lib[0 .. lib.len - ".lib".len]);
815829 } else {
816830 exe.addObjectFile(.{ .cwd_relative = lib });
deps/aro/build/GenerateDef.zig+2-2
......@@ -21,7 +21,7 @@ pub const Options = struct {
2121 pub const Kind = enum { dafsa, named };
2222};
2323
24pub fn create(owner: *std.Build, options: Options) std.Build.ModuleDependency {
24pub fn create(owner: *std.Build, options: Options) std.Build.Module.Import {
2525 const self = owner.allocator.create(GenerateDef) catch @panic("OOM");
2626 const path = owner.pathJoin(&.{ options.src_prefix, options.name });
2727
......@@ -39,7 +39,7 @@ pub fn create(owner: *std.Build, options: Options) std.Build.ModuleDependency {
3939 .generated_file = .{ .step = &self.step },
4040 };
4141 const module = self.step.owner.createModule(.{
42 .source_file = .{ .generated = &self.generated_file },
42 .root_source_file = .{ .generated = &self.generated_file },
4343 });
4444 return .{
4545 .module = module,
lib/build_runner.zig+6-1
......@@ -46,7 +46,12 @@ pub fn main() !void {
4646 return error.InvalidArgs;
4747 };
4848
49 const host = try std.zig.system.NativeTargetInfo.detect(.{});
49 const detected = try std.zig.system.NativeTargetInfo.detect(.{});
50 const host: std.Build.ResolvedTarget = .{
51 .query = .{},
52 .target = detected.target,
53 .dynamic_linker = detected.dynamic_linker,
54 };
5055
5156 const build_root_directory: std.Build.Cache.Directory = .{
5257 .path = build_root,
lib/std/Build.zig+129-48
......@@ -14,20 +14,11 @@ const process = std.process;
1414const EnvMap = std.process.EnvMap;
1515const fmt_lib = std.fmt;
1616const File = std.fs.File;
17const CrossTarget = std.zig.CrossTarget;
18const NativeTargetInfo = std.zig.system.NativeTargetInfo;
17const TargetQuery = std.zig.CrossTarget;
1918const Sha256 = std.crypto.hash.sha2.Sha256;
2019const Build = @This();
2120
2221pub const Cache = @import("Build/Cache.zig");
23
24/// deprecated: use `Step.Compile`.
25pub const LibExeObjStep = Step.Compile;
26/// deprecated: use `Build`.
27pub const Builder = Build;
28/// deprecated: use `Step.InstallDir.Options`
29pub const InstallDirectoryOptions = Step.InstallDir.Options;
30
3122pub const Step = @import("Build/Step.zig");
3223pub const Module = @import("Build/Module.zig");
3324
......@@ -94,7 +85,7 @@ enable_wine: bool = false,
9485glibc_runtimes_dir: ?[]const u8 = null,
9586
9687/// Information about the native target. Computed before build() is invoked.
97host: NativeTargetInfo,
88host: ResolvedTarget,
9889
9990dep_prefix: []const u8 = "",
10091
......@@ -220,7 +211,7 @@ pub fn create(
220211 build_root: Cache.Directory,
221212 cache_root: Cache.Directory,
222213 global_cache_root: Cache.Directory,
223 host: NativeTargetInfo,
214 host: ResolvedTarget,
224215 cache: *Cache,
225216 available_deps: AvailableDeps,
226217) !*Build {
......@@ -384,7 +375,7 @@ fn userInputOptionsFromArgs(allocator: Allocator, args: anytype) UserInputOption
384375 const v = @field(args, field.name);
385376 const T = @TypeOf(v);
386377 switch (T) {
387 CrossTarget => {
378 TargetQuery => {
388379 user_input_options.put(field.name, .{
389380 .name = field.name,
390381 .value = .{ .scalar = v.zigTriple(allocator) catch @panic("OOM") },
......@@ -593,14 +584,22 @@ pub fn addOptions(self: *Build) *Step.Options {
593584
594585pub const ExecutableOptions = struct {
595586 name: []const u8,
587 /// If you want the executable to run on the same computer as the one
588 /// building the package, pass the `host` field of the package's `Build`
589 /// instance.
590 target: ResolvedTarget,
596591 root_source_file: ?LazyPath = null,
597592 version: ?std.SemanticVersion = null,
598 target: CrossTarget = .{},
599593 optimize: std.builtin.OptimizeMode = .Debug,
600594 linkage: ?Step.Compile.Linkage = null,
601595 max_rss: usize = 0,
602596 link_libc: ?bool = null,
603597 single_threaded: ?bool = null,
598 pic: ?bool = null,
599 strip: ?bool = null,
600 unwind_tables: ?bool = null,
601 omit_frame_pointer: ?bool = null,
602 sanitize_thread: ?bool = null,
604603 use_llvm: ?bool = null,
605604 use_lld: ?bool = null,
606605 zig_lib_dir: ?LazyPath = null,
......@@ -621,6 +620,11 @@ pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile {
621620 .optimize = options.optimize,
622621 .link_libc = options.link_libc,
623622 .single_threaded = options.single_threaded,
623 .pic = options.pic,
624 .strip = options.strip,
625 .unwind_tables = options.unwind_tables,
626 .omit_frame_pointer = options.omit_frame_pointer,
627 .sanitize_thread = options.sanitize_thread,
624628 },
625629 .version = options.version,
626630 .kind = .exe,
......@@ -636,11 +640,18 @@ pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile {
636640pub const ObjectOptions = struct {
637641 name: []const u8,
638642 root_source_file: ?LazyPath = null,
639 target: CrossTarget,
643 /// To choose the same computer as the one building the package, pass the
644 /// `host` field of the package's `Build` instance.
645 target: ResolvedTarget,
640646 optimize: std.builtin.OptimizeMode,
641647 max_rss: usize = 0,
642648 link_libc: ?bool = null,
643649 single_threaded: ?bool = null,
650 pic: ?bool = null,
651 strip: ?bool = null,
652 unwind_tables: ?bool = null,
653 omit_frame_pointer: ?bool = null,
654 sanitize_thread: ?bool = null,
644655 use_llvm: ?bool = null,
645656 use_lld: ?bool = null,
646657 zig_lib_dir: ?LazyPath = null,
......@@ -655,6 +666,11 @@ pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile {
655666 .optimize = options.optimize,
656667 .link_libc = options.link_libc,
657668 .single_threaded = options.single_threaded,
669 .pic = options.pic,
670 .strip = options.strip,
671 .unwind_tables = options.unwind_tables,
672 .omit_frame_pointer = options.omit_frame_pointer,
673 .sanitize_thread = options.sanitize_thread,
658674 },
659675 .kind = .obj,
660676 .max_rss = options.max_rss,
......@@ -666,13 +682,20 @@ pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile {
666682
667683pub const SharedLibraryOptions = struct {
668684 name: []const u8,
685 /// To choose the same computer as the one building the package, pass the
686 /// `host` field of the package's `Build` instance.
687 target: ResolvedTarget,
688 optimize: std.builtin.OptimizeMode,
669689 root_source_file: ?LazyPath = null,
670690 version: ?std.SemanticVersion = null,
671 target: CrossTarget,
672 optimize: std.builtin.OptimizeMode,
673691 max_rss: usize = 0,
674692 link_libc: ?bool = null,
675693 single_threaded: ?bool = null,
694 pic: ?bool = null,
695 strip: ?bool = null,
696 unwind_tables: ?bool = null,
697 omit_frame_pointer: ?bool = null,
698 sanitize_thread: ?bool = null,
676699 use_llvm: ?bool = null,
677700 use_lld: ?bool = null,
678701 zig_lib_dir: ?LazyPath = null,
......@@ -693,6 +716,11 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile
693716 .root_source_file = options.root_source_file,
694717 .link_libc = options.link_libc,
695718 .single_threaded = options.single_threaded,
719 .pic = options.pic,
720 .strip = options.strip,
721 .unwind_tables = options.unwind_tables,
722 .omit_frame_pointer = options.omit_frame_pointer,
723 .sanitize_thread = options.sanitize_thread,
696724 },
697725 .kind = .lib,
698726 .linkage = .dynamic,
......@@ -708,12 +736,19 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile
708736pub const StaticLibraryOptions = struct {
709737 name: []const u8,
710738 root_source_file: ?LazyPath = null,
711 target: CrossTarget,
739 /// To choose the same computer as the one building the package, pass the
740 /// `host` field of the package's `Build` instance.
741 target: ResolvedTarget,
712742 optimize: std.builtin.OptimizeMode,
713743 version: ?std.SemanticVersion = null,
714744 max_rss: usize = 0,
715745 link_libc: ?bool = null,
716746 single_threaded: ?bool = null,
747 pic: ?bool = null,
748 strip: ?bool = null,
749 unwind_tables: ?bool = null,
750 omit_frame_pointer: ?bool = null,
751 sanitize_thread: ?bool = null,
717752 use_llvm: ?bool = null,
718753 use_lld: ?bool = null,
719754 zig_lib_dir: ?LazyPath = null,
......@@ -728,6 +763,11 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile
728763 .root_source_file = options.root_source_file,
729764 .link_libc = options.link_libc,
730765 .single_threaded = options.single_threaded,
766 .pic = options.pic,
767 .strip = options.strip,
768 .unwind_tables = options.unwind_tables,
769 .omit_frame_pointer = options.omit_frame_pointer,
770 .sanitize_thread = options.sanitize_thread,
731771 },
732772 .kind = .lib,
733773 .linkage = .static,
......@@ -742,7 +782,7 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile
742782pub const TestOptions = struct {
743783 name: []const u8 = "test",
744784 root_source_file: LazyPath,
745 target: CrossTarget = .{},
785 target: ?ResolvedTarget = null,
746786 optimize: std.builtin.OptimizeMode = .Debug,
747787 version: ?std.SemanticVersion = null,
748788 max_rss: usize = 0,
......@@ -750,6 +790,11 @@ pub const TestOptions = struct {
750790 test_runner: ?[]const u8 = null,
751791 link_libc: ?bool = null,
752792 single_threaded: ?bool = null,
793 pic: ?bool = null,
794 strip: ?bool = null,
795 unwind_tables: ?bool = null,
796 omit_frame_pointer: ?bool = null,
797 sanitize_thread: ?bool = null,
753798 use_llvm: ?bool = null,
754799 use_lld: ?bool = null,
755800 zig_lib_dir: ?LazyPath = null,
......@@ -761,10 +806,15 @@ pub fn addTest(b: *Build, options: TestOptions) *Step.Compile {
761806 .kind = .@"test",
762807 .root_module = .{
763808 .root_source_file = options.root_source_file,
764 .target = options.target,
809 .target = options.target orelse b.host,
765810 .optimize = options.optimize,
766811 .link_libc = options.link_libc,
767812 .single_threaded = options.single_threaded,
813 .pic = options.pic,
814 .strip = options.strip,
815 .unwind_tables = options.unwind_tables,
816 .omit_frame_pointer = options.omit_frame_pointer,
817 .sanitize_thread = options.sanitize_thread,
768818 },
769819 .max_rss = options.max_rss,
770820 .filter = options.filter,
......@@ -778,7 +828,9 @@ pub fn addTest(b: *Build, options: TestOptions) *Step.Compile {
778828pub const AssemblyOptions = struct {
779829 name: []const u8,
780830 source_file: LazyPath,
781 target: CrossTarget,
831 /// To choose the same computer as the one building the package, pass the
832 /// `host` field of the package's `Build` instance.
833 target: ResolvedTarget,
782834 optimize: std.builtin.OptimizeMode,
783835 max_rss: usize = 0,
784836 zig_lib_dir: ?LazyPath = null,
......@@ -1061,7 +1113,7 @@ pub fn option(self: *Build, comptime T: type, name_raw: []const u8, description_
10611113 return null;
10621114 },
10631115 .scalar => |s| {
1064 if (Step.Compile.BuildId.parse(s)) |build_id| {
1116 if (std.zig.BuildId.parse(s)) |build_id| {
10651117 return build_id;
10661118 } else |err| {
10671119 log.err("unable to parse option '-D{s}': {s}", .{ name, @errorName(err) });
......@@ -1126,13 +1178,20 @@ pub fn standardOptimizeOption(self: *Build, options: StandardOptimizeOptionOptio
11261178}
11271179
11281180pub const StandardTargetOptionsArgs = struct {
1129 whitelist: ?[]const CrossTarget = null,
1181 whitelist: ?[]const TargetQuery = null,
11301182
1131 default_target: CrossTarget = CrossTarget{},
1183 default_target: TargetQuery = .{},
11321184};
11331185
1186/// Exposes standard `zig build` options for choosing a target and additionally
1187/// resolves the target query.
1188pub fn standardTargetOptions(b: *Build, args: StandardTargetOptionsArgs) ResolvedTarget {
1189 const query = b.standardTargetOptionsQueryOnly(args);
1190 return b.resolveTargetQuery(query);
1191}
1192
11341193/// Exposes standard `zig build` options for choosing a target.
1135pub fn standardTargetOptions(self: *Build, args: StandardTargetOptionsArgs) CrossTarget {
1194pub fn standardTargetOptionsQueryOnly(self: *Build, args: StandardTargetOptionsArgs) TargetQuery {
11361195 const maybe_triple = self.option(
11371196 []const u8,
11381197 "target",
......@@ -1146,8 +1205,8 @@ pub fn standardTargetOptions(self: *Build, args: StandardTargetOptionsArgs) Cros
11461205
11471206 const triple = maybe_triple orelse "native";
11481207
1149 var diags: CrossTarget.ParseOptions.Diagnostics = .{};
1150 const selected_target = CrossTarget.parse(.{
1208 var diags: TargetQuery.ParseOptions.Diagnostics = .{};
1209 const selected_target = TargetQuery.parse(.{
11511210 .arch_os_abi = triple,
11521211 .cpu_features = mcpu,
11531212 .diagnostics = &diags,
......@@ -1203,7 +1262,7 @@ pub fn standardTargetOptions(self: *Build, args: StandardTargetOptionsArgs) Cros
12031262 // Make sure it's a match of one of the list.
12041263 var mismatch_triple = true;
12051264 var mismatch_cpu_features = true;
1206 var whitelist_item = CrossTarget{};
1265 var whitelist_item: TargetQuery = .{};
12071266 for (list) |t| {
12081267 mismatch_cpu_features = true;
12091268 mismatch_triple = true;
......@@ -1340,7 +1399,7 @@ pub fn addUserInputFlag(self: *Build, name_raw: []const u8) !bool {
13401399
13411400fn typeToEnum(comptime T: type) TypeId {
13421401 return switch (T) {
1343 Step.Compile.BuildId => .build_id,
1402 std.zig.BuildId => .build_id,
13441403 else => return switch (@typeInfo(T)) {
13451404 .Int => .int,
13461405 .Float => .float,
......@@ -1408,7 +1467,7 @@ pub fn installFile(self: *Build, src_path: []const u8, dest_rel_path: []const u8
14081467 self.getInstallStep().dependOn(&self.addInstallFileWithDir(.{ .path = src_path }, .prefix, dest_rel_path).step);
14091468}
14101469
1411pub fn installDirectory(self: *Build, options: InstallDirectoryOptions) void {
1470pub fn installDirectory(self: *Build, options: Step.InstallDir.Options) void {
14121471 self.getInstallStep().dependOn(&self.addInstallDirectory(options).step);
14131472}
14141473
......@@ -1454,7 +1513,7 @@ pub fn addInstallFileWithDir(
14541513 return Step.InstallFile.create(self, source.dupe(self), install_dir, dest_rel_path);
14551514}
14561515
1457pub fn addInstallDirectory(self: *Build, options: InstallDirectoryOptions) *Step.InstallDir {
1516pub fn addInstallDirectory(self: *Build, options: Step.InstallDir.Options) *Step.InstallDir {
14581517 return Step.InstallDir.create(self, options);
14591518}
14601519
......@@ -1511,7 +1570,7 @@ pub fn fmt(self: *Build, comptime format: []const u8, args: anytype) []u8 {
15111570
15121571pub fn findProgram(self: *Build, names: []const []const u8, paths: []const []const u8) ![]const u8 {
15131572 // TODO report error for ambiguous situations
1514 const exe_extension = @as(CrossTarget, .{}).exeFileExt();
1573 const exe_extension = @as(TargetQuery, .{}).exeFileExt();
15151574 for (self.search_prefixes.items) |search_prefix| {
15161575 for (names) |name| {
15171576 if (fs.path.isAbsolute(name)) {
......@@ -1957,22 +2016,6 @@ pub fn dumpBadGetPathHelp(
19572016 tty_config.setColor(w, .reset) catch {};
19582017}
19592018
1960/// Allocates a new string for assigning a value to a named macro.
1961/// If the value is omitted, it is set to 1.
1962/// `name` and `value` need not live longer than the function call.
1963pub fn constructCMacro(allocator: Allocator, name: []const u8, value: ?[]const u8) []const u8 {
1964 var macro = allocator.alloc(
1965 u8,
1966 name.len + if (value) |value_slice| value_slice.len + 1 else 0,
1967 ) catch |err| if (err == error.OutOfMemory) @panic("Out of memory") else unreachable;
1968 @memcpy(macro[0..name.len], name);
1969 if (value) |value_slice| {
1970 macro[name.len] = '=';
1971 @memcpy(macro[name.len + 1 ..][0..value_slice.len], value_slice);
1972 }
1973 return macro;
1974}
1975
19762019pub const InstallDir = union(enum) {
19772020 prefix: void,
19782021 lib: void,
......@@ -2062,6 +2105,44 @@ pub fn hex64(x: u64) [16]u8 {
20622105 return result;
20632106}
20642107
2108/// A pair of target query and fully resolved target.
2109/// This type is generally required by build system API that need to be given a
2110/// target. The query is kept because the Zig toolchain needs to know which parts
2111/// of the target are "native". This can apply to the CPU, the OS, or even the ABI.
2112pub const ResolvedTarget = struct {
2113 query: TargetQuery,
2114 target: std.Target,
2115 dynamic_linker: std.Target.DynamicLinker,
2116
2117 pub fn toNativeTargetInfo(self: ResolvedTarget) std.zig.system.NativeTargetInfo {
2118 return .{
2119 .target = self.target,
2120 .dynamic_linker = self.dynamic_linker,
2121 };
2122 }
2123};
2124
2125/// Converts a target query into a fully resolved target that can be passed to
2126/// various parts of the API.
2127pub fn resolveTargetQuery(b: *Build, query: TargetQuery) ResolvedTarget {
2128 // This context will likely be required in the future when the target is
2129 // resolved via a WASI API or via the build protocol.
2130 _ = b;
2131
2132 const result = std.zig.system.NativeTargetInfo.detect(query) catch
2133 @panic("unable to resolve target query");
2134
2135 return .{
2136 .query = query,
2137 .target = result.target,
2138 .dynamic_linker = result.dynamic_linker,
2139 };
2140}
2141
2142pub fn wantSharedLibSymLinks(target: std.Target) bool {
2143 return target.os.tag != .windows;
2144}
2145
20652146test {
20662147 _ = Cache;
20672148 _ = Step;
lib/std/Build/Cache.zig+1-1
......@@ -270,7 +270,7 @@ pub const HashHelper = struct {
270270 .none => {},
271271 }
272272 },
273 std.Build.Step.Compile.BuildId => switch (x) {
273 std.zig.BuildId => switch (x) {
274274 .none, .fast, .uuid, .sha1, .md5 => hh.add(std.meta.activeTag(x)),
275275 .hexstring => |hex_string| hh.addBytes(hex_string.toSlice()),
276276 },
lib/std/Build/Module.zig+239-108
......@@ -3,27 +3,24 @@ owner: *std.Build,
33/// Tracks the set of steps that depend on this `Module`. This ensures that
44/// when making this `Module` depend on other `Module` objects and `Step`
55/// objects, respective `Step` dependencies can be added.
6depending_steps: std.AutoArrayHashMapUnmanaged(*std.Build.Step.Compile, void),
7/// This could either be a generated file, in which case the module
8/// contains exactly one file, or it could be a path to the root source
9/// file of directory of files which constitute the module.
10/// If `null`, it means this module is made up of only `link_objects`.
6depending_steps: std.AutoArrayHashMapUnmanaged(*Step.Compile, void),
117root_source_file: ?LazyPath,
128/// The modules that are mapped into this module's import table.
13import_table: std.StringArrayHashMap(*Module),
9/// Use `addImport` rather than modifying this field directly in order to
10/// maintain step dependency edges.
11import_table: std.StringArrayHashMapUnmanaged(*Module),
1412
15target: std.zig.CrossTarget,
16target_info: NativeTargetInfo,
17optimize: std.builtin.OptimizeMode,
13target: ?std.Build.ResolvedTarget = null,
14optimize: ?std.builtin.OptimizeMode = null,
1815dwarf_format: ?std.dwarf.Format,
1916
20c_macros: std.ArrayList([]const u8),
21include_dirs: std.ArrayList(IncludeDir),
22lib_paths: std.ArrayList(LazyPath),
23rpaths: std.ArrayList(LazyPath),
24frameworks: std.StringArrayHashMapUnmanaged(FrameworkLinkInfo),
17c_macros: std.ArrayListUnmanaged([]const u8),
18include_dirs: std.ArrayListUnmanaged(IncludeDir),
19lib_paths: std.ArrayListUnmanaged(LazyPath),
20rpaths: std.ArrayListUnmanaged(RPath),
21frameworks: std.StringArrayHashMapUnmanaged(LinkFrameworkOptions),
2522c_std: std.Build.CStd,
26link_objects: std.ArrayList(LinkObject),
23link_objects: std.ArrayListUnmanaged(LinkObject),
2724
2825strip: ?bool,
2926unwind_tables: ?bool,
......@@ -53,9 +50,14 @@ link_libcpp: ?bool,
5350/// Symbols to be exported when compiling to WebAssembly.
5451export_symbol_names: []const []const u8 = &.{},
5552
53pub const RPath = union(enum) {
54 lazy_path: LazyPath,
55 special: []const u8,
56};
57
5658pub const LinkObject = union(enum) {
5759 static_path: LazyPath,
58 other_step: *std.Build.Step.Compile,
60 other_step: *Step.Compile,
5961 system_lib: SystemLib,
6062 assembly_file: LazyPath,
6163 c_source_file: *CSourceFile,
......@@ -133,21 +135,32 @@ pub const IncludeDir = union(enum) {
133135 path_after: LazyPath,
134136 framework_path: LazyPath,
135137 framework_path_system: LazyPath,
136 other_step: *std.Build.Step.Compile,
137 config_header_step: *std.Build.Step.ConfigHeader,
138 other_step: *Step.Compile,
139 config_header_step: *Step.ConfigHeader,
138140};
139141
140pub const FrameworkLinkInfo = struct {
142pub const LinkFrameworkOptions = struct {
141143 needed: bool = false,
142144 weak: bool = false,
143145};
144146
147/// Unspecified options here will be inherited from parent `Module` when
148/// inserted into an import table.
145149pub const CreateOptions = struct {
146 target: std.zig.CrossTarget,
147 target_info: ?NativeTargetInfo = null,
148 optimize: std.builtin.OptimizeMode,
150 /// This could either be a generated file, in which case the module
151 /// contains exactly one file, or it could be a path to the root source
152 /// file of directory of files which constitute the module.
153 /// If `null`, it means this module is made up of only `link_objects`.
149154 root_source_file: ?LazyPath = null,
150 import_table: []const Import = &.{},
155
156 /// The table of other modules that this module can access via `@import`.
157 /// Imports are allowed to be cyclical, so this table can be added to after
158 /// the `Module` is created via `addImport`.
159 imports: []const Import = &.{},
160
161 target: ?std.Build.ResolvedTarget = null,
162 optimize: ?std.builtin.OptimizeMode = null,
163
151164 link_libc: ?bool = null,
152165 link_libcpp: ?bool = null,
153166 single_threaded: ?bool = null,
......@@ -173,26 +186,26 @@ pub const Import = struct {
173186 module: *Module,
174187};
175188
176pub fn init(owner: *std.Build, options: CreateOptions, compile: ?*std.Build.Step.Compile) Module {
177 var m: Module = .{
189pub fn init(m: *Module, owner: *std.Build, options: CreateOptions, compile: ?*Step.Compile) void {
190 const allocator = owner.allocator;
191
192 m.* = .{
178193 .owner = owner,
179194 .depending_steps = .{},
180195 .root_source_file = if (options.root_source_file) |lp| lp.dupe(owner) else null,
181 .import_table = std.StringArrayHashMap(*Module).init(owner.allocator),
196 .import_table = .{},
182197 .target = options.target,
183 .target_info = options.target_info orelse
184 NativeTargetInfo.detect(options.target) catch @panic("unhandled error"),
185198 .optimize = options.optimize,
186199 .link_libc = options.link_libc,
187200 .link_libcpp = options.link_libcpp,
188201 .dwarf_format = options.dwarf_format,
189 .c_macros = std.ArrayList([]const u8).init(owner.allocator),
190 .include_dirs = std.ArrayList(IncludeDir).init(owner.allocator),
191 .lib_paths = std.ArrayList(LazyPath).init(owner.allocator),
192 .rpaths = std.ArrayList(LazyPath).init(owner.allocator),
202 .c_macros = .{},
203 .include_dirs = .{},
204 .lib_paths = .{},
205 .rpaths = .{},
193206 .frameworks = .{},
194207 .c_std = options.c_std,
195 .link_objects = std.ArrayList(LinkObject).init(owner.allocator),
208 .link_objects = .{},
196209 .strip = options.strip,
197210 .unwind_tables = options.unwind_tables,
198211 .single_threaded = options.single_threaded,
......@@ -208,31 +221,30 @@ pub fn init(owner: *std.Build, options: CreateOptions, compile: ?*std.Build.Step
208221 .export_symbol_names = &.{},
209222 };
210223
211 if (compile) |c| {
212 m.depending_steps.put(owner.allocator, c, {}) catch @panic("OOM");
224 m.import_table.ensureUnusedCapacity(allocator, options.imports.len) catch @panic("OOM");
225 for (options.imports) |dep| {
226 m.import_table.putAssumeCapacity(dep.name, dep.module);
213227 }
214228
215 m.import_table.ensureUnusedCapacity(options.import_table.len) catch @panic("OOM");
216 for (options.import_table) |dep| {
217 m.import_table.putAssumeCapacity(dep.name, dep.module);
229 if (compile) |c| {
230 m.depending_steps.put(allocator, c, {}) catch @panic("OOM");
218231 }
219232
233 // This logic accesses `depending_steps` which was just modified above.
220234 var it = m.iterateDependencies(null);
221 while (it.next()) |item| addShallowDependencies(&m, item.module);
222
223 return m;
235 while (it.next()) |item| addShallowDependencies(m, item.module);
224236}
225237
226238pub fn create(owner: *std.Build, options: CreateOptions) *Module {
227239 const m = owner.allocator.create(Module) catch @panic("OOM");
228 m.* = init(owner, options, null);
240 m.init(owner, options, null);
229241 return m;
230242}
231243
232244/// Adds an existing module to be used with `@import`.
233245pub fn addImport(m: *Module, name: []const u8, module: *Module) void {
234246 const b = m.owner;
235 m.import_table.put(b.dupe(name), module) catch @panic("OOM");
247 m.import_table.put(b.allocator, b.dupe(name), module) catch @panic("OOM");
236248
237249 var it = module.iterateDependencies(null);
238250 while (it.next()) |item| addShallowDependencies(m, item.module);
......@@ -244,7 +256,10 @@ pub fn addImport(m: *Module, name: []const u8, module: *Module) void {
244256fn addShallowDependencies(m: *Module, dependee: *Module) void {
245257 if (dependee.root_source_file) |lazy_path| addLazyPathDependencies(m, dependee, lazy_path);
246258 for (dependee.lib_paths.items) |lib_path| addLazyPathDependencies(m, dependee, lib_path);
247 for (dependee.rpaths.items) |rpath| addLazyPathDependencies(m, dependee, rpath);
259 for (dependee.rpaths.items) |rpath| switch (rpath) {
260 .lazy_path => |lp| addLazyPathDependencies(m, dependee, lp),
261 .special => {},
262 };
248263
249264 for (dependee.link_objects.items) |link_object| switch (link_object) {
250265 .other_step => |compile| addStepDependencies(m, dependee, &compile.step),
......@@ -277,7 +292,7 @@ fn addLazyPathDependenciesOnly(m: *Module, lazy_path: LazyPath) void {
277292 }
278293}
279294
280fn addStepDependencies(m: *Module, module: *Module, dependee: *std.Build.Step) void {
295fn addStepDependencies(m: *Module, module: *Module, dependee: *Step) void {
281296 addStepDependenciesOnly(m, dependee);
282297 if (m != module) {
283298 for (m.depending_steps.keys()) |compile| {
......@@ -286,20 +301,19 @@ fn addStepDependencies(m: *Module, module: *Module, dependee: *std.Build.Step) v
286301 }
287302}
288303
289fn addStepDependenciesOnly(m: *Module, dependee: *std.Build.Step) void {
304fn addStepDependenciesOnly(m: *Module, dependee: *Step) void {
290305 for (m.depending_steps.keys()) |compile| {
291306 compile.step.dependOn(dependee);
292307 }
293308}
294309
295310/// Creates a new module and adds it to be used with `@import`.
296pub fn addAnonymousImport(m: *Module, name: []const u8, options: std.Build.CreateModuleOptions) void {
297 const b = m.step.owner;
298 const module = b.createModule(options);
311pub fn addAnonymousImport(m: *Module, name: []const u8, options: CreateOptions) void {
312 const module = create(m.owner, options);
299313 return addImport(m, name, module);
300314}
301315
302pub fn addOptions(m: *Module, module_name: []const u8, options: *std.Build.Step.Options) void {
316pub fn addOptions(m: *Module, module_name: []const u8, options: *Step.Options) void {
303317 addImport(m, module_name, options.createModule());
304318}
305319
......@@ -311,14 +325,14 @@ pub const DependencyIterator = struct {
311325 pub const Key = struct {
312326 /// The compilation that contains the `Module`. Note that a `Module` might be
313327 /// used by more than one compilation.
314 compile: ?*std.Build.Step.Compile,
328 compile: ?*Step.Compile,
315329 module: *Module,
316330 };
317331
318332 pub const Item = struct {
319333 /// The compilation that contains the `Module`. Note that a `Module` might be
320334 /// used by more than one compilation.
321 compile: ?*std.Build.Step.Compile,
335 compile: ?*Step.Compile,
322336 module: *Module,
323337 name: []const u8,
324338 };
......@@ -368,7 +382,7 @@ pub const DependencyIterator = struct {
368382
369383pub fn iterateDependencies(
370384 m: *Module,
371 chase_steps: ?*std.Build.Step.Compile,
385 chase_steps: ?*Step.Compile,
372386) DependencyIterator {
373387 var it: DependencyIterator = .{
374388 .allocator = m.owner.allocator,
......@@ -397,16 +411,18 @@ pub fn linkSystemLibrary(
397411 options: LinkSystemLibraryOptions,
398412) void {
399413 const b = m.owner;
400 if (m.target_info.target.is_libc_lib_name(name)) {
414
415 const target = m.requireKnownTarget();
416 if (target.is_libc_lib_name(name)) {
401417 m.link_libc = true;
402418 return;
403419 }
404 if (m.target_info.target.is_libcpp_lib_name(name)) {
420 if (target.is_libcpp_lib_name(name)) {
405421 m.link_libcpp = true;
406422 return;
407423 }
408424
409 m.link_objects.append(.{
425 m.link_objects.append(b.allocator, .{
410426 .system_lib = .{
411427 .name = b.dupe(name),
412428 .needed = options.needed,
......@@ -418,6 +434,11 @@ pub fn linkSystemLibrary(
418434 }) catch @panic("OOM");
419435}
420436
437pub fn linkFramework(m: *Module, name: []const u8, options: LinkFrameworkOptions) void {
438 const b = m.owner;
439 m.frameworks.put(b.allocator, b.dupe(name), options) catch @panic("OOM");
440}
441
421442pub const AddCSourceFilesOptions = struct {
422443 /// When provided, `files` are relative to `dependency` rather than the
423444 /// package that owns the `Compile` step.
......@@ -428,19 +449,23 @@ pub const AddCSourceFilesOptions = struct {
428449
429450/// Handy when you have many C/C++ source files and want them all to have the same flags.
430451pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
431 const c_source_files = m.owner.allocator.create(CSourceFiles) catch @panic("OOM");
452 const b = m.owner;
453 const allocator = b.allocator;
454 const c_source_files = allocator.create(CSourceFiles) catch @panic("OOM");
432455 c_source_files.* = .{
433456 .dependency = options.dependency,
434 .files = m.owner.dupeStrings(options.files),
435 .flags = m.owner.dupeStrings(options.flags),
457 .files = b.dupeStrings(options.files),
458 .flags = b.dupeStrings(options.flags),
436459 };
437 m.link_objects.append(.{ .c_source_files = c_source_files }) catch @panic("OOM");
460 m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM");
438461}
439462
440463pub fn addCSourceFile(m: *Module, source: CSourceFile) void {
441 const c_source_file = m.owner.allocator.create(CSourceFile) catch @panic("OOM");
442 c_source_file.* = source.dupe(m.owner);
443 m.link_objects.append(.{ .c_source_file = c_source_file }) catch @panic("OOM");
464 const b = m.owner;
465 const allocator = b.allocator;
466 const c_source_file = allocator.create(CSourceFile) catch @panic("OOM");
467 c_source_file.* = source.dupe(b);
468 m.link_objects.append(allocator, .{ .c_source_file = c_source_file }) catch @panic("OOM");
444469 addLazyPathDependenciesOnly(m, source.file);
445470}
446471
......@@ -448,30 +473,122 @@ pub fn addCSourceFile(m: *Module, source: CSourceFile) void {
448473/// Can be called regardless of target. The .rc file will be ignored
449474/// if the target object format does not support embedded resources.
450475pub fn addWin32ResourceFile(m: *Module, source: RcSourceFile) void {
476 const b = m.owner;
477 const allocator = b.allocator;
478 const target = m.requireKnownTarget();
451479 // Only the PE/COFF format has a Resource Table, so for any other target
452480 // the resource file is ignored.
453 if (m.target_info.target.ofmt != .coff) return;
481 if (target.ofmt != .coff) return;
454482
455 const rc_source_file = m.owner.allocator.create(RcSourceFile) catch @panic("OOM");
456 rc_source_file.* = source.dupe(m.owner);
457 m.link_objects.append(.{ .win32_resource_file = rc_source_file }) catch @panic("OOM");
483 const rc_source_file = allocator.create(RcSourceFile) catch @panic("OOM");
484 rc_source_file.* = source.dupe(b);
485 m.link_objects.append(allocator, .{ .win32_resource_file = rc_source_file }) catch @panic("OOM");
458486 addLazyPathDependenciesOnly(m, source.file);
459487}
460488
461489pub fn addAssemblyFile(m: *Module, source: LazyPath) void {
462 m.link_objects.append(.{ .assembly_file = source.dupe(m.owner) }) catch @panic("OOM");
490 const b = m.owner;
491 m.link_objects.append(b.allocator, .{ .assembly_file = source.dupe(b) }) catch @panic("OOM");
463492 addLazyPathDependenciesOnly(m, source);
464493}
465494
466pub fn addObjectFile(m: *Module, source: LazyPath) void {
467 m.link_objects.append(.{ .static_path = source.dupe(m.owner) }) catch @panic("OOM");
468 addLazyPathDependencies(m, source);
495pub fn addObjectFile(m: *Module, object: LazyPath) void {
496 const b = m.owner;
497 m.link_objects.append(b.allocator, .{ .static_path = object.dupe(b) }) catch @panic("OOM");
498 addLazyPathDependenciesOnly(m, object);
499}
500
501pub fn addObject(m: *Module, object: *Step.Compile) void {
502 assert(object.kind == .obj);
503 m.linkLibraryOrObject(object);
504}
505
506pub fn linkLibrary(m: *Module, library: *Step.Compile) void {
507 assert(library.kind == .lib);
508 m.linkLibraryOrObject(library);
509}
510
511pub fn addAfterIncludePath(m: *Module, lazy_path: LazyPath) void {
512 const b = m.owner;
513 m.include_dirs.append(b.allocator, .{ .path_after = lazy_path.dupe(b) }) catch @panic("OOM");
514 addLazyPathDependenciesOnly(m, lazy_path);
515}
516
517pub fn addSystemIncludePath(m: *Module, lazy_path: LazyPath) void {
518 const b = m.owner;
519 m.include_dirs.append(b.allocator, .{ .path_system = lazy_path.dupe(b) }) catch @panic("OOM");
520 addLazyPathDependenciesOnly(m, lazy_path);
521}
522
523pub fn addIncludePath(m: *Module, lazy_path: LazyPath) void {
524 const b = m.owner;
525 m.include_dirs.append(b.allocator, .{ .path = lazy_path.dupe(b) }) catch @panic("OOM");
526 addLazyPathDependenciesOnly(m, lazy_path);
527}
528
529pub fn addConfigHeader(m: *Module, config_header: *Step.ConfigHeader) void {
530 const allocator = m.owner.allocator;
531 m.include_dirs.append(allocator, .{ .config_header_step = config_header }) catch @panic("OOM");
532 addStepDependenciesOnly(m, &config_header.step);
533}
534
535pub fn addSystemFrameworkPath(m: *Module, directory_path: LazyPath) void {
536 const b = m.owner;
537 m.include_dirs.append(b.allocator, .{ .framework_path_system = directory_path.dupe(b) }) catch
538 @panic("OOM");
539 addLazyPathDependenciesOnly(m, directory_path);
540}
541
542pub fn addFrameworkPath(m: *Module, directory_path: LazyPath) void {
543 const b = m.owner;
544 m.include_dirs.append(b.allocator, .{ .framework_path = directory_path.dupe(b) }) catch
545 @panic("OOM");
546 addLazyPathDependenciesOnly(m, directory_path);
547}
548
549pub fn addLibraryPath(m: *Module, directory_path: LazyPath) void {
550 const b = m.owner;
551 m.lib_paths.append(b.allocator, directory_path.dupe(b)) catch @panic("OOM");
552 addLazyPathDependenciesOnly(m, directory_path);
553}
554
555pub fn addRPath(m: *Module, directory_path: LazyPath) void {
556 const b = m.owner;
557 switch (directory_path) {
558 .path, .cwd_relative => |path| {
559 // TODO: remove this check after people upgrade and stop expecting it to work
560 if (std.mem.startsWith(u8, path, "@executable_path") or
561 std.mem.startsWith(u8, path, "@loader_path"))
562 {
563 @panic("this function is for adding directory paths. It does not support special rpaths. use addRPathSpecial for that.");
564 }
565 },
566 else => {},
567 }
568 m.rpaths.append(b.allocator, .{ .lazy_path = directory_path.dupe(b) }) catch @panic("OOM");
569 addLazyPathDependenciesOnly(m, directory_path);
570}
571
572pub fn addRPathSpecial(m: *Module, bytes: []const u8) void {
573 const b = m.owner;
574 m.rpaths.append(b.allocator, .{ .special = b.dupe(bytes) }) catch @panic("OOM");
575}
576
577/// Equvialent to the following C code, applied to all C source files owned by
578/// this `Module`:
579/// ```c
580/// #define name value
581/// ```
582/// `name` and `value` need not live longer than the function call.
583pub fn addCMacro(m: *Module, name: []const u8, value: []const u8) void {
584 const b = m.owner;
585 m.c_macros.append(b.allocator, b.fmt("-D{s}={s}", .{ name, value })) catch @panic("OOM");
469586}
470587
471588pub fn appendZigProcessFlags(
472589 m: *Module,
473590 zig_args: *std.ArrayList([]const u8),
474 asking_step: ?*std.Build.Step,
591 asking_step: ?*Step,
475592) !void {
476593 const b = m.owner;
477594
......@@ -495,27 +612,30 @@ pub fn appendZigProcessFlags(
495612 }
496613
497614 try zig_args.ensureUnusedCapacity(1);
498 switch (m.optimize) {
499 .Debug => {}, // Skip since it's the default.
615 if (m.optimize) |optimize| switch (optimize) {
616 .Debug => zig_args.appendAssumeCapacity("-ODebug"),
500617 .ReleaseSmall => zig_args.appendAssumeCapacity("-OReleaseSmall"),
501618 .ReleaseFast => zig_args.appendAssumeCapacity("-OReleaseFast"),
502619 .ReleaseSafe => zig_args.appendAssumeCapacity("-OReleaseSafe"),
503 }
620 };
504621
505622 if (m.code_model != .default) {
506623 try zig_args.append("-mcmodel");
507624 try zig_args.append(@tagName(m.code_model));
508625 }
509626
510 if (!m.target.isNative()) {
511 try zig_args.appendSlice(&.{
512 "-target", try m.target.zigTriple(b.allocator),
513 "-mcpu", try std.Build.serializeCpu(b.allocator, m.target.getCpu()),
514 });
515
516 if (m.target.dynamic_linker.get()) |dynamic_linker| {
517 try zig_args.append("--dynamic-linker");
518 try zig_args.append(dynamic_linker);
627 if (m.target) |target| {
628 // Communicate the query via CLI since it's more compact.
629 if (!target.query.isNative()) {
630 try zig_args.appendSlice(&.{
631 "-target", try target.query.zigTriple(b.allocator),
632 "-mcpu", try std.Build.serializeCpu(b.allocator, target.query.getCpu()),
633 });
634
635 if (target.query.dynamic_linker.get()) |dynamic_linker| {
636 try zig_args.append("--dynamic-linker");
637 try zig_args.append(dynamic_linker);
638 }
519639 }
520640 }
521641
......@@ -565,10 +685,7 @@ pub fn appendZigProcessFlags(
565685 }
566686 }
567687
568 for (m.c_macros.items) |c_macro| {
569 try zig_args.append("-D");
570 try zig_args.append(c_macro);
571 }
688 try zig_args.appendSlice(m.c_macros.items);
572689
573690 try zig_args.ensureUnusedCapacity(2 * m.lib_paths.items.len);
574691 for (m.lib_paths.items) |lib_path| {
......@@ -577,26 +694,16 @@ pub fn appendZigProcessFlags(
577694 }
578695
579696 try zig_args.ensureUnusedCapacity(2 * m.rpaths.items.len);
580 for (m.rpaths.items) |rpath| {
581 zig_args.appendAssumeCapacity("-rpath");
582
583 if (m.target_info.target.isDarwin()) switch (rpath) {
584 .path, .cwd_relative => |path| {
585 // On Darwin, we should not try to expand special runtime paths such as
586 // * @executable_path
587 // * @loader_path
588 if (std.mem.startsWith(u8, path, "@executable_path") or
589 std.mem.startsWith(u8, path, "@loader_path"))
590 {
591 zig_args.appendAssumeCapacity(path);
592 continue;
593 }
594 },
595 .generated, .dependency => {},
596 };
597
598 zig_args.appendAssumeCapacity(rpath.getPath2(b, asking_step));
599 }
697 for (m.rpaths.items) |rpath| switch (rpath) {
698 .lazy_path => |lp| {
699 zig_args.appendAssumeCapacity("-rpath");
700 zig_args.appendAssumeCapacity(lp.getPath2(b, asking_step));
701 },
702 .special => |bytes| {
703 zig_args.appendAssumeCapacity("-rpath");
704 zig_args.appendAssumeCapacity(bytes);
705 },
706 };
600707}
601708
602709fn addFlag(
......@@ -609,8 +716,32 @@ fn addFlag(
609716 return args.append(if (cond) then_name else else_name);
610717}
611718
719fn linkLibraryOrObject(m: *Module, other: *Step.Compile) void {
720 const allocator = m.owner.allocator;
721 _ = other.getEmittedBin(); // Indicate there is a dependency on the outputted binary.
722 addStepDependenciesOnly(m, &other.step);
723
724 if (other.rootModuleTarget().os.tag == .windows and other.isDynamicLibrary()) {
725 _ = other.getEmittedImplib(); // Indicate dependency on the outputted implib.
726 }
727
728 m.link_objects.append(allocator, .{ .other_step = other }) catch @panic("OOM");
729 m.include_dirs.append(allocator, .{ .other_step = other }) catch @panic("OOM");
730
731 for (other.installed_headers.items) |install_step| {
732 addStepDependenciesOnly(m, install_step);
733 }
734}
735
736fn requireKnownTarget(m: *Module) std.Target {
737 const resolved_target = m.target orelse
738 @panic("this API requires the Module to be created with a known 'target' field");
739 return resolved_target.target;
740}
741
612742const Module = @This();
613743const std = @import("std");
614744const assert = std.debug.assert;
615745const LazyPath = std.Build.LazyPath;
616746const NativeTargetInfo = std.zig.system.NativeTargetInfo;
747const Step = std.Build.Step;
lib/std/Build/Step/CheckObject.zig+26-22
......@@ -44,11 +44,11 @@ pub fn create(
4444
4545const SearchPhrase = struct {
4646 string: []const u8,
47 file_source: ?std.Build.LazyPath = null,
47 lazy_path: ?std.Build.LazyPath = null,
4848
4949 fn resolve(phrase: SearchPhrase, b: *std.Build, step: *Step) []const u8 {
50 const file_source = phrase.file_source orelse return phrase.string;
51 return b.fmt("{s} {s}", .{ phrase.string, file_source.getPath2(b, step) });
50 const lazy_path = phrase.lazy_path orelse return phrase.string;
51 return b.fmt("{s} {s}", .{ phrase.string, lazy_path.getPath2(b, step) });
5252 }
5353};
5454
......@@ -321,14 +321,14 @@ pub fn checkExact(self: *CheckObject, phrase: []const u8) void {
321321
322322/// Like `checkExact()` but takes an additional argument `LazyPath` which will be
323323/// resolved to a full search query in `make()`.
324pub fn checkExactPath(self: *CheckObject, phrase: []const u8, file_source: std.Build.LazyPath) void {
325 self.checkExactInner(phrase, file_source);
324pub fn checkExactPath(self: *CheckObject, phrase: []const u8, lazy_path: std.Build.LazyPath) void {
325 self.checkExactInner(phrase, lazy_path);
326326}
327327
328fn checkExactInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.LazyPath) void {
328fn checkExactInner(self: *CheckObject, phrase: []const u8, lazy_path: ?std.Build.LazyPath) void {
329329 assert(self.checks.items.len > 0);
330330 const last = &self.checks.items[self.checks.items.len - 1];
331 last.exact(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source });
331 last.exact(.{ .string = self.step.owner.dupe(phrase), .lazy_path = lazy_path });
332332}
333333
334334/// Adds a fuzzy match phrase to the latest created Check.
......@@ -336,16 +336,20 @@ pub fn checkContains(self: *CheckObject, phrase: []const u8) void {
336336 self.checkContainsInner(phrase, null);
337337}
338338
339/// Like `checkContains()` but takes an additional argument `FileSource` which will be
339/// Like `checkContains()` but takes an additional argument `lazy_path` which will be
340340/// resolved to a full search query in `make()`.
341pub fn checkContainsPath(self: *CheckObject, phrase: []const u8, file_source: std.Build.LazyPath) void {
342 self.checkContainsInner(phrase, file_source);
341pub fn checkContainsPath(
342 self: *CheckObject,
343 phrase: []const u8,
344 lazy_path: std.Build.LazyPath,
345) void {
346 self.checkContainsInner(phrase, lazy_path);
343347}
344348
345fn checkContainsInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void {
349fn checkContainsInner(self: *CheckObject, phrase: []const u8, lazy_path: ?std.Build.LazyPath) void {
346350 assert(self.checks.items.len > 0);
347351 const last = &self.checks.items[self.checks.items.len - 1];
348 last.contains(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source });
352 last.contains(.{ .string = self.step.owner.dupe(phrase), .lazy_path = lazy_path });
349353}
350354
351355/// Adds an exact match phrase with variable extractor to the latest created Check.
......@@ -353,16 +357,16 @@ pub fn checkExtract(self: *CheckObject, phrase: []const u8) void {
353357 self.checkExtractInner(phrase, null);
354358}
355359
356/// Like `checkExtract()` but takes an additional argument `FileSource` which will be
360/// Like `checkExtract()` but takes an additional argument `LazyPath` which will be
357361/// resolved to a full search query in `make()`.
358pub fn checkExtractFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void {
359 self.checkExtractInner(phrase, file_source);
362pub fn checkExtractLazyPath(self: *CheckObject, phrase: []const u8, lazy_path: std.Build.LazyPath) void {
363 self.checkExtractInner(phrase, lazy_path);
360364}
361365
362fn checkExtractInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void {
366fn checkExtractInner(self: *CheckObject, phrase: []const u8, lazy_path: ?std.Build.LazyPath) void {
363367 assert(self.checks.items.len > 0);
364368 const last = &self.checks.items[self.checks.items.len - 1];
365 last.extract(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source });
369 last.extract(.{ .string = self.step.owner.dupe(phrase), .lazy_path = lazy_path });
366370}
367371
368372/// Adds another searched phrase to the latest created Check
......@@ -371,16 +375,16 @@ pub fn checkNotPresent(self: *CheckObject, phrase: []const u8) void {
371375 self.checkNotPresentInner(phrase, null);
372376}
373377
374/// Like `checkExtract()` but takes an additional argument `FileSource` which will be
378/// Like `checkExtract()` but takes an additional argument `LazyPath` which will be
375379/// resolved to a full search query in `make()`.
376pub fn checkNotPresentFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void {
377 self.checkNotPresentInner(phrase, file_source);
380pub fn checkNotPresentLazyPath(self: *CheckObject, phrase: []const u8, lazy_path: std.Build.LazyPath) void {
381 self.checkNotPresentInner(phrase, lazy_path);
378382}
379383
380fn checkNotPresentInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void {
384fn checkNotPresentInner(self: *CheckObject, phrase: []const u8, lazy_path: ?std.Build.LazyPath) void {
381385 assert(self.checks.items.len > 0);
382386 const last = &self.checks.items[self.checks.items.len - 1];
383 last.notPresent(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source });
387 last.notPresent(.{ .string = self.step.owner.dupe(phrase), .lazy_path = lazy_path });
384388}
385389
386390/// Creates a new check checking in the file headers (section, program headers, etc.).
lib/std/Build/Step/Compile.zig+81-203
......@@ -88,7 +88,7 @@ each_lib_rpath: ?bool = null,
8888/// As an example, the bloaty project refuses to work unless its inputs have
8989/// build ids, in order to prevent accidental mismatches.
9090/// The default is to not include this section because it slows down linking.
91build_id: ?BuildId = null,
91build_id: ?std.zig.BuildId = null,
9292
9393/// Create a .eh_frame_hdr section and a PT_GNU_EH_FRAME segment in the ELF
9494/// file.
......@@ -200,7 +200,7 @@ pub const ExpectedCompileErrors = union(enum) {
200200 exact: []const []const u8,
201201};
202202
203const Entry = union(enum) {
203pub const Entry = union(enum) {
204204 /// Let the compiler decide whether to make an entry point and what to name
205205 /// it.
206206 default,
......@@ -232,82 +232,6 @@ pub const Options = struct {
232232 win32_manifest: ?LazyPath = null,
233233};
234234
235pub const BuildId = union(enum) {
236 none,
237 fast,
238 uuid,
239 sha1,
240 md5,
241 hexstring: HexString,
242
243 pub fn eql(a: BuildId, b: BuildId) bool {
244 const a_tag = std.meta.activeTag(a);
245 const b_tag = std.meta.activeTag(b);
246 if (a_tag != b_tag) return false;
247 return switch (a) {
248 .none, .fast, .uuid, .sha1, .md5 => true,
249 .hexstring => |a_hexstring| mem.eql(u8, a_hexstring.toSlice(), b.hexstring.toSlice()),
250 };
251 }
252
253 pub const HexString = struct {
254 bytes: [32]u8,
255 len: u8,
256
257 /// Result is byte values, *not* hex-encoded.
258 pub fn toSlice(hs: *const HexString) []const u8 {
259 return hs.bytes[0..hs.len];
260 }
261 };
262
263 /// Input is byte values, *not* hex-encoded.
264 /// Asserts `bytes` fits inside `HexString`
265 pub fn initHexString(bytes: []const u8) BuildId {
266 var result: BuildId = .{ .hexstring = .{
267 .bytes = undefined,
268 .len = @as(u8, @intCast(bytes.len)),
269 } };
270 @memcpy(result.hexstring.bytes[0..bytes.len], bytes);
271 return result;
272 }
273
274 /// Converts UTF-8 text to a `BuildId`.
275 pub fn parse(text: []const u8) !BuildId {
276 if (mem.eql(u8, text, "none")) {
277 return .none;
278 } else if (mem.eql(u8, text, "fast")) {
279 return .fast;
280 } else if (mem.eql(u8, text, "uuid")) {
281 return .uuid;
282 } else if (mem.eql(u8, text, "sha1") or mem.eql(u8, text, "tree")) {
283 return .sha1;
284 } else if (mem.eql(u8, text, "md5")) {
285 return .md5;
286 } else if (mem.startsWith(u8, text, "0x")) {
287 var result: BuildId = .{ .hexstring = undefined };
288 const slice = try std.fmt.hexToBytes(&result.hexstring.bytes, text[2..]);
289 result.hexstring.len = @as(u8, @intCast(slice.len));
290 return result;
291 }
292 return error.InvalidBuildIdStyle;
293 }
294
295 test parse {
296 try std.testing.expectEqual(BuildId.md5, try parse("md5"));
297 try std.testing.expectEqual(BuildId.none, try parse("none"));
298 try std.testing.expectEqual(BuildId.fast, try parse("fast"));
299 try std.testing.expectEqual(BuildId.uuid, try parse("uuid"));
300 try std.testing.expectEqual(BuildId.sha1, try parse("sha1"));
301 try std.testing.expectEqual(BuildId.sha1, try parse("tree"));
302
303 try std.testing.expect(BuildId.initHexString("").eql(try parse("0x")));
304 try std.testing.expect(BuildId.initHexString("\x12\x34\x56").eql(try parse("0x123456")));
305 try std.testing.expectError(error.InvalidLength, parse("0x12-34"));
306 try std.testing.expectError(error.InvalidCharacter, parse("0xfoobbb"));
307 try std.testing.expectError(error.InvalidBuildIdStyle, parse("yaddaxxx"));
308 }
309};
310
311235pub const Kind = enum {
312236 exe,
313237 lib,
......@@ -329,6 +253,8 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
329253 else
330254 owner.fmt("{s} ", .{name});
331255
256 const target = options.root_module.target.?.target;
257
332258 const step_name = owner.fmt("{s} {s}{s} {s}", .{
333259 switch (options.kind) {
334260 .exe => "zig build-exe",
......@@ -337,16 +263,13 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
337263 .@"test" => "zig test",
338264 },
339265 name_adjusted,
340 @tagName(options.root_module.optimize),
341 options.root_module.target.zigTriple(owner.allocator) catch @panic("OOM"),
266 @tagName(options.root_module.optimize orelse .Debug),
267 target.zigTriple(owner.allocator) catch @panic("OOM"),
342268 });
343269
344 const target_info = NativeTargetInfo.detect(options.root_module.target) catch
345 @panic("unhandled error");
346
347270 const out_filename = std.zig.binNameAlloc(owner.allocator, .{
348271 .root_name = name,
349 .target = target_info.target,
272 .target = target,
350273 .output_mode = switch (options.kind) {
351274 .lib => .Lib,
352275 .obj => .Obj,
......@@ -361,7 +284,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
361284
362285 const self = owner.allocator.create(Compile) catch @panic("OOM");
363286 self.* = .{
364 .root_module = Module.init(owner, options.root_module, self),
287 .root_module = undefined,
365288 .verbose_link = false,
366289 .verbose_cc = false,
367290 .linkage = options.linkage,
......@@ -403,6 +326,8 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
403326 .use_lld = options.use_lld,
404327 };
405328
329 self.root_module.init(owner, options.root_module, self);
330
406331 if (options.zig_lib_dir) |lp| {
407332 self.zig_lib_dir = lp.dupe(self.step.owner);
408333 lp.addStepDependencies(&self.step);
......@@ -410,7 +335,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
410335
411336 // Only the PE/COFF format has a Resource Table which is where the manifest
412337 // gets embedded, so for any other target the manifest file is just ignored.
413 if (target_info.target.ofmt == .coff) {
338 if (target.ofmt == .coff) {
414339 if (options.win32_manifest) |lp| {
415340 self.win32_manifest = lp.dupe(self.step.owner);
416341 lp.addStepDependencies(&self.step);
......@@ -421,14 +346,14 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
421346 if (self.linkage != null and self.linkage.? == .static) {
422347 self.out_lib_filename = self.out_filename;
423348 } else if (self.version) |version| {
424 if (target_info.target.isDarwin()) {
349 if (target.isDarwin()) {
425350 self.major_only_filename = owner.fmt("lib{s}.{d}.dylib", .{
426351 self.name,
427352 version.major,
428353 });
429354 self.name_only_filename = owner.fmt("lib{s}.dylib", .{self.name});
430355 self.out_lib_filename = self.out_filename;
431 } else if (target_info.target.os.tag == .windows) {
356 } else if (target.os.tag == .windows) {
432357 self.out_lib_filename = owner.fmt("{s}.lib", .{self.name});
433358 } else {
434359 self.major_only_filename = owner.fmt("lib{s}.so.{d}", .{ self.name, version.major });
......@@ -436,9 +361,9 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
436361 self.out_lib_filename = self.out_filename;
437362 }
438363 } else {
439 if (target_info.target.isDarwin()) {
364 if (target.isDarwin()) {
440365 self.out_lib_filename = self.out_filename;
441 } else if (target_info.target.os.tag == .windows) {
366 } else if (target.os.tag == .windows) {
442367 self.out_lib_filename = owner.fmt("{s}.lib", .{self.name});
443368 } else {
444369 self.out_lib_filename = self.out_filename;
......@@ -545,7 +470,7 @@ pub const run = @compileError("deprecated; use std.Build.addRunArtifact");
545470pub const install = @compileError("deprecated; use std.Build.installArtifact");
546471
547472pub fn checkObject(self: *Compile) *Step.CheckObject {
548 return Step.CheckObject.create(self.step.owner, self.getEmittedBin(), self.target_info.target.ofmt);
473 return Step.CheckObject.create(self.step.owner, self.getEmittedBin(), self.rootModuleTarget().ofmt);
549474}
550475
551476/// deprecated: use `setLinkerScript`
......@@ -562,25 +487,6 @@ pub fn forceUndefinedSymbol(self: *Compile, symbol_name: []const u8) void {
562487 self.force_undefined_symbols.put(b.dupe(symbol_name), {}) catch @panic("OOM");
563488}
564489
565pub fn linkFramework(self: *Compile, framework_name: []const u8) void {
566 const b = self.step.owner;
567 self.frameworks.put(b.dupe(framework_name), .{}) catch @panic("OOM");
568}
569
570pub fn linkFrameworkNeeded(self: *Compile, framework_name: []const u8) void {
571 const b = self.step.owner;
572 self.frameworks.put(b.dupe(framework_name), .{
573 .needed = true,
574 }) catch @panic("OOM");
575}
576
577pub fn linkFrameworkWeak(self: *Compile, framework_name: []const u8) void {
578 const b = self.step.owner;
579 self.frameworks.put(b.dupe(framework_name), .{
580 .weak = true,
581 }) catch @panic("OOM");
582}
583
584490/// Returns whether the library, executable, or object depends on a particular system library.
585491pub fn dependsOnSystemLibrary(self: *const Compile, name: []const u8) bool {
586492 var is_linking_libc = false;
......@@ -598,22 +504,17 @@ pub fn dependsOnSystemLibrary(self: *const Compile, name: []const u8) bool {
598504 is_linking_libcpp = is_linking_libcpp or module.link_libcpp == true;
599505 }
600506
601 if (self.root_module.target_info.target.is_libc_lib_name(name)) {
507 if (self.rootModuleTarget().is_libc_lib_name(name)) {
602508 return is_linking_libc;
603509 }
604510
605 if (self.root_module.target_info.target.is_libcpp_lib_name(name)) {
511 if (self.rootModuleTarget().is_libcpp_lib_name(name)) {
606512 return is_linking_libcpp;
607513 }
608514
609515 return false;
610516}
611517
612pub fn linkLibrary(self: *Compile, lib: *Compile) void {
613 assert(lib.kind == .lib);
614 self.linkLibraryOrObject(lib);
615}
616
617518pub fn isDynamicLibrary(self: *const Compile) bool {
618519 return self.kind == .lib and self.linkage == Linkage.dynamic;
619520}
......@@ -623,16 +524,24 @@ pub fn isStaticLibrary(self: *const Compile) bool {
623524}
624525
625526pub fn producesPdbFile(self: *Compile) bool {
527 const target = self.rootModuleTarget();
626528 // TODO: Is this right? Isn't PDB for *any* PE/COFF file?
627529 // TODO: just share this logic with the compiler, silly!
628 if (!self.target.isWindows() and !self.target.isUefi()) return false;
629 if (self.target.getObjectFormat() == .c) return false;
630 if (self.strip == true or (self.strip == null and self.optimize == .ReleaseSmall)) return false;
530 switch (target.os.tag) {
531 .windows, .uefi => {},
532 else => return false,
533 }
534 if (target.ofmt == .c) return false;
535 if (self.root_module.strip == true or
536 (self.root_module.strip == null and self.root_module.optimize == .ReleaseSmall))
537 {
538 return false;
539 }
631540 return self.isDynamicLibrary() or self.kind == .exe or self.kind == .@"test";
632541}
633542
634543pub fn producesImplib(self: *Compile) bool {
635 return self.isDynamicLibrary() and self.root_module.target_info.target.os.tag == .windows;
544 return self.isDynamicLibrary() and self.rootModuleTarget().os.tag == .windows;
636545}
637546
638547pub fn linkLibC(self: *Compile) void {
......@@ -643,18 +552,9 @@ pub fn linkLibCpp(self: *Compile) void {
643552 self.root_module.link_libcpp = true;
644553}
645554
646/// If the value is omitted, it is set to 1.
647/// `name` and `value` need not live longer than the function call.
648pub fn defineCMacro(self: *Compile, name: []const u8, value: ?[]const u8) void {
649 const b = self.step.owner;
650 const macro = std.Build.constructCMacro(b.allocator, name, value);
651 self.c_macros.append(macro) catch @panic("OOM");
652}
653
654/// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1.
655pub fn defineCMacroRaw(self: *Compile, name_and_value: []const u8) void {
656 const b = self.step.owner;
657 self.c_macros.append(b.dupe(name_and_value)) catch @panic("OOM");
555/// Deprecated. Use `c.root_module.addCMacro`.
556pub fn defineCMacro(c: *Compile, name: []const u8, value: ?[]const u8) void {
557 c.root_module.addCMacro(name, value orelse "1");
658558}
659559
660560/// Run pkg-config for the given library name and parse the output, returning the arguments
......@@ -765,6 +665,20 @@ pub fn linkSystemLibrary2(
765665 return self.root_module.linkSystemLibrary(name, options);
766666}
767667
668pub fn linkFramework(c: *Compile, name: []const u8) void {
669 c.root_module.linkFramework(name, .{});
670}
671
672/// Deprecated. Use `c.root_module.linkFramework`.
673pub fn linkFrameworkNeeded(c: *Compile, name: []const u8) void {
674 c.root_module.linkFramework(name, .{ .needed = true });
675}
676
677/// Deprecated. Use `c.root_module.linkFramework`.
678pub fn linkFrameworkWeak(c: *Compile, name: []const u8) void {
679 c.root_module.linkFramework(name, .{ .weak = true });
680}
681
768682/// Handy when you have many C/C++ source files and want them all to have the same flags.
769683pub fn addCSourceFiles(self: *Compile, options: Module.AddCSourceFilesOptions) void {
770684 self.root_module.addCSourceFiles(options);
......@@ -805,27 +719,18 @@ fn getEmittedFileGeneric(self: *Compile, output_file: *?*GeneratedFile) LazyPath
805719 return .{ .generated = generated_file };
806720}
807721
808/// deprecated: use `getEmittedBinDirectory`
809pub const getOutputDirectorySource = getEmittedBinDirectory;
810
811722/// Returns the path to the directory that contains the emitted binary file.
812723pub fn getEmittedBinDirectory(self: *Compile) LazyPath {
813724 _ = self.getEmittedBin();
814725 return self.getEmittedFileGeneric(&self.emit_directory);
815726}
816727
817/// deprecated: use `getEmittedBin`
818pub const getOutputSource = getEmittedBin;
819
820728/// Returns the path to the generated executable, library or object file.
821729/// To run an executable built with zig build, use `run`, or create an install step and invoke it.
822730pub fn getEmittedBin(self: *Compile) LazyPath {
823731 return self.getEmittedFileGeneric(&self.generated_bin);
824732}
825733
826/// deprecated: use `getEmittedImplib`
827pub const getOutputLibSource = getEmittedImplib;
828
829734/// Returns the path to the generated import library.
830735/// This function can only be called for libraries.
831736pub fn getEmittedImplib(self: *Compile) LazyPath {
......@@ -833,9 +738,6 @@ pub fn getEmittedImplib(self: *Compile) LazyPath {
833738 return self.getEmittedFileGeneric(&self.generated_implib);
834739}
835740
836/// deprecated: use `getEmittedH`
837pub const getOutputHSource = getEmittedH;
838
839741/// Returns the path to the generated header file.
840742/// This function can only be called for libraries or objects.
841743pub fn getEmittedH(self: *Compile) LazyPath {
......@@ -843,9 +745,6 @@ pub fn getEmittedH(self: *Compile) LazyPath {
843745 return self.getEmittedFileGeneric(&self.generated_h);
844746}
845747
846/// deprecated: use `getEmittedPdb`.
847pub const getOutputPdbSource = getEmittedPdb;
848
849748/// Returns the generated PDB file.
850749/// If the compilation does not produce a PDB file, this causes a FileNotFound error
851750/// at build time.
......@@ -882,56 +781,44 @@ pub fn addObjectFile(self: *Compile, source: LazyPath) void {
882781 self.root_module.addObjectFile(source);
883782}
884783
885pub fn addObject(self: *Compile, obj: *Compile) void {
886 assert(obj.kind == .obj);
887 self.linkLibraryOrObject(obj);
784pub fn addObject(self: *Compile, object: *Compile) void {
785 self.root_module.addObject(object);
888786}
889787
890pub fn addAfterIncludePath(self: *Compile, path: LazyPath) void {
891 const b = self.step.owner;
892 self.include_dirs.append(.{ .path_after = path.dupe(b) }) catch @panic("OOM");
893 path.addStepDependencies(&self.step);
788pub fn linkLibrary(self: *Compile, library: *Compile) void {
789 self.root_module.linkLibrary(library);
894790}
895791
896pub fn addSystemIncludePath(self: *Compile, path: LazyPath) void {
897 const b = self.step.owner;
898 self.include_dirs.append(.{ .path_system = path.dupe(b) }) catch @panic("OOM");
899 path.addStepDependencies(&self.step);
792pub fn addAfterIncludePath(self: *Compile, lazy_path: LazyPath) void {
793 self.root_module.addAfterIncludePath(lazy_path);
900794}
901795
902pub fn addIncludePath(self: *Compile, path: LazyPath) void {
903 const b = self.step.owner;
904 self.include_dirs.append(.{ .path = path.dupe(b) }) catch @panic("OOM");
905 path.addStepDependencies(&self.step);
796pub fn addSystemIncludePath(self: *Compile, lazy_path: LazyPath) void {
797 self.root_module.addSystemIncludePath(lazy_path);
798}
799
800pub fn addIncludePath(self: *Compile, lazy_path: LazyPath) void {
801 self.root_module.addIncludePath(lazy_path);
906802}
907803
908804pub fn addConfigHeader(self: *Compile, config_header: *Step.ConfigHeader) void {
909 self.step.dependOn(&config_header.step);
910 self.include_dirs.append(.{ .config_header_step = config_header }) catch @panic("OOM");
805 self.root_module.addConfigHeader(config_header);
911806}
912807
913pub fn addLibraryPath(self: *Compile, directory_source: LazyPath) void {
914 const b = self.step.owner;
915 self.lib_paths.append(directory_source.dupe(b)) catch @panic("OOM");
916 directory_source.addStepDependencies(&self.step);
808pub fn addLibraryPath(self: *Compile, directory_path: LazyPath) void {
809 self.root_module.addLibraryPath(directory_path);
917810}
918811
919pub fn addRPath(self: *Compile, directory_source: LazyPath) void {
920 const b = self.step.owner;
921 self.rpaths.append(directory_source.dupe(b)) catch @panic("OOM");
922 directory_source.addStepDependencies(&self.step);
812pub fn addRPath(self: *Compile, directory_path: LazyPath) void {
813 self.root_module.addRPath(directory_path);
923814}
924815
925pub fn addSystemFrameworkPath(self: *Compile, directory_source: LazyPath) void {
926 const b = self.step.owner;
927 self.include_dirs.append(.{ .framework_path_system = directory_source.dupe(b) }) catch @panic("OOM");
928 directory_source.addStepDependencies(&self.step);
816pub fn addSystemFrameworkPath(self: *Compile, directory_path: LazyPath) void {
817 self.root_module.addSystemFrameworkPath(directory_path);
929818}
930819
931pub fn addFrameworkPath(self: *Compile, directory_source: LazyPath) void {
932 const b = self.step.owner;
933 self.include_dirs.append(.{ .framework_path = directory_source.dupe(b) }) catch @panic("OOM");
934 directory_source.addStepDependencies(&self.step);
820pub fn addFrameworkPath(self: *Compile, directory_path: LazyPath) void {
821 self.root_module.addFrameworkPath(directory_path);
935822}
936823
937824pub fn setExecCmd(self: *Compile, args: []const ?[]const u8) void {
......@@ -944,20 +831,6 @@ pub fn setExecCmd(self: *Compile, args: []const ?[]const u8) void {
944831 self.exec_cmd_args = duped_args;
945832}
946833
947fn linkLibraryOrObject(self: *Compile, other: *Compile) void {
948 other.getEmittedBin().addStepDependencies(&self.step);
949 if (other.target.isWindows() and other.isDynamicLibrary()) {
950 other.getEmittedImplib().addStepDependencies(&self.step);
951 }
952
953 self.link_objects.append(.{ .other_step = other }) catch @panic("OOM");
954 self.include_dirs.append(.{ .other_step = other }) catch @panic("OOM");
955
956 for (other.installed_headers.items) |install_step| {
957 self.step.dependOn(install_step);
958 }
959}
960
961834fn appendModuleArgs(cs: *Compile, zig_args: *ArrayList([]const u8)) !void {
962835 const b = cs.step.owner;
963836 // First, traverse the whole dependency graph and give every module a
......@@ -1014,7 +887,7 @@ fn appendModuleArgs(cs: *Compile, zig_args: *ArrayList([]const u8)) !void {
1014887fn constructDepString(
1015888 allocator: std.mem.Allocator,
1016889 mod_names: std.AutoArrayHashMapUnmanaged(*Module, []const u8),
1017 deps: std.StringArrayHashMap(*Module),
890 deps: std.StringArrayHashMapUnmanaged(*Module),
1018891) ![]const u8 {
1019892 var deps_str = std.ArrayList(u8).init(allocator);
1020893 var it = deps.iterator();
......@@ -1082,7 +955,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
1082955 try addFlag(&zig_args, "llvm", self.use_llvm);
1083956 try addFlag(&zig_args, "lld", self.use_lld);
1084957
1085 if (self.root_module.target.ofmt) |ofmt| {
958 if (self.root_module.target.?.query.ofmt) |ofmt| {
1086959 try zig_args.append(try std.fmt.allocPrint(b.allocator, "-ofmt={s}", .{@tagName(ofmt)}));
1087960 }
1088961
......@@ -1110,7 +983,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
1110983
1111984 {
1112985 var seen_system_libs: std.StringHashMapUnmanaged(void) = .{};
1113 var frameworks: std.StringArrayHashMapUnmanaged(Module.FrameworkLinkInfo) = .{};
986 var frameworks: std.StringArrayHashMapUnmanaged(Module.LinkFrameworkOptions) = .{};
1114987
1115988 var prev_has_cflags = false;
1116989 var prev_has_rcflags = false;
......@@ -1240,7 +1113,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
12401113 try zig_args.append(full_path_lib);
12411114
12421115 if (other.linkage == Linkage.dynamic and
1243 self.root_module.target_info.target.os.tag != .windows)
1116 self.rootModuleTarget().os.tag != .windows)
12441117 {
12451118 if (fs.path.dirname(full_path_lib)) |dirname| {
12461119 try zig_args.append("-rpath");
......@@ -1471,11 +1344,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
14711344 try zig_args.append(b.fmt("{}", .{version}));
14721345 }
14731346
1474 if (self.root_module.target_info.target.isDarwin()) {
1347 if (self.rootModuleTarget().isDarwin()) {
14751348 const install_name = self.install_name orelse b.fmt("@rpath/{s}{s}{s}", .{
1476 self.root_module.target_info.target.libPrefix(),
1349 self.rootModuleTarget().libPrefix(),
14771350 self.name,
1478 self.root_module.target_info.target.dynamicLibSuffix(),
1351 self.rootModuleTarget().dynamicLibSuffix(),
14791352 });
14801353 try zig_args.append("-install_name");
14811354 try zig_args.append(install_name);
......@@ -1763,7 +1636,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
17631636 }
17641637
17651638 if (self.kind == .lib and self.linkage != null and self.linkage.? == .dynamic and
1766 self.version != null and self.root_module.target.wantSharedLibSymLinks())
1639 self.version != null and std.Build.wantSharedLibSymLinks(self.rootModuleTarget()))
17671640 {
17681641 try doAtomicSymLinks(
17691642 step,
......@@ -1932,3 +1805,8 @@ fn matchCompileError(actual: []const u8, expected: []const u8) bool {
19321805 }
19331806 return false;
19341807}
1808
1809pub fn rootModuleTarget(c: *Compile) std.Target {
1810 // The root module is always given a target, so we know this to be non-null.
1811 return c.root_module.target.?.target;
1812}
lib/std/Build/Step/ConfigHeader.zig-6
......@@ -15,9 +15,6 @@ pub const Style = union(enum) {
1515 /// Start with nothing, like blank, and output a nasm .asm file.
1616 nasm,
1717
18 /// deprecated: use `getPath`
19 pub const getFileSource = getPath;
20
2118 pub fn getPath(style: Style) ?std.Build.LazyPath {
2219 switch (style) {
2320 .autoconf, .cmake => |s| return s,
......@@ -107,9 +104,6 @@ pub fn addValues(self: *ConfigHeader, values: anytype) void {
107104 return addValuesInner(self, values) catch @panic("OOM");
108105}
109106
110/// deprecated: use `getOutput`
111pub const getFileSource = getOutput;
112
113107pub fn getOutput(self: *ConfigHeader) std.Build.LazyPath {
114108 return .{ .generated = &self.output_file };
115109}
lib/std/Build/Step/InstallArtifact.zig+1-1
......@@ -94,7 +94,7 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile, options: Options) *Ins
9494 .dylib_symlinks = if (options.dylib_symlinks orelse (dest_dir != null and
9595 artifact.isDynamicLibrary() and
9696 artifact.version != null and
97 artifact.target.wantSharedLibSymLinks())) .{
97 std.Build.wantSharedLibSymLinks(artifact.rootModuleTarget()))) .{
9898 .major_only_filename = artifact.major_only_filename.?,
9999 .name_only_filename = artifact.name_only_filename.?,
100100 } else null,
lib/std/Build/Step/Options.zig+1-5
......@@ -165,9 +165,6 @@ fn printLiteral(out: anytype, val: anytype, indent: u8) !void {
165165 }
166166}
167167
168/// deprecated: use `addOptionPath`
169pub const addOptionFileSource = addOptionPath;
170
171168/// The value is the path in the cache dir.
172169/// Adds a dependency automatically.
173170pub fn addOptionPath(
......@@ -189,8 +186,7 @@ pub fn addOptionArtifact(self: *Options, name: []const u8, artifact: *Step.Compi
189186
190187pub fn createModule(self: *Options) *std.Build.Module {
191188 return self.step.owner.createModule(.{
192 .source_file = self.getOutput(),
193 .dependencies = &.{},
189 .root_source_file = self.getOutput(),
194190 });
195191}
196192
lib/std/Build/Step/Run.zig+11-16
......@@ -197,16 +197,10 @@ pub fn addPrefixedOutputFileArg(
197197 return .{ .generated = &output.generated_file };
198198}
199199
200/// deprecated: use `addFileArg`
201pub const addFileSourceArg = addFileArg;
202
203200pub fn addFileArg(self: *Run, lp: std.Build.LazyPath) void {
204201 self.addPrefixedFileArg("", lp);
205202}
206203
207// deprecated: use `addPrefixedFileArg`
208pub const addPrefixedFileSourceArg = addPrefixedFileArg;
209
210204pub fn addPrefixedFileArg(self: *Run, prefix: []const u8, lp: std.Build.LazyPath) void {
211205 const b = self.step.owner;
212206
......@@ -488,7 +482,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
488482 man.hash.addBytes(file_path);
489483 },
490484 .artifact => |artifact| {
491 if (artifact.root_module.target_info.target.os.tag == .windows) {
485 if (artifact.rootModuleTarget().os.tag == .windows) {
492486 // On Windows we don't have rpaths so we have to add .dll search paths to PATH
493487 self.addPathForDynLibs(artifact);
494488 }
......@@ -682,9 +676,10 @@ fn runCommand(
682676 else => break :interpret,
683677 }
684678
685 const need_cross_glibc = exe.root_module.target_info.target.isGnuLibC() and
679 const need_cross_glibc = exe.rootModuleTarget().isGnuLibC() and
686680 exe.is_linking_libc;
687 switch (b.host.getExternalExecutor(&exe.root_module.target_info, .{
681 const other_target_info = exe.root_module.target.?.toNativeTargetInfo();
682 switch (b.host.toNativeTargetInfo().getExternalExecutor(&other_target_info, .{
688683 .qemu_fixes_dl = need_cross_glibc and b.glibc_runtimes_dir != null,
689684 .link_libc = exe.is_linking_libc,
690685 })) {
......@@ -715,9 +710,9 @@ fn runCommand(
715710 // needs the directory to be called "i686" rather than
716711 // "x86" which is why we do it manually here.
717712 const fmt_str = "{s}" ++ fs.path.sep_str ++ "{s}-{s}-{s}";
718 const cpu_arch = exe.root_module.target_info.target.cpu.arch;
719 const os_tag = exe.root_module.target_info.target.os.tag;
720 const abi = exe.root_module.target_info.target.abi;
713 const cpu_arch = exe.rootModuleTarget().cpu.arch;
714 const os_tag = exe.rootModuleTarget().os.tag;
715 const abi = exe.rootModuleTarget().abi;
721716 const cpu_arch_name: []const u8 = if (cpu_arch == .x86)
722717 "i686"
723718 else
......@@ -770,7 +765,7 @@ fn runCommand(
770765 if (allow_skip) return error.MakeSkipped;
771766
772767 const host_name = try b.host.target.zigTriple(b.allocator);
773 const foreign_name = try exe.root_module.target_info.target.zigTriple(b.allocator);
768 const foreign_name = try exe.rootModuleTarget().zigTriple(b.allocator);
774769
775770 return step.fail("the host system ({s}) is unable to execute binaries from the target ({s})", .{
776771 host_name, foreign_name,
......@@ -778,7 +773,7 @@ fn runCommand(
778773 },
779774 }
780775
781 if (exe.root_module.target_info.target.os.tag == .windows) {
776 if (exe.rootModuleTarget().os.tag == .windows) {
782777 // On Windows we don't have rpaths so we have to add .dll search paths to PATH
783778 self.addPathForDynLibs(exe);
784779 }
......@@ -1300,7 +1295,7 @@ fn addPathForDynLibs(self: *Run, artifact: *Step.Compile) void {
13001295 while (it.next()) |item| {
13011296 const other = item.compile.?;
13021297 if (item.module == &other.root_module) {
1303 if (item.module.target_info.target.os.tag == .windows and other.isDynamicLibrary()) {
1298 if (item.module.target.?.target.os.tag == .windows and other.isDynamicLibrary()) {
13041299 addPathDir(self, fs.path.dirname(other.getEmittedBin().getPath(b)).?);
13051300 addPathForDynLibs(self, other);
13061301 }
......@@ -1321,7 +1316,7 @@ fn failForeign(
13211316
13221317 const b = self.step.owner;
13231318 const host_name = try b.host.target.zigTriple(b.allocator);
1324 const foreign_name = try exe.root_module.target_info.target.zigTriple(b.allocator);
1319 const foreign_name = try exe.rootModuleTarget().zigTriple(b.allocator);
13251320
13261321 return self.step.fail(
13271322 \\unable to spawn foreign binary '{s}' ({s}) on host system ({s})
lib/std/Build/Step/TranslateC.zig+5-6
......@@ -2,7 +2,6 @@ const std = @import("std");
22const Step = std.Build.Step;
33const fs = std.fs;
44const mem = std.mem;
5const CrossTarget = std.zig.CrossTarget;
65
76const TranslateC = @This();
87
......@@ -13,7 +12,7 @@ source: std.Build.LazyPath,
1312include_dirs: std.ArrayList([]const u8),
1413c_macros: std.ArrayList([]const u8),
1514out_basename: []const u8,
16target: CrossTarget,
15target: std.Build.ResolvedTarget,
1716optimize: std.builtin.OptimizeMode,
1817output_file: std.Build.GeneratedFile,
1918link_libc: bool,
......@@ -21,7 +20,7 @@ use_clang: bool,
2120
2221pub const Options = struct {
2322 source_file: std.Build.LazyPath,
24 target: CrossTarget,
23 target: std.Build.ResolvedTarget,
2524 optimize: std.builtin.OptimizeMode,
2625 link_libc: bool = true,
2726 use_clang: bool = true,
......@@ -54,7 +53,7 @@ pub fn create(owner: *std.Build, options: Options) *TranslateC {
5453pub const AddExecutableOptions = struct {
5554 name: ?[]const u8 = null,
5655 version: ?std.SemanticVersion = null,
57 target: ?CrossTarget = null,
56 target: ?std.Build.ResolvedTarget = null,
5857 optimize: ?std.builtin.OptimizeMode = null,
5958 linkage: ?Step.Compile.Linkage = null,
6059};
......@@ -139,9 +138,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
139138
140139 try argv_list.append("--listen=-");
141140
142 if (!self.target.isNative()) {
141 if (!self.target.query.isNative()) {
143142 try argv_list.append("-target");
144 try argv_list.append(try self.target.zigTriple(b.allocator));
143 try argv_list.append(try self.target.query.zigTriple(b.allocator));
145144 }
146145
147146 switch (self.optimize) {
lib/std/Build/Step/WriteFile.zig-7
......@@ -28,9 +28,6 @@ pub const File = struct {
2828 sub_path: []const u8,
2929 contents: Contents,
3030
31 /// deprecated: use `getPath`
32 pub const getFileSource = getPath;
33
3431 pub fn getPath(self: *File) std.Build.LazyPath {
3532 return .{ .generated = &self.generated_file };
3633 }
......@@ -126,10 +123,6 @@ pub fn addBytesToSource(wf: *WriteFile, bytes: []const u8, sub_path: []const u8)
126123 }) catch @panic("OOM");
127124}
128125
129pub const getFileSource = @compileError("Deprecated; use the return value from add()/addCopyFile(), or use files[i].getPath()");
130
131pub const getDirectorySource = getDirectory;
132
133126/// Returns a `LazyPath` representing the base directory that contains all the
134127/// files from this `WriteFile`.
135128pub fn getDirectory(wf: *WriteFile) std.Build.LazyPath {
lib/std/std.zig-3
......@@ -194,9 +194,6 @@ pub const zig = @import("zig.zig");
194194
195195pub const start = @import("start.zig");
196196
197/// deprecated: use `Build`.
198pub const build = Build;
199
200197const root = @import("root");
201198const options_override = if (@hasDecl(root, "std_options")) root.std_options else struct {};
202199
lib/std/zig.zig+77
......@@ -203,6 +203,83 @@ pub fn binNameAlloc(allocator: std.mem.Allocator, options: BinNameOptions) error
203203 }
204204}
205205
206pub const BuildId = union(enum) {
207 none,
208 fast,
209 uuid,
210 sha1,
211 md5,
212 hexstring: HexString,
213
214 pub fn eql(a: BuildId, b: BuildId) bool {
215 const Tag = @TypeOf(BuildId).Union.tag_type.?;
216 const a_tag: Tag = a;
217 const b_tag: Tag = b;
218 if (a_tag != b_tag) return false;
219 return switch (a) {
220 .none, .fast, .uuid, .sha1, .md5 => true,
221 .hexstring => |a_hexstring| std.mem.eql(u8, a_hexstring.toSlice(), b.hexstring.toSlice()),
222 };
223 }
224
225 pub const HexString = struct {
226 bytes: [32]u8,
227 len: u8,
228
229 /// Result is byte values, *not* hex-encoded.
230 pub fn toSlice(hs: *const HexString) []const u8 {
231 return hs.bytes[0..hs.len];
232 }
233 };
234
235 /// Input is byte values, *not* hex-encoded.
236 /// Asserts `bytes` fits inside `HexString`
237 pub fn initHexString(bytes: []const u8) BuildId {
238 var result: BuildId = .{ .hexstring = .{
239 .bytes = undefined,
240 .len = @intCast(bytes.len),
241 } };
242 @memcpy(result.hexstring.bytes[0..bytes.len], bytes);
243 return result;
244 }
245
246 /// Converts UTF-8 text to a `BuildId`.
247 pub fn parse(text: []const u8) !BuildId {
248 if (std.mem.eql(u8, text, "none")) {
249 return .none;
250 } else if (std.mem.eql(u8, text, "fast")) {
251 return .fast;
252 } else if (std.mem.eql(u8, text, "uuid")) {
253 return .uuid;
254 } else if (std.mem.eql(u8, text, "sha1") or std.mem.eql(u8, text, "tree")) {
255 return .sha1;
256 } else if (std.mem.eql(u8, text, "md5")) {
257 return .md5;
258 } else if (std.mem.startsWith(u8, text, "0x")) {
259 var result: BuildId = .{ .hexstring = undefined };
260 const slice = try std.fmt.hexToBytes(&result.hexstring.bytes, text[2..]);
261 result.hexstring.len = @as(u8, @intCast(slice.len));
262 return result;
263 }
264 return error.InvalidBuildIdStyle;
265 }
266
267 test parse {
268 try std.testing.expectEqual(BuildId.md5, try parse("md5"));
269 try std.testing.expectEqual(BuildId.none, try parse("none"));
270 try std.testing.expectEqual(BuildId.fast, try parse("fast"));
271 try std.testing.expectEqual(BuildId.uuid, try parse("uuid"));
272 try std.testing.expectEqual(BuildId.sha1, try parse("sha1"));
273 try std.testing.expectEqual(BuildId.sha1, try parse("tree"));
274
275 try std.testing.expect(BuildId.initHexString("").eql(try parse("0x")));
276 try std.testing.expect(BuildId.initHexString("\x12\x34\x56").eql(try parse("0x123456")));
277 try std.testing.expectError(error.InvalidLength, parse("0x12-34"));
278 try std.testing.expectError(error.InvalidCharacter, parse("0xfoobbb"));
279 try std.testing.expectError(error.InvalidBuildIdStyle, parse("yaddaxxx"));
280 }
281};
282
206283test {
207284 @import("std").testing.refAllDecls(@This());
208285}
lib/std/zig/CrossTarget.zig-4
......@@ -632,10 +632,6 @@ pub fn linuxTriple(self: CrossTarget, allocator: mem.Allocator) ![]u8 {
632632 return Target.linuxTripleSimple(allocator, self.getCpuArch(), self.getOsTag(), self.getAbi());
633633}
634634
635pub fn wantSharedLibSymLinks(self: CrossTarget) bool {
636 return self.getOsTag() != .windows;
637}
638
639635pub fn isGnuLibC(self: CrossTarget) bool {
640636 return Target.isGnuLibC_os_tag_abi(self.getOsTag(), self.getAbi());
641637}
lib/std/zig/system/NativeTargetInfo.zig+2-2
......@@ -1008,8 +1008,8 @@ pub const GetExternalExecutorOptions = struct {
10081008 link_libc: bool = false,
10091009};
10101010
1011/// Return whether or not the given host target is capable of executing natively executables
1012/// of the other target.
1011/// Return whether or not the given host is capable of running executables of
1012/// the other target.
10131013pub fn getExternalExecutor(
10141014 host: NativeTargetInfo,
10151015 candidate: *const NativeTargetInfo,
src/Compilation.zig+1-2
......@@ -30,7 +30,6 @@ const fatal = @import("main.zig").fatal;
3030const clangMain = @import("main.zig").clangMain;
3131const Module = @import("Module.zig");
3232const InternPool = @import("InternPool.zig");
33const BuildId = std.Build.CompileStep.BuildId;
3433const Cache = std.Build.Cache;
3534const c_codegen = @import("codegen/c.zig");
3635const libtsan = @import("libtsan.zig");
......@@ -910,7 +909,7 @@ pub const InitOptions = struct {
910909 linker_print_map: bool = false,
911910 linker_opt_bisect_limit: i32 = -1,
912911 each_lib_rpath: ?bool = null,
913 build_id: ?BuildId = null,
912 build_id: ?std.zig.BuildId = null,
914913 disable_c_depfile: bool = false,
915914 linker_z_nodelete: bool = false,
916915 linker_z_notext: bool = false,
src/link.zig+1-2
......@@ -10,7 +10,6 @@ const wasi_libc = @import("wasi_libc.zig");
1010
1111const Air = @import("Air.zig");
1212const Allocator = std.mem.Allocator;
13const BuildId = std.Build.CompileStep.BuildId;
1413const Cache = std.Build.Cache;
1514const Compilation = @import("Compilation.zig");
1615const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
......@@ -185,7 +184,7 @@ pub const Options = struct {
185184 error_return_tracing: bool,
186185 skip_linker_dependencies: bool,
187186 each_lib_rpath: bool,
188 build_id: BuildId,
187 build_id: std.zig.BuildId,
189188 disable_lld_caching: bool,
190189 is_test: bool,
191190 hash_style: HashStyle,
src/main.zig+3-4
......@@ -21,7 +21,6 @@ const introspect = @import("introspect.zig");
2121const EnvVar = introspect.EnvVar;
2222const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
2323const wasi_libc = @import("wasi_libc.zig");
24const BuildId = std.Build.CompileStep.BuildId;
2524const Cache = std.Build.Cache;
2625const target_util = @import("target.zig");
2726const crash_report = @import("crash_report.zig");
......@@ -882,7 +881,7 @@ fn buildOutputType(
882881 var link_eh_frame_hdr = false;
883882 var link_emit_relocs = false;
884883 var each_lib_rpath: ?bool = null;
885 var build_id: ?BuildId = null;
884 var build_id: ?std.zig.BuildId = null;
886885 var sysroot: ?[]const u8 = null;
887886 var libc_paths_file: ?[]const u8 = try EnvVar.ZIG_LIBC.get(arena);
888887 var machine_code_model: std.builtin.CodeModel = .default;
......@@ -1551,7 +1550,7 @@ fn buildOutputType(
15511550 build_id = .fast;
15521551 } else if (mem.startsWith(u8, arg, "--build-id=")) {
15531552 const style = arg["--build-id=".len..];
1554 build_id = BuildId.parse(style) catch |err| {
1553 build_id = std.zig.BuildId.parse(style) catch |err| {
15551554 fatal("unable to parse --build-id style '{s}': {s}", .{
15561555 style, @errorName(err),
15571556 });
......@@ -1847,7 +1846,7 @@ fn buildOutputType(
18471846 const key = linker_arg[0..equals_pos];
18481847 const value = linker_arg[equals_pos + 1 ..];
18491848 if (mem.eql(u8, key, "--build-id")) {
1850 build_id = BuildId.parse(value) catch |err| {
1849 build_id = std.zig.BuildId.parse(value) catch |err| {
18511850 fatal("unable to parse --build-id style '{s}': {s}", .{
18521851 value, @errorName(err),
18531852 });
test/behavior/eval.zig+10-10
......@@ -876,27 +876,27 @@ test "two comptime calls with array default initialized to undefined" {
876876 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
877877
878878 const S = struct {
879 const CrossTarget = struct {
880 dynamic_linker: DynamicLinker = DynamicLinker{},
879 const A = struct {
880 c: B = B{},
881881
882 pub fn parse() void {
883 var result: CrossTarget = .{};
884 result.getCpuArch();
882 pub fn d() void {
883 var f: A = .{};
884 f.e();
885885 }
886886
887 pub fn getCpuArch(self: CrossTarget) void {
888 _ = self;
887 pub fn e(g: A) void {
888 _ = g;
889889 }
890890 };
891891
892 const DynamicLinker = struct {
892 const B = struct {
893893 buffer: [255]u8 = undefined,
894894 };
895895 };
896896
897897 comptime {
898 S.CrossTarget.parse();
899 S.CrossTarget.parse();
898 S.A.d();
899 S.A.d();
900900 }
901901}
902902
test/cases.zig+5-5
......@@ -9,9 +9,9 @@ pub const BuildOptions = struct {
99 llvm_has_xtensa: bool,
1010};
1111
12pub fn addCases(cases: *Cases, build_options: BuildOptions) !void {
13 try @import("compile_errors.zig").addCases(cases);
14 try @import("cbe.zig").addCases(cases);
15 try @import("llvm_targets.zig").addCases(cases, build_options);
16 try @import("nvptx.zig").addCases(cases);
12pub fn addCases(cases: *Cases, build_options: BuildOptions, b: *std.Build) !void {
13 try @import("compile_errors.zig").addCases(cases, b);
14 try @import("cbe.zig").addCases(cases, b);
15 try @import("llvm_targets.zig").addCases(cases, build_options, b);
16 try @import("nvptx.zig").addCases(cases, b);
1717}
test/cbe.zig+24-24
......@@ -2,16 +2,16 @@ const std = @import("std");
22const Cases = @import("src/Cases.zig");
33const nl = if (@import("builtin").os.tag == .windows) "\r\n" else "\n";
44
5// These tests should work with all platforms, but we're using linux_x64 for
6// now for consistency. Will be expanded eventually.
7const linux_x64 = std.zig.CrossTarget{
8 .cpu_arch = .x86_64,
9 .os_tag = .linux,
10};
11
12pub fn addCases(ctx: *Cases) !void {
5pub fn addCases(ctx: *Cases, b: *std.Build) !void {
6 // These tests should work with all platforms, but we're using linux_x64 for
7 // now for consistency. Will be expanded eventually.
8 const linux_x64: std.zig.CrossTarget = .{
9 .cpu_arch = .x86_64,
10 .os_tag = .linux,
11 };
12
1313 {
14 var case = ctx.exeFromCompiledC("hello world with updates", .{});
14 var case = ctx.exeFromCompiledC("hello world with updates", .{}, b);
1515
1616 // Regular old hello world
1717 case.addCompareOutput(
......@@ -59,7 +59,7 @@ pub fn addCases(ctx: *Cases) !void {
5959 }
6060
6161 {
62 var case = ctx.exeFromCompiledC("var args", .{});
62 var case = ctx.exeFromCompiledC("var args", .{}, b);
6363
6464 case.addCompareOutput(
6565 \\extern fn printf(format: [*:0]const u8, ...) c_int;
......@@ -72,7 +72,7 @@ pub fn addCases(ctx: *Cases) !void {
7272 }
7373
7474 {
75 var case = ctx.exeFromCompiledC("errorFromInt", .{});
75 var case = ctx.exeFromCompiledC("errorFromInt", .{}, b);
7676
7777 case.addCompareOutput(
7878 \\pub export fn main() c_int {
......@@ -108,7 +108,7 @@ pub fn addCases(ctx: *Cases) !void {
108108 }
109109
110110 {
111 var case = ctx.exeFromCompiledC("x86_64-linux inline assembly", linux_x64);
111 var case = ctx.exeFromCompiledC("x86_64-linux inline assembly", linux_x64, b);
112112
113113 // Exit with 0
114114 case.addCompareOutput(
......@@ -202,7 +202,7 @@ pub fn addCases(ctx: *Cases) !void {
202202 }
203203
204204 {
205 var case = ctx.exeFromCompiledC("alloc and retptr", .{});
205 var case = ctx.exeFromCompiledC("alloc and retptr", .{}, b);
206206
207207 case.addCompareOutput(
208208 \\fn add(a: i32, b: i32) i32 {
......@@ -220,7 +220,7 @@ pub fn addCases(ctx: *Cases) !void {
220220 }
221221
222222 {
223 var case = ctx.exeFromCompiledC("inferred local const and var", .{});
223 var case = ctx.exeFromCompiledC("inferred local const and var", .{}, b);
224224
225225 case.addCompareOutput(
226226 \\fn add(a: i32, b: i32) i32 {
......@@ -236,7 +236,7 @@ pub fn addCases(ctx: *Cases) !void {
236236 , "");
237237 }
238238 {
239 var case = ctx.exeFromCompiledC("control flow", .{});
239 var case = ctx.exeFromCompiledC("control flow", .{}, b);
240240
241241 // Simple while loop
242242 case.addCompareOutput(
......@@ -423,7 +423,7 @@ pub fn addCases(ctx: *Cases) !void {
423423 });
424424 }
425425 //{
426 // var case = ctx.exeFromCompiledC("optionals", .{});
426 // var case = ctx.exeFromCompiledC("optionals", .{}, b);
427427
428428 // // Simple while loop
429429 // case.addCompareOutput(
......@@ -451,7 +451,7 @@ pub fn addCases(ctx: *Cases) !void {
451451 //}
452452
453453 {
454 var case = ctx.exeFromCompiledC("errors", .{});
454 var case = ctx.exeFromCompiledC("errors", .{}, b);
455455 case.addCompareOutput(
456456 \\pub export fn main() c_int {
457457 \\ var e1 = error.Foo;
......@@ -495,7 +495,7 @@ pub fn addCases(ctx: *Cases) !void {
495495 }
496496
497497 {
498 var case = ctx.exeFromCompiledC("structs", .{});
498 var case = ctx.exeFromCompiledC("structs", .{}, b);
499499 case.addError(
500500 \\const Point = struct { x: i32, y: i32 };
501501 \\pub export fn main() c_int {
......@@ -562,7 +562,7 @@ pub fn addCases(ctx: *Cases) !void {
562562 }
563563
564564 {
565 var case = ctx.exeFromCompiledC("unions", .{});
565 var case = ctx.exeFromCompiledC("unions", .{}, b);
566566
567567 case.addError(
568568 \\const U = union {
......@@ -596,7 +596,7 @@ pub fn addCases(ctx: *Cases) !void {
596596 }
597597
598598 {
599 var case = ctx.exeFromCompiledC("enums", .{});
599 var case = ctx.exeFromCompiledC("enums", .{}, b);
600600
601601 case.addError(
602602 \\const E1 = packed enum { a, b, c };
......@@ -838,7 +838,7 @@ pub fn addCases(ctx: *Cases) !void {
838838 }
839839
840840 {
841 var case = ctx.exeFromCompiledC("shift right and left", .{});
841 var case = ctx.exeFromCompiledC("shift right and left", .{}, b);
842842 case.addCompareOutput(
843843 \\pub export fn main() c_int {
844844 \\ var i: u32 = 16;
......@@ -863,7 +863,7 @@ pub fn addCases(ctx: *Cases) !void {
863863 }
864864
865865 {
866 var case = ctx.exeFromCompiledC("inferred error sets", .{});
866 var case = ctx.exeFromCompiledC("inferred error sets", .{}, b);
867867
868868 case.addCompareOutput(
869869 \\pub export fn main() c_int {
......@@ -884,7 +884,7 @@ pub fn addCases(ctx: *Cases) !void {
884884
885885 {
886886 // TODO: add u64 tests, ran into issues with the literal generated for std.math.maxInt(u64)
887 var case = ctx.exeFromCompiledC("add and sub wrapping operations", .{});
887 var case = ctx.exeFromCompiledC("add and sub wrapping operations", .{}, b);
888888 case.addCompareOutput(
889889 \\pub export fn main() c_int {
890890 \\ // Addition
......@@ -933,7 +933,7 @@ pub fn addCases(ctx: *Cases) !void {
933933 }
934934
935935 {
936 var case = ctx.exeFromCompiledC("rem", linux_x64);
936 var case = ctx.exeFromCompiledC("rem", linux_x64, b);
937937 case.addCompareOutput(
938938 \\fn assert(ok: bool) void {
939939 \\ if (!ok) unreachable;
test/compile_errors.zig+11-11
......@@ -2,9 +2,9 @@ const std = @import("std");
22const builtin = @import("builtin");
33const Cases = @import("src/Cases.zig");
44
5pub fn addCases(ctx: *Cases) !void {
5pub fn addCases(ctx: *Cases, b: *std.Build) !void {
66 {
7 const case = ctx.obj("multiline error messages", .{});
7 const case = ctx.obj("multiline error messages", b.host);
88
99 case.addError(
1010 \\comptime {
......@@ -39,7 +39,7 @@ pub fn addCases(ctx: *Cases) !void {
3939 }
4040
4141 {
42 const case = ctx.obj("isolated carriage return in multiline string literal", .{});
42 const case = ctx.obj("isolated carriage return in multiline string literal", b.host);
4343
4444 case.addError("const foo = \\\\\test\r\r rogue carriage return\n;", &[_][]const u8{
4545 ":1:19: error: expected ';' after declaration",
......@@ -48,7 +48,7 @@ pub fn addCases(ctx: *Cases) !void {
4848 }
4949
5050 {
51 const case = ctx.obj("missing semicolon at EOF", .{});
51 const case = ctx.obj("missing semicolon at EOF", b.host);
5252 case.addError(
5353 \\const foo = 1
5454 , &[_][]const u8{
......@@ -57,7 +57,7 @@ pub fn addCases(ctx: *Cases) !void {
5757 }
5858
5959 {
60 const case = ctx.obj("argument causes error", .{});
60 const case = ctx.obj("argument causes error", b.host);
6161
6262 case.addError(
6363 \\pub export fn entry() void {
......@@ -80,7 +80,7 @@ pub fn addCases(ctx: *Cases) !void {
8080 }
8181
8282 {
83 const case = ctx.obj("astgen failure in file struct", .{});
83 const case = ctx.obj("astgen failure in file struct", b.host);
8484
8585 case.addError(
8686 \\pub export fn entry() void {
......@@ -95,7 +95,7 @@ pub fn addCases(ctx: *Cases) !void {
9595 }
9696
9797 {
98 const case = ctx.obj("invalid store to comptime field", .{});
98 const case = ctx.obj("invalid store to comptime field", b.host);
9999
100100 case.addError(
101101 \\const a = @import("a.zig");
......@@ -119,7 +119,7 @@ pub fn addCases(ctx: *Cases) !void {
119119 }
120120
121121 {
122 const case = ctx.obj("file in multiple modules", .{});
122 const case = ctx.obj("file in multiple modules", b.host);
123123 case.addDepModule("foo", "foo.zig");
124124
125125 case.addError(
......@@ -138,7 +138,7 @@ pub fn addCases(ctx: *Cases) !void {
138138 }
139139
140140 {
141 const case = ctx.obj("wrong same named struct", .{});
141 const case = ctx.obj("wrong same named struct", b.host);
142142
143143 case.addError(
144144 \\const a = @import("a.zig");
......@@ -172,7 +172,7 @@ pub fn addCases(ctx: *Cases) !void {
172172 }
173173
174174 {
175 const case = ctx.obj("non-printable invalid character", .{});
175 const case = ctx.obj("non-printable invalid character", b.host);
176176
177177 case.addError("\xff\xfe" ++
178178 \\export fn foo() bool {
......@@ -185,7 +185,7 @@ pub fn addCases(ctx: *Cases) !void {
185185 }
186186
187187 {
188 const case = ctx.obj("imported generic method call with invalid param", .{});
188 const case = ctx.obj("imported generic method call with invalid param", b.host);
189189
190190 case.addError(
191191 \\pub const import = @import("import.zig");
test/link/bss/build.zig+1
......@@ -7,6 +7,7 @@ pub fn build(b: *std.Build) void {
77 const exe = b.addExecutable(.{
88 .name = "bss",
99 .root_source_file = .{ .path = "main.zig" },
10 .target = b.host,
1011 .optimize = .Debug,
1112 });
1213
test/link/common_symbols/build.zig+1-1
......@@ -14,7 +14,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1414 const lib_a = b.addStaticLibrary(.{
1515 .name = "a",
1616 .optimize = optimize,
17 .target = .{},
17 .target = b.host,
1818 });
1919 lib_a.addCSourceFiles(.{
2020 .files = &.{ "c.c", "a.c", "b.c" },
test/link/common_symbols_alignment/build.zig+1-1
......@@ -14,7 +14,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1414 const lib_a = b.addStaticLibrary(.{
1515 .name = "a",
1616 .optimize = optimize,
17 .target = .{},
17 .target = b.host,
1818 });
1919 lib_a.addCSourceFiles(.{
2020 .files = &.{"a.c"},
test/link/elf.zig+656-441
......@@ -5,20 +5,20 @@
55pub fn testAll(b: *Build) *Step {
66 const elf_step = b.step("test-elf", "Run ELF tests");
77
8 const default_target = CrossTarget{
8 const default_target = b.resolveTargetQuery(.{
99 .cpu_arch = .x86_64, // TODO relax this once ELF linker is able to handle other archs
1010 .os_tag = .linux,
11 };
12 const musl_target = CrossTarget{
11 });
12 const musl_target = b.resolveTargetQuery(.{
1313 .cpu_arch = .x86_64,
1414 .os_tag = .linux,
1515 .abi = .musl,
16 };
17 const glibc_target = CrossTarget{
16 });
17 const glibc_target = b.resolveTargetQuery(.{
1818 .cpu_arch = .x86_64,
1919 .os_tag = .linux,
2020 .abi = .gnu,
21 };
21 });
2222
2323 // Exercise linker in -r mode
2424 elf_step.dependOn(testEmitRelocatable(b, .{ .use_llvm = false, .target = musl_target }));
......@@ -132,14 +132,18 @@ pub fn testAll(b: *Build) *Step {
132132fn testAbsSymbols(b: *Build, opts: Options) *Step {
133133 const test_step = addTestStep(b, "abs-symbols", opts);
134134
135 const obj = addObject(b, "obj", opts);
136 addAsmSourceBytes(obj,
135 const obj = addObject(b, opts, .{
136 .name = "obj",
137 .asm_source_bytes =
137138 \\.globl foo
138139 \\foo = 0x800008
139 );
140 \\
141 ,
142 });
140143
141 const exe = addExecutable(b, "test", opts);
142 addCSourceBytes(exe,
144 const exe = addExecutable(b, opts, .{
145 .name = "test",
146 .c_source_bytes =
143147 \\#include <signal.h>
144148 \\#include <stdio.h>
145149 \\#include <stdlib.h>
......@@ -159,7 +163,8 @@ fn testAbsSymbols(b: *Build, opts: Options) *Step {
159163 \\ foo = 5;
160164 \\ return 0;
161165 \\}
162 , &.{});
166 ,
167 });
163168 exe.addObject(obj);
164169 exe.linkLibC();
165170
......@@ -173,31 +178,36 @@ fn testAbsSymbols(b: *Build, opts: Options) *Step {
173178fn testAsNeeded(b: *Build, opts: Options) *Step {
174179 const test_step = addTestStep(b, "as-needed", opts);
175180
176 const main_o = addObject(b, "main", opts);
177 addCSourceBytes(main_o,
181 const main_o = addObject(b, opts, .{
182 .name = "main",
183 .c_source_bytes =
178184 \\#include <stdio.h>
179185 \\int baz();
180186 \\int main() {
181187 \\ printf("%d\n", baz());
182188 \\ return 0;
183189 \\}
184 , &.{});
190 \\
191 ,
192 });
185193 main_o.linkLibC();
186194
187 const libfoo = addSharedLibrary(b, "foo", opts);
195 const libfoo = addSharedLibrary(b, opts, .{ .name = "foo" });
188196 addCSourceBytes(libfoo, "int foo() { return 42; }", &.{});
189197
190 const libbar = addSharedLibrary(b, "bar", opts);
198 const libbar = addSharedLibrary(b, opts, .{ .name = "bar" });
191199 addCSourceBytes(libbar, "int bar() { return 42; }", &.{});
192200
193 const libbaz = addSharedLibrary(b, "baz", opts);
201 const libbaz = addSharedLibrary(b, opts, .{ .name = "baz" });
194202 addCSourceBytes(libbaz,
195203 \\int foo();
196204 \\int baz() { return foo(); }
197205 , &.{});
198206
199207 {
200 const exe = addExecutable(b, "test", opts);
208 const exe = addExecutable(b, opts, .{
209 .name = "test",
210 });
201211 exe.addObject(main_o);
202212 exe.linkSystemLibrary2("foo", .{ .needed = true });
203213 exe.addLibraryPath(libfoo.getEmittedBinDirectory());
......@@ -225,7 +235,9 @@ fn testAsNeeded(b: *Build, opts: Options) *Step {
225235 }
226236
227237 {
228 const exe = addExecutable(b, "test", opts);
238 const exe = addExecutable(b, opts, .{
239 .name = "test",
240 });
229241 exe.addObject(main_o);
230242 exe.linkSystemLibrary2("foo", .{ .needed = false });
231243 exe.addLibraryPath(libfoo.getEmittedBinDirectory());
......@@ -259,7 +271,7 @@ fn testAsNeeded(b: *Build, opts: Options) *Step {
259271fn testCanonicalPlt(b: *Build, opts: Options) *Step {
260272 const test_step = addTestStep(b, "canonical-plt", opts);
261273
262 const dso = addSharedLibrary(b, "a", opts);
274 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
263275 addCSourceBytes(dso,
264276 \\void *foo() {
265277 \\ return foo;
......@@ -269,17 +281,21 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {
269281 \\}
270282 , &.{});
271283
272 const b_o = addObject(b, "obj", opts);
273 addCSourceBytes(b_o,
284 const b_o = addObject(b, opts, .{
285 .name = "obj",
286 .c_source_bytes =
274287 \\void *bar();
275288 \\void *baz() {
276289 \\ return bar;
277290 \\}
278 , &.{});
279 b_o.force_pic = true;
291 \\
292 ,
293 .pic = true,
294 });
280295
281 const main_o = addObject(b, "main", opts);
282 addCSourceBytes(main_o,
296 const main_o = addObject(b, opts, .{
297 .name = "main",
298 .c_source_bytes =
283299 \\#include <assert.h>
284300 \\void *foo();
285301 \\void *bar();
......@@ -290,11 +306,15 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {
290306 \\ assert(bar == baz());
291307 \\ return 0;
292308 \\}
293 , &.{});
309 \\
310 ,
311 .pic = false,
312 });
294313 main_o.linkLibC();
295 main_o.force_pic = false;
296314
297 const exe = addExecutable(b, "main", opts);
315 const exe = addExecutable(b, opts, .{
316 .name = "main",
317 });
298318 exe.addObject(main_o);
299319 exe.addObject(b_o);
300320 exe.linkLibrary(dso);
......@@ -311,7 +331,9 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {
311331fn testCommonSymbols(b: *Build, opts: Options) *Step {
312332 const test_step = addTestStep(b, "common-symbols", opts);
313333
314 const exe = addExecutable(b, "test", opts);
334 const exe = addExecutable(b, opts, .{
335 .name = "test",
336 });
315337 addCSourceBytes(exe,
316338 \\int foo;
317339 \\int bar;
......@@ -338,8 +360,9 @@ fn testCommonSymbols(b: *Build, opts: Options) *Step {
338360fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step {
339361 const test_step = addTestStep(b, "common-symbols-in-archive", opts);
340362
341 const a_o = addObject(b, "a", opts);
342 addCSourceBytes(a_o,
363 const a_o = addObject(b, opts, .{
364 .name = "a",
365 .c_source_bytes =
343366 \\#include <stdio.h>
344367 \\int foo;
345368 \\int bar;
......@@ -348,28 +371,43 @@ fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step {
348371 \\int main() {
349372 \\ printf("%d %d %d %d\n", foo, bar, baz, two ? two() : -1);
350373 \\}
351 , &.{"-fcommon"});
374 \\
375 ,
376 .c_source_flags = &.{"-fcommon"},
377 });
352378 a_o.linkLibC();
353379
354 const b_o = addObject(b, "b", opts);
355 addCSourceBytes(b_o, "int foo = 5;", &.{"-fcommon"});
380 const b_o = addObject(b, opts, .{
381 .name = "b",
382 .c_source_bytes = "int foo = 5;",
383 .c_source_flags = &.{"-fcommon"},
384 });
356385
357386 {
358 const c_o = addObject(b, "c", opts);
359 addCSourceBytes(c_o,
387 const c_o = addObject(b, opts, .{
388 .name = "c",
389 .c_source_bytes =
360390 \\int bar;
361391 \\int two() { return 2; }
362 , &.{"-fcommon"});
363
364 const d_o = addObject(b, "d", opts);
365 addCSourceBytes(d_o, "int baz;", &.{"-fcommon"});
366
367 const lib = addStaticLibrary(b, "lib", opts);
392 \\
393 ,
394 .c_source_flags = &.{"-fcommon"},
395 });
396
397 const d_o = addObject(b, opts, .{
398 .name = "d",
399 .c_source_bytes = "int baz;",
400 .c_source_flags = &.{"-fcommon"},
401 });
402
403 const lib = addStaticLibrary(b, opts, .{ .name = "lib" });
368404 lib.addObject(b_o);
369405 lib.addObject(c_o);
370406 lib.addObject(d_o);
371407
372 const exe = addExecutable(b, "test", opts);
408 const exe = addExecutable(b, opts, .{
409 .name = "test",
410 });
373411 exe.addObject(a_o);
374412 exe.linkLibrary(lib);
375413 exe.linkLibC();
......@@ -380,18 +418,23 @@ fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step {
380418 }
381419
382420 {
383 const e_o = addObject(b, "e", opts);
384 addCSourceBytes(e_o,
421 const e_o = addObject(b, opts, .{
422 .name = "e",
423 .c_source_bytes =
385424 \\int bar = 0;
386425 \\int baz = 7;
387426 \\int two() { return 2; }
388 , &.{"-fcommon"});
427 ,
428 .c_source_flags = &.{"-fcommon"},
429 });
389430
390 const lib = addStaticLibrary(b, "lib", opts);
431 const lib = addStaticLibrary(b, opts, .{ .name = "lib" });
391432 lib.addObject(b_o);
392433 lib.addObject(e_o);
393434
394 const exe = addExecutable(b, "test", opts);
435 const exe = addExecutable(b, opts, .{
436 .name = "test",
437 });
395438 exe.addObject(a_o);
396439 exe.linkLibrary(lib);
397440 exe.linkLibC();
......@@ -407,21 +450,23 @@ fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step {
407450fn testCopyrel(b: *Build, opts: Options) *Step {
408451 const test_step = addTestStep(b, "copyrel", opts);
409452
410 const dso = addSharedLibrary(b, "a", opts);
453 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
411454 addCSourceBytes(dso,
412455 \\int foo = 3;
413456 \\int bar = 5;
414457 , &.{});
415458
416 const exe = addExecutable(b, "main", opts);
417 addCSourceBytes(exe,
459 const exe = addExecutable(b, opts, .{
460 .name = "main",
461 .c_source_bytes =
418462 \\#include<stdio.h>
419463 \\extern int foo, bar;
420464 \\int main() {
421465 \\ printf("%d %d\n", foo, bar);
422466 \\ return 0;
423467 \\}
424 , &.{});
468 ,
469 });
425470 exe.linkLibrary(dso);
426471 exe.linkLibC();
427472 // https://github.com/ziglang/zig/issues/17619
......@@ -437,7 +482,7 @@ fn testCopyrel(b: *Build, opts: Options) *Step {
437482fn testCopyrelAlias(b: *Build, opts: Options) *Step {
438483 const test_step = addTestStep(b, "copyrel-alias", opts);
439484
440 const dso = addSharedLibrary(b, "a", opts);
485 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
441486 addCSourceBytes(dso,
442487 \\int bruh = 31;
443488 \\int foo = 42;
......@@ -445,7 +490,10 @@ fn testCopyrelAlias(b: *Build, opts: Options) *Step {
445490 \\extern int baz __attribute__((alias("foo")));
446491 , &.{});
447492
448 const exe = addExecutable(b, "main", opts);
493 const exe = addExecutable(b, opts, .{
494 .name = "main",
495 .pic = false,
496 });
449497 addCSourceBytes(exe,
450498 \\#include<stdio.h>
451499 \\extern int foo;
......@@ -461,7 +509,6 @@ fn testCopyrelAlias(b: *Build, opts: Options) *Step {
461509 , &.{});
462510 exe.linkLibrary(dso);
463511 exe.linkLibC();
464 exe.force_pic = false;
465512 exe.pie = false;
466513
467514 const run = addRunArtifact(exe);
......@@ -474,28 +521,31 @@ fn testCopyrelAlias(b: *Build, opts: Options) *Step {
474521fn testCopyrelAlignment(b: *Build, opts: Options) *Step {
475522 const test_step = addTestStep(b, "copyrel-alignment", opts);
476523
477 const a_so = addSharedLibrary(b, "a", opts);
524 const a_so = addSharedLibrary(b, opts, .{ .name = "a" });
478525 addCSourceBytes(a_so, "__attribute__((aligned(32))) int foo = 5;", &.{});
479526
480 const b_so = addSharedLibrary(b, "b", opts);
527 const b_so = addSharedLibrary(b, opts, .{ .name = "b" });
481528 addCSourceBytes(b_so, "__attribute__((aligned(8))) int foo = 5;", &.{});
482529
483 const c_so = addSharedLibrary(b, "c", opts);
530 const c_so = addSharedLibrary(b, opts, .{ .name = "c" });
484531 addCSourceBytes(c_so, "__attribute__((aligned(256))) int foo = 5;", &.{});
485532
486 const obj = addObject(b, "main", opts);
487 addCSourceBytes(obj,
533 const obj = addObject(b, opts, .{
534 .name = "main",
535 .c_source_bytes =
488536 \\#include <stdio.h>
489537 \\extern int foo;
490538 \\int main() { printf("%d\n", foo); }
491 , &.{});
539 \\
540 ,
541 .pic = false,
542 });
492543 obj.linkLibC();
493 obj.force_pic = false;
494544
495545 const exp_stdout = "5\n";
496546
497547 {
498 const exe = addExecutable(b, "main", opts);
548 const exe = addExecutable(b, opts, .{ .name = "main" });
499549 exe.addObject(obj);
500550 exe.linkLibrary(a_so);
501551 exe.linkLibC();
......@@ -514,7 +564,7 @@ fn testCopyrelAlignment(b: *Build, opts: Options) *Step {
514564 }
515565
516566 {
517 const exe = addExecutable(b, "main", opts);
567 const exe = addExecutable(b, opts, .{ .name = "main" });
518568 exe.addObject(obj);
519569 exe.linkLibrary(b_so);
520570 exe.linkLibC();
......@@ -533,7 +583,7 @@ fn testCopyrelAlignment(b: *Build, opts: Options) *Step {
533583 }
534584
535585 {
536 const exe = addExecutable(b, "main", opts);
586 const exe = addExecutable(b, opts, .{ .name = "main" });
537587 exe.addObject(obj);
538588 exe.linkLibrary(c_so);
539589 exe.linkLibC();
......@@ -557,7 +607,7 @@ fn testCopyrelAlignment(b: *Build, opts: Options) *Step {
557607fn testDsoPlt(b: *Build, opts: Options) *Step {
558608 const test_step = addTestStep(b, "dso-plt", opts);
559609
560 const dso = addSharedLibrary(b, "dso", opts);
610 const dso = addSharedLibrary(b, opts, .{ .name = "dso" });
561611 addCSourceBytes(dso,
562612 \\#include<stdio.h>
563613 \\void world() {
......@@ -573,7 +623,7 @@ fn testDsoPlt(b: *Build, opts: Options) *Step {
573623 , &.{});
574624 dso.linkLibC();
575625
576 const exe = addExecutable(b, "test", opts);
626 const exe = addExecutable(b, opts, .{ .name = "test" });
577627 addCSourceBytes(exe,
578628 \\#include<stdio.h>
579629 \\void world() {
......@@ -599,7 +649,7 @@ fn testDsoPlt(b: *Build, opts: Options) *Step {
599649fn testDsoUndef(b: *Build, opts: Options) *Step {
600650 const test_step = addTestStep(b, "dso-undef", opts);
601651
602 const dso = addSharedLibrary(b, "dso", opts);
652 const dso = addSharedLibrary(b, opts, .{ .name = "dso" });
603653 addCSourceBytes(dso,
604654 \\extern int foo;
605655 \\int bar = 5;
......@@ -607,13 +657,15 @@ fn testDsoUndef(b: *Build, opts: Options) *Step {
607657 , &.{});
608658 dso.linkLibC();
609659
610 const obj = addObject(b, "obj", opts);
611 addCSourceBytes(obj, "int foo = 3;", &.{});
660 const obj = addObject(b, opts, .{
661 .name = "obj",
662 .c_source_bytes = "int foo = 3;",
663 });
612664
613 const lib = addStaticLibrary(b, "lib", opts);
665 const lib = addStaticLibrary(b, opts, .{ .name = "lib" });
614666 lib.addObject(obj);
615667
616 const exe = addExecutable(b, "test", opts);
668 const exe = addExecutable(b, opts, .{ .name = "test" });
617669 exe.linkLibrary(dso);
618670 exe.linkLibrary(lib);
619671 addCSourceBytes(exe,
......@@ -641,8 +693,9 @@ fn testDsoUndef(b: *Build, opts: Options) *Step {
641693fn testEmitRelocatable(b: *Build, opts: Options) *Step {
642694 const test_step = addTestStep(b, "emit-relocatable", opts);
643695
644 const obj1 = addObject(b, "obj1", opts);
645 addZigSourceBytes(obj1,
696 const obj1 = addObject(b, opts, .{
697 .name = "obj1",
698 .zig_source_bytes =
646699 \\const std = @import("std");
647700 \\extern var bar: i32;
648701 \\export fn foo() i32 {
......@@ -651,18 +704,20 @@ fn testEmitRelocatable(b: *Build, opts: Options) *Step {
651704 \\export fn printFoo() void {
652705 \\ std.debug.print("foo={d}\n", .{foo()});
653706 \\}
654 );
655 addCSourceBytes(obj1,
707 ,
708 .c_source_bytes =
656709 \\#include <stdio.h>
657710 \\int bar = 42;
658711 \\void printBar() {
659712 \\ fprintf(stderr, "bar=%d\n", bar);
660713 \\}
661 , &.{});
714 ,
715 });
662716 obj1.linkLibC();
663717
664 const exe = addExecutable(b, "test", opts);
665 addZigSourceBytes(exe,
718 const exe = addExecutable(b, opts, .{
719 .name = "test",
720 .zig_source_bytes =
666721 \\const std = @import("std");
667722 \\extern fn printFoo() void;
668723 \\extern fn printBar() void;
......@@ -670,7 +725,8 @@ fn testEmitRelocatable(b: *Build, opts: Options) *Step {
670725 \\ printFoo();
671726 \\ printBar();
672727 \\}
673 );
728 ,
729 });
674730 exe.addObject(obj1);
675731 exe.linkLibC();
676732
......@@ -688,20 +744,26 @@ fn testEmitRelocatable(b: *Build, opts: Options) *Step {
688744fn testEmitStaticLib(b: *Build, opts: Options) *Step {
689745 const test_step = addTestStep(b, "emit-static-lib", opts);
690746
691 const obj1 = addObject(b, "obj1", opts);
692 addCSourceBytes(obj1,
747 const obj1 = addObject(b, opts, .{
748 .name = "obj1",
749 .c_source_bytes =
693750 \\int foo = 0;
694751 \\int bar = 2;
695752 \\int fooBar() {
696753 \\ return foo + bar;
697754 \\}
698 , &.{});
755 ,
756 });
699757
700 const obj2 = addObject(b, "obj2", opts);
701 addCSourceBytes(obj2, "int tentative;", &.{"-fcommon"});
758 const obj2 = addObject(b, opts, .{
759 .name = "obj2",
760 .c_source_bytes = "int tentative;",
761 .c_source_flags = &.{"-fcommon"},
762 });
702763
703 const obj3 = addObject(b, "a_very_long_file_name_so_that_it_ends_up_in_strtab", opts);
704 addZigSourceBytes(obj3,
764 const obj3 = addObject(b, opts, .{
765 .name = "a_very_long_file_name_so_that_it_ends_up_in_strtab",
766 .zig_source_bytes =
705767 \\fn weakFoo() callconv(.C) usize {
706768 \\ return 42;
707769 \\}
......@@ -710,9 +772,10 @@ fn testEmitStaticLib(b: *Build, opts: Options) *Step {
710772 \\ @export(weakFoo, .{ .name = "weakFoo", .linkage = .Weak });
711773 \\ @export(strongBar, .{ .name = "strongBarAlias", .linkage = .Strong });
712774 \\}
713 );
775 ,
776 });
714777
715 const lib = addStaticLibrary(b, "lib", opts);
778 const lib = addStaticLibrary(b, opts, .{ .name = "lib" });
716779 lib.addObject(obj1);
717780 lib.addObject(obj2);
718781 lib.addObject(obj3);
......@@ -747,30 +810,36 @@ fn testEmitStaticLib(b: *Build, opts: Options) *Step {
747810fn testEmitStaticLibZig(b: *Build, opts: Options) *Step {
748811 const test_step = addTestStep(b, "emit-static-lib-zig", opts);
749812
750 const obj1 = addObject(b, "obj1", opts);
751 addZigSourceBytes(obj1,
813 const obj1 = addObject(b, opts, .{
814 .name = "obj1",
815 .zig_source_bytes =
752816 \\export var foo: i32 = 42;
753817 \\export var bar: i32 = 2;
754 );
818 ,
819 });
755820
756 const lib = addStaticLibrary(b, "lib", opts);
757 addZigSourceBytes(lib,
821 const lib = addStaticLibrary(b, opts, .{
822 .name = "lib",
823 .zig_source_bytes =
758824 \\extern var foo: i32;
759825 \\extern var bar: i32;
760826 \\export fn fooBar() i32 {
761827 \\ return foo + bar;
762828 \\}
763 );
829 ,
830 });
764831 lib.addObject(obj1);
765832
766 const exe = addExecutable(b, "test", opts);
767 addZigSourceBytes(exe,
833 const exe = addExecutable(b, opts, .{
834 .name = "test",
835 .zig_source_bytes =
768836 \\const std = @import("std");
769837 \\extern fn fooBar() i32;
770838 \\pub fn main() void {
771839 \\ std.debug.print("{d}", .{fooBar()});
772840 \\}
773 );
841 ,
842 });
774843 exe.linkLibrary(lib);
775844
776845 const run = addRunArtifact(exe);
......@@ -783,7 +852,7 @@ fn testEmitStaticLibZig(b: *Build, opts: Options) *Step {
783852fn testEmptyObject(b: *Build, opts: Options) *Step {
784853 const test_step = addTestStep(b, "empty-object", opts);
785854
786 const exe = addExecutable(b, "test", opts);
855 const exe = addExecutable(b, opts, .{ .name = "test" });
787856 addCSourceBytes(exe, "int main() { return 0; }", &.{});
788857 addCSourceBytes(exe, "", &.{});
789858 exe.linkLibC();
......@@ -798,18 +867,23 @@ fn testEmptyObject(b: *Build, opts: Options) *Step {
798867fn testEntryPoint(b: *Build, opts: Options) *Step {
799868 const test_step = addTestStep(b, "entry-point", opts);
800869
801 const a_o = addObject(b, "a", opts);
802 addAsmSourceBytes(a_o,
870 const a_o = addObject(b, opts, .{
871 .name = "a",
872 .asm_source_bytes =
803873 \\.globl foo, bar
804874 \\foo = 0x1000
805875 \\bar = 0x2000
806 );
876 \\
877 ,
878 });
807879
808 const b_o = addObject(b, "b", opts);
809 addCSourceBytes(b_o, "int main() { return 0; }", &.{});
880 const b_o = addObject(b, opts, .{
881 .name = "b",
882 .c_source_bytes = "int main() { return 0; }",
883 });
810884
811885 {
812 const exe = addExecutable(b, "main", opts);
886 const exe = addExecutable(b, opts, .{ .name = "main" });
813887 exe.addObject(a_o);
814888 exe.addObject(b_o);
815889 exe.entry = .{ .symbol_name = "foo" };
......@@ -825,7 +899,7 @@ fn testEntryPoint(b: *Build, opts: Options) *Step {
825899 // TODO looks like not assigning a unique name to this executable will
826900 // cause an artifact collision taking the cached executable from the above
827901 // step instead of generating a new one.
828 const exe = addExecutable(b, "other", opts);
902 const exe = addExecutable(b, opts, .{ .name = "other" });
829903 exe.addObject(a_o);
830904 exe.addObject(b_o);
831905 exe.entry = .{ .symbol_name = "bar" };
......@@ -843,8 +917,9 @@ fn testEntryPoint(b: *Build, opts: Options) *Step {
843917fn testExportDynamic(b: *Build, opts: Options) *Step {
844918 const test_step = addTestStep(b, "export-dynamic", opts);
845919
846 const obj = addObject(b, "obj", opts);
847 addAsmSourceBytes(obj,
920 const obj = addObject(b, opts, .{
921 .name = "obj",
922 .asm_source_bytes =
848923 \\.text
849924 \\ .globl foo
850925 \\ .hidden foo
......@@ -856,12 +931,14 @@ fn testExportDynamic(b: *Build, opts: Options) *Step {
856931 \\ .globl _start
857932 \\_start:
858933 \\ nop
859 );
934 \\
935 ,
936 });
860937
861 const dso = addSharedLibrary(b, "a", opts);
938 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
862939 addCSourceBytes(dso, "int baz = 10;", &.{});
863940
864 const exe = addExecutable(b, "main", opts);
941 const exe = addExecutable(b, opts, .{ .name = "main" });
865942 addCSourceBytes(exe,
866943 \\extern int baz;
867944 \\int callBaz() {
......@@ -885,7 +962,7 @@ fn testExportDynamic(b: *Build, opts: Options) *Step {
885962fn testExportSymbolsFromExe(b: *Build, opts: Options) *Step {
886963 const test_step = addTestStep(b, "export-symbols-from-exe", opts);
887964
888 const dso = addSharedLibrary(b, "a", opts);
965 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
889966 addCSourceBytes(dso,
890967 \\void expfn1();
891968 \\void expfn2() {}
......@@ -895,7 +972,7 @@ fn testExportSymbolsFromExe(b: *Build, opts: Options) *Step {
895972 \\}
896973 , &.{});
897974
898 const exe = addExecutable(b, "main", opts);
975 const exe = addExecutable(b, opts, .{ .name = "main" });
899976 addCSourceBytes(exe,
900977 \\void expfn1() {}
901978 \\void expfn2() {}
......@@ -923,10 +1000,10 @@ fn testExportSymbolsFromExe(b: *Build, opts: Options) *Step {
9231000fn testFuncAddress(b: *Build, opts: Options) *Step {
9241001 const test_step = addTestStep(b, "func-address", opts);
9251002
926 const dso = addSharedLibrary(b, "a", opts);
1003 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
9271004 addCSourceBytes(dso, "void fn() {}", &.{});
9281005
929 const exe = addExecutable(b, "main", opts);
1006 const exe = addExecutable(b, opts, .{ .name = "main" });
9301007 addCSourceBytes(exe,
9311008 \\#include <assert.h>
9321009 \\typedef void Func();
......@@ -937,7 +1014,7 @@ fn testFuncAddress(b: *Build, opts: Options) *Step {
9371014 \\}
9381015 , &.{});
9391016 exe.linkLibrary(dso);
940 exe.force_pic = false;
1017 exe.root_module.pic = false;
9411018 exe.pie = false;
9421019
9431020 const run = addRunArtifact(exe);
......@@ -950,8 +1027,9 @@ fn testFuncAddress(b: *Build, opts: Options) *Step {
9501027fn testGcSections(b: *Build, opts: Options) *Step {
9511028 const test_step = addTestStep(b, "gc-sections", opts);
9521029
953 const obj = addObject(b, "obj", opts);
954 addCppSourceBytes(obj,
1030 const obj = addObject(b, opts, .{
1031 .name = "obj",
1032 .cpp_source_bytes =
9551033 \\#include <stdio.h>
9561034 \\int two() { return 2; }
9571035 \\int live_var1 = 1;
......@@ -966,14 +1044,15 @@ fn testGcSections(b: *Build, opts: Options) *Step {
9661044 \\ printf("%d %d\n", live_var1, live_var2);
9671045 \\ live_fn2();
9681046 \\}
969 , &.{});
1047 ,
1048 });
9701049 obj.link_function_sections = true;
9711050 obj.link_data_sections = true;
9721051 obj.linkLibC();
9731052 obj.linkLibCpp();
9741053
9751054 {
976 const exe = addExecutable(b, "test", opts);
1055 const exe = addExecutable(b, opts, .{ .name = "test" });
9771056 exe.addObject(obj);
9781057 exe.link_gc_sections = false;
9791058 exe.linkLibC();
......@@ -1004,7 +1083,7 @@ fn testGcSections(b: *Build, opts: Options) *Step {
10041083 }
10051084
10061085 {
1007 const exe = addExecutable(b, "test", opts);
1086 const exe = addExecutable(b, opts, .{ .name = "test" });
10081087 exe.addObject(obj);
10091088 exe.link_gc_sections = true;
10101089 exe.linkLibC();
......@@ -1040,11 +1119,12 @@ fn testGcSections(b: *Build, opts: Options) *Step {
10401119fn testGcSectionsZig(b: *Build, opts: Options) *Step {
10411120 const test_step = addTestStep(b, "gc-sections-zig", opts);
10421121
1043 const obj = addObject(b, "obj", .{
1122 const obj = addObject(b, .{
10441123 .target = opts.target,
10451124 .use_llvm = true,
1046 });
1047 addCSourceBytes(obj,
1125 }, .{
1126 .name = "obj",
1127 .c_source_bytes =
10481128 \\int live_var1 = 1;
10491129 \\int live_var2 = 2;
10501130 \\int dead_var1 = 3;
......@@ -1053,13 +1133,15 @@ fn testGcSectionsZig(b: *Build, opts: Options) *Step {
10531133 \\void live_fn2() { live_fn1(); }
10541134 \\void dead_fn1() {}
10551135 \\void dead_fn2() { dead_fn1(); }
1056 , &.{});
1136 ,
1137 });
10571138 obj.link_function_sections = true;
10581139 obj.link_data_sections = true;
10591140
10601141 {
1061 const exe = addExecutable(b, "test1", opts);
1062 addZigSourceBytes(exe,
1142 const exe = addExecutable(b, opts, .{
1143 .name = "test1",
1144 .zig_source_bytes =
10631145 \\const std = @import("std");
10641146 \\extern var live_var1: i32;
10651147 \\extern var live_var2: i32;
......@@ -1069,7 +1151,8 @@ fn testGcSectionsZig(b: *Build, opts: Options) *Step {
10691151 \\ stdout.writer().print("{d} {d}\n", .{ live_var1, live_var2 }) catch unreachable;
10701152 \\ live_fn2();
10711153 \\}
1072 );
1154 ,
1155 });
10731156 exe.addObject(obj);
10741157 exe.link_gc_sections = false;
10751158
......@@ -1098,8 +1181,9 @@ fn testGcSectionsZig(b: *Build, opts: Options) *Step {
10981181 }
10991182
11001183 {
1101 const exe = addExecutable(b, "test2", opts);
1102 addZigSourceBytes(exe,
1184 const exe = addExecutable(b, opts, .{
1185 .name = "test2",
1186 .zig_source_bytes =
11031187 \\const std = @import("std");
11041188 \\extern var live_var1: i32;
11051189 \\extern var live_var2: i32;
......@@ -1109,7 +1193,8 @@ fn testGcSectionsZig(b: *Build, opts: Options) *Step {
11091193 \\ stdout.writer().print("{d} {d}\n", .{ live_var1, live_var2 }) catch unreachable;
11101194 \\ live_fn2();
11111195 \\}
1112 );
1196 ,
1197 });
11131198 exe.addObject(obj);
11141199 exe.link_gc_sections = true;
11151200
......@@ -1143,7 +1228,7 @@ fn testGcSectionsZig(b: *Build, opts: Options) *Step {
11431228fn testHiddenWeakUndef(b: *Build, opts: Options) *Step {
11441229 const test_step = addTestStep(b, "hidden-weak-undef", opts);
11451230
1146 const dso = addSharedLibrary(b, "a", opts);
1231 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
11471232 addCSourceBytes(dso,
11481233 \\__attribute__((weak, visibility("hidden"))) void foo();
11491234 \\void bar() { foo(); }
......@@ -1162,7 +1247,7 @@ fn testHiddenWeakUndef(b: *Build, opts: Options) *Step {
11621247fn testIFuncAlias(b: *Build, opts: Options) *Step {
11631248 const test_step = addTestStep(b, "ifunc-alias", opts);
11641249
1165 const exe = addExecutable(b, "main", opts);
1250 const exe = addExecutable(b, opts, .{ .name = "main" });
11661251 addCSourceBytes(exe,
11671252 \\#include <assert.h>
11681253 \\void foo() {}
......@@ -1173,7 +1258,7 @@ fn testIFuncAlias(b: *Build, opts: Options) *Step {
11731258 \\ assert(bar == bar2);
11741259 \\}
11751260 , &.{});
1176 exe.force_pic = true;
1261 exe.root_module.pic = true;
11771262 exe.linkLibC();
11781263 // https://github.com/ziglang/zig/issues/17619
11791264 exe.pie = true;
......@@ -1188,7 +1273,7 @@ fn testIFuncAlias(b: *Build, opts: Options) *Step {
11881273fn testIFuncDlopen(b: *Build, opts: Options) *Step {
11891274 const test_step = addTestStep(b, "ifunc-dlopen", opts);
11901275
1191 const dso = addSharedLibrary(b, "a", opts);
1276 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
11921277 addCSourceBytes(dso,
11931278 \\__attribute__((ifunc("resolve_foo")))
11941279 \\void foo(void);
......@@ -1200,7 +1285,7 @@ fn testIFuncDlopen(b: *Build, opts: Options) *Step {
12001285 \\}
12011286 , &.{});
12021287
1203 const exe = addExecutable(b, "main", opts);
1288 const exe = addExecutable(b, opts, .{ .name = "main" });
12041289 addCSourceBytes(exe,
12051290 \\#include <dlfcn.h>
12061291 \\#include <assert.h>
......@@ -1219,7 +1304,7 @@ fn testIFuncDlopen(b: *Build, opts: Options) *Step {
12191304 exe.linkLibrary(dso);
12201305 exe.linkLibC();
12211306 exe.linkSystemLibrary2("dl", .{});
1222 exe.force_pic = false;
1307 exe.root_module.pic = false;
12231308 exe.pie = false;
12241309
12251310 const run = addRunArtifact(exe);
......@@ -1232,7 +1317,7 @@ fn testIFuncDlopen(b: *Build, opts: Options) *Step {
12321317fn testIFuncDso(b: *Build, opts: Options) *Step {
12331318 const test_step = addTestStep(b, "ifunc-dso", opts);
12341319
1235 const dso = addSharedLibrary(b, "a", opts);
1320 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
12361321 addCSourceBytes(dso,
12371322 \\#include<stdio.h>
12381323 \\__attribute__((ifunc("resolve_foobar")))
......@@ -1247,7 +1332,7 @@ fn testIFuncDso(b: *Build, opts: Options) *Step {
12471332 , &.{});
12481333 dso.linkLibC();
12491334
1250 const exe = addExecutable(b, "main", opts);
1335 const exe = addExecutable(b, opts, .{ .name = "main" });
12511336 addCSourceBytes(exe,
12521337 \\void foobar(void);
12531338 \\int main() {
......@@ -1283,7 +1368,7 @@ fn testIFuncDynamic(b: *Build, opts: Options) *Step {
12831368 ;
12841369
12851370 {
1286 const exe = addExecutable(b, "main", opts);
1371 const exe = addExecutable(b, opts, .{ .name = "main" });
12871372 addCSourceBytes(exe, main_c, &.{});
12881373 exe.linkLibC();
12891374 exe.link_z_lazy = true;
......@@ -1295,7 +1380,7 @@ fn testIFuncDynamic(b: *Build, opts: Options) *Step {
12951380 test_step.dependOn(&run.step);
12961381 }
12971382 {
1298 const exe = addExecutable(b, "other", opts);
1383 const exe = addExecutable(b, opts, .{ .name = "other" });
12991384 addCSourceBytes(exe, main_c, &.{});
13001385 exe.linkLibC();
13011386 // https://github.com/ziglang/zig/issues/17619
......@@ -1312,7 +1397,7 @@ fn testIFuncDynamic(b: *Build, opts: Options) *Step {
13121397fn testIFuncExport(b: *Build, opts: Options) *Step {
13131398 const test_step = addTestStep(b, "ifunc-export", opts);
13141399
1315 const dso = addSharedLibrary(b, "a", opts);
1400 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
13161401 addCSourceBytes(dso,
13171402 \\#include <stdio.h>
13181403 \\__attribute__((ifunc("resolve_foobar")))
......@@ -1338,7 +1423,7 @@ fn testIFuncExport(b: *Build, opts: Options) *Step {
13381423fn testIFuncFuncPtr(b: *Build, opts: Options) *Step {
13391424 const test_step = addTestStep(b, "ifunc-func-ptr", opts);
13401425
1341 const exe = addExecutable(b, "main", opts);
1426 const exe = addExecutable(b, opts, .{ .name = "main" });
13421427 addCSourceBytes(exe,
13431428 \\typedef int Fn();
13441429 \\int foo() __attribute__((ifunc("resolve_foo")));
......@@ -1361,7 +1446,7 @@ fn testIFuncFuncPtr(b: *Build, opts: Options) *Step {
13611446 \\ printf("%d\n", f());
13621447 \\}
13631448 , &.{});
1364 exe.force_pic = true;
1449 exe.root_module.pic = true;
13651450 exe.linkLibC();
13661451 // https://github.com/ziglang/zig/issues/17619
13671452 exe.pie = true;
......@@ -1376,7 +1461,7 @@ fn testIFuncFuncPtr(b: *Build, opts: Options) *Step {
13761461fn testIFuncNoPlt(b: *Build, opts: Options) *Step {
13771462 const test_step = addTestStep(b, "ifunc-noplt", opts);
13781463
1379 const exe = addExecutable(b, "main", opts);
1464 const exe = addExecutable(b, opts, .{ .name = "main" });
13801465 addCSourceBytes(exe,
13811466 \\#include <stdio.h>
13821467 \\__attribute__((ifunc("resolve_foo")))
......@@ -1392,7 +1477,7 @@ fn testIFuncNoPlt(b: *Build, opts: Options) *Step {
13921477 \\ foo();
13931478 \\}
13941479 , &.{"-fno-plt"});
1395 exe.force_pic = true;
1480 exe.root_module.pic = true;
13961481 exe.linkLibC();
13971482 // https://github.com/ziglang/zig/issues/17619
13981483 exe.pie = true;
......@@ -1407,7 +1492,7 @@ fn testIFuncNoPlt(b: *Build, opts: Options) *Step {
14071492fn testIFuncStatic(b: *Build, opts: Options) *Step {
14081493 const test_step = addTestStep(b, "ifunc-static", opts);
14091494
1410 const exe = addExecutable(b, "main", opts);
1495 const exe = addExecutable(b, opts, .{ .name = "main" });
14111496 addCSourceBytes(exe,
14121497 \\#include <stdio.h>
14131498 \\void foo() __attribute__((ifunc("resolve_foo")));
......@@ -1435,7 +1520,7 @@ fn testIFuncStatic(b: *Build, opts: Options) *Step {
14351520fn testIFuncStaticPie(b: *Build, opts: Options) *Step {
14361521 const test_step = addTestStep(b, "ifunc-static-pie", opts);
14371522
1438 const exe = addExecutable(b, "main", opts);
1523 const exe = addExecutable(b, opts, .{ .name = "main" });
14391524 addCSourceBytes(exe,
14401525 \\#include <stdio.h>
14411526 \\void foo() __attribute__((ifunc("resolve_foo")));
......@@ -1451,7 +1536,7 @@ fn testIFuncStaticPie(b: *Build, opts: Options) *Step {
14511536 \\}
14521537 , &.{});
14531538 exe.linkage = .static;
1454 exe.force_pic = true;
1539 exe.root_module.pic = true;
14551540 exe.pie = true;
14561541 exe.linkLibC();
14571542
......@@ -1478,7 +1563,7 @@ fn testImageBase(b: *Build, opts: Options) *Step {
14781563 const test_step = addTestStep(b, "image-base", opts);
14791564
14801565 {
1481 const exe = addExecutable(b, "main1", opts);
1566 const exe = addExecutable(b, opts, .{ .name = "main1" });
14821567 addCSourceBytes(exe,
14831568 \\#include <stdio.h>
14841569 \\int main() {
......@@ -1502,7 +1587,7 @@ fn testImageBase(b: *Build, opts: Options) *Step {
15021587 }
15031588
15041589 {
1505 const exe = addExecutable(b, "main2", opts);
1590 const exe = addExecutable(b, opts, .{ .name = "main2" });
15061591 addCSourceBytes(exe, "void _start() {}", &.{});
15071592 exe.image_base = 0xffffffff8000000;
15081593
......@@ -1520,22 +1605,26 @@ fn testImageBase(b: *Build, opts: Options) *Step {
15201605fn testImportingDataDynamic(b: *Build, opts: Options) *Step {
15211606 const test_step = addTestStep(b, "importing-data-dynamic", opts);
15221607
1523 const dso = addSharedLibrary(b, "a", .{
1608 const dso = addSharedLibrary(b, .{
15241609 .target = opts.target,
15251610 .optimize = opts.optimize,
15261611 .use_llvm = true,
1612 }, .{
1613 .name = "a",
1614 .c_source_bytes = "int foo = 42;",
15271615 });
1528 addCSourceBytes(dso, "int foo = 42;", &.{});
15291616
1530 const main = addExecutable(b, "main", opts);
1531 addZigSourceBytes(main,
1617 const main = addExecutable(b, opts, .{
1618 .name = "main",
1619 .zig_source_bytes =
15321620 \\extern var foo: i32;
15331621 \\pub fn main() void {
15341622 \\ @import("std").debug.print("{d}\n", .{foo});
15351623 \\}
1536 );
1624 ,
1625 .strip = true, // TODO temp hack
1626 });
15371627 main.pie = true;
1538 main.strip = true; // TODO temp hack
15391628 main.linkLibrary(dso);
15401629 main.linkLibC();
15411630
......@@ -1549,28 +1638,34 @@ fn testImportingDataDynamic(b: *Build, opts: Options) *Step {
15491638fn testImportingDataStatic(b: *Build, opts: Options) *Step {
15501639 const test_step = addTestStep(b, "importing-data-static", opts);
15511640
1552 const obj = addObject(b, "a", .{
1641 const obj = addObject(b, .{
15531642 .target = opts.target,
15541643 .optimize = opts.optimize,
15551644 .use_llvm = true,
1645 }, .{
1646 .name = "a",
1647 .c_source_bytes = "int foo = 42;",
15561648 });
1557 addCSourceBytes(obj, "int foo = 42;", &.{});
15581649
1559 const lib = addStaticLibrary(b, "a", .{
1650 const lib = addStaticLibrary(b, .{
15601651 .target = opts.target,
15611652 .optimize = opts.optimize,
15621653 .use_llvm = true,
1654 }, .{
1655 .name = "a",
15631656 });
15641657 lib.addObject(obj);
15651658
1566 const main = addExecutable(b, "main", opts);
1567 addZigSourceBytes(main,
1659 const main = addExecutable(b, opts, .{
1660 .name = "main",
1661 .zig_source_bytes =
15681662 \\extern var foo: i32;
15691663 \\pub fn main() void {
15701664 \\ @import("std").debug.print("{d}\n", .{foo});
15711665 \\}
1572 );
1573 main.strip = true; // TODO temp hack
1666 ,
1667 .strip = true, // TODO temp hack
1668 });
15741669 main.linkLibrary(lib);
15751670 main.linkLibC();
15761671
......@@ -1584,63 +1679,76 @@ fn testImportingDataStatic(b: *Build, opts: Options) *Step {
15841679fn testInitArrayOrder(b: *Build, opts: Options) *Step {
15851680 const test_step = addTestStep(b, "init-array-order", opts);
15861681
1587 const a_o = addObject(b, "a", opts);
1588 addCSourceBytes(a_o,
1682 const a_o = addObject(b, opts, .{
1683 .name = "a",
1684 .c_source_bytes =
15891685 \\#include <stdio.h>
15901686 \\__attribute__((constructor(10000))) void init4() { printf("1"); }
1591 , &.{});
1687 ,
1688 });
15921689 a_o.linkLibC();
15931690
1594 const b_o = addObject(b, "b", opts);
1595 addCSourceBytes(b_o,
1691 const b_o = addObject(b, opts, .{
1692 .name = "b",
1693 .c_source_bytes =
15961694 \\#include <stdio.h>
15971695 \\__attribute__((constructor(1000))) void init3() { printf("2"); }
1598 , &.{});
1696 ,
1697 });
15991698 b_o.linkLibC();
16001699
1601 const c_o = addObject(b, "c", opts);
1602 addCSourceBytes(c_o,
1700 const c_o = addObject(b, opts, .{
1701 .name = "c",
1702 .c_source_bytes =
16031703 \\#include <stdio.h>
16041704 \\__attribute__((constructor)) void init1() { printf("3"); }
1605 , &.{});
1705 ,
1706 });
16061707 c_o.linkLibC();
16071708
1608 const d_o = addObject(b, "d", opts);
1609 addCSourceBytes(d_o,
1709 const d_o = addObject(b, opts, .{
1710 .name = "d",
1711 .c_source_bytes =
16101712 \\#include <stdio.h>
16111713 \\__attribute__((constructor)) void init2() { printf("4"); }
1612 , &.{});
1714 ,
1715 });
16131716 d_o.linkLibC();
16141717
1615 const e_o = addObject(b, "e", opts);
1616 addCSourceBytes(e_o,
1718 const e_o = addObject(b, opts, .{
1719 .name = "e",
1720 .c_source_bytes =
16171721 \\#include <stdio.h>
16181722 \\__attribute__((destructor(10000))) void fini4() { printf("5"); }
1619 , &.{});
1723 ,
1724 });
16201725 e_o.linkLibC();
16211726
1622 const f_o = addObject(b, "f", opts);
1623 addCSourceBytes(f_o,
1727 const f_o = addObject(b, opts, .{
1728 .name = "f",
1729 .c_source_bytes =
16241730 \\#include <stdio.h>
16251731 \\__attribute__((destructor(1000))) void fini3() { printf("6"); }
1626 , &.{});
1732 ,
1733 });
16271734 f_o.linkLibC();
16281735
1629 const g_o = addObject(b, "g", opts);
1630 addCSourceBytes(g_o,
1736 const g_o = addObject(b, opts, .{
1737 .name = "g",
1738 .c_source_bytes =
16311739 \\#include <stdio.h>
16321740 \\__attribute__((destructor)) void fini1() { printf("7"); }
1633 , &.{});
1741 ,
1742 });
16341743 g_o.linkLibC();
16351744
1636 const h_o = addObject(b, "h", opts);
1637 addCSourceBytes(h_o,
1638 \\#include <stdio.h>
1639 \\__attribute__((destructor)) void fini2() { printf("8"); }
1640 , &.{});
1745 const h_o = addObject(b, opts, .{ .name = "h", .c_source_bytes =
1746 \\#include <stdio.h>
1747 \\__attribute__((destructor)) void fini2() { printf("8"); }
1748 });
16411749 h_o.linkLibC();
16421750
1643 const exe = addExecutable(b, "main", opts);
1751 const exe = addExecutable(b, opts, .{ .name = "main" });
16441752 addCSourceBytes(exe, "int main() { return 0; }", &.{});
16451753 exe.addObject(a_o);
16461754 exe.addObject(b_o);
......@@ -1651,7 +1759,7 @@ fn testInitArrayOrder(b: *Build, opts: Options) *Step {
16511759 exe.addObject(g_o);
16521760 exe.addObject(h_o);
16531761
1654 if (opts.target.isGnuLibC()) {
1762 if (opts.target.target.isGnuLibC()) {
16551763 // TODO I think we need to clarify our use of `-fPIC -fPIE` flags for different targets
16561764 exe.pie = true;
16571765 }
......@@ -1666,7 +1774,7 @@ fn testInitArrayOrder(b: *Build, opts: Options) *Step {
16661774fn testLargeAlignmentDso(b: *Build, opts: Options) *Step {
16671775 const test_step = addTestStep(b, "large-alignment-dso", opts);
16681776
1669 const dso = addSharedLibrary(b, "dso", opts);
1777 const dso = addSharedLibrary(b, opts, .{ .name = "dso" });
16701778 addCSourceBytes(dso,
16711779 \\#include <stdio.h>
16721780 \\#include <stdint.h>
......@@ -1695,7 +1803,7 @@ fn testLargeAlignmentDso(b: *Build, opts: Options) *Step {
16951803 check.checkComputeCompare("addr2 16 %", .{ .op = .eq, .value = .{ .literal = 0 } });
16961804 test_step.dependOn(&check.step);
16971805
1698 const exe = addExecutable(b, "test", opts);
1806 const exe = addExecutable(b, opts, .{ .name = "test" });
16991807 addCSourceBytes(exe,
17001808 \\void greet();
17011809 \\int main() { greet(); }
......@@ -1715,7 +1823,7 @@ fn testLargeAlignmentDso(b: *Build, opts: Options) *Step {
17151823fn testLargeAlignmentExe(b: *Build, opts: Options) *Step {
17161824 const test_step = addTestStep(b, "large-alignment-exe", opts);
17171825
1718 const exe = addExecutable(b, "test", opts);
1826 const exe = addExecutable(b, opts, .{ .name = "test" });
17191827 addCSourceBytes(exe,
17201828 \\#include <stdio.h>
17211829 \\#include <stdint.h>
......@@ -1760,7 +1868,7 @@ fn testLargeAlignmentExe(b: *Build, opts: Options) *Step {
17601868fn testLargeBss(b: *Build, opts: Options) *Step {
17611869 const test_step = addTestStep(b, "large-bss", opts);
17621870
1763 const exe = addExecutable(b, "main", opts);
1871 const exe = addExecutable(b, opts, .{ .name = "main" });
17641872 addCSourceBytes(exe,
17651873 \\char arr[0x100000000];
17661874 \\int main() {
......@@ -1781,27 +1889,31 @@ fn testLargeBss(b: *Build, opts: Options) *Step {
17811889fn testLinkOrder(b: *Build, opts: Options) *Step {
17821890 const test_step = addTestStep(b, "link-order", opts);
17831891
1784 const obj = addObject(b, "obj", opts);
1785 addCSourceBytes(obj, "void foo() {}", &.{});
1786 obj.force_pic = true;
1892 const obj = addObject(b, opts, .{
1893 .name = "obj",
1894 .c_source_bytes = "void foo() {}",
1895 .pic = true,
1896 });
17871897
1788 const dso = addSharedLibrary(b, "a", opts);
1898 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
17891899 dso.addObject(obj);
17901900
1791 const lib = addStaticLibrary(b, "b", opts);
1901 const lib = addStaticLibrary(b, opts, .{ .name = "b" });
17921902 lib.addObject(obj);
17931903
1794 const main_o = addObject(b, "main", opts);
1795 addCSourceBytes(main_o,
1904 const main_o = addObject(b, opts, .{
1905 .name = "main",
1906 .c_source_bytes =
17961907 \\void foo();
17971908 \\int main() {
17981909 \\ foo();
17991910 \\}
1800 , &.{});
1911 ,
1912 });
18011913
18021914 // https://github.com/ziglang/zig/issues/17450
18031915 // {
1804 // const exe = addExecutable(b, "main1", opts);
1916 // const exe = addExecutable(b, opts, .{ .name = "main1"});
18051917 // exe.addObject(main_o);
18061918 // exe.linkSystemLibrary2("a", .{});
18071919 // exe.addLibraryPath(dso.getEmittedBinDirectory());
......@@ -1818,7 +1930,7 @@ fn testLinkOrder(b: *Build, opts: Options) *Step {
18181930 // }
18191931
18201932 {
1821 const exe = addExecutable(b, "main2", opts);
1933 const exe = addExecutable(b, opts, .{ .name = "main2" });
18221934 exe.addObject(main_o);
18231935 exe.linkSystemLibrary2("b", .{});
18241936 exe.addLibraryPath(lib.getEmittedBinDirectory());
......@@ -1840,14 +1952,14 @@ fn testLinkOrder(b: *Build, opts: Options) *Step {
18401952fn testLdScript(b: *Build, opts: Options) *Step {
18411953 const test_step = addTestStep(b, "ld-script", opts);
18421954
1843 const dso = addSharedLibrary(b, "bar", opts);
1955 const dso = addSharedLibrary(b, opts, .{ .name = "bar" });
18441956 addCSourceBytes(dso, "int foo() { return 42; }", &.{});
18451957
18461958 const scripts = WriteFile.create(b);
18471959 _ = scripts.add("liba.so", "INPUT(libfoo.so)");
18481960 _ = scripts.add("libfoo.so", "GROUP(AS_NEEDED(-lbar))");
18491961
1850 const exe = addExecutable(b, "main", opts);
1962 const exe = addExecutable(b, opts, .{ .name = "main" });
18511963 addCSourceBytes(exe,
18521964 \\int foo();
18531965 \\int main() {
......@@ -1875,7 +1987,7 @@ fn testLdScriptPathError(b: *Build, opts: Options) *Step {
18751987 const scripts = WriteFile.create(b);
18761988 _ = scripts.add("liba.so", "INPUT(libfoo.so)");
18771989
1878 const exe = addExecutable(b, "main", opts);
1990 const exe = addExecutable(b, opts, .{ .name = "main" });
18791991 addCSourceBytes(exe, "int main() { return 0; }", &.{});
18801992 exe.linkSystemLibrary2("a", .{});
18811993 exe.addLibraryPath(scripts.getDirectory());
......@@ -1895,13 +2007,15 @@ fn testLdScriptPathError(b: *Build, opts: Options) *Step {
18952007fn testMismatchedCpuArchitectureError(b: *Build, opts: Options) *Step {
18962008 const test_step = addTestStep(b, "mismatched-cpu-architecture-error", opts);
18972009
1898 const obj = addObject(b, "a", .{
1899 .target = .{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .gnu },
2010 const obj = addObject(b, .{
2011 .target = b.resolveTargetQuery(.{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .gnu }),
2012 }, .{
2013 .name = "a",
2014 .c_source_bytes = "int foo;",
2015 .strip = true,
19002016 });
1901 addCSourceBytes(obj, "int foo;", &.{});
1902 obj.strip = true;
19032017
1904 const exe = addExecutable(b, "main", opts);
2018 const exe = addExecutable(b, opts, .{ .name = "main" });
19052019 addCSourceBytes(exe,
19062020 \\extern int foo;
19072021 \\int main() {
......@@ -1922,7 +2036,7 @@ fn testMismatchedCpuArchitectureError(b: *Build, opts: Options) *Step {
19222036fn testLinkingC(b: *Build, opts: Options) *Step {
19232037 const test_step = addTestStep(b, "linking-c", opts);
19242038
1925 const exe = addExecutable(b, "test", opts);
2039 const exe = addExecutable(b, opts, .{ .name = "test" });
19262040 addCSourceBytes(exe,
19272041 \\#include <stdio.h>
19282042 \\int main() {
......@@ -1951,7 +2065,7 @@ fn testLinkingC(b: *Build, opts: Options) *Step {
19512065fn testLinkingCpp(b: *Build, opts: Options) *Step {
19522066 const test_step = addTestStep(b, "linking-cpp", opts);
19532067
1954 const exe = addExecutable(b, "test", opts);
2068 const exe = addExecutable(b, opts, .{ .name = "test" });
19552069 addCppSourceBytes(exe,
19562070 \\#include <iostream>
19572071 \\int main() {
......@@ -1981,24 +2095,28 @@ fn testLinkingCpp(b: *Build, opts: Options) *Step {
19812095fn testLinkingObj(b: *Build, opts: Options) *Step {
19822096 const test_step = addTestStep(b, "linking-obj", opts);
19832097
1984 const obj = addObject(b, "aobj", opts);
1985 addZigSourceBytes(obj,
2098 const obj = addObject(b, opts, .{
2099 .name = "aobj",
2100 .zig_source_bytes =
19862101 \\extern var mod: usize;
19872102 \\export fn callMe() usize {
19882103 \\ return me * mod;
19892104 \\}
19902105 \\var me: usize = 42;
1991 );
2106 ,
2107 });
19922108
1993 const exe = addExecutable(b, "testobj", opts);
1994 addZigSourceBytes(exe,
2109 const exe = addExecutable(b, opts, .{
2110 .name = "testobj",
2111 .zig_source_bytes =
19952112 \\const std = @import("std");
19962113 \\extern fn callMe() usize;
19972114 \\export var mod: usize = 2;
19982115 \\pub fn main() void {
19992116 \\ std.debug.print("{d}\n", .{callMe()});
20002117 \\}
2001 );
2118 ,
2119 });
20022120 exe.addObject(obj);
20032121
20042122 const run = addRunArtifact(exe);
......@@ -2011,26 +2129,32 @@ fn testLinkingObj(b: *Build, opts: Options) *Step {
20112129fn testLinkingStaticLib(b: *Build, opts: Options) *Step {
20122130 const test_step = addTestStep(b, "linking-static-lib", opts);
20132131
2014 const obj = addObject(b, "bobj", opts);
2015 addZigSourceBytes(obj, "export var bar: i32 = -42;");
2132 const obj = addObject(b, opts, .{
2133 .name = "bobj",
2134 .zig_source_bytes = "export var bar: i32 = -42;",
2135 });
20162136
2017 const lib = addStaticLibrary(b, "alib", opts);
2018 addZigSourceBytes(lib,
2137 const lib = addStaticLibrary(b, opts, .{
2138 .name = "alib",
2139 .zig_source_bytes =
20192140 \\export fn foo() i32 {
20202141 \\ return 42;
20212142 \\}
2022 );
2143 ,
2144 });
20232145 lib.addObject(obj);
20242146
2025 const exe = addExecutable(b, "testlib", opts);
2026 addZigSourceBytes(exe,
2147 const exe = addExecutable(b, opts, .{
2148 .name = "testlib",
2149 .zig_source_bytes =
20272150 \\const std = @import("std");
20282151 \\extern fn foo() i32;
20292152 \\extern var bar: i32;
20302153 \\pub fn main() void {
20312154 \\ std.debug.print("{d}\n", .{foo() + bar});
20322155 \\}
2033 );
2156 ,
2157 });
20342158 exe.linkLibrary(lib);
20352159
20362160 const run = addRunArtifact(exe);
......@@ -2043,12 +2167,14 @@ fn testLinkingStaticLib(b: *Build, opts: Options) *Step {
20432167fn testLinkingZig(b: *Build, opts: Options) *Step {
20442168 const test_step = addTestStep(b, "linking-zig-static", opts);
20452169
2046 const exe = addExecutable(b, "test", opts);
2047 addZigSourceBytes(exe,
2170 const exe = addExecutable(b, opts, .{
2171 .name = "test",
2172 .zig_source_bytes =
20482173 \\pub fn main() void {
20492174 \\ @import("std").debug.print("Hello World!\n", .{});
20502175 \\}
2051 );
2176 ,
2177 });
20522178
20532179 const run = addRunArtifact(exe);
20542180 run.expectStdErrEqual("Hello World!\n");
......@@ -2069,7 +2195,7 @@ fn testLinkingZig(b: *Build, opts: Options) *Step {
20692195fn testNoEhFrameHdr(b: *Build, opts: Options) *Step {
20702196 const test_step = addTestStep(b, "no-eh-frame-hdr", opts);
20712197
2072 const exe = addExecutable(b, "main", opts);
2198 const exe = addExecutable(b, opts, .{ .name = "main" });
20732199 addCSourceBytes(exe, "int main() { return 0; }", &.{});
20742200 exe.link_eh_frame_hdr = false;
20752201 exe.linkLibC();
......@@ -2086,7 +2212,7 @@ fn testNoEhFrameHdr(b: *Build, opts: Options) *Step {
20862212fn testPie(b: *Build, opts: Options) *Step {
20872213 const test_step = addTestStep(b, "hello-pie", opts);
20882214
2089 const exe = addExecutable(b, "main", opts);
2215 const exe = addExecutable(b, opts, .{ .name = "main" });
20902216 addCSourceBytes(exe,
20912217 \\#include <stdio.h>
20922218 \\int main() {
......@@ -2095,7 +2221,7 @@ fn testPie(b: *Build, opts: Options) *Step {
20952221 \\}
20962222 , &.{});
20972223 exe.linkLibC();
2098 exe.force_pic = true;
2224 exe.root_module.pic = true;
20992225 exe.pie = true;
21002226
21012227 const run = addRunArtifact(exe);
......@@ -2117,7 +2243,7 @@ fn testPie(b: *Build, opts: Options) *Step {
21172243fn testPltGot(b: *Build, opts: Options) *Step {
21182244 const test_step = addTestStep(b, "plt-got", opts);
21192245
2120 const dso = addSharedLibrary(b, "a", opts);
2246 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
21212247 addCSourceBytes(dso,
21222248 \\#include <stdio.h>
21232249 \\void ignore(void *foo) {}
......@@ -2127,7 +2253,7 @@ fn testPltGot(b: *Build, opts: Options) *Step {
21272253 , &.{});
21282254 dso.linkLibC();
21292255
2130 const exe = addExecutable(b, "main", opts);
2256 const exe = addExecutable(b, opts, .{ .name = "main" });
21312257 addCSourceBytes(exe,
21322258 \\void ignore(void *);
21332259 \\int hello();
......@@ -2135,7 +2261,7 @@ fn testPltGot(b: *Build, opts: Options) *Step {
21352261 \\int main() { hello(); }
21362262 , &.{});
21372263 exe.linkLibrary(dso);
2138 exe.force_pic = true;
2264 exe.root_module.pic = true;
21392265 exe.linkLibC();
21402266 // https://github.com/ziglang/zig/issues/17619
21412267 exe.pie = true;
......@@ -2151,10 +2277,12 @@ fn testPreinitArray(b: *Build, opts: Options) *Step {
21512277 const test_step = addTestStep(b, "preinit-array", opts);
21522278
21532279 {
2154 const obj = addObject(b, "obj", opts);
2155 addCSourceBytes(obj, "void _start() {}", &.{});
2280 const obj = addObject(b, opts, .{
2281 .name = "obj",
2282 .c_source_bytes = "void _start() {}",
2283 });
21562284
2157 const exe = addExecutable(b, "main1", opts);
2285 const exe = addExecutable(b, opts, .{ .name = "main1" });
21582286 exe.addObject(obj);
21592287
21602288 const check = exe.checkObject();
......@@ -2163,7 +2291,7 @@ fn testPreinitArray(b: *Build, opts: Options) *Step {
21632291 }
21642292
21652293 {
2166 const exe = addExecutable(b, "main2", opts);
2294 const exe = addExecutable(b, opts, .{ .name = "main2" });
21672295 addCSourceBytes(exe,
21682296 \\void preinit_fn() {}
21692297 \\int main() {}
......@@ -2183,38 +2311,48 @@ fn testPreinitArray(b: *Build, opts: Options) *Step {
21832311fn testRelocatableArchive(b: *Build, opts: Options) *Step {
21842312 const test_step = addTestStep(b, "relocatable-archive", opts);
21852313
2186 const obj1 = addObject(b, "obj1", opts);
2187 addCSourceBytes(obj1,
2314 const obj1 = addObject(b, opts, .{
2315 .name = "obj1",
2316 .c_source_bytes =
21882317 \\void bar();
21892318 \\void foo() {
21902319 \\ bar();
21912320 \\}
2192 , &.{});
2321 ,
2322 });
21932323
2194 const obj2 = addObject(b, "obj2", opts);
2195 addCSourceBytes(obj2,
2324 const obj2 = addObject(b, opts, .{
2325 .name = "obj2",
2326 .c_source_bytes =
21962327 \\void bar() {}
2197 , &.{});
2328 ,
2329 });
21982330
2199 const obj3 = addObject(b, "obj3", opts);
2200 addCSourceBytes(obj3,
2331 const obj3 = addObject(b, opts, .{
2332 .name = "obj3",
2333 .c_source_bytes =
22012334 \\void baz();
2202 , &.{});
2335 ,
2336 });
22032337
2204 const obj4 = addObject(b, "obj4", opts);
2205 addCSourceBytes(obj4,
2338 const obj4 = addObject(b, opts, .{
2339 .name = "obj4",
2340 .c_source_bytes =
22062341 \\void foo();
22072342 \\int main() {
22082343 \\ foo();
22092344 \\}
2210 , &.{});
2345 ,
2346 });
22112347
2212 const lib = addStaticLibrary(b, "lib", opts);
2348 const lib = addStaticLibrary(b, opts, .{ .name = "lib" });
22132349 lib.addObject(obj1);
22142350 lib.addObject(obj2);
22152351 lib.addObject(obj3);
22162352
2217 const obj5 = addObject(b, "obj5", opts);
2353 const obj5 = addObject(b, opts, .{
2354 .name = "obj5",
2355 });
22182356 obj5.addObject(obj4);
22192357 obj5.linkLibrary(lib);
22202358
......@@ -2234,13 +2372,15 @@ fn testRelocatableEhFrame(b: *Build, opts: Options) *Step {
22342372 const test_step = addTestStep(b, "relocatable-eh-frame", opts);
22352373
22362374 {
2237 const obj = addObject(b, "obj1", opts);
2238 addCppSourceBytes(obj,
2375 const obj = addObject(b, opts, .{
2376 .name = "obj1",
2377 .cpp_source_bytes =
22392378 \\#include <stdexcept>
22402379 \\int try_me() {
22412380 \\ throw std::runtime_error("Oh no!");
22422381 \\}
2243 , &.{});
2382 ,
2383 });
22442384 addCppSourceBytes(obj,
22452385 \\extern int try_me();
22462386 \\int try_again() {
......@@ -2249,7 +2389,7 @@ fn testRelocatableEhFrame(b: *Build, opts: Options) *Step {
22492389 , &.{});
22502390 obj.linkLibCpp();
22512391
2252 const exe = addExecutable(b, "test1", opts);
2392 const exe = addExecutable(b, opts, .{ .name = "test1" });
22532393 addCppSourceBytes(exe,
22542394 \\#include <iostream>
22552395 \\#include <stdexcept>
......@@ -2273,13 +2413,15 @@ fn testRelocatableEhFrame(b: *Build, opts: Options) *Step {
22732413
22742414 {
22752415 // Let's make the object file COMDAT group heavy!
2276 const obj = addObject(b, "obj2", opts);
2277 addCppSourceBytes(obj,
2416 const obj = addObject(b, opts, .{
2417 .name = "obj2",
2418 .cpp_source_bytes =
22782419 \\#include <stdexcept>
22792420 \\int try_me() {
22802421 \\ throw std::runtime_error("Oh no!");
22812422 \\}
2282 , &.{});
2423 ,
2424 });
22832425 addCppSourceBytes(obj,
22842426 \\extern int try_me();
22852427 \\int try_again() {
......@@ -2301,7 +2443,7 @@ fn testRelocatableEhFrame(b: *Build, opts: Options) *Step {
23012443 , &.{});
23022444 obj.linkLibCpp();
23032445
2304 const exe = addExecutable(b, "test2", opts);
2446 const exe = addExecutable(b, opts, .{ .name = "test2" });
23052447 exe.addObject(obj);
23062448 exe.linkLibCpp();
23072449
......@@ -2316,13 +2458,18 @@ fn testRelocatableEhFrame(b: *Build, opts: Options) *Step {
23162458fn testRelocatableNoEhFrame(b: *Build, opts: Options) *Step {
23172459 const test_step = addTestStep(b, "relocatable-no-eh-frame", opts);
23182460
2319 const obj1 = addObject(b, "obj1", opts);
2320 addCSourceBytes(obj1, "int bar() { return 42; }", &.{
2321 "-fno-unwind-tables",
2322 "-fno-asynchronous-unwind-tables",
2461 const obj1 = addObject(b, opts, .{
2462 .name = "obj1",
2463 .c_source_bytes = "int bar() { return 42; }",
2464 .c_source_flags = &.{
2465 "-fno-unwind-tables",
2466 "-fno-asynchronous-unwind-tables",
2467 },
23232468 });
23242469
2325 const obj2 = addObject(b, "obj2", opts);
2470 const obj2 = addObject(b, opts, .{
2471 .name = "obj2",
2472 });
23262473 obj2.addObject(obj1);
23272474
23282475 const check1 = obj1.checkObject();
......@@ -2343,23 +2490,25 @@ fn testRelocatableNoEhFrame(b: *Build, opts: Options) *Step {
23432490fn testSharedAbsSymbol(b: *Build, opts: Options) *Step {
23442491 const test_step = addTestStep(b, "shared-abs-symbol", opts);
23452492
2346 const dso = addSharedLibrary(b, "a", opts);
2493 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
23472494 addAsmSourceBytes(dso,
23482495 \\.globl foo
23492496 \\foo = 3;
23502497 );
23512498
2352 const obj = addObject(b, "obj", opts);
2353 addCSourceBytes(obj,
2499 const obj = addObject(b, opts, .{
2500 .name = "obj",
2501 .c_source_bytes =
23542502 \\#include <stdio.h>
23552503 \\extern char foo;
23562504 \\int main() { printf("foo=%p\n", &foo); }
2357 , &.{});
2358 obj.force_pic = true;
2505 ,
2506 .pic = true,
2507 });
23592508 obj.linkLibC();
23602509
23612510 {
2362 const exe = addExecutable(b, "main1", opts);
2511 const exe = addExecutable(b, opts, .{ .name = "main1" });
23632512 exe.addObject(obj);
23642513 exe.linkLibrary(dso);
23652514 exe.pie = true;
......@@ -2380,7 +2529,7 @@ fn testSharedAbsSymbol(b: *Build, opts: Options) *Step {
23802529
23812530 // https://github.com/ziglang/zig/issues/17430
23822531 // {
2383 // const exe = addExecutable(b, "main2", opts);
2532 // const exe = addExecutable(b, opts, .{ .name = "main2"});
23842533 // exe.addObject(obj);
23852534 // exe.linkLibrary(dso);
23862535 // exe.pie = false;
......@@ -2405,20 +2554,22 @@ fn testSharedAbsSymbol(b: *Build, opts: Options) *Step {
24052554fn testStrip(b: *Build, opts: Options) *Step {
24062555 const test_step = addTestStep(b, "strip", opts);
24072556
2408 const obj = addObject(b, "obj", opts);
2409 addCSourceBytes(obj,
2557 const obj = addObject(b, opts, .{
2558 .name = "obj",
2559 .c_source_bytes =
24102560 \\#include <stdio.h>
24112561 \\int main() {
24122562 \\ printf("Hello!\n");
24132563 \\ return 0;
24142564 \\}
2415 , &.{});
2565 ,
2566 });
24162567 obj.linkLibC();
24172568
24182569 {
2419 const exe = addExecutable(b, "main1", opts);
2570 const exe = addExecutable(b, opts, .{ .name = "main1" });
24202571 exe.addObject(obj);
2421 exe.strip = false;
2572 exe.root_module.strip = false;
24222573 exe.linkLibC();
24232574
24242575 const check = exe.checkObject();
......@@ -2429,9 +2580,9 @@ fn testStrip(b: *Build, opts: Options) *Step {
24292580 }
24302581
24312582 {
2432 const exe = addExecutable(b, "main2", opts);
2583 const exe = addExecutable(b, opts, .{ .name = "main2" });
24332584 exe.addObject(obj);
2434 exe.strip = true;
2585 exe.root_module.strip = true;
24352586 exe.linkLibC();
24362587
24372588 const check = exe.checkObject();
......@@ -2447,16 +2598,19 @@ fn testStrip(b: *Build, opts: Options) *Step {
24472598fn testTlsDfStaticTls(b: *Build, opts: Options) *Step {
24482599 const test_step = addTestStep(b, "tls-df-static-tls", opts);
24492600
2450 const obj = addObject(b, "obj", opts);
2451 addCSourceBytes(obj,
2601 const obj = addObject(b, opts, .{
2602 .name = "obj",
2603 .c_source_bytes =
24522604 \\static _Thread_local int foo = 5;
24532605 \\void mutate() { ++foo; }
24542606 \\int bar() { return foo; }
2455 , &.{"-ftls-model=initial-exec"});
2456 obj.force_pic = true;
2607 ,
2608 .c_source_flags = &.{"-ftls-model=initial-exec"},
2609 .pic = true,
2610 });
24572611
24582612 {
2459 const dso = addSharedLibrary(b, "a", opts);
2613 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
24602614 dso.addObject(obj);
24612615 // dso.link_relax = true;
24622616
......@@ -2468,7 +2622,7 @@ fn testTlsDfStaticTls(b: *Build, opts: Options) *Step {
24682622
24692623 // TODO add -Wl,--no-relax
24702624 // {
2471 // const dso = addSharedLibrary(b, "a", opts);
2625 // const dso = addSharedLibrary(b, opts, .{ .name = "a"});
24722626 // dso.addObject(obj);
24732627 // dso.link_relax = false;
24742628
......@@ -2484,7 +2638,7 @@ fn testTlsDfStaticTls(b: *Build, opts: Options) *Step {
24842638fn testTlsDso(b: *Build, opts: Options) *Step {
24852639 const test_step = addTestStep(b, "tls-dso", opts);
24862640
2487 const dso = addSharedLibrary(b, "a", opts);
2641 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
24882642 addCSourceBytes(dso,
24892643 \\extern _Thread_local int foo;
24902644 \\_Thread_local int bar;
......@@ -2492,7 +2646,7 @@ fn testTlsDso(b: *Build, opts: Options) *Step {
24922646 \\int get_bar1() { return bar; }
24932647 , &.{});
24942648
2495 const exe = addExecutable(b, "main", opts);
2649 const exe = addExecutable(b, opts, .{ .name = "main" });
24962650 addCSourceBytes(exe,
24972651 \\#include <stdio.h>
24982652 \\_Thread_local int foo;
......@@ -2526,8 +2680,9 @@ fn testTlsDso(b: *Build, opts: Options) *Step {
25262680fn testTlsGd(b: *Build, opts: Options) *Step {
25272681 const test_step = addTestStep(b, "tls-gd", opts);
25282682
2529 const main_o = addObject(b, "main", opts);
2530 addCSourceBytes(main_o,
2683 const main_o = addObject(b, opts, .{
2684 .name = "main",
2685 .c_source_bytes =
25312686 \\#include <stdio.h>
25322687 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x1 = 1;
25332688 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x2;
......@@ -2540,37 +2695,42 @@ fn testTlsGd(b: *Build, opts: Options) *Step {
25402695 \\ printf("%d %d %d %d %d %d\n", x1, x2, x3, x4, get_x5(), get_x6());
25412696 \\ return 0;
25422697 \\}
2543 , &.{});
2698 ,
2699 .pic = true,
2700 });
25442701 main_o.linkLibC();
2545 main_o.force_pic = true;
25462702
2547 const a_o = addObject(b, "a", opts);
2548 addCSourceBytes(a_o,
2703 const a_o = addObject(b, opts, .{
2704 .name = "a",
2705 .c_source_bytes =
25492706 \\__attribute__((tls_model("global-dynamic"))) _Thread_local int x3 = 3;
25502707 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x5 = 5;
25512708 \\int get_x5() { return x5; }
2552 , &.{});
2553 a_o.force_pic = true;
2709 ,
2710 .pic = true,
2711 });
25542712
2555 const b_o = addObject(b, "b", opts);
2556 addCSourceBytes(b_o,
2713 const b_o = addObject(b, opts, .{
2714 .name = "b",
2715 .c_source_bytes =
25572716 \\__attribute__((tls_model("global-dynamic"))) _Thread_local int x4 = 4;
25582717 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x6 = 6;
25592718 \\int get_x6() { return x6; }
2560 , &.{});
2561 b_o.force_pic = true;
2719 ,
2720 .pic = true,
2721 });
25622722
25632723 const exp_stdout = "1 2 3 4 5 6\n";
25642724
2565 const dso1 = addSharedLibrary(b, "a", opts);
2725 const dso1 = addSharedLibrary(b, opts, .{ .name = "a" });
25662726 dso1.addObject(a_o);
25672727
2568 const dso2 = addSharedLibrary(b, "b", opts);
2728 const dso2 = addSharedLibrary(b, opts, .{ .name = "b" });
25692729 dso2.addObject(b_o);
25702730 // dso2.link_relax = false; // TODO
25712731
25722732 {
2573 const exe = addExecutable(b, "main1", opts);
2733 const exe = addExecutable(b, opts, .{ .name = "main1" });
25742734 exe.addObject(main_o);
25752735 exe.linkLibrary(dso1);
25762736 exe.linkLibrary(dso2);
......@@ -2581,7 +2741,7 @@ fn testTlsGd(b: *Build, opts: Options) *Step {
25812741 }
25822742
25832743 {
2584 const exe = addExecutable(b, "main2", opts);
2744 const exe = addExecutable(b, opts, .{ .name = "main2" });
25852745 exe.addObject(main_o);
25862746 // exe.link_relax = false; // TODO
25872747 exe.linkLibrary(dso1);
......@@ -2594,7 +2754,7 @@ fn testTlsGd(b: *Build, opts: Options) *Step {
25942754
25952755 // https://github.com/ziglang/zig/issues/17430 ??
25962756 // {
2597 // const exe = addExecutable(b, "main3", opts);
2757 // const exe = addExecutable(b, opts, .{ .name = "main3"});
25982758 // exe.addObject(main_o);
25992759 // exe.linkLibrary(dso1);
26002760 // exe.linkLibrary(dso2);
......@@ -2606,7 +2766,7 @@ fn testTlsGd(b: *Build, opts: Options) *Step {
26062766 // }
26072767
26082768 // {
2609 // const exe = addExecutable(b, "main4", opts);
2769 // const exe = addExecutable(b, opts, .{ .name = "main4"});
26102770 // exe.addObject(main_o);
26112771 // // exe.link_relax = false; // TODO
26122772 // exe.linkLibrary(dso1);
......@@ -2624,8 +2784,9 @@ fn testTlsGd(b: *Build, opts: Options) *Step {
26242784fn testTlsGdNoPlt(b: *Build, opts: Options) *Step {
26252785 const test_step = addTestStep(b, "tls-gd-no-plt", opts);
26262786
2627 const obj = addObject(b, "obj", opts);
2628 addCSourceBytes(obj,
2787 const obj = addObject(b, opts, .{
2788 .name = "obj",
2789 .c_source_bytes =
26292790 \\#include <stdio.h>
26302791 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x1 = 1;
26312792 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x2;
......@@ -2639,18 +2800,20 @@ fn testTlsGdNoPlt(b: *Build, opts: Options) *Step {
26392800 \\ printf("%d %d %d %d %d %d\n", x1, x2, x3, x4, get_x5(), get_x6());
26402801 \\ return 0;
26412802 \\}
2642 , &.{"-fno-plt"});
2643 obj.force_pic = true;
2803 ,
2804 .c_source_flags = &.{"-fno-plt"},
2805 .pic = true,
2806 });
26442807 obj.linkLibC();
26452808
2646 const a_so = addSharedLibrary(b, "a", opts);
2809 const a_so = addSharedLibrary(b, opts, .{ .name = "a" });
26472810 addCSourceBytes(a_so,
26482811 \\__attribute__((tls_model("global-dynamic"))) _Thread_local int x3 = 3;
26492812 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x5 = 5;
26502813 \\int get_x5() { return x5; }
26512814 , &.{"-fno-plt"});
26522815
2653 const b_so = addSharedLibrary(b, "b", opts);
2816 const b_so = addSharedLibrary(b, opts, .{ .name = "b" });
26542817 addCSourceBytes(b_so,
26552818 \\__attribute__((tls_model("global-dynamic"))) _Thread_local int x4 = 4;
26562819 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x6 = 6;
......@@ -2659,7 +2822,7 @@ fn testTlsGdNoPlt(b: *Build, opts: Options) *Step {
26592822 // b_so.link_relax = false; // TODO
26602823
26612824 {
2662 const exe = addExecutable(b, "main1", opts);
2825 const exe = addExecutable(b, opts, .{ .name = "main1" });
26632826 exe.addObject(obj);
26642827 exe.linkLibrary(a_so);
26652828 exe.linkLibrary(b_so);
......@@ -2673,7 +2836,7 @@ fn testTlsGdNoPlt(b: *Build, opts: Options) *Step {
26732836 }
26742837
26752838 {
2676 const exe = addExecutable(b, "main2", opts);
2839 const exe = addExecutable(b, opts, .{ .name = "main2" });
26772840 exe.addObject(obj);
26782841 exe.linkLibrary(a_so);
26792842 exe.linkLibrary(b_so);
......@@ -2693,8 +2856,9 @@ fn testTlsGdNoPlt(b: *Build, opts: Options) *Step {
26932856fn testTlsGdToIe(b: *Build, opts: Options) *Step {
26942857 const test_step = addTestStep(b, "tls-gd-to-ie", opts);
26952858
2696 const a_o = addObject(b, "a", opts);
2697 addCSourceBytes(a_o,
2859 const a_o = addObject(b, opts, .{
2860 .name = "a",
2861 .c_source_bytes =
26982862 \\#include <stdio.h>
26992863 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x1 = 1;
27002864 \\__attribute__((tls_model("global-dynamic"))) _Thread_local int x2 = 2;
......@@ -2705,22 +2869,25 @@ fn testTlsGdToIe(b: *Build, opts: Options) *Step {
27052869 \\ printf("%d %d %d\n", x1, x2, x3);
27062870 \\ return 0;
27072871 \\}
2708 , &.{});
2872 ,
2873 .pic = true,
2874 });
27092875 a_o.linkLibC();
2710 a_o.force_pic = true;
27112876
2712 const b_o = addObject(b, "b", opts);
2713 addCSourceBytes(b_o,
2877 const b_o = addObject(b, opts, .{
2878 .name = "b",
2879 .c_source_bytes =
27142880 \\int foo();
27152881 \\int main() { foo(); }
2716 , &.{});
2717 b_o.force_pic = true;
2882 ,
2883 .pic = true,
2884 });
27182885
27192886 {
2720 const dso = addSharedLibrary(b, "a1", opts);
2887 const dso = addSharedLibrary(b, opts, .{ .name = "a1" });
27212888 dso.addObject(a_o);
27222889
2723 const exe = addExecutable(b, "main1", opts);
2890 const exe = addExecutable(b, opts, .{ .name = "main1" });
27242891 exe.addObject(b_o);
27252892 exe.linkLibrary(dso);
27262893 exe.linkLibC();
......@@ -2733,11 +2900,11 @@ fn testTlsGdToIe(b: *Build, opts: Options) *Step {
27332900 }
27342901
27352902 {
2736 const dso = addSharedLibrary(b, "a2", opts);
2903 const dso = addSharedLibrary(b, opts, .{ .name = "a2" });
27372904 dso.addObject(a_o);
27382905 // dso.link_relax = false; // TODO
27392906
2740 const exe = addExecutable(b, "main2", opts);
2907 const exe = addExecutable(b, opts, .{ .name = "main2" });
27412908 exe.addObject(b_o);
27422909 exe.linkLibrary(dso);
27432910 exe.linkLibC();
......@@ -2750,11 +2917,11 @@ fn testTlsGdToIe(b: *Build, opts: Options) *Step {
27502917 }
27512918
27522919 // {
2753 // const dso = addSharedLibrary(b, "a", opts);
2920 // const dso = addSharedLibrary(b, opts, .{ .name = "a"});
27542921 // dso.addObject(a_o);
27552922 // dso.link_z_nodlopen = true;
27562923
2757 // const exe = addExecutable(b, "main", opts);
2924 // const exe = addExecutable(b, opts, .{ .name = "main"});
27582925 // exe.addObject(b_o);
27592926 // exe.linkLibrary(dso);
27602927
......@@ -2764,12 +2931,12 @@ fn testTlsGdToIe(b: *Build, opts: Options) *Step {
27642931 // }
27652932
27662933 // {
2767 // const dso = addSharedLibrary(b, "a", opts);
2934 // const dso = addSharedLibrary(b, opts, .{ .name = "a"});
27682935 // dso.addObject(a_o);
27692936 // dso.link_relax = false;
27702937 // dso.link_z_nodlopen = true;
27712938
2772 // const exe = addExecutable(b, "main", opts);
2939 // const exe = addExecutable(b, opts, .{ .name = "main"});
27732940 // exe.addObject(b_o);
27742941 // exe.linkLibrary(dso);
27752942
......@@ -2784,7 +2951,7 @@ fn testTlsGdToIe(b: *Build, opts: Options) *Step {
27842951fn testTlsIe(b: *Build, opts: Options) *Step {
27852952 const test_step = addTestStep(b, "tls-ie", opts);
27862953
2787 const dso = addSharedLibrary(b, "a", opts);
2954 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
27882955 addCSourceBytes(dso,
27892956 \\#include <stdio.h>
27902957 \\__attribute__((tls_model("initial-exec"))) static _Thread_local int foo;
......@@ -2799,8 +2966,9 @@ fn testTlsIe(b: *Build, opts: Options) *Step {
27992966 , &.{});
28002967 dso.linkLibC();
28012968
2802 const main_o = addObject(b, "main", opts);
2803 addCSourceBytes(main_o,
2969 const main_o = addObject(b, opts, .{
2970 .name = "main",
2971 .c_source_bytes =
28042972 \\#include <stdio.h>
28052973 \\_Thread_local int baz;
28062974 \\void set();
......@@ -2812,13 +2980,14 @@ fn testTlsIe(b: *Build, opts: Options) *Step {
28122980 \\ print();
28132981 \\ printf("%d\n", baz);
28142982 \\}
2815 , &.{});
2983 ,
2984 });
28162985 main_o.linkLibC();
28172986
28182987 const exp_stdout = "0 0 3 5 7\n";
28192988
28202989 {
2821 const exe = addExecutable(b, "main1", opts);
2990 const exe = addExecutable(b, opts, .{ .name = "main1" });
28222991 exe.addObject(main_o);
28232992 exe.linkLibrary(dso);
28242993 exe.linkLibC();
......@@ -2831,7 +3000,7 @@ fn testTlsIe(b: *Build, opts: Options) *Step {
28313000 }
28323001
28333002 {
2834 const exe = addExecutable(b, "main2", opts);
3003 const exe = addExecutable(b, opts, .{ .name = "main2" });
28353004 exe.addObject(main_o);
28363005 exe.linkLibrary(dso);
28373006 exe.linkLibC();
......@@ -2850,38 +3019,46 @@ fn testTlsIe(b: *Build, opts: Options) *Step {
28503019fn testTlsLargeAlignment(b: *Build, opts: Options) *Step {
28513020 const test_step = addTestStep(b, "tls-large-alignment", opts);
28523021
2853 const a_o = addObject(b, "a", opts);
2854 addCSourceBytes(a_o,
3022 const a_o = addObject(b, opts, .{
3023 .name = "a",
3024 .c_source_bytes =
28553025 \\__attribute__((section(".tdata1")))
28563026 \\_Thread_local int x = 42;
2857 , &.{"-std=c11"});
2858 a_o.force_pic = true;
3027 ,
3028 .c_source_flags = &.{"-std=c11"},
3029 .pic = true,
3030 });
28593031
2860 const b_o = addObject(b, "b", opts);
2861 addCSourceBytes(b_o,
3032 const b_o = addObject(b, opts, .{
3033 .name = "b",
3034 .c_source_bytes =
28623035 \\__attribute__((section(".tdata2")))
28633036 \\_Alignas(256) _Thread_local int y[] = { 1, 2, 3 };
2864 , &.{"-std=c11"});
2865 b_o.force_pic = true;
3037 ,
3038 .c_source_flags = &.{"-std=c11"},
3039 .pic = true,
3040 });
28663041
2867 const c_o = addObject(b, "c", opts);
2868 addCSourceBytes(c_o,
3042 const c_o = addObject(b, opts, .{
3043 .name = "c",
3044 .c_source_bytes =
28693045 \\#include <stdio.h>
28703046 \\extern _Thread_local int x;
28713047 \\extern _Thread_local int y[];
28723048 \\int main() {
28733049 \\ printf("%d %d %d %d\n", x, y[0], y[1], y[2]);
28743050 \\}
2875 , &.{});
2876 c_o.force_pic = true;
3051 ,
3052 .pic = true,
3053 });
28773054 c_o.linkLibC();
28783055
28793056 {
2880 const dso = addSharedLibrary(b, "a", opts);
3057 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
28813058 dso.addObject(a_o);
28823059 dso.addObject(b_o);
28833060
2884 const exe = addExecutable(b, "main", opts);
3061 const exe = addExecutable(b, opts, .{ .name = "main" });
28853062 exe.addObject(c_o);
28863063 exe.linkLibrary(dso);
28873064 exe.linkLibC();
......@@ -2894,7 +3071,7 @@ fn testTlsLargeAlignment(b: *Build, opts: Options) *Step {
28943071 }
28953072
28963073 {
2897 const exe = addExecutable(b, "main", opts);
3074 const exe = addExecutable(b, opts, .{ .name = "main" });
28983075 exe.addObject(a_o);
28993076 exe.addObject(b_o);
29003077 exe.addObject(c_o);
......@@ -2913,7 +3090,7 @@ fn testTlsLargeAlignment(b: *Build, opts: Options) *Step {
29133090fn testTlsLargeTbss(b: *Build, opts: Options) *Step {
29143091 const test_step = addTestStep(b, "tls-large-tbss", opts);
29153092
2916 const exe = addExecutable(b, "main", opts);
3093 const exe = addExecutable(b, opts, .{ .name = "main" });
29173094 addAsmSourceBytes(exe,
29183095 \\.globl x, y
29193096 \\.section .tbss,"awT",@nobits
......@@ -2947,7 +3124,7 @@ fn testTlsLargeTbss(b: *Build, opts: Options) *Step {
29473124fn testTlsLargeStaticImage(b: *Build, opts: Options) *Step {
29483125 const test_step = addTestStep(b, "tls-large-static-image", opts);
29493126
2950 const exe = addExecutable(b, "main", opts);
3127 const exe = addExecutable(b, opts, .{ .name = "main" });
29513128 addCSourceBytes(exe, "_Thread_local int x[] = { 1, 2, 3, [10000] = 5 };", &.{});
29523129 addCSourceBytes(exe,
29533130 \\#include <stdio.h>
......@@ -2956,7 +3133,7 @@ fn testTlsLargeStaticImage(b: *Build, opts: Options) *Step {
29563133 \\ printf("%d %d %d %d %d\n", x[0], x[1], x[2], x[3], x[10000]);
29573134 \\}
29583135 , &.{});
2959 exe.force_pic = true;
3136 exe.root_module.pic = true;
29603137 exe.linkLibC();
29613138 // https://github.com/ziglang/zig/issues/17619
29623139 exe.pie = true;
......@@ -2971,8 +3148,9 @@ fn testTlsLargeStaticImage(b: *Build, opts: Options) *Step {
29713148fn testTlsLd(b: *Build, opts: Options) *Step {
29723149 const test_step = addTestStep(b, "tls-ld", opts);
29733150
2974 const main_o = addObject(b, "main", opts);
2975 addCSourceBytes(main_o,
3151 const main_o = addObject(b, opts, .{
3152 .name = "main",
3153 .c_source_bytes =
29763154 \\#include <stdio.h>
29773155 \\extern _Thread_local int foo;
29783156 \\static _Thread_local int bar;
......@@ -2983,18 +3161,23 @@ fn testTlsLd(b: *Build, opts: Options) *Step {
29833161 \\ printf("%d %d %d %d\n", *get_foo_addr(), *get_bar_addr(), foo, bar);
29843162 \\ return 0;
29853163 \\}
2986 , &.{"-ftls-model=local-dynamic"});
2987 main_o.force_pic = true;
3164 ,
3165 .c_source_flags = &.{"-ftls-model=local-dynamic"},
3166 .pic = true,
3167 });
29883168 main_o.linkLibC();
29893169
2990 const a_o = addObject(b, "a", opts);
2991 addCSourceBytes(a_o, "_Thread_local int foo = 3;", &.{"-ftls-model=local-dynamic"});
2992 a_o.force_pic = true;
3170 const a_o = addObject(b, opts, .{
3171 .name = "a",
3172 .c_source_bytes = "_Thread_local int foo = 3;",
3173 .c_source_flags = &.{"-ftls-model=local-dynamic"},
3174 .pic = true,
3175 });
29933176
29943177 const exp_stdout = "3 5 3 5\n";
29953178
29963179 {
2997 const exe = addExecutable(b, "main1", opts);
3180 const exe = addExecutable(b, opts, .{ .name = "main1" });
29983181 exe.addObject(main_o);
29993182 exe.addObject(a_o);
30003183 exe.linkLibC();
......@@ -3007,7 +3190,7 @@ fn testTlsLd(b: *Build, opts: Options) *Step {
30073190 }
30083191
30093192 {
3010 const exe = addExecutable(b, "main2", opts);
3193 const exe = addExecutable(b, opts, .{ .name = "main2" });
30113194 exe.addObject(main_o);
30123195 exe.addObject(a_o);
30133196 exe.linkLibC();
......@@ -3026,14 +3209,14 @@ fn testTlsLd(b: *Build, opts: Options) *Step {
30263209fn testTlsLdDso(b: *Build, opts: Options) *Step {
30273210 const test_step = addTestStep(b, "tls-ld-dso", opts);
30283211
3029 const dso = addSharedLibrary(b, "a", opts);
3212 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
30303213 addCSourceBytes(dso,
30313214 \\static _Thread_local int def, def1;
30323215 \\int f0() { return ++def; }
30333216 \\int f1() { return ++def1 + def; }
30343217 , &.{"-ftls-model=local-dynamic"});
30353218
3036 const exe = addExecutable(b, "main", opts);
3219 const exe = addExecutable(b, opts, .{ .name = "main" });
30373220 addCSourceBytes(exe,
30383221 \\#include <stdio.h>
30393222 \\extern int f0();
......@@ -3060,8 +3243,9 @@ fn testTlsLdDso(b: *Build, opts: Options) *Step {
30603243fn testTlsLdNoPlt(b: *Build, opts: Options) *Step {
30613244 const test_step = addTestStep(b, "tls-ld-no-plt", opts);
30623245
3063 const a_o = addObject(b, "a", opts);
3064 addCSourceBytes(a_o,
3246 const a_o = addObject(b, opts, .{
3247 .name = "a",
3248 .c_source_bytes =
30653249 \\#include <stdio.h>
30663250 \\extern _Thread_local int foo;
30673251 \\static _Thread_local int bar;
......@@ -3073,16 +3257,21 @@ fn testTlsLdNoPlt(b: *Build, opts: Options) *Step {
30733257 \\ printf("%d %d %d %d\n", *get_foo_addr(), *get_bar_addr(), foo, bar);
30743258 \\ return 0;
30753259 \\}
3076 , &.{ "-ftls-model=local-dynamic", "-fno-plt" });
3260 ,
3261 .c_source_flags = &.{ "-ftls-model=local-dynamic", "-fno-plt" },
3262 .pic = true,
3263 });
30773264 a_o.linkLibC();
3078 a_o.force_pic = true;
30793265
3080 const b_o = addObject(b, "b", opts);
3081 addCSourceBytes(b_o, "_Thread_local int foo = 3;", &.{ "-ftls-model=local-dynamic", "-fno-plt" });
3082 b_o.force_pic = true;
3266 const b_o = addObject(b, opts, .{
3267 .name = "b",
3268 .c_source_bytes = "_Thread_local int foo = 3;",
3269 .c_source_flags = &.{ "-ftls-model=local-dynamic", "-fno-plt" },
3270 .pic = true,
3271 });
30833272
30843273 {
3085 const exe = addExecutable(b, "main1", opts);
3274 const exe = addExecutable(b, opts, .{ .name = "main1" });
30863275 exe.addObject(a_o);
30873276 exe.addObject(b_o);
30883277 exe.linkLibC();
......@@ -3095,7 +3284,7 @@ fn testTlsLdNoPlt(b: *Build, opts: Options) *Step {
30953284 }
30963285
30973286 {
3098 const exe = addExecutable(b, "main2", opts);
3287 const exe = addExecutable(b, opts, .{ .name = "main2" });
30993288 exe.addObject(a_o);
31003289 exe.addObject(b_o);
31013290 exe.linkLibC();
......@@ -3114,7 +3303,7 @@ fn testTlsLdNoPlt(b: *Build, opts: Options) *Step {
31143303fn testTlsNoPic(b: *Build, opts: Options) *Step {
31153304 const test_step = addTestStep(b, "tls-no-pic", opts);
31163305
3117 const exe = addExecutable(b, "main", opts);
3306 const exe = addExecutable(b, opts, .{ .name = "main" });
31183307 addCSourceBytes(exe,
31193308 \\#include <stdio.h>
31203309 \\__attribute__((tls_model("global-dynamic"))) extern _Thread_local int foo;
......@@ -3132,7 +3321,7 @@ fn testTlsNoPic(b: *Build, opts: Options) *Step {
31323321 addCSourceBytes(exe,
31333322 \\__attribute__((tls_model("global-dynamic"))) _Thread_local int foo;
31343323 , &.{});
3135 exe.force_pic = false;
3324 exe.root_module.pic = false;
31363325 exe.linkLibC();
31373326
31383327 const run = addRunArtifact(exe);
......@@ -3145,7 +3334,7 @@ fn testTlsNoPic(b: *Build, opts: Options) *Step {
31453334fn testTlsOffsetAlignment(b: *Build, opts: Options) *Step {
31463335 const test_step = addTestStep(b, "tls-offset-alignment", opts);
31473336
3148 const dso = addSharedLibrary(b, "a", opts);
3337 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
31493338 addCSourceBytes(dso,
31503339 \\#include <assert.h>
31513340 \\#include <stdlib.h>
......@@ -3163,7 +3352,7 @@ fn testTlsOffsetAlignment(b: *Build, opts: Options) *Step {
31633352 , &.{});
31643353 dso.linkLibC();
31653354
3166 const exe = addExecutable(b, "main", opts);
3355 const exe = addExecutable(b, opts, .{ .name = "main" });
31673356 addCSourceBytes(exe,
31683357 \\#include <pthread.h>
31693358 \\#include <dlfcn.h>
......@@ -3186,7 +3375,7 @@ fn testTlsOffsetAlignment(b: *Build, opts: Options) *Step {
31863375 , &.{});
31873376 exe.addRPath(dso.getEmittedBinDirectory());
31883377 exe.linkLibC();
3189 exe.force_pic = true;
3378 exe.root_module.pic = true;
31903379 // https://github.com/ziglang/zig/issues/17619
31913380 exe.pie = true;
31923381
......@@ -3200,8 +3389,9 @@ fn testTlsOffsetAlignment(b: *Build, opts: Options) *Step {
32003389fn testTlsPic(b: *Build, opts: Options) *Step {
32013390 const test_step = addTestStep(b, "tls-pic", opts);
32023391
3203 const obj = addObject(b, "obj", opts);
3204 addCSourceBytes(obj,
3392 const obj = addObject(b, opts, .{
3393 .name = "obj",
3394 .c_source_bytes =
32053395 \\#include <stdio.h>
32063396 \\__attribute__((tls_model("global-dynamic"))) extern _Thread_local int foo;
32073397 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int bar;
......@@ -3213,11 +3403,12 @@ fn testTlsPic(b: *Build, opts: Options) *Step {
32133403 \\ printf("%d %d %d %d\n", *get_foo_addr(), *get_bar_addr(), foo, bar);
32143404 \\ return 0;
32153405 \\}
3216 , &.{});
3406 ,
3407 .pic = true,
3408 });
32173409 obj.linkLibC();
3218 obj.force_pic = true;
32193410
3220 const exe = addExecutable(b, "main", opts);
3411 const exe = addExecutable(b, opts, .{ .name = "main" });
32213412 addCSourceBytes(exe,
32223413 \\__attribute__((tls_model("global-dynamic"))) _Thread_local int foo = 3;
32233414 , &.{});
......@@ -3236,30 +3427,38 @@ fn testTlsPic(b: *Build, opts: Options) *Step {
32363427fn testTlsSmallAlignment(b: *Build, opts: Options) *Step {
32373428 const test_step = addTestStep(b, "tls-small-alignment", opts);
32383429
3239 const a_o = addObject(b, "a", opts);
3240 addAsmSourceBytes(a_o,
3430 const a_o = addObject(b, opts, .{
3431 .name = "a",
3432 .asm_source_bytes =
32413433 \\.text
32423434 \\.byte 0
3243 );
3244 a_o.force_pic = true;
3435 \\
3436 ,
3437 .pic = true,
3438 });
32453439
3246 const b_o = addObject(b, "b", opts);
3247 addCSourceBytes(b_o, "_Thread_local char x = 42;", &.{"-std=c11"});
3248 b_o.force_pic = true;
3440 const b_o = addObject(b, opts, .{
3441 .name = "b",
3442 .c_source_bytes = "_Thread_local char x = 42;",
3443 .c_source_flags = &.{"-std=c11"},
3444 .pic = true,
3445 });
32493446
3250 const c_o = addObject(b, "c", opts);
3251 addCSourceBytes(c_o,
3447 const c_o = addObject(b, opts, .{
3448 .name = "c",
3449 .c_source_bytes =
32523450 \\#include <stdio.h>
32533451 \\extern _Thread_local char x;
32543452 \\int main() {
32553453 \\ printf("%d\n", x);
32563454 \\}
3257 , &.{});
3455 ,
3456 .pic = true,
3457 });
32583458 c_o.linkLibC();
3259 c_o.force_pic = true;
32603459
32613460 {
3262 const exe = addExecutable(b, "main", opts);
3461 const exe = addExecutable(b, opts, .{ .name = "main" });
32633462 exe.addObject(a_o);
32643463 exe.addObject(b_o);
32653464 exe.addObject(c_o);
......@@ -3273,11 +3472,11 @@ fn testTlsSmallAlignment(b: *Build, opts: Options) *Step {
32733472 }
32743473
32753474 {
3276 const dso = addSharedLibrary(b, "a", opts);
3475 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
32773476 dso.addObject(a_o);
32783477 dso.addObject(b_o);
32793478
3280 const exe = addExecutable(b, "main", opts);
3479 const exe = addExecutable(b, opts, .{ .name = "main" });
32813480 exe.addObject(c_o);
32823481 exe.linkLibrary(dso);
32833482 exe.linkLibC();
......@@ -3295,7 +3494,7 @@ fn testTlsSmallAlignment(b: *Build, opts: Options) *Step {
32953494fn testTlsStatic(b: *Build, opts: Options) *Step {
32963495 const test_step = addTestStep(b, "tls-static", opts);
32973496
3298 const exe = addExecutable(b, "test", opts);
3497 const exe = addExecutable(b, opts, .{ .name = "test" });
32993498 addCSourceBytes(exe,
33003499 \\#include <stdio.h>
33013500 \\_Thread_local int a = 10;
......@@ -3326,12 +3525,14 @@ fn testTlsStatic(b: *Build, opts: Options) *Step {
33263525fn testUnknownFileTypeError(b: *Build, opts: Options) *Step {
33273526 const test_step = addTestStep(b, "unknown-file-type-error", opts);
33283527
3329 const dylib = addSharedLibrary(b, "a", .{
3330 .target = .{ .cpu_arch = .x86_64, .os_tag = .macos },
3528 const dylib = addSharedLibrary(b, .{
3529 .target = b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .macos }),
3530 }, .{
3531 .name = "a",
3532 .zig_source_bytes = "export var foo: i32 = 0;",
33313533 });
3332 addZigSourceBytes(dylib, "export var foo: i32 = 0;");
33333534
3334 const exe = addExecutable(b, "main", opts);
3535 const exe = addExecutable(b, opts, .{ .name = "main" });
33353536 addCSourceBytes(exe,
33363537 \\extern int foo;
33373538 \\int main() {
......@@ -3354,28 +3555,34 @@ fn testUnknownFileTypeError(b: *Build, opts: Options) *Step {
33543555fn testUnresolvedError(b: *Build, opts: Options) *Step {
33553556 const test_step = addTestStep(b, "unresolved-error", opts);
33563557
3357 const obj1 = addObject(b, "a", opts);
3358 addCSourceBytes(obj1,
3558 const obj1 = addObject(b, opts, .{
3559 .name = "a",
3560 .c_source_bytes =
33593561 \\#include <stdio.h>
33603562 \\int foo();
33613563 \\int bar() {
33623564 \\ return foo() + 1;
33633565 \\}
3364 , &.{"-ffunction-sections"});
3566 ,
3567 .c_source_flags = &.{"-ffunction-sections"},
3568 });
33653569 obj1.linkLibC();
33663570
3367 const obj2 = addObject(b, "b", opts);
3368 addCSourceBytes(obj2,
3571 const obj2 = addObject(b, opts, .{
3572 .name = "b",
3573 .c_source_bytes =
33693574 \\#include <stdio.h>
33703575 \\int foo();
33713576 \\int bar();
33723577 \\int main() {
33733578 \\ return foo() + bar();
33743579 \\}
3375 , &.{"-ffunction-sections"});
3580 ,
3581 .c_source_flags = &.{"-ffunction-sections"},
3582 });
33763583 obj2.linkLibC();
33773584
3378 const exe = addExecutable(b, "main", opts);
3585 const exe = addExecutable(b, opts, .{ .name = "main" });
33793586 exe.addObject(obj1);
33803587 exe.addObject(obj2);
33813588 exe.linkLibC();
......@@ -3392,19 +3599,21 @@ fn testUnresolvedError(b: *Build, opts: Options) *Step {
33923599fn testWeakExports(b: *Build, opts: Options) *Step {
33933600 const test_step = addTestStep(b, "weak-exports", opts);
33943601
3395 const obj = addObject(b, "obj", opts);
3396 addCSourceBytes(obj,
3602 const obj = addObject(b, opts, .{
3603 .name = "obj",
3604 .c_source_bytes =
33973605 \\#include <stdio.h>
33983606 \\__attribute__((weak)) int foo();
33993607 \\int main() {
34003608 \\ printf("%d\n", foo ? foo() : 3);
34013609 \\}
3402 , &.{});
3610 ,
3611 .pic = true,
3612 });
34033613 obj.linkLibC();
3404 obj.force_pic = true;
34053614
34063615 {
3407 const dso = addSharedLibrary(b, "a", opts);
3616 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
34083617 dso.addObject(obj);
34093618 dso.linkLibC();
34103619
......@@ -3415,7 +3624,7 @@ fn testWeakExports(b: *Build, opts: Options) *Step {
34153624 }
34163625
34173626 {
3418 const exe = addExecutable(b, "main", opts);
3627 const exe = addExecutable(b, opts, .{ .name = "main" });
34193628 exe.addObject(obj);
34203629 exe.linkLibC();
34213630 // https://github.com/ziglang/zig/issues/17619
......@@ -3437,14 +3646,14 @@ fn testWeakExports(b: *Build, opts: Options) *Step {
34373646fn testWeakUndefsDso(b: *Build, opts: Options) *Step {
34383647 const test_step = addTestStep(b, "weak-undef-dso", opts);
34393648
3440 const dso = addSharedLibrary(b, "a", opts);
3649 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
34413650 addCSourceBytes(dso,
34423651 \\__attribute__((weak)) int foo();
34433652 \\int bar() { return foo ? foo() : -1; }
34443653 , &.{});
34453654
34463655 {
3447 const exe = addExecutable(b, "main", opts);
3656 const exe = addExecutable(b, opts, .{ .name = "main" });
34483657 addCSourceBytes(exe,
34493658 \\#include <stdio.h>
34503659 \\int bar();
......@@ -3461,7 +3670,7 @@ fn testWeakUndefsDso(b: *Build, opts: Options) *Step {
34613670 }
34623671
34633672 {
3464 const exe = addExecutable(b, "main", opts);
3673 const exe = addExecutable(b, opts, .{ .name = "main" });
34653674 addCSourceBytes(exe,
34663675 \\#include <stdio.h>
34673676 \\int foo() { return 5; }
......@@ -3484,12 +3693,14 @@ fn testWeakUndefsDso(b: *Build, opts: Options) *Step {
34843693fn testZNow(b: *Build, opts: Options) *Step {
34853694 const test_step = addTestStep(b, "z-now", opts);
34863695
3487 const obj = addObject(b, "obj", opts);
3488 addCSourceBytes(obj, "int main() { return 0; }", &.{});
3489 obj.force_pic = true;
3696 const obj = addObject(b, opts, .{
3697 .name = "obj",
3698 .c_source_bytes = "int main() { return 0; }",
3699 .pic = true,
3700 });
34903701
34913702 {
3492 const dso = addSharedLibrary(b, "a", opts);
3703 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
34933704 dso.addObject(obj);
34943705
34953706 const check = dso.checkObject();
......@@ -3499,7 +3710,7 @@ fn testZNow(b: *Build, opts: Options) *Step {
34993710 }
35003711
35013712 {
3502 const dso = addSharedLibrary(b, "a", opts);
3713 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
35033714 dso.addObject(obj);
35043715 dso.link_z_lazy = true;
35053716
......@@ -3515,7 +3726,7 @@ fn testZNow(b: *Build, opts: Options) *Step {
35153726fn testZStackSize(b: *Build, opts: Options) *Step {
35163727 const test_step = addTestStep(b, "z-stack-size", opts);
35173728
3518 const exe = addExecutable(b, "main", opts);
3729 const exe = addExecutable(b, opts, .{ .name = "main" });
35193730 addCSourceBytes(exe, "int main() { return 0; }", &.{});
35203731 exe.stack_size = 0x800000;
35213732 exe.linkLibC();
......@@ -3540,8 +3751,9 @@ fn testZText(b: *Build, opts: Options) *Step {
35403751 // musl supports only a very limited number of text relocations and only in DSOs (and
35413752 // rightly so!).
35423753
3543 const a_o = addObject(b, "a", opts);
3544 addAsmSourceBytes(a_o,
3754 const a_o = addObject(b, opts, .{
3755 .name = "a",
3756 .asm_source_bytes =
35453757 \\.globl fn1
35463758 \\fn1:
35473759 \\ sub $8, %rsp
......@@ -3549,10 +3761,13 @@ fn testZText(b: *Build, opts: Options) *Step {
35493761 \\ call *%rax
35503762 \\ add $8, %rsp
35513763 \\ ret
3552 );
3764 \\
3765 ,
3766 });
35533767
3554 const b_o = addObject(b, "b", opts);
3555 addCSourceBytes(b_o,
3768 const b_o = addObject(b, opts, .{
3769 .name = "b",
3770 .c_source_bytes =
35563771 \\int fn1();
35573772 \\int fn2() {
35583773 \\ return 3;
......@@ -3561,15 +3776,16 @@ fn testZText(b: *Build, opts: Options) *Step {
35613776 \\int fnn() {
35623777 \\ return fn1();
35633778 \\}
3564 , &.{});
3565 b_o.force_pic = true;
3779 ,
3780 .pic = true,
3781 });
35663782
3567 const dso = addSharedLibrary(b, "a", opts);
3783 const dso = addSharedLibrary(b, opts, .{ .name = "a" });
35683784 dso.addObject(a_o);
35693785 dso.addObject(b_o);
35703786 dso.link_z_notext = true;
35713787
3572 const exe = addExecutable(b, "main", opts);
3788 const exe = addExecutable(b, opts, .{ .name = "main" });
35733789 addCSourceBytes(exe,
35743790 \\#include <stdio.h>
35753791 \\int fnn();
......@@ -3608,7 +3824,6 @@ const addObject = link.addObject;
36083824const addRunArtifact = link.addRunArtifact;
36093825const addSharedLibrary = link.addSharedLibrary;
36103826const addStaticLibrary = link.addStaticLibrary;
3611const addZigSourceBytes = link.addZigSourceBytes;
36123827const expectLinkErrors = link.expectLinkErrors;
36133828const link = @import("link.zig");
36143829const std = @import("std");
test/link/glibc_compat/build.zig+2-2
......@@ -8,9 +8,9 @@ pub fn build(b: *std.Build) void {
88 const exe = b.addExecutable(.{
99 .name = t,
1010 .root_source_file = .{ .path = "main.c" },
11 .target = std.zig.CrossTarget.parse(
11 .target = b.resolveTargetQuery(std.zig.CrossTarget.parse(
1212 .{ .arch_os_abi = t },
13 ) catch unreachable,
13 ) catch unreachable),
1414 });
1515 exe.linkLibC();
1616 // TODO: actually test the output
test/link/interdependent_static_c_libs/build.zig+2-2
......@@ -14,7 +14,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1414 const lib_a = b.addStaticLibrary(.{
1515 .name = "a",
1616 .optimize = optimize,
17 .target = .{},
17 .target = b.host,
1818 });
1919 lib_a.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &[_][]const u8{} });
2020 lib_a.addIncludePath(.{ .path = "." });
......@@ -22,7 +22,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2222 const lib_b = b.addStaticLibrary(.{
2323 .name = "b",
2424 .optimize = optimize,
25 .target = .{},
25 .target = b.host,
2626 });
2727 lib_b.addCSourceFile(.{ .file = .{ .path = "b.c" }, .flags = &[_][]const u8{} });
2828 lib_b.addIncludePath(.{ .path = "." });
test/link/link.zig+133-42
......@@ -7,62 +7,160 @@ pub fn build(b: *Build) void {
77}
88
99pub const Options = struct {
10 target: CrossTarget,
10 target: std.Build.ResolvedTarget,
1111 optimize: std.builtin.OptimizeMode = .Debug,
1212 use_llvm: bool = true,
1313 use_lld: bool = false,
1414};
1515
16pub fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
17 const target = opts.target.zigTriple(b.allocator) catch @panic("OOM");
16pub fn addTestStep(b: *Build, prefix: []const u8, opts: Options) *Step {
17 const target = opts.target.target.zigTriple(b.allocator) catch @panic("OOM");
1818 const optimize = @tagName(opts.optimize);
1919 const use_llvm = if (opts.use_llvm) "llvm" else "no-llvm";
20 const name = std.fmt.allocPrint(b.allocator, "test-" ++ prefix ++ "-{s}-{s}-{s}", .{
21 target,
22 optimize,
23 use_llvm,
20 const name = std.fmt.allocPrint(b.allocator, "test-{s}-{s}-{s}-{s}", .{
21 prefix, target, optimize, use_llvm,
2422 }) catch @panic("OOM");
2523 return b.step(name, "");
2624}
2725
28pub fn addExecutable(b: *Build, name: []const u8, opts: Options) *Compile {
29 return b.addExecutable(.{
30 .name = name,
31 .target = opts.target,
32 .optimize = opts.optimize,
33 .use_llvm = opts.use_llvm,
34 .use_lld = opts.use_lld,
26const OverlayOptions = struct {
27 name: []const u8,
28 asm_source_bytes: ?[]const u8 = null,
29 c_source_bytes: ?[]const u8 = null,
30 c_source_flags: []const []const u8 = &.{},
31 cpp_source_bytes: ?[]const u8 = null,
32 cpp_source_flags: []const []const u8 = &.{},
33 zig_source_bytes: ?[]const u8 = null,
34 pic: ?bool = null,
35 strip: ?bool = null,
36};
37
38pub fn addExecutable(b: *std.Build, base: Options, overlay: OverlayOptions) *Step.Compile {
39 const compile_step = b.addExecutable(.{
40 .name = overlay.name,
41 .root_source_file = rsf: {
42 const bytes = overlay.zig_source_bytes orelse break :rsf null;
43 break :rsf b.addWriteFiles().add("a.zig", bytes);
44 },
45 .target = base.target,
46 .optimize = base.optimize,
47 .use_llvm = base.use_llvm,
48 .use_lld = base.use_lld,
49 .pic = overlay.pic,
50 .strip = overlay.strip,
3551 });
52 if (overlay.cpp_source_bytes) |bytes| {
53 compile_step.addCSourceFile(.{
54 .file = b.addWriteFiles().add("a.cpp", bytes),
55 .flags = overlay.cpp_source_flags,
56 });
57 }
58 if (overlay.c_source_bytes) |bytes| {
59 compile_step.addCSourceFile(.{
60 .file = b.addWriteFiles().add("a.c", bytes),
61 .flags = overlay.c_source_flags,
62 });
63 }
64 if (overlay.asm_source_bytes) |bytes| {
65 compile_step.addAssemblyFile(b.addWriteFiles().add("a.s", bytes));
66 }
67 return compile_step;
3668}
3769
38pub fn addObject(b: *Build, name: []const u8, opts: Options) *Compile {
39 return b.addObject(.{
40 .name = name,
41 .target = opts.target,
42 .optimize = opts.optimize,
43 .use_llvm = opts.use_llvm,
44 .use_lld = opts.use_lld,
70pub fn addObject(b: *Build, base: Options, overlay: OverlayOptions) *Step.Compile {
71 const compile_step = b.addObject(.{
72 .name = overlay.name,
73 .root_source_file = rsf: {
74 const bytes = overlay.zig_source_bytes orelse break :rsf null;
75 break :rsf b.addWriteFiles().add("a.zig", bytes);
76 },
77 .target = base.target,
78 .optimize = base.optimize,
79 .use_llvm = base.use_llvm,
80 .use_lld = base.use_lld,
81 .pic = overlay.pic,
82 .strip = overlay.strip,
4583 });
84 if (overlay.cpp_source_bytes) |bytes| {
85 compile_step.addCSourceFile(.{
86 .file = b.addWriteFiles().add("a.cpp", bytes),
87 .flags = overlay.cpp_source_flags,
88 });
89 }
90 if (overlay.c_source_bytes) |bytes| {
91 compile_step.addCSourceFile(.{
92 .file = b.addWriteFiles().add("a.c", bytes),
93 .flags = overlay.c_source_flags,
94 });
95 }
96 if (overlay.asm_source_bytes) |bytes| {
97 compile_step.addAssemblyFile(b.addWriteFiles().add("a.s", bytes));
98 }
99 return compile_step;
46100}
47101
48pub fn addStaticLibrary(b: *Build, name: []const u8, opts: Options) *Compile {
49 return b.addStaticLibrary(.{
50 .name = name,
51 .target = opts.target,
52 .optimize = opts.optimize,
53 .use_llvm = opts.use_llvm,
54 .use_lld = opts.use_lld,
102pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
103 const compile_step = b.addStaticLibrary(.{
104 .name = overlay.name,
105 .root_source_file = rsf: {
106 const bytes = overlay.zig_source_bytes orelse break :rsf null;
107 break :rsf b.addWriteFiles().add("a.zig", bytes);
108 },
109 .target = base.target,
110 .optimize = base.optimize,
111 .use_llvm = base.use_llvm,
112 .use_lld = base.use_lld,
113 .pic = overlay.pic,
114 .strip = overlay.strip,
55115 });
116 if (overlay.cpp_source_bytes) |bytes| {
117 compile_step.addCSourceFile(.{
118 .file = b.addWriteFiles().add("a.cpp", bytes),
119 .flags = overlay.cpp_source_flags,
120 });
121 }
122 if (overlay.c_source_bytes) |bytes| {
123 compile_step.addCSourceFile(.{
124 .file = b.addWriteFiles().add("a.c", bytes),
125 .flags = overlay.c_source_flags,
126 });
127 }
128 if (overlay.asm_source_bytes) |bytes| {
129 compile_step.addAssemblyFile(b.addWriteFiles().add("a.s", bytes));
130 }
131 return compile_step;
56132}
57133
58pub fn addSharedLibrary(b: *Build, name: []const u8, opts: Options) *Compile {
59 return b.addSharedLibrary(.{
60 .name = name,
61 .target = opts.target,
62 .optimize = opts.optimize,
63 .use_llvm = opts.use_llvm,
64 .use_lld = opts.use_lld,
134pub fn addSharedLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
135 const compile_step = b.addSharedLibrary(.{
136 .name = overlay.name,
137 .root_source_file = rsf: {
138 const bytes = overlay.zig_source_bytes orelse break :rsf null;
139 break :rsf b.addWriteFiles().add("a.zig", bytes);
140 },
141 .target = base.target,
142 .optimize = base.optimize,
143 .use_llvm = base.use_llvm,
144 .use_lld = base.use_lld,
145 .pic = overlay.pic,
146 .strip = overlay.strip,
65147 });
148 if (overlay.cpp_source_bytes) |bytes| {
149 compile_step.addCSourceFile(.{
150 .file = b.addWriteFiles().add("a.cpp", bytes),
151 .flags = overlay.cpp_source_flags,
152 });
153 }
154 if (overlay.c_source_bytes) |bytes| {
155 compile_step.addCSourceFile(.{
156 .file = b.addWriteFiles().add("a.c", bytes),
157 .flags = overlay.c_source_flags,
158 });
159 }
160 if (overlay.asm_source_bytes) |bytes| {
161 compile_step.addAssemblyFile(b.addWriteFiles().add("a.s", bytes));
162 }
163 return compile_step;
66164}
67165
68166pub fn addRunArtifact(comp: *Compile) *Run {
......@@ -72,13 +170,6 @@ pub fn addRunArtifact(comp: *Compile) *Run {
72170 return run;
73171}
74172
75pub fn addZigSourceBytes(comp: *Compile, bytes: []const u8) void {
76 const b = comp.step.owner;
77 const file = WriteFile.create(b).add("a.zig", bytes);
78 file.addStepDependencies(&comp.step);
79 comp.root_src = file;
80}
81
82173pub fn addCSourceBytes(comp: *Compile, bytes: []const u8, flags: []const []const u8) void {
83174 const b = comp.step.owner;
84175 const file = WriteFile.create(b).add("a.c", bytes);
test/link/macho.zig+32-27
......@@ -1,26 +1,29 @@
11//! Here we test our MachO linker for correctness and functionality.
22//! TODO migrate standalone tests from test/link/macho/* to here.
33
4pub fn testAll(b: *Build) *Step {
4pub fn testAll(b: *std.Build) *Step {
55 const macho_step = b.step("test-macho", "Run MachO tests");
66
7 const default_target = CrossTarget{ .os_tag = .macos };
8
9 macho_step.dependOn(testResolvingBoundarySymbols(b, .{ .target = default_target }));
7 macho_step.dependOn(testResolvingBoundarySymbols(b, .{
8 .target = b.resolveTargetQuery(.{ .os_tag = .macos }),
9 }));
1010
1111 return macho_step;
1212}
1313
14fn testResolvingBoundarySymbols(b: *Build, opts: Options) *Step {
14fn testResolvingBoundarySymbols(b: *std.Build, opts: Options) *Step {
1515 const test_step = addTestStep(b, "macho-resolving-boundary-symbols", opts);
1616
17 const obj1 = addObject(b, "obj1", opts);
18 addCppSourceBytes(obj1,
17 const obj1 = addObject(b, opts, .{
18 .name = "obj1",
19 .cpp_source_bytes =
1920 \\constexpr const char* MESSAGE __attribute__((used, section("__DATA_CONST,__message_ptr"))) = "codebase";
20 , &.{});
21 ,
22 });
2123
22 const main_o = addObject(b, "main", opts);
23 addZigSourceBytes(main_o,
24 const main_o = addObject(b, opts, .{
25 .name = "main",
26 .zig_source_bytes =
2427 \\const std = @import("std");
2528 \\extern fn interop() [*:0]const u8;
2629 \\pub fn main() !void {
......@@ -28,23 +31,27 @@ fn testResolvingBoundarySymbols(b: *Build, opts: Options) *Step {
2831 \\ std.mem.span(interop()),
2932 \\ });
3033 \\}
31 );
34 ,
35 });
3236
3337 {
34 const obj2 = addObject(b, "obj2", opts);
35 addCppSourceBytes(obj2,
38 const obj2 = addObject(b, opts, .{
39 .name = "obj2",
40 .cpp_source_bytes =
3641 \\extern const char* message_pointer __asm("section$start$__DATA_CONST$__message_ptr");
3742 \\extern "C" const char* interop() {
3843 \\ return message_pointer;
3944 \\}
40 , &.{});
45 ,
46 });
4147
42 const exe = addExecutable(b, "test", opts);
48 const exe = addExecutable(b, opts, .{ .name = "test" });
4349 exe.addObject(obj1);
4450 exe.addObject(obj2);
4551 exe.addObject(main_o);
4652
47 const run = addRunArtifact(exe);
53 const run = b.addRunArtifact(exe);
54 run.skip_foreign_checks = true;
4855 run.expectStdErrEqual("All your codebase are belong to us.\n");
4956 test_step.dependOn(&run.step);
5057
......@@ -55,15 +62,17 @@ fn testResolvingBoundarySymbols(b: *Build, opts: Options) *Step {
5562 }
5663
5764 {
58 const obj3 = addObject(b, "obj3", opts);
59 addCppSourceBytes(obj3,
65 const obj3 = addObject(b, opts, .{
66 .name = "obj3",
67 .cpp_source_bytes =
6068 \\extern const char* message_pointer __asm("section$start$__DATA$__message_ptr");
6169 \\extern "C" const char* interop() {
6270 \\ return message_pointer;
6371 \\}
64 , &.{});
72 ,
73 });
6574
66 const exe = addExecutable(b, "test", opts);
75 const exe = addExecutable(b, opts, .{ .name = "test" });
6776 exe.addObject(obj1);
6877 exe.addObject(obj3);
6978 exe.addObject(main_o);
......@@ -77,20 +86,16 @@ fn testResolvingBoundarySymbols(b: *Build, opts: Options) *Step {
7786 return test_step;
7887}
7988
80fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
89fn addTestStep(b: *std.Build, comptime prefix: []const u8, opts: Options) *Step {
8190 return link.addTestStep(b, "macho-" ++ prefix, opts);
8291}
8392
84const addCppSourceBytes = link.addCppSourceBytes;
85const addExecutable = link.addExecutable;
8693const addObject = link.addObject;
87const addRunArtifact = link.addRunArtifact;
88const addZigSourceBytes = link.addZigSourceBytes;
94const addExecutable = link.addExecutable;
8995const expectLinkErrors = link.expectLinkErrors;
9096const link = @import("link.zig");
9197const std = @import("std");
9298
93const Build = std.Build;
9499const CrossTarget = std.zig.CrossTarget;
95100const Options = link.Options;
96const Step = Build.Step;
101const Step = std.Build.Step;
test/link/macho/bugs/13056/build.zig+3-3
......@@ -14,14 +14,14 @@ pub fn build(b: *std.Build) void {
1414}
1515
1616fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
17 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
18 const target_info = std.zig.system.NativeTargetInfo.detect(target) catch unreachable;
19 const sdk = std.zig.system.darwin.getSdk(b.allocator, target_info.target) orelse
17 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
18 const sdk = std.zig.system.darwin.getSdk(b.allocator, target.target) orelse
2019 @panic("macOS SDK is required to run the test");
2120
2221 const exe = b.addExecutable(.{
2322 .name = "test",
2423 .optimize = optimize,
24 .target = b.host,
2525 });
2626 exe.addSystemIncludePath(.{ .path = b.pathJoin(&.{ sdk, "/usr/include" }) });
2727 exe.addIncludePath(.{ .path = b.pathJoin(&.{ sdk, "/usr/include/c++/v1" }) });
test/link/macho/bugs/13457/build.zig+1-1
......@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
1313}
1414
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
16 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
16 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1717
1818 const exe = b.addExecutable(.{
1919 .name = "test",
test/link/macho/bugs/16308/build.zig+1-1
......@@ -6,7 +6,7 @@ pub fn build(b: *std.Build) void {
66 const test_step = b.step("test", "Test it");
77 b.default_step = test_step;
88
9 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
9 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1010
1111 const lib = b.addSharedLibrary(.{
1212 .name = "a",
test/link/macho/bugs/16628/build.zig+1-1
......@@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void {
1515}
1616
1717fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
18 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
18 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1919
2020 const exe = b.addExecutable(.{
2121 .name = "test",
test/link/macho/dead_strip/build.zig+2-2
......@@ -4,7 +4,7 @@ pub const requires_symlinks = true;
44
55pub fn build(b: *std.Build) void {
66 const optimize: std.builtin.OptimizeMode = .Debug;
7 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
7 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
88
99 const test_step = b.step("test", "Test the program");
1010 b.default_step = test_step;
......@@ -44,7 +44,7 @@ pub fn build(b: *std.Build) void {
4444fn createScenario(
4545 b: *std.Build,
4646 optimize: std.builtin.OptimizeMode,
47 target: std.zig.CrossTarget,
47 target: std.Build.ResolvedTarget,
4848 name: []const u8,
4949) *std.Build.Step.Compile {
5050 const exe = b.addExecutable(.{
test/link/macho/dead_strip_dylibs/build.zig+1
......@@ -52,6 +52,7 @@ fn createScenario(
5252 const exe = b.addExecutable(.{
5353 .name = name,
5454 .optimize = optimize,
55 .target = b.host,
5556 });
5657 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} });
5758 exe.linkLibC();
test/link/macho/dylib/build.zig+2-2
......@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
1313}
1414
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
16 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
16 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1717
1818 const dylib = b.addSharedLibrary(.{
1919 .name = "a",
......@@ -55,7 +55,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
5555
5656 check_exe.checkInHeaders();
5757 check_exe.checkExact("cmd RPATH");
58 check_exe.checkExactPath("path", dylib.getOutputDirectorySource());
58 check_exe.checkExactPath("path", dylib.getEmittedBinDirectory());
5959 test_step.dependOn(&check_exe.step);
6060
6161 const run = b.addRunArtifact(exe);
test/link/macho/empty/build.zig+1-1
......@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
1313}
1414
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
16 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
16 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1717
1818 const exe = b.addExecutable(.{
1919 .name = "test",
test/link/macho/entry/build.zig+1-1
......@@ -16,7 +16,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1616 const exe = b.addExecutable(.{
1717 .name = "main",
1818 .optimize = optimize,
19 .target = .{ .os_tag = .macos },
19 .target = b.resolveTargetQuery(.{ .os_tag = .macos }),
2020 });
2121 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} });
2222 exe.linkLibC();
test/link/macho/entry_in_archive/build.zig+2-2
......@@ -16,7 +16,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1616 const lib = b.addStaticLibrary(.{
1717 .name = "main",
1818 .optimize = optimize,
19 .target = .{ .os_tag = .macos },
19 .target = b.resolveTargetQuery(.{ .os_tag = .macos }),
2020 });
2121 lib.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} });
2222 lib.linkLibC();
......@@ -24,7 +24,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2424 const exe = b.addExecutable(.{
2525 .name = "main",
2626 .optimize = optimize,
27 .target = .{ .os_tag = .macos },
27 .target = b.resolveTargetQuery(.{ .os_tag = .macos }),
2828 });
2929 exe.linkLibrary(lib);
3030 exe.linkLibC();
test/link/macho/entry_in_dylib/build.zig+2-2
......@@ -16,7 +16,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1616 const lib = b.addSharedLibrary(.{
1717 .name = "bootstrap",
1818 .optimize = optimize,
19 .target = .{ .os_tag = .macos },
19 .target = b.resolveTargetQuery(.{ .os_tag = .macos }),
2020 });
2121 lib.addCSourceFile(.{ .file = .{ .path = "bootstrap.c" }, .flags = &.{} });
2222 lib.linkLibC();
......@@ -25,7 +25,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2525 const exe = b.addExecutable(.{
2626 .name = "main",
2727 .optimize = optimize,
28 .target = .{ .os_tag = .macos },
28 .target = b.resolveTargetQuery(.{ .os_tag = .macos }),
2929 });
3030 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} });
3131 exe.linkLibrary(lib);
test/link/macho/headerpad/build.zig+1
......@@ -112,6 +112,7 @@ fn simpleExe(
112112 const exe = b.addExecutable(.{
113113 .name = name,
114114 .optimize = optimize,
115 .target = b.host,
115116 });
116117 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} });
117118 exe.linkLibC();
test/link/macho/linksection/build.zig+1-1
......@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
1313}
1414
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
16 const target = std.zig.CrossTarget{ .os_tag = .macos };
16 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1717
1818 const obj = b.addObject(.{
1919 .name = "test",
test/link/macho/needed_framework/build.zig+1
......@@ -19,6 +19,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1919 const exe = b.addExecutable(.{
2020 .name = "test",
2121 .optimize = optimize,
22 .target = b.host,
2223 });
2324 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} });
2425 exe.linkLibC();
test/link/macho/needed_library/build.zig+2-2
......@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
1313}
1414
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
16 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
16 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1717
1818 const dylib = b.addSharedLibrary(.{
1919 .name = "a",
......@@ -33,7 +33,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
3333 });
3434 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} });
3535 exe.linkLibC();
36 exe.linkSystemLibraryNeeded("a");
36 exe.root_module.linkSystemLibrary("a", .{ .needed = true });
3737 exe.addLibraryPath(dylib.getEmittedBinDirectory());
3838 exe.addRPath(dylib.getEmittedBinDirectory());
3939 exe.dead_strip_dylibs = true;
test/link/macho/objc/build.zig+1
......@@ -17,6 +17,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1717 const exe = b.addExecutable(.{
1818 .name = "test",
1919 .optimize = optimize,
20 .target = b.host,
2021 });
2122 exe.addIncludePath(.{ .path = "." });
2223 exe.addCSourceFile(.{ .file = .{ .path = "Foo.m" }, .flags = &[0][]const u8{} });
test/link/macho/objcpp/build.zig+1
......@@ -17,6 +17,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1717 const exe = b.addExecutable(.{
1818 .name = "test",
1919 .optimize = optimize,
20 .target = b.host,
2021 });
2122 b.default_step.dependOn(&exe.step);
2223 exe.addIncludePath(.{ .path = "." });
test/link/macho/pagezero/build.zig+1-1
......@@ -7,7 +7,7 @@ pub fn build(b: *std.Build) void {
77 b.default_step = test_step;
88
99 const optimize: std.builtin.OptimizeMode = .Debug;
10 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
10 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1111
1212 {
1313 const exe = b.addExecutable(.{
test/link/macho/reexports/build.zig+1-1
......@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
1313}
1414
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
16 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
16 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1717
1818 const lib = b.addStaticLibrary(.{
1919 .name = "a",
test/link/macho/search_strategy/build.zig+3-3
......@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
1313}
1414
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
16 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
16 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1717
1818 {
1919 // -search_dylibs_first
......@@ -45,9 +45,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
4545fn createScenario(
4646 b: *std.Build,
4747 optimize: std.builtin.OptimizeMode,
48 target: std.zig.CrossTarget,
48 target: std.Build.ResolvedTarget,
4949 name: []const u8,
50 search_strategy: std.Build.Step.Compile.SystemLib.SearchStrategy,
50 search_strategy: std.Build.Module.SystemLib.SearchStrategy,
5151) *std.Build.Step.Compile {
5252 const static = b.addStaticLibrary(.{
5353 .name = name,
test/link/macho/stack_size/build.zig+1-1
......@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
1313}
1414
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
16 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
16 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1717
1818 const exe = b.addExecutable(.{
1919 .name = "main",
test/link/macho/strict_validation/build.zig+1-1
......@@ -14,7 +14,7 @@ pub fn build(b: *std.Build) void {
1414}
1515
1616fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
17 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
17 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1818
1919 const exe = b.addExecutable(.{
2020 .name = "main",
test/link/macho/tbdv3/build.zig+1-1
......@@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void {
1515}
1616
1717fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
18 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
18 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1919
2020 const lib = b.addSharedLibrary(.{
2121 .name = "a",
test/link/macho/tls/build.zig+1-1
......@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
1313}
1414
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
16 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
16 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1717
1818 const lib = b.addSharedLibrary(.{
1919 .name = "a",
test/link/macho/unwind_info/build.zig+3-3
......@@ -14,7 +14,7 @@ pub fn build(b: *std.Build) void {
1414}
1515
1616fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
17 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
17 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1818
1919 testUnwindInfo(b, test_step, optimize, target, false, "no-dead-strip");
2020 testUnwindInfo(b, test_step, optimize, target, true, "yes-dead-strip");
......@@ -24,7 +24,7 @@ fn testUnwindInfo(
2424 b: *std.Build,
2525 test_step: *std.Build.Step,
2626 optimize: std.builtin.OptimizeMode,
27 target: std.zig.CrossTarget,
27 target: std.Build.ResolvedTarget,
2828 dead_strip: bool,
2929 name: []const u8,
3030) void {
......@@ -66,7 +66,7 @@ fn testUnwindInfo(
6666fn createScenario(
6767 b: *std.Build,
6868 optimize: std.builtin.OptimizeMode,
69 target: std.zig.CrossTarget,
69 target: std.Build.ResolvedTarget,
7070 name: []const u8,
7171) *std.Build.Step.Compile {
7272 const exe = b.addExecutable(.{
test/link/macho/weak_framework/build.zig+1
......@@ -17,6 +17,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1717 const exe = b.addExecutable(.{
1818 .name = "test",
1919 .optimize = optimize,
20 .target = b.host,
2021 });
2122 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} });
2223 exe.linkLibC();
test/link/macho/weak_library/build.zig+2-2
......@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
1313}
1414
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
16 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
16 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
1717
1818 const dylib = b.addSharedLibrary(.{
1919 .name = "a",
......@@ -32,7 +32,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
3232 });
3333 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} });
3434 exe.linkLibC();
35 exe.linkSystemLibraryWeak("a");
35 exe.root_module.linkSystemLibrary("a", .{ .weak = true });
3636 exe.addLibraryPath(dylib.getEmittedBinDirectory());
3737 exe.addRPath(dylib.getEmittedBinDirectory());
3838
test/link/static_libs_from_object_files/build.zig+8-6
......@@ -59,7 +59,7 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
5959 .name = "test1",
6060 .root_source_file = .{ .path = "main.zig" },
6161 .optimize = optimize,
62 .target = .{},
62 .target = b.host,
6363 });
6464
6565 for (files) |file| {
......@@ -77,12 +77,12 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
7777 {
7878 const lib_a = b.addStaticLibrary(.{
7979 .name = "test2_a",
80 .target = .{},
80 .target = b.host,
8181 .optimize = optimize,
8282 });
8383 const lib_b = b.addStaticLibrary(.{
8484 .name = "test2_b",
85 .target = .{},
85 .target = b.host,
8686 .optimize = optimize,
8787 });
8888
......@@ -94,6 +94,7 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
9494 const exe = b.addExecutable(.{
9595 .name = "test2",
9696 .root_source_file = .{ .path = "main.zig" },
97 .target = b.host,
9798 .optimize = optimize,
9899 });
99100 exe.linkLibrary(lib_a);
......@@ -110,19 +111,19 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
110111 {
111112 const lib_a = b.addStaticLibrary(.{
112113 .name = "test3_a",
113 .target = .{},
114 .target = b.host,
114115 .optimize = optimize,
115116 });
116117 const lib_b = b.addStaticLibrary(.{
117118 .name = "test3_b",
118 .target = .{},
119 .target = b.host,
119120 .optimize = optimize,
120121 });
121122
122123 for (files, 1..) |file, i| {
123124 const obj = b.addObject(.{
124125 .name = b.fmt("obj_{}", .{i}),
125 .target = .{},
126 .target = b.host,
126127 .optimize = optimize,
127128 });
128129 obj.addCSourceFile(.{ .file = file, .flags = &flags });
......@@ -134,6 +135,7 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
134135 const exe = b.addExecutable(.{
135136 .name = "test3",
136137 .root_source_file = .{ .path = "main.zig" },
138 .target = b.host,
137139 .optimize = optimize,
138140 });
139141 exe.linkLibrary(lib_a);
test/link/wasm/archive/build.zig+1-1
......@@ -20,11 +20,11 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2020 .root_source_file = .{ .path = "main.zig" },
2121 .optimize = optimize,
2222 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
23 .strip = false,
2324 });
2425 lib.entry = .disabled;
2526 lib.use_llvm = false;
2627 lib.use_lld = false;
27 lib.strip = false;
2828
2929 const check = lib.checkObject();
3030 check.checkInHeaders();
test/link/wasm/basic-features/build.zig+2-2
......@@ -8,12 +8,12 @@ pub fn build(b: *std.Build) void {
88 .name = "lib",
99 .root_source_file = .{ .path = "main.zig" },
1010 .optimize = .Debug,
11 .target = .{
11 .target = b.resolveTargetQuery(.{
1212 .cpu_arch = .wasm32,
1313 .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
1414 .cpu_features_add = std.Target.wasm.featureSet(&.{.atomics}),
1515 .os_tag = .freestanding,
16 },
16 }),
1717 });
1818 lib.entry = .disabled;
1919 lib.use_llvm = false;
test/link/wasm/bss/build.zig+4-4
......@@ -17,13 +17,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
1717 const lib = b.addExecutable(.{
1818 .name = "lib",
1919 .root_source_file = .{ .path = "lib.zig" },
20 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
20 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
2121 .optimize = optimize_mode,
22 .strip = false,
2223 });
2324 lib.entry = .disabled;
2425 lib.use_llvm = false;
2526 lib.use_lld = false;
26 lib.strip = false;
2727 // to make sure the bss segment is emitted, we must import memory
2828 lib.import_memory = true;
2929 lib.link_gc_sections = false;
......@@ -65,13 +65,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
6565 const lib = b.addExecutable(.{
6666 .name = "lib",
6767 .root_source_file = .{ .path = "lib2.zig" },
68 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
68 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
6969 .optimize = optimize_mode,
70 .strip = false,
7071 });
7172 lib.entry = .disabled;
7273 lib.use_llvm = false;
7374 lib.use_lld = false;
74 lib.strip = false;
7575 // to make sure the bss segment is emitted, we must import memory
7676 lib.import_memory = true;
7777 lib.link_gc_sections = false;
test/link/wasm/export-data/build.zig+1-1
......@@ -17,7 +17,7 @@ pub fn build(b: *std.Build) void {
1717 });
1818 lib.entry = .disabled;
1919 lib.use_lld = false;
20 lib.export_symbol_names = &.{ "foo", "bar" };
20 lib.root_module.export_symbol_names = &.{ "foo", "bar" };
2121 lib.global_base = 0; // put data section at address 0 to make data symbols easier to parse
2222
2323 const check_lib = lib.checkObject();
test/link/wasm/export/build.zig+4-4
......@@ -17,7 +17,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1717 .name = "no-export",
1818 .root_source_file = .{ .path = "main.zig" },
1919 .optimize = optimize,
20 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
20 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
2121 });
2222 no_export.entry = .disabled;
2323 no_export.use_llvm = false;
......@@ -27,7 +27,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2727 .name = "dynamic",
2828 .root_source_file = .{ .path = "main.zig" },
2929 .optimize = optimize,
30 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
30 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
3131 });
3232 dynamic_export.entry = .disabled;
3333 dynamic_export.rdynamic = true;
......@@ -38,10 +38,10 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
3838 .name = "force",
3939 .root_source_file = .{ .path = "main.zig" },
4040 .optimize = optimize,
41 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
41 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
4242 });
4343 force_export.entry = .disabled;
44 force_export.export_symbol_names = &.{"foo"};
44 force_export.root_module.export_symbol_names = &.{"foo"};
4545 force_export.use_llvm = false;
4646 force_export.use_lld = false;
4747
test/link/wasm/extern-mangle/build.zig+1-1
......@@ -14,7 +14,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1414 const lib = b.addExecutable(.{
1515 .name = "lib",
1616 .root_source_file = .{ .path = "lib.zig" },
17 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
17 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
1818 .optimize = optimize,
1919 });
2020 lib.entry = .disabled;
test/link/wasm/extern/build.zig+1-1
......@@ -17,7 +17,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1717 .name = "extern",
1818 .root_source_file = .{ .path = "main.zig" },
1919 .optimize = optimize,
20 .target = .{ .cpu_arch = .wasm32, .os_tag = .wasi },
20 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .wasi }),
2121 });
2222 exe.addCSourceFile(.{ .file = .{ .path = "foo.c" }, .flags = &.{} });
2323 exe.use_llvm = false;
test/link/wasm/function-table/build.zig+3-3
......@@ -16,7 +16,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1616 const import_table = b.addExecutable(.{
1717 .name = "import_table",
1818 .root_source_file = .{ .path = "lib.zig" },
19 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
19 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
2020 .optimize = optimize,
2121 });
2222 import_table.entry = .disabled;
......@@ -28,7 +28,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2828 const export_table = b.addExecutable(.{
2929 .name = "export_table",
3030 .root_source_file = .{ .path = "lib.zig" },
31 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
31 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
3232 .optimize = optimize,
3333 });
3434 export_table.entry = .disabled;
......@@ -40,7 +40,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
4040 const regular_table = b.addExecutable(.{
4141 .name = "regular_table",
4242 .root_source_file = .{ .path = "lib.zig" },
43 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
43 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
4444 .optimize = optimize,
4545 });
4646 regular_table.entry = .disabled;
test/link/wasm/infer-features/build.zig+4-4
......@@ -7,11 +7,11 @@ pub fn build(b: *std.Build) void {
77 const c_obj = b.addObject(.{
88 .name = "c_obj",
99 .optimize = .Debug,
10 .target = .{
10 .target = b.resolveTargetQuery(.{
1111 .cpu_arch = .wasm32,
1212 .cpu_model = .{ .explicit = &std.Target.wasm.cpu.bleeding_edge },
1313 .os_tag = .freestanding,
14 },
14 }),
1515 });
1616 c_obj.addCSourceFile(.{ .file = .{ .path = "foo.c" }, .flags = &.{} });
1717
......@@ -21,11 +21,11 @@ pub fn build(b: *std.Build) void {
2121 .name = "lib",
2222 .root_source_file = .{ .path = "main.zig" },
2323 .optimize = .Debug,
24 .target = .{
24 .target = b.resolveTargetQuery(.{
2525 .cpu_arch = .wasm32,
2626 .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
2727 .os_tag = .freestanding,
28 },
28 }),
2929 });
3030 lib.entry = .disabled;
3131 lib.use_llvm = false;
test/link/wasm/producers/build.zig+2-2
......@@ -17,13 +17,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1717 const lib = b.addExecutable(.{
1818 .name = "lib",
1919 .root_source_file = .{ .path = "lib.zig" },
20 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
20 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
2121 .optimize = optimize,
22 .strip = false,
2223 });
2324 lib.entry = .disabled;
2425 lib.use_llvm = false;
2526 lib.use_lld = false;
26 lib.strip = false;
2727 b.installArtifact(lib);
2828
2929 const version_fmt = "version " ++ builtin.zig_version_string;
test/link/wasm/segments/build.zig+2-2
......@@ -16,13 +16,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1616 const lib = b.addExecutable(.{
1717 .name = "lib",
1818 .root_source_file = .{ .path = "lib.zig" },
19 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
19 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
2020 .optimize = optimize,
21 .strip = false,
2122 });
2223 lib.entry = .disabled;
2324 lib.use_llvm = false;
2425 lib.use_lld = false;
25 lib.strip = false;
2626 lib.link_gc_sections = false; // so data is not garbage collected and we can verify data section
2727 b.installArtifact(lib);
2828
test/link/wasm/shared-memory/build.zig+3-3
......@@ -21,16 +21,16 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
2121 .os_tag = .freestanding,
2222 },
2323 .optimize = optimize_mode,
24 .strip = false,
25 .single_threaded = false,
2426 });
2527 lib.entry = .disabled;
2628 lib.use_lld = false;
27 lib.strip = false;
2829 lib.import_memory = true;
2930 lib.export_memory = true;
3031 lib.shared_memory = true;
3132 lib.max_memory = 67108864;
32 lib.single_threaded = false;
33 lib.export_symbol_names = &.{"foo"};
33 lib.root_module.export_symbol_names = &.{"foo"};
3434
3535 const check_lib = lib.checkObject();
3636
test/link/wasm/stack_pointer/build.zig+2-2
......@@ -16,13 +16,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1616 const lib = b.addExecutable(.{
1717 .name = "lib",
1818 .root_source_file = .{ .path = "lib.zig" },
19 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
19 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
2020 .optimize = optimize,
21 .strip = false,
2122 });
2223 lib.entry = .disabled;
2324 lib.use_llvm = false;
2425 lib.use_lld = false;
25 lib.strip = false;
2626 lib.stack_size = std.wasm.page_size * 2; // set an explicit stack size
2727 lib.link_gc_sections = false;
2828 b.installArtifact(lib);
test/link/wasm/type/build.zig+2-2
......@@ -16,13 +16,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1616 const lib = b.addExecutable(.{
1717 .name = "lib",
1818 .root_source_file = .{ .path = "lib.zig" },
19 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
19 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
2020 .optimize = optimize,
21 .strip = false,
2122 });
2223 lib.entry = .disabled;
2324 lib.use_llvm = false;
2425 lib.use_lld = false;
25 lib.strip = false;
2626 b.installArtifact(lib);
2727
2828 const check_lib = lib.checkObject();
test/llvm_targets.zig+8-4
......@@ -127,17 +127,21 @@ const targets = [_]std.zig.CrossTarget{
127127 .{ .cpu_arch = .xtensa, .os_tag = .linux, .abi = .none },
128128};
129129
130pub fn addCases(ctx: *Cases, build_options: @import("cases.zig").BuildOptions) !void {
130pub fn addCases(
131 ctx: *Cases,
132 build_options: @import("cases.zig").BuildOptions,
133 b: *std.Build,
134) !void {
131135 if (!build_options.enable_llvm) return;
132 for (targets) |target| {
133 if (target.cpu_arch) |arch| switch (arch) {
136 for (targets) |target_query| {
137 if (target_query.cpu_arch) |arch| switch (arch) {
134138 .m68k => if (!build_options.llvm_has_m68k) continue,
135139 .csky => if (!build_options.llvm_has_csky) continue,
136140 .arc => if (!build_options.llvm_has_arc) continue,
137141 .xtensa => if (!build_options.llvm_has_xtensa) continue,
138142 else => {},
139143 };
140 var case = ctx.noEmitUsingLlvmBackend("llvm_targets", target);
144 var case = ctx.noEmitUsingLlvmBackend("llvm_targets", b.resolveTargetQuery(target_query));
141145 case.addCompile("");
142146 }
143147}
test/nvptx.zig+12-15
......@@ -1,9 +1,14 @@
11const std = @import("std");
22const Cases = @import("src/Cases.zig");
33
4pub fn addCases(ctx: *Cases) !void {
4pub fn addCases(ctx: *Cases, b: *std.Build) !void {
5 const target = b.resolveTargetQuery(.{
6 .cpu_arch = .nvptx64,
7 .os_tag = .cuda,
8 });
9
510 {
6 var case = addPtx(ctx, "simple addition and subtraction");
11 var case = addPtx(ctx, target, "simple addition and subtraction");
712
813 case.addCompile(
914 \\fn add(a: i32, b: i32) i32 {
......@@ -20,7 +25,7 @@ pub fn addCases(ctx: *Cases) !void {
2025 }
2126
2227 {
23 var case = addPtx(ctx, "read special registers");
28 var case = addPtx(ctx, target, "read special registers");
2429
2530 case.addCompile(
2631 \\fn threadIdX() u32 {
......@@ -37,7 +42,7 @@ pub fn addCases(ctx: *Cases) !void {
3742 }
3843
3944 {
40 var case = addPtx(ctx, "address spaces");
45 var case = addPtx(ctx, target, "address spaces");
4146
4247 case.addCompile(
4348 \\var x: i32 addrspace(.global) = 0;
......@@ -50,7 +55,7 @@ pub fn addCases(ctx: *Cases) !void {
5055 }
5156
5257 {
53 var case = addPtx(ctx, "reduce in shared mem");
58 var case = addPtx(ctx, target, "reduce in shared mem");
5459 case.addCompile(
5560 \\fn threadIdX() u32 {
5661 \\ return asm ("mov.u32 \t%[r], %tid.x;"
......@@ -82,18 +87,10 @@ pub fn addCases(ctx: *Cases) !void {
8287 }
8388}
8489
85const nvptx_target = std.zig.CrossTarget{
86 .cpu_arch = .nvptx64,
87 .os_tag = .cuda,
88};
89
90pub fn addPtx(
91 ctx: *Cases,
92 name: []const u8,
93) *Cases.Case {
90fn addPtx(ctx: *Cases, target: std.Build.ResolvedTarget, name: []const u8) *Cases.Case {
9491 ctx.cases.append(.{
9592 .name = name,
96 .target = nvptx_target,
93 .target = target,
9794 .updates = std.ArrayList(Cases.Update).init(ctx.cases.allocator),
9895 .output_mode = .Obj,
9996 .deps = std.ArrayList(Cases.DepModule).init(ctx.cases.allocator),
test/src/Cases.zig+60-49
......@@ -76,7 +76,7 @@ pub const Case = struct {
7676 name: []const u8,
7777 /// The platform the test targets. For non-native platforms, an emulator
7878 /// such as QEMU is required for tests to complete.
79 target: CrossTarget,
79 target: std.Build.ResolvedTarget,
8080 /// In order to be able to run e.g. Execution updates, this must be set
8181 /// to Executable.
8282 output_mode: std.builtin.OutputMode,
......@@ -155,7 +155,7 @@ pub const Translate = struct {
155155 name: []const u8,
156156
157157 input: [:0]const u8,
158 target: CrossTarget,
158 target: std.Build.ResolvedTarget,
159159 link_libc: bool,
160160 c_frontend: CFrontend,
161161 kind: union(enum) {
......@@ -171,7 +171,7 @@ pub const Translate = struct {
171171pub fn addExe(
172172 ctx: *Cases,
173173 name: []const u8,
174 target: CrossTarget,
174 target: std.Build.ResolvedTarget,
175175) *Case {
176176 ctx.cases.append(Case{
177177 .name = name,
......@@ -184,16 +184,16 @@ pub fn addExe(
184184}
185185
186186/// Adds a test case for Zig input, producing an executable
187pub fn exe(ctx: *Cases, name: []const u8, target: CrossTarget) *Case {
187pub fn exe(ctx: *Cases, name: []const u8, target: std.Build.ResolvedTarget) *Case {
188188 return ctx.addExe(name, target);
189189}
190190
191pub fn exeFromCompiledC(ctx: *Cases, name: []const u8, target: CrossTarget) *Case {
192 var target_adjusted = target;
193 target_adjusted.ofmt = .c;
191pub fn exeFromCompiledC(ctx: *Cases, name: []const u8, target_query: std.zig.CrossTarget, b: *std.Build) *Case {
192 var adjusted_query = target_query;
193 adjusted_query.ofmt = .c;
194194 ctx.cases.append(Case{
195195 .name = name,
196 .target = target_adjusted,
196 .target = b.resolveTargetQuery(adjusted_query),
197197 .updates = std.ArrayList(Update).init(ctx.cases.allocator),
198198 .output_mode = .Exe,
199199 .deps = std.ArrayList(DepModule).init(ctx.arena),
......@@ -202,7 +202,7 @@ pub fn exeFromCompiledC(ctx: *Cases, name: []const u8, target: CrossTarget) *Cas
202202 return &ctx.cases.items[ctx.cases.items.len - 1];
203203}
204204
205pub fn noEmitUsingLlvmBackend(ctx: *Cases, name: []const u8, target: CrossTarget) *Case {
205pub fn noEmitUsingLlvmBackend(ctx: *Cases, name: []const u8, target: std.Build.ResolvedTarget) *Case {
206206 ctx.cases.append(Case{
207207 .name = name,
208208 .target = target,
......@@ -217,7 +217,7 @@ pub fn noEmitUsingLlvmBackend(ctx: *Cases, name: []const u8, target: CrossTarget
217217
218218/// Adds a test case that uses the LLVM backend to emit an executable.
219219/// Currently this implies linking libc, because only then we can generate a testable executable.
220pub fn exeUsingLlvmBackend(ctx: *Cases, name: []const u8, target: CrossTarget) *Case {
220pub fn exeUsingLlvmBackend(ctx: *Cases, name: []const u8, target: std.Build.ResolvedTarget) *Case {
221221 ctx.cases.append(Case{
222222 .name = name,
223223 .target = target,
......@@ -233,7 +233,7 @@ pub fn exeUsingLlvmBackend(ctx: *Cases, name: []const u8, target: CrossTarget) *
233233pub fn addObj(
234234 ctx: *Cases,
235235 name: []const u8,
236 target: CrossTarget,
236 target: std.Build.ResolvedTarget,
237237) *Case {
238238 ctx.cases.append(Case{
239239 .name = name,
......@@ -248,7 +248,7 @@ pub fn addObj(
248248pub fn addTest(
249249 ctx: *Cases,
250250 name: []const u8,
251 target: CrossTarget,
251 target: std.Build.ResolvedTarget,
252252) *Case {
253253 ctx.cases.append(Case{
254254 .name = name,
......@@ -262,17 +262,17 @@ pub fn addTest(
262262}
263263
264264/// Adds a test case for Zig input, producing an object file.
265pub fn obj(ctx: *Cases, name: []const u8, target: CrossTarget) *Case {
265pub fn obj(ctx: *Cases, name: []const u8, target: std.Build.ResolvedTarget) *Case {
266266 return ctx.addObj(name, target);
267267}
268268
269269/// Adds a test case for ZIR input, producing an object file.
270pub fn objZIR(ctx: *Cases, name: []const u8, target: CrossTarget) *Case {
270pub fn objZIR(ctx: *Cases, name: []const u8, target: std.Build.ResolvedTarget) *Case {
271271 return ctx.addObj(name, target, .ZIR);
272272}
273273
274274/// Adds a test case for Zig or ZIR input, producing C code.
275pub fn addC(ctx: *Cases, name: []const u8, target: CrossTarget) *Case {
275pub fn addC(ctx: *Cases, name: []const u8, target: std.Build.ResolvedTarget) *Case {
276276 var target_adjusted = target;
277277 target_adjusted.ofmt = std.Target.ObjectFormat.c;
278278 ctx.cases.append(Case{
......@@ -308,7 +308,7 @@ pub fn compareOutput(
308308pub fn addTransform(
309309 ctx: *Cases,
310310 name: []const u8,
311 target: CrossTarget,
311 target: std.Build.ResolvedTarget,
312312 src: [:0]const u8,
313313 result: [:0]const u8,
314314) void {
......@@ -320,7 +320,7 @@ pub fn addTransform(
320320pub fn transform(
321321 ctx: *Cases,
322322 name: []const u8,
323 target: CrossTarget,
323 target: std.Build.ResolvedTarget,
324324 src: [:0]const u8,
325325 result: [:0]const u8,
326326) void {
......@@ -330,7 +330,7 @@ pub fn transform(
330330pub fn addError(
331331 ctx: *Cases,
332332 name: []const u8,
333 target: CrossTarget,
333 target: std.Build.ResolvedTarget,
334334 src: [:0]const u8,
335335 expected_errors: []const []const u8,
336336) void {
......@@ -343,7 +343,7 @@ pub fn addError(
343343pub fn compileError(
344344 ctx: *Cases,
345345 name: []const u8,
346 target: CrossTarget,
346 target: std.Build.ResolvedTarget,
347347 src: [:0]const u8,
348348 expected_errors: []const []const u8,
349349) void {
......@@ -355,7 +355,7 @@ pub fn compileError(
355355pub fn addCompile(
356356 ctx: *Cases,
357357 name: []const u8,
358 target: CrossTarget,
358 target: std.Build.ResolvedTarget,
359359 src: [:0]const u8,
360360) void {
361361 ctx.addObj(name, target).addCompile(src);
......@@ -368,9 +368,9 @@ pub fn addCompile(
368368/// Each file should include a test manifest as a contiguous block of comments at
369369/// the end of the file. The first line should be the test type, followed by a set of
370370/// key-value config values, followed by a blank line, then the expected output.
371pub fn addFromDir(ctx: *Cases, dir: std.fs.Dir) void {
371pub fn addFromDir(ctx: *Cases, dir: std.fs.Dir, b: *std.Build) void {
372372 var current_file: []const u8 = "none";
373 ctx.addFromDirInner(dir, &current_file) catch |err| {
373 ctx.addFromDirInner(dir, &current_file, b) catch |err| {
374374 std.debug.panicExtra(
375375 @errorReturnTrace(),
376376 @returnAddress(),
......@@ -386,6 +386,7 @@ fn addFromDirInner(
386386 /// This is kept up to date with the currently being processed file so
387387 /// that if any errors occur the caller knows it happened during this file.
388388 current_file: *[]const u8,
389 b: *std.Build,
389390) !void {
390391 var it = try iterable_dir.walk(ctx.arena);
391392 var filenames = std.ArrayList([]const u8).init(ctx.arena);
......@@ -422,7 +423,7 @@ fn addFromDirInner(
422423 var manifest = try TestManifest.parse(ctx.arena, src);
423424
424425 const backends = try manifest.getConfigForKeyAlloc(ctx.arena, "backend", Backend);
425 const targets = try manifest.getConfigForKeyAlloc(ctx.arena, "target", CrossTarget);
426 const targets = try manifest.getConfigForKeyAlloc(ctx.arena, "target", std.zig.CrossTarget);
426427 const c_frontends = try manifest.getConfigForKeyAlloc(ctx.arena, "c_frontend", CFrontend);
427428 const is_test = try manifest.getConfigForKeyAssertSingle("is_test", bool);
428429 const link_libc = try manifest.getConfigForKeyAssertSingle("link_libc", bool);
......@@ -430,12 +431,12 @@ fn addFromDirInner(
430431
431432 if (manifest.type == .translate_c) {
432433 for (c_frontends) |c_frontend| {
433 for (targets) |target| {
434 for (targets) |target_query| {
434435 const output = try manifest.trailingLinesSplit(ctx.arena);
435436 try ctx.translate.append(.{
436437 .name = std.fs.path.stem(filename),
437438 .c_frontend = c_frontend,
438 .target = target,
439 .target = b.resolveTargetQuery(target_query),
439440 .link_libc = link_libc,
440441 .input = src,
441442 .kind = .{ .translate = output },
......@@ -446,12 +447,12 @@ fn addFromDirInner(
446447 }
447448 if (manifest.type == .run_translated_c) {
448449 for (c_frontends) |c_frontend| {
449 for (targets) |target| {
450 for (targets) |target_query| {
450451 const output = try manifest.trailingSplit(ctx.arena);
451452 try ctx.translate.append(.{
452453 .name = std.fs.path.stem(filename),
453454 .c_frontend = c_frontend,
454 .target = target,
455 .target = b.resolveTargetQuery(target_query),
455456 .link_libc = link_libc,
456457 .input = src,
457458 .kind = .{ .run = output },
......@@ -464,16 +465,18 @@ fn addFromDirInner(
464465 var cases = std.ArrayList(usize).init(ctx.arena);
465466
466467 // Cross-product to get all possible test combinations
467 for (backends) |backend| {
468 for (targets) |target| {
468 for (targets) |target_query| {
469 const resolved_target = b.resolveTargetQuery(target_query);
470 const target = resolved_target.target;
471 for (backends) |backend| {
469472 if (backend == .stage2 and
470 target.getCpuArch() != .wasm32 and target.getCpuArch() != .x86_64)
473 target.cpu.arch != .wasm32 and target.cpu.arch != .x86_64)
471474 {
472475 // Other backends don't support new liveness format
473476 continue;
474477 }
475 if (backend == .stage2 and target.getOsTag() == .macos and
476 target.getCpuArch() == .x86_64 and builtin.cpu.arch == .aarch64)
478 if (backend == .stage2 and target.os.tag == .macos and
479 target.cpu.arch == .x86_64 and builtin.cpu.arch == .aarch64)
477480 {
478481 // Rosetta has issues with ZLD
479482 continue;
......@@ -482,7 +485,7 @@ fn addFromDirInner(
482485 const next = ctx.cases.items.len;
483486 try ctx.cases.append(.{
484487 .name = std.fs.path.stem(filename),
485 .target = target,
488 .target = resolved_target,
486489 .backend = backend,
487490 .updates = std.ArrayList(Cases.Update).init(ctx.cases.allocator),
488491 .is_test = is_test,
......@@ -622,8 +625,8 @@ pub fn lowerToBuildSteps(
622625 }
623626
624627 for (case.deps.items) |dep| {
625 artifact.addAnonymousModule(dep.name, .{
626 .source_file = file_sources.get(dep.path).?,
628 artifact.root_module.addAnonymousImport(dep.name, .{
629 .root_source_file = file_sources.get(dep.path).?,
627630 });
628631 }
629632
......@@ -644,9 +647,8 @@ pub fn lowerToBuildSteps(
644647 parent_step.dependOn(&artifact.step);
645648 },
646649 .Execution => |expected_stdout| no_exec: {
647 const run = if (case.target.ofmt == .c) run_step: {
648 const target_info = std.zig.system.NativeTargetInfo.detect(case.target) catch |err|
649 std.debug.panic("unable to detect target host: {s}\n", .{@errorName(err)});
650 const run = if (case.target.target.ofmt == .c) run_step: {
651 const target_info = case.target.toNativeTargetInfo();
650652 if (host.getExternalExecutor(&target_info, .{ .link_libc = true }) != .native) {
651653 // We wouldn't be able to run the compiled C code.
652654 break :no_exec;
......@@ -666,7 +668,7 @@ pub fn lowerToBuildSteps(
666668 "--",
667669 "-lc",
668670 "-target",
669 case.target.zigTriple(b.allocator) catch @panic("OOM"),
671 case.target.target.zigTriple(b.allocator) catch @panic("OOM"),
670672 });
671673 run_c.addArtifactArg(artifact);
672674 break :run_step run_c;
......@@ -692,8 +694,7 @@ pub fn lowerToBuildSteps(
692694 continue; // Pass test.
693695 }
694696
695 const target_info = std.zig.system.NativeTargetInfo.detect(case.target) catch |err|
696 std.debug.panic("unable to detect target host: {s}\n", .{@errorName(err)});
697 const target_info = case.target.toNativeTargetInfo();
697698 if (host.getExternalExecutor(&target_info, .{ .link_libc = true }) != .native) {
698699 // We wouldn't be able to run the compiled C code.
699700 continue; // Pass test.
......@@ -1159,9 +1160,9 @@ const TestManifest = struct {
11591160 }
11601161
11611162 fn getDefaultParser(comptime T: type) ParseFn(T) {
1162 if (T == CrossTarget) return struct {
1163 if (T == std.zig.CrossTarget) return struct {
11631164 fn parse(str: []const u8) anyerror!T {
1164 return CrossTarget.parse(.{ .arch_os_abi = str });
1165 return std.zig.CrossTarget.parse(.{ .arch_os_abi = str });
11651166 }
11661167 }.parse;
11671168
......@@ -1198,7 +1199,6 @@ const builtin = @import("builtin");
11981199const std = @import("std");
11991200const assert = std.debug.assert;
12001201const Allocator = std.mem.Allocator;
1201const CrossTarget = std.zig.CrossTarget;
12021202const Compilation = @import("../../src/Compilation.zig");
12031203const zig_h = @import("../../src/link.zig").File.C.zig_h;
12041204const introspect = @import("../../src/introspect.zig");
......@@ -1287,7 +1287,7 @@ pub fn main() !void {
12871287
12881288 if (cases.items.len == 0) {
12891289 const backends = try manifest.getConfigForKeyAlloc(arena, "backend", Backend);
1290 const targets = try manifest.getConfigForKeyAlloc(arena, "target", CrossTarget);
1290 const targets = try manifest.getConfigForKeyAlloc(arena, "target", std.zig.CrossTarget);
12911291 const c_frontends = try manifest.getConfigForKeyAlloc(ctx.arena, "c_frontend", CFrontend);
12921292 const is_test = try manifest.getConfigForKeyAssertSingle("is_test", bool);
12931293 const link_libc = try manifest.getConfigForKeyAssertSingle("link_libc", bool);
......@@ -1295,12 +1295,12 @@ pub fn main() !void {
12951295
12961296 if (manifest.type == .translate_c) {
12971297 for (c_frontends) |c_frontend| {
1298 for (targets) |target| {
1298 for (targets) |target_query| {
12991299 const output = try manifest.trailingLinesSplit(ctx.arena);
13001300 try ctx.translate.append(.{
13011301 .name = std.fs.path.stem(filename),
13021302 .c_frontend = c_frontend,
1303 .target = target,
1303 .target = resolveTargetQuery(target_query),
13041304 .is_test = is_test,
13051305 .link_libc = link_libc,
13061306 .input = src,
......@@ -1312,12 +1312,12 @@ pub fn main() !void {
13121312 }
13131313 if (manifest.type == .run_translated_c) {
13141314 for (c_frontends) |c_frontend| {
1315 for (targets) |target| {
1315 for (targets) |target_query| {
13161316 const output = try manifest.trailingSplit(ctx.arena);
13171317 try ctx.translate.append(.{
13181318 .name = std.fs.path.stem(filename),
13191319 .c_frontend = c_frontend,
1320 .target = target,
1320 .target = resolveTargetQuery(target_query),
13211321 .is_test = is_test,
13221322 .link_libc = link_libc,
13231323 .output = output,
......@@ -1385,6 +1385,17 @@ pub fn main() !void {
13851385 return runCases(&ctx, zig_exe_path);
13861386}
13871387
1388fn resolveTargetQuery(query: std.zig.CrossTarget) std.Build.ResolvedTarget {
1389 const result = std.zig.system.NativeTargetInfo.detect(query) catch
1390 @panic("unable to resolve target query");
1391
1392 return .{
1393 .query = query,
1394 .target = result.target,
1395 .dynamic_linker = result.dynamic_linker,
1396 };
1397}
1398
13881399fn runCases(self: *Cases, zig_exe_path: []const u8) !void {
13891400 const host = try std.zig.system.NativeTargetInfo.detect(.{});
13901401
test/src/CompareOutput.zig+3-3
......@@ -96,7 +96,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
9696
9797 const exe = b.addExecutable(.{
9898 .name = "test",
99 .target = .{},
99 .target = b.host,
100100 .optimize = .Debug,
101101 });
102102 exe.addAssemblyFile(write_src.files.items[0].getPath());
......@@ -121,7 +121,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
121121 .name = "test",
122122 .root_source_file = write_src.files.items[0].getPath(),
123123 .optimize = optimize,
124 .target = .{},
124 .target = b.host,
125125 });
126126 if (case.link_libc) {
127127 exe.linkSystemLibrary("c");
......@@ -146,7 +146,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
146146 const exe = b.addExecutable(.{
147147 .name = "test",
148148 .root_source_file = write_src.files.items[0].getPath(),
149 .target = .{},
149 .target = b.host,
150150 .optimize = .Debug,
151151 });
152152 if (case.link_libc) {
test/src/StackTrace.zig+1-1
......@@ -77,7 +77,7 @@ fn addExpect(
7777 .name = "test",
7878 .root_source_file = write_src.files.items[0].getPath(),
7979 .optimize = optimize_mode,
80 .target = .{},
80 .target = b.host,
8181 });
8282
8383 const run = b.addRunArtifact(exe);
test/src/run_translated_c.zig+2-2
......@@ -11,7 +11,7 @@ pub const RunTranslatedCContext = struct {
1111 step: *std.Build.Step,
1212 test_index: usize,
1313 test_filter: ?[]const u8,
14 target: std.zig.CrossTarget,
14 target: std.Build.ResolvedTarget,
1515
1616 const TestCase = struct {
1717 name: []const u8,
......@@ -86,7 +86,7 @@ pub const RunTranslatedCContext = struct {
8686 }
8787 const translate_c = b.addTranslateC(.{
8888 .source_file = write_src.files.items[0].getPath(),
89 .target = .{},
89 .target = b.host,
9090 .optimize = .Debug,
9191 });
9292
test/src/translate_c.zig+2-2
......@@ -18,7 +18,7 @@ pub const TranslateCContext = struct {
1818 sources: ArrayList(SourceFile),
1919 expected_lines: ArrayList([]const u8),
2020 allow_warnings: bool,
21 target: CrossTarget = CrossTarget{},
21 target: CrossTarget = .{},
2222
2323 const SourceFile = struct {
2424 filename: []const u8,
......@@ -109,7 +109,7 @@ pub const TranslateCContext = struct {
109109
110110 const translate_c = b.addTranslateC(.{
111111 .source_file = write_src.files.items[0].getPath(),
112 .target = case.target,
112 .target = b.resolveTargetQuery(case.target),
113113 .optimize = .Debug,
114114 });
115115
test/standalone.zig-4
......@@ -89,10 +89,6 @@ pub const build_cases = [_]BuildCase{
8989 // .build_root = "test/standalone/issue_13970",
9090 // .import = @import("standalone/issue_13970/build.zig"),
9191 //},
92 .{
93 .build_root = "test/standalone/main_pkg_path",
94 .import = @import("standalone/main_pkg_path/build.zig"),
95 },
9692 .{
9793 .build_root = "test/standalone/shared_library",
9894 .import = @import("standalone/shared_library/build.zig"),
test/standalone/c_compiler/build.zig+2-2
......@@ -23,7 +23,7 @@ fn add(
2323 cpp_name: []const u8,
2424 optimize: std.builtin.OptimizeMode,
2525) void {
26 const target: std.zig.CrossTarget = .{};
26 const target = b.host;
2727
2828 const exe_c = b.addExecutable(.{
2929 .name = c_name,
......@@ -42,7 +42,7 @@ fn add(
4242 exe_cpp.addCSourceFile(.{ .file = .{ .path = "test.cpp" }, .flags = &[0][]const u8{} });
4343 exe_cpp.linkLibCpp();
4444
45 switch (target.getOsTag()) {
45 switch (target.target.os.tag) {
4646 .windows => {
4747 // https://github.com/ziglang/zig/issues/8531
4848 exe_cpp.want_lto = false;
test/standalone/child_process/build.zig+1-1
......@@ -6,7 +6,7 @@ pub fn build(b: *std.Build) void {
66 b.default_step = test_step;
77
88 const optimize: std.builtin.OptimizeMode = .Debug;
9 const target: std.zig.CrossTarget = .{};
9 const target = b.host;
1010
1111 if (builtin.os.tag == .wasi) return;
1212
test/standalone/compiler_rt_panic/build.zig+4-4
......@@ -4,16 +4,16 @@ pub fn build(b: *std.Build) void {
44 const test_step = b.step("test", "Test it");
55 b.default_step = test_step;
66
7 const target = b.standardTargetOptions(.{});
7 const resolved_target = b.standardTargetOptions(.{});
8 const target = resolved_target.target;
89 const optimize = b.standardOptimizeOption(.{});
910
10 const abi = target.getAbi();
11 if (target.getObjectFormat() != .elf or !(abi.isMusl() or abi.isGnu())) return;
11 if (target.ofmt != .elf or !(target.abi.isMusl() or target.abi.isGnu())) return;
1212
1313 const exe = b.addExecutable(.{
1414 .name = "main",
1515 .optimize = optimize,
16 .target = target,
16 .target = resolved_target,
1717 });
1818 exe.linkLibC();
1919 exe.addCSourceFile(.{
test/standalone/dep_diamond/build.zig+8-7
......@@ -7,21 +7,22 @@ pub fn build(b: *std.Build) void {
77 const optimize: std.builtin.OptimizeMode = .Debug;
88
99 const shared = b.createModule(.{
10 .source_file = .{ .path = "shared.zig" },
10 .root_source_file = .{ .path = "shared.zig" },
1111 });
1212
1313 const exe = b.addExecutable(.{
1414 .name = "test",
1515 .root_source_file = .{ .path = "test.zig" },
16 .target = b.host,
1617 .optimize = optimize,
1718 });
18 exe.addAnonymousModule("foo", .{
19 .source_file = .{ .path = "foo.zig" },
20 .dependencies = &.{.{ .name = "shared", .module = shared }},
19 exe.root_module.addAnonymousImport("foo", .{
20 .root_source_file = .{ .path = "foo.zig" },
21 .imports = &.{.{ .name = "shared", .module = shared }},
2122 });
22 exe.addAnonymousModule("bar", .{
23 .source_file = .{ .path = "bar.zig" },
24 .dependencies = &.{.{ .name = "shared", .module = shared }},
23 exe.root_module.addAnonymousImport("bar", .{
24 .root_source_file = .{ .path = "bar.zig" },
25 .imports = &.{.{ .name = "shared", .module = shared }},
2526 });
2627
2728 const run = b.addRunArtifact(exe);
test/standalone/dep_mutually_recursive/build.zig+6-5
......@@ -7,20 +7,21 @@ pub fn build(b: *std.Build) void {
77 const optimize: std.builtin.OptimizeMode = .Debug;
88
99 const foo = b.createModule(.{
10 .source_file = .{ .path = "foo.zig" },
10 .root_source_file = .{ .path = "foo.zig" },
1111 });
1212 const bar = b.createModule(.{
13 .source_file = .{ .path = "bar.zig" },
13 .root_source_file = .{ .path = "bar.zig" },
1414 });
15 foo.dependencies.put("bar", bar) catch @panic("OOM");
16 bar.dependencies.put("foo", foo) catch @panic("OOM");
15 foo.addImport("bar", bar);
16 bar.addImport("foo", foo);
1717
1818 const exe = b.addExecutable(.{
1919 .name = "test",
2020 .root_source_file = .{ .path = "test.zig" },
21 .target = b.host,
2122 .optimize = optimize,
2223 });
23 exe.addModule("foo", foo);
24 exe.root_module.addImport("foo", foo);
2425
2526 const run = b.addRunArtifact(exe);
2627 test_step.dependOn(&run.step);
test/standalone/dep_recursive/build.zig+4-3
......@@ -7,16 +7,17 @@ pub fn build(b: *std.Build) void {
77 const optimize: std.builtin.OptimizeMode = .Debug;
88
99 const foo = b.createModule(.{
10 .source_file = .{ .path = "foo.zig" },
10 .root_source_file = .{ .path = "foo.zig" },
1111 });
12 foo.dependencies.put("foo", foo) catch @panic("OOM");
12 foo.addImport("foo", foo);
1313
1414 const exe = b.addExecutable(.{
1515 .name = "test",
1616 .root_source_file = .{ .path = "test.zig" },
17 .target = b.host,
1718 .optimize = optimize,
1819 });
19 exe.addModule("foo", foo);
20 exe.root_module.addImport("foo", foo);
2021
2122 const run = b.addRunArtifact(exe);
2223 test_step.dependOn(&run.step);
test/standalone/dep_shared_builtin/build.zig+3-2
......@@ -9,10 +9,11 @@ pub fn build(b: *std.Build) void {
99 const exe = b.addExecutable(.{
1010 .name = "test",
1111 .root_source_file = .{ .path = "test.zig" },
12 .target = b.host,
1213 .optimize = optimize,
1314 });
14 exe.addAnonymousModule("foo", .{
15 .source_file = .{ .path = "foo.zig" },
15 exe.root_module.addAnonymousImport("foo", .{
16 .root_source_file = .{ .path = "foo.zig" },
1617 });
1718
1819 const run = b.addRunArtifact(exe);
test/standalone/dep_triangle/build.zig+6-5
......@@ -7,19 +7,20 @@ pub fn build(b: *std.Build) void {
77 const optimize: std.builtin.OptimizeMode = .Debug;
88
99 const shared = b.createModule(.{
10 .source_file = .{ .path = "shared.zig" },
10 .root_source_file = .{ .path = "shared.zig" },
1111 });
1212
1313 const exe = b.addExecutable(.{
1414 .name = "test",
1515 .root_source_file = .{ .path = "test.zig" },
16 .target = b.host,
1617 .optimize = optimize,
1718 });
18 exe.addAnonymousModule("foo", .{
19 .source_file = .{ .path = "foo.zig" },
20 .dependencies = &.{.{ .name = "shared", .module = shared }},
19 exe.root_module.addAnonymousImport("foo", .{
20 .root_source_file = .{ .path = "foo.zig" },
21 .imports = &.{.{ .name = "shared", .module = shared }},
2122 });
22 exe.addModule("shared", shared);
23 exe.root_module.addImport("shared", shared);
2324
2425 const run = b.addRunArtifact(exe);
2526 test_step.dependOn(&run.step);
test/standalone/embed_generated_file/build.zig+4-4
......@@ -7,10 +7,10 @@ pub fn build(b: *std.Build) void {
77 const bootloader = b.addExecutable(.{
88 .name = "bootloader",
99 .root_source_file = .{ .path = "bootloader.zig" },
10 .target = .{
10 .target = b.resolveTargetQuery(.{
1111 .cpu_arch = .x86,
1212 .os_tag = .freestanding,
13 },
13 }),
1414 .optimize = .ReleaseSmall,
1515 });
1616
......@@ -18,8 +18,8 @@ pub fn build(b: *std.Build) void {
1818 .root_source_file = .{ .path = "main.zig" },
1919 .optimize = .Debug,
2020 });
21 exe.addAnonymousModule("bootloader.elf", .{
22 .source_file = bootloader.getEmittedBin(),
21 exe.root_module.addAnonymousImport("bootloader.elf", .{
22 .root_source_file = bootloader.getEmittedBin(),
2323 });
2424
2525 // TODO: actually check the output
test/standalone/empty_env/build.zig+1
......@@ -15,6 +15,7 @@ pub fn build(b: *std.Build) void {
1515 const main = b.addExecutable(.{
1616 .name = "main",
1717 .root_source_file = .{ .path = "main.zig" },
18 .target = b.host,
1819 .optimize = optimize,
1920 });
2021
test/standalone/extern/build.zig+1-1
......@@ -6,7 +6,7 @@ pub fn build(b: *std.Build) void {
66 const obj = b.addObject(.{
77 .name = "exports",
88 .root_source_file = .{ .path = "exports.zig" },
9 .target = .{},
9 .target = b.host,
1010 .optimize = optimize,
1111 });
1212 const main = b.addTest(.{
test/standalone/global_linkage/build.zig+1-1
......@@ -5,7 +5,7 @@ pub fn build(b: *std.Build) void {
55 b.default_step = test_step;
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
8 const target: std.zig.CrossTarget = .{};
8 const target = b.host;
99
1010 const obj1 = b.addStaticLibrary(.{
1111 .name = "obj1",
test/standalone/install_raw_hex/build.zig+2-2
......@@ -5,12 +5,12 @@ pub fn build(b: *std.Build) void {
55 const test_step = b.step("test", "Test it");
66 b.default_step = test_step;
77
8 const target = .{
8 const target = b.resolveTargetQuery(.{
99 .cpu_arch = .thumb,
1010 .cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m4 },
1111 .os_tag = .freestanding,
1212 .abi = .gnueabihf,
13 };
13 });
1414
1515 const optimize: std.builtin.OptimizeMode = .Debug;
1616
test/standalone/ios/build.zig+4-4
......@@ -8,12 +8,12 @@ pub fn build(b: *std.Build) void {
88 b.default_step = test_step;
99
1010 const optimize: std.builtin.OptimizeMode = .Debug;
11 const target: std.zig.CrossTarget = .{
11 const target = b.resolveTargetQuery(.{
1212 .cpu_arch = .aarch64,
1313 .os_tag = .ios,
14 };
15 const target_info = std.zig.system.NativeTargetInfo.detect(target) catch @panic("couldn't detect native target");
16 const sdk = std.zig.system.darwin.getSdk(b.allocator, target_info.target) orelse @panic("no iOS SDK found");
14 });
15 const sdk = std.zig.system.darwin.getSdk(b.allocator, target.target) orelse
16 @panic("no iOS SDK found");
1717 b.sysroot = sdk;
1818
1919 const exe = b.addExecutable(.{
test/standalone/issue_11595/build.zig+1-1
......@@ -6,7 +6,7 @@ pub fn build(b: *std.Build) void {
66 b.default_step = test_step;
77
88 const optimize: std.builtin.OptimizeMode = .Debug;
9 const target: std.zig.CrossTarget = .{};
9 const target = b.host;
1010
1111 if (builtin.os.tag == .windows) {
1212 // https://github.com/ziglang/zig/issues/12419
test/standalone/issue_12588/build.zig+1-2
......@@ -5,13 +5,12 @@ pub fn build(b: *std.Build) void {
55 b.default_step = test_step;
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
8 const target: std.zig.CrossTarget = .{};
98
109 const obj = b.addObject(.{
1110 .name = "main",
1211 .root_source_file = .{ .path = "main.zig" },
1312 .optimize = optimize,
14 .target = target,
13 .target = b.host,
1514 });
1615 _ = obj.getEmittedLlvmIr();
1716 _ = obj.getEmittedLlvmBc();
test/standalone/issue_12706/build.zig+1-2
......@@ -1,13 +1,12 @@
11const std = @import("std");
22const builtin = @import("builtin");
3const CrossTarget = std.zig.CrossTarget;
43
54pub fn build(b: *std.Build) void {
65 const test_step = b.step("test", "Test it");
76 b.default_step = test_step;
87
98 const optimize: std.builtin.OptimizeMode = .Debug;
10 const target: std.zig.CrossTarget = .{};
9 const target = b.host;
1110
1211 const exe = b.addExecutable(.{
1312 .name = "main",
test/standalone/issue_339/build.zig+1-1
......@@ -5,7 +5,7 @@ pub fn build(b: *std.Build) void {
55 b.default_step = test_step;
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
8 const target: std.zig.CrossTarget = .{};
8 const target = b.host;
99
1010 const obj = b.addObject(.{
1111 .name = "test",
test/standalone/issue_8550/build.zig+2-2
......@@ -5,13 +5,13 @@ pub fn build(b: *std.Build) !void {
55 b.default_step = test_step;
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
8 const target = std.zig.CrossTarget{
8 const target = b.resolveTargetQuery(.{
99 .os_tag = .freestanding,
1010 .cpu_arch = .arm,
1111 .cpu_model = .{
1212 .explicit = &std.Target.arm.cpu.arm1176jz_s,
1313 },
14 };
14 });
1515
1616 const kernel = b.addExecutable(.{
1717 .name = "kernel",
test/standalone/load_dynamic_library/build.zig+1-1
......@@ -6,7 +6,7 @@ pub fn build(b: *std.Build) void {
66 b.default_step = test_step;
77
88 const optimize: std.builtin.OptimizeMode = .Debug;
9 const target: std.zig.CrossTarget = .{};
9 const target = b.host;
1010
1111 if (builtin.os.tag == .wasi) return;
1212
test/standalone/main_pkg_path/a/test.zig deleted-5
......@@ -1,5 +0,0 @@
1const b = @import("../b.zig");
2
3test "main pkg path" {
4 b.foo();
5}
test/standalone/main_pkg_path/b.zig deleted-1
......@@ -1 +0,0 @@
1pub fn foo() void {}
test/standalone/main_pkg_path/build.zig deleted-13
......@@ -1,13 +0,0 @@
1const std = @import("std");
2
3pub fn build(b: *std.Build) void {
4 const test_step = b.step("test", "Test it");
5 b.default_step = test_step;
6
7 const test_exe = b.addTest(.{
8 .root_source_file = .{ .path = "a/test.zig" },
9 .main_pkg_path = .{ .path = "." },
10 });
11
12 test_step.dependOn(&b.addRunArtifact(test_exe).step);
13}
test/standalone/mix_c_files/build.zig+1
......@@ -19,6 +19,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1919 const exe = b.addExecutable(.{
2020 .name = "test",
2121 .root_source_file = .{ .path = "main.zig" },
22 .target = b.host,
2223 .optimize = optimize,
2324 });
2425 exe.addCSourceFile(.{ .file = .{ .path = "test.c" }, .flags = &[_][]const u8{"-std=c11"} });
test/standalone/mix_o_files/build.zig+1-1
......@@ -5,7 +5,7 @@ pub fn build(b: *std.Build) void {
55 b.default_step = test_step;
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
8 const target: std.zig.CrossTarget = .{};
8 const target = b.host;
99
1010 const obj = b.addObject(.{
1111 .name = "base64",
test/standalone/options/build.zig+1-1
......@@ -3,7 +3,7 @@ const std = @import("std");
33pub fn build(b: *std.Build) void {
44 const main = b.addTest(.{
55 .root_source_file = .{ .path = "src/main.zig" },
6 .target = .{},
6 .target = b.host,
77 .optimize = .Debug,
88 });
99
test/standalone/pie/build.zig+2-2
......@@ -5,10 +5,10 @@ pub fn build(b: *std.Build) void {
55 b.default_step = test_step;
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
8 const target: std.zig.CrossTarget = .{
8 const target = b.resolveTargetQuery(.{
99 .os_tag = .linux,
1010 .cpu_arch = .x86_64,
11 };
11 });
1212
1313 const main = b.addTest(.{
1414 .root_source_file = .{ .path = "main.zig" },
test/standalone/pkg_import/build.zig+2-1
......@@ -10,8 +10,9 @@ pub fn build(b: *std.Build) void {
1010 .name = "test",
1111 .root_source_file = .{ .path = "test.zig" },
1212 .optimize = optimize,
13 .target = b.host,
1314 });
14 exe.addAnonymousModule("my_pkg", .{ .source_file = .{ .path = "pkg.zig" } });
15 exe.root_module.addAnonymousImport("my_pkg", .{ .root_source_file = .{ .path = "pkg.zig" } });
1516
1617 const run = b.addRunArtifact(exe);
1718 test_step.dependOn(&run.step);
test/standalone/self_exe_symlink/build.zig+2-2
......@@ -7,11 +7,11 @@ pub fn build(b: *std.Build) void {
77 b.default_step = test_step;
88
99 const optimize: std.builtin.OptimizeMode = .Debug;
10 const target: std.zig.CrossTarget = .{};
10 const target = b.host;
1111
1212 // The test requires getFdPath in order to to get the path of the
1313 // File returned by openSelfExe
14 if (!std.os.isGetFdPathSupportedOnTarget(target.getOs())) return;
14 if (!std.os.isGetFdPathSupportedOnTarget(target.target.os)) return;
1515
1616 const main = b.addExecutable(.{
1717 .name = "main",
test/standalone/shared_library/build.zig+1-1
......@@ -10,7 +10,7 @@ pub fn build(b: *std.Build) void {
1010 }
1111
1212 const optimize: std.builtin.OptimizeMode = .Debug;
13 const target: std.zig.CrossTarget = .{};
13 const target = b.host;
1414 const lib = b.addSharedLibrary(.{
1515 .name = "mathtest",
1616 .root_source_file = .{ .path = "mathtest.zig" },
test/standalone/stack_iterator/build.zig+9-10
......@@ -22,11 +22,10 @@ pub fn build(b: *std.Build) void {
2222 .root_source_file = .{ .path = "unwind.zig" },
2323 .target = target,
2424 .optimize = optimize,
25 .unwind_tables = target.target.isDarwin(),
26 .omit_frame_pointer = false,
2527 });
2628
27 if (target.isDarwin()) exe.unwind_tables = true;
28 exe.omit_frame_pointer = false;
29
3029 const run_cmd = b.addRunArtifact(exe);
3130 test_step.dependOn(&run_cmd.step);
3231 }
......@@ -46,11 +45,10 @@ pub fn build(b: *std.Build) void {
4645 .root_source_file = .{ .path = "unwind.zig" },
4746 .target = target,
4847 .optimize = optimize,
48 .unwind_tables = true,
49 .omit_frame_pointer = true,
4950 });
5051
51 exe.omit_frame_pointer = true;
52 exe.unwind_tables = true;
53
5452 const run_cmd = b.addRunArtifact(exe);
5553 test_step.dependOn(&run_cmd.step);
5654 }
......@@ -69,11 +67,12 @@ pub fn build(b: *std.Build) void {
6967 .name = "c_shared_lib",
7068 .target = target,
7169 .optimize = optimize,
70 .strip = false,
7271 });
7372
74 if (target.isWindows()) c_shared_lib.defineCMacro("LIB_API", "__declspec(dllexport)");
73 if (target.target.os.tag == .windows)
74 c_shared_lib.defineCMacro("LIB_API", "__declspec(dllexport)");
7575
76 c_shared_lib.strip = false;
7776 c_shared_lib.addCSourceFile(.{
7877 .file = .{ .path = "shared_lib.c" },
7978 .flags = &.{"-fomit-frame-pointer"},
......@@ -85,10 +84,10 @@ pub fn build(b: *std.Build) void {
8584 .root_source_file = .{ .path = "shared_lib_unwind.zig" },
8685 .target = target,
8786 .optimize = optimize,
87 .unwind_tables = target.target.isDarwin(),
88 .omit_frame_pointer = true,
8889 });
8990
90 if (target.isDarwin()) exe.unwind_tables = true;
91 exe.omit_frame_pointer = true;
9291 exe.linkLibrary(c_shared_lib);
9392
9493 const run_cmd = b.addRunArtifact(exe);
test/standalone/static_c_lib/build.zig+1-1
......@@ -9,7 +9,7 @@ pub fn build(b: *std.Build) void {
99 const foo = b.addStaticLibrary(.{
1010 .name = "foo",
1111 .optimize = optimize,
12 .target = .{},
12 .target = b.host,
1313 });
1414 foo.addCSourceFile(.{ .file = .{ .path = "foo.c" }, .flags = &[_][]const u8{} });
1515 foo.addIncludePath(.{ .path = "." });
test/standalone/strip_empty_loop/build.zig+2-2
......@@ -5,15 +5,15 @@ pub fn build(b: *std.Build) void {
55 b.default_step = test_step;
66
77 const optimize = std.builtin.OptimizeMode.Debug;
8 const target = std.zig.CrossTarget{};
8 const target = b.host;
99
1010 const main = b.addExecutable(.{
1111 .name = "main",
1212 .root_source_file = .{ .path = "main.zig" },
1313 .optimize = optimize,
1414 .target = target,
15 .strip = true,
1516 });
16 main.strip = true;
1717
1818 // TODO: actually check the output
1919 _ = main.getEmittedBin();
test/standalone/strip_struct_init/build.zig+1-1
......@@ -9,8 +9,8 @@ pub fn build(b: *std.Build) void {
99 const main = b.addTest(.{
1010 .root_source_file = .{ .path = "main.zig" },
1111 .optimize = optimize,
12 .strip = true,
1213 });
13 main.strip = true;
1414
1515 test_step.dependOn(&b.addRunArtifact(main).step);
1616}
test/standalone/test_runner_module_imports/build.zig+4-4
......@@ -6,13 +6,13 @@ pub fn build(b: *std.Build) void {
66 .test_runner = "test_runner/main.zig",
77 });
88
9 const module1 = b.createModule(.{ .source_file = .{ .path = "module1/main.zig" } });
9 const module1 = b.createModule(.{ .root_source_file = .{ .path = "module1/main.zig" } });
1010 const module2 = b.createModule(.{
11 .source_file = .{ .path = "module2/main.zig" },
12 .dependencies = &.{.{ .name = "module1", .module = module1 }},
11 .root_source_file = .{ .path = "module2/main.zig" },
12 .imports = &.{.{ .name = "module1", .module = module1 }},
1313 });
1414
15 t.addModule("module2", module2);
15 t.root_module.addImport("module2", module2);
1616
1717 const test_step = b.step("test", "Run unit tests");
1818 test_step.dependOn(&b.addRunArtifact(t).step);
test/standalone/windows_resources/build.zig+10-6
......@@ -4,21 +4,25 @@ pub fn build(b: *std.Build) void {
44 const test_step = b.step("test", "Test it");
55 b.default_step = test_step;
66
7 const native_target: std.zig.CrossTarget = .{};
8 const cross_target = .{
7 const cross_target = b.resolveTargetQuery(.{
98 .cpu_arch = .x86_64,
109 .os_tag = .windows,
1110 .abi = .gnu,
12 };
11 });
1312
14 add(b, native_target, .any, test_step);
13 add(b, b.host, .any, test_step);
1514 add(b, cross_target, .any, test_step);
1615
17 add(b, native_target, .gnu, test_step);
16 add(b, b.host, .gnu, test_step);
1817 add(b, cross_target, .gnu, test_step);
1918}
2019
21fn add(b: *std.Build, target: std.zig.CrossTarget, rc_includes: enum { any, gnu }, test_step: *std.Build.Step) void {
20fn add(
21 b: *std.Build,
22 target: std.Build.ResolvedTarget,
23 rc_includes: enum { any, gnu },
24 test_step: *std.Build.Step,
25) void {
2226 const exe = b.addExecutable(.{
2327 .name = "zig_resource_test",
2428 .root_source_file = .{ .path = "main.zig" },
test/standalone/windows_spawn/build.zig+1-1
......@@ -6,7 +6,7 @@ pub fn build(b: *std.Build) void {
66 b.default_step = test_step;
77
88 const optimize: std.builtin.OptimizeMode = .Debug;
9 const target: std.zig.CrossTarget = .{};
9 const target = b.host;
1010
1111 if (builtin.os.tag != .windows) return;
1212
test/standalone/zerolength_check/build.zig+2-2
......@@ -13,11 +13,11 @@ pub fn build(b: *std.Build) void {
1313fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1414 const unit_tests = b.addTest(.{
1515 .root_source_file = .{ .path = "src/main.zig" },
16 .target = .{
16 .target = b.resolveTargetQuery(.{
1717 .os_tag = .wasi,
1818 .cpu_arch = .wasm32,
1919 .cpu_features_add = std.Target.wasm.featureSet(&.{.bulk_memory}),
20 },
20 }),
2121 .optimize = optimize,
2222 });
2323
test/tests.zig+34-35
......@@ -1,7 +1,6 @@
11const std = @import("std");
22const builtin = @import("builtin");
33const assert = std.debug.assert;
4const CrossTarget = std.zig.CrossTarget;
54const mem = std.mem;
65const OptimizeMode = std.builtin.OptimizeMode;
76const Step = std.Build.Step;
......@@ -22,13 +21,13 @@ pub const CompareOutputContext = @import("src/CompareOutput.zig");
2221pub const StackTracesContext = @import("src/StackTrace.zig");
2322
2423const TestTarget = struct {
25 target: CrossTarget = .{},
24 target: std.zig.CrossTarget = .{},
2625 optimize_mode: std.builtin.OptimizeMode = .Debug,
2726 link_libc: ?bool = null,
2827 single_threaded: ?bool = null,
2928 use_llvm: ?bool = null,
3029 use_lld: ?bool = null,
31 force_pic: ?bool = null,
30 pic: ?bool = null,
3231 strip: ?bool = null,
3332};
3433
......@@ -105,7 +104,7 @@ const test_targets = blk: {
105104 },
106105 .use_llvm = false,
107106 .use_lld = false,
108 .force_pic = true,
107 .pic = true,
109108 },
110109 .{
111110 .target = .{
......@@ -146,7 +145,7 @@ const test_targets = blk: {
146145 //},
147146 // https://github.com/ziglang/zig/issues/13623
148147 //.{
149 // .target = CrossTarget.parse(.{
148 // .target = std.zig.CrossTarget.parse(.{
150149 // .arch_os_abi = "arm-linux-none",
151150 // .cpu_features = "generic+v8a",
152151 // }) catch unreachable,
......@@ -287,13 +286,13 @@ const test_targets = blk: {
287286 },
288287
289288 .{
290 .target = CrossTarget.parse(.{
289 .target = std.zig.CrossTarget.parse(.{
291290 .arch_os_abi = "arm-linux-none",
292291 .cpu_features = "generic+v8a",
293292 }) catch unreachable,
294293 },
295294 .{
296 .target = CrossTarget.parse(.{
295 .target = std.zig.CrossTarget.parse(.{
297296 .arch_os_abi = "arm-linux-musleabihf",
298297 .cpu_features = "generic+v8a",
299298 }) catch unreachable,
......@@ -301,7 +300,7 @@ const test_targets = blk: {
301300 },
302301 // https://github.com/ziglang/zig/issues/3287
303302 //.{
304 // .target = CrossTarget.parse(.{
303 // .target = std.zig.CrossTarget.parse(.{
305304 // .arch_os_abi = "arm-linux-gnueabihf",
306305 // .cpu_features = "generic+v8a",
307306 // }) catch unreachable,
......@@ -495,10 +494,10 @@ const test_targets = blk: {
495494};
496495
497496const CAbiTarget = struct {
498 target: CrossTarget = .{},
497 target: std.zig.CrossTarget = .{},
499498 use_llvm: ?bool = null,
500499 use_lld: ?bool = null,
501 force_pic: ?bool = null,
500 pic: ?bool = null,
502501 strip: ?bool = null,
503502 c_defines: []const []const u8 = &.{},
504503};
......@@ -543,7 +542,7 @@ const c_abi_targets = [_]CAbiTarget{
543542 },
544543 .use_llvm = false,
545544 .use_lld = false,
546 .force_pic = true,
545 .pic = true,
547546 .c_defines = &.{"ZIG_BACKEND_STAGE2_X86_64"},
548547 },
549548 .{
......@@ -645,7 +644,7 @@ pub fn addStackTraceTests(
645644 const check_exe = b.addExecutable(.{
646645 .name = "check-stack-trace",
647646 .root_source_file = .{ .path = "test/src/check-stack-trace.zig" },
648 .target = .{},
647 .target = b.host,
649648 .optimize = .Debug,
650649 });
651650
......@@ -682,12 +681,14 @@ pub fn addStandaloneTests(
682681 if (os_tag != builtin.os.tag) continue;
683682 }
684683
684 const resolved_target = b.resolveTargetQuery(case.target);
685
685686 if (case.is_exe) {
686687 const exe = b.addExecutable(.{
687688 .name = std.fs.path.stem(case.src_path),
688689 .root_source_file = .{ .path = case.src_path },
689690 .optimize = optimize,
690 .target = case.target,
691 .target = resolved_target,
691692 });
692693 if (case.link_libc) exe.linkLibC();
693694
......@@ -701,7 +702,7 @@ pub fn addStandaloneTests(
701702 .name = std.fs.path.stem(case.src_path),
702703 .root_source_file = .{ .path = case.src_path },
703704 .optimize = optimize,
704 .target = case.target,
705 .target = resolved_target,
705706 });
706707 if (case.link_libc) exe.linkLibC();
707708
......@@ -1001,7 +1002,7 @@ pub fn addTranslateCTests(b: *std.Build, test_filter: ?[]const u8) *Step {
10011002pub fn addRunTranslatedCTests(
10021003 b: *std.Build,
10031004 test_filter: ?[]const u8,
1004 target: std.zig.CrossTarget,
1005 target: std.Build.ResolvedTarget,
10051006) *Step {
10061007 const cases = b.allocator.create(RunTranslatedCContext) catch @panic("OOM");
10071008 cases.* = .{
......@@ -1105,7 +1106,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
11051106 const these_tests = b.addTest(.{
11061107 .root_source_file = .{ .path = options.root_src },
11071108 .optimize = test_target.optimize_mode,
1108 .target = test_target.target,
1109 .target = b.resolveTargetQuery(test_target.target),
11091110 .max_rss = max_rss,
11101111 .filter = options.test_filter,
11111112 .link_libc = test_target.link_libc,
......@@ -1113,9 +1114,9 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
11131114 .use_llvm = test_target.use_llvm,
11141115 .use_lld = test_target.use_lld,
11151116 .zig_lib_dir = .{ .path = "lib" },
1117 .pic = test_target.pic,
1118 .strip = test_target.strip,
11161119 });
1117 these_tests.force_pic = test_target.force_pic;
1118 these_tests.strip = test_target.strip;
11191120 const single_threaded_suffix = if (test_target.single_threaded == true) "-single" else "";
11201121 const backend_suffix = if (test_target.use_llvm == true)
11211122 "-llvm"
......@@ -1126,7 +1127,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
11261127 else
11271128 "";
11281129 const use_lld = if (test_target.use_lld == false) "-no-lld" else "";
1129 const use_pic = if (test_target.force_pic == true) "-pic" else "";
1130 const use_pic = if (test_target.pic == true) "-pic" else "";
11301131
11311132 these_tests.addIncludePath(.{ .path = "test" });
11321133
......@@ -1154,7 +1155,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
11541155 const compile_c = b.addExecutable(.{
11551156 .name = qualified_name,
11561157 .link_libc = test_target.link_libc,
1157 .target = altered_target,
1158 .target = b.resolveTargetQuery(altered_target),
11581159 .zig_lib_dir = .{ .path = "lib" },
11591160 });
11601161 compile_c.addCSourceFile(.{
......@@ -1229,41 +1230,39 @@ pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *S
12291230 for (c_abi_targets) |c_abi_target| {
12301231 if (skip_non_native and !c_abi_target.target.isNative()) continue;
12311232
1232 if (c_abi_target.target.isWindows() and c_abi_target.target.getCpuArch() == .aarch64) {
1233 const resolved_target = b.resolveTargetQuery(c_abi_target.target);
1234 const target = resolved_target.target;
1235
1236 if (target.os.tag == .windows and target.cpu.arch == .aarch64) {
12331237 // https://github.com/ziglang/zig/issues/14908
12341238 continue;
12351239 }
12361240
12371241 const test_step = b.addTest(.{
12381242 .name = b.fmt("test-c-abi-{s}-{s}-{s}{s}{s}{s}", .{
1239 c_abi_target.target.zigTriple(b.allocator) catch @panic("OOM"),
1240 c_abi_target.target.getCpuModel().name,
1243 target.zigTriple(b.allocator) catch @panic("OOM"),
1244 target.cpu.model.name,
12411245 @tagName(optimize_mode),
12421246 if (c_abi_target.use_llvm == true)
12431247 "-llvm"
1244 else if (c_abi_target.target.ofmt == std.Target.ObjectFormat.c)
1248 else if (target.ofmt == .c)
12451249 "-cbe"
12461250 else if (c_abi_target.use_llvm == false)
12471251 "-selfhosted"
12481252 else
12491253 "",
12501254 if (c_abi_target.use_lld == false) "-no-lld" else "",
1251 if (c_abi_target.force_pic == true) "-pic" else "",
1255 if (c_abi_target.pic == true) "-pic" else "",
12521256 }),
12531257 .root_source_file = .{ .path = "test/c_abi/main.zig" },
1254 .target = c_abi_target.target,
1258 .target = resolved_target,
12551259 .optimize = optimize_mode,
12561260 .link_libc = true,
12571261 .use_llvm = c_abi_target.use_llvm,
12581262 .use_lld = c_abi_target.use_lld,
1263 .pic = c_abi_target.pic,
1264 .strip = c_abi_target.strip,
12591265 });
1260 test_step.force_pic = c_abi_target.force_pic;
1261 test_step.strip = c_abi_target.strip;
1262 if (c_abi_target.target.abi != null and c_abi_target.target.abi.?.isMusl()) {
1263 // TODO NativeTargetInfo insists on dynamically linking musl
1264 // for some reason?
1265 test_step.target_info.dynamic_linker.max_byte = null;
1266 }
12671266 test_step.addCSourceFile(.{
12681267 .file = .{ .path = "test/c_abi/cfuncs.c" },
12691268 .flags = &.{"-std=c99"},
......@@ -1297,8 +1296,8 @@ pub fn addCases(
12971296 var dir = try b.build_root.handle.openDir("test/cases", .{ .iterate = true });
12981297 defer dir.close();
12991298
1300 cases.addFromDir(dir);
1301 try @import("cases.zig").addCases(&cases, build_options);
1299 cases.addFromDir(dir, b);
1300 try @import("cases.zig").addCases(&cases, build_options, b);
13021301
13031302 const cases_dir_path = try b.build_root.join(b.allocator, &.{ "test", "cases" });
13041303 cases.lowerToBuildSteps(