Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: include zig.h when CompileStep.emit_h is true #14576

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/build_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions lib/std/Build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 4 additions & 0 deletions lib/std/Build/CompileStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
5 changes: 5 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions test/standalone.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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", .{});
}
29 changes: 29 additions & 0 deletions test/standalone/c_exe_with_emit_h/build.zig
Original file line number Diff line number Diff line change
@@ -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);
}
11 changes: 11 additions & 0 deletions test/standalone/c_exe_with_emit_h/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This header is generated by zig from math.zig
#include "math.h"
#include <stdio.h>

int main(int argc, char **argv) {
int32_t result = add(42, 1337);
if (result != 1379) {
return 1;
}
return 0;
}
3 changes: 3 additions & 0 deletions test/standalone/c_exe_with_emit_h/math.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export fn add(a: i32, b: i32) i32 {
return a + b;
}