Skip to content

Commit 04b79f1

Browse files
authored
[CRTP] Target PCH API (#152)
1 parent f3d9bef commit 04b79f1

File tree

8 files changed

+70
-24
lines changed

8 files changed

+70
-24
lines changed

buildcc/lib/target/cmake/mock_target.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_library(mock_target STATIC
1111
src/api/source_api.cpp
1212
src/api/include_api.cpp
1313
src/api/lib_api.cpp
14+
src/api/pch_api.cpp
1415

1516
# Generator
1617
src/generator/generator_loader.cpp
@@ -31,7 +32,6 @@ add_library(mock_target STATIC
3132
src/target/target_loader.cpp
3233
src/target/target_storer.cpp
3334

34-
src/target/pch.cpp
3535
src/target/flags.cpp
3636
src/target/additional_deps.cpp
3737

buildcc/lib/target/cmake/target.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ set(TARGET_SRCS
2222
src/api/source_api.cpp
2323
src/api/include_api.cpp
2424
src/api/lib_api.cpp
25+
src/api/pch_api.cpp
2526
include/target/api/copy_api.h
2627
include/target/api/source_api.h
2728
include/target/api/include_api.h
2829
include/target/api/lib_api.h
30+
include/target/api/pch_api.h
2931

3032
# Generator
3133
src/generator/generator_loader.cpp
@@ -52,7 +54,6 @@ set(TARGET_SRCS
5254
include/target/target_storer.h
5355
include/target/target.h
5456

55-
src/target/pch.cpp
5657
src/target/flags.cpp
5758
src/target/additional_deps.cpp
5859

buildcc/lib/target/include/target/api/lib_api.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ namespace fs = std::filesystem;
2424

2525
namespace buildcc::base {
2626

27+
class Target;
28+
2729
// Requires
2830
// - TargetStorer
2931
// - TargetState
3032
// - TargetEnv
3133
// T::GetTargetPath
3234
template <typename T> class LibApi {
3335
public:
34-
void AddLibDep(const T &lib_dep);
36+
void AddLibDep(const Target &lib_dep);
3537
void AddLibDep(const std::string &lib_dep);
3638

3739
void AddLibDir(const fs::path &relative_lib_dir);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2021 Niket Naidu. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef TARGET_API_PCH_API_H_
18+
#define TARGET_API_PCH_API_H_
19+
20+
#include <filesystem>
21+
22+
namespace fs = std::filesystem;
23+
24+
namespace buildcc::base {
25+
26+
// Requires
27+
// - TargetStorer
28+
// - TargetState
29+
// - TargetConfig
30+
// - TargetEnv
31+
template <typename T> class PchApi {
32+
public:
33+
void AddPch(const fs::path &relative_filename,
34+
const fs::path &relative_to_target_path = "");
35+
void AddPchAbsolute(const fs::path &absolute_filepath);
36+
};
37+
38+
} // namespace buildcc::base
39+
40+
#endif

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "target/api/copy_api.h"
4141
#include "target/api/include_api.h"
4242
#include "target/api/lib_api.h"
43+
#include "target/api/pch_api.h"
4344
#include "target/api/source_api.h"
4445

4546
// Friend
@@ -71,7 +72,8 @@ class Target : public BuilderInterface,
7172
public CopyApi<Target>,
7273
public SourceApi<Target>,
7374
public IncludeApi<Target>,
74-
public LibApi<Target> {
75+
public LibApi<Target>,
76+
public PchApi<Target> {
7577

7678
public:
7779
explicit Target(const std::string &name, TargetType type,
@@ -93,11 +95,6 @@ class Target : public BuilderInterface,
9395

9496
// Setters
9597

96-
// PCH
97-
void AddPch(const fs::path &relative_filename,
98-
const fs::path &relative_to_target_path = "");
99-
void AddPchAbsolute(const fs::path &absolute_filepath);
100-
10198
// * Flags
10299
void AddPreprocessorFlag(const std::string &flag);
103100
void AddCommonCompileFlag(const std::string &flag);
@@ -231,6 +228,7 @@ class Target : public BuilderInterface,
231228
friend class SourceApi<Target>;
232229
friend class IncludeApi<Target>;
233230
friend class LibApi<Target>;
231+
friend class PchApi<Target>;
234232

235233
private:
236234
void Initialize();

buildcc/lib/target/src/api/lib_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void LibApi<T>::AddLibDirAbsolute(const fs::path &absolute_lib_dir) {
3636
t.storer_.current_lib_dirs.insert(absolute_lib_dir);
3737
}
3838

39-
template <typename T> void LibApi<T>::AddLibDep(const T &lib_dep) {
39+
template <typename T> void LibApi<T>::AddLibDep(const Target &lib_dep) {
4040
T &t = static_cast<T &>(*this);
4141

4242
t.state_.ExpectsUnlock();

buildcc/lib/target/src/target/pch.cpp renamed to buildcc/lib/target/src/api/pch_api.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,35 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include "target/api/pch_api.h"
18+
1719
#include "target/target.h"
1820

1921
namespace buildcc::base {
2022

21-
void Target::AddPchAbsolute(const fs::path &absolute_filepath) {
22-
state_.ExpectsUnlock();
23-
env::assert_fatal(config_.IsValidHeader(absolute_filepath),
24-
fmt::format("{} does not have a valid header extension",
25-
absolute_filepath));
23+
template <typename T>
24+
void PchApi<T>::AddPchAbsolute(const fs::path &absolute_filepath) {
25+
T &t = static_cast<T &>(*this);
26+
27+
t.state_.ExpectsUnlock();
28+
t.config_.ExpectsValidHeader(absolute_filepath);
2629

2730
const fs::path absolute_pch = fs::path(absolute_filepath).make_preferred();
28-
storer_.current_pch_files.user.insert(absolute_pch);
31+
t.storer_.current_pch_files.user.insert(absolute_pch);
2932
}
3033

31-
void Target::AddPch(const fs::path &relative_filename,
32-
const fs::path &relative_to_target_path) {
33-
env::log_trace(name_, __FUNCTION__);
34+
template <typename T>
35+
void PchApi<T>::AddPch(const fs::path &relative_filename,
36+
const fs::path &relative_to_target_path) {
37+
T &t = static_cast<T &>(*this);
3438

3539
// Compute the absolute source path
3640
fs::path absolute_pch =
37-
GetTargetRootDir() / relative_to_target_path / relative_filename;
41+
t.env_.GetTargetRootDir() / relative_to_target_path / relative_filename;
3842

3943
AddPchAbsolute(absolute_pch);
4044
}
4145

42-
} // namespace buildcc::base
46+
template class PchApi<Target>;
47+
48+
} // namespace buildcc::base

buildcc/lib/target/src/target/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ Check the `include/target/api` and `src/api` folder
2424
- `include_api`
2525
- [x] Lib and Lib Dir
2626
- `lib_api`
27-
- [ ] PCH
27+
- [x] PCH
28+
- `pch_api`
2829
- [ ] Flags
2930
- [ ] Rebuild Deps
3031
- [ ] Getters
3132
- [ ] Target Info
3233

3334
## Inputs to Target
3435

35-
- [x] `pch.cpp`
36-
- Precompile Header files
3736
- [x] `flags.cpp`
3837
- PreprocessorFlags
3938
- CommonCompileFlags

0 commit comments

Comments
 (0)