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 {...@@ -311,12 +311,15 @@ pub const Builder = struct {
311 return child;311 return child;
312 }312 }
313313
314 pub fn applyArgs(b: *Builder, args: anytype) !void {314 fn applyArgs(b: *Builder, args: anytype) !void {
315 // TODO this function is the way that a build.zig file communicates315 // TODO this function is the way that a build.zig file communicates
316 // options to its dependencies. It is the programmatic way to give316 // options to its dependencies. It is the programmatic way to give
317 // command line arguments to a build.zig script.317 // command line arguments to a build.zig script.
318 _ = b;
319 _ = args;318 _ = 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, .{});
320 }323 }
321324
322 pub fn destroy(self: *Builder) void {325 pub fn destroy(self: *Builder) void {
...@@ -1154,6 +1157,10 @@ pub const Builder = struct {...@@ -1154,6 +1157,10 @@ pub const Builder = struct {
1154 return self.addInstallFileWithDir(source.dupe(self), .lib, dest_rel_path);1157 return self.addInstallFileWithDir(source.dupe(self), .lib, dest_rel_path);
1155 }1158 }
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
1157 pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, options: InstallRawStep.CreateOptions) *InstallRawStep {1164 pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, options: InstallRawStep.CreateOptions) *InstallRawStep {
1158 return InstallRawStep.create(self, artifact, dest_filename, options);1165 return InstallRawStep.create(self, artifact, dest_filename, options);
1159 }1166 }
lib/std/build/LibExeObjStep.zig+29-9
...@@ -108,6 +108,7 @@ object_src: []const u8,...@@ -108,6 +108,7 @@ object_src: []const u8,
108link_objects: ArrayList(LinkObject),108link_objects: ArrayList(LinkObject),
109include_dirs: ArrayList(IncludeDir),109include_dirs: ArrayList(IncludeDir),
110c_macros: ArrayList([]const u8),110c_macros: ArrayList([]const u8),
111installed_headers: ArrayList(*std.build.InstallFileStep),
111output_dir: ?[]const u8,112output_dir: ?[]const u8,
112is_linking_libc: bool = false,113is_linking_libc: bool = false,
113is_linking_libcpp: bool = false,114is_linking_libcpp: bool = false,
...@@ -370,6 +371,7 @@ fn initExtraArgs(...@@ -370,6 +371,7 @@ fn initExtraArgs(
370 .lib_paths = ArrayList([]const u8).init(builder.allocator),371 .lib_paths = ArrayList([]const u8).init(builder.allocator),
371 .rpaths = ArrayList([]const u8).init(builder.allocator),372 .rpaths = ArrayList([]const u8).init(builder.allocator),
372 .framework_dirs = ArrayList([]const u8).init(builder.allocator),373 .framework_dirs = ArrayList([]const u8).init(builder.allocator),
374 .installed_headers = ArrayList(*std.build.InstallFileStep).init(builder.allocator),
373 .object_src = undefined,375 .object_src = undefined,
374 .c_std = Builder.CStd.C99,376 .c_std = Builder.CStd.C99,
375 .override_lib_dir = null,377 .override_lib_dir = null,
...@@ -472,6 +474,13 @@ pub fn installRaw(self: *LibExeObjStep, dest_filename: []const u8, options: Inst...@@ -472,6 +474,13 @@ pub fn installRaw(self: *LibExeObjStep, dest_filename: []const u8, options: Inst
472 return self.builder.installRaw(self, dest_filename, options);474 return self.builder.installRaw(self, dest_filename, options);
473}475}
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
475/// Creates a `RunStep` with an executable built with `addExecutable`.484/// Creates a `RunStep` with an executable built with `addExecutable`.
476/// Add command line arguments with `addArg`.485/// Add command line arguments with `addArg`.
477pub fn run(exe: *LibExeObjStep) *RunStep {486pub fn run(exe: *LibExeObjStep) *RunStep {
...@@ -1362,7 +1371,7 @@ fn make(step: *Step) !void {...@@ -1362,7 +1371,7 @@ fn make(step: *Step) !void {
13621371
1363 if (self.libc_file) |libc_file| {1372 if (self.libc_file) |libc_file| {
1364 try zig_args.append("--libc");1373 try zig_args.append("--libc");
1365 try zig_args.append(libc_file.getPath(self.builder));1374 try zig_args.append(libc_file.getPath(builder));
1366 } else if (builder.libc_file) |libc_file| {1375 } else if (builder.libc_file) |libc_file| {
1367 try zig_args.append("--libc");1376 try zig_args.append("--libc");
1368 try zig_args.append(libc_file);1377 try zig_args.append(libc_file);
...@@ -1577,7 +1586,7 @@ fn make(step: *Step) !void {...@@ -1577,7 +1586,7 @@ fn make(step: *Step) !void {
1577 } else {1586 } else {
1578 const need_cross_glibc = self.target.isGnuLibC() and self.is_linking_libc;1587 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, .{
1581 .qemu_fixes_dl = need_cross_glibc and builder.glibc_runtimes_dir != null,1590 .qemu_fixes_dl = need_cross_glibc and builder.glibc_runtimes_dir != null,
1582 .link_libc = self.is_linking_libc,1591 .link_libc = self.is_linking_libc,
1583 })) {1592 })) {
...@@ -1661,7 +1670,7 @@ fn make(step: *Step) !void {...@@ -1661,7 +1670,7 @@ fn make(step: *Step) !void {
1661 switch (include_dir) {1670 switch (include_dir) {
1662 .raw_path => |include_path| {1671 .raw_path => |include_path| {
1663 try zig_args.append("-I");1672 try zig_args.append("-I");
1664 try zig_args.append(self.builder.pathFromRoot(include_path));1673 try zig_args.append(builder.pathFromRoot(include_path));
1665 },1674 },
1666 .raw_path_system => |include_path| {1675 .raw_path_system => |include_path| {
1667 if (builder.sysroot != null) {1676 if (builder.sysroot != null) {
...@@ -1670,7 +1679,7 @@ fn make(step: *Step) !void {...@@ -1670,7 +1679,7 @@ fn make(step: *Step) !void {
1670 try zig_args.append("-isystem");1679 try zig_args.append("-isystem");
1671 }1680 }
16721681
1673 const resolved_include_path = self.builder.pathFromRoot(include_path);1682 const resolved_include_path = builder.pathFromRoot(include_path);
16741683
1675 const common_include_path = if (builtin.os.tag == .windows and builder.sysroot != null and fs.path.isAbsolute(resolved_include_path)) blk: {1684 const common_include_path = if (builtin.os.tag == .windows and builder.sysroot != null and fs.path.isAbsolute(resolved_include_path)) blk: {
1676 // We need to check for disk designator and strip it out from dir path so1685 // We need to check for disk designator and strip it out from dir path so
...@@ -1686,10 +1695,21 @@ fn make(step: *Step) !void {...@@ -1686,10 +1695,21 @@ fn make(step: *Step) !void {
16861695
1687 try zig_args.append(common_include_path);1696 try zig_args.append(common_include_path);
1688 },1697 },
1689 .other_step => |other| if (other.emit_h) {1698 .other_step => |other| {
1690 const h_path = other.getOutputHSource().getPath(self.builder);1699 if (other.emit_h) {
1691 try zig_args.append("-isystem");1700 const h_path = other.getOutputHSource().getPath(builder);
1692 try zig_args.append(fs.path.dirname(h_path).?);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 }
1693 },1713 },
1694 .config_header_step => |config_header| {1714 .config_header_step => |config_header| {
1695 try zig_args.append("-I");1715 try zig_args.append("-I");
...@@ -1790,7 +1810,7 @@ fn make(step: *Step) !void {...@@ -1790,7 +1810,7 @@ fn make(step: *Step) !void {
1790 if (self.override_lib_dir) |dir| {1810 if (self.override_lib_dir) |dir| {
1791 try zig_args.append("--zig-lib-dir");1811 try zig_args.append("--zig-lib-dir");
1792 try zig_args.append(builder.pathFromRoot(dir));1812 try zig_args.append(builder.pathFromRoot(dir));
1793 } else if (self.builder.override_lib_dir) |dir| {1813 } else if (builder.override_lib_dir) |dir| {
1794 try zig_args.append("--zig-lib-dir");1814 try zig_args.append("--zig-lib-dir");
1795 try zig_args.append(builder.pathFromRoot(dir));1815 try zig_args.append(builder.pathFromRoot(dir));
1796 }1816 }