authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-07 19:17:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:14-07:00
log9a9b0083009f62dda9500f978544380a17b1f325
tree733c47ab8fe148da8e4ea54b604eb7c058979d01
parentcdf0a2af58ccd4ca4250435f258b35735e953aaf

std.Build.CompileStep: add FileSource support to some paths

Library paths, RPaths, and framework paths now support being fulfilled by FileSource arguments.

1 files changed, 51 insertions(+), 26 deletions(-)

lib/std/Build/CompileStep.zig+51-26
......@@ -46,9 +46,9 @@ strip: ?bool,
4646unwind_tables: ?bool,
4747// keep in sync with src/link.zig:CompressDebugSections
4848compress_debug_sections: enum { none, zlib } = .none,
49lib_paths: ArrayList([]const u8),
50rpaths: ArrayList([]const u8),
51framework_dirs: ArrayList([]const u8),
49lib_paths: ArrayList(FileSource),
50rpaths: ArrayList(FileSource),
51framework_dirs: ArrayList(FileSource),
5252frameworks: StringHashMap(FrameworkLinkInfo),
5353verbose_link: bool,
5454verbose_cc: bool,
......@@ -211,6 +211,7 @@ output_path_source: GeneratedFile,
211211output_lib_path_source: GeneratedFile,
212212output_h_path_source: GeneratedFile,
213213output_pdb_path_source: GeneratedFile,
214output_dirname_source: GeneratedFile,
214215
215216pub const CSourceFiles = struct {
216217 files: []const []const u8,
......@@ -359,9 +360,9 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
359360 .include_dirs = ArrayList(IncludeDir).init(owner.allocator),
360361 .link_objects = ArrayList(LinkObject).init(owner.allocator),
361362 .c_macros = ArrayList([]const u8).init(owner.allocator),
362 .lib_paths = ArrayList([]const u8).init(owner.allocator),
363 .rpaths = ArrayList([]const u8).init(owner.allocator),
364 .framework_dirs = ArrayList([]const u8).init(owner.allocator),
363 .lib_paths = ArrayList(FileSource).init(owner.allocator),
364 .rpaths = ArrayList(FileSource).init(owner.allocator),
365 .framework_dirs = ArrayList(FileSource).init(owner.allocator),
365366 .installed_headers = ArrayList(*Step).init(owner.allocator),
366367 .object_src = undefined,
367368 .c_std = std.Build.CStd.C99,
......@@ -384,6 +385,7 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
384385 .output_lib_path_source = GeneratedFile{ .step = &self.step },
385386 .output_h_path_source = GeneratedFile{ .step = &self.step },
386387 .output_pdb_path_source = GeneratedFile{ .step = &self.step },
388 .output_dirname_source = GeneratedFile{ .step = &self.step },
387389
388390 .target_info = NativeTargetInfo.detect(self.target) catch @panic("unhandled error"),
389391 };
......@@ -914,13 +916,17 @@ pub fn setLibCFile(self: *CompileStep, libc_file: ?FileSource) void {
914916/// Returns the generated executable, library or object file.
915917/// To run an executable built with zig build, use `run`, or create an install step and invoke it.
916918pub fn getOutputSource(self: *CompileStep) FileSource {
917 return FileSource{ .generated = &self.output_path_source };
919 return .{ .generated = &self.output_path_source };
920}
921
922pub fn getOutputDirectorySource(self: *CompileStep) FileSource {
923 return .{ .generated = &self.output_dirname_source };
918924}
919925
920926/// Returns the generated import library. This function can only be called for libraries.
921927pub fn getOutputLibSource(self: *CompileStep) FileSource {
922928 assert(self.kind == .lib);
923 return FileSource{ .generated = &self.output_lib_path_source };
929 return .{ .generated = &self.output_lib_path_source };
924930}
925931
926932/// Returns the generated header file.
......@@ -928,14 +934,14 @@ pub fn getOutputLibSource(self: *CompileStep) FileSource {
928934pub fn getOutputHSource(self: *CompileStep) FileSource {
929935 assert(self.kind != .exe and self.kind != .test_exe and self.kind != .@"test");
930936 assert(self.emit_h);
931 return FileSource{ .generated = &self.output_h_path_source };
937 return .{ .generated = &self.output_h_path_source };
932938}
933939
934940/// Returns the generated PDB file. This function can only be called for Windows and UEFI.
935941pub fn getOutputPdbSource(self: *CompileStep) FileSource {
936942 // TODO: Is this right? Isn't PDB for *any* PE/COFF file?
937943 assert(self.target.isWindows() or self.target.isUefi());
938 return FileSource{ .generated = &self.output_pdb_path_source };
944 return .{ .generated = &self.output_pdb_path_source };
939945}
940946
941947pub fn addAssemblyFile(self: *CompileStep, path: []const u8) void {
......@@ -989,17 +995,32 @@ pub fn addConfigHeader(self: *CompileStep, config_header: *ConfigHeaderStep) voi
989995
990996pub fn addLibraryPath(self: *CompileStep, path: []const u8) void {
991997 const b = self.step.owner;
992 self.lib_paths.append(b.dupe(path)) catch @panic("OOM");
998 self.lib_paths.append(.{ .path = b.dupe(path) }) catch @panic("OOM");
999}
1000
1001pub fn addLibraryPathDirectorySource(self: *CompileStep, directory_source: FileSource) void {
1002 self.lib_paths.append(directory_source) catch @panic("OOM");
1003 directory_source.addStepDependencies(&self.step);
9931004}
9941005
9951006pub fn addRPath(self: *CompileStep, path: []const u8) void {
9961007 const b = self.step.owner;
997 self.rpaths.append(b.dupe(path)) catch @panic("OOM");
1008 self.rpaths.append(.{ .path = b.dupe(path) }) catch @panic("OOM");
1009}
1010
1011pub fn addRPathDirectorySource(self: *CompileStep, directory_source: FileSource) void {
1012 self.rpaths.append(directory_source) catch @panic("OOM");
1013 directory_source.addStepDependencies(&self.step);
9981014}
9991015
10001016pub fn addFrameworkPath(self: *CompileStep, dir_path: []const u8) void {
10011017 const b = self.step.owner;
1002 self.framework_dirs.append(b.dupe(dir_path)) catch @panic("OOM");
1018 self.framework_dirs.append(.{ .path = b.dupe(dir_path) }) catch @panic("OOM");
1019}
1020
1021pub fn addFrameworkPathDirectorySource(self: *CompileStep, directory_source: FileSource) void {
1022 self.framework_dirs.append(directory_source) catch @panic("OOM");
1023 directory_source.addStepDependencies(&self.step);
10031024}
10041025
10051026/// Adds a module to be used with `@import` and exposing it in the current
......@@ -1065,7 +1086,7 @@ pub fn addVcpkgPaths(self: *CompileStep, linkage: CompileStep.Linkage) !void {
10651086 try self.include_dirs.append(IncludeDir{ .raw_path = include_path });
10661087
10671088 const lib_path = b.pathJoin(&.{ root, "installed", triplet, "lib" });
1068 try self.lib_paths.append(lib_path);
1089 try self.lib_paths.append(.{ .path = lib_path });
10691090
10701091 self.vcpkg_bin_path = b.pathJoin(&.{ root, "installed", triplet, "bin" });
10711092 },
......@@ -1768,30 +1789,32 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
17681789 }
17691790 }
17701791
1771 for (self.lib_paths.items) |lib_path| {
1772 try zig_args.append("-L");
1773 try zig_args.append(lib_path);
1792 for (self.c_macros.items) |c_macro| {
1793 try zig_args.append("-D");
1794 try zig_args.append(c_macro);
17741795 }
17751796
1776 for (self.rpaths.items) |rpath| {
1777 try zig_args.append("-rpath");
1778 try zig_args.append(rpath);
1797 try zig_args.ensureUnusedCapacity(2 * self.lib_paths.items.len);
1798 for (self.lib_paths.items) |lib_path| {
1799 zig_args.appendAssumeCapacity("-L");
1800 zig_args.appendAssumeCapacity(lib_path.getPath2(b, step));
17791801 }
17801802
1781 for (self.c_macros.items) |c_macro| {
1782 try zig_args.append("-D");
1783 try zig_args.append(c_macro);
1803 try zig_args.ensureUnusedCapacity(2 * self.rpaths.items.len);
1804 for (self.rpaths.items) |rpath| {
1805 zig_args.appendAssumeCapacity("-rpath");
1806 zig_args.appendAssumeCapacity(rpath.getPath2(b, step));
17841807 }
17851808
1786 for (self.framework_dirs.items) |dir| {
1809 for (self.framework_dirs.items) |directory_source| {
17871810 if (b.sysroot != null) {
17881811 try zig_args.append("-iframeworkwithsysroot");
17891812 } else {
17901813 try zig_args.append("-iframework");
17911814 }
1792 try zig_args.append(dir);
1815 try zig_args.append(directory_source.getPath2(b, step));
17931816 try zig_args.append("-F");
1794 try zig_args.append(dir);
1817 try zig_args.append(directory_source.getPath2(b, step));
17951818 }
17961819
17971820 {
......@@ -1954,6 +1977,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
19541977
19551978 // Update generated files
19561979 if (self.output_dir != null) {
1980 self.output_dirname_source.path = self.output_dir.?;
1981
19571982 self.output_path_source.path = b.pathJoin(
19581983 &.{ self.output_dir.?, self.out_filename },
19591984 );