Skip to content

Commit 097a177

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 b336b0a commit 097a177

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
@@ -146,21 +146,37 @@ pub const CSourceLang = enum {
146146
}
147147
};
148148

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

159175
pub const CSourceFile = struct {
160176
file: LazyPath,
161177
lang: ?CSourceLang = null,
162178
flags: []const []const u8 = &.{},
163-
precompiled_header: ?LazyPath = null,
179+
precompiled_header: ?PrecompiledHeader = null,
164180

165181
pub fn dupe(file: CSourceFile, b: *std.Build) CSourceFile {
166182
return .{
@@ -396,7 +412,7 @@ fn addStepDependencies(m: *Module, module: *Module, dependee: *Step) void {
396412
}
397413
}
398414

399-
fn addStepDependenciesOnly(m: *Module, dependee: *Step) void {
415+
pub fn addStepDependenciesOnly(m: *Module, dependee: *Step) void {
400416
for (m.depending_steps.keys()) |compile| {
401417
compile.step.dependOn(dependee);
402418
}
@@ -560,7 +576,7 @@ pub const AddCSourceFilesOptions = struct {
560576
files: []const []const u8,
561577
lang: ?CSourceLang = null,
562578
flags: []const []const u8 = &.{},
563-
precompiled_header: ?LazyPath = null,
579+
precompiled_header: ?PrecompiledHeader = null,
564580
};
565581

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

591607
if (options.precompiled_header) |pch| {
592-
addLazyPathDependenciesOnly(m, pch);
608+
switch (pch) {
609+
.source_header => |src| addLazyPathDependenciesOnly(m, src.path),
610+
.pch_step => |step| {
611+
_ = step.getEmittedBin(); // Indicate there is a dependency on the outputted binary.
612+
addStepDependenciesOnly(m, &step.step);
613+
},
614+
}
593615
}
594616
}
595617

@@ -602,7 +624,13 @@ pub fn addCSourceFile(m: *Module, source: CSourceFile) void {
602624
addLazyPathDependenciesOnly(m, source.file);
603625

604626
if (source.precompiled_header) |pch| {
605-
addLazyPathDependenciesOnly(m, pch);
627+
switch (pch) {
628+
.source_header => |src| addLazyPathDependenciesOnly(m, src.path),
629+
.pch_step => |step| {
630+
_ = step.getEmittedBin(); // Indicate there is a dependency on the outputted binary.
631+
addStepDependenciesOnly(m, &step.step);
632+
},
633+
}
606634
}
607635
}
608636

lib/std/Build/Step/Compile.zig

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

18251825
fn finalize(step: *Step) !void {
18261826
const compile: *Compile = @fieldParentPtr("step", step);
1827+
const b = step.owner;
18271828

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

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