@@ -267,6 +267,7 @@ pub const Kind = enum {
267
267
exe ,
268
268
lib ,
269
269
obj ,
270
+ pch ,
270
271
@"test" ,
271
272
};
272
273
@@ -350,24 +351,29 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
350
351
.exe = > "zig build-exe" ,
351
352
.lib = > "zig build-lib" ,
352
353
.obj = > "zig build-obj" ,
354
+ .pch = > "zig build-pch" ,
353
355
.@"test" = > "zig test" ,
354
356
},
355
357
name_adjusted ,
356
358
@tagName (options .root_module .optimize orelse .Debug ),
357
359
resolved_target .query .zigTriple (owner .allocator ) catch @panic ("OOM" ),
358
360
});
359
361
360
- const out_filename = std .zig .binNameAlloc (owner .allocator , .{
361
- .root_name = name ,
362
- .target = target ,
363
- .output_mode = switch (options .kind ) {
364
- .lib = > .Lib ,
365
- .obj = > .Obj ,
366
- .exe , .@"test" = > .Exe ,
367
- },
368
- .link_mode = options .linkage ,
369
- .version = options .version ,
370
- }) catch @panic ("OOM" );
362
+ const out_filename = if (options .kind == .pch )
363
+ std .fmt .allocPrint (owner .allocator , "{s}.pch" , .{name }) catch @panic ("OOM" )
364
+ else
365
+ std .zig .binNameAlloc (owner .allocator , .{
366
+ .root_name = name ,
367
+ .target = target ,
368
+ .output_mode = switch (options .kind ) {
369
+ .lib = > .Lib ,
370
+ .obj = > .Obj ,
371
+ .exe , .@"test" = > .Exe ,
372
+ .pch = > unreachable ,
373
+ },
374
+ .link_mode = options .linkage ,
375
+ .version = options .version ,
376
+ }) catch @panic ("OOM" );
371
377
372
378
const compile = owner .allocator .create (Compile ) catch @panic ("OOM" );
373
379
compile .* = .{
@@ -796,17 +802,20 @@ pub fn linkFrameworkWeak(c: *Compile, name: []const u8) void {
796
802
797
803
/// Handy when you have many C/C++ source files and want them all to have the same flags.
798
804
pub fn addCSourceFiles (compile : * Compile , options : Module.AddCSourceFilesOptions ) void {
805
+ assert (compile .kind != .pch ); // pch can only be generated from a single C header file
799
806
compile .root_module .addCSourceFiles (options );
800
807
}
801
808
802
809
pub fn addCSourceFile (compile : * Compile , source : Module.CSourceFile ) void {
810
+ assert (compile .kind != .pch or compile .root_module .link_objects .items .len == 0 ); // pch can only be generated from a single C header file
803
811
compile .root_module .addCSourceFile (source );
804
812
}
805
813
806
814
/// Resource files must have the extension `.rc`.
807
815
/// Can be called regardless of target. The .rc file will be ignored
808
816
/// if the target object format does not support embedded resources.
809
817
pub fn addWin32ResourceFile (compile : * Compile , source : Module.RcSourceFile ) void {
818
+ assert (compile .kind != .pch ); // pch can only be generated from a single C header file
810
819
compile .root_module .addWin32ResourceFile (source );
811
820
}
812
821
@@ -887,14 +896,17 @@ pub fn getEmittedLlvmBc(compile: *Compile) LazyPath {
887
896
}
888
897
889
898
pub fn addAssemblyFile (compile : * Compile , source : Module.AsmSourceFile ) void {
899
+ assert (compile .kind != .pch ); // pch can only be generated from a single C header file
890
900
compile .root_module .addAssemblyFile (source );
891
901
}
892
902
893
903
pub fn addObjectFile (compile : * Compile , source : LazyPath ) void {
904
+ assert (compile .kind != .pch ); // pch can only be generated from a single C header file
894
905
compile .root_module .addObjectFile (source );
895
906
}
896
907
897
908
pub fn addObject (compile : * Compile , object : * Compile ) void {
909
+ assert (compile .kind != .pch ); // pch can only be generated from a single C header file
898
910
compile .root_module .addObject (object );
899
911
}
900
912
@@ -1019,6 +1031,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
1019
1031
.lib = > "build-lib" ,
1020
1032
.exe = > "build-exe" ,
1021
1033
.obj = > "build-obj" ,
1034
+ .pch = > "build-pch" ,
1022
1035
.@"test" = > "test" ,
1023
1036
};
1024
1037
try zig_args .append (cmd );
@@ -1086,6 +1099,14 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
1086
1099
}
1087
1100
}
1088
1101
1102
+ if (compile .kind == .pch ) {
1103
+ // precompiled headers must have a single input header file.
1104
+ var it = compile .root_module .iterateDependencies (compile , false );
1105
+ const link_objects = it .next ().? .module .link_objects ;
1106
+ assert (link_objects .items .len == 1 and link_objects .items [0 ] == .c_source_file );
1107
+ assert (it .next () == null );
1108
+ }
1109
+
1089
1110
var cli_named_modules = try CliNamedModules .init (arena , & compile .root_module );
1090
1111
1091
1112
// For this loop, don't chase dynamic libraries because their link
@@ -1197,6 +1218,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
1197
1218
switch (other .kind ) {
1198
1219
.exe = > return step .fail ("cannot link with an executable build artifact" , .{}),
1199
1220
.@"test" = > return step .fail ("cannot link with a test" , .{}),
1221
+ .pch = > @panic ("Cannot link with a precompiled header file" ),
1200
1222
.obj = > {
1201
1223
const included_in_lib_or_obj = ! my_responsibility and
1202
1224
(dep .compile .? .kind == .lib or dep .compile .? .kind == .obj );
0 commit comments