Skip to content

Commit 2a27135

Browse files
authored
Target Info (#155)
Basic Target Information class to hold input information. Useful when - We need to store common information for multiple Targets - Do not need to Build the Target, ex. Header only compilation units
1 parent 4723e5d commit 2a27135

File tree

14 files changed

+409
-128
lines changed

14 files changed

+409
-128
lines changed

buildcc/lib/target/cmake/mock_target.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ add_library(mock_target STATIC
77
src/common/target_state.cpp
88

99
# API
10-
src/api/copy_api.cpp
1110
src/api/source_api.cpp
1211
src/api/include_api.cpp
1312
src/api/lib_api.cpp
1413
src/api/pch_api.cpp
1514
src/api/flag_api.cpp
1615
src/api/deps_api.cpp
1716

17+
src/api/copy_api.cpp
18+
19+
src/api/target_info_getter.cpp
20+
21+
1822
# Generator
1923
src/generator/generator_loader.cpp
2024
src/generator/generator_storer.cpp

buildcc/lib/target/cmake/target.cmake

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,25 @@ set(TARGET_SRCS
1818
include/target/common/target_type.h
1919

2020
# API
21-
src/api/copy_api.cpp
2221
src/api/source_api.cpp
2322
src/api/include_api.cpp
2423
src/api/lib_api.cpp
2524
src/api/pch_api.cpp
2625
src/api/flag_api.cpp
2726
src/api/deps_api.cpp
28-
include/target/api/copy_api.h
2927
include/target/api/source_api.h
3028
include/target/api/include_api.h
3129
include/target/api/lib_api.h
3230
include/target/api/pch_api.h
3331
include/target/api/flag_api.h
3432
include/target/api/deps_api.h
3533

34+
src/api/copy_api.cpp
35+
include/target/api/copy_api.h
36+
37+
src/api/target_info_getter.cpp
38+
include/target/api/target_info_getter.h
39+
3640
# Generator
3741
src/generator/generator_loader.cpp
3842
src/generator/generator_storer.cpp
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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_TARGET_INFO_GETTER_H_
18+
#define TARGET_API_TARGET_INFO_GETTER_H_
19+
20+
#include "target/path.h"
21+
22+
#include "target/common/target_config.h"
23+
#include "target/common/target_state.h"
24+
25+
namespace buildcc::base {
26+
27+
// Requires
28+
// - TargetStorer
29+
// - TargetState
30+
// - TargetEnv
31+
// - TargetConfig
32+
template <typename T> class TargetInfoGetter {
33+
public:
34+
// Target State
35+
const TargetState &GetState() const;
36+
bool GetBuildState() const;
37+
bool GetLockState() const;
38+
39+
// Target Env
40+
const fs::path &GetTargetRootDir() const;
41+
const fs::path &GetTargetBuildDir() const;
42+
43+
// Target Config
44+
const TargetConfig &GetConfig() const;
45+
46+
// Target Storer
47+
const internal::fs_unordered_set &GetCurrentSourceFiles() const;
48+
const internal::fs_unordered_set &GetCurrentHeaderFiles() const;
49+
const internal::fs_unordered_set &GetCurrentPchFiles() const;
50+
const internal::fs_unordered_set &GetTargetLibDeps() const;
51+
const std::unordered_set<std::string> &GetCurrentExternalLibDeps() const;
52+
const internal::fs_unordered_set &GetCurrentIncludeDirs() const;
53+
const internal::fs_unordered_set &GetCurrentLibDirs() const;
54+
const std::unordered_set<std::string> &GetCurrentPreprocessorFlags() const;
55+
const std::unordered_set<std::string> &GetCurrentCommonCompileFlags() const;
56+
const std::unordered_set<std::string> &GetCurrentPchCompileFlags() const;
57+
const std::unordered_set<std::string> &GetCurrentPchObjectFlags() const;
58+
const std::unordered_set<std::string> &GetCurrentAsmCompileFlags() const;
59+
const std::unordered_set<std::string> &GetCurrentCCompileFlags() const;
60+
const std::unordered_set<std::string> &GetCurrentCppCompileFlags() const;
61+
const std::unordered_set<std::string> &GetCurrentLinkFlags() const;
62+
const internal::fs_unordered_set &GetCurrentCompileDependencies() const;
63+
const internal::fs_unordered_set &GetCurrentLinkDependencies() const;
64+
};
65+
66+
} // namespace buildcc::base
67+
68+
#endif

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@
2121

2222
namespace buildcc::base {
2323

24+
// TODO, Seperate TargetState into lock_ and other internal boolean variables
25+
// NOTE, This is because apart from lock_ every other parameter is updated
26+
// during `Target::Build`
27+
// TargetInfo does not have a `Build` method, it is only meant to hold
28+
// information
2429
struct TargetState {
2530
void SetSourceState(TargetFileExt file_extension);
2631
void SetPch();
2732
void SetLock();
2833

2934
void ExpectsUnlock() const;
3035
void ExpectsLock() const;
36+
// TODO, IsLocked
3137

3238
bool ContainsPch() const { return contains_pch_; }
33-
// TODO, ContainsAsm
34-
// TODO, ContainsC
35-
// TODO, ContainsCpp
36-
37-
// TODO, IsLocked
38-
// TODO, IsBuilt
3939

4040
// TODO, Make these private getters
4141
bool contains_asm{false};

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

Lines changed: 20 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,12 @@
3030
// Interface
3131
#include "target/builder_interface.h"
3232

33+
// API
34+
#include "target/target_info.h"
35+
3336
// Common
34-
#include "target/common/target_config.h"
35-
#include "target/common/target_env.h"
36-
#include "target/common/target_state.h"
3737
#include "target/common/target_type.h"
3838

39-
// API
40-
#include "target/api/copy_api.h"
41-
#include "target/api/deps_api.h"
42-
#include "target/api/flag_api.h"
43-
#include "target/api/include_api.h"
44-
#include "target/api/lib_api.h"
45-
#include "target/api/pch_api.h"
46-
#include "target/api/source_api.h"
47-
4839
// Friend
4940
#include "target/friend/compile_object.h"
5041
#include "target/friend/compile_pch.h"
@@ -70,52 +61,31 @@ namespace buildcc::base {
7061
// of the inheritance pattern
7162
// NOTE, base::Target is meant to be a blank slate which can be customized by
7263
// the specialized target-toolchain classes
73-
class Target : public BuilderInterface,
74-
public CopyApi<Target>,
75-
public SourceApi<Target>,
76-
public IncludeApi<Target>,
77-
public LibApi<Target>,
78-
public PchApi<Target>,
79-
public FlagApi<Target>,
80-
public DepsApi<Target> {
64+
class Target : public BuilderInterface, public TargetInfo {
8165

8266
public:
8367
explicit Target(const std::string &name, TargetType type,
8468
const Toolchain &toolchain, const TargetEnv &env,
85-
const TargetConfig &config = {})
86-
: name_(name), type_(type), toolchain_(toolchain), config_(config),
87-
env_(env.GetTargetRootDir(),
88-
env.GetTargetBuildDir() / toolchain.GetName() / name),
69+
const TargetConfig &config = TargetConfig())
70+
: TargetInfo(
71+
TargetEnv(env.GetTargetRootDir(),
72+
env.GetTargetBuildDir() / toolchain.GetName() / name),
73+
config),
74+
name_(name), type_(type), toolchain_(toolchain),
8975
loader_(name, env_.GetTargetBuildDir()), compile_pch_(*this),
9076
compile_object_(*this), link_target_(*this) {
9177
Initialize();
9278
}
9379
virtual ~Target() {}
94-
9580
Target(const Target &target) = delete;
9681

9782
// Builders
9883
void Build() override;
9984

100-
// TODO, Add more setters
101-
102-
//
103-
std::optional<std::string> SelectCompileFlags(TargetFileExt ext) const;
104-
std::optional<std::string> SelectCompiler(TargetFileExt ext) const;
105-
10685
// Getters (GENERIC)
10786

108-
// Target state
109-
110-
// Set during first build or rebuild
111-
// lock == true after Build is called
112-
const TargetState &GetState() const { return state_; }
113-
bool GetBuildState() const { return state_.build; }
114-
bool GetLockState() const { return state_.lock; }
115-
11687
// NOTE, We are constructing the path
11788
fs::path GetBinaryPath() const { return loader_.GetBinaryPath(); }
118-
11989
const fs::path &GetTargetPath() const { return link_target_.GetOutput(); }
12090
const fs::path &GetPchHeaderPath() const {
12191
return compile_pch_.GetHeaderPath();
@@ -130,62 +100,8 @@ class Target : public BuilderInterface,
130100
const std::string &GetName() const { return name_; }
131101
const Toolchain &GetToolchain() const { return toolchain_; }
132102
TargetType GetType() const { return type_; }
133-
const fs::path &GetTargetRootDir() const { return env_.GetTargetRootDir(); }
134-
const fs::path &GetTargetBuildDir() const { return env_.GetTargetBuildDir(); }
135-
const TargetConfig &GetConfig() const { return config_; }
136103

137104
//
138-
const internal::fs_unordered_set &GetCurrentSourceFiles() const {
139-
return storer_.current_source_files.user;
140-
}
141-
const internal::fs_unordered_set &GetCurrentHeaderFiles() const {
142-
return storer_.current_header_files.user;
143-
}
144-
const internal::fs_unordered_set &GetCurrentPchFiles() const {
145-
return storer_.current_pch_files.user;
146-
}
147-
const internal::fs_unordered_set &GetTargetLibDeps() const {
148-
return storer_.current_lib_deps.user;
149-
}
150-
const std::unordered_set<std::string> &GetCurrentExternalLibDeps() const {
151-
return storer_.current_external_lib_deps;
152-
}
153-
const internal::fs_unordered_set &GetCurrentIncludeDirs() const {
154-
return storer_.current_include_dirs;
155-
}
156-
const internal::fs_unordered_set &GetCurrentLibDirs() const {
157-
return storer_.current_lib_dirs;
158-
}
159-
const std::unordered_set<std::string> &GetCurrentPreprocessorFlags() const {
160-
return storer_.current_preprocessor_flags;
161-
}
162-
const std::unordered_set<std::string> &GetCurrentCommonCompileFlags() const {
163-
return storer_.current_common_compile_flags;
164-
}
165-
const std::unordered_set<std::string> &GetCurrentPchCompileFlags() const {
166-
return storer_.current_pch_compile_flags;
167-
}
168-
const std::unordered_set<std::string> &GetCurrentPchObjectFlags() const {
169-
return storer_.current_pch_object_flags;
170-
}
171-
const std::unordered_set<std::string> &GetCurrentAsmCompileFlags() const {
172-
return storer_.current_asm_compile_flags;
173-
}
174-
const std::unordered_set<std::string> &GetCurrentCCompileFlags() const {
175-
return storer_.current_c_compile_flags;
176-
}
177-
const std::unordered_set<std::string> &GetCurrentCppCompileFlags() const {
178-
return storer_.current_cpp_compile_flags;
179-
}
180-
const std::unordered_set<std::string> &GetCurrentLinkFlags() const {
181-
return storer_.current_link_flags;
182-
}
183-
const internal::fs_unordered_set &GetCurrentCompileDependencies() const {
184-
return storer_.current_compile_dependencies.user;
185-
}
186-
const internal::fs_unordered_set &GetCurrentLinkDependencies() const {
187-
return storer_.current_link_dependencies.user;
188-
}
189105

190106
// Getters (state_.ExpectsLock)
191107

@@ -221,6 +137,10 @@ class Target : public BuilderInterface,
221137
private:
222138
void Initialize();
223139

140+
//
141+
std::optional<std::string> SelectCompileFlags(TargetFileExt ext) const;
142+
std::optional<std::string> SelectCompiler(TargetFileExt ext) const;
143+
224144
// Recompilation checks
225145
void RecheckPaths(const internal::path_unordered_set &previous_path,
226146
const internal::path_unordered_set &current_path);
@@ -235,7 +155,10 @@ class Target : public BuilderInterface,
235155
// Fbs
236156
bool Store() override;
237157

238-
// Callbacks
158+
// Tasks
159+
void TaskDeps();
160+
161+
// Callbacks for unit tests
239162
void SourceRemoved();
240163
void SourceAdded();
241164
void SourceUpdated();
@@ -247,25 +170,18 @@ class Target : public BuilderInterface,
247170
void FlagChanged();
248171
void ExternalLibChanged();
249172

250-
void TaskDeps();
251-
252173
private:
253-
// Constructor defined
254174
std::string name_;
255175
TargetType type_;
256176
const Toolchain &toolchain_;
257-
TargetConfig config_;
258-
TargetEnv env_;
259177
internal::TargetLoader loader_;
260178

261-
// Friend
179+
// Friend classes
262180
CompilePch compile_pch_;
263181
CompileObject compile_object_;
264182
LinkTarget link_target_;
265183

266-
// Used for serialization
267-
internal::TargetStorer storer_;
268-
TargetState state_;
184+
//
269185
Command command_;
270186
tf::Taskflow tf_;
271187
};

0 commit comments

Comments
 (0)