Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Function lock segregation with Target state #196

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 7 commits into from
Feb 22, 2022
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
48 changes: 48 additions & 0 deletions buildcc/lib/target/include/target/common/function_lock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021-2022 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_COMMON_FUNCTION_LOCK_H_
#define TARGET_COMMON_FUNCTION_LOCK_H_

#include <string_view>

#include "fmt/format.h"

#include "env/assert_fatal.h"

namespace buildcc {

class FunctionLock {
public:
void Lock() { lock_ = true; }
void Unlock() { lock_ = false; }
bool IsLocked() const { return lock_; }
void ExpectsUnlock(std::string_view tag) const {
env::assert_fatal(!lock_,
fmt::format("Cannot use {} when lock == true", tag));
}
void ExpectsLock(std::string_view tag) const {
env::assert_fatal(lock_,
fmt::format("Cannot use {} when lock == false", tag));
}

private:
bool lock_{false};
};

} // namespace buildcc

#endif
31 changes: 12 additions & 19 deletions buildcc/lib/target/include/target/common/target_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,24 @@

namespace buildcc {

// 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(FileExt file_extension);
void SetPch();
void SetLock();

void ExpectsUnlock() const;
void ExpectsLock() const;
// TODO, IsLocked
void BuildCompleted();
void SourceDetected(FileExt file_extension);
void PchDetected();

bool IsBuilt() const { return build_; }
bool ContainsPch() const { return contains_pch_; }

// TODO, Make these private getters
bool contains_asm{false};
bool contains_c{false};
bool contains_cpp{false};
bool build{false};
bool lock{false};
bool ContainsAsm() const { return contains_asm_; }
bool ContainsC() const { return contains_c_; }
bool ContainsCpp() const { return contains_cpp_; }

private:
bool build_{false};

bool contains_pch_{false};
bool contains_asm_{false};
bool contains_c_{false};
bool contains_cpp_{false};
};

} // namespace buildcc
Expand Down
3 changes: 0 additions & 3 deletions buildcc/lib/target/include/target/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ class Target : public internal::BuilderInterface,
env.GetTargetBuildDir() / toolchain.GetName() / name),
config),
name_(name), type_(type),
// toolchain_(toolchain),
// loader_(name, env_.GetTargetBuildDir()),
serialization_(env_.GetTargetBuildDir() / fmt::format("{}.bin", name)),
compile_pch_(*this), compile_object_(*this), link_target_(*this) {
Initialize();
Expand Down Expand Up @@ -141,7 +139,6 @@ class Target : public internal::BuilderInterface,
private:
std::string name_;
TargetType type_;
// const Toolchain &toolchain_;
internal::TargetSerialization serialization_;

// Friend classes
Expand Down
2 changes: 2 additions & 0 deletions buildcc/lib/target/include/target/target_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "toolchain/toolchain.h"

#include "target/common/function_lock.h"
#include "target/common/target_config.h"
#include "target/common/target_env.h"
#include "target/common/target_state.h"
Expand Down Expand Up @@ -88,6 +89,7 @@ class TargetInfo : public internal::SourceApi<TargetInfo>,

UserTargetSchema user_;

FunctionLock lock_;
TargetState state_;
};

Expand Down
4 changes: 2 additions & 2 deletions buildcc/lib/target/src/api/deps_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ template <typename T>
void DepsApi<T>::AddCompileDependencyAbsolute(const fs::path &absolute_path) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.compile_dependencies.insert(absolute_path);
}
template <typename T>
void DepsApi<T>::AddLinkDependencyAbsolute(const fs::path &absolute_path) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.link_dependencies.insert(absolute_path);
}

Expand Down
16 changes: 8 additions & 8 deletions buildcc/lib/target/src/api/flag_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,55 @@ template <typename T>
void FlagApi<T>::AddPreprocessorFlag(const std::string &flag) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.preprocessor_flags.insert(flag);
}
template <typename T>
void FlagApi<T>::AddCommonCompileFlag(const std::string &flag) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.common_compile_flags.insert(flag);
}
template <typename T>
void FlagApi<T>::AddPchCompileFlag(const std::string &flag) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.pch_compile_flags.insert(flag);
}
template <typename T>
void FlagApi<T>::AddPchObjectFlag(const std::string &flag) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.pch_object_flags.insert(flag);
}
template <typename T>
void FlagApi<T>::AddAsmCompileFlag(const std::string &flag) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.asm_compile_flags.insert(flag);
}
template <typename T>
void FlagApi<T>::AddCCompileFlag(const std::string &flag) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.c_compile_flags.insert(flag);
}
template <typename T>
void FlagApi<T>::AddCppCompileFlag(const std::string &flag) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.cpp_compile_flags.insert(flag);
}
template <typename T> void FlagApi<T>::AddLinkFlag(const std::string &flag) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.link_flags.insert(flag);
}

Expand Down
4 changes: 2 additions & 2 deletions buildcc/lib/target/src/api/include_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ template <typename T>
void IncludeApi<T>::AddHeaderAbsolute(const fs::path &absolute_filepath) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.toolchain_.GetConfig().ExpectsValidHeader(absolute_filepath);
t.user_.headers.insert(absolute_filepath);
}
Expand Down Expand Up @@ -74,7 +74,7 @@ void IncludeApi<T>::AddIncludeDirAbsolute(const fs::path &absolute_include_dir,
bool glob_headers) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.include_dirs.insert(absolute_include_dir);

if (glob_headers) {
Expand Down
6 changes: 3 additions & 3 deletions buildcc/lib/target/src/api/lib_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ template <typename T>
void LibApi<T>::AddLibDirAbsolute(const fs::path &absolute_lib_dir) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.lib_dirs.insert(absolute_lib_dir);
}

