Skip to content

Commit d639b79

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 8773317 commit d639b79

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/Compilation.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4424,11 +4424,11 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
44244424
else
44254425
"/dev/null";
44264426

4427-
try argv.ensureUnusedCapacity(6);
4427+
try argv.ensureUnusedCapacity(7);
44284428
switch (comp.clang_preprocessor_mode) {
44294429
.no => argv.appendSliceAssumeCapacity(&.{ "-c", "-o", out_obj_path }),
44304430
.yes => argv.appendSliceAssumeCapacity(&.{ "-E", "-o", out_obj_path }),
4431-
.pch => argv.appendSliceAssumeCapacity(&.{ "-Xclang", "-emit-pch", "-o", out_obj_path }),
4431+
.pch => argv.appendSliceAssumeCapacity(&.{ "-Xclang", "-emit-pch", "-fpch-validate-input-files-content", "-o", out_obj_path }),
44324432
.stdout => argv.appendAssumeCapacity("-E"),
44334433
}
44344434

@@ -4463,11 +4463,11 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
44634463
try argv.appendSlice(c_object.src.extra_flags);
44644464
try argv.appendSlice(c_object.src.cache_exempt_flags);
44654465

4466-
try argv.ensureUnusedCapacity(6);
4466+
try argv.ensureUnusedCapacity(7);
44674467
switch (comp.clang_preprocessor_mode) {
44684468
.no => argv.appendSliceAssumeCapacity(&.{ "-c", "-o", out_obj_path }),
44694469
.yes => argv.appendSliceAssumeCapacity(&.{ "-E", "-o", out_obj_path }),
4470-
.pch => argv.appendSliceAssumeCapacity(&.{ "-Xclang", "-emit-pch", "-o", out_obj_path }),
4470+
.pch => argv.appendSliceAssumeCapacity(&.{ "-Xclang", "-emit-pch", "-fpch-validate-input-files-content", "-o", out_obj_path }),
44714471
.stdout => argv.appendAssumeCapacity("-E"),
44724472
}
44734473
if (comp.clang_passthrough_mode) {

src/main.zig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const normal_usage =
9090
\\ build-exe Create executable from source or object files
9191
\\ build-lib Create library from source or object files
9292
\\ build-obj Create object from source or object files
93+
\\ build-pch Create a precompiled header from a c or c++ header
9394
\\ test Perform unit testing
9495
\\ run Create executable and run immediately
9596
\\
@@ -279,6 +280,8 @@ pub fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
279280
return buildOutputType(gpa, arena, args, .{ .build = .Lib });
280281
} else if (mem.eql(u8, cmd, "build-obj")) {
281282
return buildOutputType(gpa, arena, args, .{ .build = .Obj });
283+
} else if (mem.eql(u8, cmd, "build-pch")) {
284+
return buildOutputType(gpa, arena, args, .pch);
282285
} else if (mem.eql(u8, cmd, "test")) {
283286
return buildOutputType(gpa, arena, args, .zig_test);
284287
} else if (mem.eql(u8, cmd, "run")) {
@@ -355,6 +358,7 @@ const usage_build_generic =
355358
\\Usage: zig build-exe [options] [files]
356359
\\ zig build-lib [options] [files]
357360
\\ zig build-obj [options] [files]
361+
\\ zig build-pch [options] [files]
358362
\\ zig test [options] [files]
359363
\\ zig run [options] [files] [-- [args]]
360364
\\ zig translate-c [options] [file]
@@ -697,6 +701,7 @@ const ArgMode = union(enum) {
697701
build: std.builtin.OutputMode,
698702
cc,
699703
cpp,
704+
pch,
700705
translate_c,
701706
zig_test,
702707
run,
@@ -976,11 +981,15 @@ fn buildOutputType(
976981
var color: Color = if (builtin.os.tag == .wasi or EnvVar.NO_COLOR.isSet()) .off else .auto;
977982

978983
switch (arg_mode) {
979-
.build, .translate_c, .zig_test, .run => {
984+
.build, .translate_c, .zig_test, .run, .pch => {
980985
switch (arg_mode) {
981986
.build => |m| {
982987
create_module.opts.output_mode = m;
983988
},
989+
.pch => {
990+
create_module.opts.output_mode = .Obj;
991+
clang_preprocessor_mode = .pch;
992+
},
984993
.translate_c => {
985994
emit_bin = .no;
986995
create_module.opts.output_mode = .Obj;

0 commit comments

Comments
 (0)