From bca6e89579d5672969857bf87d04ecf8607f3f2c Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Mon, 6 Feb 2023 16:18:57 +0100 Subject: [PATCH] build: include zig.h when CompileStep.emit_h is true When linking to a library or object and emit_h is set to true, include the zig.h header file, since the generated header file will include it. Add a standalone test case. --- lib/build_runner.zig | 5 ++++ lib/std/Build.zig | 4 +++ lib/std/Build/CompileStep.zig | 4 +++ src/main.zig | 5 ++++ test/standalone.zig | 1 + test/standalone/c_exe_with_emit_h/build.zig | 29 +++++++++++++++++++++ test/standalone/c_exe_with_emit_h/main.c | 11 ++++++++ test/standalone/c_exe_with_emit_h/math.zig | 3 +++ 8 files changed, 62 insertions(+) create mode 100644 test/standalone/c_exe_with_emit_h/build.zig create mode 100644 test/standalone/c_exe_with_emit_h/main.c create mode 100644 test/standalone/c_exe_with_emit_h/math.zig diff --git a/lib/build_runner.zig b/lib/build_runner.zig index f2b2eba9504e..26bf8baad73a 100644 --- a/lib/build_runner.zig +++ b/lib/build_runner.zig @@ -28,6 +28,10 @@ pub fn main() !void { std.debug.print("Expected path to zig compiler\n", .{}); return error.InvalidArgs; }; + const zig_lib_dir = nextArg(args, &arg_idx) orelse { + std.debug.print("Expected zig lib directory path\n", .{}); + return error.InvalidArgs; + }; const build_root = nextArg(args, &arg_idx) orelse { std.debug.print("Expected build root directory path\n", .{}); return error.InvalidArgs; @@ -46,6 +50,7 @@ pub fn main() !void { const builder = try std.Build.create( allocator, zig_exe, + zig_lib_dir, build_root, cache_root, global_cache_root, diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 86b16d234c24..312354a4c55b 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -63,6 +63,7 @@ color: enum { auto, on, off } = .auto, reference_trace: ?u32 = null, invalid_user_input: bool, zig_exe: []const u8, +zig_lib_dir: []const u8, default_step: *Step, env_map: *EnvMap, top_level_steps: ArrayList(*TopLevelStep), @@ -185,6 +186,7 @@ pub const DirList = struct { pub fn create( allocator: Allocator, zig_exe: []const u8, + zig_lib_dir: []const u8, build_root: []const u8, cache_root: []const u8, global_cache_root: []const u8, @@ -196,6 +198,7 @@ pub fn create( const self = try allocator.create(Build); self.* = Build{ .zig_exe = zig_exe, + .zig_lib_dir = zig_lib_dir, .build_root = build_root, .cache_root = try fs.path.relative(allocator, build_root, cache_root), .global_cache_root = global_cache_root, @@ -281,6 +284,7 @@ fn createChildOnly(parent: *Build, dep_name: []const u8, build_root: []const u8) .reference_trace = parent.reference_trace, .invalid_user_input = false, .zig_exe = parent.zig_exe, + .zig_lib_dir = parent.zig_exe, .default_step = undefined, .env_map = parent.env_map, .top_level_steps = ArrayList(*TopLevelStep).init(allocator), diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index e0d90add3c35..a23c9de4e45a 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -1630,6 +1630,10 @@ fn make(step: *Step) !void { const h_path = other.getOutputHSource().getPath(builder); try zig_args.append("-isystem"); try zig_args.append(fs.path.dirname(h_path).?); + if (builder.zig_lib_dir.len > 0) { + try zig_args.append("-isystem"); + try zig_args.append(builder.zig_lib_dir); + } } if (other.installed_headers.items.len > 0) { for (other.installed_headers.items) |install_step| { diff --git a/src/main.zig b/src/main.zig index f634c259ff9e..535829d9cbc9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3936,6 +3936,9 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi try child_argv.append(self_exe_path); + const argv_index_zig_lib_dir = child_argv.items.len; + _ = try child_argv.addOne(); + const argv_index_build_file = child_argv.items.len; _ = try child_argv.addOne(); @@ -4004,6 +4007,8 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi }; defer zig_lib_directory.handle.close(); + child_argv.items[argv_index_zig_lib_dir] = zig_lib_directory.path orelse ""; + var cleanup_build_dir: ?fs.Dir = null; defer if (cleanup_build_dir) |*dir| dir.close(); diff --git a/test/standalone.zig b/test/standalone.zig index af972ccb8665..f084b46e8e01 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -103,4 +103,5 @@ pub fn addCases(cases: *tests.StandaloneContext) void { cases.addBuildFile("test/standalone/emit_asm_and_bin/build.zig", .{}); cases.addBuildFile("test/standalone/issue_12588/build.zig", .{}); cases.addBuildFile("test/standalone/embed_generated_file/build.zig", .{}); + cases.addBuildFile("test/standalone/c_exe_with_emit_h/build.zig", .{}); } diff --git a/test/standalone/c_exe_with_emit_h/build.zig b/test/standalone/c_exe_with_emit_h/build.zig new file mode 100644 index 000000000000..5d783dc5688b --- /dev/null +++ b/test/standalone/c_exe_with_emit_h/build.zig @@ -0,0 +1,29 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const optimize = b.standardOptimizeOption(.{}); + const target = b.standardTargetOptions(.{}); + + const lib = b.addSharedLibrary(.{ + .name = "math", + .root_source_file = .{ .path = "math.zig" }, + .version = .{ .major = 1, .minor = 0, .patch = 0 }, + .target = target, + .optimize = optimize, + }); + lib.emit_h = true; + + const exe = b.addExecutable(.{ + .name = "main", + }); + exe.addCSourceFile("main.c", &[_][]const u8{"-std=c99"}); + exe.linkLibrary(lib); + exe.linkSystemLibrary("c"); + + b.default_step.dependOn(&exe.step); + + const run_cmd = exe.run(); + + const test_step = b.step("test", "Test the program"); + test_step.dependOn(&run_cmd.step); +} diff --git a/test/standalone/c_exe_with_emit_h/main.c b/test/standalone/c_exe_with_emit_h/main.c new file mode 100644 index 000000000000..70fab61d1e25 --- /dev/null +++ b/test/standalone/c_exe_with_emit_h/main.c @@ -0,0 +1,11 @@ +// This header is generated by zig from math.zig +#include "math.h" +#include + +int main(int argc, char **argv) { + int32_t result = add(42, 1337); + if (result != 1379) { + return 1; + } + return 0; +} diff --git a/test/standalone/c_exe_with_emit_h/math.zig b/test/standalone/c_exe_with_emit_h/math.zig new file mode 100644 index 000000000000..a04ec1544dc8 --- /dev/null +++ b/test/standalone/c_exe_with_emit_h/math.zig @@ -0,0 +1,3 @@ +export fn add(a: i32, b: i32) i32 { + return a + b; +}