Skip to content

[CRTP] Target Include API #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion buildcc/lib/target/cmake/mock_target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,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
Expand Down
3 changes: 2 additions & 1 deletion buildcc/lib/target/cmake/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,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
Expand Down
43 changes: 43 additions & 0 deletions buildcc/lib/target/include/target/api/include_api.h
Original file line number Diff line number Diff line change
@@ -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 <filesystem>

namespace fs = std::filesystem;

namespace buildcc::base {

template <typename T> 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
1 change: 1 addition & 0 deletions buildcc/lib/target/include/target/common/target_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"};
Expand Down
18 changes: 4 additions & 14 deletions buildcc/lib/target/include/target/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

// API
#include "target/api/copy_api.h"
#include "target/api/include_api.h"
#include "target/api/source_api.h"

// Friend
Expand Down Expand Up @@ -67,7 +68,8 @@ namespace buildcc::base {
// the specialized target-toolchain classes
class Target : public BuilderInterface,
public CopyApi<Target>,
public SourceApi<Target> {
public SourceApi<Target>,
public IncludeApi<Target> {

public:
explicit Target(const std::string &name, TargetType type,
Expand All @@ -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);

Expand Down Expand Up @@ -246,6 +235,7 @@ class Target : public BuilderInterface,

friend class CopyApi<Target>;
friend class SourceApi<Target>;
friend class IncludeApi<Target>;

private:
void Initialize();
Expand Down
88 changes: 88 additions & 0 deletions buildcc/lib/target/src/api/include_api.cpp
Original file line number Diff line number Diff line change
@@ -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 <typename T>
void IncludeApi<T>::AddHeaderAbsolute(const fs::path &absolute_filepath) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.config_.ExpectsValidHeader(absolute_filepath);
t.storer_.current_header_files.user.insert(absolute_filepath);
}

template <typename T>
void IncludeApi<T>::AddHeader(const fs::path &relative_filename,
const fs::path &relative_to_target_path) {
T &t = static_cast<T &>(*this);

// Check Source
fs::path absolute_filepath =
t.env_.GetTargetRootDir() / relative_to_target_path / relative_filename;
AddHeaderAbsolute(absolute_filepath);
}

template <typename T>
void IncludeApi<T>::GlobHeaders(const fs::path &relative_to_target_path) {
T &t = static_cast<T &>(*this);

fs::path absolute_path = t.env_.GetTargetRootDir() / relative_to_target_path;
GlobHeadersAbsolute(absolute_path);
}

template <typename T>
void IncludeApi<T>::GlobHeadersAbsolute(const fs::path &absolute_path) {
T &t = static_cast<T &>(*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 <typename T>
void IncludeApi<T>::AddIncludeDir(const fs::path &relative_include_dir,
bool glob_headers) {
T &t = static_cast<T &>(*this);

const fs::path absolute_include_dir =
t.env_.GetTargetRootDir() / relative_include_dir;
AddIncludeDirAbsolute(absolute_include_dir, glob_headers);
}

template <typename T>
void IncludeApi<T>::AddIncludeDirAbsolute(const fs::path &absolute_include_dir,
bool glob_headers) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.storer_.current_include_dirs.insert(absolute_include_dir);

if (glob_headers) {
GlobHeadersAbsolute(absolute_include_dir);
}
}

template class IncludeApi<Target>;

} // namespace buildcc::base
6 changes: 6 additions & 0 deletions buildcc/lib/target/src/common/target_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions buildcc/lib/target/src/target/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<CopyOption>`
- `copy_api`
- [x] Source
- [ ] Header and Include Dir
- `source_api`
- [x] Header and Include Dir
- `include_api`
- [ ] Lib and Lib Dir
- [ ] PCH
- [ ] Flags
Expand All @@ -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`
Expand Down
82 changes: 0 additions & 82 deletions buildcc/lib/target/src/target/include_dir.cpp

This file was deleted.