Skip to content

Commit 03b4642

Browse files
committed
std.Build: precompiled_header option can now directly be a headerfile.
a compile step to build the pch file will be automatically created. To benefit from precompiled headers, it is now possible to simply change exe.addCSourceFiles(.{ .files = &.{"file.c"}, .flags = ..., }); into exe.addCSourceFiles(.{ .files = &.{"file.c"}, .flags = ..., .precompiled_header = .{ .source_header = .{ path = b.path("rootincludes.h") } }, });
1 parent 1d0af1f commit 03b4642

File tree

6 files changed

+136
-25
lines changed

6 files changed

+136
-25
lines changed

lib/std/Build/Module.zig

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,22 @@ pub const CSourceLang = enum {
149149
}
150150
};
151151

152+
pub const PrecompiledHeader = union(enum) {
153+
/// automatically create the PCH compile step for the source header file,
154+
/// inheriting the options from the parent compile step.
155+
source_header: struct { path: LazyPath, lang: ?CSourceLang = null },
156+
157+
/// final PCH compile step,
158+
/// can be provided by the user or else will be created from the `source_header` field during step finalization.
159+
pch_step: *Step.Compile,
160+
161+
pub fn getPath(pch: PrecompiledHeader, b: *std.Build) []const u8 {
162+
switch (pch) {
163+
.source_header => unreachable,
164+
.pch_step => |pch_step| return pch_step.getEmittedBin().getPath(b),
165+
}
166+
}
167+
};
152168
pub const CSourceFiles = struct {
153169
root: LazyPath,
154170
/// `files` is relative to `root`, which is
@@ -157,14 +173,14 @@ pub const CSourceFiles = struct {
157173
/// if null, deduce the language from the file extension
158174
lang: ?CSourceLang = null,
159175
flags: []const []const u8,
160-
precompiled_header: ?LazyPath = null,
176+
precompiled_header: ?PrecompiledHeader = null,
161177
};
162178

163179
pub const CSourceFile = struct {
164180
file: LazyPath,
165181
lang: ?CSourceLang = null,
166182
flags: []const []const u8 = &.{},
167-
precompiled_header: ?LazyPath = null,
183+
precompiled_header: ?PrecompiledHeader = null,
168184

169185
pub fn dupe(file: CSourceFile, b: *std.Build) CSourceFile {
170186
return .{
@@ -438,7 +454,7 @@ fn addStepDependencies(m: *Module, module: *Module, dependee: *Step) void {
438454
}
439455
}
440456

441-
fn addStepDependenciesOnly(m: *Module, dependee: *Step) void {
457+
pub fn addStepDependenciesOnly(m: *Module, dependee: *Step) void {
442458
for (m.depending_steps.keys()) |compile| {
443459
compile.step.dependOn(dependee);
444460
}
@@ -602,7 +618,7 @@ pub const AddCSourceFilesOptions = struct {
602618
files: []const []const u8,
603619
lang: ?CSourceLang = null,
604620
flags: []const []const u8 = &.{},
605-
precompiled_header: ?LazyPath = null,
621+
precompiled_header: ?PrecompiledHeader = null,
606622
};
607623

608624
/// Handy when you have many C/C++ source files and want them all to have the same flags.
@@ -631,7 +647,13 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
631647
addLazyPathDependenciesOnly(m, c_source_files.root);
632648

633649
if (options.precompiled_header) |pch| {
634-
addLazyPathDependenciesOnly(m, pch);
650+
switch (pch) {
651+
.source_header => |src| addLazyPathDependenciesOnly(m, src.path),
652+
.pch_step => |step| {
653+
_ = step.getEmittedBin(); // Indicate there is a dependency on the outputted binary.
654+
addStepDependenciesOnly(m, &step.step);
655+
},
656+
}
635657
}
636658
}
637659

@@ -644,7 +666,13 @@ pub fn addCSourceFile(m: *Module, source: CSourceFile) void {
644666
addLazyPathDependenciesOnly(m, source.file);
645667

646668
if (source.precompiled_header) |pch| {
647-
addLazyPathDependenciesOnly(m, pch);
669+
switch (pch) {
670+
.source_header => |src| addLazyPathDependenciesOnly(m, src.path),
671+
.pch_step => |step| {
672+
_ = step.getEmittedBin(); // Indicate there is a dependency on the outputted binary.
673+
addStepDependenciesOnly(m, &step.step);
674+
},
675+
}
648676
}
649677
}
650678

lib/std/Build/Step/Compile.zig

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
17921792

17931793
fn finalize(step: *Step) !void {
17941794
const compile: *Compile = @fieldParentPtr("step", step);
1795+
const b = step.owner;
17951796

17961797
{
17971798
// Fully recursive iteration including dynamic libraries to detect
@@ -1810,6 +1811,67 @@ fn finalize(step: *Step) !void {
18101811
assert(link_objects.items.len == 1 and link_objects.items[0] == .c_source_file);
18111812
assert(it.next() == null);
18121813
}
1814+
1815+
// add additional compile steps for precompiled headers
1816+
for (compile.root_module.link_objects.items) |*link_object| {
1817+
const pch_ptr: *Module.PrecompiledHeader, const flags: []const []const u8 = blk: {
1818+
switch (link_object.*) {
1819+
.c_source_file => |src| {
1820+
if (src.precompiled_header) |*pch| break :blk .{ pch, src.flags };
1821+
},
1822+
.c_source_files => |src| {
1823+
if (src.precompiled_header) |*pch| break :blk .{ pch, src.flags };
1824+
},
1825+
else => {},
1826+
}
1827+
1828+
continue;
1829+
};
1830+
1831+
switch (pch_ptr.*) {
1832+
.pch_step => {
1833+
// step customized by the user, nothing to do.
1834+
},
1835+
.source_header => |src| {
1836+
const name = switch (src.path) {
1837+
.src_path => |sp| fs.path.basename(sp.sub_path),
1838+
.cwd_relative => |p| fs.path.basename(p),
1839+
.generated => "generated",
1840+
.dependency => "dependency",
1841+
};
1842+
1843+
const step_name = b.fmt("zig build-pch {s}{s} {s}", .{
1844+
name,
1845+
@tagName(compile.root_module.optimize orelse .Debug),
1846+
compile.root_module.resolved_target.?.query.zigTriple(b.allocator) catch @panic("OOM"),
1847+
});
1848+
1849+
// We generate a new compile step for each use,
1850+
// leveraging the cache system to reuse the generated pch file when possible.
1851+
const compile_pch = b.allocator.create(Compile) catch @panic("OOM");
1852+
1853+
// For robustness, suppose all options have an impact on the header compilation.
1854+
// (instead of auditing each llvm version for flags observable from header compilation)
1855+
// So, copy everything and minimally adjust as needed:
1856+
compile_pch.* = compile.*;
1857+
1858+
compile_pch.kind = .pch;
1859+
compile_pch.step.name = step_name;
1860+
compile_pch.name = name;
1861+
compile_pch.out_filename = std.fmt.allocPrint(b.allocator, "{s}.pch", .{name}) catch @panic("OOM");
1862+
compile_pch.installed_headers = .init(b.allocator);
1863+
compile_pch.force_undefined_symbols = .init(b.allocator);
1864+
1865+
compile_pch.root_module.link_objects = .{};
1866+
compile_pch.root_module.addCSourceFile(.{ .file = src.path, .lang = src.lang, .flags = flags });
1867+
1868+
// finalize the parent compile step by modifying it to use the generated pch compile step
1869+
pch_ptr.* = .{ .pch_step = compile_pch };
1870+
_ = compile_pch.getEmittedBin(); // Indicate there is a dependency on the outputted binary.
1871+
compile.root_module.addStepDependenciesOnly(&compile_pch.step);
1872+
},
1873+
}
1874+
}
18131875
}
18141876

18151877
fn make(step: *Step, options: Step.MakeOptions) !void {

test/standalone/c_header/build.zig

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ pub fn build(b: *std.Build) void {
2727
test_step.dependOn(&b.addRunArtifact(exe).step);
2828
}
2929

30-
// testcase 2: precompiled c-header
30+
// testcase 2: precompiled header in C, from a generated file, with a compile step generated automaticcaly, twice with a cache hit
31+
// and it also test the explicit source lang not inferred from file extenson.
3132
{
3233
const exe = b.addExecutable(.{
3334
.name = "pchtest",
@@ -36,28 +37,30 @@ pub fn build(b: *std.Build) void {
3637
.link_libc = true,
3738
});
3839

39-
const pch = b.addPrecompiledCHeader(.{
40-
.name = "pch_c",
41-
.target = target,
42-
.optimize = optimize,
43-
.link_libc = true,
44-
}, .{
45-
.file = b.path("include_a.h"),
40+
const generated_header = b.addWriteFiles().add("generated.h",
41+
\\ /* generated file */
42+
\\ #include "include_a.h"
43+
);
44+
45+
exe.addCSourceFile(.{
46+
.file = b.path("test.c2"),
4647
.flags = &[_][]const u8{},
47-
.lang = .h,
48+
.lang = .c,
49+
.precompiled_header = .{ .source_header = .{ .path = generated_header, .lang = .h } },
4850
});
49-
5051
exe.addCSourceFiles(.{
5152
.files = &.{"test.c"},
5253
.flags = &[_][]const u8{},
5354
.lang = .c,
54-
.precompiled_header = pch.getEmittedBin(),
55+
.precompiled_header = .{ .source_header = .{ .path = generated_header, .lang = .h } },
5556
});
5657

58+
exe.addIncludePath(b.path("."));
59+
5760
test_step.dependOn(&b.addRunArtifact(exe).step);
5861
}
5962

60-
// testcase 3: precompiled c++-header
63+
// testcase 3: precompiled header in C++, from a .h file that must be precompiled as c++, with an explicit pch compile step.
6164
{
6265
const exe = b.addExecutable(.{
6366
.name = "pchtest++",
@@ -80,7 +83,7 @@ pub fn build(b: *std.Build) void {
8083
exe.addCSourceFile(.{
8184
.file = b.path("test.cpp"),
8285
.flags = &[_][]const u8{},
83-
.precompiled_header = pch.getEmittedBin(),
86+
.precompiled_header = .{ .pch_step = pch },
8487
});
8588

8689
test_step.dependOn(&b.addRunArtifact(exe).step);

test/standalone/c_header/include_a.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <iostream>
1010
#else
1111
#include <stdio.h>
12+
#include <stdbool.h>
1213
#endif
1314

1415
#define A_INCLUDED 1

test/standalone/c_header/test.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010
#error "pch not included"
1111
#endif
1212

13+
extern int func(real a, bool cond);
14+
1315
int main(int argc, char *argv[])
1416
{
1517
real a = 0.123;
16-
17-
if (argc > 1) {
18-
fprintf(stdout, "abs(%g)=%g\n", a, fabs(a));
19-
}
20-
21-
return EXIT_SUCCESS;
18+
return func(a, (argc > 1));
2219
}

test/standalone/c_header/test.c2

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
// includes commented out to make sure the symbols come from the precompiled header.
3+
//#include "include_a.h"
4+
//#include "include_b.h"
5+
6+
#ifndef A_INCLUDED
7+
#error "pch not included"
8+
#endif
9+
#ifndef B_INCLUDED
10+
#error "pch not included"
11+
#endif
12+
13+
int func(real a, bool cond)
14+
{
15+
if (cond) {
16+
fprintf(stdout, "abs(%g)=%g\n", a, fabs(a));
17+
}
18+
19+
return EXIT_SUCCESS;
20+
}

0 commit comments

Comments
 (0)