Skip to content

Commit 6ed79a6

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 c1961ce commit 6ed79a6

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
@@ -147,6 +147,22 @@ pub const CSourceLang = enum {
147147
}
148148
};
149149

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

161177
pub const CSourceFile = struct {
162178
file: LazyPath,
163179
lang: ?CSourceLang = null,
164180
flags: []const []const u8 = &.{},
165-
precompiled_header: ?LazyPath = null,
181+
precompiled_header: ?PrecompiledHeader = null,
166182

167183
pub fn dupe(file: CSourceFile, b: *std.Build) CSourceFile {
168184
return .{
@@ -436,7 +452,7 @@ fn addStepDependencies(m: *Module, module: *Module, dependee: *Step) void {
436452
}
437453
}
438454

439-
fn addStepDependenciesOnly(m: *Module, dependee: *Step) void {
455+
pub fn addStepDependenciesOnly(m: *Module, dependee: *Step) void {
440456
for (m.depending_steps.keys()) |compile| {
441457
compile.step.dependOn(dependee);
442458
}
@@ -600,7 +616,7 @@ pub const AddCSourceFilesOptions = struct {
600616
files: []const []const u8,
601617
lang: ?CSourceLang = null,
602618
flags: []const []const u8 = &.{},
603-
precompiled_header: ?LazyPath = null,
619+
precompiled_header: ?PrecompiledHeader = null,
604620
};
605621

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

631647
if (options.precompiled_header) |pch| {
632-
addLazyPathDependenciesOnly(m, pch);
648+
switch (pch) {
649+
.source_header => |src| addLazyPathDependenciesOnly(m, src.path),
650+
.pch_step => |step| {
651+
_ = step.getEmittedBin(); // Indicate there is a dependency on the outputted binary.
652+
addStepDependenciesOnly(m, &step.step);
653+
},
654+
}
633655
}
634656
}
635657

@@ -642,7 +664,13 @@ pub fn addCSourceFile(m: *Module, source: CSourceFile) void {
642664
addLazyPathDependenciesOnly(m, source.file);
643665

644666
if (source.precompiled_header) |pch| {
645-
addLazyPathDependenciesOnly(m, pch);
667+
switch (pch) {
668+
.source_header => |src| addLazyPathDependenciesOnly(m, src.path),
669+
.pch_step => |step| {
670+
_ = step.getEmittedBin(); // Indicate there is a dependency on the outputted binary.
671+
addStepDependenciesOnly(m, &step.step);
672+
},
673+
}
646674
}
647675
}
648676

lib/std/Build/Step/Compile.zig

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

18411841
fn finalize(step: *Step) !void {
18421842
const compile: *Compile = @fieldParentPtr("step", step);
1843+
const b = step.owner;
18431844

18441845
{
18451846
// Fully recursive iteration including dynamic libraries to detect
@@ -1858,6 +1859,67 @@ fn finalize(step: *Step) !void {
18581859
assert(link_objects.items.len == 1 and link_objects.items[0] == .c_source_file);
18591860
assert(it.next() == null);
18601861
}
1862+
1863+
// add additional compile steps for precompiled headers
1864+
for (compile.root_module.link_objects.items) |*link_object| {
1865+
const pch_ptr: *Module.PrecompiledHeader, const flags: []const []const u8 = blk: {
1866+
switch (link_object.*) {
1867+
.c_source_file => |src| {
1868+
if (src.precompiled_header) |*pch| break :blk .{ pch, src.flags };
1869+
},
1870+
.c_source_files => |src| {
1871+
if (src.precompiled_header) |*pch| break :blk .{ pch, src.flags };
1872+
},
1873+
else => {},
1874+
}
1875+
1876+
continue;
1877+
};
1878+
1879+
switch (pch_ptr.*) {
1880+
.pch_step => {
1881+
// step customized by the user, nothing to do.
1882+
},
1883+
.source_header => |src| {
1884+
const name = switch (src.path) {
1885+
.src_path => |sp| fs.path.basename(sp.sub_path),
1886+
.cwd_relative => |p| fs.path.basename(p),
1887+
.generated => "generated",
1888+
.dependency => "dependency",
1889+
};
1890+
1891+
const step_name = b.fmt("zig build-pch {s}{s} {s}", .{
1892+
name,
1893+
@tagName(compile.root_module.optimize orelse .Debug),
1894+
compile.root_module.resolved_target.?.query.zigTriple(b.allocator) catch @panic("OOM"),
1895+
});
1896+
1897+
// We generate a new compile step for each use,
1898+
// leveraging the cache system to reuse the generated pch file when possible.
1899+
const compile_pch = b.allocator.create(Compile) catch @panic("OOM");
1900+
1901+
// For robustness, suppose all options have an impact on the header compilation.
1902+
// (instead of auditing each llvm version for flags observable from header compilation)
1903+
// So, copy everything and minimally adjust as needed:
1904+
compile_pch.* = compile.*;
1905+
1906+
compile_pch.kind = .pch;
1907+
compile_pch.step.name = step_name;
1908+
compile_pch.name = name;
1909+
compile_pch.out_filename = std.fmt.allocPrint(b.allocator, "{s}.pch", .{name}) catch @panic("OOM");
1910+
compile_pch.installed_headers = .init(b.allocator);
1911+
compile_pch.force_undefined_symbols = .init(b.allocator);
1912+
1913+
compile_pch.root_module.link_objects = .{};
1914+
compile_pch.root_module.addCSourceFile(.{ .file = src.path, .lang = src.lang, .flags = flags });
1915+
1916+
// finalize the parent compile step by modifying it to use the generated pch compile step
1917+
pch_ptr.* = .{ .pch_step = compile_pch };
1918+
_ = compile_pch.getEmittedBin(); // Indicate there is a dependency on the outputted binary.
1919+
compile.root_module.addStepDependenciesOnly(&compile_pch.step);
1920+
},
1921+
}
1922+
}
18611923
}
18621924

18631925
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)