Skip to content

Commit f3d9bef

Browse files
authored
[CRTP] Target Lib API (#151)
1 parent 601ef61 commit f3d9bef

File tree

8 files changed

+112
-69
lines changed

8 files changed

+112
-69
lines changed

buildcc/lib/target/cmake/mock_target.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_library(mock_target STATIC
1010
src/api/copy_api.cpp
1111
src/api/source_api.cpp
1212
src/api/include_api.cpp
13+
src/api/lib_api.cpp
1314

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

3334
src/target/pch.cpp
34-
src/target/lib.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
@@ -21,9 +21,11 @@ set(TARGET_SRCS
2121
src/api/copy_api.cpp
2222
src/api/source_api.cpp
2323
src/api/include_api.cpp
24+
src/api/lib_api.cpp
2425
include/target/api/copy_api.h
2526
include/target/api/source_api.h
2627
include/target/api/include_api.h
28+
include/target/api/lib_api.h
2729

2830
# Generator
2931
src/generator/generator_loader.cpp
@@ -51,7 +53,6 @@ set(TARGET_SRCS
5153
include/target/target.h
5254

5355
src/target/pch.cpp
54-
src/target/lib.cpp
5556
src/target/flags.cpp
5657
src/target/additional_deps.cpp
5758

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ namespace fs = std::filesystem;
2323

2424
namespace buildcc::base {
2525

26+
// Requires
27+
// - TargetStorer
28+
// - TargetState
29+
// - TargetConfig
30+
// - TargetEnv
2631
template <typename T> class IncludeApi {
2732
public:
2833
void AddHeader(const fs::path &relative_filename,
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_LIB_API_H_
18+
#define TARGET_API_LIB_API_H_
19+
20+
#include <filesystem>
21+
#include <string>
22+
23+
namespace fs = std::filesystem;
24+
25+
namespace buildcc::base {
26+
27+
// Requires
28+
// - TargetStorer
29+
// - TargetState
30+
// - TargetEnv
31+
// T::GetTargetPath
32+
template <typename T> class LibApi {
33+
public:
34+
void AddLibDep(const T &lib_dep);
35+
void AddLibDep(const std::string &lib_dep);
36+
37+
void AddLibDir(const fs::path &relative_lib_dir);
38+
void AddLibDirAbsolute(const fs::path &absolute_lib_dir);
39+
};
40+
41+
} // namespace buildcc::base
42+
43+
#endif

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
// API
4040
#include "target/api/copy_api.h"
4141
#include "target/api/include_api.h"
42+
#include "target/api/lib_api.h"
4243
#include "target/api/source_api.h"
4344

4445
// Friend
@@ -69,7 +70,8 @@ namespace buildcc::base {
6970
class Target : public BuilderInterface,
7071
public CopyApi<Target>,
7172
public SourceApi<Target>,
72-
public IncludeApi<Target> {
73+
public IncludeApi<Target>,
74+
public LibApi<Target> {
7375

7476
public:
7577
explicit Target(const std::string &name, TargetType type,
@@ -96,14 +98,6 @@ class Target : public BuilderInterface,
9698
const fs::path &relative_to_target_path = "");
9799
void AddPchAbsolute(const fs::path &absolute_filepath);
98100

99-
// * Include and Lib directory
100-
void AddLibDir(const fs::path &relative_lib_dir);
101-
void AddLibDirAbsolute(const fs::path &absolute_lib_dir);
102-
103-
// * Libraries
104-
void AddLibDep(const Target &lib_dep);
105-
void AddLibDep(const std::string &lib_dep);
106-
107101
// * Flags
108102
void AddPreprocessorFlag(const std::string &flag);
109103
void AddCommonCompileFlag(const std::string &flag);
@@ -236,6 +230,7 @@ class Target : public BuilderInterface,
236230
friend class CopyApi<Target>;
237231
friend class SourceApi<Target>;
238232
friend class IncludeApi<Target>;
233+
friend class LibApi<Target>;
239234

240235
private:
241236
void Initialize();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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/lib_api.h"
18+
19+
#include "target/target.h"
20+
21+
namespace buildcc::base {
22+
23+
template <typename T>
24+
void LibApi<T>::AddLibDir(const fs::path &relative_lib_dir) {
25+
T &t = static_cast<T &>(*this);
26+
27+
fs::path final_lib_dir = t.env_.GetTargetRootDir() / relative_lib_dir;
28+
AddLibDirAbsolute(final_lib_dir);
29+
}
30+
31+
template <typename T>
32+
void LibApi<T>::AddLibDirAbsolute(const fs::path &absolute_lib_dir) {
33+
T &t = static_cast<T &>(*this);
34+
35+
t.state_.ExpectsUnlock();
36+
t.storer_.current_lib_dirs.insert(absolute_lib_dir);
37+
}
38+
39+
template <typename T> void LibApi<T>::AddLibDep(const T &lib_dep) {
40+
T &t = static_cast<T &>(*this);
41+
42+
t.state_.ExpectsUnlock();
43+
t.storer_.current_lib_deps.user.insert(lib_dep.GetTargetPath());
44+
}
45+
46+
template <typename T> void LibApi<T>::AddLibDep(const std::string &lib_dep) {
47+
T &t = static_cast<T &>(*this);
48+
49+
t.state_.ExpectsUnlock();
50+
t.storer_.current_external_lib_deps.insert(lib_dep);
51+
}
52+
53+
template class LibApi<Target>;
54+
55+
} // namespace buildcc::base

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Check the `include/target/api` and `src/api` folder
2222
- `source_api`
2323
- [x] Header and Include Dir
2424
- `include_api`
25-
- [ ] Lib and Lib Dir
25+
- [x] Lib and Lib Dir
26+
- `lib_api`
2627
- [ ] PCH
2728
- [ ] Flags
2829
- [ ] Rebuild Deps
@@ -33,10 +34,6 @@ Check the `include/target/api` and `src/api` folder
3334

3435
- [x] `pch.cpp`
3536
- Precompile Header files
36-
- [x] `lib.cpp`
37-
- Lib Dir
38-
- Lib File (full path Target supplied)
39-
- External Lib File (relative link using -l)
4037
- [x] `flags.cpp`
4138
- PreprocessorFlags
4239
- CommonCompileFlags

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

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

0 commit comments

Comments
 (0)