Skip to content

[CRTP] Target Lib API #151

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 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 @@ -10,6 +10,7 @@ add_library(mock_target STATIC
src/api/copy_api.cpp
src/api/source_api.cpp
src/api/include_api.cpp
src/api/lib_api.cpp

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

src/target/pch.cpp
src/target/lib.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 @@ -21,9 +21,11 @@ set(TARGET_SRCS
src/api/copy_api.cpp
src/api/source_api.cpp
src/api/include_api.cpp
src/api/lib_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

# Generator
src/generator/generator_loader.cpp
Expand Down Expand Up @@ -51,7 +53,6 @@ set(TARGET_SRCS
include/target/target.h

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

Expand Down
5 changes: 5 additions & 0 deletions buildcc/lib/target/include/target/api/include_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ namespace fs = std::filesystem;

namespace buildcc::base {

// Requires
// - TargetStorer
// - TargetState
// - TargetConfig
// - TargetEnv
template <typename T> class IncludeApi {
public:
void AddHeader(const fs::path &relative_filename,
Expand Down
43 changes: 43 additions & 0 deletions buildcc/lib/target/include/target/api/lib_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_LIB_API_H_
#define TARGET_API_LIB_API_H_

#include <filesystem>
#include <string>

namespace fs = std::filesystem;

namespace buildcc::base {

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

void AddLibDir(const fs::path &relative_lib_dir);
void AddLibDirAbsolute(const fs::path &absolute_lib_dir);
};

} // namespace buildcc::base

#endif
13 changes: 4 additions & 9 deletions buildcc/lib/target/include/target/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// API
#include "target/api/copy_api.h"
#include "target/api/include_api.h"
#include "target/api/lib_api.h"
#include "target/api/source_api.h"

// Friend
Expand Down Expand Up @@ -69,7 +70,8 @@ namespace buildcc::base {
class Target : public BuilderInterface,
public CopyApi<Target>,
public SourceApi<Target>,
public IncludeApi<Target> {
public IncludeApi<Target>,
public LibApi<Target> {

public:
explicit Target(const std::string &name, TargetType type,
Expand All @@ -96,14 +98,6 @@ class Target : public BuilderInterface,
const fs::path &relative_to_target_path = "");
void AddPchAbsolute(const fs::path &absolute_filepath);

// * Include and Lib directory
void AddLibDir(const fs::path &relative_lib_dir);
void AddLibDirAbsolute(const fs::path &absolute_lib_dir);

// * Libraries
void AddLibDep(const Target &lib_dep);
void AddLibDep(const std::string &lib_dep);

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

private:
void Initialize();
Expand Down
55 changes: 55 additions & 0 deletions buildcc/lib/target/src/api/lib_api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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/lib_api.h"

#include "target/target.h"

namespace buildcc::base {

template <typename T>
void LibApi<T>::AddLibDir(const fs::path &relative_lib_dir) {
T &t = static_cast<T &>(*this);

fs::path final_lib_dir = t.env_.GetTargetRootDir() / relative_lib_dir;
AddLibDirAbsolute(final_lib_dir);
}

template <typename T>
void LibApi<T>::AddLibDirAbsolute(const fs::path &absolute_lib_dir) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.storer_.current_lib_dirs.insert(absolute_lib_dir);
}

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

t.state_.ExpectsUnlock();
t.storer_.current_lib_deps.user.insert(lib_dep.GetTargetPath());
}

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

t.state_.ExpectsUnlock();
t.storer_.current_external_lib_deps.insert(lib_dep);
}

template class LibApi<Target>;

} // namespace buildcc::base
7 changes: 2 additions & 5 deletions buildcc/lib/target/src/target/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Check the `include/target/api` and `src/api` folder
- `source_api`
- [x] Header and Include Dir
- `include_api`
- [ ] Lib and Lib Dir
- [x] Lib and Lib Dir
- `lib_api`
- [ ] PCH
- [ ] Flags
- [ ] Rebuild Deps
Expand All @@ -33,10 +34,6 @@ Check the `include/target/api` and `src/api` folder

- [x] `pch.cpp`
- Precompile Header files
- [x] `lib.cpp`
- Lib Dir
- Lib File (full path Target supplied)
- External Lib File (relative link using -l)
- [x] `flags.cpp`
- PreprocessorFlags
- CommonCompileFlags
Expand Down
53 changes: 0 additions & 53 deletions buildcc/lib/target/src/target/lib.cpp

This file was deleted.