Skip to content

Commit 601ef61

Browse files
authored
[CRTP] Target Include API (#150)
1 parent 2f09f50 commit 601ef61

File tree

9 files changed

+149
-102
lines changed

9 files changed

+149
-102
lines changed

buildcc/lib/target/cmake/mock_target.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_library(mock_target STATIC
99
# API
1010
src/api/copy_api.cpp
1111
src/api/source_api.cpp
12+
src/api/include_api.cpp
1213

1314
# Generator
1415
src/generator/generator_loader.cpp
@@ -29,7 +30,6 @@ add_library(mock_target STATIC
2930
src/target/target_loader.cpp
3031
src/target/target_storer.cpp
3132

32-
src/target/include_dir.cpp
3333
src/target/pch.cpp
3434
src/target/lib.cpp
3535
src/target/flags.cpp

buildcc/lib/target/cmake/target.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ set(TARGET_SRCS
2020
# API
2121
src/api/copy_api.cpp
2222
src/api/source_api.cpp
23+
src/api/include_api.cpp
2324
include/target/api/copy_api.h
2425
include/target/api/source_api.h
26+
include/target/api/include_api.h
2527

2628
# Generator
2729
src/generator/generator_loader.cpp
@@ -48,7 +50,6 @@ set(TARGET_SRCS
4850
include/target/target_storer.h
4951
include/target/target.h
5052

51-
src/target/include_dir.cpp
5253
src/target/pch.cpp
5354
src/target/lib.cpp
5455
src/target/flags.cpp
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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_INCLUDE_API_H_
18+
#define TARGET_API_INCLUDE_API_H_
19+
20+
#include <filesystem>
21+
22+
namespace fs = std::filesystem;
23+
24+
namespace buildcc::base {
25+
26+
template <typename T> class IncludeApi {
27+
public:
28+
void AddHeader(const fs::path &relative_filename,
29+
const fs::path &relative_to_target_path = "");
30+
void AddHeaderAbsolute(const fs::path &absolute_filepath);
31+
32+
void GlobHeaders(const fs::path &relative_to_target_path);
33+
void GlobHeadersAbsolute(const fs::path &absolute_path);
34+
35+
void AddIncludeDir(const fs::path &relative_include_dir,
36+
bool glob_headers = false);
37+
void AddIncludeDirAbsolute(const fs::path &absolute_include_dir,
38+
bool glob_headers = false);
39+
};
40+
41+
} // namespace buildcc::base
42+
43+
#endif

buildcc/lib/target/include/target/common/target_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct TargetConfig {
3535
bool IsValidHeader(const fs::path &filepath) const;
3636

3737
void ExpectsValidSource(const fs::path &filepath) const;
38+
void ExpectsValidHeader(const fs::path &filepath) const;
3839

3940
std::string target_ext{""};
4041
std::string obj_ext{".o"};

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
// API
4040
#include "target/api/copy_api.h"
41+
#include "target/api/include_api.h"
4142
#include "target/api/source_api.h"
4243

4344
// Friend
@@ -67,7 +68,8 @@ namespace buildcc::base {
6768
// the specialized target-toolchain classes
6869
class Target : public BuilderInterface,
6970
public CopyApi<Target>,
70-
public SourceApi<Target> {
71+
public SourceApi<Target>,
72+
public IncludeApi<Target> {
7173

7274
public:
7375
explicit Target(const std::string &name, TargetType type,
@@ -89,25 +91,12 @@ class Target : public BuilderInterface,
8991

9092
// Setters
9193

92-
// * Headers
93-
void AddHeader(const fs::path &relative_filename,
94-
const fs::path &relative_to_target_path = "");
95-
void AddHeaderAbsolute(const fs::path &absolute_filepath);
96-
97-
void GlobHeaders(const fs::path &relative_to_target_path);
98-
void GlobHeadersAbsolute(const fs::path &absolute_path);
99-
10094
// PCH
10195
void AddPch(const fs::path &relative_filename,
10296
const fs::path &relative_to_target_path = "");
10397
void AddPchAbsolute(const fs::path &absolute_filepath);
10498

10599
// * Include and Lib directory
106-
void AddIncludeDir(const fs::path &relative_include_dir,
107-
bool glob_headers = false);
108-
void AddIncludeDirAbsolute(const fs::path &absolute_include_dir,
109-
bool glob_headers = false);
110-
111100
void AddLibDir(const fs::path &relative_lib_dir);
112101
void AddLibDirAbsolute(const fs::path &absolute_lib_dir);
113102

@@ -246,6 +235,7 @@ class Target : public BuilderInterface,
246235

247236
friend class CopyApi<Target>;
248237
friend class SourceApi<Target>;
238+
friend class IncludeApi<Target>;
249239

250240
private:
251241
void Initialize();
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
#include "target/api/include_api.h"
18+
19+
#include "target/target.h"
20+
21+
namespace buildcc::base {
22+
23+
template <typename T>
24+
void IncludeApi<T>::AddHeaderAbsolute(const fs::path &absolute_filepath) {
25+
T &t = static_cast<T &>(*this);
26+
27+
t.state_.ExpectsUnlock();
28+
t.config_.ExpectsValidHeader(absolute_filepath);
29+
t.storer_.current_header_files.user.insert(absolute_filepath);
30+
}
31+
32+
template <typename T>
33+
void IncludeApi<T>::AddHeader(const fs::path &relative_filename,
34+
const fs::path &relative_to_target_path) {
35+
T &t = static_cast<T &>(*this);
36+
37+
// Check Source
38+
fs::path absolute_filepath =
39+
t.env_.GetTargetRootDir() / relative_to_target_path / relative_filename;
40+
AddHeaderAbsolute(absolute_filepath);
41+
}
42+
43+
template <typename T>
44+
void IncludeApi<T>::GlobHeaders(const fs::path &relative_to_target_path) {
45+
T &t = static_cast<T &>(*this);
46+
47+
fs::path absolute_path = t.env_.GetTargetRootDir() / relative_to_target_path;
48+
GlobHeadersAbsolute(absolute_path);
49+
}
50+
51+
template <typename T>
52+
void IncludeApi<T>::GlobHeadersAbsolute(const fs::path &absolute_path) {
53+
T &t = static_cast<T &>(*this);
54+
55+
for (const auto &p : fs::directory_iterator(absolute_path)) {
56+
if (t.config_.IsValidHeader(p.path())) {
57+
env::log_trace(__FUNCTION__, fmt::format("Added header {}", p.path()));
58+
AddHeaderAbsolute(p.path());
59+
}
60+
}
61+
}
62+
63+
template <typename T>
64+
void IncludeApi<T>::AddIncludeDir(const fs::path &relative_include_dir,
65+
bool glob_headers) {
66+
T &t = static_cast<T &>(*this);
67+
68+
const fs::path absolute_include_dir =
69+
t.env_.GetTargetRootDir() / relative_include_dir;
70+
AddIncludeDirAbsolute(absolute_include_dir, glob_headers);
71+
}
72+
73+
template <typename T>
74+
void IncludeApi<T>::AddIncludeDirAbsolute(const fs::path &absolute_include_dir,
75+
bool glob_headers) {
76+
T &t = static_cast<T &>(*this);
77+
78+
t.state_.ExpectsUnlock();
79+
t.storer_.current_include_dirs.insert(absolute_include_dir);
80+
81+
if (glob_headers) {
82+
GlobHeadersAbsolute(absolute_include_dir);
83+
}
84+
}
85+
86+
template class IncludeApi<Target>;
87+
88+
} // namespace buildcc::base

buildcc/lib/target/src/common/target_config.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,10 @@ bool TargetConfig::IsValidHeader(const fs::path &filepath) const {
7979
return valid;
8080
}
8181

82+
void TargetConfig::ExpectsValidHeader(const fs::path &filepath) const {
83+
env::assert_fatal(
84+
IsValidHeader(filepath),
85+
fmt::format("{} does not have a valid header extension", filepath));
86+
}
87+
8288
} // namespace buildcc::base

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ Check the `include/target/api` and `src/api` folder
1717
- [x] Copy
1818
- Copy src target to dest
1919
- Selective copy using `std::initializer<CopyOption>`
20+
- `copy_api`
2021
- [x] Source
21-
- [ ] Header and Include Dir
22+
- `source_api`
23+
- [x] Header and Include Dir
24+
- `include_api`
2225
- [ ] Lib and Lib Dir
2326
- [ ] PCH
2427
- [ ] Flags
@@ -28,9 +31,6 @@ Check the `include/target/api` and `src/api` folder
2831

2932
## Inputs to Target
3033

31-
- [x] `include_dir.cpp`
32-
- Include Dir
33-
- Header File
3434
- [x] `pch.cpp`
3535
- Precompile Header files
3636
- [x] `lib.cpp`

buildcc/lib/target/src/target/include_dir.cpp

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)