diff --git a/buildcc/lib/target/cmake/mock_target.cmake b/buildcc/lib/target/cmake/mock_target.cmake index 96d037c9..a622254b 100644 --- a/buildcc/lib/target/cmake/mock_target.cmake +++ b/buildcc/lib/target/cmake/mock_target.cmake @@ -7,7 +7,6 @@ add_library(mock_target STATIC src/common/target_state.cpp # API - src/api/copy_api.cpp src/api/source_api.cpp src/api/include_api.cpp src/api/lib_api.cpp @@ -15,6 +14,11 @@ add_library(mock_target STATIC src/api/flag_api.cpp src/api/deps_api.cpp + src/api/copy_api.cpp + + src/api/target_info_getter.cpp + + # Generator src/generator/generator_loader.cpp src/generator/generator_storer.cpp diff --git a/buildcc/lib/target/cmake/target.cmake b/buildcc/lib/target/cmake/target.cmake index 16b3c190..6510e998 100644 --- a/buildcc/lib/target/cmake/target.cmake +++ b/buildcc/lib/target/cmake/target.cmake @@ -18,14 +18,12 @@ set(TARGET_SRCS include/target/common/target_type.h # API - src/api/copy_api.cpp src/api/source_api.cpp src/api/include_api.cpp src/api/lib_api.cpp src/api/pch_api.cpp src/api/flag_api.cpp src/api/deps_api.cpp - include/target/api/copy_api.h include/target/api/source_api.h include/target/api/include_api.h include/target/api/lib_api.h @@ -33,6 +31,12 @@ set(TARGET_SRCS include/target/api/flag_api.h include/target/api/deps_api.h + src/api/copy_api.cpp + include/target/api/copy_api.h + + src/api/target_info_getter.cpp + include/target/api/target_info_getter.h + # Generator src/generator/generator_loader.cpp src/generator/generator_storer.cpp diff --git a/buildcc/lib/target/include/target/api/target_info_getter.h b/buildcc/lib/target/include/target/api/target_info_getter.h new file mode 100644 index 00000000..c3db01d4 --- /dev/null +++ b/buildcc/lib/target/include/target/api/target_info_getter.h @@ -0,0 +1,68 @@ +/* + * Copyright 2021 Niket Naidu. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TARGET_API_TARGET_INFO_GETTER_H_ +#define TARGET_API_TARGET_INFO_GETTER_H_ + +#include "target/path.h" + +#include "target/common/target_config.h" +#include "target/common/target_state.h" + +namespace buildcc::base { + +// Requires +// - TargetStorer +// - TargetState +// - TargetEnv +// - TargetConfig +template class TargetInfoGetter { +public: + // Target State + const TargetState &GetState() const; + bool GetBuildState() const; + bool GetLockState() const; + + // Target Env + const fs::path &GetTargetRootDir() const; + const fs::path &GetTargetBuildDir() const; + + // Target Config + const TargetConfig &GetConfig() const; + + // Target Storer + const internal::fs_unordered_set &GetCurrentSourceFiles() const; + const internal::fs_unordered_set &GetCurrentHeaderFiles() const; + const internal::fs_unordered_set &GetCurrentPchFiles() const; + const internal::fs_unordered_set &GetTargetLibDeps() const; + const std::unordered_set &GetCurrentExternalLibDeps() const; + const internal::fs_unordered_set &GetCurrentIncludeDirs() const; + const internal::fs_unordered_set &GetCurrentLibDirs() const; + const std::unordered_set &GetCurrentPreprocessorFlags() const; + const std::unordered_set &GetCurrentCommonCompileFlags() const; + const std::unordered_set &GetCurrentPchCompileFlags() const; + const std::unordered_set &GetCurrentPchObjectFlags() const; + const std::unordered_set &GetCurrentAsmCompileFlags() const; + const std::unordered_set &GetCurrentCCompileFlags() const; + const std::unordered_set &GetCurrentCppCompileFlags() const; + const std::unordered_set &GetCurrentLinkFlags() const; + const internal::fs_unordered_set &GetCurrentCompileDependencies() const; + const internal::fs_unordered_set &GetCurrentLinkDependencies() const; +}; + +} // namespace buildcc::base + +#endif diff --git a/buildcc/lib/target/include/target/common/target_state.h b/buildcc/lib/target/include/target/common/target_state.h index 92f1ba18..49affaba 100644 --- a/buildcc/lib/target/include/target/common/target_state.h +++ b/buildcc/lib/target/include/target/common/target_state.h @@ -21,6 +21,11 @@ namespace buildcc::base { +// TODO, Seperate TargetState into lock_ and other internal boolean variables +// NOTE, This is because apart from lock_ every other parameter is updated +// during `Target::Build` +// TargetInfo does not have a `Build` method, it is only meant to hold +// information struct TargetState { void SetSourceState(TargetFileExt file_extension); void SetPch(); @@ -28,14 +33,9 @@ struct TargetState { void ExpectsUnlock() const; void ExpectsLock() const; + // TODO, IsLocked bool ContainsPch() const { return contains_pch_; } - // TODO, ContainsAsm - // TODO, ContainsC - // TODO, ContainsCpp - - // TODO, IsLocked - // TODO, IsBuilt // TODO, Make these private getters bool contains_asm{false}; diff --git a/buildcc/lib/target/include/target/target.h b/buildcc/lib/target/include/target/target.h index f01786bc..98a4548d 100644 --- a/buildcc/lib/target/include/target/target.h +++ b/buildcc/lib/target/include/target/target.h @@ -30,21 +30,12 @@ // Interface #include "target/builder_interface.h" +// API +#include "target/target_info.h" + // Common -#include "target/common/target_config.h" -#include "target/common/target_env.h" -#include "target/common/target_state.h" #include "target/common/target_type.h" -// API -#include "target/api/copy_api.h" -#include "target/api/deps_api.h" -#include "target/api/flag_api.h" -#include "target/api/include_api.h" -#include "target/api/lib_api.h" -#include "target/api/pch_api.h" -#include "target/api/source_api.h" - // Friend #include "target/friend/compile_object.h" #include "target/friend/compile_pch.h" @@ -70,52 +61,31 @@ namespace buildcc::base { // of the inheritance pattern // NOTE, base::Target is meant to be a blank slate which can be customized by // the specialized target-toolchain classes -class Target : public BuilderInterface, - public CopyApi, - public SourceApi, - public IncludeApi, - public LibApi, - public PchApi, - public FlagApi, - public DepsApi { +class Target : public BuilderInterface, public TargetInfo { public: explicit Target(const std::string &name, TargetType type, const Toolchain &toolchain, const TargetEnv &env, - const TargetConfig &config = {}) - : name_(name), type_(type), toolchain_(toolchain), config_(config), - env_(env.GetTargetRootDir(), - env.GetTargetBuildDir() / toolchain.GetName() / name), + const TargetConfig &config = TargetConfig()) + : TargetInfo( + TargetEnv(env.GetTargetRootDir(), + env.GetTargetBuildDir() / toolchain.GetName() / name), + config), + name_(name), type_(type), toolchain_(toolchain), loader_(name, env_.GetTargetBuildDir()), compile_pch_(*this), compile_object_(*this), link_target_(*this) { Initialize(); } virtual ~Target() {} - Target(const Target &target) = delete; // Builders void Build() override; - // TODO, Add more setters - - // - std::optional SelectCompileFlags(TargetFileExt ext) const; - std::optional SelectCompiler(TargetFileExt ext) const; - // Getters (GENERIC) - // Target state - - // Set during first build or rebuild - // lock == true after Build is called - const TargetState &GetState() const { return state_; } - bool GetBuildState() const { return state_.build; } - bool GetLockState() const { return state_.lock; } - // NOTE, We are constructing the path fs::path GetBinaryPath() const { return loader_.GetBinaryPath(); } - const fs::path &GetTargetPath() const { return link_target_.GetOutput(); } const fs::path &GetPchHeaderPath() const { return compile_pch_.GetHeaderPath(); @@ -130,62 +100,8 @@ class Target : public BuilderInterface, const std::string &GetName() const { return name_; } const Toolchain &GetToolchain() const { return toolchain_; } TargetType GetType() const { return type_; } - const fs::path &GetTargetRootDir() const { return env_.GetTargetRootDir(); } - const fs::path &GetTargetBuildDir() const { return env_.GetTargetBuildDir(); } - const TargetConfig &GetConfig() const { return config_; } // - const internal::fs_unordered_set &GetCurrentSourceFiles() const { - return storer_.current_source_files.user; - } - const internal::fs_unordered_set &GetCurrentHeaderFiles() const { - return storer_.current_header_files.user; - } - const internal::fs_unordered_set &GetCurrentPchFiles() const { - return storer_.current_pch_files.user; - } - const internal::fs_unordered_set &GetTargetLibDeps() const { - return storer_.current_lib_deps.user; - } - const std::unordered_set &GetCurrentExternalLibDeps() const { - return storer_.current_external_lib_deps; - } - const internal::fs_unordered_set &GetCurrentIncludeDirs() const { - return storer_.current_include_dirs; - } - const internal::fs_unordered_set &GetCurrentLibDirs() const { - return storer_.current_lib_dirs; - } - const std::unordered_set &GetCurrentPreprocessorFlags() const { - return storer_.current_preprocessor_flags; - } - const std::unordered_set &GetCurrentCommonCompileFlags() const { - return storer_.current_common_compile_flags; - } - const std::unordered_set &GetCurrentPchCompileFlags() const { - return storer_.current_pch_compile_flags; - } - const std::unordered_set &GetCurrentPchObjectFlags() const { - return storer_.current_pch_object_flags; - } - const std::unordered_set &GetCurrentAsmCompileFlags() const { - return storer_.current_asm_compile_flags; - } - const std::unordered_set &GetCurrentCCompileFlags() const { - return storer_.current_c_compile_flags; - } - const std::unordered_set &GetCurrentCppCompileFlags() const { - return storer_.current_cpp_compile_flags; - } - const std::unordered_set &GetCurrentLinkFlags() const { - return storer_.current_link_flags; - } - const internal::fs_unordered_set &GetCurrentCompileDependencies() const { - return storer_.current_compile_dependencies.user; - } - const internal::fs_unordered_set &GetCurrentLinkDependencies() const { - return storer_.current_link_dependencies.user; - } // Getters (state_.ExpectsLock) @@ -221,6 +137,10 @@ class Target : public BuilderInterface, private: void Initialize(); + // + std::optional SelectCompileFlags(TargetFileExt ext) const; + std::optional SelectCompiler(TargetFileExt ext) const; + // Recompilation checks void RecheckPaths(const internal::path_unordered_set &previous_path, const internal::path_unordered_set ¤t_path); @@ -235,7 +155,10 @@ class Target : public BuilderInterface, // Fbs bool Store() override; - // Callbacks + // Tasks + void TaskDeps(); + + // Callbacks for unit tests void SourceRemoved(); void SourceAdded(); void SourceUpdated(); @@ -247,25 +170,18 @@ class Target : public BuilderInterface, void FlagChanged(); void ExternalLibChanged(); - void TaskDeps(); - private: - // Constructor defined std::string name_; TargetType type_; const Toolchain &toolchain_; - TargetConfig config_; - TargetEnv env_; internal::TargetLoader loader_; - // Friend + // Friend classes CompilePch compile_pch_; CompileObject compile_object_; LinkTarget link_target_; - // Used for serialization - internal::TargetStorer storer_; - TargetState state_; + // Command command_; tf::Taskflow tf_; }; diff --git a/buildcc/lib/target/include/target/target_info.h b/buildcc/lib/target/include/target/target_info.h new file mode 100644 index 00000000..bdf889bd --- /dev/null +++ b/buildcc/lib/target/include/target/target_info.h @@ -0,0 +1,84 @@ +/* + * Copyright 2021 Niket Naidu. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TARGET_TARGET_INFO_H_ +#define TARGET_TARGET_INFO_H_ + +#include + +#include "target/common/target_config.h" +#include "target/common/target_env.h" +#include "target/common/target_state.h" +#include "target/target_storer.h" + +#include "target/api/copy_api.h" +#include "target/api/deps_api.h" +#include "target/api/flag_api.h" +#include "target/api/include_api.h" +#include "target/api/lib_api.h" +#include "target/api/pch_api.h" + +#include "target/api/source_api.h" + +#include "target/api/target_info_getter.h" + +namespace buildcc::base { + +// +class TargetInfo : public SourceApi, + public IncludeApi, + public LibApi, + public PchApi, + public FlagApi, + public DepsApi, + public CopyApi, + public TargetInfoGetter { +public: + TargetInfo(const TargetEnv &env, const TargetConfig &config = TargetConfig()) + : env_(env), config_(config) {} + +private: + // Inputs + friend class SourceApi; + friend class IncludeApi; + friend class LibApi; + friend class PchApi; + friend class FlagApi; + friend class DepsApi; + + // Feature + friend class CopyApi; + + // Getters + friend class TargetInfoGetter; + +protected: + TargetEnv env_; + TargetConfig config_; + + internal::TargetStorer storer_; + TargetState state_; +}; + +} // namespace buildcc::base + +namespace buildcc { + +typedef base::TargetInfo TargetInfo; + +} + +#endif diff --git a/buildcc/lib/target/src/api/copy_api.cpp b/buildcc/lib/target/src/api/copy_api.cpp index b3dfbddf..cbdd8108 100644 --- a/buildcc/lib/target/src/api/copy_api.cpp +++ b/buildcc/lib/target/src/api/copy_api.cpp @@ -16,7 +16,7 @@ #include "target/api/copy_api.h" -#include "target/target.h" +#include "target/target_info.h" namespace buildcc::base { @@ -24,13 +24,13 @@ template void CopyApi::Copy(const T &target, std::initializer_list options) { env::log_trace(__FUNCTION__, "Copy by const ref"); - SpecializedCopy(target, options); + SpecializedCopy(target, options); } template void CopyApi::Copy(T &&target, std::initializer_list options) { env::log_trace(__FUNCTION__, "Copy by move"); - SpecializedCopy(std::move(target), options); + SpecializedCopy(std::move(target), options); } // template @@ -116,6 +116,6 @@ void CopyApi::SpecializedCopy(TargetType target, } } -template class CopyApi; +template class CopyApi; } // namespace buildcc::base diff --git a/buildcc/lib/target/src/api/deps_api.cpp b/buildcc/lib/target/src/api/deps_api.cpp index ff6b51d4..f0c85a91 100644 --- a/buildcc/lib/target/src/api/deps_api.cpp +++ b/buildcc/lib/target/src/api/deps_api.cpp @@ -16,7 +16,7 @@ #include "target/api/deps_api.h" -#include "target/target.h" +#include "target/target_info.h" namespace buildcc::base { @@ -51,6 +51,6 @@ void DepsApi::AddLinkDependency(const fs::path &relative_path) { AddLinkDependencyAbsolute(absolute_path); } -template class DepsApi; +template class DepsApi; } // namespace buildcc::base diff --git a/buildcc/lib/target/src/api/flag_api.cpp b/buildcc/lib/target/src/api/flag_api.cpp index 0609ee47..7145f54b 100644 --- a/buildcc/lib/target/src/api/flag_api.cpp +++ b/buildcc/lib/target/src/api/flag_api.cpp @@ -16,7 +16,7 @@ #include "target/api/flag_api.h" -#include "target/target.h" +#include "target/target_info.h" namespace buildcc::base { @@ -76,6 +76,6 @@ template void FlagApi::AddLinkFlag(const std::string &flag) { t.storer_.current_link_flags.insert(flag); } -template class FlagApi; +template class FlagApi; } // namespace buildcc::base diff --git a/buildcc/lib/target/src/api/include_api.cpp b/buildcc/lib/target/src/api/include_api.cpp index 9833f5ab..2b66eb19 100644 --- a/buildcc/lib/target/src/api/include_api.cpp +++ b/buildcc/lib/target/src/api/include_api.cpp @@ -16,7 +16,7 @@ #include "target/api/include_api.h" -#include "target/target.h" +#include "target/target_info.h" namespace buildcc::base { @@ -83,6 +83,6 @@ void IncludeApi::AddIncludeDirAbsolute(const fs::path &absolute_include_dir, } } -template class IncludeApi; +template class IncludeApi; } // namespace buildcc::base diff --git a/buildcc/lib/target/src/api/lib_api.cpp b/buildcc/lib/target/src/api/lib_api.cpp index 290f93b8..5fcd05d5 100644 --- a/buildcc/lib/target/src/api/lib_api.cpp +++ b/buildcc/lib/target/src/api/lib_api.cpp @@ -17,6 +17,7 @@ #include "target/api/lib_api.h" #include "target/target.h" +#include "target/target_info.h" namespace buildcc::base { @@ -50,6 +51,6 @@ template void LibApi::AddLibDep(const std::string &lib_dep) { t.storer_.current_external_lib_deps.insert(lib_dep); } -template class LibApi; +template class LibApi; } // namespace buildcc::base diff --git a/buildcc/lib/target/src/api/pch_api.cpp b/buildcc/lib/target/src/api/pch_api.cpp index 115e2d6f..58692067 100644 --- a/buildcc/lib/target/src/api/pch_api.cpp +++ b/buildcc/lib/target/src/api/pch_api.cpp @@ -16,7 +16,7 @@ #include "target/api/pch_api.h" -#include "target/target.h" +#include "target/target_info.h" namespace buildcc::base { @@ -43,6 +43,6 @@ void PchApi::AddPch(const fs::path &relative_filename, AddPchAbsolute(absolute_pch); } -template class PchApi; +template class PchApi; } // namespace buildcc::base diff --git a/buildcc/lib/target/src/api/source_api.cpp b/buildcc/lib/target/src/api/source_api.cpp index 4e407a1e..154c1fda 100644 --- a/buildcc/lib/target/src/api/source_api.cpp +++ b/buildcc/lib/target/src/api/source_api.cpp @@ -16,7 +16,7 @@ #include "target/api/source_api.h" -#include "target/target.h" +#include "target/target_info.h" namespace buildcc::base { @@ -67,6 +67,6 @@ void SourceApi::GlobSources(const fs::path &relative_to_target_path) { } // -template class SourceApi; +template class SourceApi; } // namespace buildcc::base diff --git a/buildcc/lib/target/src/api/target_info_getter.cpp b/buildcc/lib/target/src/api/target_info_getter.cpp new file mode 100644 index 00000000..66c7381e --- /dev/null +++ b/buildcc/lib/target/src/api/target_info_getter.cpp @@ -0,0 +1,204 @@ +/* + * Copyright 2021 Niket Naidu. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "target/api/target_info_getter.h" + +#include "target/target_info.h" + +namespace buildcc::base { + +// Target State +template const TargetState &TargetInfoGetter::GetState() const { + const T &t = static_cast(*this); + + return t.state_; +} + +template bool TargetInfoGetter::GetBuildState() const { + const T &t = static_cast(*this); + + return t.state_.build; +} + +template bool TargetInfoGetter::GetLockState() const { + const T &t = static_cast(*this); + + return t.state_.lock; +} + +// Target Env +template +const fs::path &TargetInfoGetter::GetTargetRootDir() const { + const T &t = static_cast(*this); + + return t.env_.GetTargetRootDir(); +} + +template +const fs::path &TargetInfoGetter::GetTargetBuildDir() const { + const T &t = static_cast(*this); + + return t.env_.GetTargetBuildDir(); +} + +// Target Config +template +const TargetConfig &TargetInfoGetter::GetConfig() const { + const T &t = static_cast(*this); + + return t.config_; +} + +// Target Storer +template +const internal::fs_unordered_set & +TargetInfoGetter::GetCurrentSourceFiles() const { + const T &t = static_cast(*this); + + return t.storer_.current_source_files.user; +} + +template +const internal::fs_unordered_set & +TargetInfoGetter::GetCurrentHeaderFiles() const { + const T &t = static_cast(*this); + + return t.storer_.current_header_files.user; +} + +template +const internal::fs_unordered_set & +TargetInfoGetter::GetCurrentPchFiles() const { + const T &t = static_cast(*this); + + return t.storer_.current_pch_files.user; +} + +template +const internal::fs_unordered_set & +TargetInfoGetter::GetTargetLibDeps() const { + const T &t = static_cast(*this); + + return t.storer_.current_lib_deps.user; +} + +template +const std::unordered_set & +TargetInfoGetter::GetCurrentExternalLibDeps() const { + const T &t = static_cast(*this); + + return t.storer_.current_external_lib_deps; +} + +template +const internal::fs_unordered_set & +TargetInfoGetter::GetCurrentIncludeDirs() const { + const T &t = static_cast(*this); + + return t.storer_.current_include_dirs; +} + +template +const internal::fs_unordered_set & +TargetInfoGetter::GetCurrentLibDirs() const { + const T &t = static_cast(*this); + + return t.storer_.current_lib_dirs; +} + +template +const std::unordered_set & +TargetInfoGetter::GetCurrentPreprocessorFlags() const { + const T &t = static_cast(*this); + + return t.storer_.current_preprocessor_flags; +} + +template +const std::unordered_set & +TargetInfoGetter::GetCurrentCommonCompileFlags() const { + const T &t = static_cast(*this); + + return t.storer_.current_common_compile_flags; +} + +template +const std::unordered_set & +TargetInfoGetter::GetCurrentPchCompileFlags() const { + const T &t = static_cast(*this); + + return t.storer_.current_pch_compile_flags; +} + +template +const std::unordered_set & +TargetInfoGetter::GetCurrentPchObjectFlags() const { + const T &t = static_cast(*this); + + return t.storer_.current_pch_object_flags; +} + +template +const std::unordered_set & +TargetInfoGetter::GetCurrentAsmCompileFlags() const { + const T &t = static_cast(*this); + + return t.storer_.current_asm_compile_flags; +} + +template +const std::unordered_set & +TargetInfoGetter::GetCurrentCCompileFlags() const { + const T &t = static_cast(*this); + + return t.storer_.current_c_compile_flags; +} + +template +const std::unordered_set & +TargetInfoGetter::GetCurrentCppCompileFlags() const { + const T &t = static_cast(*this); + + return t.storer_.current_cpp_compile_flags; +} + +template +const std::unordered_set & +TargetInfoGetter::GetCurrentLinkFlags() const { + const T &t = static_cast(*this); + + return t.storer_.current_link_flags; +} + +template +const internal::fs_unordered_set & +TargetInfoGetter::GetCurrentCompileDependencies() const { + const T &t = static_cast(*this); + + return t.storer_.current_compile_dependencies.user; +} + +template +const internal::fs_unordered_set & +TargetInfoGetter::GetCurrentLinkDependencies() const { + const T &t = static_cast(*this); + + return t.storer_.current_link_dependencies.user; +} + +template class TargetInfoGetter; + +} // namespace buildcc::base