-
-
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 an option to
addCSourceFiles()
to include a precompi…
…led header
- Loading branch information
Showing
9 changed files
with
174 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(b: *std.Build) void { | ||
const test_step = b.step("test", "Test it"); | ||
b.default_step = test_step; | ||
|
||
const target = b.standardTargetOptions(.{}); | ||
const optimize = b.standardOptimizeOption(.{}); | ||
|
||
// c-header | ||
{ | ||
const exe = b.addExecutable(.{ | ||
.name = "pchtest", | ||
.target = target, | ||
.optimize = optimize, | ||
.link_libc = true, | ||
}); | ||
|
||
const pch = b.addPrecompiledCHeader(.{ | ||
.name = "pch_c", | ||
.target = target, | ||
.optimize = optimize, | ||
.link_libc = true, | ||
}, .{ | ||
.file = .{ .path = "include_a.h" }, | ||
.flags = &[_][]const u8{}, | ||
.lang = .h, | ||
}); | ||
|
||
exe.addCSourceFiles(.{ | ||
.files = &.{"test.c"}, | ||
.flags = &[_][]const u8{}, | ||
.lang = .c, | ||
.precompiled_header = pch.getEmittedBin(), | ||
}); | ||
|
||
test_step.dependOn(&b.addRunArtifact(exe).step); | ||
} | ||
|
||
// c++-header | ||
{ | ||
const exe = b.addExecutable(.{ | ||
.name = "pchtest++", | ||
.target = target, | ||
.optimize = optimize, | ||
.link_libc = true, | ||
}); | ||
exe.linkLibCpp(); | ||
|
||
const pch = b.addPrecompiledCHeader(.{ | ||
.name = "pch_c++", | ||
.target = target, | ||
.optimize = optimize, | ||
.link_libcpp = true, | ||
}, .{ | ||
.file = .{ .path = "include_a.h" }, | ||
.flags = &[_][]const u8{}, | ||
.lang = .hpp, | ||
}); | ||
|
||
exe.addCSourceFile(.{ | ||
.file = .{ .path = "test.cpp" }, | ||
.flags = &[_][]const u8{}, | ||
.precompiled_header = pch.getEmittedBin(), | ||
}); | ||
|
||
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,7 @@ | ||
#pragma once | ||
|
||
#include <string.h> | ||
|
||
#define C_INCLUDED 1 | ||
|
||
int one = 3; |
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 "include_a.h" | ||
//#include "include_b.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, fabs(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; | ||
} | ||
|