-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
std.Build: add precompiled C header support
v2: add `-fpch-validate-input-files-content` "Validate PCH input files based on content if mtime differs" so that llvm check matches zig caching behaviour.
- Loading branch information
Showing
9 changed files
with
225 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const std = @import("std"); | ||
const Builder = std.build.Builder; | ||
|
||
pub fn build(b: *Builder) void { | ||
const test_step = b.step("test", "Test it"); | ||
b.default_step = test_step; | ||
|
||
const target = b.standardTargetOptions(.{}); | ||
const mode = b.standardOptimizeOption(.{}); | ||
|
||
// c-header | ||
{ | ||
const exe = b.addExecutable(.{ | ||
.name = "pchtest", | ||
.target = target, | ||
.optimize = mode, | ||
.link_libc = true, | ||
}); | ||
|
||
const pch = b.addPrecompiledCHeader(.{ | ||
.name = "pch_c", | ||
.target = target, | ||
.optimize = mode, | ||
.cpp_header = false, | ||
}, .{ | ||
.file = .{ .path = "include_a.h" }, | ||
.flags = &[_][]const u8{}, | ||
}); | ||
|
||
exe.addPrecompiledCHeader(pch); | ||
|
||
exe.addCSourceFile(.{ | ||
.file = .{ .path = "test.c" }, | ||
.flags = &[_][]const u8{}, | ||
}); | ||
|
||
test_step.dependOn(&b.addRunArtifact(exe).step); | ||
} | ||
|
||
// c++-header | ||
{ | ||
const exe = b.addExecutable(.{ | ||
.name = "pchtest++", | ||
.target = target, | ||
.optimize = mode, | ||
.link_libc = true, | ||
}); | ||
exe.linkLibCpp(); | ||
|
||
const pch = b.addPrecompiledCHeader(.{ | ||
.name = "pch_c++", | ||
.target = target, | ||
.optimize = mode, | ||
.cpp_header = true, | ||
}, .{ | ||
.file = .{ .path = "include_a.h" }, | ||
.flags = &[_][]const u8{}, | ||
}); | ||
|
||
exe.addPrecompiledCHeader(pch); | ||
|
||
exe.addCSourceFile(.{ | ||
.file = .{ .path = "test.cpp" }, | ||
.flags = &[_][]const u8{}, | ||
}); | ||
|
||
test_step.dependOn(&b.addRunArtifact(exe).step); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include "include_b.h" | ||
|
||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
#if defined(__cplusplus) | ||
#include <iostream> | ||
#else | ||
#include <stdio.h> | ||
#endif | ||
|
||
#define A_INCLUDED 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
#include <math.h> | ||
|
||
typedef double real; | ||
|
||
#define B_INCLUDED 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
// includes commented out to make sure the symbols come from the precompiled header. | ||
//#include "includeA.h" | ||
//#include "includeB.h" | ||
|
||
#ifndef A_INCLUDED | ||
#error "pch not included" | ||
#endif | ||
#ifndef B_INCLUDED | ||
#error "pch not included" | ||
#endif | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
real a = 0.123; | ||
|
||
if (argc > 1) { | ||
fprintf(stdout, "abs(%g)=%g\n", a, abs(a)); | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
// includes commented out to make sure the symbols come from the precompiled header. | ||
//#include "includeA.h" | ||
//#include "includeB.h" | ||
|
||
#ifndef A_INCLUDED | ||
#error "pch not included" | ||
#endif | ||
#ifndef B_INCLUDED | ||
#error "pch not included" | ||
#endif | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
real a = -0.123; | ||
|
||
if (argc > 1) { | ||
std::cout << "abs(" << a << ")=" << fabs(a) << "\n"; | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
|