Skip to content

Commit 139bff2

Browse files
committed
std.Build: add precompiled C header support
1 parent 5b78d47 commit 139bff2

File tree

9 files changed

+227
-18
lines changed

9 files changed

+227
-18
lines changed

lib/std/Build.zig

+23
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,29 @@ pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile {
700700
});
701701
}
702702

703+
pub const PchOptions = struct {
704+
name: []const u8,
705+
target: CrossTarget,
706+
optimize: std.builtin.OptimizeMode,
707+
max_rss: usize = 0,
708+
cpp_header: bool = false,
709+
};
710+
711+
pub fn addPrecompiledCHeader(b: *Build, options: PchOptions, source: Step.Compile.CSourceFile) *Step.Compile {
712+
const pch = Step.Compile.create(b, .{
713+
.name = options.name,
714+
.target = options.target,
715+
.optimize = options.optimize,
716+
.kind = .pch,
717+
.max_rss = options.max_rss,
718+
.use_llvm = true,
719+
});
720+
pch.is_linking_libc = !options.cpp_header;
721+
pch.is_linking_libcpp = options.cpp_header;
722+
pch.addCSourceFile(source);
723+
return pch;
724+
}
725+
703726
pub const SharedLibraryOptions = struct {
704727
name: []const u8,
705728
root_source_file: ?LazyPath = null,

lib/std/Build/Step/Compile.zig

+59-17
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ root_src: ?LazyPath,
8383
out_lib_filename: []const u8,
8484
modules: std.StringArrayHashMap(*Module),
8585

86+
precompiled_header: ?*Compile,
8687
link_objects: ArrayList(LinkObject),
8788
include_dirs: ArrayList(IncludeDir),
8889
c_macros: ArrayList([]const u8),
@@ -435,6 +436,7 @@ pub const Kind = enum {
435436
exe,
436437
lib,
437438
obj,
439+
pch,
438440
@"test",
439441
};
440442

@@ -458,6 +460,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
458460
.exe => "zig build-exe",
459461
.lib => "zig build-lib",
460462
.obj => "zig build-obj",
463+
.pch => "zig build-pch",
461464
.@"test" => "zig test",
462465
},
463466
name_adjusted,
@@ -467,20 +470,24 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
467470

468471
const target_info = NativeTargetInfo.detect(options.target) catch @panic("unhandled error");
469472

470-
const out_filename = std.zig.binNameAlloc(owner.allocator, .{
471-
.root_name = name,
472-
.target = target_info.target,
473-
.output_mode = switch (options.kind) {
474-
.lib => .Lib,
475-
.obj => .Obj,
476-
.exe, .@"test" => .Exe,
477-
},
478-
.link_mode = if (options.linkage) |some| @as(std.builtin.LinkMode, switch (some) {
479-
.dynamic => .Dynamic,
480-
.static => .Static,
481-
}) else null,
482-
.version = options.version,
483-
}) catch @panic("OOM");
473+
const out_filename = if (options.kind == .pch)
474+
std.fmt.allocPrint(owner.allocator, "{s}.pch", .{name}) catch @panic("OOM")
475+
else
476+
std.zig.binNameAlloc(owner.allocator, .{
477+
.root_name = name,
478+
.target = target_info.target,
479+
.output_mode = switch (options.kind) {
480+
.lib => .Lib,
481+
.obj => .Obj,
482+
.exe, .@"test" => .Exe,
483+
.pch => unreachable,
484+
},
485+
.link_mode = if (options.linkage) |some| @as(std.builtin.LinkMode, switch (some) {
486+
.dynamic => .Dynamic,
487+
.static => .Static,
488+
}) else null,
489+
.version = options.version,
490+
}) catch @panic("OOM");
484491

