authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-10 21:59:43-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-11 15:39:49-08:00
log416717d04e182841d87783960e12068100b5b1fb
treefad0ea2b69f4917fb52a440fdeae343207cec31d
parentcfcf9771c1bde357ad64d81cda9d61ba72d80b15

zig build: support libraries aware of installed headers


2 files changed, 38 insertions(+), 11 deletions(-)

lib/std/build.zig+9-2
......@@ -311,12 +311,15 @@ pub const Builder = struct {
311311 return child;
312312 }
313313
314 pub fn applyArgs(b: *Builder, args: anytype) !void {
314 fn applyArgs(b: *Builder, args: anytype) !void {
315315 // TODO this function is the way that a build.zig file communicates
316316 // options to its dependencies. It is the programmatic way to give
317317 // command line arguments to a build.zig script.
318 _ = b;
319318 _ = args;
319 // TODO create a hash based on the args and the package hash, use this
320 // to compute the install prefix.
321 const install_prefix = b.pathJoin(&.{ b.cache_root, "pkg" });
322 b.resolveInstallPrefix(install_prefix, .{});
320323 }
321324
322325 pub fn destroy(self: *Builder) void {
......@@ -1154,6 +1157,10 @@ pub const Builder = struct {
11541157 return self.addInstallFileWithDir(source.dupe(self), .lib, dest_rel_path);
11551158 }
11561159
1160 pub fn addInstallHeaderFile(b: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep {
1161 return b.addInstallFileWithDir(.{ .path = src_path }, .header, dest_rel_path);
1162 }
1163
11571164 pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, options: InstallRawStep.CreateOptions) *InstallRawStep {
11581165 return InstallRawStep.create(self, artifact, dest_filename, options);
11591166 }
lib/std/build/LibExeObjStep.zig+29-9
......@@ -108,6 +108,7 @@ object_src: []const u8,
108108link_objects: ArrayList(LinkObject),
109109include_dirs: ArrayList(IncludeDir),
110110c_macros: ArrayList([]const u8),
111installed_headers: ArrayList(*std.build.InstallFileStep),
111112output_dir: ?[]const u8,
112113is_linking_libc: bool = false,
113114is_linking_libcpp: bool = false,
......@@ -370,6 +371,7 @@ fn initExtraArgs(
370371 .lib_paths = ArrayList([]const u8).init(builder.allocator),
371372 .rpaths = ArrayList([]const u8).init(builder.allocator),
372373 .framework_dirs = ArrayList([]const u8).init(builder.allocator),
374 .installed_headers = ArrayList(*std.build.InstallFileStep).init(builder.allocator),
373375 .object_src = undefined,
374376 .c_std = Builder.CStd.C99,
375377 .override_lib_dir = null,
......@@ -472,6 +474,13 @@ pub fn installRaw(self: *LibExeObjStep, dest_filename: []const u8, options: Inst
472474 return self.builder.installRaw(self, dest_filename, options);
473475}
474476
477pub fn installHeader(a: *LibExeObjStep, src_path: []const u8) void {
478 const basename = fs.path.basename(src_path);
479 const install_file = a.builder.addInstallHeaderFile(src_path, basename);
480 a.builder.getInstallStep().dependOn(&install_file.step);
481 a.installed_headers.append(install_file) catch unreachable;
482}
483
475484/// Creates a `RunStep` with an executable built with `addExecutable`.
476485/// Add command line arguments with `addArg`.
477486pub fn run(exe: *LibExeObjStep) *RunStep {
......@@ -1362,7 +1371,7 @@ fn make(step: *Step) !void {
13621371
13631372 if (self.libc_file) |libc_file| {
13641373 try zig_args.append("--libc");
1365 try zig_args.append(libc_file.getPath(self.builder));
1374 try zig_args.append(libc_file.getPath(builder));
13661375 } else if (builder.libc_file) |libc_file| {
13671376 try zig_args.append("--libc");
13681377 try zig_args.append(libc_file);
......@@ -1577,7 +1586,7 @@ fn make(step: *Step) !void {
15771586 } else {
15781587 const need_cross_glibc = self.target.isGnuLibC() and self.is_linking_libc;
15791588
1580 switch (self.builder.host.getExternalExecutor(self.target_info, .{
1589 switch (builder.host.getExternalExecutor(self.target_info, .{
15811590 .qemu_fixes_dl = need_cross_glibc and builder.glibc_runtimes_dir != null,
15821591 .link_libc = self.is_linking_libc,
15831592 })) {
......@@ -1661,7 +1670,7 @@ fn make(step: *Step) !void {
16611670 switch (include_dir) {
16621671 .raw_path => |include_path| {
16631672 try zig_args.append("-I");
1664 try zig_args.append(self.builder.pathFromRoot(include_path));
1673 try zig_args.append(builder.pathFromRoot(include_path));
16651674 },
16661675 .raw_path_system => |include_path| {
16671676 if (builder.sysroot != null) {
......@@ -1670,7 +1679,7 @@ fn make(step: *Step) !void {
16701679 try zig_args.append("-isystem");
16711680 }
16721681
1673 const resolved_include_path = self.builder.pathFromRoot(include_path);
1682 const resolved_include_path = builder.pathFromRoot(include_path);
16741683
16751684 const common_include_path = if (builtin.os.tag == .windows and builder.sysroot != null and fs.path.isAbsolute(resolved_include_path)) blk: {
16761685 // We need to check for disk designator and strip it out from dir path so
......@@ -1686,10 +1695,21 @@ fn make(step: *Step) !void {
16861695
16871696 try zig_args.append(common_include_path);
16881697 },
1689 .other_step => |other| if (other.emit_h) {
1690 const h_path = other.getOutputHSource().getPath(self.builder);
1691 try zig_args.append("-isystem");
1692 try zig_args.append(fs.path.dirname(h_path).?);
1698 .other_step => |other| {
1699 if (other.emit_h) {
1700 const h_path = other.getOutputHSource().getPath(builder);
1701 try zig_args.append("-isystem");
1702 try zig_args.append(fs.path.dirname(h_path).?);
1703 }
1704 if (other.installed_headers.items.len != 0) {
1705 for (other.installed_headers.items) |install_file| {
1706 try install_file.step.make();
1707 }
1708 try zig_args.append("-I");
1709 try zig_args.append(builder.pathJoin(&.{
1710 other.builder.install_prefix, "include",
1711 }));
1712 }
16931713 },
16941714 .config_header_step => |config_header| {
16951715 try zig_args.append("-I");
......@@ -1790,7 +1810,7 @@ fn make(step: *Step) !void {
17901810 if (self.override_lib_dir) |dir| {
17911811 try zig_args.append("--zig-lib-dir");
17921812 try zig_args.append(builder.pathFromRoot(dir));
1793 } else if (self.builder.override_lib_dir) |dir| {
1813 } else if (builder.override_lib_dir) |dir| {
17941814 try zig_args.append("--zig-lib-dir");
17951815 try zig_args.append(builder.pathFromRoot(dir));
17961816 }