Skip to content

[CRTP] Target PCH API #152

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 6 commits into from
Nov 18, 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 @@ -11,6 +11,7 @@ add_library(mock_target STATIC
src/api/source_api.cpp
src/api/include_api.cpp
src/api/lib_api.cpp
src/api/pch_api.cpp

# Generator
src/generator/generator_loader.cpp
Expand All @@ -31,7 +32,6 @@ add_library(mock_target STATIC
src/target/target_loader.cpp
src/target/target_storer.cpp

src/target/pch.cpp
src/target/flags.cpp
src/target/additional_deps.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 @@ -22,10 +22,12 @@ set(TARGET_SRCS
src/api/source_api.cpp
src/api/include_api.cpp
src/api/lib_api.cpp
src/api/pch_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
include/target/api/pch_api.h

# Generator
src/generator/generator_loader.cpp
Expand All @@ -52,7 +54,6 @@ set(TARGET_SRCS
include/target/target_storer.h
include/target/target.h

src/target/pch.cpp
src/target/flags.cpp
src/target/additional_deps.cpp

Expand Down
4 changes: 3 additions & 1 deletion buildcc/lib/target/include/target/api/lib_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ namespace fs = std::filesystem;

namespace buildcc::base {

class Target;

// Requires
// - TargetStorer
// - TargetState
// - TargetEnv
// T::GetTargetPath
template <typename T> class LibApi {
public:
void AddLibDep(const T &lib_dep);
void AddLibDep(const Target &lib_dep);
void AddLibDep(const std::string &lib_dep);

void AddLibDir(const fs::path &relative_lib_dir);
Expand Down
40 changes: 40 additions & 0 deletions buildcc/lib/target/include/target/api/pch_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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_PCH_API_H_
#define TARGET_API_PCH_API_H_

#include <filesystem>

namespace fs = std::filesystem;

namespace buildcc::base {

// Requires
// - TargetStorer
// - TargetState
// - TargetConfig
// - TargetEnv
template <typename T> class PchApi {
public:
void AddPch(const fs::path &relative_filename,
const fs::path &relative_to_target_path = "");
void AddPchAbsolute(const fs::path &absolute_filepath);
};

} // namespace buildcc::base

#endif
10 changes: 4 additions & 6 deletions buildcc/lib/target/include/target/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "target/api/copy_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
Expand Down Expand Up @@ -71,7 +72,8 @@ class Target : public BuilderInterface,
public CopyApi<Target>,
public SourceApi<Target>,
public IncludeApi<Target>,
public LibApi<Target> {
public LibApi<Target>,
public PchApi<Target> {

public:
explicit Target(const std::string &name, TargetType type,
Expand All @@ -93,11 +95,6 @@ class Target : public BuilderInterface,

// Setters

// PCH
void AddPch(const fs::path &relative_filename,
const fs::path &relative_to_target_path = "");
void AddPchAbsolute(const fs::path &absolute_filepath);

// * Flags
void AddPreprocessorFlag(const std::string &flag);
void AddCommonCompileFlag(const std::string &flag);
Expand Down Expand Up @@ -231,6 +228,7 @@ class Target : public BuilderInterface,
friend class SourceApi<Target>;
friend class IncludeApi<Target>;
friend class LibApi<Target>;
friend class PchApi<Target>;

private:
void Initialize();
Expand Down
2 changes: 1 addition & 1 deletion buildcc/lib/target/src/api/lib_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void LibApi<T>::AddLibDirAbsolute(const fs::path &absolute_lib_dir) {
t.storer_.current_lib_dirs.insert(absolute_lib_dir);
}

template <typename T> void LibApi<T>::AddLibDep(const T &lib_dep) {
template <typename T> void LibApi<T>::AddLibDep(const Target &lib_dep) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,35 @@
* limitations under the License.
*/

#include "target/api/pch_api.h"

#include "target/target.h"

namespace buildcc::base {

void Target::AddPchAbsolute(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));
template <typename T>
void PchApi<T>::AddPchAbsolute(const fs::path &absolute_filepath) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.config_.ExpectsValidHeader(absolute_filepath);

const fs::path absolute_pch = fs::path(absolute_filepath).make_preferred();
storer_.current_pch_files.user.insert(absolute_pch);
t.storer_.current_pch_files.user.insert(absolute_pch);
}

void Target::AddPch(const fs::path &relative_filename,
const fs::path &relative_to_target_path) {
env::log_trace(name_, __FUNCTION__);
template <typename T>
void PchApi<T>::AddPch(const fs::path &relative_filename,
const fs::path &relative_to_target_path) {
T &t = static_cast<T &>(*this);

// Compute the absolute source path
fs::path absolute_pch =
GetTargetRootDir() / relative_to_target_path / relative_filename;
t.env_.GetTargetRootDir() / relative_to_target_path / relative_filename;

AddPchAbsolute(absolute_pch);
}

} // namespace buildcc::base
template class PchApi<Target>;

} // namespace buildcc::base
5 changes: 2 additions & 3 deletions buildcc/lib/target/src/target/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ Check the `include/target/api` and `src/api` folder
- `include_api`
- [x] Lib and Lib Dir
- `lib_api`
- [ ] PCH
- [x] PCH
- `pch_api`
- [ ] Flags
- [ ] Rebuild Deps
- [ ] Getters
- [ ] Target Info

## Inputs to Target

- [x] `pch.cpp`
- Precompile Header files
- [x] `flags.cpp`
- PreprocessorFlags
- CommonCompileFlags
Expand Down