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,...@@ -46,9 +46,9 @@ strip: ?bool,
46unwind_tables: ?bool,46unwind_tables: ?bool,
47// keep in sync with src/link.zig:CompressDebugSections47// keep in sync with src/link.zig:CompressDebugSections
48compress_debug_sections: enum { none, zlib } = .none,48compress_debug_sections: enum { none, zlib } = .none,
49lib_paths: ArrayList([]const u8),49lib_paths: ArrayList(FileSource),
50rpaths: ArrayList([]const u8),50rpaths: ArrayList(FileSource),
51framework_dirs: ArrayList([]const u8),51framework_dirs: ArrayList(FileSource),
52frameworks: StringHashMap(FrameworkLinkInfo),52frameworks: StringHashMap(FrameworkLinkInfo),
53verbose_link: bool,53verbose_link: bool,
54verbose_cc: bool,54verbose_cc: bool,
...@@ -211,6 +211,7 @@ output_path_source: GeneratedFile,...@@ -211,6 +211,7 @@ output_path_source: GeneratedFile,
211output_lib_path_source: GeneratedFile,211output_lib_path_source: GeneratedFile,
212output_h_path_source: GeneratedFile,212output_h_path_source: GeneratedFile,
213output_pdb_path_source: GeneratedFile,213output_pdb_path_source: GeneratedFile,
214output_dirname_source: GeneratedFile,
214215
215pub const CSourceFiles = struct {216pub const CSourceFiles = struct {
216 files: []const []const u8,217 files: []const []const u8,
...@@ -359,9 +360,9 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {...@@ -359,9 +360,9 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
359 .include_dirs = ArrayList(IncludeDir).init(owner.allocator),360 .include_dirs = ArrayList(IncludeDir).init(owner.allocator),
360 .link_objects = ArrayList(LinkObject).init(owner.allocator),361 .link_objects = ArrayList(LinkObject).init(owner.allocator),
361 .c_macros = ArrayList([]const u8).init(owner.allocator),362 .c_macros = ArrayList([]const u8).init(owner.allocator),
362 .lib_paths = ArrayList([]const u8).init(owner.allocator),363 .lib_paths = ArrayList(FileSource).init(owner.allocator),
363 .rpaths = ArrayList([]const u8).init(owner.allocator),364 .rpaths = ArrayList(FileSource).init(owner.allocator),
364 .framework_dirs = ArrayList([]const u8).init(owner.allocator),365 .framework_dirs = ArrayList(FileSource).init(owner.allocator),
365 .installed_headers = ArrayList(*Step).init(owner.allocator),366 .installed_headers = ArrayList(*Step).init(owner.allocator),
366 .object_src = undefined,367 .object_src = undefined,
367 .c_std = std.Build.CStd.C99,368 .c_std = std.Build.CStd.C99,
...@@ -384,6 +385,7 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {...@@ -384,6 +385,7 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
384 .output_lib_path_source = GeneratedFile{ .step = &self.step },385 .output_lib_path_source = GeneratedFile{ .step = &self.step },
385 .output_h_path_source = GeneratedFile{ .step = &self.step },386 .output_h_path_source = GeneratedFile{ .step = &self.step },
386 .output_pdb_path_source = GeneratedFile{ .step = &self.step },387 .output_pdb_path_source = GeneratedFile{ .step = &self.step },
388 .output_dirname_source = GeneratedFile{ .step = &self.step },
387389
388 .target_info = NativeTargetInfo.detect(self.target) catch @panic("unhandled error"),390 .target_info = NativeTargetInfo.detect(self.target) catch @panic("unhandled error"),
389 };391 };
...@@ -914,13 +916,17 @@ pub fn setLibCFile(self: *CompileStep, libc_file: ?FileSource) void {...@@ -914,13 +916,17 @@ pub fn setLibCFile(self: *CompileStep, libc_file: ?FileSource) void {
914/// Returns the generated executable, library or object file.916/// Returns the generated executable, library or object file.
915/// To run an executable built with zig build, use `run`, or create an install step and invoke it.917/// To run an executable built with zig build, use `run`, or create an install step and invoke it.
916pub fn getOutputSource(self: *CompileStep) FileSource {918pub 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 };
918}924}
919925
920/// Returns the generated import library. This function can only be called for libraries.926/// Returns the generated import library. This function can only be called for libraries.
921pub fn getOutputLibSource(self: *CompileStep) FileSource {927pub fn getOutputLibSource(self: *CompileStep) FileSource {
922 assert(self.kind == .lib);928 assert(self.kind == .lib);
923 return FileSource{ .generated = &self.output_lib_path_source };929 return .{ .generated = &self.output_lib_path_source };
924}930}
925931
926/// Returns the generated header file.932/// Returns the generated header file.
...@@ -928,14 +934,14 @@ pub fn getOutputLibSource(self: *CompileStep) FileSource {...@@ -928,14 +934,14 @@ pub fn getOutputLibSource(self: *CompileStep) FileSource {
928pub fn getOutputHSource(self: *CompileStep) FileSource {934pub fn getOutputHSource(self: *CompileStep) FileSource {
929 assert(self.kind != .exe and self.kind != .test_exe and self.kind != .@"test");935 assert(self.kind != .exe and self.kind != .test_exe and self.kind != .@"test");
930 assert(self.emit_h);936 assert(self.emit_h);
931 return FileSource{ .generated = &self.output_h_path_source };937 return .{ .generated = &self.output_h_path_source };
932}938}
933939
934/// Returns the generated PDB file. This function can only be called for Windows and UEFI.940/// Returns the generated PDB file. This function can only be called for Windows and UEFI.
935pub fn getOutputPdbSource(self: *CompileStep) FileSource {941pub fn getOutputPdbSource(self: *CompileStep) FileSource {
936 // TODO: Is this right? Isn't PDB for *any* PE/COFF file?942 // TODO: Is this right? Isn't PDB for *any* PE/COFF file?
937 assert(self.target.isWindows() or self.target.isUefi());943 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 };
939}945}
940946
941pub fn addAssemblyFile(self: *CompileStep, path: []const u8) void {947pub fn addAssemblyFile(self: *CompileStep, path: []const u8) void {
...@@ -989,17 +995,32 @@ pub fn addConfigHeader(self: *CompileStep, config_header: *ConfigHeaderStep) voi...@@ -989,17 +995,32 @@ pub fn addConfigHeader(self: *CompileStep, config_header: *ConfigHeaderStep) voi
989995
990pub fn addLibraryPath(self: *CompileStep, path: []const u8) void {996pub fn addLibraryPath(self: *CompileStep, path: []const u8) void {
991 const b = self.step.owner;997 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);
993}1004}
9941005
995pub fn addRPath(self: *CompileStep, path: []const u8) void {1006pub fn addRPath(self: *CompileStep, path: []const u8) void {
996 const b = self.step.owner;1007 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);
998}1014}
9991015
1000pub fn addFrameworkPath(self: *CompileStep, dir_path: []const u8) void {1016pub fn addFrameworkPath(self: *CompileStep, dir_path: []const u8) void {
1001 const b = self.step.owner;1017 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);
1003}1024}
10041025
1005/// Adds a module to be used with `@import` and exposing it in the current1026/// 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 {...@@ -1065,7 +1086,7 @@ pub fn addVcpkgPaths(self: *CompileStep, linkage: CompileStep.Linkage) !void {
1065 try self.include_dirs.append(IncludeDir{ .raw_path = include_path });1086 try self.include_dirs.append(IncludeDir{ .raw_path = include_path });
10661087
1067 const lib_path = b.pathJoin(&.{ root, "installed", triplet, "lib" });1088 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
1070 self.vcpkg_bin_path = b.pathJoin(&.{ root, "installed", triplet, "bin" });1091 self.vcpkg_bin_path = b.pathJoin(&.{ root, "installed", triplet, "bin" });
1071 },1092 },
...@@ -1768,30 +1789,32 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -1768,30 +1789,32 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
1768 }1789 }
1769 }1790 }
17701791
1771 for (self.lib_paths.items) |lib_path| {1792 for (self.c_macros.items) |c_macro| {
1772 try zig_args.append("-L");1793 try zig_args.append("-D");
1773 try zig_args.append(lib_path);1794 try zig_args.append(c_macro);
1774 }1795 }
17751796
1776 for (self.rpaths.items) |rpath| {1797 try zig_args.ensureUnusedCapacity(2 * self.lib_paths.items.len);
1777 try zig_args.append("-rpath");1798 for (self.lib_paths.items) |lib_path| {
1778 try zig_args.append(rpath);1799 zig_args.appendAssumeCapacity("-L");
1800 zig_args.appendAssumeCapacity(lib_path.getPath2(b, step));
1779 }1801 }
17801802
1781 for (self.c_macros.items) |c_macro| {1803 try zig_args.ensureUnusedCapacity(2 * self.rpaths.items.len);
1782 try zig_args.append("-D");1804 for (self.rpaths.items) |rpath| {
1783 try zig_args.append(c_macro);1805 zig_args.appendAssumeCapacity("-rpath");
1806 zig_args.appendAssumeCapacity(rpath.getPath2(b, step));
1784 }1807 }
17851808
1786 for (self.framework_dirs.items) |dir| {1809 for (self.framework_dirs.items) |directory_source| {
1787 if (b.sysroot != null) {1810 if (b.sysroot != null) {
1788 try zig_args.append("-iframeworkwithsysroot");1811 try zig_args.append("-iframeworkwithsysroot");
1789 } else {1812 } else {
1790 try zig_args.append("-iframework");1813 try zig_args.append("-iframework");
1791 }1814 }
1792 try zig_args.append(dir);1815 try zig_args.append(directory_source.getPath2(b, step));
1793 try zig_args.append("-F");1816 try zig_args.append("-F");
1794 try zig_args.append(dir);1817 try zig_args.append(directory_source.getPath2(b, step));
1795 }1818 }
17961819
1797 {1820 {
...@@ -1954,6 +1977,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -1954,6 +1977,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
19541977
1955 // Update generated files1978 // Update generated files
1956 if (self.output_dir != null) {1979 if (self.output_dir != null) {
1980 self.output_dirname_source.path = self.output_dir.?;
1981
1957 self.output_path_source.path = b.pathJoin(1982 self.output_path_source.path = b.pathJoin(
1958 &.{ self.output_dir.?, self.out_filename },1983 &.{ self.output_dir.?, self.out_filename },
1959 );1984 );