Skip to content

Commit 05ee26a

Browse files
authored
Target GCC PCH (#127)
1 parent 4cb38ea commit 05ee26a

File tree

30 files changed

+496
-69
lines changed

30 files changed

+496
-69
lines changed

.github/workflows/linux_gcc_cmake_build.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ jobs:
101101
run: |
102102
cmake --build . --target run_hybrid_generic_example
103103
104+
- name: Hybrid PCH Target Example
105+
working-directory: ${{github.workspace}}/${{env.BUILD_FOLDER_DEV_ALL}}
106+
run: |
107+
cmake --build . --target run_hybrid_pch_example_linux
108+
104109
build_single:
105110
name: GCC single lib
106111
runs-on: ubuntu-latest
@@ -197,6 +202,11 @@ jobs:
197202
run: |
198203
cmake --build . --target run_hybrid_generic_example
199204
205+
- name: Hybrid PCH Target Example
206+
working-directory: ${{github.workspace}}/${{env.BUILD_FOLDER_DEV_SINGLE}}
207+
run: |
208+
cmake --build . --target run_hybrid_pch_example_linux
209+
200210
build_interface:
201211
name: GCC interface lib
202212
runs-on: ubuntu-latest

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,5 @@ if (${BUILDCC_EXAMPLES})
113113
add_subdirectory(example/hybrid/external_lib)
114114
add_subdirectory(example/hybrid/custom_target)
115115
add_subdirectory(example/hybrid/generic)
116+
add_subdirectory(example/hybrid/pch)
116117
endif()

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ Build C, C++ and ASM files in C++
5858
- Argument passing has been made easy using the `buildcc::Args` module.
5959

6060
**Taskflow dependency for hybrid/simple example**
61-
![Taskflow dependency](example/hybrid/simple/graph.PNG)
62-
See also [Software Architecture](#software-architecture)
61+
![Hybrid Simple example](example/hybrid/simple/graph.PNG)
6362

64-
## Software Architecture
63+
**Taskflow dependency for hybrid/pch example**
64+
![Hybrid PCH example](example/hybrid/pch/graph.PNG)
65+
66+
# Software Architecture
6567

6668
### Interface lib dependencies
6769

@@ -82,6 +84,10 @@ See also [Software Architecture](#software-architecture)
8284
## Community Plugin
8385

8486
- [x] [ClangCompileCommands](buildcc/plugins/include/plugins/clang_compile_commands.h)
87+
- [ ] [BuildCCFind](buildcc/plugins/include/plugins/buildcc_find.h)
88+
- [x] Host system executable
89+
- [ ] BuildCC Library
90+
- [ ] BuildCC Plugin
8591
- [ ] ClangFormat
8692
- [ ] Target graph visualizer (through Taskflow)
8793

@@ -165,7 +171,7 @@ cpack -G NSIS
165171

166172
Contains **proof of concept** and **real world** [examples](example/README.md).
167173

168-
# Developer
174+
# Developer Guide
169175

170176
Developers interested in contributing to **_BuildCC_**
171177

buildcc/lib/target/include/target/target.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,24 @@ class Target : public BuilderInterface {
5757
DynamicLibrary,
5858
};
5959

60+
// Defaults set for the GCC compiler
6061
struct Config {
6162
Config() {}
6263

6364
std::string target_ext{""};
6465
std::string obj_ext{".o"};
65-
std::string pch_header_ext{".hpp"};
66+
std::string pch_header_ext{".h"};
6667
std::string pch_compile_ext{".gch"};
6768

6869
std::string prefix_include_dir{"-I"};
6970
std::string prefix_lib_dir{"-L"};
7071

7172
std::string pch_command{"{compiler} {preprocessor_flags} {include_dirs} "
72-
"{common_compile_flags} {pch_flags} "
73+
"{common_compile_flags} {pch_compile_flags} "
7374
"{compile_flags} -o {output} -c {input}"};
7475
std::string compile_command{
7576
"{compiler} {preprocessor_flags} {include_dirs} {common_compile_flags} "
76-
"{compile_flags} -o {output} -c {input}"};
77+
"{pch_object_flags} {compile_flags} -o {output} -c {input}"};
7778
std::string link_command{
7879
"{cpp_compiler} {link_flags} {compiled_sources} -o {output} "
7980
"{lib_dirs} {lib_deps}"};
@@ -155,7 +156,8 @@ class Target : public BuilderInterface {
155156
// * Flags
156157
void AddPreprocessorFlag(const std::string &flag);
157158
void AddCommonCompileFlag(const std::string &flag);
158-
void AddPchFlag(const std::string &flag);
159+
void AddPchCompileFlag(const std::string &flag);
160+
void AddPchObjectFlag(const std::string &flag);
159161
void AddAsmCompileFlag(const std::string &flag);
160162
void AddCCompileFlag(const std::string &flag);
161163
void AddCppCompileFlag(const std::string &flag);
@@ -234,8 +236,11 @@ class Target : public BuilderInterface {
234236
const std::unordered_set<std::string> &GetCurrentCommonCompileFlags() const {
235237
return storer_.current_common_compile_flags;
236238
}
237-
const std::unordered_set<std::string> &GetCurrentPchFlags() const {
238-
return storer_.current_pch_flags;
239+
const std::unordered_set<std::string> &GetCurrentPchCompileFlags() const {
240+
return storer_.current_pch_compile_flags;
241+
}
242+
const std::unordered_set<std::string> &GetCurrentPchObjectFlags() const {
243+
return storer_.current_pch_object_flags;
239244
}
240245
const std::unordered_set<std::string> &GetCurrentAsmCompileFlags() const {
241246
return storer_.current_asm_compile_flags;

buildcc/lib/target/include/target/target_loader.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ class TargetLoader : public LoaderInterface {
6565
const std::unordered_set<std::string> &GetLoadedCommonCompileFlags() const {
6666
return loaded_common_compile_flags_;
6767
}
68-
const std::unordered_set<std::string> &GetLoadedPchFlags() const {
69-
return loaded_pch_flags_;
68+
const std::unordered_set<std::string> &GetLoadedPchCompileFlags() const {
69+
return loaded_pch_compile_flags_;
70+
}
71+
const std::unordered_set<std::string> &GetLoadedPchObjectFlags() const {
72+
return loaded_pch_object_flags_;
7073
}
7174
const std::unordered_set<std::string> &GetLoadedAsmCompileFlags() const {
7275
return loaded_asm_compile_flags_;
@@ -107,7 +110,8 @@ class TargetLoader : public LoaderInterface {
107110

108111
std::unordered_set<std::string> loaded_preprocessor_flags_;
109112
std::unordered_set<std::string> loaded_common_compile_flags_;
110-
std::unordered_set<std::string> loaded_pch_flags_;
113+
std::unordered_set<std::string> loaded_pch_compile_flags_;
114+
std::unordered_set<std::string> loaded_pch_object_flags_;
111115
std::unordered_set<std::string> loaded_asm_compile_flags_;
112116
std::unordered_set<std::string> loaded_c_compile_flags_;
113117
std::unordered_set<std::string> loaded_cpp_compile_flags_;

buildcc/lib/target/include/target/target_storer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ struct TargetStorer {
3636

3737
std::unordered_set<std::string> current_preprocessor_flags;
3838
std::unordered_set<std::string> current_common_compile_flags;
39-
std::unordered_set<std::string> current_pch_flags;
39+
std::unordered_set<std::string> current_pch_compile_flags;
40+
std::unordered_set<std::string> current_pch_object_flags;
4041
std::unordered_set<std::string> current_asm_compile_flags;
4142
std::unordered_set<std::string> current_c_compile_flags;
4243
std::unordered_set<std::string> current_cpp_compile_flags;

buildcc/lib/target/src/target/build.cpp

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@
2222

2323
#include "fmt/format.h"
2424

25+
namespace {
26+
27+
constexpr const char *const kIncludeDirs = "include_dirs";
28+
constexpr const char *const kLibDirs = "lib_dirs";
29+
30+
constexpr const char *const kPreprocessorFlags = "preprocessor_flags";
31+
constexpr const char *const kCommonCompileFlags = "common_compile_flags";
32+
constexpr const char *const kLinkFlags = "link_flags";
33+
34+
constexpr const char *const kPchCompileFlags = "pch_compile_flags";
35+
constexpr const char *const kPchObjectFlags = "pch_object_flags";
36+
37+
constexpr const char *const kAsmCompiler = "asm_compiler";
38+
constexpr const char *const kCCompiler = "c_compiler";
39+
constexpr const char *const kCppCompiler = "cpp_compiler";
40+
constexpr const char *const kArchiver = "archiver";
41+
constexpr const char *const kLinker = "linker";
42+
43+
} // namespace
44+
2545
namespace buildcc::base {
2646

2747
// * Load
@@ -40,34 +60,41 @@ void Target::Build() {
4060
tf_.name(fmt::format("[{}] {}", toolchain_.GetName(), name_));
4161

4262
command_.AddDefaultArguments({
43-
{"include_dirs",
44-
internal::aggregate_with_prefix(config_.prefix_include_dir,
45-
GetCurrentIncludeDirs())},
46-
{"lib_dirs", internal::aggregate_with_prefix(config_.prefix_lib_dir,
47-
GetCurrentLibDirs())},
48-
49-
{"preprocessor_flags",
50-
internal::aggregate(GetCurrentPreprocessorFlags())},
51-
{"common_compile_flags",
63+
{kIncludeDirs, internal::aggregate_with_prefix(config_.prefix_include_dir,
64+
GetCurrentIncludeDirs())},
65+
{kLibDirs, internal::aggregate_with_prefix(config_.prefix_lib_dir,
66+
GetCurrentLibDirs())},
67+
68+
{kPreprocessorFlags, internal::aggregate(GetCurrentPreprocessorFlags())},
69+
{kCommonCompileFlags,
5270
internal::aggregate(GetCurrentCommonCompileFlags())},
53-
{"link_flags", internal::aggregate(GetCurrentLinkFlags())},
71+
{kLinkFlags, internal::aggregate(GetCurrentLinkFlags())},
5472

5573
// Toolchain executables here
56-
{"asm_compiler", toolchain_.GetAsmCompiler()},
57-
{"c_compiler", toolchain_.GetCCompiler()},
58-
{"cpp_compiler", toolchain_.GetCppCompiler()},
59-
{"archiver", toolchain_.GetArchiver()},
60-
{"linker", toolchain_.GetLinker()},
74+
{kAsmCompiler, toolchain_.GetAsmCompiler()},
75+
{kCCompiler, toolchain_.GetCCompiler()},
76+
{kCppCompiler, toolchain_.GetCppCompiler()},
77+
{kArchiver, toolchain_.GetArchiver()},
78+
{kLinker, toolchain_.GetLinker()},
6179
});
6280

6381
// Load the serialized file
6482
(void)loader_.Load();
6583

6684
// PCH Compile
6785
if (!GetCurrentPchFiles().empty()) {
86+
command_.AddDefaultArguments({
87+
{kPchCompileFlags, internal::aggregate(GetCurrentPchCompileFlags())},
88+
{kPchObjectFlags, internal::aggregate(GetCurrentPchObjectFlags())},
89+
});
90+
6891
// TODO, Update .output at Constructor
6992
pch_file_.command = ConstructPchCompileCommand();
7093
PchTask();
94+
} else {
95+
command_.AddDefaultArguments({
96+
{kPchObjectFlags, ""},
97+
});
7198
}
7299

73100
// Compile Command

buildcc/lib/target/src/target/compile_pch.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,19 @@
2222

2323
namespace {
2424

25+
constexpr const char *const kCompiler = "compiler";
26+
constexpr const char *const kCompileFlags = "compile_flags";
27+
constexpr const char *const kOutput = "output";
28+
constexpr const char *const kInput = "input";
29+
2530
constexpr const char *const kFormat = R"(// Generated by BuildCC
26-
#pragma once
31+
#ifndef BUILDCC_GENERATED_PCH_H_
32+
#define BUILDCC_GENERATED_PCH_H_
2733
2834
// clang-format off
2935
{aggregated_includes}
36+
37+
#endif
3038
)";
3139

3240
void AggregateToFile(const fs::path &filename,
@@ -59,15 +67,13 @@ std::string Target::ConstructPchCompileCommand() const {
5967
GetState().contains_cpp_src ? FileExt::Type::Cpp : FileExt::Type::C;
6068
const std::string compile_flags =
6169
ext_.GetCompileFlags(file_ext_type).value_or("");
62-
return command_.Construct(
63-
config_.pch_command,
64-
{
65-
{"compiler", compiler},
66-
{"pch_flags", internal::aggregate(GetCurrentPchFlags())},
67-
{"compile_flags", compile_flags},
68-
{"output", GetPchCompilePath().string()},
69-
{"input", GetPchHeaderPath().string()},
70-
});
70+
return command_.Construct(config_.pch_command,
71+
{
72+
{kCompiler, compiler},
73+
{kCompileFlags, compile_flags},
74+
{kOutput, GetPchCompilePath().string()},
75+
{kInput, GetPchHeaderPath().string()},
76+
});
7177
}
7278

7379
void Target::PrePchCompile() { storer_.current_pch_files.Convert(); }
@@ -81,7 +87,8 @@ void Target::BuildPchCompile() {
8187
GetCurrentPreprocessorFlags());
8288
RecheckFlags(loader_.GetLoadedCommonCompileFlags(),
8389
GetCurrentCommonCompileFlags());
84-
RecheckFlags(loader_.GetLoadedPchFlags(), GetCurrentPchFlags());
90+
RecheckFlags(loader_.GetLoadedPchCompileFlags(),
91+
GetCurrentPchCompileFlags());
8592
RecheckFlags(loader_.GetLoadedCCompileFlags(), GetCurrentCCompileFlags());
8693
RecheckFlags(loader_.GetLoadedCppCompileFlags(),
8794
GetCurrentCppCompileFlags());

buildcc/lib/target/src/target/compile_source.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616

1717
#include "target/target.h"
1818

19+
namespace {
20+
21+
constexpr const char *const kCompiler = "compiler";
22+
constexpr const char *const kCompileFlags = "compile_flags";
23+
constexpr const char *const kOutput = "output";
24+
constexpr const char *const kInput = "input";
25+
26+
} // namespace
27+
1928
namespace buildcc::base {
2029

2130
internal::fs_unordered_set Target::GetCompiledSources() const {
@@ -81,10 +90,10 @@ Target::ConstructCompileCommand(const fs::path &absolute_current_source) const {
8190
return command_.Construct(
8291
config_.compile_command,
8392
{
84-
{"compiler", selected_compiler},
85-
{"compile_flags", selected_aggregated_compile_flags},
86-
{"output", output},
87-
{"input", input},
93+
{kCompiler, selected_compiler},
94+
{kCompileFlags, selected_aggregated_compile_flags},
95+
{kOutput, output},
96+
{kInput, input},
8897
});
8998
}
9099

@@ -166,6 +175,7 @@ void Target::BuildObjectCompile(std::vector<fs::path> &source_files,
166175
GetCurrentPreprocessorFlags());
167176
RecheckFlags(loader_.GetLoadedCommonCompileFlags(),
168177
GetCurrentCommonCompileFlags());
178+
RecheckFlags(loader_.GetLoadedPchObjectFlags(), GetCurrentPchObjectFlags());
169179
RecheckFlags(loader_.GetLoadedAsmCompileFlags(),
170180
GetCurrentAsmCompileFlags());
171181
RecheckFlags(loader_.GetLoadedCCompileFlags(), GetCurrentCCompileFlags());

buildcc/lib/target/src/target/flags.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ void Target::AddCommonCompileFlag(const std::string &flag) {
2626
LockedAfterBuild();
2727
storer_.current_common_compile_flags.insert(flag);
2828
}
29-
void Target::AddPchFlag(const std::string &flag) {
29+
void Target::AddPchCompileFlag(const std::string &flag) {
3030
LockedAfterBuild();
31-
storer_.current_pch_flags.insert(flag);
31+
storer_.current_pch_compile_flags.insert(flag);
32+
}
33+
void Target::AddPchObjectFlag(const std::string &flag) {
34+
LockedAfterBuild();
35+
storer_.current_pch_object_flags.insert(flag);
3236
}
3337
void Target::AddAsmCompileFlag(const std::string &flag) {
3438
LockedAfterBuild();

0 commit comments

Comments
 (0)