From ce95a3b153e9f3e83232e641c26a41e7dbd01165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20=28xq=29=20Quei=C3=9Fner?= Date: Wed, 19 Jul 2023 10:49:34 +0200 Subject: [PATCH 01/10] Build.zig rename orgy (aka: #16353). Renames FileSource to LazyPath and removes functions that take literal paths instead of LazyPath. --- build.zig | 35 ++-- lib/std/Build.zig | 50 ++--- lib/std/Build/Step/CheckFile.zig | 4 +- lib/std/Build/Step/CheckObject.zig | 14 +- lib/std/Build/Step/Compile.zig | 171 ++++++++---------- lib/std/Build/Step/ConfigHeader.zig | 16 +- lib/std/Build/Step/InstallArtifact.zig | 8 +- lib/std/Build/Step/InstallDir.zig | 4 +- lib/std/Build/Step/InstallFile.zig | 6 +- lib/std/Build/Step/ObjCopy.zig | 18 +- lib/std/Build/Step/Options.zig | 54 +++--- lib/std/Build/Step/Run.zig | 48 +++-- lib/std/Build/Step/TranslateC.zig | 16 +- lib/std/Build/Step/WriteFile.zig | 24 ++- .../interdependent_static_c_libs/build.zig | 10 +- test/link/macho/bugs/13056/build.zig | 10 +- test/link/macho/dead_strip/build.zig | 2 +- test/link/macho/dead_strip_dylibs/build.zig | 2 +- test/link/macho/dylib/build.zig | 10 +- test/link/macho/empty/build.zig | 4 +- test/link/macho/entry/build.zig | 2 +- test/link/macho/entry_in_archive/build.zig | 2 +- test/link/macho/entry_in_dylib/build.zig | 4 +- test/link/macho/headerpad/build.zig | 2 +- test/link/macho/needed_framework/build.zig | 2 +- test/link/macho/needed_library/build.zig | 8 +- test/link/macho/objc/build.zig | 6 +- test/link/macho/objcpp/build.zig | 6 +- test/link/macho/pagezero/build.zig | 4 +- test/link/macho/search_strategy/build.zig | 12 +- test/link/macho/stack_size/build.zig | 2 +- test/link/macho/tls/build.zig | 2 +- test/link/macho/unwind_info/build.zig | 2 +- test/link/macho/weak_framework/build.zig | 2 +- test/link/macho/weak_library/build.zig | 8 +- .../static_libs_from_object_files/build.zig | 12 +- test/link/wasm/extern/build.zig | 2 +- test/link/wasm/infer-features/build.zig | 2 +- test/src/Cases.zig | 6 +- test/src/CompareOutput.zig | 6 +- test/src/StackTrace.zig | 4 +- test/src/run_translated_c.zig | 2 +- test/src/translate_c.zig | 2 +- test/standalone/c_compiler/build.zig | 4 +- .../standalone/embed_generated_file/build.zig | 2 +- test/standalone/issue_794/build.zig | 2 +- test/standalone/issue_8550/build.zig | 4 +- test/standalone/main_pkg_path/build.zig | 2 +- test/standalone/mix_c_files/build.zig | 2 +- test/standalone/mix_o_files/build.zig | 5 +- test/standalone/shared_library/build.zig | 5 +- test/standalone/stack_iterator/build.zig | 5 +- test/standalone/static_c_lib/build.zig | 6 +- test/standalone/use_alias/build.zig | 2 +- test/tests.zig | 21 ++- 55 files changed, 337 insertions(+), 329 deletions(-) diff --git a/build.zig b/build.zig index 9e6e8638677e..b7db2d7aa81d 100644 --- a/build.zig +++ b/build.zig @@ -45,9 +45,9 @@ pub fn build(b: *std.Build) !void { const docgen_cmd = b.addRunArtifact(docgen_exe); docgen_cmd.addArgs(&.{ "--zig", b.zig_exe }); if (b.zig_lib_dir) |p| { - docgen_cmd.addArgs(&.{ "--zig-lib-dir", p }); + docgen_cmd.addArgs(&.{ "--zig-lib-dir", b.pathFromRoot(p) }); } - docgen_cmd.addFileSourceArg(.{ .path = "doc/langref.html.in" }); + docgen_cmd.addFileArg(.{ .path = "doc/langref.html.in" }); const langref_file = docgen_cmd.addOutputFileArg("langref.html"); const install_langref = b.addInstallFileWithDir(langref_file, .prefix, "doc/langref.html"); if (!skip_install_langref) { @@ -58,7 +58,7 @@ pub fn build(b: *std.Build) !void { .root_source_file = .{ .path = "lib/std/std.zig" }, .target = target, }); - autodoc_test.overrideZigLibDir("lib"); + autodoc_test.overrideZigLibDir(.{ .path = "lib" }); autodoc_test.emit_bin = .no_emit; // https://github.com/ziglang/zig/issues/16351 const install_std_docs = b.addInstallDirectory(.{ .source_dir = autodoc_test.getEmittedDocs(), @@ -89,7 +89,7 @@ pub fn build(b: *std.Build) !void { .root_source_file = .{ .path = "test/src/Cases.zig" }, .optimize = optimize, }); - check_case_exe.main_pkg_path = "."; + check_case_exe.setMainPkgPath(.{ .path = "." }); check_case_exe.stack_size = stack_size; check_case_exe.single_threaded = single_threaded; @@ -352,10 +352,9 @@ pub fn build(b: *std.Build) !void { exe_options.addOption(bool, "enable_tracy_allocation", tracy_allocation); exe_options.addOption(bool, "value_tracing", value_tracing); if (tracy) |tracy_path| { - const client_cpp = fs.path.join( - b.allocator, + const client_cpp = b.pathJoin( &[_][]const u8{ tracy_path, "public", "TracyClient.cpp" }, - ) catch unreachable; + ); // On mingw, we need to opt into windows 7+ to get some features required by tracy. const tracy_c_flags: []const []const u8 = if (target.isWindows() and target.getAbi() == .gnu) @@ -363,8 +362,8 @@ pub fn build(b: *std.Build) !void { else &[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" }; - exe.addIncludePath(tracy_path); - exe.addCSourceFile(client_cpp, tracy_c_flags); + exe.addIncludePath(.{ .path = tracy_path }); + exe.addCSourceFile(.{ .file = .{ .path = client_cpp }, .flags = tracy_c_flags }); if (!enable_llvm) { exe.linkSystemLibraryName("c++"); } @@ -554,7 +553,7 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void { }); run_opt.addArtifactArg(exe); run_opt.addArg("-o"); - run_opt.addFileSourceArg(.{ .path = "stage1/zig1.wasm" }); + run_opt.addFileArg(.{ .path = "stage1/zig1.wasm" }); const copy_zig_h = b.addWriteFiles(); copy_zig_h.addCopyFileToSource(.{ .path = "lib/zig.h" }, "stage1/zig.h"); @@ -603,7 +602,7 @@ fn addCmakeCfgOptionsToExe( // useful for package maintainers exe.headerpad_max_install_names = true; } - exe.addObjectFile(fs.path.join(b.allocator, &[_][]const u8{ + exe.addObjectFile(.{ .path = b.pathJoin(&[_][]const u8{ cfg.cmake_binary_dir, "zigcpp", b.fmt("{s}{s}{s}", .{ @@ -611,11 +610,11 @@ fn addCmakeCfgOptionsToExe( "zigcpp", cfg.cmake_static_library_suffix, }), - }) catch unreachable); + }) }); assert(cfg.lld_include_dir.len != 0); - exe.addIncludePath(cfg.lld_include_dir); - exe.addIncludePath(cfg.llvm_include_dir); - exe.addLibraryPath(cfg.llvm_lib_dir); + exe.addIncludePath(.{ .path = cfg.lld_include_dir }); + exe.addIncludePath(.{ .path = cfg.llvm_include_dir }); + exe.addLibraryPath(.{ .path = cfg.llvm_lib_dir }); addCMakeLibraryList(exe, cfg.clang_libraries); addCMakeLibraryList(exe, cfg.lld_libraries); addCMakeLibraryList(exe, cfg.llvm_libraries); @@ -671,7 +670,7 @@ fn addCmakeCfgOptionsToExe( } if (cfg.dia_guids_lib.len != 0) { - exe.addObjectFile(cfg.dia_guids_lib); + exe.addObjectFile(.{ .path = cfg.dia_guids_lib }); } } @@ -732,7 +731,7 @@ fn addCxxKnownPath( } return error.RequiredLibraryNotFound; } - exe.addObjectFile(path_unpadded); + exe.addObjectFile(.{ .path = path_unpadded }); // TODO a way to integrate with system c++ include files here // c++ -E -Wp,-v -xc++ /dev/null @@ -752,7 +751,7 @@ fn addCMakeLibraryList(exe: *std.Build.Step.Compile, list: []const u8) void { } else if (exe.target.isWindows() and mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib)) { exe.linkSystemLibrary(lib[0 .. lib.len - ".lib".len]); } else { - exe.addObjectFile(lib); + exe.addObjectFile(.{ .path = lib }); } } } diff --git a/lib/std/Build.zig b/lib/std/Build.zig index aaf152f5ac65..e68a860b8218 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -471,7 +471,7 @@ pub fn addOptions(self: *Build) *Step.Options { pub const ExecutableOptions = struct { name: []const u8, - root_source_file: ?FileSource = null, + root_source_file: ?LazyPath = null, version: ?std.SemanticVersion = null, target: CrossTarget = .{}, optimize: std.builtin.Mode = .Debug, @@ -502,7 +502,7 @@ pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile { pub const ObjectOptions = struct { name: []const u8, - root_source_file: ?FileSource = null, + root_source_file: ?LazyPath = null, target: CrossTarget, optimize: std.builtin.Mode, max_rss: usize = 0, @@ -529,7 +529,7 @@ pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile { pub const SharedLibraryOptions = struct { name: []const u8, - root_source_file: ?FileSource = null, + root_source_file: ?LazyPath = null, version: ?std.SemanticVersion = null, target: CrossTarget, optimize: std.builtin.Mode, @@ -559,7 +559,7 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile pub const StaticLibraryOptions = struct { name: []const u8, - root_source_file: ?FileSource = null, + root_source_file: ?LazyPath = null, target: CrossTarget, optimize: std.builtin.Mode, version: ?std.SemanticVersion = null, @@ -589,7 +589,7 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile pub const TestOptions = struct { name: []const u8 = "test", - root_source_file: FileSource, + root_source_file: LazyPath, target: CrossTarget = .{}, optimize: std.builtin.Mode = .Debug, version: ?std.SemanticVersion = null, @@ -621,7 +621,7 @@ pub fn addTest(b: *Build, options: TestOptions) *Step.Compile { pub const AssemblyOptions = struct { name: []const u8, - source_file: FileSource, + source_file: LazyPath, target: CrossTarget, optimize: std.builtin.Mode, max_rss: usize = 0, @@ -636,7 +636,7 @@ pub fn addAssembly(b: *Build, options: AssemblyOptions) *Step.Compile { .optimize = options.optimize, .max_rss = options.max_rss, }); - obj_step.addAssemblyFileSource(options.source_file.dupe(b)); + obj_step.addAssemblyLazyPath(options.source_file.dupe(b)); return obj_step; } @@ -655,7 +655,7 @@ pub const ModuleDependency = struct { }; pub const CreateModuleOptions = struct { - source_file: FileSource, + source_file: LazyPath, dependencies: []const ModuleDependency = &.{}, }; @@ -1284,22 +1284,22 @@ pub fn installLibFile(self: *Build, src_path: []const u8, dest_rel_path: []const self.getInstallStep().dependOn(&self.addInstallFileWithDir(.{ .path = src_path }, .lib, dest_rel_path).step); } -pub fn addObjCopy(b: *Build, source: FileSource, options: Step.ObjCopy.Options) *Step.ObjCopy { +pub fn addObjCopy(b: *Build, source: LazyPath, options: Step.ObjCopy.Options) *Step.ObjCopy { return Step.ObjCopy.create(b, source, options); } ///`dest_rel_path` is relative to install prefix path -pub fn addInstallFile(self: *Build, source: FileSource, dest_rel_path: []const u8) *Step.InstallFile { +pub fn addInstallFile(self: *Build, source: LazyPath, dest_rel_path: []const u8) *Step.InstallFile { return self.addInstallFileWithDir(source.dupe(self), .prefix, dest_rel_path); } ///`dest_rel_path` is relative to bin path -pub fn addInstallBinFile(self: *Build, source: FileSource, dest_rel_path: []const u8) *Step.InstallFile { +pub fn addInstallBinFile(self: *Build, source: LazyPath, dest_rel_path: []const u8) *Step.InstallFile { return self.addInstallFileWithDir(source.dupe(self), .bin, dest_rel_path); } ///`dest_rel_path` is relative to lib path -pub fn addInstallLibFile(self: *Build, source: FileSource, dest_rel_path: []const u8) *Step.InstallFile { +pub fn addInstallLibFile(self: *Build, source: LazyPath, dest_rel_path: []const u8) *Step.InstallFile { return self.addInstallFileWithDir(source.dupe(self), .lib, dest_rel_path); } @@ -1309,7 +1309,7 @@ pub fn addInstallHeaderFile(b: *Build, src_path: []const u8, dest_rel_path: []co pub fn addInstallFileWithDir( self: *Build, - source: FileSource, + source: LazyPath, install_dir: InstallDir, dest_rel_path: []const u8, ) *Step.InstallFile { @@ -1322,7 +1322,7 @@ pub fn addInstallDirectory(self: *Build, options: InstallDirectoryOptions) *Step pub fn addCheckFile( b: *Build, - file_source: FileSource, + file_source: LazyPath, options: Step.CheckFile.Options, ) *Step.CheckFile { return Step.CheckFile.create(b, file_source, options); @@ -1608,7 +1608,7 @@ pub const Module = struct { /// This could either be a generated file, in which case the module /// contains exactly one file, or it could be a path to the root source /// file of directory of files which constitute the module. - source_file: FileSource, + source_file: LazyPath, dependencies: std.StringArrayHashMap(*Module), }; @@ -1630,8 +1630,10 @@ pub const GeneratedFile = struct { } }; -/// A file source is a reference to an existing or future file. -pub const FileSource = union(enum) { +pub const FileSource = LazyPath; // DEPRECATED, use LazyPath now + +/// A reference to an existing or future path. +pub const LazyPath = union(enum) { /// A plain file path, relative to build root or absolute. path: []const u8, @@ -1641,14 +1643,14 @@ pub const FileSource = union(enum) { /// Returns a new file source that will have a relative path to the build root guaranteed. /// This should be preferred over setting `.path` directly as it documents that the files are in the project directory. - pub fn relative(path: []const u8) FileSource { + pub fn relative(path: []const u8) LazyPath { std.debug.assert(!std.fs.path.isAbsolute(path)); - return FileSource{ .path = path }; + return LazyPath{ .path = path }; } /// Returns a string that can be shown to represent the file source. /// Either returns the path or `"generated"`. - pub fn getDisplayName(self: FileSource) []const u8 { + pub fn getDisplayName(self: LazyPath) []const u8 { return switch (self) { .path => self.path, .generated => "generated", @@ -1656,7 +1658,7 @@ pub const FileSource = union(enum) { } /// Adds dependencies this file source implies to the given step. - pub fn addStepDependencies(self: FileSource, other_step: *Step) void { + pub fn addStepDependencies(self: LazyPath, other_step: *Step) void { switch (self) { .path => {}, .generated => |gen| other_step.dependOn(gen.step), @@ -1664,14 +1666,14 @@ pub const FileSource = union(enum) { } /// Should only be called during make(), returns a path relative to the build root or absolute. - pub fn getPath(self: FileSource, src_builder: *Build) []const u8 { + pub fn getPath(self: LazyPath, src_builder: *Build) []const u8 { return getPath2(self, src_builder, null); } /// Should only be called during make(), returns a path relative to the build root or absolute. /// asking_step is only used for debugging purposes; it's the step being run that is asking for /// the path. - pub fn getPath2(self: FileSource, src_builder: *Build, asking_step: ?*Step) []const u8 { + pub fn getPath2(self: LazyPath, src_builder: *Build, asking_step: ?*Step) []const u8 { switch (self) { .path => |p| return src_builder.pathFromRoot(p), .generated => |gen| return gen.path orelse { @@ -1684,7 +1686,7 @@ pub const FileSource = union(enum) { } /// Duplicates the file source for a given builder. - pub fn dupe(self: FileSource, b: *Build) FileSource { + pub fn dupe(self: LazyPath, b: *Build) LazyPath { return switch (self) { .path => |p| .{ .path = b.dupePath(p) }, .generated => |gen| .{ .generated = gen }, diff --git a/lib/std/Build/Step/CheckFile.zig b/lib/std/Build/Step/CheckFile.zig index dc359b5654d2..d072865922fb 100644 --- a/lib/std/Build/Step/CheckFile.zig +++ b/lib/std/Build/Step/CheckFile.zig @@ -11,7 +11,7 @@ const mem = std.mem; step: Step, expected_matches: []const []const u8, expected_exact: ?[]const u8, -source: std.Build.FileSource, +source: std.Build.LazyPath, max_bytes: usize = 20 * 1024 * 1024, pub const base_id = .check_file; @@ -23,7 +23,7 @@ pub const Options = struct { pub fn create( owner: *std.Build, - source: std.Build.FileSource, + source: std.Build.LazyPath, options: Options, ) *CheckFile { const self = owner.allocator.create(CheckFile) catch @panic("OOM"); diff --git a/lib/std/Build/Step/CheckObject.zig b/lib/std/Build/Step/CheckObject.zig index e79468ec9bc7..f69427ab05ec 100644 --- a/lib/std/Build/Step/CheckObject.zig +++ b/lib/std/Build/Step/CheckObject.zig @@ -15,14 +15,14 @@ const Step = std.Build.Step; pub const base_id = .check_object; step: Step, -source: std.Build.FileSource, +source: std.Build.LazyPath, max_bytes: usize = 20 * 1024 * 1024, checks: std.ArrayList(Check), obj_format: std.Target.ObjectFormat, pub fn create( owner: *std.Build, - source: std.Build.FileSource, + source: std.Build.LazyPath, obj_format: std.Target.ObjectFormat, ) *CheckObject { const gpa = owner.allocator; @@ -44,7 +44,7 @@ pub fn create( const SearchPhrase = struct { string: []const u8, - file_source: ?std.Build.FileSource = null, + file_source: ?std.Build.LazyPath = null, fn resolve(phrase: SearchPhrase, b: *std.Build, step: *Step) []const u8 { const file_source = phrase.file_source orelse return phrase.string; @@ -302,13 +302,13 @@ pub fn checkExact(self: *CheckObject, phrase: []const u8) void { self.checkExactInner(phrase, null); } -/// Like `checkExact()` but takes an additional argument `FileSource` which will be +/// Like `checkExact()` but takes an additional argument `LazyPath` which will be /// resolved to a full search query in `make()`. -pub fn checkExactFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void { +pub fn checkExactPath(self: *CheckObject, phrase: []const u8, file_source: std.Build.LazyPath) void { self.checkExactInner(phrase, file_source); } -fn checkExactInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void { +fn checkExactInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.LazyPath) void { assert(self.checks.items.len > 0); const last = &self.checks.items[self.checks.items.len - 1]; last.exact(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source }); @@ -321,7 +321,7 @@ pub fn checkContains(self: *CheckObject, phrase: []const u8) void { /// Like `checkContains()` but takes an additional argument `FileSource` which will be /// resolved to a full search query in `make()`. -pub fn checkContainsFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void { +pub fn checkContainsPath(self: *CheckObject, phrase: []const u8, file_source: std.Build.LazyPath) void { self.checkContainsInner(phrase, file_source); } diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index d3c8d54442c2..e3d4aa4155e4 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -11,7 +11,7 @@ const Allocator = mem.Allocator; const Step = std.Build.Step; const CrossTarget = std.zig.CrossTarget; const NativeTargetInfo = std.zig.system.NativeTargetInfo; -const FileSource = std.Build.FileSource; +const LazyPath = std.Build.LazyPath; const PkgConfigPkg = std.Build.PkgConfigPkg; const PkgConfigError = std.Build.PkgConfigError; const ExecError = std.Build.ExecError; @@ -28,7 +28,7 @@ name: []const u8, target: CrossTarget, target_info: NativeTargetInfo, optimize: std.builtin.Mode, -linker_script: ?FileSource = null, +linker_script: ?LazyPath = null, version_script: ?[]const u8 = null, out_filename: []const u8, linkage: ?Linkage = null, @@ -40,9 +40,9 @@ strip: ?bool, unwind_tables: ?bool, // keep in sync with src/link.zig:CompressDebugSections compress_debug_sections: enum { none, zlib } = .none, -lib_paths: ArrayList(FileSource), -rpaths: ArrayList(FileSource), -framework_dirs: ArrayList(FileSource), +lib_paths: ArrayList(LazyPath), +rpaths: ArrayList(LazyPath), +framework_dirs: ArrayList(LazyPath), frameworks: StringHashMap(FrameworkLinkInfo), verbose_link: bool, verbose_cc: bool, @@ -74,8 +74,8 @@ max_memory: ?u64 = null, shared_memory: bool = false, global_base: ?u64 = null, c_std: std.Build.CStd, -zig_lib_dir: ?[]const u8, -main_pkg_path: ?[]const u8, +zig_lib_dir: ?LazyPath, +main_pkg_path: ?LazyPath, exec_cmd_args: ?[]const ?[]const u8, filter: ?[]const u8, test_evented_io: bool = false, @@ -85,7 +85,7 @@ wasi_exec_model: ?std.builtin.WasiExecModel = null, /// Symbols to be exported when compiling to wasm export_symbol_names: []const []const u8 = &.{}, -root_src: ?FileSource, +root_src: ?LazyPath, out_h_filename: []const u8, out_lib_filename: []const u8, out_pdb_filename: []const u8, @@ -106,7 +106,7 @@ installed_path: ?[]const u8, /// Base address for an executable image. image_base: ?u64 = null, -libc_file: ?FileSource = null, +libc_file: ?LazyPath = null, valgrind_support: ?bool = null, each_lib_rpath: ?bool = null, @@ -223,22 +223,22 @@ pub const CSourceFiles = struct { }; pub const CSourceFile = struct { - source: FileSource, - args: []const []const u8, + file: LazyPath, + flags: []const []const u8, pub fn dupe(self: CSourceFile, b: *std.Build) CSourceFile { return .{ - .source = self.source.dupe(b), - .args = b.dupeStrings(self.args), + .file = self.file.dupe(b), + .flags = b.dupeStrings(self.flags), }; } }; pub const LinkObject = union(enum) { - static_path: FileSource, + static_path: LazyPath, other_step: *Compile, system_lib: SystemLib, - assembly_file: FileSource, + assembly_file: LazyPath, c_source_file: *CSourceFile, c_source_files: *CSourceFiles, }; @@ -265,15 +265,15 @@ const FrameworkLinkInfo = struct { }; pub const IncludeDir = union(enum) { - raw_path: []const u8, - raw_path_system: []const u8, + path: LazyPath, + path_system: LazyPath, other_step: *Compile, config_header_step: *Step.ConfigHeader, }; pub const Options = struct { name: []const u8, - root_source_file: ?FileSource = null, + root_source_file: ?LazyPath = null, target: CrossTarget, optimize: std.builtin.Mode, kind: Kind, @@ -391,7 +391,7 @@ pub const EmitOption = union(enum) { pub fn create(owner: *std.Build, options: Options) *Compile { const name = owner.dupe(options.name); - const root_src: ?FileSource = if (options.root_source_file) |rsrc| rsrc.dupe(owner) else null; + const root_src: ?LazyPath = if (options.root_source_file) |rsrc| rsrc.dupe(owner) else null; if (mem.indexOf(u8, name, "/") != null or mem.indexOf(u8, name, "\\") != null) { panic("invalid name: '{s}'. It looks like a file path, but it is supposed to be the library or application name.", .{name}); } @@ -462,9 +462,9 @@ pub fn create(owner: *std.Build, options: Options) *Compile { .include_dirs = ArrayList(IncludeDir).init(owner.allocator), .link_objects = ArrayList(LinkObject).init(owner.allocator), .c_macros = ArrayList([]const u8).init(owner.allocator), - .lib_paths = ArrayList(FileSource).init(owner.allocator), - .rpaths = ArrayList(FileSource).init(owner.allocator), - .framework_dirs = ArrayList(FileSource).init(owner.allocator), + .lib_paths = ArrayList(LazyPath).init(owner.allocator), + .rpaths = ArrayList(LazyPath).init(owner.allocator), + .framework_dirs = ArrayList(LazyPath).init(owner.allocator), .installed_headers = ArrayList(*Step).init(owner.allocator), .c_std = std.Build.CStd.C99, .zig_lib_dir = null, @@ -614,7 +614,7 @@ pub fn addObjCopy(cs: *Compile, options: Step.ObjCopy.Options) *Step.ObjCopy { copy.basename = cs.name; } } - return b.addObjCopy(cs.getOutputSource(), copy); + return b.addObjCopy(cs.getEmittedBin(), copy); } /// This function would run in the context of the package that created the executable, @@ -626,10 +626,12 @@ pub const run = @compileError("deprecated; use std.Build.addRunArtifact"); pub const install = @compileError("deprecated; use std.Build.installArtifact"); pub fn checkObject(self: *Compile) *Step.CheckObject { - return Step.CheckObject.create(self.step.owner, self.getOutputSource(), self.target_info.target.ofmt); + return Step.CheckObject.create(self.step.owner, self.getEmittedBin(), self.target_info.target.ofmt); } -pub fn setLinkerScriptPath(self: *Compile, source: FileSource) void { +pub const setLinkerScriptPath = setLinkerScript; // DEPRECATED, use setLinkerScript + +pub fn setLinkerScript(self: *Compile, source: LazyPath) void { const b = self.step.owner; self.linker_script = source.dupe(b); source.addStepDependencies(&self.step); @@ -935,19 +937,12 @@ pub fn addCSourceFiles(self: *Compile, files: []const []const u8, flags: []const self.link_objects.append(.{ .c_source_files = c_source_files }) catch @panic("OOM"); } -pub fn addCSourceFile(self: *Compile, file: []const u8, flags: []const []const u8) void { - self.addCSourceFileSource(.{ - .args = flags, - .source = .{ .path = file }, - }); -} - -pub fn addCSourceFileSource(self: *Compile, source: CSourceFile) void { +pub fn addCSourceFile(self: *Compile, source: CSourceFile) void { const b = self.step.owner; const c_source_file = b.allocator.create(CSourceFile) catch @panic("OOM"); c_source_file.* = source.dupe(b); self.link_objects.append(.{ .c_source_file = c_source_file }) catch @panic("OOM"); - source.source.addStepDependencies(&self.step); + source.file.addStepDependencies(&self.step); } pub fn setVerboseLink(self: *Compile, value: bool) void { @@ -958,53 +953,63 @@ pub fn setVerboseCC(self: *Compile, value: bool) void { self.verbose_cc = value; } -pub fn overrideZigLibDir(self: *Compile, dir_path: []const u8) void { - const b = self.step.owner; - self.zig_lib_dir = b.dupePath(dir_path); +pub fn overrideZigLibDir(self: *Compile, dir_path: LazyPath) void { + self.zig_lib_dir = dir_path.dupe(self.step.owner); + dir_path.addStepDependencies(&self.step); } -pub fn setMainPkgPath(self: *Compile, dir_path: []const u8) void { - const b = self.step.owner; - self.main_pkg_path = b.dupePath(dir_path); +pub fn setMainPkgPath(self: *Compile, dir_path: LazyPath) void { + self.main_pkg_path = dir_path.dupe(self.step.owner); + dir_path.addStepDependencies(&self.step); } -pub fn setLibCFile(self: *Compile, libc_file: ?FileSource) void { +pub fn setLibCFile(self: *Compile, libc_file: ?LazyPath) void { const b = self.step.owner; self.libc_file = if (libc_file) |f| f.dupe(b) else null; } +pub const getOutputSource = getEmittedBin; // DEPRECATED, use getEmittedBin + /// Returns the generated executable, library or object file. /// To run an executable built with zig build, use `run`, or create an install step and invoke it. -pub fn getOutputSource(self: *Compile) FileSource { +pub fn getEmittedBin(self: *Compile) LazyPath { return .{ .generated = &self.output_path_source }; } -pub fn getOutputDirectorySource(self: *Compile) FileSource { +pub const getOutputDirectorySource = getEmitDirectory; // DEPRECATED, use getEmitDirectory + +pub fn getEmitDirectory(self: *Compile) LazyPath { return .{ .generated = &self.output_dirname_source }; } +pub const getOutputLibSource = getEmittedImplib; // DEPRECATED, use getEmittedImplib + /// Returns the generated import library. This function can only be called for libraries. -pub fn getOutputLibSource(self: *Compile) FileSource { +pub fn getEmittedImplib(self: *Compile) LazyPath { assert(self.kind == .lib); return .{ .generated = &self.output_lib_path_source }; } +pub const getOutputHSource = getEmittedH; // DEPRECATED, use getEmittedH + /// Returns the generated header file. /// This function can only be called for libraries or object files which have `emit_h` set. -pub fn getOutputHSource(self: *Compile) FileSource { +pub fn getEmittedH(self: *Compile) LazyPath { assert(self.kind != .exe and self.kind != .@"test"); assert(self.emit_h); return .{ .generated = &self.output_h_path_source }; } +pub const getOutputPdbSource = getEmittedPdb; // DEPRECATED, use getEmittedPdb + /// Returns the generated PDB file. This function can only be called for Windows and UEFI. -pub fn getOutputPdbSource(self: *Compile) FileSource { +pub fn getEmittedPdb(self: *Compile) LazyPath { // TODO: Is this right? Isn't PDB for *any* PE/COFF file? assert(self.target.isWindows() or self.target.isUefi()); return .{ .generated = &self.output_pdb_path_source }; } -pub fn getEmittedDocs(self: *Compile) FileSource { +pub fn getEmittedDocs(self: *Compile) LazyPath { if (self.generated_docs) |g| return .{ .generated = g }; const arena = self.step.owner.allocator; const generated_file = arena.create(GeneratedFile) catch @panic("OOM"); @@ -1013,25 +1018,14 @@ pub fn getEmittedDocs(self: *Compile) FileSource { return .{ .generated = generated_file }; } -pub fn addAssemblyFile(self: *Compile, path: []const u8) void { - const b = self.step.owner; - self.link_objects.append(.{ - .assembly_file = .{ .path = b.dupe(path) }, - }) catch @panic("OOM"); -} - -pub fn addAssemblyFileSource(self: *Compile, source: FileSource) void { +pub fn addAssemblyFile(self: *Compile, source: LazyPath) void { const b = self.step.owner; const source_duped = source.dupe(b); self.link_objects.append(.{ .assembly_file = source_duped }) catch @panic("OOM"); source_duped.addStepDependencies(&self.step); } -pub fn addObjectFile(self: *Compile, source_file: []const u8) void { - self.addObjectFileSource(.{ .path = source_file }); -} - -pub fn addObjectFileSource(self: *Compile, source: FileSource) void { +pub fn addObjectFile(self: *Compile, source: LazyPath) void { const b = self.step.owner; self.link_objects.append(.{ .static_path = source.dupe(b) }) catch @panic("OOM"); source.addStepDependencies(&self.step); @@ -1042,14 +1036,16 @@ pub fn addObject(self: *Compile, obj: *Compile) void { self.linkLibraryOrObject(obj); } -pub fn addSystemIncludePath(self: *Compile, path: []const u8) void { +pub fn addSystemIncludePath(self: *Compile, path: LazyPath) void { const b = self.step.owner; - self.include_dirs.append(IncludeDir{ .raw_path_system = b.dupe(path) }) catch @panic("OOM"); + self.include_dirs.append(IncludeDir{ .path_system = path.dupe(b) }) catch @panic("OOM"); + path.addStepDependencies(&self.step); } -pub fn addIncludePath(self: *Compile, path: []const u8) void { +pub fn addIncludePath(self: *Compile, path: LazyPath) void { const b = self.step.owner; - self.include_dirs.append(IncludeDir{ .raw_path = b.dupe(path) }) catch @panic("OOM"); + self.include_dirs.append(IncludeDir{ .path = path.dupe(b) }) catch @panic("OOM"); + path.addStepDependencies(&self.step); } pub fn addConfigHeader(self: *Compile, config_header: *Step.ConfigHeader) void { @@ -1057,32 +1053,17 @@ pub fn addConfigHeader(self: *Compile, config_header: *Step.ConfigHeader) void { self.include_dirs.append(.{ .config_header_step = config_header }) catch @panic("OOM"); } -pub fn addLibraryPath(self: *Compile, path: []const u8) void { - const b = self.step.owner; - self.lib_paths.append(.{ .path = b.dupe(path) }) catch @panic("OOM"); -} - -pub fn addLibraryPathDirectorySource(self: *Compile, directory_source: FileSource) void { +pub fn addLibraryPath(self: *Compile, directory_source: LazyPath) void { self.lib_paths.append(directory_source) catch @panic("OOM"); directory_source.addStepDependencies(&self.step); } -pub fn addRPath(self: *Compile, path: []const u8) void { - const b = self.step.owner; - self.rpaths.append(.{ .path = b.dupe(path) }) catch @panic("OOM"); -} - -pub fn addRPathDirectorySource(self: *Compile, directory_source: FileSource) void { +pub fn addRPath(self: *Compile, directory_source: LazyPath) void { self.rpaths.append(directory_source) catch @panic("OOM"); directory_source.addStepDependencies(&self.step); } -pub fn addFrameworkPath(self: *Compile, dir_path: []const u8) void { - const b = self.step.owner; - self.framework_dirs.append(.{ .path = b.dupe(dir_path) }) catch @panic("OOM"); -} - -pub fn addFrameworkPathDirectorySource(self: *Compile, directory_source: FileSource) void { +pub fn addFrameworkPath(self: *Compile, directory_source: LazyPath) void { self.framework_dirs.append(directory_source) catch @panic("OOM"); directory_source.addStepDependencies(&self.step); } @@ -1364,7 +1345,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { .exe => @panic("Cannot link with an executable build artifact"), .@"test" => @panic("Cannot link with a test"), .obj => { - try zig_args.append(other.getOutputSource().getPath(b)); + try zig_args.append(other.getEmittedBin().getPath(b)); }, .lib => l: { if (self.isStaticLibrary() and other.isStaticLibrary()) { @@ -1372,7 +1353,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { break :l; } - const full_path_lib = other.getOutputLibSource().getPath(b); + const full_path_lib = other.getEmittedImplib().getPath(b); try zig_args.append(full_path_lib); if (other.linkage == Linkage.dynamic and !self.target.isWindows()) { @@ -1432,7 +1413,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { }, .c_source_file => |c_source_file| { - if (c_source_file.args.len == 0) { + if (c_source_file.flags.len == 0) { if (prev_has_cflags) { try zig_args.append("-cflags"); try zig_args.append("--"); @@ -1440,13 +1421,13 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } } else { try zig_args.append("-cflags"); - for (c_source_file.args) |arg| { + for (c_source_file.flags) |arg| { try zig_args.append(arg); } try zig_args.append("--"); prev_has_cflags = true; } - try zig_args.append(c_source_file.source.getPath(b)); + try zig_args.append(c_source_file.file.getPath(b)); }, .c_source_files => |c_source_files| { @@ -1746,18 +1727,18 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { for (self.include_dirs.items) |include_dir| { switch (include_dir) { - .raw_path => |include_path| { + .path => |include_path| { try zig_args.append("-I"); - try zig_args.append(b.pathFromRoot(include_path)); + try zig_args.append(include_path.getPath(b)); }, - .raw_path_system => |include_path| { + .path_system => |include_path| { if (b.sysroot != null) { try zig_args.append("-iwithsysroot"); } else { try zig_args.append("-isystem"); } - const resolved_include_path = b.pathFromRoot(include_path); + const resolved_include_path = include_path.getPath(b); const common_include_path = if (builtin.os.tag == .windows and b.sysroot != null and fs.path.isAbsolute(resolved_include_path)) blk: { // We need to check for disk designator and strip it out from dir path so @@ -1775,7 +1756,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { }, .other_step => |other| { if (other.emit_h) { - const h_path = other.getOutputHSource().getPath(b); + const h_path = other.getEmittedH().getPath(b); try zig_args.append("-isystem"); try zig_args.append(fs.path.dirname(h_path).?); } @@ -1907,7 +1888,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { if (self.zig_lib_dir) |dir| { try zig_args.append("--zig-lib-dir"); - try zig_args.append(b.pathFromRoot(dir)); + try zig_args.append(dir.getPath(b)); } else if (b.zig_lib_dir) |dir| { try zig_args.append("--zig-lib-dir"); try zig_args.append(dir); @@ -1915,7 +1896,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { if (self.main_pkg_path) |dir| { try zig_args.append("--main-pkg-path"); - try zig_args.append(b.pathFromRoot(dir)); + try zig_args.append(dir.getPath(b)); } try addFlag(&zig_args, "PIC", self.force_pic); @@ -2042,7 +2023,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { { try doAtomicSymLinks( step, - self.getOutputSource().getPath(b), + self.getEmittedBin().getPath(b), self.major_only_filename.?, self.name_only_filename.?, ); diff --git a/lib/std/Build/Step/ConfigHeader.zig b/lib/std/Build/Step/ConfigHeader.zig index a697d7eda8fe..dbf63a9df448 100644 --- a/lib/std/Build/Step/ConfigHeader.zig +++ b/lib/std/Build/Step/ConfigHeader.zig @@ -6,16 +6,18 @@ const Allocator = std.mem.Allocator; pub const Style = union(enum) { /// The configure format supported by autotools. It uses `#undef foo` to /// mark lines that can be substituted with different values. - autoconf: std.Build.FileSource, + autoconf: std.Build.LazyPath, /// The configure format supported by CMake. It uses `@@FOO@@` and /// `#cmakedefine` for template substitution. - cmake: std.Build.FileSource, + cmake: std.Build.LazyPath, /// Instead of starting with an input file, start with nothing. blank, /// Start with nothing, like blank, and output a nasm .asm file. nasm, - pub fn getFileSource(style: Style) ?std.Build.FileSource { + pub const getFileSource = getPath; // DEPRECATED, use getPath + + pub fn getPath(style: Style) ?std.Build.LazyPath { switch (style) { .autoconf, .cmake => |s| return s, .blank, .nasm => return null, @@ -54,7 +56,7 @@ pub fn create(owner: *std.Build, options: Options) *ConfigHeader { var include_path: []const u8 = "config.h"; - if (options.style.getFileSource()) |s| switch (s) { + if (options.style.getPath()) |s| switch (s) { .path => |p| { const basename = std.fs.path.basename(p); if (std.mem.endsWith(u8, basename, ".h.in")) { @@ -68,7 +70,7 @@ pub fn create(owner: *std.Build, options: Options) *ConfigHeader { include_path = p; } - const name = if (options.style.getFileSource()) |s| + const name = if (options.style.getPath()) |s| owner.fmt("configure {s} header {s} to {s}", .{ @tagName(options.style), s.getDisplayName(), include_path, }) @@ -98,7 +100,9 @@ pub fn addValues(self: *ConfigHeader, values: anytype) void { return addValuesInner(self, values) catch @panic("OOM"); } -pub fn getFileSource(self: *ConfigHeader) std.Build.FileSource { +pub const getFileSource = getTemplate; // DEPRECATED, use getOutput + +pub fn getTemplate(self: *ConfigHeader) std.Build.LazyPath { return .{ .generated = &self.output_file }; } diff --git a/lib/std/Build/Step/InstallArtifact.zig b/lib/std/Build/Step/InstallArtifact.zig index 9552a4444044..ab01e03f98e5 100644 --- a/lib/std/Build/Step/InstallArtifact.zig +++ b/lib/std/Build/Step/InstallArtifact.zig @@ -76,7 +76,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { var all_cached = true; { - const full_src_path = self.artifact.getOutputSource().getPath(src_builder); + const full_src_path = self.artifact.getEmittedBin().getPath(src_builder); const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_dest_path, .{}) catch |err| { return step.fail("unable to update file from '{s}' to '{s}': {s}", .{ full_src_path, full_dest_path, @errorName(err), @@ -95,7 +95,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { self.artifact.target.isWindows() and self.artifact.emit_implib != .no_emit) { - const full_src_path = self.artifact.getOutputLibSource().getPath(src_builder); + const full_src_path = self.artifact.getEmittedImplib().getPath(src_builder); const full_implib_path = dest_builder.getInstallPath(self.dest_dir, self.artifact.out_lib_filename); const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_implib_path, .{}) catch |err| { return step.fail("unable to update file from '{s}' to '{s}': {s}", .{ @@ -105,7 +105,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { all_cached = all_cached and p == .fresh; } if (self.pdb_dir) |pdb_dir| { - const full_src_path = self.artifact.getOutputPdbSource().getPath(src_builder); + const full_src_path = self.artifact.getEmittedPdb().getPath(src_builder); const full_pdb_path = dest_builder.getInstallPath(pdb_dir, self.artifact.out_pdb_filename); const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_pdb_path, .{}) catch |err| { return step.fail("unable to update file from '{s}' to '{s}': {s}", .{ @@ -115,7 +115,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { all_cached = all_cached and p == .fresh; } if (self.h_dir) |h_dir| { - const full_src_path = self.artifact.getOutputHSource().getPath(src_builder); + const full_src_path = self.artifact.getEmittedH().getPath(src_builder); const full_h_path = dest_builder.getInstallPath(h_dir, self.artifact.out_h_filename); const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_h_path, .{}) catch |err| { return step.fail("unable to update file from '{s}' to '{s}': {s}", .{ diff --git a/lib/std/Build/Step/InstallDir.zig b/lib/std/Build/Step/InstallDir.zig index 5c9af3157d5f..a38786a42429 100644 --- a/lib/std/Build/Step/InstallDir.zig +++ b/lib/std/Build/Step/InstallDir.zig @@ -2,7 +2,7 @@ const std = @import("std"); const mem = std.mem; const fs = std.fs; const Step = std.Build.Step; -const FileSource = std.Build.FileSource; +const LazyPath = std.Build.LazyPath; const InstallDir = std.Build.InstallDir; const InstallDirStep = @This(); @@ -15,7 +15,7 @@ dest_builder: *std.Build, pub const base_id = .install_dir; pub const Options = struct { - source_dir: FileSource, + source_dir: LazyPath, install_dir: InstallDir, install_subdir: []const u8, /// File paths which end in any of these suffixes will be excluded diff --git a/lib/std/Build/Step/InstallFile.zig b/lib/std/Build/Step/InstallFile.zig index 784685dc3a45..f7487de5985f 100644 --- a/lib/std/Build/Step/InstallFile.zig +++ b/lib/std/Build/Step/InstallFile.zig @@ -1,6 +1,6 @@ const std = @import("std"); const Step = std.Build.Step; -const FileSource = std.Build.FileSource; +const LazyPath = std.Build.LazyPath; const InstallDir = std.Build.InstallDir; const InstallFile = @This(); const assert = std.debug.assert; @@ -8,7 +8,7 @@ const assert = std.debug.assert; pub const base_id = .install_file; step: Step, -source: FileSource, +source: LazyPath, dir: InstallDir, dest_rel_path: []const u8, /// This is used by the build system when a file being installed comes from one @@ -17,7 +17,7 @@ dest_builder: *std.Build, pub fn create( owner: *std.Build, - source: FileSource, + source: LazyPath, dir: InstallDir, dest_rel_path: []const u8, ) *InstallFile { diff --git a/lib/std/Build/Step/ObjCopy.zig b/lib/std/Build/Step/ObjCopy.zig index a8a0dafaef1d..77f1c2a0de9e 100644 --- a/lib/std/Build/Step/ObjCopy.zig +++ b/lib/std/Build/Step/ObjCopy.zig @@ -20,7 +20,7 @@ pub const RawFormat = enum { }; step: Step, -file_source: std.Build.FileSource, +input_file: std.Build.LazyPath, basename: []const u8, output_file: std.Build.GeneratedFile, @@ -37,30 +37,32 @@ pub const Options = struct { pub fn create( owner: *std.Build, - file_source: std.Build.FileSource, + input_file: std.Build.LazyPath, options: Options, ) *ObjCopy { const self = owner.allocator.create(ObjCopy) catch @panic("OOM"); self.* = ObjCopy{ .step = Step.init(.{ .id = base_id, - .name = owner.fmt("objcopy {s}", .{file_source.getDisplayName()}), + .name = owner.fmt("objcopy {s}", .{input_file.getDisplayName()}), .owner = owner, .makeFn = make, }), - .file_source = file_source, - .basename = options.basename orelse file_source.getDisplayName(), + .input_file = input_file, + .basename = options.basename orelse input_file.getDisplayName(), .output_file = std.Build.GeneratedFile{ .step = &self.step }, .format = options.format, .only_section = options.only_section, .pad_to = options.pad_to, }; - file_source.addStepDependencies(&self.step); + input_file.addStepDependencies(&self.step); return self; } -pub fn getOutputSource(self: *const ObjCopy) std.Build.FileSource { +pub const getOutputSource = getOutput; // DEPRECATED, use getOutput + +pub fn getOutput(self: *const ObjCopy) std.Build.LazyPath { return .{ .generated = &self.output_file }; } @@ -75,7 +77,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { // bytes when ObjCopy implementation is modified incompatibly. man.hash.add(@as(u32, 0xe18b7baf)); - const full_src_path = self.file_source.getPath(b); + const full_src_path = self.input_file.getPath(b); _ = try man.addFile(full_src_path, null); man.hash.addOptionalBytes(self.only_section); man.hash.addOptional(self.pad_to); diff --git a/lib/std/Build/Step/Options.zig b/lib/std/Build/Step/Options.zig index 52821a595108..0f058cab05a0 100644 --- a/lib/std/Build/Step/Options.zig +++ b/lib/std/Build/Step/Options.zig @@ -3,7 +3,7 @@ const builtin = @import("builtin"); const fs = std.fs; const Step = std.Build.Step; const GeneratedFile = std.Build.GeneratedFile; -const FileSource = std.Build.FileSource; +const LazyPath = std.Build.LazyPath; const Options = @This(); @@ -13,8 +13,7 @@ step: Step, generated_file: GeneratedFile, contents: std.ArrayList(u8), -artifact_args: std.ArrayList(OptionArtifactArg), -file_source_args: std.ArrayList(OptionFileSourceArg), +args: std.ArrayList(OptionLazyPathArg), pub fn create(owner: *std.Build) *Options { const self = owner.allocator.create(Options) catch @panic("OOM"); @@ -27,8 +26,7 @@ pub fn create(owner: *std.Build) *Options { }), .generated_file = undefined, .contents = std.ArrayList(u8).init(owner.allocator), - .artifact_args = std.ArrayList(OptionArtifactArg).init(owner.allocator), - .file_source_args = std.ArrayList(OptionFileSourceArg).init(owner.allocator), + .args = std.ArrayList(OptionLazyPathArg).init(owner.allocator), }; self.generated_file = .{ .step = &self.step }; @@ -168,35 +166,42 @@ fn printLiteral(out: anytype, val: anytype, indent: u8) !void { } } +pub const addOptionFileSource = addOptionPath; // DEPRECATED, use addPathOption + /// The value is the path in the cache dir. /// Adds a dependency automatically. -pub fn addOptionFileSource( +pub fn addOptionPath( self: *Options, name: []const u8, - source: FileSource, + path: LazyPath, ) void { - self.file_source_args.append(.{ - .name = name, - .source = source.dupe(self.step.owner), + self.args.append(.{ + .name = self.step.owner.dupe(name), + .path = path.dupe(self.step.owner), }) catch @panic("OOM"); - source.addStepDependencies(&self.step); + path.addStepDependencies(&self.step); } /// The value is the path in the cache dir. /// Adds a dependency automatically. pub fn addOptionArtifact(self: *Options, name: []const u8, artifact: *Step.Compile) void { - self.artifact_args.append(.{ .name = self.step.owner.dupe(name), .artifact = artifact }) catch @panic("OOM"); + self.args.append(.{ + .name = self.step.owner.dupe(name), + .artifact = artifact.getEmittedBin, + }) catch @panic("OOM"); self.step.dependOn(&artifact.step); } pub fn createModule(self: *Options) *std.Build.Module { return self.step.owner.createModule(.{ - .source_file = self.getSource(), + .source_file = self.getOutput(), .dependencies = &.{}, }); } -pub fn getSource(self: *Options) FileSource { +pub const getSource = getOutput; // DEPRECATED, use getOutput + +pub fn getOutput(self: *Options) LazyPath { return .{ .generated = &self.generated_file }; } @@ -207,19 +212,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { const b = step.owner; const self = @fieldParentPtr(Options, "step", step); - for (self.artifact_args.items) |item| { - self.addOption( - []const u8, - item.name, - b.pathFromRoot(item.artifact.getOutputSource().getPath(b)), - ); - } - - for (self.file_source_args.items) |item| { + for (self.args.items) |item| { self.addOption( []const u8, item.name, - item.source.getPath(b), + item.path.getPath(b), ); } @@ -294,14 +291,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } } -const OptionArtifactArg = struct { - name: []const u8, - artifact: *Step.Compile, -}; - -const OptionFileSourceArg = struct { +const OptionLazyPathArg = struct { name: []const u8, - source: FileSource, + path: LazyPath, }; test Options { diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index 076a6e0352a7..5b1bca25b4e4 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -82,7 +82,7 @@ has_side_effects: bool = false, pub const StdIn = union(enum) { none, bytes: []const u8, - file_source: std.Build.FileSource, + file_source: std.Build.LazyPath, }; pub const StdIo = union(enum) { @@ -120,15 +120,15 @@ pub const StdIo = union(enum) { pub const Arg = union(enum) { artifact: *Step.Compile, - file_source: PrefixedFileSource, - directory_source: PrefixedFileSource, + file_source: PrefixedLazyPath, + directory_source: PrefixedLazyPath, bytes: []u8, output: *Output, }; -pub const PrefixedFileSource = struct { +pub const PrefixedLazyPath = struct { prefix: []const u8, - file_source: std.Build.FileSource, + file_source: std.Build.LazyPath, }; pub const Output = struct { @@ -169,9 +169,9 @@ pub fn addArtifactArg(self: *Run, artifact: *Step.Compile) void { } /// This provides file path as a command line argument to the command being -/// run, and returns a FileSource which can be used as inputs to other APIs +/// run, and returns a LazyPath which can be used as inputs to other APIs /// throughout the build system. -pub fn addOutputFileArg(self: *Run, basename: []const u8) std.Build.FileSource { +pub fn addOutputFileArg(self: *Run, basename: []const u8) std.Build.LazyPath { return self.addPrefixedOutputFileArg("", basename); } @@ -179,7 +179,7 @@ pub fn addPrefixedOutputFileArg( self: *Run, prefix: []const u8, basename: []const u8, -) std.Build.FileSource { +) std.Build.LazyPath { const b = self.step.owner; const output = b.allocator.create(Output) catch @panic("OOM"); @@ -197,14 +197,18 @@ pub fn addPrefixedOutputFileArg( return .{ .generated = &output.generated_file }; } -pub fn addFileSourceArg(self: *Run, file_source: std.Build.FileSource) void { - self.addPrefixedFileSourceArg("", file_source); +pub const addFileSourceArg = addFileArg; // DEPRECATED, use addFileArg + +pub fn addFileArg(self: *Run, file_source: std.Build.LazyPath) void { + self.addPrefixedFileArg("", file_source); } -pub fn addPrefixedFileSourceArg(self: *Run, prefix: []const u8, file_source: std.Build.FileSource) void { +pub const addPrefixedFileSourceArg = addPrefixedFileArg; // DEPRECATED, use addPrefixedFileArg + +pub fn addPrefixedFileArg(self: *Run, prefix: []const u8, file_source: std.Build.LazyPath) void { const b = self.step.owner; - const prefixed_file_source: PrefixedFileSource = .{ + const prefixed_file_source: PrefixedLazyPath = .{ .prefix = b.dupe(prefix), .file_source = file_source.dupe(b), }; @@ -212,14 +216,18 @@ pub fn addPrefixedFileSourceArg(self: *Run, prefix: []const u8, file_source: std file_source.addStepDependencies(&self.step); } -pub fn addDirectorySourceArg(self: *Run, directory_source: std.Build.FileSource) void { - self.addPrefixedDirectorySourceArg("", directory_source); +pub const addDirectorySourceArg = addDirectoryArg; // DEPRECATED, use addDirectoryArg + +pub fn addDirectoryArg(self: *Run, directory_source: std.Build.LazyPath) void { + self.addPrefixedDirectoryArg("", directory_source); } -pub fn addPrefixedDirectorySourceArg(self: *Run, prefix: []const u8, directory_source: std.Build.FileSource) void { +pub const addPrefixedDirectorySourceArg = addPrefixedDirectoryArg; // DEPRECATED, use addPrefixedDirectoryArg + +pub fn addPrefixedDirectoryArg(self: *Run, prefix: []const u8, directory_source: std.Build.LazyPath) void { const b = self.step.owner; - const prefixed_directory_source: PrefixedFileSource = .{ + const prefixed_directory_source: PrefixedLazyPath = .{ .prefix = b.dupe(prefix), .file_source = directory_source.dupe(b), }; @@ -331,7 +339,7 @@ pub fn addCheck(self: *Run, new_check: StdIo.Check) void { } } -pub fn captureStdErr(self: *Run) std.Build.FileSource { +pub fn captureStdErr(self: *Run) std.Build.LazyPath { assert(self.stdio != .inherit); if (self.captured_stderr) |output| return .{ .generated = &output.generated_file }; @@ -346,7 +354,7 @@ pub fn captureStdErr(self: *Run) std.Build.FileSource { return .{ .generated = &output.generated_file }; } -pub fn captureStdOut(self: *Run) std.Build.FileSource { +pub fn captureStdOut(self: *Run) std.Build.LazyPath { assert(self.stdio != .inherit); if (self.captured_stdout) |output| return .{ .generated = &output.generated_file }; @@ -449,7 +457,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { self.addPathForDynLibs(artifact); } const file_path = artifact.installed_path orelse - artifact.getOutputSource().getPath(b); + artifact.getEmittedBin().getPath(b); try argv_list.append(file_path); @@ -1241,7 +1249,7 @@ fn addPathForDynLibs(self: *Run, artifact: *Step.Compile) void { switch (link_object) { .other_step => |other| { if (other.target.isWindows() and other.isDynamicLibrary()) { - addPathDir(self, fs.path.dirname(other.getOutputSource().getPath(b)).?); + addPathDir(self, fs.path.dirname(other.getEmittedBin().getPath(b)).?); addPathForDynLibs(self, other); } }, diff --git a/lib/std/Build/Step/TranslateC.zig b/lib/std/Build/Step/TranslateC.zig index cfbabc2fa958..c491b3c7ce4f 100644 --- a/lib/std/Build/Step/TranslateC.zig +++ b/lib/std/Build/Step/TranslateC.zig @@ -9,7 +9,7 @@ const TranslateC = @This(); pub const base_id = .translate_c; step: Step, -source: std.Build.FileSource, +source: std.Build.LazyPath, include_dirs: std.ArrayList([]const u8), c_macros: std.ArrayList([]const u8), out_basename: []const u8, @@ -18,7 +18,7 @@ optimize: std.builtin.OptimizeMode, output_file: std.Build.GeneratedFile, pub const Options = struct { - source_file: std.Build.FileSource, + source_file: std.Build.LazyPath, target: CrossTarget, optimize: std.builtin.OptimizeMode, }; @@ -53,10 +53,14 @@ pub const AddExecutableOptions = struct { linkage: ?Step.Compile.Linkage = null, }; +pub fn getOutput(self: *TranslateC) std.Build.LazyPath { + return .{ .generated = &self.output_file }; +} + /// Creates a step to build an executable from the translated source. pub fn addExecutable(self: *TranslateC, options: AddExecutableOptions) *Step.Compile { return self.step.owner.addExecutable(.{ - .root_source_file = .{ .generated = &self.output_file }, + .root_source_file = self.getOutput(), .name = options.name orelse "translated_c", .version = options.version, .target = options.target orelse self.target, @@ -70,7 +74,7 @@ pub fn addExecutable(self: *TranslateC, options: AddExecutableOptions) *Step.Com /// `createModule` can be used instead to create a private module. pub fn addModule(self: *TranslateC, name: []const u8) *std.Build.Module { return self.step.owner.addModule(name, .{ - .source_file = .{ .generated = &self.output_file }, + .source_file = self.getOutput(), }); } @@ -83,7 +87,7 @@ pub fn createModule(self: *TranslateC) *std.Build.Module { module.* = .{ .builder = b, - .source_file = .{ .generated = &self.output_file }, + .source_file = self.getOutput(), .dependencies = std.StringArrayHashMap(*std.Build.Module).init(b.allocator), }; return module; @@ -96,7 +100,7 @@ pub fn addIncludeDir(self: *TranslateC, include_dir: []const u8) void { pub fn addCheckFile(self: *TranslateC, expected_matches: []const []const u8) *Step.CheckFile { return Step.CheckFile.create( self.step.owner, - .{ .generated = &self.output_file }, + self.getOutput(), .{ .expected_matches = expected_matches }, ); } diff --git a/lib/std/Build/Step/WriteFile.zig b/lib/std/Build/Step/WriteFile.zig index 0448aa8d2a11..f8fc60621671 100644 --- a/lib/std/Build/Step/WriteFile.zig +++ b/lib/std/Build/Step/WriteFile.zig @@ -28,7 +28,9 @@ pub const File = struct { sub_path: []const u8, contents: Contents, - pub fn getFileSource(self: *File) std.Build.FileSource { + pub const getFileSource = getPath; // DEPRECATED, use getPath + + pub fn getPath(self: *File) std.Build.LazyPath { return .{ .generated = &self.generated_file }; } }; @@ -40,7 +42,7 @@ pub const OutputSourceFile = struct { pub const Contents = union(enum) { bytes: []const u8, - copy: std.Build.FileSource, + copy: std.Build.LazyPath, }; pub fn create(owner: *std.Build) *WriteFile { @@ -59,7 +61,7 @@ pub fn create(owner: *std.Build) *WriteFile { return wf; } -pub fn add(wf: *WriteFile, sub_path: []const u8, bytes: []const u8) std.Build.FileSource { +pub fn add(wf: *WriteFile, sub_path: []const u8, bytes: []const u8) std.Build.LazyPath { const b = wf.step.owner; const gpa = b.allocator; const file = gpa.create(File) catch @panic("OOM"); @@ -70,7 +72,7 @@ pub fn add(wf: *WriteFile, sub_path: []const u8, bytes: []const u8) std.Build.Fi }; wf.files.append(gpa, file) catch @panic("OOM"); wf.maybeUpdateName(); - return file.getFileSource(); + return file.getPath(); } /// Place the file into the generated directory within the local cache, @@ -80,7 +82,7 @@ pub fn add(wf: *WriteFile, sub_path: []const u8, bytes: []const u8) std.Build.Fi /// include sub-directories, in which case this step will ensure the /// required sub-path exists. /// This is the option expected to be used most commonly with `addCopyFile`. -pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []const u8) std.Build.FileSource { +pub fn addCopyFile(wf: *WriteFile, source: std.Build.LazyPath, sub_path: []const u8) std.Build.LazyPath { const b = wf.step.owner; const gpa = b.allocator; const file = gpa.create(File) catch @panic("OOM"); @@ -93,7 +95,7 @@ pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []con wf.maybeUpdateName(); source.addStepDependencies(&wf.step); - return file.getFileSource(); + return file.getLazyPath(); } /// A path relative to the package root. @@ -101,7 +103,7 @@ pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []con /// used as part of the normal build process, but as a utility occasionally /// run by a developer with intent to modify source files and then commit /// those changes to version control. -pub fn addCopyFileToSource(wf: *WriteFile, source: std.Build.FileSource, sub_path: []const u8) void { +pub fn addCopyFileToSource(wf: *WriteFile, source: std.Build.LazyPath, sub_path: []const u8) void { const b = wf.step.owner; wf.output_source_files.append(b.allocator, .{ .contents = .{ .copy = source }, @@ -123,11 +125,13 @@ pub fn addBytesToSource(wf: *WriteFile, bytes: []const u8, sub_path: []const u8) }) catch @panic("OOM"); } -pub const getFileSource = @compileError("Deprecated; use the return value from add()/addCopyFile(), or use files[i].getFileSource()"); +pub const getFileSource = @compileError("Deprecated; use the return value from add()/addCopyFile(), or use files[i].getPath()"); + +pub const getDirectorySource = getDirectory; -/// Returns a `FileSource` representing the base directory that contains all the +/// Returns a `LazyPath` representing the base directory that contains all the /// files from this `WriteFile`. -pub fn getDirectorySource(wf: *WriteFile) std.Build.FileSource { +pub fn getDirectory(wf: *WriteFile) std.Build.LazyPath { return .{ .generated = &wf.generated_directory }; } diff --git a/test/link/interdependent_static_c_libs/build.zig b/test/link/interdependent_static_c_libs/build.zig index 2eb8fa27b4e7..757c0d419b63 100644 --- a/test/link/interdependent_static_c_libs/build.zig +++ b/test/link/interdependent_static_c_libs/build.zig @@ -16,16 +16,16 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = .{}, }); - lib_a.addCSourceFile("a.c", &[_][]const u8{}); - lib_a.addIncludePath("."); + lib_a.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &[_][]const u8{} }); + lib_a.addIncludePath(.{ .path = "." }); const lib_b = b.addStaticLibrary(.{ .name = "b", .optimize = optimize, .target = .{}, }); - lib_b.addCSourceFile("b.c", &[_][]const u8{}); - lib_b.addIncludePath("."); + lib_b.addCSourceFile(.{ .file = .{ .path = "b.c" }, .flags = &[_][]const u8{} }); + lib_b.addIncludePath(.{ .path = "." }); const test_exe = b.addTest(.{ .root_source_file = .{ .path = "main.zig" }, @@ -33,7 +33,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize }); test_exe.linkLibrary(lib_a); test_exe.linkLibrary(lib_b); - test_exe.addIncludePath("."); + test_exe.addIncludePath(.{ .path = "." }); test_step.dependOn(&b.addRunArtifact(test_exe).step); } diff --git a/test/link/macho/bugs/13056/build.zig b/test/link/macho/bugs/13056/build.zig index 1ac50a61c62d..eade752bc46a 100644 --- a/test/link/macho/bugs/13056/build.zig +++ b/test/link/macho/bugs/13056/build.zig @@ -23,13 +23,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .name = "test", .optimize = optimize, }); - exe.addSystemIncludePath(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/include" }) catch unreachable); - exe.addIncludePath(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/include/c++/v1" }) catch unreachable); - exe.addCSourceFile("test.cpp", &.{ + exe.addSystemIncludePath(.{ .path = b.pathJoin(&.{ sdk.path, "/usr/include" }) }); + exe.addIncludePath(.{ .path = b.pathJoin(&.{ sdk.path, "/usr/include/c++/v1" }) }); + exe.addCSourceFile(.{ .file = .{ .path = "test.cpp" }, .flags = &.{ "-nostdlib++", "-nostdinc++", - }); - exe.addObjectFile(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/lib/libc++.tbd" }) catch unreachable); + } }); + exe.addObjectFile(.{ .path = b.pathJoin(&.{ sdk.path, "/usr/lib/libc++.tbd" }) }); const run_cmd = b.addRunArtifact(exe); run_cmd.expectStdErrEqual("x: 5\n"); diff --git a/test/link/macho/dead_strip/build.zig b/test/link/macho/dead_strip/build.zig index 8dd75b70194f..8b8449b6b214 100644 --- a/test/link/macho/dead_strip/build.zig +++ b/test/link/macho/dead_strip/build.zig @@ -52,7 +52,7 @@ fn createScenario( .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} }); exe.linkLibC(); return exe; } diff --git a/test/link/macho/dead_strip_dylibs/build.zig b/test/link/macho/dead_strip_dylibs/build.zig index 4767a7cf34a7..cf243b523156 100644 --- a/test/link/macho/dead_strip_dylibs/build.zig +++ b/test/link/macho/dead_strip_dylibs/build.zig @@ -53,7 +53,7 @@ fn createScenario( .name = name, .optimize = optimize, }); - exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} }); exe.linkLibC(); exe.linkFramework("Cocoa"); return exe; diff --git a/test/link/macho/dylib/build.zig b/test/link/macho/dylib/build.zig index de693625d03d..aecc9b38ec10 100644 --- a/test/link/macho/dylib/build.zig +++ b/test/link/macho/dylib/build.zig @@ -21,7 +21,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = target, }); - dylib.addCSourceFile("a.c", &.{}); + dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); dylib.linkLibC(); const check_dylib = dylib.checkObject(); @@ -39,10 +39,10 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &.{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} }); exe.linkSystemLibrary("a"); - exe.addLibraryPathDirectorySource(dylib.getOutputDirectorySource()); - exe.addRPathDirectorySource(dylib.getOutputDirectorySource()); + exe.addLibraryPath(dylib.getEmitDirectory()); + exe.addRPath(dylib.getEmitDirectory()); exe.linkLibC(); const check_exe = exe.checkObject(); @@ -55,7 +55,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize check_exe.checkStart(); check_exe.checkExact("cmd RPATH"); - check_exe.checkExactFileSource("path", dylib.getOutputDirectorySource()); + check_exe.checkExactPath("path", dylib.getOutputDirectorySource()); test_step.dependOn(&check_exe.step); const run = b.addRunArtifact(exe); diff --git a/test/link/macho/empty/build.zig b/test/link/macho/empty/build.zig index 9933746d533a..6a1350770271 100644 --- a/test/link/macho/empty/build.zig +++ b/test/link/macho/empty/build.zig @@ -20,8 +20,8 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &[0][]const u8{}); - exe.addCSourceFile("empty.c", &[0][]const u8{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} }); + exe.addCSourceFile(.{ .file = .{ .path = "empty.c" }, .flags = &[0][]const u8{} }); exe.linkLibC(); const run_cmd = b.addRunArtifact(exe); diff --git a/test/link/macho/entry/build.zig b/test/link/macho/entry/build.zig index a5269ff6abf5..fcba02cd9a88 100644 --- a/test/link/macho/entry/build.zig +++ b/test/link/macho/entry/build.zig @@ -18,7 +18,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = .{ .os_tag = .macos }, }); - exe.addCSourceFile("main.c", &.{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} }); exe.linkLibC(); exe.entry_symbol_name = "_non_main"; diff --git a/test/link/macho/entry_in_archive/build.zig b/test/link/macho/entry_in_archive/build.zig index f4b04bd162aa..f48e612d95d4 100644 --- a/test/link/macho/entry_in_archive/build.zig +++ b/test/link/macho/entry_in_archive/build.zig @@ -18,7 +18,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = .{ .os_tag = .macos }, }); - lib.addCSourceFile("main.c", &.{}); + lib.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} }); lib.linkLibC(); const exe = b.addExecutable(.{ diff --git a/test/link/macho/entry_in_dylib/build.zig b/test/link/macho/entry_in_dylib/build.zig index a5deaa73fecb..97ffa917b451 100644 --- a/test/link/macho/entry_in_dylib/build.zig +++ b/test/link/macho/entry_in_dylib/build.zig @@ -18,7 +18,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = .{ .os_tag = .macos }, }); - lib.addCSourceFile("bootstrap.c", &.{}); + lib.addCSourceFile(.{ .file = .{ .path = "bootstrap.c" }, .flags = &.{} }); lib.linkLibC(); lib.linker_allow_shlib_undefined = true; @@ -27,7 +27,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = .{ .os_tag = .macos }, }); - exe.addCSourceFile("main.c", &.{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} }); exe.linkLibrary(lib); exe.linkLibC(); exe.entry_symbol_name = "_bootstrap"; diff --git a/test/link/macho/headerpad/build.zig b/test/link/macho/headerpad/build.zig index 2c1146f2497a..e1aebb0a8f2c 100644 --- a/test/link/macho/headerpad/build.zig +++ b/test/link/macho/headerpad/build.zig @@ -113,7 +113,7 @@ fn simpleExe( .name = name, .optimize = optimize, }); - exe.addCSourceFile("main.c", &.{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} }); exe.linkLibC(); exe.linkFramework("CoreFoundation"); exe.linkFramework("Foundation"); diff --git a/test/link/macho/needed_framework/build.zig b/test/link/macho/needed_framework/build.zig index 2323f277e6f9..f189ef7578de 100644 --- a/test/link/macho/needed_framework/build.zig +++ b/test/link/macho/needed_framework/build.zig @@ -20,7 +20,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .name = "test", .optimize = optimize, }); - exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} }); exe.linkLibC(); exe.linkFrameworkNeeded("Cocoa"); exe.dead_strip_dylibs = true; diff --git a/test/link/macho/needed_library/build.zig b/test/link/macho/needed_library/build.zig index 26c69ea81500..1c2cd9b45215 100644 --- a/test/link/macho/needed_library/build.zig +++ b/test/link/macho/needed_library/build.zig @@ -21,7 +21,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = target, }); - dylib.addCSourceFile("a.c", &.{}); + dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); dylib.linkLibC(); // -dead_strip_dylibs @@ -31,11 +31,11 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} }); exe.linkLibC(); exe.linkSystemLibraryNeeded("a"); - exe.addLibraryPathDirectorySource(dylib.getOutputDirectorySource()); - exe.addRPathDirectorySource(dylib.getOutputDirectorySource()); + exe.addLibraryPath(dylib.getEmitDirectory()); + exe.addRPath(dylib.getEmitDirectory()); exe.dead_strip_dylibs = true; const check = exe.checkObject(); diff --git a/test/link/macho/objc/build.zig b/test/link/macho/objc/build.zig index 4cd12f786fe1..979c0aa94327 100644 --- a/test/link/macho/objc/build.zig +++ b/test/link/macho/objc/build.zig @@ -18,9 +18,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .name = "test", .optimize = optimize, }); - exe.addIncludePath("."); - exe.addCSourceFile("Foo.m", &[0][]const u8{}); - exe.addCSourceFile("test.m", &[0][]const u8{}); + exe.addIncludePath(.{ .path = "." }); + exe.addCSourceFile(.{ .file = .{ .path = "Foo.m" }, .flags = &[0][]const u8{} }); + exe.addCSourceFile(.{ .file = .{ .path = "test.m" }, .flags = &[0][]const u8{} }); exe.linkLibC(); // TODO when we figure out how to ship framework stubs for cross-compilation, // populate paths to the sysroot here. diff --git a/test/link/macho/objcpp/build.zig b/test/link/macho/objcpp/build.zig index aebf0e17fd93..183d3cb9d3ef 100644 --- a/test/link/macho/objcpp/build.zig +++ b/test/link/macho/objcpp/build.zig @@ -19,9 +19,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, }); b.default_step.dependOn(&exe.step); - exe.addIncludePath("."); - exe.addCSourceFile("Foo.mm", &[0][]const u8{}); - exe.addCSourceFile("test.mm", &[0][]const u8{}); + exe.addIncludePath(.{ .path = "." }); + exe.addCSourceFile(.{ .file = .{ .path = "Foo.mm" }, .flags = &[0][]const u8{} }); + exe.addCSourceFile(.{ .file = .{ .path = "test.mm" }, .flags = &[0][]const u8{} }); exe.linkLibCpp(); // TODO when we figure out how to ship framework stubs for cross-compilation, // populate paths to the sysroot here. diff --git a/test/link/macho/pagezero/build.zig b/test/link/macho/pagezero/build.zig index f7b13e171ebc..5a0713846448 100644 --- a/test/link/macho/pagezero/build.zig +++ b/test/link/macho/pagezero/build.zig @@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &.{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} }); exe.linkLibC(); exe.pagezero_size = 0x4000; @@ -39,7 +39,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &.{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} }); exe.linkLibC(); exe.pagezero_size = 0; diff --git a/test/link/macho/search_strategy/build.zig b/test/link/macho/search_strategy/build.zig index df0cab9d5df7..3ecefc306bb0 100644 --- a/test/link/macho/search_strategy/build.zig +++ b/test/link/macho/search_strategy/build.zig @@ -55,7 +55,7 @@ fn createScenario( .optimize = optimize, .target = target, }); - static.addCSourceFile("a.c", &.{}); + static.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); static.linkLibC(); static.override_dest_dir = std.Build.InstallDir{ .custom = "static", @@ -67,7 +67,7 @@ fn createScenario( .optimize = optimize, .target = target, }); - dylib.addCSourceFile("a.c", &.{}); + dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); dylib.linkLibC(); dylib.override_dest_dir = std.Build.InstallDir{ .custom = "dynamic", @@ -78,11 +78,11 @@ fn createScenario( .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &.{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} }); exe.linkSystemLibraryName(name); exe.linkLibC(); - exe.addLibraryPathDirectorySource(static.getOutputDirectorySource()); - exe.addLibraryPathDirectorySource(dylib.getOutputDirectorySource()); - exe.addRPathDirectorySource(dylib.getOutputDirectorySource()); + exe.addLibraryPath(static.getEmitDirectory()); + exe.addLibraryPath(dylib.getEmitDirectory()); + exe.addRPath(dylib.getEmitDirectory()); return exe; } diff --git a/test/link/macho/stack_size/build.zig b/test/link/macho/stack_size/build.zig index d7ea44b626b0..4210d9802d00 100644 --- a/test/link/macho/stack_size/build.zig +++ b/test/link/macho/stack_size/build.zig @@ -20,7 +20,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &.{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} }); exe.linkLibC(); exe.stack_size = 0x100000000; diff --git a/test/link/macho/tls/build.zig b/test/link/macho/tls/build.zig index 555fe207c55e..443f698ea6be 100644 --- a/test/link/macho/tls/build.zig +++ b/test/link/macho/tls/build.zig @@ -21,7 +21,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = target, }); - lib.addCSourceFile("a.c", &.{}); + lib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); lib.linkLibC(); const test_exe = b.addTest(.{ diff --git a/test/link/macho/unwind_info/build.zig b/test/link/macho/unwind_info/build.zig index c3b9d1ea4f17..ead71b15d230 100644 --- a/test/link/macho/unwind_info/build.zig +++ b/test/link/macho/unwind_info/build.zig @@ -75,7 +75,7 @@ fn createScenario( .target = target, }); b.default_step.dependOn(&exe.step); - exe.addIncludePath("."); + exe.addIncludePath(.{ .path = "." }); exe.addCSourceFiles(&[_][]const u8{ "main.cpp", "simple_string.cpp", diff --git a/test/link/macho/weak_framework/build.zig b/test/link/macho/weak_framework/build.zig index 8069d62bd388..8a71584b254c 100644 --- a/test/link/macho/weak_framework/build.zig +++ b/test/link/macho/weak_framework/build.zig @@ -18,7 +18,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .name = "test", .optimize = optimize, }); - exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} }); exe.linkLibC(); exe.linkFrameworkWeak("Cocoa"); diff --git a/test/link/macho/weak_library/build.zig b/test/link/macho/weak_library/build.zig index 831a96e88042..3d0819c19b15 100644 --- a/test/link/macho/weak_library/build.zig +++ b/test/link/macho/weak_library/build.zig @@ -21,7 +21,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .target = target, .optimize = optimize, }); - dylib.addCSourceFile("a.c", &.{}); + dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); dylib.linkLibC(); b.installArtifact(dylib); @@ -30,11 +30,11 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .target = target, .optimize = optimize, }); - exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} }); exe.linkLibC(); exe.linkSystemLibraryWeak("a"); - exe.addLibraryPathDirectorySource(dylib.getOutputDirectorySource()); - exe.addRPathDirectorySource(dylib.getOutputDirectorySource()); + exe.addLibraryPath(dylib.getEmitDirectory()); + exe.addRPath(dylib.getEmitDirectory()); const check = exe.checkObject(); check.checkStart(); diff --git a/test/link/static_libs_from_object_files/build.zig b/test/link/static_libs_from_object_files/build.zig index 13365ba15334..e5a4b79acd8f 100644 --- a/test/link/static_libs_from_object_files/build.zig +++ b/test/link/static_libs_from_object_files/build.zig @@ -2,7 +2,7 @@ const std = @import("std"); const builtin = @import("builtin"); const Build = std.Build; -const FileSource = Build.FileSource; +const LazyPath = Build.LazyPath; const Step = Build.Step; const Run = Step.Run; const WriteFile = Step.WriteFile; @@ -14,7 +14,7 @@ pub fn build(b: *Build) void { b.default_step = test_step; // generate c files - const files = b.allocator.alloc(std.Build.FileSource, nb_files) catch unreachable; + const files = b.allocator.alloc(LazyPath, nb_files) catch unreachable; defer b.allocator.free(files); { for (files[0 .. nb_files - 1], 1..nb_files) |*file, i| { @@ -47,7 +47,7 @@ pub fn build(b: *Build) void { add(b, test_step, files, .ReleaseFast); } -fn add(b: *Build, test_step: *Step, files: []const std.Build.FileSource, optimize: std.builtin.OptimizeMode) void { +fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.builtin.OptimizeMode) void { const flags = [_][]const u8{ "-Wall", "-std=c11", @@ -63,7 +63,7 @@ fn add(b: *Build, test_step: *Step, files: []const std.Build.FileSource, optimiz }); for (files) |file| { - exe.addCSourceFileSource(.{ .source = file, .args = &flags }); + exe.addCSourceFile(.{ .file = file, .flags = &flags }); } const run_cmd = b.addRunArtifact(exe); @@ -88,7 +88,7 @@ fn add(b: *Build, test_step: *Step, files: []const std.Build.FileSource, optimiz for (files, 1..) |file, i| { const lib = if (i & 1 == 0) lib_a else lib_b; - lib.addCSourceFileSource(.{ .source = file, .args = &flags }); + lib.addCSourceFile(.{ .file = file, .flags = &flags }); } const exe = b.addExecutable(.{ @@ -125,7 +125,7 @@ fn add(b: *Build, test_step: *Step, files: []const std.Build.FileSource, optimiz .target = .{}, .optimize = optimize, }); - obj.addCSourceFileSource(.{ .source = file, .args = &flags }); + obj.addCSourceFile(.{ .file = file, .flags = &flags }); const lib = if (i & 1 == 0) lib_a else lib_b; lib.addObject(obj); diff --git a/test/link/wasm/extern/build.zig b/test/link/wasm/extern/build.zig index 55562143c2f0..a50589952042 100644 --- a/test/link/wasm/extern/build.zig +++ b/test/link/wasm/extern/build.zig @@ -19,7 +19,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = .{ .cpu_arch = .wasm32, .os_tag = .wasi }, }); - exe.addCSourceFile("foo.c", &.{}); + exe.addCSourceFile(.{ .file = .{ .path = "foo.c" }, .flags = &.{} }); exe.use_llvm = false; exe.use_lld = false; diff --git a/test/link/wasm/infer-features/build.zig b/test/link/wasm/infer-features/build.zig index 35ab68abf67e..6264edfba912 100644 --- a/test/link/wasm/infer-features/build.zig +++ b/test/link/wasm/infer-features/build.zig @@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void { .os_tag = .freestanding, }, }); - c_obj.addCSourceFile("foo.c", &.{}); + c_obj.addCSourceFile(.{ .file = .{ .path = "foo.c" }, .flags = &.{} }); // Wasm library that doesn't have any features specified. This will // infer its featureset from other linked object files. diff --git a/test/src/Cases.zig b/test/src/Cases.zig index ce080b5490f8..694c2800872b 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -518,12 +518,12 @@ pub fn lowerToBuildSteps( } const writefiles = b.addWriteFiles(); - var file_sources = std.StringHashMap(std.Build.FileSource).init(b.allocator); + var file_sources = std.StringHashMap(std.Build.LazyPath).init(b.allocator); defer file_sources.deinit(); for (update.files.items) |file| { file_sources.put(file.path, writefiles.add(file.path, file.src)) catch @panic("OOM"); } - const root_source_file = writefiles.files.items[0].getFileSource(); + const root_source_file = writefiles.files.items[0].getPath(); const artifact = if (case.is_test) b.addTest(.{ .root_source_file = root_source_file, @@ -577,7 +577,7 @@ pub fn lowerToBuildSteps( parent_step.dependOn(&artifact.step); }, .CompareObjectFile => |expected_output| { - const check = b.addCheckFile(artifact.getOutputSource(), .{ + const check = b.addCheckFile(artifact.getEmittedBin(), .{ .expected_exact = expected_output, }); diff --git a/test/src/CompareOutput.zig b/test/src/CompareOutput.zig index d2f7a2308986..39226f036929 100644 --- a/test/src/CompareOutput.zig +++ b/test/src/CompareOutput.zig @@ -99,7 +99,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { .target = .{}, .optimize = .Debug, }); - exe.addAssemblyFileSource(write_src.files.items[0].getFileSource()); + exe.addAssemblyFile(write_src.files.items[0].getPath()); const run = b.addRunArtifact(exe); run.setName(annotated_case_name); @@ -119,7 +119,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { const exe = b.addExecutable(.{ .name = "test", - .root_source_file = write_src.files.items[0].getFileSource(), + .root_source_file = write_src.files.items[0].getPath(), .optimize = optimize, .target = .{}, }); @@ -145,7 +145,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { const exe = b.addExecutable(.{ .name = "test", - .root_source_file = write_src.files.items[0].getFileSource(), + .root_source_file = write_src.files.items[0].getPath(), .target = .{}, .optimize = .Debug, }); diff --git a/test/src/StackTrace.zig b/test/src/StackTrace.zig index 71394e00b8ec..c42e9114be8c 100644 --- a/test/src/StackTrace.zig +++ b/test/src/StackTrace.zig @@ -75,7 +75,7 @@ fn addExpect( const write_src = b.addWriteFile("source.zig", source); const exe = b.addExecutable(.{ .name = "test", - .root_source_file = write_src.files.items[0].getFileSource(), + .root_source_file = write_src.files.items[0].getPath(), .optimize = optimize_mode, .target = .{}, }); @@ -88,7 +88,7 @@ fn addExpect( const check_run = b.addRunArtifact(self.check_exe); check_run.setName(annotated_case_name); - check_run.addFileSourceArg(run.captureStdErr()); + check_run.addFileArg(run.captureStdErr()); check_run.addArgs(&.{ @tagName(optimize_mode), }); diff --git a/test/src/run_translated_c.zig b/test/src/run_translated_c.zig index 42db8b19c561..cd2d0d9e3b16 100644 --- a/test/src/run_translated_c.zig +++ b/test/src/run_translated_c.zig @@ -85,7 +85,7 @@ pub const RunTranslatedCContext = struct { _ = write_src.add(src_file.filename, src_file.source); } const translate_c = b.addTranslateC(.{ - .source_file = write_src.files.items[0].getFileSource(), + .source_file = write_src.files.items[0].getPath(), .target = .{}, .optimize = .Debug, }); diff --git a/test/src/translate_c.zig b/test/src/translate_c.zig index a1f312d623a7..a71886099f7a 100644 --- a/test/src/translate_c.zig +++ b/test/src/translate_c.zig @@ -108,7 +108,7 @@ pub const TranslateCContext = struct { } const translate_c = b.addTranslateC(.{ - .source_file = write_src.files.items[0].getFileSource(), + .source_file = write_src.files.items[0].getPath(), .target = case.target, .optimize = .Debug, }); diff --git a/test/standalone/c_compiler/build.zig b/test/standalone/c_compiler/build.zig index 6c5f2b4db6b9..36622dde185a 100644 --- a/test/standalone/c_compiler/build.zig +++ b/test/standalone/c_compiler/build.zig @@ -19,7 +19,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = target, }); - exe_c.addCSourceFile("test.c", &[0][]const u8{}); + exe_c.addCSourceFile(.{ .file = .{ .path = "test.c" }, .flags = &[0][]const u8{} }); exe_c.linkLibC(); const exe_cpp = b.addExecutable(.{ @@ -28,7 +28,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .target = target, }); b.default_step.dependOn(&exe_cpp.step); - exe_cpp.addCSourceFile("test.cpp", &[0][]const u8{}); + exe_cpp.addCSourceFile(.{ .file = .{ .path = "test.cpp" }, .flags = &[0][]const u8{} }); exe_cpp.linkLibCpp(); switch (target.getOsTag()) { diff --git a/test/standalone/embed_generated_file/build.zig b/test/standalone/embed_generated_file/build.zig index af1ae0a00f70..b914b9ffab3f 100644 --- a/test/standalone/embed_generated_file/build.zig +++ b/test/standalone/embed_generated_file/build.zig @@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void { .optimize = .Debug, }); exe.addAnonymousModule("bootloader.elf", .{ - .source_file = bootloader.getOutputSource(), + .source_file = bootloader.getEmittedBin(), }); test_step.dependOn(&exe.step); diff --git a/test/standalone/issue_794/build.zig b/test/standalone/issue_794/build.zig index 8527f4af2c77..1a319798cb65 100644 --- a/test/standalone/issue_794/build.zig +++ b/test/standalone/issue_794/build.zig @@ -7,7 +7,7 @@ pub fn build(b: *std.Build) void { const test_artifact = b.addTest(.{ .root_source_file = .{ .path = "main.zig" }, }); - test_artifact.addIncludePath("a_directory"); + test_artifact.addIncludePath(.{ .path = "a_directory" }); test_step.dependOn(&test_artifact.step); } diff --git a/test/standalone/issue_8550/build.zig b/test/standalone/issue_8550/build.zig index 32bb3e10eb89..a7815618744b 100644 --- a/test/standalone/issue_8550/build.zig +++ b/test/standalone/issue_8550/build.zig @@ -19,8 +19,8 @@ pub fn build(b: *std.Build) !void { .optimize = optimize, .target = target, }); - kernel.addObjectFile("./boot.S"); - kernel.setLinkerScriptPath(.{ .path = "./linker.ld" }); + kernel.addObjectFile(.{ .path = "./boot.S" }); + kernel.setLinkerScript(.{ .path = "./linker.ld" }); b.installArtifact(kernel); test_step.dependOn(&kernel.step); diff --git a/test/standalone/main_pkg_path/build.zig b/test/standalone/main_pkg_path/build.zig index f3eacce767b8..9f7f67255454 100644 --- a/test/standalone/main_pkg_path/build.zig +++ b/test/standalone/main_pkg_path/build.zig @@ -7,7 +7,7 @@ pub fn build(b: *std.Build) void { const test_exe = b.addTest(.{ .root_source_file = .{ .path = "a/test.zig" }, }); - test_exe.setMainPkgPath("."); + test_exe.setMainPkgPath(.{.path="."}); test_step.dependOn(&b.addRunArtifact(test_exe).step); } diff --git a/test/standalone/mix_c_files/build.zig b/test/standalone/mix_c_files/build.zig index 39a0da27ad4c..b675be9eebe4 100644 --- a/test/standalone/mix_c_files/build.zig +++ b/test/standalone/mix_c_files/build.zig @@ -21,7 +21,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .root_source_file = .{ .path = "main.zig" }, .optimize = optimize, }); - exe.addCSourceFile("test.c", &[_][]const u8{"-std=c11"}); + exe.addCSourceFile(.{ .file = .{ .path = "test.c" }, .flags = &[_][]const u8{"-std=c11"} }); exe.linkLibC(); const run_cmd = b.addRunArtifact(exe); diff --git a/test/standalone/mix_o_files/build.zig b/test/standalone/mix_o_files/build.zig index 2d9323ff00de..980e52ad3baa 100644 --- a/test/standalone/mix_o_files/build.zig +++ b/test/standalone/mix_o_files/build.zig @@ -19,7 +19,10 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .target = target, }); - exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"}); + exe.addCSourceFile(.{ + .file = .{ .path = "test.c" }, + .flags = &[_][]const u8{"-std=c99"}, + }); exe.addObject(obj); exe.linkSystemLibrary("c"); diff --git a/test/standalone/shared_library/build.zig b/test/standalone/shared_library/build.zig index 3034d8901783..3c1f95cf43ea 100644 --- a/test/standalone/shared_library/build.zig +++ b/test/standalone/shared_library/build.zig @@ -19,7 +19,10 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, }); - exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"}); + exe.addCSourceFile(.{ + .file = .{ .path = "test.c" }, + .flags = &[_][]const u8{"-std=c99"}, + }); exe.linkLibrary(lib); exe.linkSystemLibrary("c"); diff --git a/test/standalone/stack_iterator/build.zig b/test/standalone/stack_iterator/build.zig index 1c5a9673ceca..628210452c42 100644 --- a/test/standalone/stack_iterator/build.zig +++ b/test/standalone/stack_iterator/build.zig @@ -74,7 +74,10 @@ pub fn build(b: *std.Build) void { if (target.isWindows()) c_shared_lib.defineCMacro("LIB_API", "__declspec(dllexport)"); c_shared_lib.strip = false; - c_shared_lib.addCSourceFile("shared_lib.c", &.{"-fomit-frame-pointer"}); + c_shared_lib.addCSourceFile(.{ + .file = .{ .path = "shared_lib.c" }, + .flags = &.{"-fomit-frame-pointer"}, + }); c_shared_lib.linkLibC(); const exe = b.addExecutable(.{ diff --git a/test/standalone/static_c_lib/build.zig b/test/standalone/static_c_lib/build.zig index 3feae705fcdb..1e3e80c36323 100644 --- a/test/standalone/static_c_lib/build.zig +++ b/test/standalone/static_c_lib/build.zig @@ -11,15 +11,15 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .target = .{}, }); - foo.addCSourceFile("foo.c", &[_][]const u8{}); - foo.addIncludePath("."); + foo.addCSourceFile(.{ .file = .{ .path = "foo.c" }, .flags = &[_][]const u8{} }); + foo.addIncludePath(.{ .path = "." }); const test_exe = b.addTest(.{ .root_source_file = .{ .path = "foo.zig" }, .optimize = optimize, }); test_exe.linkLibrary(foo); - test_exe.addIncludePath("."); + test_exe.addIncludePath(.{ .path = "." }); test_step.dependOn(&b.addRunArtifact(test_exe).step); } diff --git a/test/standalone/use_alias/build.zig b/test/standalone/use_alias/build.zig index 6fa0a1f3a5b6..b20c20d5066f 100644 --- a/test/standalone/use_alias/build.zig +++ b/test/standalone/use_alias/build.zig @@ -10,7 +10,7 @@ pub fn build(b: *std.Build) void { .root_source_file = .{ .path = "main.zig" }, .optimize = optimize, }); - main.addIncludePath("."); + main.addIncludePath(.{.path="."}); test_step.dependOn(&b.addRunArtifact(main).step); } diff --git a/test/tests.zig b/test/tests.zig index bba1b00142d1..f8e1064fc800 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -759,7 +759,7 @@ pub fn addCliTests(b: *std.Build) *Step { "-fno-emit-bin", "-fno-emit-h", "-fstrip", "-OReleaseFast", }); - run.addFileSourceArg(writefile.files.items[0].getFileSource()); + run.addFileArg(writefile.files.items[0].getPath()); const example_s = run.addPrefixedOutputFileArg("-femit-asm=", "example.s"); const checkfile = b.addCheckFile(example_s, .{ @@ -1017,8 +1017,8 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { else ""; - these_tests.overrideZigLibDir("lib"); - these_tests.addIncludePath("test"); + these_tests.overrideZigLibDir(.{ .path = "lib" }); + these_tests.addIncludePath(.{ .path = "test" }); const qualified_name = b.fmt("{s}-{s}-{s}{s}{s}{s}", .{ options.name, @@ -1038,10 +1038,10 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { .link_libc = test_target.link_libc, .target = altered_target, }); - compile_c.overrideZigLibDir("lib"); - compile_c.addCSourceFileSource(.{ - .source = these_tests.getOutputSource(), - .args = &.{ + compile_c.overrideZigLibDir(.{ .path = "lib" }); + compile_c.addCSourceFile(.{ + .file = these_tests.getEmittedBin(), + .flags = &.{ // TODO output -std=c89 compatible C code "-std=c99", "-pedantic", @@ -1058,7 +1058,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { "-Wno-absolute-value", }, }); - compile_c.addIncludePath("lib"); // for zig.h + compile_c.addIncludePath(.{ .path = "lib" }); // for zig.h if (test_target.target.getOsTag() == .windows) { if (true) { // Unfortunately this requires about 8G of RAM for clang to compile @@ -1131,7 +1131,10 @@ pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *S test_step.target_info.dynamic_linker.max_byte = null; } test_step.linkLibC(); - test_step.addCSourceFile("test/c_abi/cfuncs.c", &.{"-std=c99"}); + test_step.addCSourceFile(.{ + .file = .{ .path = "test/c_abi/cfuncs.c" }, + .flags = &.{"-std=c99"}, + }); // This test is intentionally trying to check if the external ABI is // done properly. LTO would be a hindrance to this. From 5c0181841081170a118d8e50af2a09f5006f59e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20=28xq=29=20Quei=C3=9Fner?= Date: Tue, 25 Jul 2023 13:26:32 +0200 Subject: [PATCH 02/10] Introduces `Compile.getEmittedX()` functions, drops `Compile.emit_X`. Resolves #14971 --- build.zig | 5 - lib/std/Build.zig | 52 +--- lib/std/Build/Step.zig | 5 +- lib/std/Build/Step/Compile.zig | 262 ++++++++++++------ lib/std/Build/Step/InstallArtifact.zig | 16 +- lib/std/Build/Step/Run.zig | 7 +- lib/std/Build/util.zig | 53 ++++ test/link/glibc_compat/build.zig | 1 + test/link/macho/needed_library/build.zig | 1 + test/link/macho/search_strategy/build.zig | 2 + test/src/Cases.zig | 5 +- test/standalone.zig | 18 +- .../standalone/embed_generated_file/build.zig | 2 + test/standalone/emit_asm_and_bin/build.zig | 4 +- test/standalone/issue_12588/build.zig | 5 +- test/standalone/issue_339/build.zig | 2 + test/standalone/issue_5825/build.zig | 2 + test/standalone/issue_794/build.zig | 1 + test/standalone/main_pkg_path/build.zig | 2 +- test/standalone/strip_empty_loop/build.zig | 3 + test/standalone/use_alias/build.zig | 2 +- test/tests.zig | 2 + 22 files changed, 289 insertions(+), 163 deletions(-) create mode 100644 lib/std/Build/util.zig diff --git a/build.zig b/build.zig index b7db2d7aa81d..07130e84b051 100644 --- a/build.zig +++ b/build.zig @@ -59,7 +59,6 @@ pub fn build(b: *std.Build) !void { .target = target, }); autodoc_test.overrideZigLibDir(.{ .path = "lib" }); - autodoc_test.emit_bin = .no_emit; // https://github.com/ziglang/zig/issues/16351 const install_std_docs = b.addInstallDirectory(.{ .source_dir = autodoc_test.getEmittedDocs(), .install_dir = .prefix, @@ -196,10 +195,6 @@ pub fn build(b: *std.Build) !void { exe.pie = pie; exe.sanitize_thread = sanitize_thread; exe.entitlements = entitlements; - // TODO -femit-bin/-fno-emit-bin should be inferred by the build system - // based on whether or not the exe is run or installed. - // https://github.com/ziglang/zig/issues/16351 - if (no_bin) exe.emit_bin = .no_emit; exe.build_id = b.option( std.Build.Step.Compile.BuildId, diff --git a/lib/std/Build.zig b/lib/std/Build.zig index e68a860b8218..08da650d640b 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -19,6 +19,8 @@ const NativeTargetInfo = std.zig.system.NativeTargetInfo; const Sha256 = std.crypto.hash.sha2.Sha256; const Build = @This(); +const build_util = @import("Build/util.zig"); + pub const Cache = @import("Build/Cache.zig"); /// deprecated: use `Step.Compile`. @@ -1679,7 +1681,7 @@ pub const LazyPath = union(enum) { .generated => |gen| return gen.path orelse { std.debug.getStderrMutex().lock(); const stderr = std.io.getStdErr(); - dumpBadGetPathHelp(gen.step, stderr, src_builder, asking_step) catch {}; + build_util.dumpBadGetPathHelp(gen.step, stderr, src_builder, asking_step) catch {}; @panic("misconfigured build script"); }, } @@ -1694,54 +1696,6 @@ pub const LazyPath = union(enum) { } }; -/// In this function the stderr mutex has already been locked. -fn dumpBadGetPathHelp( - s: *Step, - stderr: fs.File, - src_builder: *Build, - asking_step: ?*Step, -) anyerror!void { - const w = stderr.writer(); - try w.print( - \\getPath() was called on a GeneratedFile that wasn't built yet. - \\ source package path: {s} - \\ Is there a missing Step dependency on step '{s}'? - \\ - , .{ - src_builder.build_root.path orelse ".", - s.name, - }); - - const tty_config = std.io.tty.detectConfig(stderr); - tty_config.setColor(w, .red) catch {}; - try stderr.writeAll(" The step was created by this stack trace:\n"); - tty_config.setColor(w, .reset) catch {}; - - const debug_info = std.debug.getSelfDebugInfo() catch |err| { - try w.print("Unable to dump stack trace: Unable to open debug info: {s}\n", .{@errorName(err)}); - return; - }; - const ally = debug_info.allocator; - std.debug.writeStackTrace(s.getStackTrace(), w, ally, debug_info, tty_config) catch |err| { - try stderr.writer().print("Unable to dump stack trace: {s}\n", .{@errorName(err)}); - return; - }; - if (asking_step) |as| { - tty_config.setColor(w, .red) catch {}; - try stderr.writeAll(" The step that is missing a dependency on the above step was created by this stack trace:\n"); - tty_config.setColor(w, .reset) catch {}; - - std.debug.writeStackTrace(as.getStackTrace(), w, ally, debug_info, tty_config) catch |err| { - try stderr.writer().print("Unable to dump stack trace: {s}\n", .{@errorName(err)}); - return; - }; - } - - tty_config.setColor(w, .red) catch {}; - try stderr.writeAll(" Hope that helps. Proceeding to panic.\n"); - tty_config.setColor(w, .reset) catch {}; -} - /// Allocates a new string for assigning a value to a named macro. /// If the value is omitted, it is set to 1. /// `name` and `value` need not live longer than the function call. diff --git a/lib/std/Build/Step.zig b/lib/std/Build/Step.zig index 991283dbc526..274188f7f2f3 100644 --- a/lib/std/Build/Step.zig +++ b/lib/std/Build/Step.zig @@ -423,7 +423,10 @@ pub fn evalZigProcess( }); } - if (s.cast(Compile)) |compile| if (compile.emit_bin == .no_emit) return result; + if (s.cast(Compile)) |compile| { + if (compile.generated_bin == null) // TODO(xq): How to handle this properly?! + return result; + } return result orelse return s.fail( "the following command failed to communicate the compilation result:\n{s}", diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index e3d4aa4155e4..d6eae499d262 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -21,6 +21,8 @@ const InstallDir = std.Build.InstallDir; const GeneratedFile = std.Build.GeneratedFile; const Compile = @This(); +const build_util = @import("../util.zig"); + pub const base_id: Step.Id = .compile; step: Step, @@ -46,14 +48,6 @@ framework_dirs: ArrayList(LazyPath), frameworks: StringHashMap(FrameworkLinkInfo), verbose_link: bool, verbose_cc: bool, -emit_asm: EmitOption = .default, -emit_bin: EmitOption = .default, -emit_implib: EmitOption = .default, -emit_llvm_bc: EmitOption = .default, -emit_llvm_ir: EmitOption = .default, -// Lots of things depend on emit_h having a consistent path, -// so it is not an EmitOption for now. -emit_h: bool = false, bundle_compiler_rt: ?bool = null, single_threaded: ?bool, stack_protector: ?bool = null, @@ -87,6 +81,9 @@ export_symbol_names: []const []const u8 = &.{}, root_src: ?LazyPath, out_h_filename: []const u8, +out_ll_filename: []const u8, +out_bc_filename: []const u8, +out_asm_filename: []const u8, out_lib_filename: []const u8, out_pdb_filename: []const u8, modules: std.StringArrayHashMap(*Module), @@ -210,12 +207,16 @@ use_lld: ?bool, /// otherwise. expect_errors: []const []const u8 = &.{}, -output_path_source: GeneratedFile, -output_lib_path_source: GeneratedFile, -output_h_path_source: GeneratedFile, -output_pdb_path_source: GeneratedFile, -output_dirname_source: GeneratedFile, +emit_directory: GeneratedFile, + generated_docs: ?*GeneratedFile, +generated_asm: ?*GeneratedFile, +generated_bin: ?*GeneratedFile, +generated_pdb: ?*GeneratedFile, +generated_implib: ?*GeneratedFile, +generated_llvm_bc: ?*GeneratedFile, +generated_llvm_ir: ?*GeneratedFile, +generated_h: ?*GeneratedFile, pub const CSourceFiles = struct { files: []const []const u8, @@ -373,22 +374,6 @@ pub const Kind = enum { pub const Linkage = enum { dynamic, static }; -pub const EmitOption = union(enum) { - default: void, - no_emit: void, - emit: void, - emit_to: []const u8, - - fn getArg(self: @This(), b: *std.Build, arg_name: []const u8) ?[]const u8 { - return switch (self) { - .no_emit => b.fmt("-fno-{s}", .{arg_name}), - .default => null, - .emit => b.fmt("-f{s}", .{arg_name}), - .emit_to => |path| b.fmt("-f{s}={s}", .{ arg_name, path }), - }; - } -}; - pub fn create(owner: *std.Build, options: Options) *Compile { const name = owner.dupe(options.name); const root_src: ?LazyPath = if (options.root_source_file) |rsrc| rsrc.dupe(owner) else null; @@ -454,6 +439,9 @@ pub fn create(owner: *std.Build, options: Options) *Compile { .version = options.version, .out_filename = out_filename, .out_h_filename = owner.fmt("{s}.h", .{name}), + .out_ll_filename = owner.fmt("{s}.bc", .{name}), + .out_bc_filename = owner.fmt("{s}.ll", .{name}), + .out_asm_filename = owner.fmt("{s}.s", .{name}), .out_lib_filename = undefined, .out_pdb_filename = owner.fmt("{s}.pdb", .{name}), .major_only_filename = null, @@ -480,12 +468,16 @@ pub fn create(owner: *std.Build, options: Options) *Compile { .installed_path = null, .force_undefined_symbols = StringHashMap(void).init(owner.allocator), - .output_path_source = GeneratedFile{ .step = &self.step }, - .output_lib_path_source = GeneratedFile{ .step = &self.step }, - .output_h_path_source = GeneratedFile{ .step = &self.step }, - .output_pdb_path_source = GeneratedFile{ .step = &self.step }, - .output_dirname_source = GeneratedFile{ .step = &self.step }, + .emit_directory = GeneratedFile{ .step = &self.step }, + .generated_docs = null, + .generated_asm = null, + .generated_bin = null, + .generated_pdb = null, + .generated_implib = null, + .generated_llvm_bc = null, + .generated_llvm_ir = null, + .generated_h = null, .target_info = target_info, @@ -692,6 +684,7 @@ pub fn isStaticLibrary(self: *Compile) bool { } pub fn producesPdbFile(self: *Compile) bool { + // TODO: Is this right? Isn't PDB for *any* PE/COFF file? if (!self.target.isWindows() and !self.target.isUefi()) return false; if (self.target.getObjectFormat() == .c) return false; if (self.strip == true or (self.strip == null and self.optimize == .ReleaseSmall)) return false; @@ -968,54 +961,75 @@ pub fn setLibCFile(self: *Compile, libc_file: ?LazyPath) void { self.libc_file = if (libc_file) |f| f.dupe(b) else null; } -pub const getOutputSource = getEmittedBin; // DEPRECATED, use getEmittedBin - -/// Returns the generated executable, library or object file. -/// To run an executable built with zig build, use `run`, or create an install step and invoke it. -pub fn getEmittedBin(self: *Compile) LazyPath { - return .{ .generated = &self.output_path_source }; +fn getEmittedFileGeneric(self: *Compile, output_file: *?*GeneratedFile) LazyPath { + if (output_file.*) |g| { + return .{ .generated = g }; + } + const arena = self.step.owner.allocator; + const generated_file = arena.create(GeneratedFile) catch @panic("OOM"); + generated_file.* = .{ .step = &self.step }; + output_file.* = generated_file; + return .{ .generated = generated_file }; } pub const getOutputDirectorySource = getEmitDirectory; // DEPRECATED, use getEmitDirectory +/// Returns the path to the output directory. pub fn getEmitDirectory(self: *Compile) LazyPath { - return .{ .generated = &self.output_dirname_source }; + return .{ .generated = &self.emit_directory }; +} + +pub const getOutputSource = getEmittedBin; // DEPRECATED, use getEmittedBin + +/// Returns the path to the generated executable, library or object file. +/// To run an executable built with zig build, use `run`, or create an install step and invoke it. +pub fn getEmittedBin(self: *Compile) LazyPath { + return self.getEmittedFileGeneric(&self.generated_bin); } pub const getOutputLibSource = getEmittedImplib; // DEPRECATED, use getEmittedImplib -/// Returns the generated import library. This function can only be called for libraries. +/// Returns the path to the generated import library. This function can only be called for libraries. pub fn getEmittedImplib(self: *Compile) LazyPath { assert(self.kind == .lib); - return .{ .generated = &self.output_lib_path_source }; + return self.getEmittedFileGeneric(&self.generated_implib); } pub const getOutputHSource = getEmittedH; // DEPRECATED, use getEmittedH -/// Returns the generated header file. -/// This function can only be called for libraries or object files which have `emit_h` set. +/// Returns the path to the generated header file. +/// This function can only be called for libraries or objects. pub fn getEmittedH(self: *Compile) LazyPath { assert(self.kind != .exe and self.kind != .@"test"); - assert(self.emit_h); - return .{ .generated = &self.output_h_path_source }; + return self.getEmittedFileGeneric(&self.generated_h); } pub const getOutputPdbSource = getEmittedPdb; // DEPRECATED, use getEmittedPdb /// Returns the generated PDB file. This function can only be called for Windows and UEFI. pub fn getEmittedPdb(self: *Compile) LazyPath { - // TODO: Is this right? Isn't PDB for *any* PE/COFF file? - assert(self.target.isWindows() or self.target.isUefi()); - return .{ .generated = &self.output_pdb_path_source }; + assert(self.producesPdbFile()); + return self.getEmittedFileGeneric(&self.generated_pdb); } +/// Returns the path to the generated documentation directory. pub fn getEmittedDocs(self: *Compile) LazyPath { - if (self.generated_docs) |g| return .{ .generated = g }; - const arena = self.step.owner.allocator; - const generated_file = arena.create(GeneratedFile) catch @panic("OOM"); - generated_file.* = .{ .step = &self.step }; - self.generated_docs = generated_file; - return .{ .generated = generated_file }; + return self.getEmittedFileGeneric(&self.generated_docs); +} + +/// Returns the path to the generated assembly code. +pub fn getEmittedAsm(self: *Compile) LazyPath { + return self.getEmittedFileGeneric(&self.generated_asm); +} + +/// Returns the path to the generated LLVM IR. +pub fn getEmittedLlvmIr(self: *Compile) LazyPath { + return self.getEmittedFileGeneric(&self.generated_llvm_ir); +} + +/// Returns the path to the generated LLVM BC. +pub fn getEmittedLlvmBc(self: *Compile) LazyPath { + return self.getEmittedFileGeneric(&self.generated_llvm_bc); } pub fn addAssemblyFile(self: *Compile, source: LazyPath) void { @@ -1149,6 +1163,12 @@ pub fn setExecCmd(self: *Compile, args: []const ?[]const u8) void { } fn linkLibraryOrObject(self: *Compile, other: *Compile) void { + _ = other.getEmittedBin(); // Force emission of the binary + + if (other.kind == .lib and other.target.isWindows()) { // TODO(xq): Is this the correct logic here? + _ = other.getEmittedImplib(); // Force emission of the binary + } + self.step.dependOn(&other.step); self.link_objects.append(.{ .other_step = other }) catch @panic("OOM"); self.include_dirs.append(.{ .other_step = other }) catch @panic("OOM"); @@ -1268,6 +1288,30 @@ fn constructDepString( } } +fn getGeneratedFilePath(self: *Compile, comptime tag_name: []const u8, asking_step: ?*Step) []const u8 { + const maybe_path: ?*GeneratedFile = @field(self, tag_name); + + const generated_file = maybe_path orelse { + std.debug.getStderrMutex().lock(); + const stderr = std.io.getStdErr(); + + build_util.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, asking_step) catch {}; + + @panic("missing emit option for " ++ tag_name); + }; + + const path = generated_file.path orelse { + std.debug.getStderrMutex().lock(); + const stderr = std.io.getStdErr(); + + build_util.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, asking_step) catch {}; + + @panic(tag_name ++ " is null. Is there a missing step dependency?"); + }; + + return path; +} + fn make(step: *Step, prog_node: *std.Progress.Node) !void { const b = step.owner; const self = @fieldParentPtr(Compile, "step", step); @@ -1353,7 +1397,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { break :l; } - const full_path_lib = other.getEmittedImplib().getPath(b); + // TODO(xq): Is that the right way? + const full_path_lib = if (other.isDynamicLibrary() and other.target.isWindows()) + other.getGeneratedFilePath("generated_implib", &self.step) + else + other.getGeneratedFilePath("generated_bin", &self.step); try zig_args.append(full_path_lib); if (other.linkage == Linkage.dynamic and !self.target.isWindows()) { @@ -1496,14 +1544,36 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { if (b.verbose_cc or self.verbose_cc) try zig_args.append("--verbose-cc"); if (b.verbose_llvm_cpu_features) try zig_args.append("--verbose-llvm-cpu-features"); - if (self.emit_asm.getArg(b, "emit-asm")) |arg| try zig_args.append(arg); - if (self.emit_bin.getArg(b, "emit-bin")) |arg| try zig_args.append(arg); - if (self.generated_docs != null) try zig_args.append("-femit-docs"); - if (self.emit_implib.getArg(b, "emit-implib")) |arg| try zig_args.append(arg); - if (self.emit_llvm_bc.getArg(b, "emit-llvm-bc")) |arg| try zig_args.append(arg); - if (self.emit_llvm_ir.getArg(b, "emit-llvm-ir")) |arg| try zig_args.append(arg); + const Emitter = struct { + ptr: ?*GeneratedFile, + emit_suffix: []const u8, + }; - if (self.emit_h) try zig_args.append("-femit-h"); + const generated_files = [_]Emitter{ + .{ .ptr = self.generated_asm, .emit_suffix = "asm" }, + .{ .ptr = self.generated_bin, .emit_suffix = "bin" }, + .{ .ptr = self.generated_docs, .emit_suffix = "docs" }, + .{ .ptr = self.generated_implib, .emit_suffix = "implib" }, + .{ .ptr = self.generated_llvm_bc, .emit_suffix = "llvm-bc" }, + .{ .ptr = self.generated_llvm_ir, .emit_suffix = "llvm-ir" }, + .{ .ptr = self.generated_h, .emit_suffix = "h" }, + }; + var any_emitted_file = false; + for (generated_files) |file| { + try zig_args.append(if (file.ptr != null) + b.fmt("-femit-{s}", .{file.emit_suffix}) + else + b.fmt("-fno-emit-{s}", .{file.emit_suffix})); + + if (file.ptr != null) any_emitted_file = true; + } + + if (!any_emitted_file) { + std.debug.getStderrMutex().lock(); + const stderr = std.io.getStdErr(); + build_util.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, null) catch {}; + std.debug.panic("Artifact '{s}' has no emit options set, but it is made. Did you forget to call `getEmitted*()`?.", .{self.name}); + } try addFlag(&zig_args, "strip", self.strip); try addFlag(&zig_args, "unwind-tables", self.unwind_tables); @@ -1755,10 +1825,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { try zig_args.append(common_include_path); }, .other_step => |other| { - if (other.emit_h) { - const h_path = other.getEmittedH().getPath(b); + if (other.generated_h) |header| { try zig_args.append("-isystem"); - try zig_args.append(fs.path.dirname(h_path).?); + try zig_args.append(fs.path.dirname(header.path.?).?); } if (other.installed_headers.items.len > 0) { try zig_args.append("-I"); @@ -1989,33 +2058,64 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { if (maybe_output_bin_path) |output_bin_path| { const output_dir = fs.path.dirname(output_bin_path).?; - self.output_dirname_source.path = output_dir; - - self.output_path_source.path = b.pathJoin( - &.{ output_dir, self.out_filename }, - ); + self.emit_directory.path = output_dir; - if (self.kind == .lib) { - self.output_lib_path_source.path = b.pathJoin( - &.{ output_dir, self.out_lib_filename }, + // -femit-bin[=path] (default) Output machine code + if (self.generated_bin) |bin| { + bin.path = b.pathJoin( + &.{ output_dir, self.out_filename }, ); } - if (self.emit_h) { - self.output_h_path_source.path = b.pathJoin( - &.{ output_dir, self.out_h_filename }, + // output PDB if someone requested it + if (self.generated_pdb) |pdb| { + std.debug.assert(self.producesPdbFile()); + pdb.path = b.pathJoin( + &.{ output_dir, self.out_pdb_filename }, ); } - if (self.target.isWindows() or self.target.isUefi()) { - self.output_pdb_path_source.path = b.pathJoin( - &.{ output_dir, self.out_pdb_filename }, + // -femit-implib[=path] (default) Produce an import .lib when building a Windows DLL + if (self.kind == .lib) { + if (self.generated_implib) |lib| { + lib.path = b.pathJoin( + &.{ output_dir, self.out_lib_filename }, + ); + } + } + + // -femit-h[=path] Generate a C header file (.h) + if (self.generated_h) |lazy_path| { + lazy_path.path = b.pathJoin( + &.{ output_dir, self.out_h_filename }, ); } + // -femit-docs[=path] Create a docs/ dir with html documentation if (self.generated_docs) |generated_docs| { generated_docs.path = b.pathJoin(&.{ output_dir, "docs" }); } + + // -femit-asm[=path] Output .s (assembly code) + if (self.generated_asm) |lazy_path| { + lazy_path.path = b.pathJoin( + &.{ output_dir, self.out_asm_filename }, + ); + } + + // -femit-llvm-ir[=path] Produce a .ll file with optimized LLVM IR (requires LLVM extensions) + if (self.generated_llvm_ir) |lazy_path| { + lazy_path.path = b.pathJoin( + &.{ output_dir, self.out_ll_filename }, + ); + } + + // -femit-llvm-bc[=path] Produce an optimized LLVM module as a .bc file (requires LLVM extensions) + if (self.generated_llvm_bc) |lazy_path| { + lazy_path.path = b.pathJoin( + &.{ output_dir, self.out_bc_filename }, + ); + } } if (self.kind == .lib and self.linkage != null and self.linkage.? == .dynamic and diff --git a/lib/std/Build/Step/InstallArtifact.zig b/lib/std/Build/Step/InstallArtifact.zig index ab01e03f98e5..7d65e3b59483 100644 --- a/lib/std/Build/Step/InstallArtifact.zig +++ b/lib/std/Build/Step/InstallArtifact.zig @@ -37,11 +37,12 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile) *InstallArtifact { break :blk InstallDir{ .lib = {} }; } } else null, - .h_dir = if (artifact.kind == .lib and artifact.emit_h) .header else null, + .h_dir = if (artifact.kind == .lib and artifact.generated_h != null) .header else null, .dest_sub_path = null, }; self.step.dependOn(&artifact.step); + _ = artifact.getEmittedBin(); // force creation owner.pushInstalledFile(self.dest_dir, artifact.out_filename); if (self.artifact.isDynamicLibrary()) { if (artifact.major_only_filename) |name| { @@ -55,9 +56,11 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile) *InstallArtifact { } } if (self.pdb_dir) |pdb_dir| { + _ = artifact.getEmittedPdb(); // force creation owner.pushInstalledFile(pdb_dir, artifact.out_pdb_filename); } if (self.h_dir) |h_dir| { + _ = artifact.getEmittedH(); // force creation owner.pushInstalledFile(h_dir, artifact.out_h_filename); } return self; @@ -66,7 +69,6 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile) *InstallArtifact { fn make(step: *Step, prog_node: *std.Progress.Node) !void { _ = prog_node; const self = @fieldParentPtr(InstallArtifact, "step", step); - const src_builder = self.artifact.step.owner; const dest_builder = step.owner; const dest_sub_path = if (self.dest_sub_path) |sub_path| sub_path else self.artifact.out_filename; @@ -76,7 +78,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { var all_cached = true; { - const full_src_path = self.artifact.getEmittedBin().getPath(src_builder); + const full_src_path = self.artifact.generated_bin.?.path.?; const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_dest_path, .{}) catch |err| { return step.fail("unable to update file from '{s}' to '{s}': {s}", .{ full_src_path, full_dest_path, @errorName(err), @@ -93,9 +95,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } if (self.artifact.isDynamicLibrary() and self.artifact.target.isWindows() and - self.artifact.emit_implib != .no_emit) + self.artifact.generated_implib != null) { - const full_src_path = self.artifact.getEmittedImplib().getPath(src_builder); + const full_src_path = self.artifact.generated_implib.?.path.?; const full_implib_path = dest_builder.getInstallPath(self.dest_dir, self.artifact.out_lib_filename); const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_implib_path, .{}) catch |err| { return step.fail("unable to update file from '{s}' to '{s}': {s}", .{ @@ -105,7 +107,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { all_cached = all_cached and p == .fresh; } if (self.pdb_dir) |pdb_dir| { - const full_src_path = self.artifact.getEmittedPdb().getPath(src_builder); + const full_src_path = self.artifact.generated_pdb.?.path.?; const full_pdb_path = dest_builder.getInstallPath(pdb_dir, self.artifact.out_pdb_filename); const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_pdb_path, .{}) catch |err| { return step.fail("unable to update file from '{s}' to '{s}': {s}", .{ @@ -115,7 +117,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { all_cached = all_cached and p == .fresh; } if (self.h_dir) |h_dir| { - const full_src_path = self.artifact.getEmittedH().getPath(src_builder); + const full_src_path = self.artifact.generated_h.?.path.?; const full_h_path = dest_builder.getInstallPath(h_dir, self.artifact.out_h_filename); const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_h_path, .{}) catch |err| { return step.fail("unable to update file from '{s}' to '{s}': {s}", .{ diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index 5b1bca25b4e4..55278e2d5b53 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -164,6 +164,10 @@ pub fn enableTestRunnerMode(self: *Run) void { } pub fn addArtifactArg(self: *Run, artifact: *Step.Compile) void { + // enforce creation of the binary file by invoking getEmittedBin + const bin_file = artifact.getEmittedBin(); + bin_file.addStepDependencies(&self.step); + self.argv.append(Arg{ .artifact = artifact }) catch @panic("OOM"); self.step.dependOn(&artifact.step); } @@ -456,8 +460,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { // On Windows we don't have rpaths so we have to add .dll search paths to PATH self.addPathForDynLibs(artifact); } - const file_path = artifact.installed_path orelse - artifact.getEmittedBin().getPath(b); + const file_path = artifact.installed_path orelse artifact.generated_bin.?.path.?; // the path is guaranteed to be set try argv_list.append(file_path); diff --git a/lib/std/Build/util.zig b/lib/std/Build/util.zig new file mode 100644 index 000000000000..5c1b91f3d646 --- /dev/null +++ b/lib/std/Build/util.zig @@ -0,0 +1,53 @@ +const std = @import("std"); +const fs = std.fs; + +const Build = std.Build; +const Step = std.Build.Step; + +/// In this function the stderr mutex has already been locked. +pub fn dumpBadGetPathHelp( + s: *Step, + stderr: fs.File, + src_builder: *Build, + asking_step: ?*Step, +) anyerror!void { + const w = stderr.writer(); + try w.print( + \\getPath() was called on a GeneratedFile that wasn't built yet. + \\ source package path: {s} + \\ Is there a missing Step dependency on step '{s}'? + \\ + , .{ + src_builder.build_root.path orelse ".", + s.name, + }); + + const tty_config = std.io.tty.detectConfig(stderr); + tty_config.setColor(w, .red) catch {}; + try stderr.writeAll(" The step was created by this stack trace:\n"); + tty_config.setColor(w, .reset) catch {}; + + const debug_info = std.debug.getSelfDebugInfo() catch |err| { + try w.print("Unable to dump stack trace: Unable to open debug info: {s}\n", .{@errorName(err)}); + return; + }; + const ally = debug_info.allocator; + std.debug.writeStackTrace(s.getStackTrace(), w, ally, debug_info, tty_config) catch |err| { + try stderr.writer().print("Unable to dump stack trace: {s}\n", .{@errorName(err)}); + return; + }; + if (asking_step) |as| { + tty_config.setColor(w, .red) catch {}; + try stderr.writeAll(" The step that is missing a dependency on the above step was created by this stack trace:\n"); + tty_config.setColor(w, .reset) catch {}; + + std.debug.writeStackTrace(as.getStackTrace(), w, ally, debug_info, tty_config) catch |err| { + try stderr.writer().print("Unable to dump stack trace: {s}\n", .{@errorName(err)}); + return; + }; + } + + tty_config.setColor(w, .red) catch {}; + try stderr.writeAll(" Hope that helps. Proceeding to panic.\n"); + tty_config.setColor(w, .reset) catch {}; +} diff --git a/test/link/glibc_compat/build.zig b/test/link/glibc_compat/build.zig index 8d850cf350cc..dfef8c49239d 100644 --- a/test/link/glibc_compat/build.zig +++ b/test/link/glibc_compat/build.zig @@ -13,6 +13,7 @@ pub fn build(b: *std.Build) void { ) catch unreachable, }); exe.linkLibC(); + _ = exe.getEmittedBin(); // force emission test_step.dependOn(&exe.step); } } diff --git a/test/link/macho/needed_library/build.zig b/test/link/macho/needed_library/build.zig index 1c2cd9b45215..cdc78d860095 100644 --- a/test/link/macho/needed_library/build.zig +++ b/test/link/macho/needed_library/build.zig @@ -23,6 +23,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize }); dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); dylib.linkLibC(); + _ = dylib.getEmittedBin(); // enforce emission // -dead_strip_dylibs // -needed-la diff --git a/test/link/macho/search_strategy/build.zig b/test/link/macho/search_strategy/build.zig index 3ecefc306bb0..150e4e0436d8 100644 --- a/test/link/macho/search_strategy/build.zig +++ b/test/link/macho/search_strategy/build.zig @@ -60,6 +60,7 @@ fn createScenario( static.override_dest_dir = std.Build.InstallDir{ .custom = "static", }; + _ = static.getEmittedBin(); // enforce emission const dylib = b.addSharedLibrary(.{ .name = name, @@ -72,6 +73,7 @@ fn createScenario( dylib.override_dest_dir = std.Build.InstallDir{ .custom = "dynamic", }; + _ = dylib.getEmittedBin(); // enforce emission const exe = b.addExecutable(.{ .name = name, diff --git a/test/src/Cases.zig b/test/src/Cases.zig index 694c2800872b..112b4f6dfb0a 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -551,7 +551,10 @@ pub fn lowerToBuildSteps( }), }; - artifact.emit_bin = if (case.emit_bin) .default else .no_emit; + if (case.emit_bin) + _ = artifact.getEmittedBin(); + + _ = artifact.getEmittedBin(); // TODO(xq): The test cases break if we set all to -fno-emit-X if (case.link_libc) artifact.linkLibC(); diff --git a/test/standalone.zig b/test/standalone.zig index 3725456fa1da..78a694d894cf 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -140,16 +140,14 @@ pub const build_cases = [_]BuildCase{ .build_root = "test/standalone/install_raw_hex", .import = @import("standalone/install_raw_hex/build.zig"), }, - // TODO take away EmitOption.emit_to option and make it give a FileSource - //.{ - // .build_root = "test/standalone/emit_asm_and_bin", - // .import = @import("standalone/emit_asm_and_bin/build.zig"), - //}, - // TODO take away EmitOption.emit_to option and make it give a FileSource - //.{ - // .build_root = "test/standalone/issue_12588", - // .import = @import("standalone/issue_12588/build.zig"), - //}, + .{ + .build_root = "test/standalone/emit_asm_and_bin", + .import = @import("standalone/emit_asm_and_bin/build.zig"), + }, + .{ + .build_root = "test/standalone/issue_12588", + .import = @import("standalone/issue_12588/build.zig"), + }, .{ .build_root = "test/standalone/child_process", .import = @import("standalone/child_process/build.zig"), diff --git a/test/standalone/embed_generated_file/build.zig b/test/standalone/embed_generated_file/build.zig index b914b9ffab3f..7e8c4a54dfdb 100644 --- a/test/standalone/embed_generated_file/build.zig +++ b/test/standalone/embed_generated_file/build.zig @@ -22,5 +22,7 @@ pub fn build(b: *std.Build) void { .source_file = bootloader.getEmittedBin(), }); + _ = exe.getEmittedBin(); // enforce emission + test_step.dependOn(&exe.step); } diff --git a/test/standalone/emit_asm_and_bin/build.zig b/test/standalone/emit_asm_and_bin/build.zig index 1a608b40a925..edc4a12fffd0 100644 --- a/test/standalone/emit_asm_and_bin/build.zig +++ b/test/standalone/emit_asm_and_bin/build.zig @@ -8,8 +8,8 @@ pub fn build(b: *std.Build) void { .root_source_file = .{ .path = "main.zig" }, .optimize = b.standardOptimizeOption(.{}), }); - main.emit_asm = .{ .emit_to = b.pathFromRoot("main.s") }; - main.emit_bin = .{ .emit_to = b.pathFromRoot("main") }; + _ = main.getEmittedBin(); // main.emit_asm = .{ .emit_to = b.pathFromRoot("main.s") }; + _ = main.getEmittedAsm(); // main.emit_bin = .{ .emit_to = b.pathFromRoot("main") }; test_step.dependOn(&b.addRunArtifact(main).step); } diff --git a/test/standalone/issue_12588/build.zig b/test/standalone/issue_12588/build.zig index fa22252fcc83..e6d01192d3cf 100644 --- a/test/standalone/issue_12588/build.zig +++ b/test/standalone/issue_12588/build.zig @@ -13,9 +13,8 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .target = target, }); - obj.emit_llvm_ir = .{ .emit_to = b.pathFromRoot("main.ll") }; - obj.emit_llvm_bc = .{ .emit_to = b.pathFromRoot("main.bc") }; - obj.emit_bin = .no_emit; + _ = obj.getEmittedLlvmIr(); + _ = obj.getEmittedLlvmBc(); b.default_step.dependOn(&obj.step); test_step.dependOn(&obj.step); diff --git a/test/standalone/issue_339/build.zig b/test/standalone/issue_339/build.zig index f4215dbb8bc2..0d2b4954d7da 100644 --- a/test/standalone/issue_339/build.zig +++ b/test/standalone/issue_339/build.zig @@ -14,5 +14,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); + _ = obj.getEmittedBin(); // enforce emission + test_step.dependOn(&obj.step); } diff --git a/test/standalone/issue_5825/build.zig b/test/standalone/issue_5825/build.zig index e8e8d4877230..9c9c913f83a5 100644 --- a/test/standalone/issue_5825/build.zig +++ b/test/standalone/issue_5825/build.zig @@ -27,5 +27,7 @@ pub fn build(b: *std.Build) void { exe.linkSystemLibrary("ntdll"); exe.addObject(obj); + _ = exe.getEmittedBin(); // enforce emission + test_step.dependOn(&exe.step); } diff --git a/test/standalone/issue_794/build.zig b/test/standalone/issue_794/build.zig index 1a319798cb65..75ec033d2102 100644 --- a/test/standalone/issue_794/build.zig +++ b/test/standalone/issue_794/build.zig @@ -8,6 +8,7 @@ pub fn build(b: *std.Build) void { .root_source_file = .{ .path = "main.zig" }, }); test_artifact.addIncludePath(.{ .path = "a_directory" }); + _ = test_artifact.getEmittedBin(); // enforce emission test_step.dependOn(&test_artifact.step); } diff --git a/test/standalone/main_pkg_path/build.zig b/test/standalone/main_pkg_path/build.zig index 9f7f67255454..bbe7cfb53b52 100644 --- a/test/standalone/main_pkg_path/build.zig +++ b/test/standalone/main_pkg_path/build.zig @@ -7,7 +7,7 @@ pub fn build(b: *std.Build) void { const test_exe = b.addTest(.{ .root_source_file = .{ .path = "a/test.zig" }, }); - test_exe.setMainPkgPath(.{.path="."}); + test_exe.setMainPkgPath(.{ .path = "." }); test_step.dependOn(&b.addRunArtifact(test_exe).step); } diff --git a/test/standalone/strip_empty_loop/build.zig b/test/standalone/strip_empty_loop/build.zig index 73c70d661627..b6752029ce81 100644 --- a/test/standalone/strip_empty_loop/build.zig +++ b/test/standalone/strip_empty_loop/build.zig @@ -14,5 +14,8 @@ pub fn build(b: *std.Build) void { .target = target, }); main.strip = true; + + _ = main.getEmittedBin(); // enforce emission + test_step.dependOn(&main.step); } diff --git a/test/standalone/use_alias/build.zig b/test/standalone/use_alias/build.zig index b20c20d5066f..0511cd3935fc 100644 --- a/test/standalone/use_alias/build.zig +++ b/test/standalone/use_alias/build.zig @@ -10,7 +10,7 @@ pub fn build(b: *std.Build) void { .root_source_file = .{ .path = "main.zig" }, .optimize = optimize, }); - main.addIncludePath(.{.path="."}); + main.addIncludePath(.{ .path = "." }); test_step.dependOn(&b.addRunArtifact(main).step); } diff --git a/test/tests.zig b/test/tests.zig index f8e1064fc800..0b317c5897ff 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -588,6 +588,8 @@ pub fn addStandaloneTests( }); if (case.link_libc) exe.linkLibC(); + _ = exe.getEmittedBin(); // Force emission + step.dependOn(&exe.step); } From 35d0a49db911e9c459902a021a5bfa05c97ca107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20=22xq=22=20Quei=C3=9Fner?= Date: Wed, 26 Jul 2023 16:32:50 +0200 Subject: [PATCH 03/10] =?UTF-8?q?Introduces=20Compile.forceBuild()=20and?= =?UTF-8?q?=20Compile.forceEmit(=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/std/Build/Step/Compile.zig | 41 +++++++++++++++++-- lib/std/Build/Step/InstallArtifact.zig | 3 +- test/link/glibc_compat/build.zig | 2 +- test/link/macho/needed_library/build.zig | 2 +- test/link/macho/search_strategy/build.zig | 4 +- test/src/Cases.zig | 9 ++-- test/standalone.zig | 18 ++++---- test/standalone/coff_dwarf/build.zig | 2 +- .../standalone/embed_generated_file/build.zig | 2 +- test/standalone/emit_asm_and_bin/build.zig | 4 +- test/standalone/issue_339/build.zig | 2 +- test/standalone/issue_5825/build.zig | 2 +- test/standalone/issue_794/build.zig | 3 +- test/standalone/strip_empty_loop/build.zig | 2 +- test/tests.zig | 2 +- 15 files changed, 69 insertions(+), 29 deletions(-) diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index d6eae499d262..6db15a1cb37e 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -207,6 +207,8 @@ use_lld: ?bool, /// otherwise. expect_errors: []const []const u8 = &.{}, +force_build: bool, + emit_directory: GeneratedFile, generated_docs: ?*GeneratedFile, @@ -374,6 +376,17 @@ pub const Kind = enum { pub const Linkage = enum { dynamic, static }; +pub const EmitOption = enum { + docs, + @"asm", + bin, + pdb, + implib, + llvm_bc, + llvm_ir, + h, +}; + pub fn create(owner: *std.Build, options: Options) *Compile { const name = owner.dupe(options.name); const root_src: ?LazyPath = if (options.root_source_file) |rsrc| rsrc.dupe(owner) else null; @@ -468,6 +481,8 @@ pub fn create(owner: *std.Build, options: Options) *Compile { .installed_path = null, .force_undefined_symbols = StringHashMap(void).init(owner.allocator), + .force_build = false, + .emit_directory = GeneratedFile{ .step = &self.step }, .generated_docs = null, @@ -972,6 +987,26 @@ fn getEmittedFileGeneric(self: *Compile, output_file: *?*GeneratedFile) LazyPath return .{ .generated = generated_file }; } +/// Disables the panic in the build evaluation if nothing is emitted. +/// +/// Unless for compilation tests this is a code smell. +pub fn forceBuild(self: *Compile) void { + self.force_build = true; +} + +pub fn forceEmit(self: *Compile, emit: EmitOption) void { + switch (emit) { + .docs => _ = self.getEmittedDocs(), + .@"asm" => _ = self.getEmittedAsm(), + .bin => _ = self.getEmittedBin(), + .pdb => _ = self.getEmittedPdb(), + .implib => _ = self.getEmittedImplib(), + .llvm_bc => _ = self.getEmittedLlvmBc(), + .llvm_ir => _ = self.getEmittedLlvmIr(), + .h => _ = self.getEmittedH(), + } +} + pub const getOutputDirectorySource = getEmitDirectory; // DEPRECATED, use getEmitDirectory /// Returns the path to the output directory. @@ -1163,7 +1198,7 @@ pub fn setExecCmd(self: *Compile, args: []const ?[]const u8) void { } fn linkLibraryOrObject(self: *Compile, other: *Compile) void { - _ = other.getEmittedBin(); // Force emission of the binary + other.forceEmit(.bin); if (other.kind == .lib and other.target.isWindows()) { // TODO(xq): Is this the correct logic here? _ = other.getEmittedImplib(); // Force emission of the binary @@ -1568,11 +1603,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { if (file.ptr != null) any_emitted_file = true; } - if (!any_emitted_file) { + if (!any_emitted_file and !self.force_build) { std.debug.getStderrMutex().lock(); const stderr = std.io.getStdErr(); build_util.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, null) catch {}; - std.debug.panic("Artifact '{s}' has no emit options set, but it is made. Did you forget to call `getEmitted*()`?.", .{self.name}); + std.debug.panic("Artifact '{s}' has no emit options set, but it is made. Did you forget to call `.getEmitted*()`? If not, use `.forceBuild()` or `.forceEmit(…)` to make sure it builds anyways.", .{self.name}); } try addFlag(&zig_args, "strip", self.strip); diff --git a/lib/std/Build/Step/InstallArtifact.zig b/lib/std/Build/Step/InstallArtifact.zig index 7d65e3b59483..a9696867eff3 100644 --- a/lib/std/Build/Step/InstallArtifact.zig +++ b/lib/std/Build/Step/InstallArtifact.zig @@ -42,7 +42,8 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile) *InstallArtifact { }; self.step.dependOn(&artifact.step); - _ = artifact.getEmittedBin(); // force creation + artifact.forceEmit(.bin); + owner.pushInstalledFile(self.dest_dir, artifact.out_filename); if (self.artifact.isDynamicLibrary()) { if (artifact.major_only_filename) |name| { diff --git a/test/link/glibc_compat/build.zig b/test/link/glibc_compat/build.zig index dfef8c49239d..8f80a37b0258 100644 --- a/test/link/glibc_compat/build.zig +++ b/test/link/glibc_compat/build.zig @@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void { ) catch unreachable, }); exe.linkLibC(); - _ = exe.getEmittedBin(); // force emission + exe.forceBuild(); test_step.dependOn(&exe.step); } } diff --git a/test/link/macho/needed_library/build.zig b/test/link/macho/needed_library/build.zig index cdc78d860095..c24569e06d61 100644 --- a/test/link/macho/needed_library/build.zig +++ b/test/link/macho/needed_library/build.zig @@ -23,7 +23,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize }); dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); dylib.linkLibC(); - _ = dylib.getEmittedBin(); // enforce emission + dylib.forceEmit(.bin); // enforce library creation, we import it below // -dead_strip_dylibs // -needed-la diff --git a/test/link/macho/search_strategy/build.zig b/test/link/macho/search_strategy/build.zig index 150e4e0436d8..d802bddbf41a 100644 --- a/test/link/macho/search_strategy/build.zig +++ b/test/link/macho/search_strategy/build.zig @@ -60,7 +60,7 @@ fn createScenario( static.override_dest_dir = std.Build.InstallDir{ .custom = "static", }; - _ = static.getEmittedBin(); // enforce emission + static.forceEmit(.bin); const dylib = b.addSharedLibrary(.{ .name = name, @@ -73,7 +73,7 @@ fn createScenario( dylib.override_dest_dir = std.Build.InstallDir{ .custom = "dynamic", }; - _ = dylib.getEmittedBin(); // enforce emission + dylib.forceEmit(.bin); // we want the binary to be built as we use it further below const exe = b.addExecutable(.{ .name = name, diff --git a/test/src/Cases.zig b/test/src/Cases.zig index 112b4f6dfb0a..1cb9b0fcdcff 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -551,10 +551,11 @@ pub fn lowerToBuildSteps( }), }; - if (case.emit_bin) - _ = artifact.getEmittedBin(); - - _ = artifact.getEmittedBin(); // TODO(xq): The test cases break if we set all to -fno-emit-X + if (case.emit_bin) { + artifact.forceEmit(.bin); + } else { + artifact.forceBuild(); + } if (case.link_libc) artifact.linkLibC(); diff --git a/test/standalone.zig b/test/standalone.zig index 78a694d894cf..dc9d7e291872 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -140,14 +140,16 @@ pub const build_cases = [_]BuildCase{ .build_root = "test/standalone/install_raw_hex", .import = @import("standalone/install_raw_hex/build.zig"), }, - .{ - .build_root = "test/standalone/emit_asm_and_bin", - .import = @import("standalone/emit_asm_and_bin/build.zig"), - }, - .{ - .build_root = "test/standalone/issue_12588", - .import = @import("standalone/issue_12588/build.zig"), - }, + // TODO take away EmitOption.emit_to option and make it give a FileSource + // .{ + // .build_root = "test/standalone/emit_asm_and_bin", + // .import = @import("standalone/emit_asm_and_bin/build.zig"), + // }, + // TODO take away EmitOption.emit_to option and make it give a FileSource + // .{ + // .build_root = "test/standalone/issue_12588", + // .import = @import("standalone/issue_12588/build.zig"), + // }, .{ .build_root = "test/standalone/child_process", .import = @import("standalone/child_process/build.zig"), diff --git a/test/standalone/coff_dwarf/build.zig b/test/standalone/coff_dwarf/build.zig index ffd7800a5b6c..d4a2f2b65bf0 100644 --- a/test/standalone/coff_dwarf/build.zig +++ b/test/standalone/coff_dwarf/build.zig @@ -23,7 +23,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .target = target, }); - lib.addCSourceFile("shared_lib.c", &.{"-gdwarf"}); + lib.addCSourceFile(.{ .file = "shared_lib.c", .flags = &.{"-gdwarf"} }); lib.linkLibC(); exe.linkLibrary(lib); diff --git a/test/standalone/embed_generated_file/build.zig b/test/standalone/embed_generated_file/build.zig index 7e8c4a54dfdb..220220b15f5f 100644 --- a/test/standalone/embed_generated_file/build.zig +++ b/test/standalone/embed_generated_file/build.zig @@ -22,7 +22,7 @@ pub fn build(b: *std.Build) void { .source_file = bootloader.getEmittedBin(), }); - _ = exe.getEmittedBin(); // enforce emission + exe.forceBuild(); test_step.dependOn(&exe.step); } diff --git a/test/standalone/emit_asm_and_bin/build.zig b/test/standalone/emit_asm_and_bin/build.zig index edc4a12fffd0..57df1185eebd 100644 --- a/test/standalone/emit_asm_and_bin/build.zig +++ b/test/standalone/emit_asm_and_bin/build.zig @@ -8,8 +8,8 @@ pub fn build(b: *std.Build) void { .root_source_file = .{ .path = "main.zig" }, .optimize = b.standardOptimizeOption(.{}), }); - _ = main.getEmittedBin(); // main.emit_asm = .{ .emit_to = b.pathFromRoot("main.s") }; - _ = main.getEmittedAsm(); // main.emit_bin = .{ .emit_to = b.pathFromRoot("main") }; + main.forceEmit(.bin); + main.forceEmit(.@"asm"); test_step.dependOn(&b.addRunArtifact(main).step); } diff --git a/test/standalone/issue_339/build.zig b/test/standalone/issue_339/build.zig index 0d2b4954d7da..6afb0125ea0d 100644 --- a/test/standalone/issue_339/build.zig +++ b/test/standalone/issue_339/build.zig @@ -14,7 +14,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); - _ = obj.getEmittedBin(); // enforce emission + obj.forceBuild(); test_step.dependOn(&obj.step); } diff --git a/test/standalone/issue_5825/build.zig b/test/standalone/issue_5825/build.zig index 9c9c913f83a5..48c43242b26b 100644 --- a/test/standalone/issue_5825/build.zig +++ b/test/standalone/issue_5825/build.zig @@ -27,7 +27,7 @@ pub fn build(b: *std.Build) void { exe.linkSystemLibrary("ntdll"); exe.addObject(obj); - _ = exe.getEmittedBin(); // enforce emission + exe.forceBuild(); test_step.dependOn(&exe.step); } diff --git a/test/standalone/issue_794/build.zig b/test/standalone/issue_794/build.zig index 75ec033d2102..8ef5cf4e9f3c 100644 --- a/test/standalone/issue_794/build.zig +++ b/test/standalone/issue_794/build.zig @@ -8,7 +8,8 @@ pub fn build(b: *std.Build) void { .root_source_file = .{ .path = "main.zig" }, }); test_artifact.addIncludePath(.{ .path = "a_directory" }); - _ = test_artifact.getEmittedBin(); // enforce emission + + test_artifact.forceBuild(); test_step.dependOn(&test_artifact.step); } diff --git a/test/standalone/strip_empty_loop/build.zig b/test/standalone/strip_empty_loop/build.zig index b6752029ce81..77a1a08f754a 100644 --- a/test/standalone/strip_empty_loop/build.zig +++ b/test/standalone/strip_empty_loop/build.zig @@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void { }); main.strip = true; - _ = main.getEmittedBin(); // enforce emission + main.forceBuild(); test_step.dependOn(&main.step); } diff --git a/test/tests.zig b/test/tests.zig index 0b317c5897ff..4174fc0147c8 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -588,7 +588,7 @@ pub fn addStandaloneTests( }); if (case.link_libc) exe.linkLibC(); - _ = exe.getEmittedBin(); // Force emission + exe.forceBuild(); step.dependOn(&exe.step); } From ae11fba44a61f4ccb81875633a4203116167509c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20=22xq=22=20Quei=C3=9Fner?= Date: Thu, 27 Jul 2023 01:23:41 +0200 Subject: [PATCH 04/10] Rebase + two fixes. --- test/link/macho/tbdv3/build.zig | 9 +++++---- test/standalone/coff_dwarf/build.zig | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/test/link/macho/tbdv3/build.zig b/test/link/macho/tbdv3/build.zig index c2eb6df626cc..3b27b5a0ca5c 100644 --- a/test/link/macho/tbdv3/build.zig +++ b/test/link/macho/tbdv3/build.zig @@ -23,8 +23,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = target, }); - lib.addCSourceFile("a.c", &.{}); + lib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); lib.linkLibC(); + lib.forceEmit(.bin); // will be referenced by the tbd file const tbd_file = b.addWriteFile("liba.tbd", \\--- !tapi-tbd-v3 @@ -43,10 +44,10 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} }); exe.linkSystemLibrary("a"); - exe.addLibraryPathDirectorySource(tbd_file.getDirectorySource()); - exe.addRPathDirectorySource(lib.getOutputDirectorySource()); + exe.addLibraryPath(tbd_file.getDirectory()); + exe.addRPath(lib.getEmitDirectory()); exe.linkLibC(); const run = b.addRunArtifact(exe); diff --git a/test/standalone/coff_dwarf/build.zig b/test/standalone/coff_dwarf/build.zig index d4a2f2b65bf0..b85b44b686cb 100644 --- a/test/standalone/coff_dwarf/build.zig +++ b/test/standalone/coff_dwarf/build.zig @@ -23,7 +23,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .target = target, }); - lib.addCSourceFile(.{ .file = "shared_lib.c", .flags = &.{"-gdwarf"} }); + lib.addCSourceFile(.{ .file = .{ .path = "shared_lib.c" }, .flags = &.{"-gdwarf"} }); lib.linkLibC(); exe.linkLibrary(lib); From f8386de7ae12fca1e50e7920964fd7c47301fc05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20=22xq=22=20Quei=C3=9Fner?= Date: Thu, 27 Jul 2023 08:37:07 +0200 Subject: [PATCH 05/10] Tries to fix Windows DLL linking. --- lib/std/Build/Step/Compile.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 6db15a1cb37e..9898fe6df5bb 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -1200,8 +1200,8 @@ pub fn setExecCmd(self: *Compile, args: []const ?[]const u8) void { fn linkLibraryOrObject(self: *Compile, other: *Compile) void { other.forceEmit(.bin); - if (other.kind == .lib and other.target.isWindows()) { // TODO(xq): Is this the correct logic here? - _ = other.getEmittedImplib(); // Force emission of the binary + if (other.target.isWindows() and other.isDynamicLibrary()) { // TODO(xq): Is this the correct logic here? + other.forceEmit(.implib); } self.step.dependOn(&other.step); @@ -1434,9 +1434,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { // TODO(xq): Is that the right way? const full_path_lib = if (other.isDynamicLibrary() and other.target.isWindows()) - other.getGeneratedFilePath("generated_implib", &self.step) + other.getGeneratedFilePath("generated_implib", &self.step) // For DLLs, we gotta link against the implib, else - other.getGeneratedFilePath("generated_bin", &self.step); + other.getGeneratedFilePath("generated_bin", &self.step); // for everything else, we directly link against the library file try zig_args.append(full_path_lib); if (other.linkage == Linkage.dynamic and !self.target.isWindows()) { From 38840e2e586d71f2b38ed825ea02529f615c5f0c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 29 Jul 2023 23:57:52 -0700 Subject: [PATCH 06/10] build system: follow-up enhancements regarding LazyPath * introduce LazyPath.cwd_relative variant and use it for --zig-lib-dir. closes #12685 * move overrideZigLibDir and setMainPkgPath to options fields set once and then never mutated. * avoid introducing Build/util.zig * use doc comments for deprecation notices so that they show up in generated documentation. * introduce InstallArtifact.Options, accept it as a parameter to addInstallArtifact, and move override_dest_dir into it. Instead of configuring the installation via Compile step, configure the installation via the InstallArtifact step. In retrospect this is obvious. * remove calls to pushInstalledFile in InstallArtifact. See #14943 * rewrite InstallArtifact to not incorrectly observe whether a Compile step has any generated outputs. InstallArtifact is meant to trigger output generation. * fix child process evaluation code handling of `-fno-emit-bin`. * don't store out_h_filename, out_ll_filename, etc., pointlessly. these are all just simple extensions appended to the root name. * make emit_directory optional. It's possible to have nothing outputted, for example, if you're just type-checking. * avoid passing -femit-foo/-fno-emit-foo when it is the default * rename ConfigHeader.getTemplate to getOutput * deprecate addOptionArtifact * update the random number seed of Options step caching. * avoid using `inline for` pointlessly * avoid using `override_Dest_dir` pointlessly * avoid emitting an executable pointlessly in test cases Removes forceBuild and forceEmit. Let's consider these additions separately. Nearly all of the usage sites were suspicious. --- build.zig | 27 ++- lib/build_runner.zig | 4 +- lib/std/Build.zig | 132 ++++++++-- lib/std/Build/Step.zig | 10 +- lib/std/Build/Step/Compile.zig | 229 ++++++------------ lib/std/Build/Step/ConfigHeader.zig | 8 +- lib/std/Build/Step/InstallArtifact.zig | 177 +++++++++----- lib/std/Build/Step/ObjCopy.zig | 3 +- lib/std/Build/Step/Options.zig | 23 +- lib/std/Build/Step/Run.zig | 25 +- lib/std/Build/Step/WriteFile.zig | 3 +- lib/std/Build/util.zig | 53 ---- test/link/glibc_compat/build.zig | 5 +- test/link/macho/dylib/build.zig | 4 +- test/link/macho/needed_library/build.zig | 5 +- test/link/macho/search_strategy/build.zig | 14 +- test/link/macho/tbdv3/build.zig | 3 +- test/link/macho/weak_library/build.zig | 4 +- test/src/Cases.zig | 6 - test/standalone.zig | 16 +- test/standalone/compiler_rt_panic/build.zig | 5 +- .../standalone/embed_generated_file/build.zig | 3 +- test/standalone/emit_asm_and_bin/build.zig | 5 +- test/standalone/issue_339/build.zig | 3 +- test/standalone/issue_5825/build.zig | 3 +- test/standalone/issue_794/build.zig | 3 +- test/standalone/main_pkg_path/build.zig | 2 +- test/standalone/strip_empty_loop/build.zig | 3 +- test/tests.zig | 6 +- 29 files changed, 396 insertions(+), 388 deletions(-) delete mode 100644 lib/std/Build/util.zig diff --git a/build.zig b/build.zig index 07130e84b051..a7d74bdc06ae 100644 --- a/build.zig +++ b/build.zig @@ -45,7 +45,8 @@ pub fn build(b: *std.Build) !void { const docgen_cmd = b.addRunArtifact(docgen_exe); docgen_cmd.addArgs(&.{ "--zig", b.zig_exe }); if (b.zig_lib_dir) |p| { - docgen_cmd.addArgs(&.{ "--zig-lib-dir", b.pathFromRoot(p) }); + docgen_cmd.addArg("--zig-lib-dir"); + docgen_cmd.addFileArg(p); } docgen_cmd.addFileArg(.{ .path = "doc/langref.html.in" }); const langref_file = docgen_cmd.addOutputFileArg("langref.html"); @@ -57,8 +58,8 @@ pub fn build(b: *std.Build) !void { const autodoc_test = b.addTest(.{ .root_source_file = .{ .path = "lib/std/std.zig" }, .target = target, + .zig_lib_dir = .{ .path = "lib" }, }); - autodoc_test.overrideZigLibDir(.{ .path = "lib" }); const install_std_docs = b.addInstallDirectory(.{ .source_dir = autodoc_test.getEmittedDocs(), .install_dir = .prefix, @@ -87,8 +88,8 @@ pub fn build(b: *std.Build) !void { .name = "check-case", .root_source_file = .{ .path = "test/src/Cases.zig" }, .optimize = optimize, + .main_pkg_path = .{ .path = "." }, }); - check_case_exe.setMainPkgPath(.{ .path = "." }); check_case_exe.stack_size = stack_size; check_case_exe.single_threaded = single_threaded; @@ -203,7 +204,7 @@ pub fn build(b: *std.Build) !void { ); if (!no_bin) { - const install_exe = b.addInstallArtifact(exe); + const install_exe = b.addInstallArtifact(exe, .{}); if (flat) { install_exe.dest_dir = .prefix; } @@ -357,8 +358,8 @@ pub fn build(b: *std.Build) !void { else &[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" }; - exe.addIncludePath(.{ .path = tracy_path }); - exe.addCSourceFile(.{ .file = .{ .path = client_cpp }, .flags = tracy_c_flags }); + exe.addIncludePath(.{ .cwd_relative = tracy_path }); + exe.addCSourceFile(.{ .file = .{ .cwd_relative = client_cpp }, .flags = tracy_c_flags }); if (!enable_llvm) { exe.linkSystemLibraryName("c++"); } @@ -597,7 +598,7 @@ fn addCmakeCfgOptionsToExe( // useful for package maintainers exe.headerpad_max_install_names = true; } - exe.addObjectFile(.{ .path = b.pathJoin(&[_][]const u8{ + exe.addObjectFile(.{ .cwd_relative = b.pathJoin(&[_][]const u8{ cfg.cmake_binary_dir, "zigcpp", b.fmt("{s}{s}{s}", .{ @@ -607,9 +608,9 @@ fn addCmakeCfgOptionsToExe( }), }) }); assert(cfg.lld_include_dir.len != 0); - exe.addIncludePath(.{ .path = cfg.lld_include_dir }); - exe.addIncludePath(.{ .path = cfg.llvm_include_dir }); - exe.addLibraryPath(.{ .path = cfg.llvm_lib_dir }); + exe.addIncludePath(.{ .cwd_relative = cfg.lld_include_dir }); + exe.addIncludePath(.{ .cwd_relative = cfg.llvm_include_dir }); + exe.addLibraryPath(.{ .cwd_relative = cfg.llvm_lib_dir }); addCMakeLibraryList(exe, cfg.clang_libraries); addCMakeLibraryList(exe, cfg.lld_libraries); addCMakeLibraryList(exe, cfg.llvm_libraries); @@ -665,7 +666,7 @@ fn addCmakeCfgOptionsToExe( } if (cfg.dia_guids_lib.len != 0) { - exe.addObjectFile(.{ .path = cfg.dia_guids_lib }); + exe.addObjectFile(.{ .cwd_relative = cfg.dia_guids_lib }); } } @@ -726,7 +727,7 @@ fn addCxxKnownPath( } return error.RequiredLibraryNotFound; } - exe.addObjectFile(.{ .path = path_unpadded }); + exe.addObjectFile(.{ .cwd_relative = path_unpadded }); // TODO a way to integrate with system c++ include files here // c++ -E -Wp,-v -xc++ /dev/null @@ -746,7 +747,7 @@ fn addCMakeLibraryList(exe: *std.Build.Step.Compile, list: []const u8) void { } else if (exe.target.isWindows() and mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib)) { exe.linkSystemLibrary(lib[0 .. lib.len - ".lib".len]); } else { - exe.addObjectFile(.{ .path = lib }); + exe.addObjectFile(.{ .cwd_relative = lib }); } } } diff --git a/lib/build_runner.zig b/lib/build_runner.zig index cdedba90b2d6..7d036fa5bf4b 100644 --- a/lib/build_runner.zig +++ b/lib/build_runner.zig @@ -188,10 +188,10 @@ pub fn main() !void { usageAndErr(builder, false, stderr_stream); }; } else if (mem.eql(u8, arg, "--zig-lib-dir")) { - builder.zig_lib_dir = nextArg(args, &arg_idx) orelse { + builder.zig_lib_dir = .{ .cwd_relative = nextArg(args, &arg_idx) orelse { std.debug.print("Expected argument after {s}\n\n", .{arg}); usageAndErr(builder, false, stderr_stream); - }; + } }; } else if (mem.eql(u8, arg, "--debug-log")) { const next_arg = nextArg(args, &arg_idx) orelse { std.debug.print("Expected argument after {s}\n\n", .{arg}); diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 08da650d640b..a7dc16e75dd6 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -19,8 +19,6 @@ const NativeTargetInfo = std.zig.system.NativeTargetInfo; const Sha256 = std.crypto.hash.sha2.Sha256; const Build = @This(); -const build_util = @import("Build/util.zig"); - pub const Cache = @import("Build/Cache.zig"); /// deprecated: use `Step.Compile`. @@ -59,6 +57,8 @@ pub const RunStep = @import("Build/Step/Run.zig"); pub const TranslateCStep = @import("Build/Step/TranslateC.zig"); /// deprecated: use `Step.WriteFile`. pub const WriteFileStep = @import("Build/Step/WriteFile.zig"); +/// deprecated: use `LazyPath`. +pub const FileSource = LazyPath; install_tls: TopLevelStep, uninstall_tls: TopLevelStep, @@ -95,8 +95,7 @@ build_root: Cache.Directory, cache_root: Cache.Directory, global_cache_root: Cache.Directory, cache: *Cache, -/// If non-null, overrides the default zig lib dir. -zig_lib_dir: ?[]const u8, +zig_lib_dir: ?LazyPath, vcpkg_root: VcpkgRoot = .unattempted, pkg_config_pkg_list: ?(PkgConfigError![]const PkgConfigPkg) = null, args: ?[][]const u8 = null, @@ -483,6 +482,8 @@ pub const ExecutableOptions = struct { single_threaded: ?bool = null, use_llvm: ?bool = null, use_lld: ?bool = null, + zig_lib_dir: ?LazyPath = null, + main_pkg_path: ?LazyPath = null, }; pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile { @@ -499,6 +500,8 @@ pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile { .single_threaded = options.single_threaded, .use_llvm = options.use_llvm, .use_lld = options.use_lld, + .zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir, + .main_pkg_path = options.main_pkg_path, }); } @@ -512,6 +515,8 @@ pub const ObjectOptions = struct { single_threaded: ?bool = null, use_llvm: ?bool = null, use_lld: ?bool = null, + zig_lib_dir: ?LazyPath = null, + main_pkg_path: ?LazyPath = null, }; pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile { @@ -526,6 +531,8 @@ pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile { .single_threaded = options.single_threaded, .use_llvm = options.use_llvm, .use_lld = options.use_lld, + .zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir, + .main_pkg_path = options.main_pkg_path, }); } @@ -540,6 +547,8 @@ pub const SharedLibraryOptions = struct { single_threaded: ?bool = null, use_llvm: ?bool = null, use_lld: ?bool = null, + zig_lib_dir: ?LazyPath = null, + main_pkg_path: ?LazyPath = null, }; pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile { @@ -556,6 +565,8 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile .single_threaded = options.single_threaded, .use_llvm = options.use_llvm, .use_lld = options.use_lld, + .zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir, + .main_pkg_path = options.main_pkg_path, }); } @@ -570,6 +581,8 @@ pub const StaticLibraryOptions = struct { single_threaded: ?bool = null, use_llvm: ?bool = null, use_lld: ?bool = null, + zig_lib_dir: ?LazyPath = null, + main_pkg_path: ?LazyPath = null, }; pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile { @@ -586,6 +599,8 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile .single_threaded = options.single_threaded, .use_llvm = options.use_llvm, .use_lld = options.use_lld, + .zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir, + .main_pkg_path = options.main_pkg_path, }); } @@ -602,6 +617,8 @@ pub const TestOptions = struct { single_threaded: ?bool = null, use_llvm: ?bool = null, use_lld: ?bool = null, + zig_lib_dir: ?LazyPath = null, + main_pkg_path: ?LazyPath = null, }; pub fn addTest(b: *Build, options: TestOptions) *Step.Compile { @@ -618,6 +635,8 @@ pub fn addTest(b: *Build, options: TestOptions) *Step.Compile { .single_threaded = options.single_threaded, .use_llvm = options.use_llvm, .use_lld = options.use_lld, + .zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir, + .main_pkg_path = options.main_pkg_path, }); } @@ -627,6 +646,7 @@ pub const AssemblyOptions = struct { target: CrossTarget, optimize: std.builtin.Mode, max_rss: usize = 0, + zig_lib_dir: ?LazyPath = null, }; pub fn addAssembly(b: *Build, options: AssemblyOptions) *Step.Compile { @@ -637,6 +657,7 @@ pub fn addAssembly(b: *Build, options: AssemblyOptions) *Step.Compile { .target = options.target, .optimize = options.optimize, .max_rss = options.max_rss, + .zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir, }); obj_step.addAssemblyLazyPath(options.source_file.dupe(b)); return obj_step; @@ -1259,12 +1280,21 @@ fn printCmd(ally: Allocator, cwd: ?[]const u8, argv: []const []const u8) void { std.debug.print("{s}\n", .{text}); } +/// This creates the install step and adds it to the dependencies of the +/// top-level install step, using all the default options. +/// See `addInstallArtifact` for a more flexible function. pub fn installArtifact(self: *Build, artifact: *Step.Compile) void { - self.getInstallStep().dependOn(&self.addInstallArtifact(artifact).step); + self.getInstallStep().dependOn(&self.addInstallArtifact(artifact, .{}).step); } -pub fn addInstallArtifact(self: *Build, artifact: *Step.Compile) *Step.InstallArtifact { - return Step.InstallArtifact.create(self, artifact); +/// This merely creates the step; it does not add it to the dependencies of the +/// top-level install step. +pub fn addInstallArtifact( + self: *Build, + artifact: *Step.Compile, + options: Step.InstallArtifact.Options, +) *Step.InstallArtifact { + return Step.InstallArtifact.create(self, artifact, options); } ///`dest_rel_path` is relative to prefix path @@ -1330,6 +1360,7 @@ pub fn addCheckFile( return Step.CheckFile.create(b, file_source, options); } +/// deprecated: https://github.com/ziglang/zig/issues/14943 pub fn pushInstalledFile(self: *Build, dir: InstallDir, dest_rel_path: []const u8) void { const file = InstalledFile{ .dir = dir, @@ -1632,19 +1663,27 @@ pub const GeneratedFile = struct { } }; -pub const FileSource = LazyPath; // DEPRECATED, use LazyPath now - /// A reference to an existing or future path. pub const LazyPath = union(enum) { - /// A plain file path, relative to build root or absolute. + /// A source file path relative to build root. + /// This should not be an absolute path, but in an older iteration of the zig build + /// system API, it was allowed to be absolute. Absolute paths should use `cwd_relative`. path: []const u8, /// A file that is generated by an interface. Those files usually are /// not available until built by a build step. generated: *const GeneratedFile, + /// An absolute path or a path relative to the current working directory of + /// the build runner process. + /// This is uncommon but used for system environment paths such as `--zig-lib-dir` which + /// ignore the file system path of build.zig and instead are relative to the directory from + /// which `zig build` was invoked. + /// Use of this tag indicates a dependency on the host system. + cwd_relative: []const u8, + /// Returns a new file source that will have a relative path to the build root guaranteed. - /// This should be preferred over setting `.path` directly as it documents that the files are in the project directory. + /// Asserts the parameter is not an absolute path. pub fn relative(path: []const u8) LazyPath { std.debug.assert(!std.fs.path.isAbsolute(path)); return LazyPath{ .path = path }; @@ -1654,7 +1693,7 @@ pub const LazyPath = union(enum) { /// Either returns the path or `"generated"`. pub fn getDisplayName(self: LazyPath) []const u8 { return switch (self) { - .path => self.path, + .path, .cwd_relative => self.path, .generated => "generated", }; } @@ -1662,26 +1701,34 @@ pub const LazyPath = union(enum) { /// Adds dependencies this file source implies to the given step. pub fn addStepDependencies(self: LazyPath, other_step: *Step) void { switch (self) { - .path => {}, + .path, .cwd_relative => {}, .generated => |gen| other_step.dependOn(gen.step), } } - /// Should only be called during make(), returns a path relative to the build root or absolute. + /// Returns a path relative to the current process's current working directory, suitable + /// for direct file system operations. + /// + /// Intended to be used during the make phase only. pub fn getPath(self: LazyPath, src_builder: *Build) []const u8 { return getPath2(self, src_builder, null); } - /// Should only be called during make(), returns a path relative to the build root or absolute. - /// asking_step is only used for debugging purposes; it's the step being run that is asking for - /// the path. + /// Returns a path relative to the current process's current working directory, suitable + /// for direct file system operations. + /// + /// Intended to be used during the make phase only. + /// + /// `asking_step` is only used for debugging purposes; it's the step being + /// run that is asking for the path. pub fn getPath2(self: LazyPath, src_builder: *Build, asking_step: ?*Step) []const u8 { switch (self) { .path => |p| return src_builder.pathFromRoot(p), + .cwd_relative => |p| return p, .generated => |gen| return gen.path orelse { std.debug.getStderrMutex().lock(); const stderr = std.io.getStdErr(); - build_util.dumpBadGetPathHelp(gen.step, stderr, src_builder, asking_step) catch {}; + dumpBadGetPathHelp(gen.step, stderr, src_builder, asking_step) catch {}; @panic("misconfigured build script"); }, } @@ -1691,11 +1738,60 @@ pub const LazyPath = union(enum) { pub fn dupe(self: LazyPath, b: *Build) LazyPath { return switch (self) { .path => |p| .{ .path = b.dupePath(p) }, + .cwd_relative => |p| .{ .cwd_relative = b.dupePath(p) }, .generated => |gen| .{ .generated = gen }, }; } }; +/// In this function the stderr mutex has already been locked. +pub fn dumpBadGetPathHelp( + s: *Step, + stderr: fs.File, + src_builder: *Build, + asking_step: ?*Step, +) anyerror!void { + const w = stderr.writer(); + try w.print( + \\getPath() was called on a GeneratedFile that wasn't built yet. + \\ source package path: {s} + \\ Is there a missing Step dependency on step '{s}'? + \\ + , .{ + src_builder.build_root.path orelse ".", + s.name, + }); + + const tty_config = std.io.tty.detectConfig(stderr); + tty_config.setColor(w, .red) catch {}; + try stderr.writeAll(" The step was created by this stack trace:\n"); + tty_config.setColor(w, .reset) catch {}; + + const debug_info = std.debug.getSelfDebugInfo() catch |err| { + try w.print("Unable to dump stack trace: Unable to open debug info: {s}\n", .{@errorName(err)}); + return; + }; + const ally = debug_info.allocator; + std.debug.writeStackTrace(s.getStackTrace(), w, ally, debug_info, tty_config) catch |err| { + try stderr.writer().print("Unable to dump stack trace: {s}\n", .{@errorName(err)}); + return; + }; + if (asking_step) |as| { + tty_config.setColor(w, .red) catch {}; + try stderr.writeAll(" The step that is missing a dependency on the above step was created by this stack trace:\n"); + tty_config.setColor(w, .reset) catch {}; + + std.debug.writeStackTrace(as.getStackTrace(), w, ally, debug_info, tty_config) catch |err| { + try stderr.writer().print("Unable to dump stack trace: {s}\n", .{@errorName(err)}); + return; + }; + } + + tty_config.setColor(w, .red) catch {}; + try stderr.writeAll(" Hope that helps. Proceeding to panic.\n"); + tty_config.setColor(w, .reset) catch {}; +} + /// Allocates a new string for assigning a value to a named macro. /// If the value is omitted, it is set to 1. /// `name` and `value` need not live longer than the function call. diff --git a/lib/std/Build/Step.zig b/lib/std/Build/Step.zig index 274188f7f2f3..8fd60b4ad618 100644 --- a/lib/std/Build/Step.zig +++ b/lib/std/Build/Step.zig @@ -423,15 +423,7 @@ pub fn evalZigProcess( }); } - if (s.cast(Compile)) |compile| { - if (compile.generated_bin == null) // TODO(xq): How to handle this properly?! - return result; - } - - return result orelse return s.fail( - "the following command failed to communicate the compilation result:\n{s}", - .{try allocPrintCmd(arena, null, argv)}, - ); + return result; } fn sendMessage(file: std.fs.File, tag: std.zig.Client.Message.Tag) !void { diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 9898fe6df5bb..d0352b257c78 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -21,8 +21,6 @@ const InstallDir = std.Build.InstallDir; const GeneratedFile = std.Build.GeneratedFile; const Compile = @This(); -const build_util = @import("../util.zig"); - pub const base_id: Step.Id = .compile; step: Step, @@ -68,7 +66,9 @@ max_memory: ?u64 = null, shared_memory: bool = false, global_base: ?u64 = null, c_std: std.Build.CStd, +/// Set via options; intended to be read-only after that. zig_lib_dir: ?LazyPath, +/// Set via options; intended to be read-only after that. main_pkg_path: ?LazyPath, exec_cmd_args: ?[]const ?[]const u8, filter: ?[]const u8, @@ -80,12 +80,7 @@ wasi_exec_model: ?std.builtin.WasiExecModel = null, export_symbol_names: []const []const u8 = &.{}, root_src: ?LazyPath, -out_h_filename: []const u8, -out_ll_filename: []const u8, -out_bc_filename: []const u8, -out_asm_filename: []const u8, out_lib_filename: []const u8, -out_pdb_filename: []const u8, modules: std.StringArrayHashMap(*Module), link_objects: ArrayList(LinkObject), @@ -96,8 +91,6 @@ is_linking_libc: bool, is_linking_libcpp: bool, vcpkg_bin_path: ?[]const u8 = null, -/// This may be set in order to override the default install directory -override_dest_dir: ?InstallDir, installed_path: ?[]const u8, /// Base address for an executable image. @@ -207,9 +200,7 @@ use_lld: ?bool, /// otherwise. expect_errors: []const []const u8 = &.{}, -force_build: bool, - -emit_directory: GeneratedFile, +emit_directory: ?*GeneratedFile, generated_docs: ?*GeneratedFile, generated_asm: ?*GeneratedFile, @@ -221,6 +212,7 @@ generated_llvm_ir: ?*GeneratedFile, generated_h: ?*GeneratedFile, pub const CSourceFiles = struct { + /// Relative to the build root. files: []const []const u8, flags: []const []const u8, }; @@ -289,6 +281,8 @@ pub const Options = struct { single_threaded: ?bool = null, use_llvm: ?bool = null, use_lld: ?bool = null, + zig_lib_dir: ?LazyPath = null, + main_pkg_path: ?LazyPath = null, }; pub const BuildId = union(enum) { @@ -376,17 +370,6 @@ pub const Kind = enum { pub const Linkage = enum { dynamic, static }; -pub const EmitOption = enum { - docs, - @"asm", - bin, - pdb, - implib, - llvm_bc, - llvm_ir, - h, -}; - pub fn create(owner: *std.Build, options: Options) *Compile { const name = owner.dupe(options.name); const root_src: ?LazyPath = if (options.root_source_file) |rsrc| rsrc.dupe(owner) else null; @@ -451,12 +434,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile { }), .version = options.version, .out_filename = out_filename, - .out_h_filename = owner.fmt("{s}.h", .{name}), - .out_ll_filename = owner.fmt("{s}.bc", .{name}), - .out_bc_filename = owner.fmt("{s}.ll", .{name}), - .out_asm_filename = owner.fmt("{s}.s", .{name}), .out_lib_filename = undefined, - .out_pdb_filename = owner.fmt("{s}.pdb", .{name}), .major_only_filename = null, .name_only_filename = null, .modules = std.StringArrayHashMap(*Module).init(owner.allocator), @@ -477,14 +455,10 @@ pub fn create(owner: *std.Build, options: Options) *Compile { .disable_sanitize_c = false, .sanitize_thread = false, .rdynamic = false, - .override_dest_dir = null, .installed_path = null, .force_undefined_symbols = StringHashMap(void).init(owner.allocator), - .force_build = false, - - .emit_directory = GeneratedFile{ .step = &self.step }, - + .emit_directory = null, .generated_docs = null, .generated_asm = null, .generated_bin = null, @@ -503,6 +477,16 @@ pub fn create(owner: *std.Build, options: Options) *Compile { .use_lld = options.use_lld, }; + if (options.zig_lib_dir) |lp| { + self.zig_lib_dir = lp.dupe(self.step.owner); + lp.addStepDependencies(&self.step); + } + + if (options.main_pkg_path) |lp| { + self.main_pkg_path = lp.dupe(self.step.owner); + lp.addStepDependencies(&self.step); + } + if (self.kind == .lib) { if (self.linkage != null and self.linkage.? == .static) { self.out_lib_filename = self.out_filename; @@ -636,7 +620,8 @@ pub fn checkObject(self: *Compile) *Step.CheckObject { return Step.CheckObject.create(self.step.owner, self.getEmittedBin(), self.target_info.target.ofmt); } -pub const setLinkerScriptPath = setLinkerScript; // DEPRECATED, use setLinkerScript +/// deprecated: use `setLinkerScript` +pub const setLinkerScriptPath = setLinkerScript; pub fn setLinkerScript(self: *Compile, source: LazyPath) void { const b = self.step.owner; @@ -700,12 +685,17 @@ pub fn isStaticLibrary(self: *Compile) bool { pub fn producesPdbFile(self: *Compile) bool { // TODO: Is this right? Isn't PDB for *any* PE/COFF file? + // TODO: just share this logic with the compiler, silly! if (!self.target.isWindows() and !self.target.isUefi()) return false; if (self.target.getObjectFormat() == .c) return false; if (self.strip == true or (self.strip == null and self.optimize == .ReleaseSmall)) return false; return self.isDynamicLibrary() or self.kind == .exe or self.kind == .@"test"; } +pub fn producesImplib(self: *Compile) bool { + return self.isDynamicLibrary() and self.target.isWindows(); +} + pub fn linkLibC(self: *Compile) void { self.is_linking_libc = true; } @@ -961,16 +951,6 @@ pub fn setVerboseCC(self: *Compile, value: bool) void { self.verbose_cc = value; } -pub fn overrideZigLibDir(self: *Compile, dir_path: LazyPath) void { - self.zig_lib_dir = dir_path.dupe(self.step.owner); - dir_path.addStepDependencies(&self.step); -} - -pub fn setMainPkgPath(self: *Compile, dir_path: LazyPath) void { - self.main_pkg_path = dir_path.dupe(self.step.owner); - dir_path.addStepDependencies(&self.step); -} - pub fn setLibCFile(self: *Compile, libc_file: ?LazyPath) void { const b = self.step.owner; self.libc_file = if (libc_file) |f| f.dupe(b) else null; @@ -987,34 +967,17 @@ fn getEmittedFileGeneric(self: *Compile, output_file: *?*GeneratedFile) LazyPath return .{ .generated = generated_file }; } -/// Disables the panic in the build evaluation if nothing is emitted. -/// -/// Unless for compilation tests this is a code smell. -pub fn forceBuild(self: *Compile) void { - self.force_build = true; -} +/// deprecated: use `getEmittedBinDirectory` +pub const getOutputDirectorySource = getEmittedBinDirectory; -pub fn forceEmit(self: *Compile, emit: EmitOption) void { - switch (emit) { - .docs => _ = self.getEmittedDocs(), - .@"asm" => _ = self.getEmittedAsm(), - .bin => _ = self.getEmittedBin(), - .pdb => _ = self.getEmittedPdb(), - .implib => _ = self.getEmittedImplib(), - .llvm_bc => _ = self.getEmittedLlvmBc(), - .llvm_ir => _ = self.getEmittedLlvmIr(), - .h => _ = self.getEmittedH(), - } +/// Returns the path to the directory that contains the emitted binary file. +pub fn getEmittedBinDirectory(self: *Compile) LazyPath { + _ = self.getEmittedBin(); + return self.getEmittedFileGeneric(&self.emit_directory); } -pub const getOutputDirectorySource = getEmitDirectory; // DEPRECATED, use getEmitDirectory - -/// Returns the path to the output directory. -pub fn getEmitDirectory(self: *Compile) LazyPath { - return .{ .generated = &self.emit_directory }; -} - -pub const getOutputSource = getEmittedBin; // DEPRECATED, use getEmittedBin +/// deprecated: use `getEmittedBin` +pub const getOutputSource = getEmittedBin; /// Returns the path to the generated executable, library or object file. /// To run an executable built with zig build, use `run`, or create an install step and invoke it. @@ -1022,15 +985,18 @@ pub fn getEmittedBin(self: *Compile) LazyPath { return self.getEmittedFileGeneric(&self.generated_bin); } -pub const getOutputLibSource = getEmittedImplib; // DEPRECATED, use getEmittedImplib +/// deprecated: use `getEmittedImplib` +pub const getOutputLibSource = getEmittedImplib; -/// Returns the path to the generated import library. This function can only be called for libraries. +/// Returns the path to the generated import library. +/// This function can only be called for libraries. pub fn getEmittedImplib(self: *Compile) LazyPath { assert(self.kind == .lib); return self.getEmittedFileGeneric(&self.generated_implib); } -pub const getOutputHSource = getEmittedH; // DEPRECATED, use getEmittedH +/// deprecated: use `getEmittedH` +pub const getOutputHSource = getEmittedH; /// Returns the path to the generated header file. /// This function can only be called for libraries or objects. @@ -1039,11 +1005,14 @@ pub fn getEmittedH(self: *Compile) LazyPath { return self.getEmittedFileGeneric(&self.generated_h); } -pub const getOutputPdbSource = getEmittedPdb; // DEPRECATED, use getEmittedPdb +/// deprecated: use `getEmittedPdb`. +pub const getOutputPdbSource = getEmittedPdb; -/// Returns the generated PDB file. This function can only be called for Windows and UEFI. +/// Returns the generated PDB file. +/// If the compilation does not produce a PDB file, this causes a FileNotFound error +/// at build time. pub fn getEmittedPdb(self: *Compile) LazyPath { - assert(self.producesPdbFile()); + _ = self.getEmittedBin(); return self.getEmittedFileGeneric(&self.generated_pdb); } @@ -1198,13 +1167,11 @@ pub fn setExecCmd(self: *Compile, args: []const ?[]const u8) void { } fn linkLibraryOrObject(self: *Compile, other: *Compile) void { - other.forceEmit(.bin); - - if (other.target.isWindows() and other.isDynamicLibrary()) { // TODO(xq): Is this the correct logic here? - other.forceEmit(.implib); + other.getEmittedBin().addStepDependencies(&self.step); + if (other.target.isWindows() and other.isDynamicLibrary()) { + other.getEmittedImplib().addStepDependencies(&self.step); } - self.step.dependOn(&other.step); self.link_objects.append(.{ .other_step = other }) catch @panic("OOM"); self.include_dirs.append(.{ .other_step = other }) catch @panic("OOM"); @@ -1330,7 +1297,7 @@ fn getGeneratedFilePath(self: *Compile, comptime tag_name: []const u8, asking_st std.debug.getStderrMutex().lock(); const stderr = std.io.getStdErr(); - build_util.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, asking_step) catch {}; + std.Build.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, asking_step) catch {}; @panic("missing emit option for " ++ tag_name); }; @@ -1339,7 +1306,7 @@ fn getGeneratedFilePath(self: *Compile, comptime tag_name: []const u8, asking_st std.debug.getStderrMutex().lock(); const stderr = std.io.getStdErr(); - build_util.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, asking_step) catch {}; + std.Build.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, asking_step) catch {}; @panic(tag_name ++ " is null. Is there a missing step dependency?"); }; @@ -1432,11 +1399,12 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { break :l; } - // TODO(xq): Is that the right way? - const full_path_lib = if (other.isDynamicLibrary() and other.target.isWindows()) - other.getGeneratedFilePath("generated_implib", &self.step) // For DLLs, we gotta link against the implib, + // For DLLs, we gotta link against the implib. For + // everything else, we directly link against the library file. + const full_path_lib = if (other.producesImplib()) + other.getGeneratedFilePath("generated_implib", &self.step) else - other.getGeneratedFilePath("generated_bin", &self.step); // for everything else, we directly link against the library file + other.getGeneratedFilePath("generated_bin", &self.step); try zig_args.append(full_path_lib); if (other.linkage == Linkage.dynamic and !self.target.isWindows()) { @@ -1579,36 +1547,13 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { if (b.verbose_cc or self.verbose_cc) try zig_args.append("--verbose-cc"); if (b.verbose_llvm_cpu_features) try zig_args.append("--verbose-llvm-cpu-features"); - const Emitter = struct { - ptr: ?*GeneratedFile, - emit_suffix: []const u8, - }; - - const generated_files = [_]Emitter{ - .{ .ptr = self.generated_asm, .emit_suffix = "asm" }, - .{ .ptr = self.generated_bin, .emit_suffix = "bin" }, - .{ .ptr = self.generated_docs, .emit_suffix = "docs" }, - .{ .ptr = self.generated_implib, .emit_suffix = "implib" }, - .{ .ptr = self.generated_llvm_bc, .emit_suffix = "llvm-bc" }, - .{ .ptr = self.generated_llvm_ir, .emit_suffix = "llvm-ir" }, - .{ .ptr = self.generated_h, .emit_suffix = "h" }, - }; - var any_emitted_file = false; - for (generated_files) |file| { - try zig_args.append(if (file.ptr != null) - b.fmt("-femit-{s}", .{file.emit_suffix}) - else - b.fmt("-fno-emit-{s}", .{file.emit_suffix})); - - if (file.ptr != null) any_emitted_file = true; - } - - if (!any_emitted_file and !self.force_build) { - std.debug.getStderrMutex().lock(); - const stderr = std.io.getStdErr(); - build_util.dumpBadGetPathHelp(&self.step, stderr, self.step.owner, null) catch {}; - std.debug.panic("Artifact '{s}' has no emit options set, but it is made. Did you forget to call `.getEmitted*()`? If not, use `.forceBuild()` or `.forceEmit(…)` to make sure it builds anyways.", .{self.name}); - } + if (self.generated_asm != null) try zig_args.append("-femit-asm"); + if (self.generated_bin == null) try zig_args.append("-fno-emit-bin"); + if (self.generated_docs != null) try zig_args.append("-femit-docs"); + if (self.generated_implib != null) try zig_args.append("-femit-implib"); + if (self.generated_llvm_bc != null) try zig_args.append("-femit-llvm-bc"); + if (self.generated_llvm_ir != null) try zig_args.append("-femit-llvm-ir"); + if (self.generated_h != null) try zig_args.append("-femit-h"); try addFlag(&zig_args, "strip", self.strip); try addFlag(&zig_args, "unwind-tables", self.unwind_tables); @@ -1895,7 +1840,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { zig_args.appendAssumeCapacity("-rpath"); if (self.target_info.target.isDarwin()) switch (rpath) { - .path => |path| { + .path, .cwd_relative => |path| { // On Darwin, we should not try to expand special runtime paths such as // * @executable_path // * @loader_path @@ -1993,9 +1938,6 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { if (self.zig_lib_dir) |dir| { try zig_args.append("--zig-lib-dir"); try zig_args.append(dir.getPath(b)); - } else if (b.zig_lib_dir) |dir| { - try zig_args.append("--zig-lib-dir"); - try zig_args.append(dir); } if (self.main_pkg_path) |dir| { @@ -2093,37 +2035,30 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { if (maybe_output_bin_path) |output_bin_path| { const output_dir = fs.path.dirname(output_bin_path).?; - self.emit_directory.path = output_dir; + if (self.emit_directory) |lp| { + lp.path = output_dir; + } // -femit-bin[=path] (default) Output machine code if (self.generated_bin) |bin| { - bin.path = b.pathJoin( - &.{ output_dir, self.out_filename }, - ); + bin.path = b.pathJoin(&.{ output_dir, self.out_filename }); } + const sep = std.fs.path.sep; + // output PDB if someone requested it if (self.generated_pdb) |pdb| { - std.debug.assert(self.producesPdbFile()); - pdb.path = b.pathJoin( - &.{ output_dir, self.out_pdb_filename }, - ); + pdb.path = b.fmt("{s}{c}{s}.pdb", .{ output_dir, sep, self.name }); } // -femit-implib[=path] (default) Produce an import .lib when building a Windows DLL - if (self.kind == .lib) { - if (self.generated_implib) |lib| { - lib.path = b.pathJoin( - &.{ output_dir, self.out_lib_filename }, - ); - } + if (self.generated_implib) |implib| { + implib.path = b.fmt("{s}{c}{s}.lib", .{ output_dir, sep, self.name }); } // -femit-h[=path] Generate a C header file (.h) - if (self.generated_h) |lazy_path| { - lazy_path.path = b.pathJoin( - &.{ output_dir, self.out_h_filename }, - ); + if (self.generated_h) |lp| { + lp.path = b.fmt("{s}{c}{s}.h", .{ output_dir, sep, self.name }); } // -femit-docs[=path] Create a docs/ dir with html documentation @@ -2132,24 +2067,18 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } // -femit-asm[=path] Output .s (assembly code) - if (self.generated_asm) |lazy_path| { - lazy_path.path = b.pathJoin( - &.{ output_dir, self.out_asm_filename }, - ); + if (self.generated_asm) |lp| { + lp.path = b.fmt("{s}{c}{s}.s", .{ output_dir, sep, self.name }); } // -femit-llvm-ir[=path] Produce a .ll file with optimized LLVM IR (requires LLVM extensions) - if (self.generated_llvm_ir) |lazy_path| { - lazy_path.path = b.pathJoin( - &.{ output_dir, self.out_ll_filename }, - ); + if (self.generated_llvm_ir) |lp| { + lp.path = b.fmt("{s}{c}{s}.ll", .{ output_dir, sep, self.name }); } // -femit-llvm-bc[=path] Produce an optimized LLVM module as a .bc file (requires LLVM extensions) - if (self.generated_llvm_bc) |lazy_path| { - lazy_path.path = b.pathJoin( - &.{ output_dir, self.out_bc_filename }, - ); + if (self.generated_llvm_bc) |lp| { + lp.path = b.fmt("{s}{c}{s}.bc", .{ output_dir, sep, self.name }); } } diff --git a/lib/std/Build/Step/ConfigHeader.zig b/lib/std/Build/Step/ConfigHeader.zig index dbf63a9df448..df090ea94856 100644 --- a/lib/std/Build/Step/ConfigHeader.zig +++ b/lib/std/Build/Step/ConfigHeader.zig @@ -15,7 +15,8 @@ pub const Style = union(enum) { /// Start with nothing, like blank, and output a nasm .asm file. nasm, - pub const getFileSource = getPath; // DEPRECATED, use getPath + /// deprecated: use `getPath` + pub const getFileSource = getPath; pub fn getPath(style: Style) ?std.Build.LazyPath { switch (style) { @@ -100,9 +101,10 @@ pub fn addValues(self: *ConfigHeader, values: anytype) void { return addValuesInner(self, values) catch @panic("OOM"); } -pub const getFileSource = getTemplate; // DEPRECATED, use getOutput +/// deprecated: use `getOutput` +pub const getFileSource = getOutput; -pub fn getTemplate(self: *ConfigHeader) std.Build.LazyPath { +pub fn getOutput(self: *ConfigHeader) std.Build.LazyPath { return .{ .generated = &self.output_file }; } diff --git a/lib/std/Build/Step/InstallArtifact.zig b/lib/std/Build/Step/InstallArtifact.zig index a9696867eff3..b662854746fc 100644 --- a/lib/std/Build/Step/InstallArtifact.zig +++ b/lib/std/Build/Step/InstallArtifact.zig @@ -3,67 +3,117 @@ const Step = std.Build.Step; const InstallDir = std.Build.InstallDir; const InstallArtifact = @This(); const fs = std.fs; - -pub const base_id = .install_artifact; +const LazyPath = std.Build.LazyPath; step: Step, -artifact: *Step.Compile, -dest_dir: InstallDir, + +dest_dir: ?InstallDir, +dest_sub_path: []const u8, +emitted_bin: ?LazyPath, + +implib_dir: ?InstallDir, +emitted_implib: ?LazyPath, + pdb_dir: ?InstallDir, +emitted_pdb: ?LazyPath, + h_dir: ?InstallDir, -/// If non-null, adds additional path components relative to dest_dir, and -/// overrides the basename of the Compile step. -dest_sub_path: ?[]const u8, +emitted_h: ?LazyPath, + +dylib_symlinks: ?DylibSymlinkInfo, + +artifact: *Step.Compile, + +const DylibSymlinkInfo = struct { + major_only_filename: []const u8, + name_only_filename: []const u8, +}; + +pub const base_id = .install_artifact; + +pub const Options = struct { + /// Which installation directory to put the main output file into. + dest_dir: Dir = .default, + pdb_dir: Dir = .default, + h_dir: Dir = .default, + implib_dir: Dir = .default, + + /// Whether to install symlinks along with dynamic libraries. + dylib_symlinks: ?bool = null, + /// If non-null, adds additional path components relative to bin dir, and + /// overrides the basename of the Compile step for installation purposes. + dest_sub_path: ?[]const u8 = null, -pub fn create(owner: *std.Build, artifact: *Step.Compile) *InstallArtifact { + pub const Dir = union(enum) { + disabled, + default, + override: InstallDir, + }; +}; + +pub fn create(owner: *std.Build, artifact: *Step.Compile, options: Options) *InstallArtifact { const self = owner.allocator.create(InstallArtifact) catch @panic("OOM"); - self.* = InstallArtifact{ + const dest_dir: ?InstallDir = switch (options.dest_dir) { + .disabled => null, + .default => switch (artifact.kind) { + .obj => @panic("object files have no standard installation procedure"), + .exe, .@"test" => InstallDir{ .bin = {} }, + .lib => InstallDir{ .lib = {} }, + }, + .override => |o| o, + }; + self.* = .{ .step = Step.init(.{ .id = base_id, .name = owner.fmt("install {s}", .{artifact.name}), .owner = owner, .makeFn = make, }), - .artifact = artifact, - .dest_dir = artifact.override_dest_dir orelse switch (artifact.kind) { - .obj => @panic("Cannot install a .obj build artifact."), - .exe, .@"test" => InstallDir{ .bin = {} }, - .lib => InstallDir{ .lib = {} }, + .dest_dir = dest_dir, + .pdb_dir = switch (options.pdb_dir) { + .disabled => null, + .default => if (artifact.producesPdbFile()) dest_dir else null, + .override => |o| o, }, - .pdb_dir = if (artifact.producesPdbFile()) blk: { - if (artifact.kind == .exe or artifact.kind == .@"test") { - break :blk InstallDir{ .bin = {} }; - } else { - break :blk InstallDir{ .lib = {} }; - } + .h_dir = switch (options.h_dir) { + .disabled => null, + .default => switch (artifact.kind) { + .lib => .header, + else => null, + }, + .override => |o| o, + }, + .implib_dir = switch (options.implib_dir) { + .disabled => null, + .default => if (artifact.producesImplib()) dest_dir else null, + .override => |o| o, + }, + + .dylib_symlinks = if (options.dylib_symlinks orelse (dest_dir != null and + artifact.isDynamicLibrary() and + artifact.version != null and + artifact.target.wantSharedLibSymLinks())) .{ + .major_only_filename = artifact.major_only_filename.?, + .name_only_filename = artifact.name_only_filename.?, } else null, - .h_dir = if (artifact.kind == .lib and artifact.generated_h != null) .header else null, - .dest_sub_path = null, + + .dest_sub_path = options.dest_sub_path orelse artifact.out_filename, + + .emitted_bin = null, + .emitted_pdb = null, + .emitted_h = null, + .emitted_implib = null, + + .artifact = artifact, }; + self.step.dependOn(&artifact.step); - artifact.forceEmit(.bin); + if (self.dest_dir != null) self.emitted_bin = artifact.getEmittedBin(); + if (self.pdb_dir != null) self.emitted_pdb = artifact.getEmittedPdb(); + if (self.h_dir != null) self.emitted_h = artifact.getEmittedH(); + if (self.implib_dir != null) self.emitted_implib = artifact.getEmittedImplib(); - owner.pushInstalledFile(self.dest_dir, artifact.out_filename); - if (self.artifact.isDynamicLibrary()) { - if (artifact.major_only_filename) |name| { - owner.pushInstalledFile(.lib, name); - } - if (artifact.name_only_filename) |name| { - owner.pushInstalledFile(.lib, name); - } - if (self.artifact.target.isWindows()) { - owner.pushInstalledFile(.lib, artifact.out_lib_filename); - } - } - if (self.pdb_dir) |pdb_dir| { - _ = artifact.getEmittedPdb(); // force creation - owner.pushInstalledFile(pdb_dir, artifact.out_pdb_filename); - } - if (self.h_dir) |h_dir| { - _ = artifact.getEmittedH(); // force creation - owner.pushInstalledFile(h_dir, artifact.out_h_filename); - } return self; } @@ -71,35 +121,30 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { _ = prog_node; const self = @fieldParentPtr(InstallArtifact, "step", step); const dest_builder = step.owner; - - const dest_sub_path = if (self.dest_sub_path) |sub_path| sub_path else self.artifact.out_filename; - const full_dest_path = dest_builder.getInstallPath(self.dest_dir, dest_sub_path); const cwd = fs.cwd(); var all_cached = true; - { - const full_src_path = self.artifact.generated_bin.?.path.?; + if (self.dest_dir) |dest_dir| { + const full_dest_path = dest_builder.getInstallPath(dest_dir, self.dest_sub_path); + const full_src_path = self.emitted_bin.?.getPath2(step.owner, step); const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_dest_path, .{}) catch |err| { return step.fail("unable to update file from '{s}' to '{s}': {s}", .{ full_src_path, full_dest_path, @errorName(err), }); }; all_cached = all_cached and p == .fresh; - } - if (self.artifact.isDynamicLibrary() and - self.artifact.version != null and - self.artifact.target.wantSharedLibSymLinks()) - { - try Step.Compile.doAtomicSymLinks(step, full_dest_path, self.artifact.major_only_filename.?, self.artifact.name_only_filename.?); + if (self.dylib_symlinks) |dls| { + try Step.Compile.doAtomicSymLinks(step, full_dest_path, dls.major_only_filename, dls.name_only_filename); + } + + self.artifact.installed_path = full_dest_path; } - if (self.artifact.isDynamicLibrary() and - self.artifact.target.isWindows() and - self.artifact.generated_implib != null) - { - const full_src_path = self.artifact.generated_implib.?.path.?; - const full_implib_path = dest_builder.getInstallPath(self.dest_dir, self.artifact.out_lib_filename); + + if (self.implib_dir) |implib_dir| { + const full_src_path = self.emitted_implib.?.getPath2(step.owner, step); + const full_implib_path = dest_builder.getInstallPath(implib_dir, fs.path.basename(full_src_path)); const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_implib_path, .{}) catch |err| { return step.fail("unable to update file from '{s}' to '{s}': {s}", .{ full_src_path, full_implib_path, @errorName(err), @@ -107,9 +152,10 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { }; all_cached = all_cached and p == .fresh; } + if (self.pdb_dir) |pdb_dir| { - const full_src_path = self.artifact.generated_pdb.?.path.?; - const full_pdb_path = dest_builder.getInstallPath(pdb_dir, self.artifact.out_pdb_filename); + const full_src_path = self.emitted_pdb.?.getPath2(step.owner, step); + const full_pdb_path = dest_builder.getInstallPath(pdb_dir, fs.path.basename(full_src_path)); const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_pdb_path, .{}) catch |err| { return step.fail("unable to update file from '{s}' to '{s}': {s}", .{ full_src_path, full_pdb_path, @errorName(err), @@ -117,9 +163,10 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { }; all_cached = all_cached and p == .fresh; } + if (self.h_dir) |h_dir| { - const full_src_path = self.artifact.generated_h.?.path.?; - const full_h_path = dest_builder.getInstallPath(h_dir, self.artifact.out_h_filename); + const full_src_path = self.emitted_h.?.getPath2(step.owner, step); + const full_h_path = dest_builder.getInstallPath(h_dir, fs.path.basename(full_src_path)); const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_h_path, .{}) catch |err| { return step.fail("unable to update file from '{s}' to '{s}': {s}", .{ full_src_path, full_h_path, @errorName(err), @@ -127,6 +174,6 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { }; all_cached = all_cached and p == .fresh; } - self.artifact.installed_path = full_dest_path; + step.result_cached = all_cached; } diff --git a/lib/std/Build/Step/ObjCopy.zig b/lib/std/Build/Step/ObjCopy.zig index 77f1c2a0de9e..1fc6aa9a4175 100644 --- a/lib/std/Build/Step/ObjCopy.zig +++ b/lib/std/Build/Step/ObjCopy.zig @@ -60,7 +60,8 @@ pub fn create( return self; } -pub const getOutputSource = getOutput; // DEPRECATED, use getOutput +/// deprecated: use getOutput +pub const getOutputSource = getOutput; pub fn getOutput(self: *const ObjCopy) std.Build.LazyPath { return .{ .generated = &self.output_file }; diff --git a/lib/std/Build/Step/Options.zig b/lib/std/Build/Step/Options.zig index 0f058cab05a0..575ee747d15e 100644 --- a/lib/std/Build/Step/Options.zig +++ b/lib/std/Build/Step/Options.zig @@ -13,7 +13,7 @@ step: Step, generated_file: GeneratedFile, contents: std.ArrayList(u8), -args: std.ArrayList(OptionLazyPathArg), +args: std.ArrayList(Arg), pub fn create(owner: *std.Build) *Options { const self = owner.allocator.create(Options) catch @panic("OOM"); @@ -26,7 +26,7 @@ pub fn create(owner: *std.Build) *Options { }), .generated_file = undefined, .contents = std.ArrayList(u8).init(owner.allocator), - .args = std.ArrayList(OptionLazyPathArg).init(owner.allocator), + .args = std.ArrayList(Arg).init(owner.allocator), }; self.generated_file = .{ .step = &self.step }; @@ -166,7 +166,8 @@ fn printLiteral(out: anytype, val: anytype, indent: u8) !void { } } -pub const addOptionFileSource = addOptionPath; // DEPRECATED, use addPathOption +/// deprecated: use `addOptionPath` +pub const addOptionFileSource = addOptionPath; /// The value is the path in the cache dir. /// Adds a dependency automatically. @@ -182,14 +183,9 @@ pub fn addOptionPath( path.addStepDependencies(&self.step); } -/// The value is the path in the cache dir. -/// Adds a dependency automatically. +/// Deprecated: use `addOptionPath(options, name, artifact.getEmittedBin())` instead. pub fn addOptionArtifact(self: *Options, name: []const u8, artifact: *Step.Compile) void { - self.args.append(.{ - .name = self.step.owner.dupe(name), - .artifact = artifact.getEmittedBin, - }) catch @panic("OOM"); - self.step.dependOn(&artifact.step); + return addOptionPath(self, name, artifact.getEmittedBin()); } pub fn createModule(self: *Options) *std.Build.Module { @@ -199,7 +195,8 @@ pub fn createModule(self: *Options) *std.Build.Module { }); } -pub const getSource = getOutput; // DEPRECATED, use getOutput +/// deprecated: use `getOutput` +pub const getSource = getOutput; pub fn getOutput(self: *Options) LazyPath { return .{ .generated = &self.generated_file }; @@ -226,7 +223,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { var hash = b.cache.hash; // Random bytes to make unique. Refresh this with new random bytes when // implementation is modified in a non-backwards-compatible way. - hash.add(@as(u32, 0x38845ef8)); + hash.add(@as(u32, 0xad95e922)); hash.addBytes(self.contents.items); const sub_path = "c" ++ fs.path.sep_str ++ hash.final() ++ fs.path.sep_str ++ basename; @@ -291,7 +288,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } } -const OptionLazyPathArg = struct { +const Arg = struct { name: []const u8, path: LazyPath, }; diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index 55278e2d5b53..1721e9dbc5d6 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -164,12 +164,9 @@ pub fn enableTestRunnerMode(self: *Run) void { } pub fn addArtifactArg(self: *Run, artifact: *Step.Compile) void { - // enforce creation of the binary file by invoking getEmittedBin const bin_file = artifact.getEmittedBin(); bin_file.addStepDependencies(&self.step); - self.argv.append(Arg{ .artifact = artifact }) catch @panic("OOM"); - self.step.dependOn(&artifact.step); } /// This provides file path as a command line argument to the command being @@ -201,32 +198,36 @@ pub fn addPrefixedOutputFileArg( return .{ .generated = &output.generated_file }; } -pub const addFileSourceArg = addFileArg; // DEPRECATED, use addFileArg +/// deprecated: use `addFileArg` +pub const addFileSourceArg = addFileArg; -pub fn addFileArg(self: *Run, file_source: std.Build.LazyPath) void { - self.addPrefixedFileArg("", file_source); +pub fn addFileArg(self: *Run, lp: std.Build.LazyPath) void { + self.addPrefixedFileArg("", lp); } -pub const addPrefixedFileSourceArg = addPrefixedFileArg; // DEPRECATED, use addPrefixedFileArg +// deprecated: use `addPrefixedFileArg` +pub const addPrefixedFileSourceArg = addPrefixedFileArg; -pub fn addPrefixedFileArg(self: *Run, prefix: []const u8, file_source: std.Build.LazyPath) void { +pub fn addPrefixedFileArg(self: *Run, prefix: []const u8, lp: std.Build.LazyPath) void { const b = self.step.owner; const prefixed_file_source: PrefixedLazyPath = .{ .prefix = b.dupe(prefix), - .file_source = file_source.dupe(b), + .file_source = lp.dupe(b), }; self.argv.append(.{ .file_source = prefixed_file_source }) catch @panic("OOM"); - file_source.addStepDependencies(&self.step); + lp.addStepDependencies(&self.step); } -pub const addDirectorySourceArg = addDirectoryArg; // DEPRECATED, use addDirectoryArg +/// deprecated: use `addDirectoryArg` +pub const addDirectorySourceArg = addDirectoryArg; pub fn addDirectoryArg(self: *Run, directory_source: std.Build.LazyPath) void { self.addPrefixedDirectoryArg("", directory_source); } -pub const addPrefixedDirectorySourceArg = addPrefixedDirectoryArg; // DEPRECATED, use addPrefixedDirectoryArg +// deprecated: use `addPrefixedDirectoryArg` +pub const addPrefixedDirectorySourceArg = addPrefixedDirectoryArg; pub fn addPrefixedDirectoryArg(self: *Run, prefix: []const u8, directory_source: std.Build.LazyPath) void { const b = self.step.owner; diff --git a/lib/std/Build/Step/WriteFile.zig b/lib/std/Build/Step/WriteFile.zig index f8fc60621671..51db4bb3c391 100644 --- a/lib/std/Build/Step/WriteFile.zig +++ b/lib/std/Build/Step/WriteFile.zig @@ -28,7 +28,8 @@ pub const File = struct { sub_path: []const u8, contents: Contents, - pub const getFileSource = getPath; // DEPRECATED, use getPath + /// deprecated: use `getPath` + pub const getFileSource = getPath; pub fn getPath(self: *File) std.Build.LazyPath { return .{ .generated = &self.generated_file }; diff --git a/lib/std/Build/util.zig b/lib/std/Build/util.zig deleted file mode 100644 index 5c1b91f3d646..000000000000 --- a/lib/std/Build/util.zig +++ /dev/null @@ -1,53 +0,0 @@ -const std = @import("std"); -const fs = std.fs; - -const Build = std.Build; -const Step = std.Build.Step; - -/// In this function the stderr mutex has already been locked. -pub fn dumpBadGetPathHelp( - s: *Step, - stderr: fs.File, - src_builder: *Build, - asking_step: ?*Step, -) anyerror!void { - const w = stderr.writer(); - try w.print( - \\getPath() was called on a GeneratedFile that wasn't built yet. - \\ source package path: {s} - \\ Is there a missing Step dependency on step '{s}'? - \\ - , .{ - src_builder.build_root.path orelse ".", - s.name, - }); - - const tty_config = std.io.tty.detectConfig(stderr); - tty_config.setColor(w, .red) catch {}; - try stderr.writeAll(" The step was created by this stack trace:\n"); - tty_config.setColor(w, .reset) catch {}; - - const debug_info = std.debug.getSelfDebugInfo() catch |err| { - try w.print("Unable to dump stack trace: Unable to open debug info: {s}\n", .{@errorName(err)}); - return; - }; - const ally = debug_info.allocator; - std.debug.writeStackTrace(s.getStackTrace(), w, ally, debug_info, tty_config) catch |err| { - try stderr.writer().print("Unable to dump stack trace: {s}\n", .{@errorName(err)}); - return; - }; - if (asking_step) |as| { - tty_config.setColor(w, .red) catch {}; - try stderr.writeAll(" The step that is missing a dependency on the above step was created by this stack trace:\n"); - tty_config.setColor(w, .reset) catch {}; - - std.debug.writeStackTrace(as.getStackTrace(), w, ally, debug_info, tty_config) catch |err| { - try stderr.writer().print("Unable to dump stack trace: {s}\n", .{@errorName(err)}); - return; - }; - } - - tty_config.setColor(w, .red) catch {}; - try stderr.writeAll(" Hope that helps. Proceeding to panic.\n"); - tty_config.setColor(w, .reset) catch {}; -} diff --git a/test/link/glibc_compat/build.zig b/test/link/glibc_compat/build.zig index 8f80a37b0258..a104633fd764 100644 --- a/test/link/glibc_compat/build.zig +++ b/test/link/glibc_compat/build.zig @@ -4,7 +4,7 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Test"); b.default_step = test_step; - inline for (.{ "aarch64-linux-gnu.2.27", "aarch64-linux-gnu.2.34" }) |t| { + for ([_][]const u8{ "aarch64-linux-gnu.2.27", "aarch64-linux-gnu.2.34" }) |t| { const exe = b.addExecutable(.{ .name = t, .root_source_file = .{ .path = "main.c" }, @@ -13,7 +13,8 @@ pub fn build(b: *std.Build) void { ) catch unreachable, }); exe.linkLibC(); - exe.forceBuild(); + // TODO: actually test the output + _ = exe.getEmittedBin(); test_step.dependOn(&exe.step); } } diff --git a/test/link/macho/dylib/build.zig b/test/link/macho/dylib/build.zig index aecc9b38ec10..b05e4f5677b3 100644 --- a/test/link/macho/dylib/build.zig +++ b/test/link/macho/dylib/build.zig @@ -41,8 +41,8 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize }); exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} }); exe.linkSystemLibrary("a"); - exe.addLibraryPath(dylib.getEmitDirectory()); - exe.addRPath(dylib.getEmitDirectory()); + exe.addLibraryPath(dylib.getEmittedBinDirectory()); + exe.addRPath(dylib.getEmittedBinDirectory()); exe.linkLibC(); const check_exe = exe.checkObject(); diff --git a/test/link/macho/needed_library/build.zig b/test/link/macho/needed_library/build.zig index c24569e06d61..de5d6b1ae6e3 100644 --- a/test/link/macho/needed_library/build.zig +++ b/test/link/macho/needed_library/build.zig @@ -23,7 +23,6 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize }); dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); dylib.linkLibC(); - dylib.forceEmit(.bin); // enforce library creation, we import it below // -dead_strip_dylibs // -needed-la @@ -35,8 +34,8 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} }); exe.linkLibC(); exe.linkSystemLibraryNeeded("a"); - exe.addLibraryPath(dylib.getEmitDirectory()); - exe.addRPath(dylib.getEmitDirectory()); + exe.addLibraryPath(dylib.getEmittedBinDirectory()); + exe.addRPath(dylib.getEmittedBinDirectory()); exe.dead_strip_dylibs = true; const check = exe.checkObject(); diff --git a/test/link/macho/search_strategy/build.zig b/test/link/macho/search_strategy/build.zig index d802bddbf41a..574782617b18 100644 --- a/test/link/macho/search_strategy/build.zig +++ b/test/link/macho/search_strategy/build.zig @@ -57,10 +57,6 @@ fn createScenario( }); static.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); static.linkLibC(); - static.override_dest_dir = std.Build.InstallDir{ - .custom = "static", - }; - static.forceEmit(.bin); const dylib = b.addSharedLibrary(.{ .name = name, @@ -70,10 +66,6 @@ fn createScenario( }); dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); dylib.linkLibC(); - dylib.override_dest_dir = std.Build.InstallDir{ - .custom = "dynamic", - }; - dylib.forceEmit(.bin); // we want the binary to be built as we use it further below const exe = b.addExecutable(.{ .name = name, @@ -83,8 +75,8 @@ fn createScenario( exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} }); exe.linkSystemLibraryName(name); exe.linkLibC(); - exe.addLibraryPath(static.getEmitDirectory()); - exe.addLibraryPath(dylib.getEmitDirectory()); - exe.addRPath(dylib.getEmitDirectory()); + exe.addLibraryPath(static.getEmittedBinDirectory()); + exe.addLibraryPath(dylib.getEmittedBinDirectory()); + exe.addRPath(dylib.getEmittedBinDirectory()); return exe; } diff --git a/test/link/macho/tbdv3/build.zig b/test/link/macho/tbdv3/build.zig index 3b27b5a0ca5c..ba11004f29b9 100644 --- a/test/link/macho/tbdv3/build.zig +++ b/test/link/macho/tbdv3/build.zig @@ -25,7 +25,6 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize }); lib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} }); lib.linkLibC(); - lib.forceEmit(.bin); // will be referenced by the tbd file const tbd_file = b.addWriteFile("liba.tbd", \\--- !tapi-tbd-v3 @@ -47,7 +46,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} }); exe.linkSystemLibrary("a"); exe.addLibraryPath(tbd_file.getDirectory()); - exe.addRPath(lib.getEmitDirectory()); + exe.addRPath(lib.getEmittedBinDirectory()); exe.linkLibC(); const run = b.addRunArtifact(exe); diff --git a/test/link/macho/weak_library/build.zig b/test/link/macho/weak_library/build.zig index 3d0819c19b15..f66316693e99 100644 --- a/test/link/macho/weak_library/build.zig +++ b/test/link/macho/weak_library/build.zig @@ -33,8 +33,8 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} }); exe.linkLibC(); exe.linkSystemLibraryWeak("a"); - exe.addLibraryPath(dylib.getEmitDirectory()); - exe.addRPath(dylib.getEmitDirectory()); + exe.addLibraryPath(dylib.getEmittedBinDirectory()); + exe.addRPath(dylib.getEmittedBinDirectory()); const check = exe.checkObject(); check.checkStart(); diff --git a/test/src/Cases.zig b/test/src/Cases.zig index 1cb9b0fcdcff..08aadaf0d42a 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -551,12 +551,6 @@ pub fn lowerToBuildSteps( }), }; - if (case.emit_bin) { - artifact.forceEmit(.bin); - } else { - artifact.forceBuild(); - } - if (case.link_libc) artifact.linkLibC(); switch (case.backend) { diff --git a/test/standalone.zig b/test/standalone.zig index dc9d7e291872..3725456fa1da 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -141,15 +141,15 @@ pub const build_cases = [_]BuildCase{ .import = @import("standalone/install_raw_hex/build.zig"), }, // TODO take away EmitOption.emit_to option and make it give a FileSource - // .{ - // .build_root = "test/standalone/emit_asm_and_bin", - // .import = @import("standalone/emit_asm_and_bin/build.zig"), - // }, + //.{ + // .build_root = "test/standalone/emit_asm_and_bin", + // .import = @import("standalone/emit_asm_and_bin/build.zig"), + //}, // TODO take away EmitOption.emit_to option and make it give a FileSource - // .{ - // .build_root = "test/standalone/issue_12588", - // .import = @import("standalone/issue_12588/build.zig"), - // }, + //.{ + // .build_root = "test/standalone/issue_12588", + // .import = @import("standalone/issue_12588/build.zig"), + //}, .{ .build_root = "test/standalone/child_process", .import = @import("standalone/child_process/build.zig"), diff --git a/test/standalone/compiler_rt_panic/build.zig b/test/standalone/compiler_rt_panic/build.zig index c10771872db1..3d22319bacf5 100644 --- a/test/standalone/compiler_rt_panic/build.zig +++ b/test/standalone/compiler_rt_panic/build.zig @@ -16,7 +16,10 @@ pub fn build(b: *std.Build) void { .target = target, }); exe.linkLibC(); - exe.addCSourceFile("main.c", &.{}); + exe.addCSourceFile(.{ + .file = .{ .path = "main.c" }, + .flags = &.{}, + }); exe.link_gc_sections = false; exe.bundle_compiler_rt = true; diff --git a/test/standalone/embed_generated_file/build.zig b/test/standalone/embed_generated_file/build.zig index 220220b15f5f..ca05c33c39a6 100644 --- a/test/standalone/embed_generated_file/build.zig +++ b/test/standalone/embed_generated_file/build.zig @@ -22,7 +22,8 @@ pub fn build(b: *std.Build) void { .source_file = bootloader.getEmittedBin(), }); - exe.forceBuild(); + // TODO: actually check the output + _ = exe.getEmittedBin(); test_step.dependOn(&exe.step); } diff --git a/test/standalone/emit_asm_and_bin/build.zig b/test/standalone/emit_asm_and_bin/build.zig index 57df1185eebd..2f671bb6cce5 100644 --- a/test/standalone/emit_asm_and_bin/build.zig +++ b/test/standalone/emit_asm_and_bin/build.zig @@ -8,8 +8,9 @@ pub fn build(b: *std.Build) void { .root_source_file = .{ .path = "main.zig" }, .optimize = b.standardOptimizeOption(.{}), }); - main.forceEmit(.bin); - main.forceEmit(.@"asm"); + // TODO: actually check these two artifacts for correctness + _ = main.getEmittedBin(); + _ = main.getEmittedAsm(); test_step.dependOn(&b.addRunArtifact(main).step); } diff --git a/test/standalone/issue_339/build.zig b/test/standalone/issue_339/build.zig index 6afb0125ea0d..d53ba992ea3e 100644 --- a/test/standalone/issue_339/build.zig +++ b/test/standalone/issue_339/build.zig @@ -14,7 +14,8 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); - obj.forceBuild(); + // TODO: actually check the output + _ = obj.getEmittedBin(); test_step.dependOn(&obj.step); } diff --git a/test/standalone/issue_5825/build.zig b/test/standalone/issue_5825/build.zig index 48c43242b26b..9a790f6bb927 100644 --- a/test/standalone/issue_5825/build.zig +++ b/test/standalone/issue_5825/build.zig @@ -27,7 +27,8 @@ pub fn build(b: *std.Build) void { exe.linkSystemLibrary("ntdll"); exe.addObject(obj); - exe.forceBuild(); + // TODO: actually check the output + _ = exe.getEmittedBin(); test_step.dependOn(&exe.step); } diff --git a/test/standalone/issue_794/build.zig b/test/standalone/issue_794/build.zig index 8ef5cf4e9f3c..eb05aa9b4fd5 100644 --- a/test/standalone/issue_794/build.zig +++ b/test/standalone/issue_794/build.zig @@ -9,7 +9,8 @@ pub fn build(b: *std.Build) void { }); test_artifact.addIncludePath(.{ .path = "a_directory" }); - test_artifact.forceBuild(); + // TODO: actually check the output + _ = test_artifact.getEmittedBin(); test_step.dependOn(&test_artifact.step); } diff --git a/test/standalone/main_pkg_path/build.zig b/test/standalone/main_pkg_path/build.zig index bbe7cfb53b52..d1b67592b5b0 100644 --- a/test/standalone/main_pkg_path/build.zig +++ b/test/standalone/main_pkg_path/build.zig @@ -6,8 +6,8 @@ pub fn build(b: *std.Build) void { const test_exe = b.addTest(.{ .root_source_file = .{ .path = "a/test.zig" }, + .main_pkg_path = .{ .path = "." }, }); - test_exe.setMainPkgPath(.{ .path = "." }); test_step.dependOn(&b.addRunArtifact(test_exe).step); } diff --git a/test/standalone/strip_empty_loop/build.zig b/test/standalone/strip_empty_loop/build.zig index 77a1a08f754a..e64781d99142 100644 --- a/test/standalone/strip_empty_loop/build.zig +++ b/test/standalone/strip_empty_loop/build.zig @@ -15,7 +15,8 @@ pub fn build(b: *std.Build) void { }); main.strip = true; - main.forceBuild(); + // TODO: actually check the output + _ = main.getEmittedBin(); test_step.dependOn(&main.step); } diff --git a/test/tests.zig b/test/tests.zig index 4174fc0147c8..d42344cb883a 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -588,7 +588,7 @@ pub fn addStandaloneTests( }); if (case.link_libc) exe.linkLibC(); - exe.forceBuild(); + _ = exe.getEmittedBin(); step.dependOn(&exe.step); } @@ -1008,6 +1008,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { .single_threaded = test_target.single_threaded, .use_llvm = test_target.use_llvm, .use_lld = test_target.use_lld, + .zig_lib_dir = .{ .path = "lib" }, }); const single_threaded_suffix = if (test_target.single_threaded == true) "-single" else ""; const backend_suffix = if (test_target.use_llvm == true) @@ -1019,7 +1020,6 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { else ""; - these_tests.overrideZigLibDir(.{ .path = "lib" }); these_tests.addIncludePath(.{ .path = "test" }); const qualified_name = b.fmt("{s}-{s}-{s}{s}{s}{s}", .{ @@ -1039,8 +1039,8 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { .name = qualified_name, .link_libc = test_target.link_libc, .target = altered_target, + .zig_lib_dir = .{ .path = "lib" }, }); - compile_c.overrideZigLibDir(.{ .path = "lib" }); compile_c.addCSourceFile(.{ .file = these_tests.getEmittedBin(), .flags = &.{ From 6393928d508d5a18d3a15b8c9ecbc53ffb3812ce Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 30 Jul 2023 11:18:09 -0700 Subject: [PATCH 07/10] link: add more respect for -fno-emit-bin closes #16347 --- src/link.zig | 6 ++++-- src/link/Coff.zig | 2 ++ src/link/Elf.zig | 2 ++ src/link/MachO.zig | 2 ++ src/link/NvPtx.zig | 3 ++- src/link/Wasm.zig | 2 ++ 6 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/link.zig b/src/link.zig index 703dfb87394b..42322cbda882 100644 --- a/src/link.zig +++ b/src/link.zig @@ -966,6 +966,8 @@ pub const File = struct { } pub fn linkAsArchive(base: *File, comp: *Compilation, prog_node: *std.Progress.Node) FlushError!void { + const emit = base.options.emit orelse return; + const tracy = trace(@src()); defer tracy.end(); @@ -973,8 +975,8 @@ pub const File = struct { defer arena_allocator.deinit(); const arena = arena_allocator.allocator(); - const directory = base.options.emit.?.directory; // Just an alias to make it shorter to type. - const full_out_path = try directory.join(arena, &[_][]const u8{base.options.emit.?.sub_path}); + const directory = emit.directory; // Just an alias to make it shorter to type. + const full_out_path = try directory.join(arena, &[_][]const u8{emit.sub_path}); const full_out_path_z = try arena.dupeZ(u8, full_out_path); // If there is no Zig code to compile, then we should skip flushing the output file diff --git a/src/link/Coff.zig b/src/link/Coff.zig index 8720fc1037b3..f1b914c36819 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -1452,6 +1452,8 @@ pub fn updateDeclExports( if (self.llvm_object) |llvm_object| return llvm_object.updateDeclExports(mod, decl_index, exports); } + if (self.base.options.emit == null) return; + const gpa = self.base.allocator; const decl = mod.declPtr(decl_index); diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 4bb049e07403..ec4da9c728e2 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -2865,6 +2865,8 @@ pub fn updateDeclExports( if (self.llvm_object) |llvm_object| return llvm_object.updateDeclExports(mod, decl_index, exports); } + if (self.base.options.emit == null) return; + const tracy = trace(@src()); defer tracy.end(); diff --git a/src/link/MachO.zig b/src/link/MachO.zig index d124a1f51c4b..44e6acc3976d 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -2386,6 +2386,8 @@ pub fn updateDeclExports( return llvm_object.updateDeclExports(mod, decl_index, exports); } + if (self.base.options.emit == null) return; + const tracy = trace(@src()); defer tracy.end(); diff --git a/src/link/NvPtx.zig b/src/link/NvPtx.zig index fc3d659770cf..fd1504a3d3e6 100644 --- a/src/link/NvPtx.zig +++ b/src/link/NvPtx.zig @@ -106,10 +106,11 @@ pub fn flushModule(self: *NvPtx, comp: *Compilation, prog_node: *std.Progress.No if (build_options.skip_non_native) { @panic("Attempted to compile for architecture that was disabled by build configuration"); } + const outfile = comp.bin_file.options.emit orelse return; + const tracy = trace(@src()); defer tracy.end(); - const outfile = comp.bin_file.options.emit.?; // We modify 'comp' before passing it to LLVM, but restore value afterwards. // We tell LLVM to not try to build a .o, only an "assembly" file. // This is required by the LLVM PTX backend. diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index c2a2de398f9b..11294daa8430 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -1712,6 +1712,8 @@ pub fn updateDeclExports( if (wasm.llvm_object) |llvm_object| return llvm_object.updateDeclExports(mod, decl_index, exports); } + if (wasm.base.options.emit == null) return; + const decl = mod.declPtr(decl_index); const atom_index = try wasm.getOrCreateAtomForDecl(decl_index); const atom = wasm.getAtom(atom_index); From bdbd6172371543c6e1c80ea3f6abcb87702eeb08 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 30 Jul 2023 17:22:54 -0700 Subject: [PATCH 08/10] std.Build.Step.InstallArtifact: disable emit-h This branch was not intended to introduce new test coverage on the emit-h feature. See #9698 --- lib/std/Build/Step/InstallArtifact.zig | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/std/Build/Step/InstallArtifact.zig b/lib/std/Build/Step/InstallArtifact.zig index b662854746fc..f3c9ca3bef0e 100644 --- a/lib/std/Build/Step/InstallArtifact.zig +++ b/lib/std/Build/Step/InstallArtifact.zig @@ -77,10 +77,12 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile, options: Options) *Ins }, .h_dir = switch (options.h_dir) { .disabled => null, - .default => switch (artifact.kind) { - .lib => .header, - else => null, - }, + // https://github.com/ziglang/zig/issues/9698 + .default => null, + //.default => switch (artifact.kind) { + // .lib => .header, + // else => null, + //}, .override => |o| o, }, .implib_dir = switch (options.implib_dir) { From 25a9487caa05c16f9f87cb6931340e1a1051a7f3 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 30 Jul 2023 18:19:39 -0700 Subject: [PATCH 09/10] std.Build.LazyPath: fix resolution of cwd_relative The callsites of getPath rely on the result being absolute so that they can pass the path to a child process with the cwd set to the build root. --- build.zig | 2 +- doc/docgen.zig | 4 +++- lib/std/Build.zig | 15 ++++++++------- lib/std/Build/Step/Run.zig | 28 ++++++++++++++-------------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/build.zig b/build.zig index a7d74bdc06ae..7458ec97a22d 100644 --- a/build.zig +++ b/build.zig @@ -46,7 +46,7 @@ pub fn build(b: *std.Build) !void { docgen_cmd.addArgs(&.{ "--zig", b.zig_exe }); if (b.zig_lib_dir) |p| { docgen_cmd.addArg("--zig-lib-dir"); - docgen_cmd.addFileArg(p); + docgen_cmd.addDirectoryArg(p); } docgen_cmd.addFileArg(.{ .path = "doc/langref.html.in" }); const langref_file = docgen_cmd.addOutputFileArg("langref.html"); diff --git a/doc/docgen.zig b/doc/docgen.zig index 675dcccf70b5..589a14818231 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -64,7 +64,9 @@ pub fn main() !void { } } else if (mem.eql(u8, arg, "--zig-lib-dir")) { if (args_it.next()) |param| { - opt_zig_lib_dir = param; + // Convert relative to absolute because this will be passed + // to a child process with a different cwd. + opt_zig_lib_dir = try fs.realpathAlloc(allocator, param); } else { fatal("expected parameter after --zig-lib-dir", .{}); } diff --git a/lib/std/Build.zig b/lib/std/Build.zig index a7dc16e75dd6..9cc501d3240e 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1390,6 +1390,11 @@ pub fn pathFromRoot(b: *Build, p: []const u8) []u8 { return fs.path.resolve(b.allocator, &.{ b.build_root.path orelse ".", p }) catch @panic("OOM"); } +fn pathFromCwd(b: *Build, p: []const u8) []u8 { + const cwd = process.getCwdAlloc(b.allocator) catch @panic("OOM"); + return fs.path.resolve(b.allocator, &.{ cwd, p }) catch @panic("OOM"); +} + pub fn pathJoin(self: *Build, paths: []const []const u8) []u8 { return fs.path.join(self.allocator, paths) catch @panic("OOM"); } @@ -1706,17 +1711,13 @@ pub const LazyPath = union(enum) { } } - /// Returns a path relative to the current process's current working directory, suitable - /// for direct file system operations. - /// + /// Returns an absolute path. /// Intended to be used during the make phase only. pub fn getPath(self: LazyPath, src_builder: *Build) []const u8 { return getPath2(self, src_builder, null); } - /// Returns a path relative to the current process's current working directory, suitable - /// for direct file system operations. - /// + /// Returns an absolute path. /// Intended to be used during the make phase only. /// /// `asking_step` is only used for debugging purposes; it's the step being @@ -1724,7 +1725,7 @@ pub const LazyPath = union(enum) { pub fn getPath2(self: LazyPath, src_builder: *Build, asking_step: ?*Step) []const u8 { switch (self) { .path => |p| return src_builder.pathFromRoot(p), - .cwd_relative => |p| return p, + .cwd_relative => |p| return src_builder.pathFromCwd(p), .generated => |gen| return gen.path orelse { std.debug.getStderrMutex().lock(); const stderr = std.io.getStdErr(); diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index 1721e9dbc5d6..4bb474f4da54 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -82,7 +82,7 @@ has_side_effects: bool = false, pub const StdIn = union(enum) { none, bytes: []const u8, - file_source: std.Build.LazyPath, + lazy_path: std.Build.LazyPath, }; pub const StdIo = union(enum) { @@ -120,7 +120,7 @@ pub const StdIo = union(enum) { pub const Arg = union(enum) { artifact: *Step.Compile, - file_source: PrefixedLazyPath, + lazy_path: PrefixedLazyPath, directory_source: PrefixedLazyPath, bytes: []u8, output: *Output, @@ -128,7 +128,7 @@ pub const Arg = union(enum) { pub const PrefixedLazyPath = struct { prefix: []const u8, - file_source: std.Build.LazyPath, + lazy_path: std.Build.LazyPath, }; pub const Output = struct { @@ -213,9 +213,9 @@ pub fn addPrefixedFileArg(self: *Run, prefix: []const u8, lp: std.Build.LazyPath const prefixed_file_source: PrefixedLazyPath = .{ .prefix = b.dupe(prefix), - .file_source = lp.dupe(b), + .lazy_path = lp.dupe(b), }; - self.argv.append(.{ .file_source = prefixed_file_source }) catch @panic("OOM"); + self.argv.append(.{ .lazy_path = prefixed_file_source }) catch @panic("OOM"); lp.addStepDependencies(&self.step); } @@ -234,7 +234,7 @@ pub fn addPrefixedDirectoryArg(self: *Run, prefix: []const u8, directory_source: const prefixed_directory_source: PrefixedLazyPath = .{ .prefix = b.dupe(prefix), - .file_source = directory_source.dupe(b), + .lazy_path = directory_source.dupe(b), }; self.argv.append(.{ .directory_source = prefixed_directory_source }) catch @panic("OOM"); directory_source.addStepDependencies(&self.step); @@ -252,7 +252,7 @@ pub fn addArgs(self: *Run, args: []const []const u8) void { pub fn setStdIn(self: *Run, stdin: StdIn) void { switch (stdin) { - .file_source => |file_source| file_source.addStepDependencies(&self.step), + .lazy_path => |lazy_path| lazy_path.addStepDependencies(&self.step), .bytes, .none => {}, } self.stdin = stdin; @@ -444,14 +444,14 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { try argv_list.append(bytes); man.hash.addBytes(bytes); }, - .file_source => |file| { - const file_path = file.file_source.getPath(b); + .lazy_path => |file| { + const file_path = file.lazy_path.getPath(b); try argv_list.append(b.fmt("{s}{s}", .{ file.prefix, file_path })); man.hash.addBytes(file.prefix); _ = try man.addFile(file_path, null); }, .directory_source => |file| { - const file_path = file.file_source.getPath(b); + const file_path = file.lazy_path.getPath(b); try argv_list.append(b.fmt("{s}{s}", .{ file.prefix, file_path })); man.hash.addBytes(file.prefix); man.hash.addBytes(file_path); @@ -486,8 +486,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { .bytes => |bytes| { man.hash.addBytes(bytes); }, - .file_source => |file_source| { - const file_path = file_source.getPath(b); + .lazy_path => |lazy_path| { + const file_path = lazy_path.getPath(b); _ = try man.addFile(file_path, null); }, .none => {}, @@ -1186,8 +1186,8 @@ fn evalGeneric(self: *Run, child: *std.process.Child) !StdIoResult { child.stdin.?.close(); child.stdin = null; }, - .file_source => |file_source| { - const path = file_source.getPath(self.step.owner); + .lazy_path => |lazy_path| { + const path = lazy_path.getPath(self.step.owner); const file = self.step.owner.build_root.handle.openFile(path, .{}) catch |err| { return self.step.fail("unable to open stdin file: {s}", .{@errorName(err)}); }; From acbb6418c3899eb79aea98e7f5d3173298716377 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 30 Jul 2023 18:44:31 -0700 Subject: [PATCH 10/10] move docgen.zig to tools/ --- build.zig | 2 +- {doc => tools}/docgen.zig | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename {doc => tools}/docgen.zig (100%) diff --git a/build.zig b/build.zig index 7458ec97a22d..88267bb55573 100644 --- a/build.zig +++ b/build.zig @@ -36,7 +36,7 @@ pub fn build(b: *std.Build) !void { const docgen_exe = b.addExecutable(.{ .name = "docgen", - .root_source_file = .{ .path = "doc/docgen.zig" }, + .root_source_file = .{ .path = "tools/docgen.zig" }, .target = .{}, .optimize = .Debug, }); diff --git a/doc/docgen.zig b/tools/docgen.zig similarity index 100% rename from doc/docgen.zig rename to tools/docgen.zig