Expand All @@ -40,14 +40,14 @@ void LibApi<T>::AddLibDir(const fs::path &relative_lib_dir) {
template <typename T> void LibApi<T>::AddLibDep(const BaseTarget &lib_dep) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.libs.push_back(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.lock_.ExpectsUnlock(__FUNCTION__);
t.user_.external_libs.push_back(lib_dep);
}

Expand Down
2 changes: 1 addition & 1 deletion buildcc/lib/target/src/api/pch_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ template <typename T>
void PchApi<T>::AddPchAbsolute(const fs::path &absolute_filepath) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.toolchain_.GetConfig().ExpectsValidHeader(absolute_filepath);

const fs::path absolute_pch = fs::path(absolute_filepath).make_preferred();
Expand Down
2 changes: 1 addition & 1 deletion buildcc/lib/target/src/api/source_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ template <typename T>
void SourceApi<T>::AddSourceAbsolute(const fs::path &absolute_source) {
T &t = static_cast<T &>(*this);

t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
t.toolchain_.GetConfig().ExpectsValidSource(absolute_source);
t.user_.sources.emplace(fs::path(absolute_source).make_preferred());
}
Expand Down
4 changes: 2 additions & 2 deletions buildcc/lib/target/src/api/sync_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ template <typename TargetType>
void SyncApi<T>::SpecializedCopy(TargetType target,
std::initializer_list<SyncOption> options) {
T &t = static_cast<T &>(*this);
t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
for (const SyncOption o : options) {
switch (o) {
case SyncOption::PreprocessorFlags:
Expand Down Expand Up @@ -122,7 +122,7 @@ template <typename TargetType>
void SyncApi<T>::SpecializedInsert(TargetType target,
std::initializer_list<SyncOption> options) {
T &t = static_cast<T &>(*this);
t.state_.ExpectsUnlock();
t.lock_.ExpectsUnlock(__FUNCTION__);
for (const SyncOption o : options) {
switch (o) {
case SyncOption::PreprocessorFlags:
Expand Down
6 changes: 3 additions & 3 deletions buildcc/lib/target/src/api/target_getter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,22 @@ const std::string &
TargetGetter<T>::GetCompileCommand(const fs::path &source) const {
const T &t = static_cast<const T &>(*this);

t.state_.ExpectsLock();
t.lock_.ExpectsLock(__FUNCTION__);
return t.compile_object_.GetObjectData(source).command;
}

template <typename T>
const std::string &TargetGetter<T>::GetLinkCommand() const {
const T &t = static_cast<const T &>(*this);

t.state_.ExpectsLock();
t.lock_.ExpectsLock(__FUNCTION__);
return t.link_target_.GetCommand();
}

template <typename T> tf::Taskflow &TargetGetter<T>::GetTaskflow() {
T &t = static_cast<T &>(*this);

t.state_.ExpectsLock();
t.lock_.ExpectsLock(__FUNCTION__);
return t.tf_;
}

Expand Down
4 changes: 2 additions & 2 deletions buildcc/lib/target/src/api/target_info_getter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ template <typename T> const TargetState &TargetInfoGetter<T>::GetState() const {
template <typename T> bool TargetInfoGetter<T>::IsBuilt() const {
const T &t = static_cast<const T &>(*this);

return t.state_.build;
return t.state_.IsBuilt();
}

template <typename T> bool TargetInfoGetter<T>::IsLocked() const {
const T &t = static_cast<const T &>(*this);

return t.state_.lock;
return t.lock_.IsLocked();
}

// Target Env
Expand Down
22 changes: 7 additions & 15 deletions buildcc/lib/target/src/common/target_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@

namespace buildcc {

void TargetState::SetSourceState(FileExt file_extension) {
void TargetState::BuildCompleted() { build_ = true; }

void TargetState::SourceDetected(FileExt file_extension) {
switch (file_extension) {
case FileExt::Asm:
contains_asm = true;
contains_asm_ = true;
break;
case FileExt::C:
contains_c = true;
contains_c_ = true;
break;
case FileExt::Cpp:
contains_cpp = true;
contains_cpp_ = true;
break;
case FileExt::Header:
case FileExt::Invalid:
Expand All @@ -38,16 +40,6 @@ void TargetState::SetSourceState(FileExt file_extension) {
}
}

void TargetState::SetPch() { contains_pch_ = true; }

void TargetState::SetLock() { lock = true; }

void TargetState::ExpectsUnlock() const {
env::assert_fatal(!lock, "Cannot use this function when lock == true");
}

void TargetState::ExpectsLock() const {
env::assert_fatal(lock, "Cannot use this function when lock == false");
}
void TargetState::PchDetected() { contains_pch_ = true; }

} // namespace buildcc
8 changes: 4 additions & 4 deletions buildcc/lib/target/src/target/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ namespace buildcc {
void Target::Build() {
env::log_trace(name_, __FUNCTION__);

state_.ExpectsUnlock();
state_.SetLock();
lock_.ExpectsUnlock(__FUNCTION__);
lock_.Lock();

// PCH state
if (!user_.pchs.empty()) {
state_.SetPch();
state_.PchDetected();
}

// Source - Object relation
// Source state
for (const auto &abs_source : user_.sources) {
// Set state
state_.SetSourceState(toolchain_.GetConfig().GetFileExt(abs_source));
state_.SourceDetected(toolchain_.GetConfig().GetFileExt(abs_source));

// Relate input source with output object
compile_object_.AddObjectData(abs_source);
Expand Down
Loading