485492
const self = owner.allocator.create(Compile) catch @panic("OOM");
486493
self.* = .{
@@ -514,6 +521,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
514521
.lib_paths = ArrayList(LazyPath).init(owner.allocator),
515522
.rpaths = ArrayList(LazyPath).init(owner.allocator),
516523
.installed_headers = ArrayList(*Step).init(owner.allocator),
524+
.precompiled_header = null,
517525
.c_std = std.Build.CStd.C99,
518526
.zig_lib_dir = null,
519527
.main_mod_path = null,
@@ -976,6 +984,8 @@ pub const AddCSourceFilesOptions = struct {
976984

977985
/// Handy when you have many C/C++ source files and want them all to have the same flags.
978986
pub fn addCSourceFiles(self: *Compile, options: AddCSourceFilesOptions) void {
987+
assert(self.kind != .pch); // pch can only be generated from a single C header file
988+
979989
const b = self.step.owner;
980990
const c_source_files = b.allocator.create(CSourceFiles) catch @panic("OOM");
981991

@@ -991,6 +1001,8 @@ pub fn addCSourceFiles(self: *Compile, options: AddCSourceFilesOptions) void {
9911001
}
9921002

9931003
pub fn addCSourceFile(self: *Compile, source: CSourceFile) void {
1004+
assert(self.kind != .pch or self.link_objects.items.len == 0); // pch can only be generated from a single C header file
1005+
9941006
const b = self.step.owner;
9951007
const c_source_file = b.allocator.create(CSourceFile) catch @panic("OOM");
9961008
c_source_file.* = source.dupe(b);
@@ -1107,23 +1119,39 @@ pub fn getEmittedLlvmBc(self: *Compile) LazyPath {
11071119
}
11081120

11091121
pub fn addAssemblyFile(self: *Compile, source: LazyPath) void {
1122+
assert(self.kind != .pch); // pch can only be generated from a single C header file
1123+
11101124
const b = self.step.owner;
11111125
const source_duped = source.dupe(b);
11121126
self.link_objects.append(.{ .assembly_file = source_duped }) catch @panic("OOM");
11131127
source_duped.addStepDependencies(&self.step);
11141128
}
11151129

11161130
pub fn addObjectFile(self: *Compile, source: LazyPath) void {
1131+
assert(self.kind != .pch); // pch can only be generated from a single C header file
1132+
11171133
const b = self.step.owner;
11181134
self.link_objects.append(.{ .static_path = source.dupe(b) }) catch @panic("OOM");
11191135
source.addStepDependencies(&self.step);
11201136
}
11211137

11221138
pub fn addObject(self: *Compile, obj: *Compile) void {
1139+
assert(self.kind != .pch); // pch can only be generated from a single C header file
1140+
11231141
assert(obj.kind == .obj);
11241142
self.linkLibraryOrObject(obj);
11251143
}
11261144

1145+
pub fn addPrecompiledCHeader(self: *Compile, pch: *Compile) void {
1146+
assert(self.kind != .pch); // pch can only be generated from a single C header file
1147+
assert(pch.kind == .pch);
1148+
1149+
if (self.precompiled_header != null) @panic("Precompiled header already defined.");
1150+
self.precompiled_header = pch;
1151+
1152+
pch.getEmittedBin().addStepDependencies(&self.step);
1153+
}
1154+
11271155
pub fn addAfterIncludePath(self: *Compile, path: LazyPath) void {
11281156
const b = self.step.owner;
11291157
self.include_dirs.append(IncludeDir{ .path_after = path.dupe(b) }) catch @panic("OOM");
@@ -1415,7 +1443,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
14151443
const cmd = switch (self.kind) {
14161444
.lib => "build-lib",
14171445
.exe => "build-exe",
1418-
.obj => "build-obj",
1446+
.obj, .pch => "build-obj",
14191447
.@"test" => "test",
14201448
};
14211449
try zig_args.append(cmd);
@@ -1431,6 +1459,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
14311459
try zig_args.append(try std.fmt.allocPrint(b.allocator, "-ofmt={s}", .{@tagName(ofmt)}));
14321460
}
14331461

1462+
if (self.kind == .pch) {
1463+
try zig_args.append("-x");
1464+
try zig_args.append(if (self.is_linking_libcpp) "c++-header" else "c-header");
1465+
}
1466+
14341467
switch (self.entry) {
14351468
.default => {},
14361469
.disabled => try zig_args.append("-fno-entry"),
@@ -1482,6 +1515,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
14821515
.other_step => |other| switch (other.kind) {
14831516
.exe => @panic("Cannot link with an executable build artifact"),
14841517
.@"test" => @panic("Cannot link with a test"),
1518+
.pch => @panic("Cannot link with a precompiled header file"),
14851519
.obj => {
14861520
try zig_args.append(other.getEmittedBin().getPath(b));
14871521
},
@@ -1578,7 +1612,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
15781612
},
15791613

15801614
.c_source_file => |c_source_file| {
1581-
if (c_source_file.flags.len == 0) {
1615+
if (c_source_file.flags.len == 0 and self.precompiled_header == null) {
15821616
if (prev_has_cflags) {
15831617
try zig_args.append("-cflags");
15841618
try zig_args.append("--");
@@ -1589,14 +1623,18 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
15891623
for (c_source_file.flags) |arg| {
15901624
try zig_args.append(arg);
15911625
}
1626+
if (self.precompiled_header) |pch| {
1627+
try zig_args.append("-include-pch");
1628+
try zig_args.append(pch.getEmittedBin().getPath(b));
1629+
}
15921630
try zig_args.append("--");
15931631
prev_has_cflags = true;
15941632
}
15951633
try zig_args.append(c_source_file.file.getPath(b));
15961634
},
15971635

15981636
.c_source_files => |c_source_files| {
1599-
if (c_source_files.flags.len == 0) {
1637+
if (c_source_files.flags.len == 0 and self.precompiled_header == null) {
16001638
if (prev_has_cflags) {
16011639
try zig_args.append("-cflags");
16021640
try zig_args.append("--");
@@ -1607,6 +1645,10 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
16071645
for (c_source_files.flags) |flag| {
16081646
try zig_args.append(flag);
16091647
}
1648+
if (self.precompiled_header) |pch| {
1649+
try zig_args.append("-include-pch");
1650+
try zig_args.append(pch.getEmittedBin().getPath(b));
1651+
}
16101652
try zig_args.append("--");
16111653
prev_has_cflags = true;
16121654
}

lib/std/Build/Step/InstallArtifact.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile, options: Options) *Ins
5656
const dest_dir: ?InstallDir = switch (options.dest_dir) {
5757
.disabled => null,
5858
.default => switch (artifact.kind) {
59-
.obj => @panic("object files have no standard installation procedure"),
59+
.obj, .pch => @panic("object files have no standard installation procedure"),
6060
.exe, .@"test" => InstallDir{ .bin = {} },
6161
.lib => InstallDir{ .lib = {} },
6262
},

test/standalone.zig

+4
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ pub const build_cases = [_]BuildCase{
258258
.build_root = "test/standalone/ios",
259259
.import = @import("standalone/ios/build.zig"),
260260
},
261+
.{
262+
.build_root = "test/standalone/pch",
263+
.import = @import("standalone/pch/build.zig"),
264+
},
261265
};
262266

263267
const std = @import("std");

test/standalone/pch/build.zig

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const std = @import("std");
2+
const assert = std.debug.assert;
3+
const Allocator = std.mem.Allocator;
4+
const Builder = std.build.Builder;
5+
const LibExeObjStep = std.build.LibExeObjStep;
6+
const CrossTarget = std.zig.CrossTarget;
7+
8+
pub fn build(b: *Builder) void {
9+
const test_step = b.step("test", "Test it");
10+
b.default_step = test_step;
11+
12+
const target = b.standardTargetOptions(.{});
13+
const mode = b.standardOptimizeOption(.{});
14+
15+
// c-header
16+
{
17+
const exe = b.addExecutable(.{
18+
.name = "pchtest",
19+
.target = target,
20+
.optimize = mode,
21+
.link_libc = true,
22+
});
23+
24+
const pch = b.addPrecompiledCHeader(.{
25+
.name = "pch_c",
26+
.target = target,
27+
.optimize = mode,
28+
.cpp_header = false,
29+
}, .{
30+
.file = .{ .path = "include_a.h" },
31+
.flags = &[_][]const u8{},
32+
});
33+
34+
exe.addPrecompiledCHeader(pch);
35+
36+
exe.addCSourceFile(.{
37+
.file = .{ .path = "test.c" },
38+
.flags = &[_][]const u8{},
39+
});
40+
41+
test_step.dependOn(&b.addRunArtifact(exe).step);
42+
}
43+
44+
// c++-header
45+
{
46+
const exe = b.addExecutable(.{
47+
.name = "pchtest++",
48+
.target = target,
49+
.optimize = mode,
50+
.link_libc = true,
51+
});
52+
exe.linkLibCpp();
53+
54+
const pch = b.addPrecompiledCHeader(.{
55+
.name = "pch_c++",
56+
.target = target,
57+
.optimize = mode,
58+
.cpp_header = true,
59+
}, .{
60+
.file = .{ .path = "include_a.h" },
61+
.flags = &[_][]const u8{},
62+
});
63+
64+
exe.addPrecompiledCHeader(pch);
65+
66+
exe.addCSourceFile(.{
67+
.file = .{ .path = "test.cpp" },
68+
.flags = &[_][]const u8{},
69+
});
70+
71+
test_step.dependOn(&b.addRunArtifact(exe).step);
72+
}
73+
}

test/standalone/pch/include_a.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include "include_b.h"
4+
5+
#include <string.h>
6+
#include <stdlib.h>
7+
8+
#if defined(__cplusplus)
9+
#include <iostream>
10+
#else
11+
#include <stdio.h>
12+
#endif
13+
14+
#define A_INCLUDED 1
15+

test/standalone/pch/include_b.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include <math.h>
4+
5+
typedef double real;
6+
7+
#define B_INCLUDED 1

test/standalone/pch/test.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
// includes commented out to make sure the symbols come from the precompiled header.
3+
//#include "includeA.h"
4+
//#include "includeB.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 main(int argc, char *argv[])
14+
{
15+
real a = 0.123;
16+
17+
if (argc > 1) {
18+
fprintf(stdout, "abs(%g)=%g\n", a, abs(a));
19+
}
20+
21+
return EXIT_SUCCESS;
22+
}

test/standalone/pch/test.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
// includes commented out to make sure the symbols come from the precompiled header.
3+
//#include "includeA.h"
4+
//#include "includeB.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 main(int argc, char *argv[])
14+
{
15+
real a = -0.123;
16+
17+
if (argc > 1) {
18+
std::cout << "abs(" << a << ")=" << fabs(a) << "\n";
19+
}
20+
21+
return EXIT_SUCCESS;
22+
}
23+

0 commit comments

Comments
 (0)