From 304b8f88f77df1887c6132d338c23b96eac2c990 Mon Sep 17 00:00:00 2001 From: Xavier Bouchoux Date: Sat, 27 Jan 2024 13:40:30 +0100 Subject: [PATCH] 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. --- src/Compilation.zig | 8 ++++---- src/main.zig | 11 ++++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index c533f2fae79d..80af12920714 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -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"), } @@ -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) { diff --git a/src/main.zig b/src/main.zig index 72b25583580f..17280ed987fd 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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 \\ @@ -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")) { @@ -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] @@ -712,6 +716,7 @@ const ArgMode = union(enum) { build: std.builtin.OutputMode, cc, cpp, + pch, translate_c, zig_test, run, @@ -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;