authorgravatar for xq@random-projects.netFelix "xq" Queißner <xq@random-projects.net> 2021-02-26 11:28:23+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-06-11 10:39:50+03:00
log27bd0971bb725fd3166bcee9364d3af868a24b5d
tree0d863026c3a4f46764f56b572b2a242a41286f1c
parent07acb1ccc97329cdcc0df37234325af9b00420a3

Changes to .path instead of .getPathFn. Changes LibExeObjStep to also provide FileSource.


6 files changed, 82 insertions(+), 94 deletions(-)

lib/std/build.zig+67-56
......@@ -1228,11 +1228,12 @@ pub const GeneratedFile = struct {
12281228 /// The step that generates the file
12291229 step: *Step,
12301230
1231 /// A function that returns the absolute path to the generated file.
1232 getPathFn: fn (self: *const GeneratedFile) []const u8,
1231 /// The path to the generated file. Must be either absolute or relative to the build root.
1232 /// This value must be set in the `fn make()` of the `step` and must not be `null` afterwards.
1233 path: ?[]const u8 = null,
12331234
12341235 pub fn getPath(self: *const GeneratedFile) []const u8 {
1235 return self.getPathFn(self);
1236 return self.path orelse @panic("getPath() was called on a GeneratedFile that wasn't build yet. Is there a missing Step dependency?");
12361237 }
12371238};
12381239
......@@ -1414,6 +1415,11 @@ pub const LibExeObjStep = struct {
14141415
14151416 want_lto: ?bool = null,
14161417
1418 output_path_source: GeneratedFile,
1419 output_lib_path_source: GeneratedFile,
1420 output_h_path_source: GeneratedFile,
1421 output_pdb_path_source: GeneratedFile,
1422
14171423 const LinkObject = union(enum) {
14181424 static_path: FileSource,
14191425 other_step: *LibExeObjStep,
......@@ -1444,36 +1450,26 @@ pub const LibExeObjStep = struct {
14441450 pub const Linkage = enum { dynamic, static };
14451451
14461452 pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?FileSource, kind: SharedLibKind) *LibExeObjStep {
1447 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1448 self.* = initExtraArgs(builder, name, root_src, Kind.Lib, .dynamic, switch (kind) {
1453 return initExtraArgs(builder, name, root_src, Kind.Lib, .dynamic, switch (kind) {
14491454 .versioned => |ver| ver,
14501455 .unversioned => null,
14511456 });
1452 return self;
14531457 }
14541458
14551459 pub fn createStaticLibrary(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
1456 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1457 self.* = initExtraArgs(builder, name, root_src, Kind.Lib, .static, null);
1458 return self;
1460 return initExtraArgs(builder, name, root_src, Kind.Lib, .static, null);
14591461 }
14601462
14611463 pub fn createObject(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
1462 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1463 self.* = initExtraArgs(builder, name, root_src, Kind.Obj, .static, null);
1464 return self;
1464 return initExtraArgs(builder, name, root_src, Kind.Obj, .static, null);
14651465 }
14661466
14671467 pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?FileSource, linkage: Linkage) *LibExeObjStep {
1468 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1469 self.* = initExtraArgs(builder, name, root_src, Kind.Exe, linkage, null);
1470 return self;
1468 return initExtraArgs(builder, name, root_src, Kind.Exe, linkage, null);
14711469 }
14721470
14731471 pub fn createTest(builder: *Builder, name: []const u8, root_src: FileSource) *LibExeObjStep {
1474 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1475 self.* = initExtraArgs(builder, name, root_src, Kind.Test, .static, null);
1476 return self;
1472 return initExtraArgs(builder, name, root_src, Kind.Test, .static, null);
14771473 }
14781474
14791475 fn initExtraArgs(
......@@ -1483,13 +1479,15 @@ pub const LibExeObjStep = struct {
14831479 kind: Kind,
14841480 linkage: Linkage,
14851481 ver: ?Version,
1486 ) LibExeObjStep {
1482 ) *LibExeObjStep {
14871483 const name = builder.dupe(name_raw);
14881484 const root_src: ?FileSource = if (root_src_raw) |rsrc| rsrc.dupe(builder) else null;
14891485 if (mem.indexOf(u8, name, "/") != null or mem.indexOf(u8, name, "\\") != null) {
14901486 panic("invalid name: '{s}'. It looks like a file path, but it is supposed to be the library or application name.", .{name});
14911487 }
1492 var self = LibExeObjStep{
1488
1489 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1490 self.* = LibExeObjStep{
14931491 .strip = false,
14941492 .builder = builder,
14951493 .verbose_link = false,
......@@ -1534,6 +1532,11 @@ pub const LibExeObjStep = struct {
15341532 .override_dest_dir = null,
15351533 .installed_path = null,
15361534 .install_step = null,
1535
1536 .output_path_source = GeneratedFile{ .step = &self.step },
1537 .output_lib_path_source = GeneratedFile{ .step = &self.step },
1538 .output_h_path_source = GeneratedFile{ .step = &self.step },
1539 .output_pdb_path_source = GeneratedFile{ .step = &self.step },
15371540 };
15381541 self.computeOutFileNames();
15391542 if (root_src) |rs| rs.addStepDependencies(&self.step);
......@@ -1871,45 +1874,31 @@ pub const LibExeObjStep = struct {
18711874 self.libc_file = if (libc_file) |f| f.dupe(self.builder) else null;
18721875 }
18731876
1874 /// Unless setOutputDir was called, this function must be called only in
1875 /// the make step, from a step that has declared a dependency on this one.
1877 /// Returns the generated executable, library or object file.
18761878 /// To run an executable built with zig build, use `run`, or create an install step and invoke it.
1877 pub fn getOutputPath(self: *LibExeObjStep) []const u8 {
1878 return fs.path.join(
1879 self.builder.allocator,
1880 &[_][]const u8{ self.output_dir.?, self.out_filename },
1881 ) catch unreachable;
1879 pub fn getOutputSource(self: *LibExeObjStep) FileSource {
1880 return FileSource{ .generated = &self.output_path_source };
18821881 }
18831882
1884 /// Unless setOutputDir was called, this function must be called only in
1885 /// the make step, from a step that has declared a dependency on this one.
1886 pub fn getOutputLibPath(self: *LibExeObjStep) []const u8 {
1883 /// Returns the generated import library. This function can only be called for libraries.
1884 pub fn getOutputLibSource(self: *LibExeObjStep) FileSource {
18871885 assert(self.kind == Kind.Lib);
1888 return fs.path.join(
1889 self.builder.allocator,
1890 &[_][]const u8{ self.output_dir.?, self.out_lib_filename },
1891 ) catch unreachable;
1886 return FileSource{ .generated = &self.output_lib_path_source };
18921887 }
18931888
1894 /// Unless setOutputDir was called, this function must be called only in
1895 /// the make step, from a step that has declared a dependency on this one.
1896 pub fn getOutputHPath(self: *LibExeObjStep) []const u8 {
1889 /// Returns the generated header file.
1890 /// This function can only be called for libraries or object files which have `emit_h` set.
1891 pub fn getOutputHSource(self: *LibExeObjStep) FileSource {
18971892 assert(self.kind != Kind.Exe);
18981893 assert(self.emit_h);
1899 return fs.path.join(
1900 self.builder.allocator,
1901 &[_][]const u8{ self.output_dir.?, self.out_h_filename },
1902 ) catch unreachable;
1894 return FileSource{ .generated = &self.output_h_path_source };
19031895 }
19041896
1905 /// Unless setOutputDir was called, this function must be called only in
1906 /// the make step, from a step that has declared a dependency on this one.
1907 pub fn getOutputPdbPath(self: *LibExeObjStep) []const u8 {
1897 /// Returns the generated PDB file. This function can only be called for Windows and UEFI.
1898 pub fn getOutputPdbSource(self: *LibExeObjStep) FileSource {
1899 // TODO: Is this right? Isn't PDB for *any* PE/COFF file?
19081900 assert(self.target.isWindows() or self.target.isUefi());
1909 return fs.path.join(
1910 self.builder.allocator,
1911 &[_][]const u8{ self.output_dir.?, self.out_pdb_filename },
1912 ) catch unreachable;
1901 return FileSource{ .generated = &self.output_pdb_path_source };
19131902 }
19141903
19151904 pub fn addAssemblyFile(self: *LibExeObjStep, path: []const u8) void {
......@@ -2185,6 +2174,28 @@ pub const LibExeObjStep = struct {
21852174 return error.NeedAnObject;
21862175 }
21872176
2177 // Update generated files
2178 self.output_path_source.path =
2179 fs.path.join(
2180 self.builder.allocator,
2181 &[_][]const u8{ self.output_dir.?, self.out_filename },
2182 ) catch unreachable;
2183 self.output_lib_path_source.path =
2184 fs.path.join(
2185 self.builder.allocator,
2186 &[_][]const u8{ self.output_dir.?, self.out_lib_filename },
2187 ) catch unreachable;
2188 self.output_h_path_source.path =
2189 fs.path.join(
2190 self.builder.allocator,
2191 &[_][]const u8{ self.output_dir.?, self.out_h_filename },
2192 ) catch unreachable;
2193 self.output_pdb_path_source.path =
2194 fs.path.join(
2195 self.builder.allocator,
2196 &[_][]const u8{ self.output_dir.?, self.out_pdb_filename },
2197 ) catch unreachable;
2198
21882199 var zig_args = ArrayList([]const u8).init(builder.allocator);
21892200 defer zig_args.deinit();
21902201
......@@ -2219,10 +2230,10 @@ pub const LibExeObjStep = struct {
22192230 .Exe => unreachable,
22202231 .Test => unreachable,
22212232 .Obj => {
2222 try zig_args.append(other.getOutputPath());
2233 try zig_args.append(other.getOutputSource().getPath(builder));
22232234 },
22242235 .Lib => {
2225 const full_path_lib = other.getOutputLibPath();
2236 const full_path_lib = other.getOutputLibSource().getPath(builder);
22262237 try zig_args.append(full_path_lib);
22272238
22282239 if (other.linkage == .dynamic and !self.target.isWindows()) {
......@@ -2296,7 +2307,7 @@ pub const LibExeObjStep = struct {
22962307 self.addBuildOption(
22972308 []const u8,
22982309 item.name,
2299 self.builder.pathFromRoot(item.artifact.getOutputPath()),
2310 self.builder.pathFromRoot(item.artifact.getOutputSource().getPath(self.builder)),
23002311 );
23012312 }
23022313 for (self.build_options_file_source_args.items) |item| {
......@@ -2560,7 +2571,7 @@ pub const LibExeObjStep = struct {
25602571 try zig_args.append(self.builder.pathFromRoot(include_path));
25612572 },
25622573 .other_step => |other| if (other.emit_h) {
2563 const h_path = other.getOutputHPath();
2574 const h_path = other.getOutputHSource().getPath(self.builder);
25642575 try zig_args.append("-isystem");
25652576 try zig_args.append(fs.path.dirname(h_path).?);
25662577 },
......@@ -2701,7 +2712,7 @@ pub const LibExeObjStep = struct {
27012712 }
27022713
27032714 if (self.kind == .Lib and self.linkage == .dynamic and self.version != null and self.target.wantSharedLibSymLinks()) {
2704 try doAtomicSymLinks(builder.allocator, self.getOutputPath(), self.major_only_filename.?, self.name_only_filename.?);
2715 try doAtomicSymLinks(builder.allocator, self.getOutputSource().getPath(builder), self.major_only_filename.?, self.name_only_filename.?);
27052716 }
27062717 }
27072718};
......@@ -2768,17 +2779,17 @@ pub const InstallArtifactStep = struct {
27682779 const builder = self.builder;
27692780
27702781 const full_dest_path = builder.getInstallPath(self.dest_dir, self.artifact.out_filename);
2771 try builder.updateFile(self.artifact.getOutputPath(), full_dest_path);
2782 try builder.updateFile(self.artifact.getOutputSource().getPath(builder), full_dest_path);
27722783 if (self.artifact.isDynamicLibrary() and self.artifact.version != null and self.artifact.target.wantSharedLibSymLinks()) {
27732784 try doAtomicSymLinks(builder.allocator, full_dest_path, self.artifact.major_only_filename.?, self.artifact.name_only_filename.?);
27742785 }
27752786 if (self.pdb_dir) |pdb_dir| {
27762787 const full_pdb_path = builder.getInstallPath(pdb_dir, self.artifact.out_pdb_filename);
2777 try builder.updateFile(self.artifact.getOutputPdbPath(), full_pdb_path);
2788 try builder.updateFile(self.artifact.getOutputPdbSource().getPath(builder), full_pdb_path);
27782789 }
27792790 if (self.h_dir) |h_dir| {
27802791 const full_pdb_path = builder.getInstallPath(h_dir, self.artifact.out_h_filename);
2781 try builder.updateFile(self.artifact.getOutputHPath(), full_pdb_path);
2792 try builder.updateFile(self.artifact.getOutputHSource().getPath(builder), full_pdb_path);
27822793 }
27832794 self.artifact.installed_path = full_dest_path;
27842795 }
lib/std/build/InstallRawStep.zig+1-1
......@@ -214,7 +214,7 @@ fn make(step: *Step) !void {
214214 return error.InvalidObjectFormat;
215215 }
216216
217 const full_src_path = self.artifact.getOutputPath();
217 const full_src_path = self.artifact.getOutputSource().getPath(builder);
218218 const full_dest_path = builder.getInstallPath(self.dest_dir, self.dest_filename);
219219
220220 fs.cwd().makePath(builder.getInstallPath(self.dest_dir, "")) catch unreachable;
lib/std/build/RunStep.zig+2-2
......@@ -166,7 +166,7 @@ fn make(step: *Step) !void {
166166 // On Windows we don't have rpaths so we have to add .dll search paths to PATH
167167 self.addPathForDynLibs(artifact);
168168 }
169 const executable_path = artifact.installed_path orelse artifact.getOutputPath();
169 const executable_path = artifact.installed_path orelse artifact.getOutputSource().getPath(self.builder);
170170 try argv_list.append(executable_path);
171171 },
172172 }
......@@ -312,7 +312,7 @@ fn addPathForDynLibs(self: *RunStep, artifact: *LibExeObjStep) void {
312312 switch (link_object) {
313313 .other_step => |other| {
314314 if (other.target.isWindows() and other.isDynamicLibrary()) {
315 self.addPathDir(fs.path.dirname(other.getOutputPath()).?);
315 self.addPathDir(fs.path.dirname(other.getOutputSource().getPath(self.builder)).?);
316316 self.addPathForDynLibs(other);
317317 }
318318 },
lib/std/build/TranslateCStep.zig+6-19
......@@ -33,30 +33,12 @@ pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep {
3333 .include_dirs = std.ArrayList([]const u8).init(builder.allocator),
3434 .output_dir = null,
3535 .out_basename = undefined,
36 .output_file = build.GeneratedFile{
37 .step = &self.step,
38 .getPathFn = getGeneratedFilePath,
39 },
36 .output_file = build.GeneratedFile{ .step = &self.step },
4037 };
4138 source.addStepDependencies(&self.step);
4239 return self;
4340}
4441
45fn getGeneratedFilePath(file: *const build.GeneratedFile) []const u8 {
46 const self = @fieldParentPtr(TranslateCStep, "step", file.step);
47 return self.getOutputPath();
48}
49
50/// Unless setOutputDir was called, this function must be called only in
51/// the make step, from a step that has declared a dependency on this one.
52/// To run an executable built with zig build, use `run`, or create an install step and invoke it.
53pub fn getOutputPath(self: *TranslateCStep) []const u8 {
54 return fs.path.join(
55 self.builder.allocator,
56 &[_][]const u8{ self.output_dir.?, self.out_basename },
57 ) catch unreachable;
58}
59
6042pub fn setTarget(self: *TranslateCStep, target: CrossTarget) void {
6143 self.target = target;
6244}
......@@ -106,4 +88,9 @@ fn make(step: *Step) !void {
10688 } else {
10789 self.output_dir = fs.path.dirname(output_path).?;
10890 }
91
92 self.source.path = fs.path.join(
93 self.builder.allocator,
94 &[_][]const u8{ self.output_dir.?, self.out_basename },
95 ) catch unreachable;
10996}
lib/std/build/WriteFileStep.zig+5-15
......@@ -37,10 +37,7 @@ pub fn add(self: *WriteFileStep, basename: []const u8, bytes: []const u8) void {
3737 const node = self.builder.allocator.create(std.TailQueue(File).Node) catch unreachable;
3838 node.* = .{
3939 .data = .{
40 .source = build.GeneratedFile{
41 .step = &self.step,
42 .getPathFn = getFilePath,
43 },
40 .source = build.GeneratedFile{ .step = &self.step },
4441 .basename = self.builder.dupePath(basename),
4542 .bytes = self.builder.dupe(bytes),
4643 },
......@@ -59,17 +56,6 @@ pub fn getFileSource(step: *WriteFileStep, basename: []const u8) ?build.FileSour
5956 return null;
6057}
6158
62/// Returns the
63fn getFilePath(source: *const build.GeneratedFile) []const u8 {
64 const file = @fieldParentPtr(File, "source", source);
65 const step = @fieldParentPtr(WriteFileStep, "step", source.step);
66
67 return fs.path.join(
68 step.builder.allocator,
69 &[_][]const u8{ step.output_dir, file.basename },
70 ) catch unreachable;
71}
72
7359fn make(step: *Step) !void {
7460 const self = @fieldParentPtr(WriteFileStep, "step", step);
7561
......@@ -124,6 +110,10 @@ fn make(step: *Step) !void {
124110 });
125111 return err;
126112 };
113 node.data.source.path = fs.path.join(
114 self.builder.allocator,
115 &[_][]const u8{ self.output_dir, node.data.basename },
116 ) catch unreachable;
127117 }
128118 }
129119}
test/tests.zig+1-1
......@@ -707,7 +707,7 @@ pub const StackTracesContext = struct {
707707 const self = @fieldParentPtr(RunAndCompareStep, "step", step);
708708 const b = self.context.b;
709709
710 const full_exe_path = self.exe.getOutputPath();
710 const full_exe_path = self.exe.getOutputSource().getPath(b);
711711 var args = ArrayList([]const u8).init(b.allocator);
712712 defer args.deinit();
713713 args.append(full_exe_path) catch unreachable;