Commit 8501bb04ad

Felix (xq) Queißner <git@mq32.de>
2021-02-22 22:11:30
Adds a lot of missing dupes, some more snakes.
1 parent 4ed567d
Changed files (2)
lib
lib/std/build/run.zig
@@ -71,7 +71,7 @@ pub const RunStep = struct {
 
     pub fn addFileSourceArg(self: *RunStep, file_source: build.FileSource) void {
         self.argv.append(Arg{
-            .file_source = file_source,
+            .file_source = file_source.dupe(self.builder),
         }) catch unreachable;
         file_source.addStepDependencies(&self.step);
     }
@@ -314,7 +314,7 @@ pub const RunStep = struct {
     fn addPathForDynLibs(self: *RunStep, artifact: *LibExeObjStep) void {
         for (artifact.link_objects.items) |link_object| {
             switch (link_object) {
-                .OtherStep => |other| {
+                .other_step => |other| {
                     if (other.target.isWindows() and other.isDynamicLibrary()) {
                         self.addPathDir(fs.path.dirname(other.getOutputPath()).?);
                         self.addPathForDynLibs(other);
lib/std/build.zig
@@ -206,7 +206,7 @@ pub const Builder = struct {
 
     fn convertOptionalPathToFileSource(path: ?[]const u8) ?FileSource {
         return if (path) |p|
-            FileSource.relative(p)
+            FileSource{ .path = p }
         else
             null;
     }
@@ -246,7 +246,7 @@ pub const Builder = struct {
     }
 
     pub fn addTestSource(self: *Builder, root_src: FileSource) *LibExeObjStep {
-        return LibExeObjStep.createTest(self, "test", root_src);
+        return LibExeObjStep.createTest(self, "test", root_src.dupe(self));
     }
 
     pub fn addAssemble(self: *Builder, name: []const u8, src: []const u8) *LibExeObjStep {
@@ -255,7 +255,7 @@ pub const Builder = struct {
 
     pub fn addAssembleSource(self: *Builder, name: []const u8, src: FileSource) *LibExeObjStep {
         const obj_step = LibExeObjStep.createObject(self, name, null);
-        obj_step.addAssemblyFileSource(src);
+        obj_step.addAssemblyFileSource(src.dupe(self));
         return obj_step;
     }
 
@@ -341,7 +341,7 @@ pub const Builder = struct {
     }
 
     pub fn addTranslateC(self: *Builder, source: FileSource) *TranslateCStep {
-        return TranslateCStep.create(self, source);
+        return TranslateCStep.create(self, source.dupe(self));
     }
 
     pub fn version(self: *const Builder, major: u32, minor: u32, patch: u32) LibExeObjStep.SharedLibKind {
@@ -898,18 +898,18 @@ pub const Builder = struct {
     }
 
     ///`dest_rel_path` is relative to install prefix path
-    pub fn addInstallFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep {
-        return self.addInstallFileWithDir(FileSource.relative(src_path), .Prefix, dest_rel_path);
+    pub fn addInstallFile(self: *Builder, source: FileSource, dest_rel_path: []const u8) *InstallFileStep {
+        return self.addInstallFileWithDir(source.dupe(self), .Prefix, dest_rel_path);
     }
 
     ///`dest_rel_path` is relative to bin path
-    pub fn addInstallBinFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep {
-        return self.addInstallFileWithDir(FileSource.relative(src_path), .Bin, dest_rel_path);
+    pub fn addInstallBinFile(self: *Builder, source: FileSource, dest_rel_path: []const u8) *InstallFileStep {
+        return self.addInstallFileWithDir(source.dupe(self), .Bin, dest_rel_path);
     }
 
     ///`dest_rel_path` is relative to lib path
-    pub fn addInstallLibFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep {
-        return self.addInstallFileWithDir(FileSource.relative(src_path), .Lib, dest_rel_path);
+    pub fn addInstallLibFile(self: *Builder, source: FileSource, dest_rel_path: []const u8) *InstallFileStep {
+        return self.addInstallFileWithDir(source.dupe(self), .Lib, dest_rel_path);
     }
 
     pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) *InstallRawStep {
@@ -926,7 +926,7 @@ pub const Builder = struct {
             panic("dest_rel_path must be non-empty", .{});
         }
         const install_step = self.allocator.create(InstallFileStep) catch unreachable;
-        install_step.* = InstallFileStep.init(self, source, install_dir, dest_rel_path);
+        install_step.* = InstallFileStep.init(self, source.dupe(self), install_dir, dest_rel_path);
         return install_step;
     }
 
@@ -1236,12 +1236,20 @@ pub const GeneratedFile = struct {
     }
 };
 
+/// A file source is a reference to an existing or future file.
+///
 pub const FileSource = union(enum) {
-    /// Relative to build root
+    /// A plain file path, relative to build root.
     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,
 
+    /// 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 {
+        std.debug.assert(!std.fs.path.isAbsolute(path));
         return FileSource{ .path = path };
     }
 
@@ -1254,6 +1262,7 @@ pub const FileSource = union(enum) {
         };
     }
 
+    /// Adds dependencies this file source implies to the given step.
     pub fn addStepDependencies(self: FileSource, step: *Step) void {
         switch (self) {
             .path => {},
@@ -1271,6 +1280,7 @@ pub const FileSource = union(enum) {
         return path;
     }
 
+    /// Duplicates the file source for a given builder.
     pub fn dupe(self: FileSource, b: *Builder) FileSource {
         return switch (self) {
             .path => |p| .{ .path = b.dupePath(p) },
@@ -1284,10 +1294,9 @@ const BuildOptionArtifactArg = struct {
     artifact: *LibExeObjStep,
 };
 
-const BuildOptionWriteFileArg = struct {
+const BuildOptionFileSourceArg = struct {
     name: []const u8,
-    write_file: *WriteFileStep,
-    basename: []const u8,
+    source: FileSource,
 };
 
 pub const LibExeObjStep = struct {
@@ -1295,7 +1304,7 @@ pub const LibExeObjStep = struct {
     builder: *Builder,
     name: []const u8,
     target: CrossTarget = CrossTarget{},
-    linker_script: ?[]const u8 = null,
+    linker_script: ?FileSource = null,
     version_script: ?[]const u8 = null,
     out_filename: []const u8,
     is_dynamic: bool,
@@ -1338,7 +1347,7 @@ pub const LibExeObjStep = struct {
     packages: ArrayList(Pkg),
     build_options_contents: std.ArrayList(u8),
     build_options_artifact_args: std.ArrayList(BuildOptionArtifactArg),
-    build_options_write_file_args: std.ArrayList(BuildOptionWriteFileArg),
+    build_options_file_source_args: std.ArrayList(BuildOptionFileSourceArg),
 
     object_src: []const u8,
 
@@ -1358,7 +1367,7 @@ pub const LibExeObjStep = struct {
     /// Base address for an executable image.
     image_base: ?u64 = null,
 
-    libc_file: ?[]const u8 = null,
+    libc_file: ?FileSource = null,
 
     valgrind_support: ?bool = null,
 
@@ -1407,18 +1416,18 @@ pub const LibExeObjStep = struct {
     want_lto: ?bool = null,
 
     const LinkObject = union(enum) {
-        StaticPath: []const u8,
-        OtherStep: *LibExeObjStep,
-        SystemLib: []const u8,
-        AssemblyFile: FileSource,
-        CSourceFile: *CSourceFile,
-        CSourceFiles: *CSourceFiles,
+        static_path: FileSource,
+        other_step: *LibExeObjStep,
+        system_lib: []const u8,
+        assembly_file: FileSource,
+        c_source_file: *CSourceFile,
+        c_source_files: *CSourceFiles,
     };
 
     const IncludeDir = union(enum) {
-        RawPath: []const u8,
-        RawPathSystem: []const u8,
-        OtherStep: *LibExeObjStep,
+        raw_path: []const u8,
+        raw_path_system: []const u8,
+        other_step: *LibExeObjStep,
     };
 
     const Kind = enum {
@@ -1508,7 +1517,7 @@ pub const LibExeObjStep = struct {
             .object_src = undefined,
             .build_options_contents = std.ArrayList(u8).init(builder.allocator),
             .build_options_artifact_args = std.ArrayList(BuildOptionArtifactArg).init(builder.allocator),
-            .build_options_write_file_args = std.ArrayList(BuildOptionWriteFileArg).init(builder.allocator),
+            .build_options_file_source_args = std.ArrayList(BuildOptionFileSourceArg).init(builder.allocator),
             .c_std = Builder.CStd.C99,
             .override_lib_dir = null,
             .main_pkg_path = null,
@@ -1613,8 +1622,8 @@ pub const LibExeObjStep = struct {
         return run_step;
     }
 
-    pub fn setLinkerScriptPath(self: *LibExeObjStep, path: []const u8) void {
-        self.linker_script = self.builder.dupePath(path);
+    pub fn setLinkerScriptPath(self: *LibExeObjStep, source: FileSource) void {
+        self.linker_script = source.dupe(self.builder);
     }
 
     pub fn linkFramework(self: *LibExeObjStep, framework_name: []const u8) void {
@@ -1633,7 +1642,7 @@ pub const LibExeObjStep = struct {
         }
         for (self.link_objects.items) |link_object| {
             switch (link_object) {
-                LinkObject.SystemLib => |n| if (mem.eql(u8, n, name)) return true,
+                .system_lib => |n| if (mem.eql(u8, n, name)) return true,
                 else => continue,
             }
         }
@@ -1658,7 +1667,7 @@ pub const LibExeObjStep = struct {
     pub fn linkLibC(self: *LibExeObjStep) void {
         if (!self.is_linking_libc) {
             self.is_linking_libc = true;
-            self.link_objects.append(LinkObject{ .SystemLib = "c" }) catch unreachable;
+            self.link_objects.append(LinkObject{ .system_lib = "c" }) catch unreachable;
         }
     }
 
@@ -1677,7 +1686,7 @@ pub const LibExeObjStep = struct {
     /// This one has no integration with anything, it just puts -lname on the command line.
     /// Prefer to use `linkSystemLibrary` instead.
     pub fn linkSystemLibraryName(self: *LibExeObjStep, name: []const u8) void {
-        self.link_objects.append(LinkObject{ .SystemLib = self.builder.dupe(name) }) catch unreachable;
+        self.link_objects.append(LinkObject{ .system_lib = self.builder.dupe(name) }) catch unreachable;
     }
 
     /// This links against a system library, exclusively using pkg-config to find the library.
@@ -1817,7 +1826,7 @@ pub const LibExeObjStep = struct {
             .files = files_copy,
             .flags = flags_copy,
         };
-        self.link_objects.append(LinkObject{ .CSourceFiles = c_source_files }) catch unreachable;
+        self.link_objects.append(LinkObject{ .c_source_files = c_source_files }) catch unreachable;
     }
 
     pub fn addCSourceFile(self: *LibExeObjStep, file: []const u8, flags: []const []const u8) void {
@@ -1830,7 +1839,8 @@ pub const LibExeObjStep = struct {
     pub fn addCSourceFileSource(self: *LibExeObjStep, source: CSourceFile) void {
         const c_source_file = self.builder.allocator.create(CSourceFile) catch unreachable;
         c_source_file.* = source.dupe(self.builder);
-        self.link_objects.append(LinkObject{ .CSourceFile = c_source_file }) catch unreachable;
+        self.link_objects.append(LinkObject{ .c_source_file = c_source_file }) catch unreachable;
+        source.source.addStepDependencies(&self.step);
     }
 
     pub fn setVerboseLink(self: *LibExeObjStep, value: bool) void {
@@ -1853,8 +1863,8 @@ pub const LibExeObjStep = struct {
         self.main_pkg_path = self.builder.dupePath(dir_path);
     }
 
-    pub fn setLibCFile(self: *LibExeObjStep, libc_file: ?[]const u8) void {
-        self.libc_file = if (libc_file) |f| self.builder.dupe(f) else null;
+    pub fn setLibCFile(self: *LibExeObjStep, libc_file: ?FileSource) void {
+        self.libc_file = if (libc_file) |f| f.dupe(self.builder) else null;
     }
 
     /// Unless setOutputDir was called, this function must be called only in
@@ -1900,18 +1910,18 @@ pub const LibExeObjStep = struct {
 
     pub fn addAssemblyFile(self: *LibExeObjStep, path: []const u8) void {
         self.link_objects.append(LinkObject{
-            .AssemblyFile = .{ .path = self.builder.dupe(path) },
+            .assembly_file = .{ .path = self.builder.dupe(path) },
         }) catch unreachable;
     }
 
     pub fn addAssemblyFileSource(self: *LibExeObjStep, source: FileSource) void {
         const source_duped = source.dupe(self.builder);
-        self.link_objects.append(LinkObject{ .AssemblyFile = source_duped }) catch unreachable;
+        self.link_objects.append(LinkObject{ .assembly_file = source_duped }) catch unreachable;
         source_duped.addStepDependencies(&self.step);
     }
 
-    pub fn addObjectFile(self: *LibExeObjStep, path: []const u8) void {
-        self.link_objects.append(LinkObject{ .StaticPath = self.builder.dupe(path) }) catch unreachable;
+    pub fn addObjectFile(self: *LibExeObjStep, source: FileSource) void {
+        self.link_objects.append(LinkObject{ .static_path = source.dupe(self.builder) }) catch unreachable;
     }
 
     pub fn addObject(self: *LibExeObjStep, obj: *LibExeObjStep) void {
@@ -2020,26 +2030,24 @@ pub const LibExeObjStep = struct {
     /// The value is the path in the cache dir.
     /// Adds a dependency automatically.
     /// basename refers to the basename of the WriteFileStep
-    pub fn addBuildOptionWriteFile(
+    pub fn addBuildOptionFileSource(
         self: *LibExeObjStep,
         name: []const u8,
-        write_file: *WriteFileStep,
-        basename: []const u8,
+        source: FileSource,
     ) void {
-        self.build_options_write_file_args.append(.{
+        self.build_options_file_source_args.append(.{
             .name = name,
-            .write_file = write_file,
-            .basename = basename,
+            .source = source.dupe(self.builder),
         }) catch unreachable;
-        self.step.dependOn(&write_file.step);
+        source.addStepDependencies(&self.step);
     }
 
     pub fn addSystemIncludeDir(self: *LibExeObjStep, path: []const u8) void {
-        self.include_dirs.append(IncludeDir{ .RawPathSystem = self.builder.dupe(path) }) catch unreachable;
+        self.include_dirs.append(IncludeDir{ .raw_path_system = self.builder.dupe(path) }) catch unreachable;
     }
 
     pub fn addIncludeDir(self: *LibExeObjStep, path: []const u8) void {
-        self.include_dirs.append(IncludeDir{ .RawPath = self.builder.dupe(path) }) catch unreachable;
+        self.include_dirs.append(IncludeDir{ .raw_path = self.builder.dupe(path) }) catch unreachable;
     }
 
     pub fn addLibPath(self: *LibExeObjStep, path: []const u8) void {
@@ -2093,7 +2101,7 @@ pub const LibExeObjStep = struct {
 
                 const include_path = try fs.path.join(allocator, &[_][]const u8{ root, "installed", triplet, "include" });
                 errdefer allocator.free(include_path);
-                try self.include_dirs.append(IncludeDir{ .RawPath = include_path });
+                try self.include_dirs.append(IncludeDir{ .raw_path = include_path });
 
                 const lib_path = try fs.path.join(allocator, &[_][]const u8{ root, "installed", triplet, "lib" });
                 try self.lib_paths.append(lib_path);
@@ -2114,13 +2122,13 @@ pub const LibExeObjStep = struct {
 
     fn linkLibraryOrObject(self: *LibExeObjStep, other: *LibExeObjStep) void {
         self.step.dependOn(&other.step);
-        self.link_objects.append(LinkObject{ .OtherStep = other }) catch unreachable;
-        self.include_dirs.append(IncludeDir{ .OtherStep = other }) catch unreachable;
+        self.link_objects.append(LinkObject{ .other_step = other }) catch unreachable;
+        self.include_dirs.append(IncludeDir{ .other_step = other }) catch unreachable;
 
         // Inherit dependency on system libraries
         for (other.link_objects.items) |link_object| {
             switch (link_object) {
-                .SystemLib => |name| self.linkSystemLibrary(name),
+                .system_lib => |name| self.linkSystemLibrary(name),
                 else => continue,
             }
         }
@@ -2187,11 +2195,9 @@ pub const LibExeObjStep = struct {
         var prev_has_extra_flags = false;
         for (self.link_objects.items) |link_object| {
             switch (link_object) {
-                .StaticPath => |static_path| {
-                    try zig_args.append(builder.pathFromRoot(static_path));
-                },
+                .static_path => |static_path| try zig_args.append(static_path.getPath(builder)),
 
-                .OtherStep => |other| switch (other.kind) {
+                .other_step => |other| switch (other.kind) {
                     .Exe => unreachable,
                     .Test => unreachable,
                     .Obj => {
@@ -2209,10 +2215,11 @@ pub const LibExeObjStep = struct {
                         }
                     },
                 },
-                .SystemLib => |name| {
+                .system_lib => |name| {
                     try zig_args.append(builder.fmt("-l{s}", .{name}));
                 },
-                .AssemblyFile => |asm_file| {
+
+                .assembly_file => |asm_file| {
                     if (prev_has_extra_flags) {
                         try zig_args.append("-extra-cflags");
                         try zig_args.append("--");
@@ -2221,7 +2228,7 @@ pub const LibExeObjStep = struct {
                     try zig_args.append(asm_file.getPath(builder));
                 },
 
-                .CSourceFile => |c_source_file| {
+                .c_source_file => |c_source_file| {
                     if (c_source_file.args.len == 0) {
                         if (prev_has_extra_flags) {
                             try zig_args.append("-cflags");
@@ -2238,7 +2245,7 @@ pub const LibExeObjStep = struct {
                     try zig_args.append(c_source_file.source.getPath(builder));
                 },
 
-                .CSourceFiles => |c_source_files| {
+                .c_source_files => |c_source_files| {
                     if (c_source_files.flags.len == 0) {
                         if (prev_has_extra_flags) {
                             try zig_args.append("-cflags");
@@ -2261,7 +2268,7 @@ pub const LibExeObjStep = struct {
 
         if (self.build_options_contents.items.len > 0 or
             self.build_options_artifact_args.items.len > 0 or
-            self.build_options_write_file_args.items.len > 0)
+            self.build_options_file_source_args.items.len > 0)
         {
             // Render build artifact and write file options at the last minute, now that the path is known.
             //
@@ -2274,11 +2281,11 @@ pub const LibExeObjStep = struct {
                     self.builder.pathFromRoot(item.artifact.getOutputPath()),
                 );
             }
-            for (self.build_options_write_file_args.items) |item| {
+            for (self.build_options_file_source_args.items) |item| {
                 self.addBuildOption(
                     []const u8,
                     item.name,
-                    self.builder.pathFromRoot(item.write_file.getOutputPath(item.basename)),
+                    item.source.getPath(self.builder),
                 );
             }
 
@@ -2349,7 +2356,7 @@ pub const LibExeObjStep = struct {
 
         if (self.libc_file) |libc_file| {
             try zig_args.append("--libc");
-            try zig_args.append(builder.pathFromRoot(libc_file));
+            try zig_args.append(libc_file.getPath(self.builder));
         }
 
         switch (self.build_mode) {
@@ -2451,7 +2458,7 @@ pub const LibExeObjStep = struct {
 
         if (self.linker_script) |linker_script| {
             try zig_args.append("--script");
-            try zig_args.append(builder.pathFromRoot(linker_script));
+            try zig_args.append(linker_script.getPath(builder));
         }
 
         if (self.version_script) |version_script| {
@@ -2526,15 +2533,15 @@ pub const LibExeObjStep = struct {
 
         for (self.include_dirs.items) |include_dir| {
             switch (include_dir) {
-                .RawPath => |include_path| {
+                .raw_path => |include_path| {
                     try zig_args.append("-I");
                     try zig_args.append(self.builder.pathFromRoot(include_path));
                 },
-                .RawPathSystem => |include_path| {
+                .raw_path_system => |include_path| {
                     try zig_args.append("-isystem");
                     try zig_args.append(self.builder.pathFromRoot(include_path));
                 },
-                .OtherStep => |other| if (other.emit_h) {
+                .other_step => |other| if (other.emit_h) {
                     const h_path = other.getOutputHPath();
                     try zig_args.append("-isystem");
                     try zig_args.append(fs.path.dirname(h_path).?);
@@ -3086,11 +3093,11 @@ test "Builder.dupePkg()" {
 
     var pkg_dep = Pkg{
         .name = "pkg_dep",
-        .path = FileSource.relative("/not/a/pkg_dep.zig"),
+        .path = .{ .path = "/not/a/pkg_dep.zig" },
     };
     var pkg_top = Pkg{
         .name = "pkg_top",
-        .path = FileSource.relative("/not/a/pkg_top.zig"),
+        .path = .{ .path = "/not/a/pkg_top.zig" },
         .dependencies = &[_]Pkg{pkg_dep},
     };
     const dupe = builder.dupePkg(pkg_top);
@@ -3168,11 +3175,11 @@ test "LibExeObjStep.addPackage" {
 
     const pkg_dep = Pkg{
         .name = "pkg_dep",
-        .path = FileSource.relative("/not/a/pkg_dep.zig"),
+        .path = .{ .path = "/not/a/pkg_dep.zig" },
     };
     const pkg_top = Pkg{
         .name = "pkg_dep",
-        .path = FileSource.relative("/not/a/pkg_top.zig"),
+        .path = .{ .path = "/not/a/pkg_top.zig" },
         .dependencies = &[_]Pkg{pkg_dep},
     };