Skip to content

Commit

Permalink
zig: add build-pch command to emit precompiled C header.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xxxbxxx committed Apr 8, 2024
1 parent d979df5 commit 304b8f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4571,11 +4571,11 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
else
"/dev/null";

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

Expand Down Expand Up @@ -4610,11 +4610,11 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
try argv.appendSlice(c_object.src.extra_flags);
try argv.appendSlice(c_object.src.cache_exempt_flags);

try argv.ensureUnusedCapacity(6);
try argv.ensureUnusedCapacity(7);
switch (comp.clang_preprocessor_mode) {
.no => argv.appendSliceAssumeCapacity(&.{ "-c", "-o", out_obj_path }),
.yes => argv.appendSliceAssumeCapacity(&.{ "-E", "-o", out_obj_path }),
.pch => argv.appendSliceAssumeCapacity(&.{ "-Xclang", "-emit-pch", "-o", out_obj_path }),
.pch => argv.appendSliceAssumeCapacity(&.{ "-Xclang", "-emit-pch", "-fpch-validate-input-files-content", "-o", out_obj_path }),
.stdout => argv.appendAssumeCapacity("-E"),
}
if (comp.clang_passthrough_mode) {
Expand Down
11 changes: 10 additions & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const normal_usage =
\\ build-exe Create executable from source or object files
\\ build-lib Create library from source or object files
\\ build-obj Create object from source or object files
\\ build-pch Create a precompiled header from a c or c++ header
\\ test Perform unit testing
\\ run Create executable and run immediately
\\
Expand Down Expand Up @@ -262,6 +263,8 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
return buildOutputType(gpa, arena, args, .{ .build = .Lib });
} else if (mem.eql(u8, cmd, "build-obj")) {
return buildOutputType(gpa, arena, args, .{ .build = .Obj });
} else if (mem.eql(u8, cmd, "build-pch")) {
return buildOutputType(gpa, arena, args, .pch);
} else if (mem.eql(u8, cmd, "test")) {
return buildOutputType(gpa, arena, args, .zig_test);
} else if (mem.eql(u8, cmd, "run")) {
Expand Down Expand Up @@ -366,6 +369,7 @@ const usage_build_generic =
\\Usage: zig build-exe [options] [files]
\\ zig build-lib [options] [files]
\\ zig build-obj [options] [files]
\\ zig build-pch [options] [files]
\\ zig test [options] [files]
\\ zig run [options] [files] [-- [args]]
\\ zig translate-c [options] [file]
Expand Down Expand Up @@ -712,6 +716,7 @@ const ArgMode = union(enum) {
build: std.builtin.OutputMode,
cc,
cpp,
pch,
translate_c,
zig_test,
run,
Expand Down Expand Up @@ -997,11 +1002,15 @@ fn buildOutputType(
var color: Color = if (native_os == .wasi or EnvVar.NO_COLOR.isSet()) .off else .auto;

switch (arg_mode) {
.build, .translate_c, .zig_test, .run => {
.build, .translate_c, .zig_test, .run, .pch => {
switch (arg_mode) {
.build => |m| {
create_module.opts.output_mode = m;
},
.pch => {
create_module.opts.output_mode = .Obj;
clang_preprocessor_mode = .pch;
},
.translate_c => {
emit_bin = .no;
create_module.opts.output_mode = .Obj;
Expand Down

0 comments on commit 304b8f8

Please sign in to comment.