Skip to content

Commit dcdd80b

Browse files
committed
zig: add build-pch command to emit precompiled C header.
usage example: `zig build-pch -lc++ -x c++-header test.h` `zig run -lc++ -cflags -include-pch test.pch -- main.cpp` It builds the file.pch with llvm "-fpch-validate-input-files-content", so it includes data for better integration with zig caching system.
1 parent a69d403 commit dcdd80b

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/Compilation.zig

+4-4
Original file line numberDiff line numberDiff line change
@@ -4896,11 +4896,11 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr
48964896
else
48974897
"/dev/null";
48984898

4899-
try argv.ensureUnusedCapacity(6);
4899+
try argv.ensureUnusedCapacity(7);
49004900
switch (comp.clang_preprocessor_mode) {
49014901
.no => argv.appendSliceAssumeCapacity(&.{ "-c", "-o", out_obj_path }),
49024902
.yes => argv.appendSliceAssumeCapacity(&.{ "-E", "-o", out_obj_path }),
4903-
.pch => argv.appendSliceAssumeCapacity(&.{ "-Xclang", "-emit-pch", "-o", out_obj_path }),
4903+
.pch => argv.appendSliceAssumeCapacity(&.{ "-Xclang", "-emit-pch", "-fpch-validate-input-files-content", "-o", out_obj_path }),
49044904
.stdout => argv.appendAssumeCapacity("-E"),
49054905
}
49064906

@@ -4939,11 +4939,11 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr
49394939
try argv.appendSlice(c_object.src.extra_flags);
49404940
try argv.appendSlice(c_object.src.cache_exempt_flags);
49414941

4942-
try argv.ensureUnusedCapacity(6);
4942+
try argv.ensureUnusedCapacity(7);
49434943
switch (comp.clang_preprocessor_mode) {
49444944
.no => argv.appendSliceAssumeCapacity(&.{ "-c", "-o", out_obj_path }),
49454945
.yes => argv.appendSliceAssumeCapacity(&.{ "-E", "-o", out_obj_path }),
4946-
.pch => argv.appendSliceAssumeCapacity(&.{ "-Xclang", "-emit-pch", "-o", out_obj_path }),
4946+
.pch => argv.appendSliceAssumeCapacity(&.{ "-Xclang", "-emit-pch", "-fpch-validate-input-files-content", "-o", out_obj_path }),
49474947
.stdout => argv.appendAssumeCapacity("-E"),
49484948
}
49494949
if (out_diag_path) |diag_file_path| {

src/main.zig

+10-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const normal_usage =
8181
\\ build-exe Create executable from source or object files
8282
\\ build-lib Create library from source or object files
8383
\\ build-obj Create object from source or object files
84+
\\ build-pch Create a precompiled header from a c or c++ header
8485
\\ test Perform unit testing
8586
\\ run Create executable and run immediately
8687
\\
@@ -262,6 +263,8 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
262263
} else if (mem.eql(u8, cmd, "build-obj")) {
263264
dev.check(.build_obj_command);
264265
return buildOutputType(gpa, arena, args, .{ .build = .Obj });
266+
} else if (mem.eql(u8, cmd, "build-pch")) {
267+
return buildOutputType(gpa, arena, args, .pch);
265268
} else if (mem.eql(u8, cmd, "test")) {
266269
dev.check(.test_command);
267270
return buildOutputType(gpa, arena, args, .zig_test);
@@ -382,6 +385,7 @@ const usage_build_generic =
382385
\\Usage: zig build-exe [options] [files]
383386
\\ zig build-lib [options] [files]
384387
\\ zig build-obj [options] [files]
388+
\\ zig build-pch [options] [files]
385389
\\ zig test [options] [files]
386390
\\ zig run [options] [files] [-- [args]]
387391
\\ zig translate-c [options] [file]
@@ -733,6 +737,7 @@ const ArgMode = union(enum) {
733737
build: std.builtin.OutputMode,
734738
cc,
735739
cpp,
740+
pch,
736741
translate_c,
737742
zig_test,
738743
run,
@@ -1019,11 +1024,15 @@ fn buildOutputType(
10191024
var n_jobs: ?u32 = null;
10201025

10211026
switch (arg_mode) {
1022-
.build, .translate_c, .zig_test, .run => {
1027+
.build, .translate_c, .zig_test, .run, .pch => {
10231028
switch (arg_mode) {
10241029
.build => |m| {
10251030
create_module.opts.output_mode = m;
10261031
},
1032+
.pch => {
1033+
create_module.opts.output_mode = .Obj;
1034+
clang_preprocessor_mode = .pch;
1035+
},
10271036
.translate_c => {
10281037
emit_bin = .no;
10291038
create_module.opts.output_mode = .Obj;

0 commit comments

Comments
 (0)