authorgravatar for xq@random-projects.netFelix "xq" Queißner <xq@random-projects.net> 2021-02-22 22:11:30+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-06-11 10:33:26+03:00
log8501bb04ada0a29b66ba2d87ec956a4cdff46cee
tree11f16f6e53d51a7f54182746b85296089a228372
parent4ed567d12e2670596ebc4bf42c710b4e3210e77e

Adds a lot of missing dupes, some more snakes.


2 files changed, 83 insertions(+), 76 deletions(-)

lib/std/build.zig+81-74
......@@ -206,7 +206,7 @@ pub const Builder = struct {
206206
207207 fn convertOptionalPathToFileSource(path: ?[]const u8) ?FileSource {
208208 return if (path) |p|
209 FileSource.relative(p)
209 FileSource{ .path = p }
210210 else
211211 null;
212212 }
......@@ -246,7 +246,7 @@ pub const Builder = struct {
246246 }
247247
248248 pub fn addTestSource(self: *Builder, root_src: FileSource) *LibExeObjStep {
249 return LibExeObjStep.createTest(self, "test", root_src);
249 return LibExeObjStep.createTest(self, "test", root_src.dupe(self));
250250 }
251251
252252 pub fn addAssemble(self: *Builder, name: []const u8, src: []const u8) *LibExeObjStep {
......@@ -255,7 +255,7 @@ pub const Builder = struct {
255255
256256 pub fn addAssembleSource(self: *Builder, name: []const u8, src: FileSource) *LibExeObjStep {
257257 const obj_step = LibExeObjStep.createObject(self, name, null);
258 obj_step.addAssemblyFileSource(src);
258 obj_step.addAssemblyFileSource(src.dupe(self));
259259 return obj_step;
260260 }
261261
......@@ -341,7 +341,7 @@ pub const Builder = struct {
341341 }
342342
343343 pub fn addTranslateC(self: *Builder, source: FileSource) *TranslateCStep {
344 return TranslateCStep.create(self, source);
344 return TranslateCStep.create(self, source.dupe(self));
345345 }
346346
347347 pub fn version(self: *const Builder, major: u32, minor: u32, patch: u32) LibExeObjStep.SharedLibKind {
......@@ -898,18 +898,18 @@ pub const Builder = struct {
898898 }
899899
900900 ///`dest_rel_path` is relative to install prefix path
901 pub fn addInstallFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep {
902 return self.addInstallFileWithDir(FileSource.relative(src_path), .Prefix, dest_rel_path);
901 pub fn addInstallFile(self: *Builder, source: FileSource, dest_rel_path: []const u8) *InstallFileStep {
902 return self.addInstallFileWithDir(source.dupe(self), .Prefix, dest_rel_path);
903903 }
904904
905905 ///`dest_rel_path` is relative to bin path
906 pub fn addInstallBinFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep {
907 return self.addInstallFileWithDir(FileSource.relative(src_path), .Bin, dest_rel_path);
906 pub fn addInstallBinFile(self: *Builder, source: FileSource, dest_rel_path: []const u8) *InstallFileStep {
907 return self.addInstallFileWithDir(source.dupe(self), .Bin, dest_rel_path);
908908 }
909909
910910 ///`dest_rel_path` is relative to lib path
911 pub fn addInstallLibFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep {
912 return self.addInstallFileWithDir(FileSource.relative(src_path), .Lib, dest_rel_path);
911 pub fn addInstallLibFile(self: *Builder, source: FileSource, dest_rel_path: []const u8) *InstallFileStep {
912 return self.addInstallFileWithDir(source.dupe(self), .Lib, dest_rel_path);
913913 }
914914
915915 pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) *InstallRawStep {
......@@ -926,7 +926,7 @@ pub const Builder = struct {
926926 panic("dest_rel_path must be non-empty", .{});
927927 }
928928 const install_step = self.allocator.create(InstallFileStep) catch unreachable;
929 install_step.* = InstallFileStep.init(self, source, install_dir, dest_rel_path);
929 install_step.* = InstallFileStep.init(self, source.dupe(self), install_dir, dest_rel_path);
930930 return install_step;
931931 }
932932
......@@ -1236,12 +1236,20 @@ pub const GeneratedFile = struct {
12361236 }
12371237};
12381238
1239/// A file source is a reference to an existing or future file.
1240///
12391241pub const FileSource = union(enum) {
1240 /// Relative to build root
1242 /// A plain file path, relative to build root.
12411243 path: []const u8,
1244
1245 /// A file that is generated by an interface. Those files usually are
1246 /// not available until built by a build step.
12421247 generated: *const GeneratedFile,
12431248
1249 /// Returns a new file source that will have a relative path to the build root guaranteed.
1250 /// This should be preferred over setting `.path` directly as it documents that the files are in the project directory.
12441251 pub fn relative(path: []const u8) FileSource {
1252 std.debug.assert(!std.fs.path.isAbsolute(path));
12451253 return FileSource{ .path = path };
12461254 }
12471255
......@@ -1254,6 +1262,7 @@ pub const FileSource = union(enum) {
12541262 };
12551263 }
12561264
1265 /// Adds dependencies this file source implies to the given step.
12571266 pub fn addStepDependencies(self: FileSource, step: *Step) void {
12581267 switch (self) {
12591268 .path => {},
......@@ -1271,6 +1280,7 @@ pub const FileSource = union(enum) {
12711280 return path;
12721281 }
12731282
1283 /// Duplicates the file source for a given builder.
12741284 pub fn dupe(self: FileSource, b: *Builder) FileSource {
12751285 return switch (self) {
12761286 .path => |p| .{ .path = b.dupePath(p) },
......@@ -1284,10 +1294,9 @@ const BuildOptionArtifactArg = struct {
12841294 artifact: *LibExeObjStep,
12851295};
12861296
1287const BuildOptionWriteFileArg = struct {
1297const BuildOptionFileSourceArg = struct {
12881298 name: []const u8,
1289 write_file: *WriteFileStep,
1290 basename: []const u8,
1299 source: FileSource,
12911300};
12921301
12931302pub const LibExeObjStep = struct {
......@@ -1295,7 +1304,7 @@ pub const LibExeObjStep = struct {
12951304 builder: *Builder,
12961305 name: []const u8,
12971306 target: CrossTarget = CrossTarget{},
1298 linker_script: ?[]const u8 = null,
1307 linker_script: ?FileSource = null,
12991308 version_script: ?[]const u8 = null,
13001309 out_filename: []const u8,
13011310 is_dynamic: bool,
......@@ -1338,7 +1347,7 @@ pub const LibExeObjStep = struct {
13381347 packages: ArrayList(Pkg),
13391348 build_options_contents: std.ArrayList(u8),
13401349 build_options_artifact_args: std.ArrayList(BuildOptionArtifactArg),
1341 build_options_write_file_args: std.ArrayList(BuildOptionWriteFileArg),
1350 build_options_file_source_args: std.ArrayList(BuildOptionFileSourceArg),
13421351
13431352 object_src: []const u8,
13441353
......@@ -1358,7 +1367,7 @@ pub const LibExeObjStep = struct {
13581367 /// Base address for an executable image.
13591368 image_base: ?u64 = null,
13601369
1361 libc_file: ?[]const u8 = null,
1370 libc_file: ?FileSource = null,
13621371
13631372 valgrind_support: ?bool = null,
13641373
......@@ -1407,18 +1416,18 @@ pub const LibExeObjStep = struct {
14071416 want_lto: ?bool = null,
14081417
14091418 const LinkObject = union(enum) {
1410 StaticPath: []const u8,
1411 OtherStep: *LibExeObjStep,
1412 SystemLib: []const u8,
1413 AssemblyFile: FileSource,
1414 CSourceFile: *CSourceFile,
1415 CSourceFiles: *CSourceFiles,
1419 static_path: FileSource,
1420 other_step: *LibExeObjStep,
1421 system_lib: []const u8,
1422 assembly_file: FileSource,
1423 c_source_file: *CSourceFile,
1424 c_source_files: *CSourceFiles,
14161425 };
14171426
14181427 const IncludeDir = union(enum) {
1419 RawPath: []const u8,
1420 RawPathSystem: []const u8,
1421 OtherStep: *LibExeObjStep,
1428 raw_path: []const u8,
1429 raw_path_system: []const u8,
1430 other_step: *LibExeObjStep,
14221431 };
14231432
14241433 const Kind = enum {
......@@ -1508,7 +1517,7 @@ pub const LibExeObjStep = struct {
15081517 .object_src = undefined,
15091518 .build_options_contents = std.ArrayList(u8).init(builder.allocator),
15101519 .build_options_artifact_args = std.ArrayList(BuildOptionArtifactArg).init(builder.allocator),
1511 .build_options_write_file_args = std.ArrayList(BuildOptionWriteFileArg).init(builder.allocator),
1520 .build_options_file_source_args = std.ArrayList(BuildOptionFileSourceArg).init(builder.allocator),
15121521 .c_std = Builder.CStd.C99,
15131522 .override_lib_dir = null,
15141523 .main_pkg_path = null,
......@@ -1613,8 +1622,8 @@ pub const LibExeObjStep = struct {
16131622 return run_step;
16141623 }
16151624
1616 pub fn setLinkerScriptPath(self: *LibExeObjStep, path: []const u8) void {
1617 self.linker_script = self.builder.dupePath(path);
1625 pub fn setLinkerScriptPath(self: *LibExeObjStep, source: FileSource) void {
1626 self.linker_script = source.dupe(self.builder);
16181627 }
16191628
16201629 pub fn linkFramework(self: *LibExeObjStep, framework_name: []const u8) void {
......@@ -1633,7 +1642,7 @@ pub const LibExeObjStep = struct {
16331642 }
16341643 for (self.link_objects.items) |link_object| {
16351644 switch (link_object) {
1636 LinkObject.SystemLib => |n| if (mem.eql(u8, n, name)) return true,
1645 .system_lib => |n| if (mem.eql(u8, n, name)) return true,
16371646 else => continue,
16381647 }
16391648 }
......@@ -1658,7 +1667,7 @@ pub const LibExeObjStep = struct {
16581667 pub fn linkLibC(self: *LibExeObjStep) void {
16591668 if (!self.is_linking_libc) {
16601669 self.is_linking_libc = true;
1661 self.link_objects.append(LinkObject{ .SystemLib = "c" }) catch unreachable;
1670 self.link_objects.append(LinkObject{ .system_lib = "c" }) catch unreachable;
16621671 }
16631672 }
16641673
......@@ -1677,7 +1686,7 @@ pub const LibExeObjStep = struct {
16771686 /// This one has no integration with anything, it just puts -lname on the command line.
16781687 /// Prefer to use `linkSystemLibrary` instead.
16791688 pub fn linkSystemLibraryName(self: *LibExeObjStep, name: []const u8) void {
1680 self.link_objects.append(LinkObject{ .SystemLib = self.builder.dupe(name) }) catch unreachable;
1689 self.link_objects.append(LinkObject{ .system_lib = self.builder.dupe(name) }) catch unreachable;
16811690 }
16821691
16831692 /// This links against a system library, exclusively using pkg-config to find the library.
......@@ -1817,7 +1826,7 @@ pub const LibExeObjStep = struct {
18171826 .files = files_copy,
18181827 .flags = flags_copy,
18191828 };
1820 self.link_objects.append(LinkObject{ .CSourceFiles = c_source_files }) catch unreachable;
1829 self.link_objects.append(LinkObject{ .c_source_files = c_source_files }) catch unreachable;
18211830 }
18221831
18231832 pub fn addCSourceFile(self: *LibExeObjStep, file: []const u8, flags: []const []const u8) void {
......@@ -1830,7 +1839,8 @@ pub const LibExeObjStep = struct {
18301839 pub fn addCSourceFileSource(self: *LibExeObjStep, source: CSourceFile) void {
18311840 const c_source_file = self.builder.allocator.create(CSourceFile) catch unreachable;
18321841 c_source_file.* = source.dupe(self.builder);
1833 self.link_objects.append(LinkObject{ .CSourceFile = c_source_file }) catch unreachable;
1842 self.link_objects.append(LinkObject{ .c_source_file = c_source_file }) catch unreachable;
1843 source.source.addStepDependencies(&self.step);
18341844 }
18351845
18361846 pub fn setVerboseLink(self: *LibExeObjStep, value: bool) void {
......@@ -1853,8 +1863,8 @@ pub const LibExeObjStep = struct {
18531863 self.main_pkg_path = self.builder.dupePath(dir_path);
18541864 }
18551865
1856 pub fn setLibCFile(self: *LibExeObjStep, libc_file: ?[]const u8) void {
1857 self.libc_file = if (libc_file) |f| self.builder.dupe(f) else null;
1866 pub fn setLibCFile(self: *LibExeObjStep, libc_file: ?FileSource) void {
1867 self.libc_file = if (libc_file) |f| f.dupe(self.builder) else null;
18581868 }
18591869
18601870 /// Unless setOutputDir was called, this function must be called only in
......@@ -1900,18 +1910,18 @@ pub const LibExeObjStep = struct {
19001910
19011911 pub fn addAssemblyFile(self: *LibExeObjStep, path: []const u8) void {
19021912 self.link_objects.append(LinkObject{
1903 .AssemblyFile = .{ .path = self.builder.dupe(path) },
1913 .assembly_file = .{ .path = self.builder.dupe(path) },
19041914 }) catch unreachable;
19051915 }
19061916
19071917 pub fn addAssemblyFileSource(self: *LibExeObjStep, source: FileSource) void {
19081918 const source_duped = source.dupe(self.builder);
1909 self.link_objects.append(LinkObject{ .AssemblyFile = source_duped }) catch unreachable;
1919 self.link_objects.append(LinkObject{ .assembly_file = source_duped }) catch unreachable;
19101920 source_duped.addStepDependencies(&self.step);
19111921 }
19121922
1913 pub fn addObjectFile(self: *LibExeObjStep, path: []const u8) void {
1914 self.link_objects.append(LinkObject{ .StaticPath = self.builder.dupe(path) }) catch unreachable;
1923 pub fn addObjectFile(self: *LibExeObjStep, source: FileSource) void {
1924 self.link_objects.append(LinkObject{ .static_path = source.dupe(self.builder) }) catch unreachable;
19151925 }
19161926
19171927 pub fn addObject(self: *LibExeObjStep, obj: *LibExeObjStep) void {
......@@ -2020,26 +2030,24 @@ pub const LibExeObjStep = struct {
20202030 /// The value is the path in the cache dir.
20212031 /// Adds a dependency automatically.
20222032 /// basename refers to the basename of the WriteFileStep
2023 pub fn addBuildOptionWriteFile(
2033 pub fn addBuildOptionFileSource(
20242034 self: *LibExeObjStep,
20252035 name: []const u8,
2026 write_file: *WriteFileStep,
2027 basename: []const u8,
2036 source: FileSource,
20282037 ) void {
2029 self.build_options_write_file_args.append(.{
2038 self.build_options_file_source_args.append(.{
20302039 .name = name,
2031 .write_file = write_file,
2032 .basename = basename,
2040 .source = source.dupe(self.builder),
20332041 }) catch unreachable;
2034 self.step.dependOn(&write_file.step);
2042 source.addStepDependencies(&self.step);
20352043 }
20362044
20372045 pub fn addSystemIncludeDir(self: *LibExeObjStep, path: []const u8) void {
2038 self.include_dirs.append(IncludeDir{ .RawPathSystem = self.builder.dupe(path) }) catch unreachable;
2046 self.include_dirs.append(IncludeDir{ .raw_path_system = self.builder.dupe(path) }) catch unreachable;
20392047 }
20402048
20412049 pub fn addIncludeDir(self: *LibExeObjStep, path: []const u8) void {
2042 self.include_dirs.append(IncludeDir{ .RawPath = self.builder.dupe(path) }) catch unreachable;
2050 self.include_dirs.append(IncludeDir{ .raw_path = self.builder.dupe(path) }) catch unreachable;
20432051 }
20442052
20452053 pub fn addLibPath(self: *LibExeObjStep, path: []const u8) void {
......@@ -2093,7 +2101,7 @@ pub const LibExeObjStep = struct {
20932101
20942102 const include_path = try fs.path.join(allocator, &[_][]const u8{ root, "installed", triplet, "include" });
20952103 errdefer allocator.free(include_path);
2096 try self.include_dirs.append(IncludeDir{ .RawPath = include_path });
2104 try self.include_dirs.append(IncludeDir{ .raw_path = include_path });
20972105
20982106 const lib_path = try fs.path.join(allocator, &[_][]const u8{ root, "installed", triplet, "lib" });
20992107 try self.lib_paths.append(lib_path);
......@@ -2114,13 +2122,13 @@ pub const LibExeObjStep = struct {
21142122
21152123 fn linkLibraryOrObject(self: *LibExeObjStep, other: *LibExeObjStep) void {
21162124 self.step.dependOn(&other.step);
2117 self.link_objects.append(LinkObject{ .OtherStep = other }) catch unreachable;
2118 self.include_dirs.append(IncludeDir{ .OtherStep = other }) catch unreachable;
2125 self.link_objects.append(LinkObject{ .other_step = other }) catch unreachable;
2126 self.include_dirs.append(IncludeDir{ .other_step = other }) catch unreachable;
21192127
21202128 // Inherit dependency on system libraries
21212129 for (other.link_objects.items) |link_object| {
21222130 switch (link_object) {
2123 .SystemLib => |name| self.linkSystemLibrary(name),
2131 .system_lib => |name| self.linkSystemLibrary(name),
21242132 else => continue,
21252133 }
21262134 }
......@@ -2187,11 +2195,9 @@ pub const LibExeObjStep = struct {
21872195 var prev_has_extra_flags = false;
21882196 for (self.link_objects.items) |link_object| {
21892197 switch (link_object) {
2190 .StaticPath => |static_path| {
2191 try zig_args.append(builder.pathFromRoot(static_path));
2192 },
2198 .static_path => |static_path| try zig_args.append(static_path.getPath(builder)),
21932199
2194 .OtherStep => |other| switch (other.kind) {
2200 .other_step => |other| switch (other.kind) {
21952201 .Exe => unreachable,
21962202 .Test => unreachable,
21972203 .Obj => {
......@@ -2209,10 +2215,11 @@ pub const LibExeObjStep = struct {
22092215 }
22102216 },
22112217 },
2212 .SystemLib => |name| {
2218 .system_lib => |name| {
22132219 try zig_args.append(builder.fmt("-l{s}", .{name}));
22142220 },
2215 .AssemblyFile => |asm_file| {
2221
2222 .assembly_file => |asm_file| {
22162223 if (prev_has_extra_flags) {
22172224 try zig_args.append("-extra-cflags");
22182225 try zig_args.append("--");
......@@ -2221,7 +2228,7 @@ pub const LibExeObjStep = struct {
22212228 try zig_args.append(asm_file.getPath(builder));
22222229 },
22232230
2224 .CSourceFile => |c_source_file| {
2231 .c_source_file => |c_source_file| {
22252232 if (c_source_file.args.len == 0) {
22262233 if (prev_has_extra_flags) {
22272234 try zig_args.append("-cflags");
......@@ -2238,7 +2245,7 @@ pub const LibExeObjStep = struct {
22382245 try zig_args.append(c_source_file.source.getPath(builder));
22392246 },
22402247
2241 .CSourceFiles => |c_source_files| {
2248 .c_source_files => |c_source_files| {
22422249 if (c_source_files.flags.len == 0) {
22432250 if (prev_has_extra_flags) {
22442251 try zig_args.append("-cflags");
......@@ -2261,7 +2268,7 @@ pub const LibExeObjStep = struct {
22612268
22622269 if (self.build_options_contents.items.len > 0 or
22632270 self.build_options_artifact_args.items.len > 0 or
2264 self.build_options_write_file_args.items.len > 0)
2271 self.build_options_file_source_args.items.len > 0)
22652272 {
22662273 // Render build artifact and write file options at the last minute, now that the path is known.
22672274 //
......@@ -2274,11 +2281,11 @@ pub const LibExeObjStep = struct {
22742281 self.builder.pathFromRoot(item.artifact.getOutputPath()),
22752282 );
22762283 }
2277 for (self.build_options_write_file_args.items) |item| {
2284 for (self.build_options_file_source_args.items) |item| {
22782285 self.addBuildOption(
22792286 []const u8,
22802287 item.name,
2281 self.builder.pathFromRoot(item.write_file.getOutputPath(item.basename)),
2288 item.source.getPath(self.builder),
22822289 );
22832290 }
22842291
......@@ -2349,7 +2356,7 @@ pub const LibExeObjStep = struct {
23492356
23502357 if (self.libc_file) |libc_file| {
23512358 try zig_args.append("--libc");
2352 try zig_args.append(builder.pathFromRoot(libc_file));
2359 try zig_args.append(libc_file.getPath(self.builder));
23532360 }
23542361
23552362 switch (self.build_mode) {
......@@ -2451,7 +2458,7 @@ pub const LibExeObjStep = struct {
24512458
24522459 if (self.linker_script) |linker_script| {
24532460 try zig_args.append("--script");
2454 try zig_args.append(builder.pathFromRoot(linker_script));
2461 try zig_args.append(linker_script.getPath(builder));
24552462 }
24562463
24572464 if (self.version_script) |version_script| {
......@@ -2526,15 +2533,15 @@ pub const LibExeObjStep = struct {
25262533
25272534 for (self.include_dirs.items) |include_dir| {
25282535 switch (include_dir) {
2529 .RawPath => |include_path| {
2536 .raw_path => |include_path| {
25302537 try zig_args.append("-I");
25312538 try zig_args.append(self.builder.pathFromRoot(include_path));
25322539 },
2533 .RawPathSystem => |include_path| {
2540 .raw_path_system => |include_path| {
25342541 try zig_args.append("-isystem");
25352542 try zig_args.append(self.builder.pathFromRoot(include_path));
25362543 },
2537 .OtherStep => |other| if (other.emit_h) {
2544 .other_step => |other| if (other.emit_h) {
25382545 const h_path = other.getOutputHPath();
25392546 try zig_args.append("-isystem");
25402547 try zig_args.append(fs.path.dirname(h_path).?);
......@@ -3086,11 +3093,11 @@ test "Builder.dupePkg()" {
30863093
30873094 var pkg_dep = Pkg{
30883095 .name = "pkg_dep",
3089 .path = FileSource.relative("/not/a/pkg_dep.zig"),
3096 .path = .{ .path = "/not/a/pkg_dep.zig" },
30903097 };
30913098 var pkg_top = Pkg{
30923099 .name = "pkg_top",
3093 .path = FileSource.relative("/not/a/pkg_top.zig"),
3100 .path = .{ .path = "/not/a/pkg_top.zig" },
30943101 .dependencies = &[_]Pkg{pkg_dep},
30953102 };
30963103 const dupe = builder.dupePkg(pkg_top);
......@@ -3168,11 +3175,11 @@ test "LibExeObjStep.addPackage" {
31683175
31693176 const pkg_dep = Pkg{
31703177 .name = "pkg_dep",
3171 .path = FileSource.relative("/not/a/pkg_dep.zig"),
3178 .path = .{ .path = "/not/a/pkg_dep.zig" },
31723179 };
31733180 const pkg_top = Pkg{
31743181 .name = "pkg_dep",
3175 .path = FileSource.relative("/not/a/pkg_top.zig"),
3182 .path = .{ .path = "/not/a/pkg_top.zig" },
31763183 .dependencies = &[_]Pkg{pkg_dep},
31773184 };
31783185
lib/std/build/run.zig+2-2
......@@ -71,7 +71,7 @@ pub const RunStep = struct {
7171
7272 pub fn addFileSourceArg(self: *RunStep, file_source: build.FileSource) void {
7373 self.argv.append(Arg{
74 .file_source = file_source,
74 .file_source = file_source.dupe(self.builder),
7575 }) catch unreachable;
7676 file_source.addStepDependencies(&self.step);
7777 }
......@@ -314,7 +314,7 @@ pub const RunStep = struct {
314314 fn addPathForDynLibs(self: *RunStep, artifact: *LibExeObjStep) void {
315315 for (artifact.link_objects.items) |link_object| {
316316 switch (link_object) {
317 .OtherStep => |other| {
317 .other_step => |other| {
318318 if (other.target.isWindows() and other.isDynamicLibrary()) {
319319 self.addPathDir(fs.path.dirname(other.getOutputPath()).?);
320320 self.addPathForDynLibs(other);