From 6edaf097ebfda38e66b760883049b856d278afa4 Mon Sep 17 00:00:00 2001 From: Niket Naidu Date: Wed, 17 Nov 2021 01:15:21 -0800 Subject: [PATCH 1/5] Added include_api --- buildcc/lib/target/cmake/mock_target.cmake | 1 + buildcc/lib/target/cmake/target.cmake | 2 + .../target/include/target/api/include_api.h | 43 +++++++++ buildcc/lib/target/src/api/include_api.cpp | 88 +++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 buildcc/lib/target/include/target/api/include_api.h create mode 100644 buildcc/lib/target/src/api/include_api.cpp diff --git a/buildcc/lib/target/cmake/mock_target.cmake b/buildcc/lib/target/cmake/mock_target.cmake index c0e494b5..41c8406a 100644 --- a/buildcc/lib/target/cmake/mock_target.cmake +++ b/buildcc/lib/target/cmake/mock_target.cmake @@ -9,6 +9,7 @@ add_library(mock_target STATIC # API src/api/copy_api.cpp src/api/source_api.cpp + src/api/include_api.cpp # Generator src/generator/generator_loader.cpp diff --git a/buildcc/lib/target/cmake/target.cmake b/buildcc/lib/target/cmake/target.cmake index 646e5806..95045709 100644 --- a/buildcc/lib/target/cmake/target.cmake +++ b/buildcc/lib/target/cmake/target.cmake @@ -20,8 +20,10 @@ set(TARGET_SRCS # API src/api/copy_api.cpp src/api/source_api.cpp + src/api/include_api.cpp include/target/api/copy_api.h include/target/api/source_api.h + include/target/api/include_api.h # Generator src/generator/generator_loader.cpp diff --git a/buildcc/lib/target/include/target/api/include_api.h b/buildcc/lib/target/include/target/api/include_api.h new file mode 100644 index 00000000..c4b1322b --- /dev/null +++ b/buildcc/lib/target/include/target/api/include_api.h @@ -0,0 +1,43 @@ +/* + * 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_INCLUDE_API_H_ +#define TARGET_API_INCLUDE_API_H_ + +#include + +namespace fs = std::filesystem; + +namespace buildcc::base { + +template class IncludeApi { +public: + void AddHeader(const fs::path &relative_filename, + const fs::path &relative_to_target_path = ""); + void AddHeaderAbsolute(const fs::path &absolute_filepath); + + void GlobHeaders(const fs::path &relative_to_target_path); + void GlobHeadersAbsolute(const fs::path &absolute_path); + + void AddIncludeDir(const fs::path &relative_include_dir, + bool glob_headers = false); + void AddIncludeDirAbsolute(const fs::path &absolute_include_dir, + bool glob_headers = false); +}; + +} // namespace buildcc::base + +#endif diff --git a/buildcc/lib/target/src/api/include_api.cpp b/buildcc/lib/target/src/api/include_api.cpp new file mode 100644 index 00000000..9833f5ab --- /dev/null +++ b/buildcc/lib/target/src/api/include_api.cpp @@ -0,0 +1,88 @@ +/* + * 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/include_api.h" + +#include "target/target.h" + +namespace buildcc::base { + +template +void IncludeApi::AddHeaderAbsolute(const fs::path &absolute_filepath) { + T &t = static_cast(*this); + + t.state_.ExpectsUnlock(); + t.config_.ExpectsValidHeader(absolute_filepath); + t.storer_.current_header_files.user.insert(absolute_filepath); +} + +template +void IncludeApi::AddHeader(const fs::path &relative_filename, + const fs::path &relative_to_target_path) { + T &t = static_cast(*this); + + // Check Source + fs::path absolute_filepath = + t.env_.GetTargetRootDir() / relative_to_target_path / relative_filename; + AddHeaderAbsolute(absolute_filepath); +} + +template +void IncludeApi::GlobHeaders(const fs::path &relative_to_target_path) { + T &t = static_cast(*this); + + fs::path absolute_path = t.env_.GetTargetRootDir() / relative_to_target_path; + GlobHeadersAbsolute(absolute_path); +} + +template +void IncludeApi::GlobHeadersAbsolute(const fs::path &absolute_path) { + T &t = static_cast(*this); + + for (const auto &p : fs::directory_iterator(absolute_path)) { + if (t.config_.IsValidHeader(p.path())) { + env::log_trace(__FUNCTION__, fmt::format("Added header {}", p.path())); + AddHeaderAbsolute(p.path()); + } + } +} + +template +void IncludeApi::AddIncludeDir(const fs::path &relative_include_dir, + bool glob_headers) { + T &t = static_cast(*this); + + const fs::path absolute_include_dir = + t.env_.GetTargetRootDir() / relative_include_dir; + AddIncludeDirAbsolute(absolute_include_dir, glob_headers); +} + +template +void IncludeApi::AddIncludeDirAbsolute(const fs::path &absolute_include_dir, + bool glob_headers) { + T &t = static_cast(*this); + + t.state_.ExpectsUnlock(); + t.storer_.current_include_dirs.insert(absolute_include_dir); + + if (glob_headers) { + GlobHeadersAbsolute(absolute_include_dir); + } +} + +template class IncludeApi; + +} // namespace buildcc::base From 171254a52c840821b7449efe1e17d20369e88da2 Mon Sep 17 00:00:00 2001 From: Niket Naidu Date: Wed, 17 Nov 2021 01:21:31 -0800 Subject: [PATCH 2/5] Added ExpectsValidHeader API to target_config --- buildcc/lib/target/include/target/common/target_config.h | 1 + buildcc/lib/target/src/common/target_config.cpp | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/buildcc/lib/target/include/target/common/target_config.h b/buildcc/lib/target/include/target/common/target_config.h index 9f58ca82..d7faf2dd 100644 --- a/buildcc/lib/target/include/target/common/target_config.h +++ b/buildcc/lib/target/include/target/common/target_config.h @@ -35,6 +35,7 @@ struct TargetConfig { bool IsValidHeader(const fs::path &filepath) const; void ExpectsValidSource(const fs::path &filepath) const; + void ExpectsValidHeader(const fs::path &filepath) const; std::string target_ext{""}; std::string obj_ext{".o"}; diff --git a/buildcc/lib/target/src/common/target_config.cpp b/buildcc/lib/target/src/common/target_config.cpp index d80b3742..8066c3e3 100644 --- a/buildcc/lib/target/src/common/target_config.cpp +++ b/buildcc/lib/target/src/common/target_config.cpp @@ -79,4 +79,10 @@ bool TargetConfig::IsValidHeader(const fs::path &filepath) const { return valid; } +void TargetConfig::ExpectsValidHeader(const fs::path &filepath) const { + env::assert_fatal( + IsValidHeader(filepath), + fmt::format("{} does not have a valid header extension", filepath)); +} + } // namespace buildcc::base From 19fd481503c623bae1df01b39abcd33e887a4664 Mon Sep 17 00:00:00 2001 From: Niket Naidu Date: Wed, 17 Nov 2021 01:21:41 -0800 Subject: [PATCH 3/5] Update target.h --- buildcc/lib/target/include/target/target.h | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/buildcc/lib/target/include/target/target.h b/buildcc/lib/target/include/target/target.h index fda93e35..44c0a8e7 100644 --- a/buildcc/lib/target/include/target/target.h +++ b/buildcc/lib/target/include/target/target.h @@ -38,6 +38,7 @@ // API #include "target/api/copy_api.h" +#include "target/api/include_api.h" #include "target/api/source_api.h" // Friend @@ -67,7 +68,8 @@ namespace buildcc::base { // the specialized target-toolchain classes class Target : public BuilderInterface, public CopyApi, - public SourceApi { + public SourceApi, + public IncludeApi { public: explicit Target(const std::string &name, TargetType type, @@ -89,25 +91,12 @@ class Target : public BuilderInterface, // Setters - // * Headers - void AddHeader(const fs::path &relative_filename, - const fs::path &relative_to_target_path = ""); - void AddHeaderAbsolute(const fs::path &absolute_filepath); - - void GlobHeaders(const fs::path &relative_to_target_path); - void GlobHeadersAbsolute(const fs::path &absolute_path); - // PCH void AddPch(const fs::path &relative_filename, const fs::path &relative_to_target_path = ""); void AddPchAbsolute(const fs::path &absolute_filepath); // * Include and Lib directory - void AddIncludeDir(const fs::path &relative_include_dir, - bool glob_headers = false); - void AddIncludeDirAbsolute(const fs::path &absolute_include_dir, - bool glob_headers = false); - void AddLibDir(const fs::path &relative_lib_dir); void AddLibDirAbsolute(const fs::path &absolute_lib_dir); @@ -246,6 +235,7 @@ class Target : public BuilderInterface, friend class CopyApi; friend class SourceApi; + friend class IncludeApi; private: void Initialize(); From 390f7c434bba46cf8b3416e7d58269b719dad85e Mon Sep 17 00:00:00 2001 From: Niket Naidu Date: Wed, 17 Nov 2021 01:23:01 -0800 Subject: [PATCH 4/5] Removed include_dir.cpp --- buildcc/lib/target/cmake/mock_target.cmake | 1 - buildcc/lib/target/cmake/target.cmake | 1 - buildcc/lib/target/src/target/include_dir.cpp | 82 ------------------- 3 files changed, 84 deletions(-) delete mode 100644 buildcc/lib/target/src/target/include_dir.cpp diff --git a/buildcc/lib/target/cmake/mock_target.cmake b/buildcc/lib/target/cmake/mock_target.cmake index 41c8406a..a8dadc66 100644 --- a/buildcc/lib/target/cmake/mock_target.cmake +++ b/buildcc/lib/target/cmake/mock_target.cmake @@ -30,7 +30,6 @@ add_library(mock_target STATIC src/target/target_loader.cpp src/target/target_storer.cpp - src/target/include_dir.cpp src/target/pch.cpp src/target/lib.cpp src/target/flags.cpp diff --git a/buildcc/lib/target/cmake/target.cmake b/buildcc/lib/target/cmake/target.cmake index 95045709..08201eb5 100644 --- a/buildcc/lib/target/cmake/target.cmake +++ b/buildcc/lib/target/cmake/target.cmake @@ -50,7 +50,6 @@ set(TARGET_SRCS include/target/target_storer.h include/target/target.h - src/target/include_dir.cpp src/target/pch.cpp src/target/lib.cpp src/target/flags.cpp diff --git a/buildcc/lib/target/src/target/include_dir.cpp b/buildcc/lib/target/src/target/include_dir.cpp deleted file mode 100644 index 1780e10e..00000000 --- a/buildcc/lib/target/src/target/include_dir.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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/target.h" - -#include "env/assert_fatal.h" -#include "env/logging.h" - -#include "target/util.h" - -#include "fmt/format.h" - -namespace buildcc::base { - -void Target::AddHeaderAbsolute(const fs::path &absolute_filepath) { - state_.ExpectsUnlock(); - env::assert_fatal(config_.IsValidHeader(absolute_filepath), - fmt::format("{} does not have a valid header extension", - absolute_filepath)); - storer_.current_header_files.user.insert(absolute_filepath); -} - -void Target::AddHeader(const fs::path &relative_filename, - const fs::path &relative_to_target_path) { - env::log_trace(name_, __FUNCTION__); - - // Check Source - fs::path absolute_filepath = - GetTargetRootDir() / relative_to_target_path / relative_filename; - AddHeaderAbsolute(absolute_filepath); -} - -void Target::GlobHeaders(const fs::path &relative_to_target_path) { - env::log_trace(name_, __FUNCTION__); - - fs::path absolute_path = GetTargetRootDir() / relative_to_target_path; - GlobHeadersAbsolute(absolute_path); -} - -void Target::GlobHeadersAbsolute(const fs::path &absolute_path) { - for (const auto &p : fs::directory_iterator(absolute_path)) { - if (config_.IsValidHeader(p.path())) { - env::log_trace(name_, fmt::format("Added header {}", p.path())); - AddHeaderAbsolute(p.path()); - } - } -} - -// Public -void Target::AddIncludeDir(const fs::path &relative_include_dir, - bool glob_headers) { - env::log_trace(name_, __FUNCTION__); - - const fs::path absolute_include_dir = - GetTargetRootDir() / relative_include_dir; - AddIncludeDirAbsolute(absolute_include_dir, glob_headers); -} - -void Target::AddIncludeDirAbsolute(const fs::path &absolute_include_dir, - bool glob_headers) { - state_.ExpectsUnlock(); - storer_.current_include_dirs.insert(absolute_include_dir); - - if (glob_headers) { - GlobHeadersAbsolute(absolute_include_dir); - } -} - -} // namespace buildcc::base From f60e1b9fde88f297faa4eb0f844da516bf307a0a Mon Sep 17 00:00:00 2001 From: Niket Naidu Date: Wed, 17 Nov 2021 01:24:50 -0800 Subject: [PATCH 5/5] Update README.md --- buildcc/lib/target/src/target/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/buildcc/lib/target/src/target/README.md b/buildcc/lib/target/src/target/README.md index 29a1cec6..e739496a 100644 --- a/buildcc/lib/target/src/target/README.md +++ b/buildcc/lib/target/src/target/README.md @@ -17,8 +17,11 @@ Check the `include/target/api` and `src/api` folder - [x] Copy - Copy src target to dest - Selective copy using `std::initializer` + - `copy_api` - [x] Source -- [ ] Header and Include Dir + - `source_api` +- [x] Header and Include Dir + - `include_api` - [ ] Lib and Lib Dir - [ ] PCH - [ ] Flags @@ -28,9 +31,6 @@ Check the `include/target/api` and `src/api` folder ## Inputs to Target -- [x] `include_dir.cpp` - - Include Dir - - Header File - [x] `pch.cpp` - Precompile Header files - [x] `lib.cpp`