Skip to content

Generator task #164

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 28 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions buildcc/lib/env/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ set(ENV_SRCS
src/assert_fatal.cpp
src/logging.cpp
include/env/assert_fatal.h
include/env/assert_throw.h
include/env/env.h
include/env/logging.h
include/env/host_os.h
Expand Down
68 changes: 68 additions & 0 deletions buildcc/lib/env/include/env/assert_throw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 ENV_ASSERT_THROW_H_
#define ENV_ASSERT_THROW_H_

#include <string>

#include "logging.h"

namespace buildcc::env {

/**
* @brief Compile time expr asserts fatally when false
*/
template <bool expr> inline void assert_throw(const char *message) {
if constexpr (!expr) {
env::log_critical("assert", message);
// TODO, If needed specialize this
throw std::exception();
}
}

/**
* @brief Compile time expr asserts fatally when false
*/
template <bool expr> inline void assert_throw(const std::string &message) {
assert_throw<expr>(message.c_str());
}

/**
* @brief Runtime expr asserts fatally when false
*/
inline void assert_throw(bool expression, const char *message) {
if (!expression) {
assert_throw<false>(message);
}
}

/**
* @brief Runtime expr asserts fatally when false
*/
inline void assert_throw(bool expression, const std::string &message) {
assert_throw(expression, message.c_str());
}

} // namespace buildcc::env

/**
* @brief Runtime expr assert throws when false
*/
#define ASSERT_THROW(expr, message) \
((expr) ? static_cast<void>(0) : buildcc::env::assert_throw<false>(message))

#endif
4 changes: 2 additions & 2 deletions buildcc/lib/env/include/env/task_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

#ifndef ENV_PRIVATE_TASK_STATE_H_
#define ENV_PRIVATE_TASK_STATE_H_
#ifndef ENV_TASK_STATE_H_
#define ENV_TASK_STATE_H_

namespace buildcc::env {

Expand Down
4 changes: 4 additions & 0 deletions buildcc/lib/env/src/task_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ buildcc::env::TaskState current_state{buildcc::env::TaskState::SUCCESS};
namespace buildcc::env {

void set_task_state(TaskState state) {
// NOTE, `Avoid resetting` if same state is provided
if (state == get_task_state()) {
return;
}
std::lock_guard<std::mutex> guard(current_state_mutex);
current_state = state;
}
Expand Down
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 @@ -2,7 +2,7 @@ add_library(mock_target STATIC
${COMMON_TARGET_SRCS}

# Generator mocks
mock/generator/task.cpp
src/generator/task.cpp
mock/generator/runner.cpp
mock/generator/recheck_states.cpp

Expand Down
6 changes: 3 additions & 3 deletions buildcc/lib/target/include/target/base/generator_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class GeneratorLoader : public LoaderInterface {
bool Load() override;

// Getters
const internal::path_unordered_set &GetLoadedInputFiles() const {
const internal::path_unordered_set &GetLoadedInputFiles() const noexcept {
return loaded_input_files_;
}

const internal::fs_unordered_set &GetLoadedOutputFiles() const {
const internal::fs_unordered_set &GetLoadedOutputFiles() const noexcept {
return loaded_output_files_;
}

const std::vector<std::string> &GetLoadedCommands() const {
const std::vector<std::string> &GetLoadedCommands() const noexcept {
return loaded_commands_;
}

Expand Down
17 changes: 12 additions & 5 deletions buildcc/lib/target/include/target/common/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <unordered_set>

// Env
#include "env/assert_fatal.h"
#include "env/assert_throw.h"

// Third party
#include "fmt/format.h"
Expand All @@ -43,13 +43,14 @@ class Path {
* @param pathname
* @return Path
*/
// TODO, Discuss if we should return `std::optional` instead of asserting
static Path CreateExistingPath(const fs::path &pathname) {
std::error_code errcode;
uint64_t last_write_timestamp =
std::filesystem::last_write_time(pathname, errcode)
.time_since_epoch()
.count();
env::assert_fatal(errcode.value() == 0,
env::assert_throw(errcode.value() == 0,
fmt::format("{} not found", pathname));

return Path(pathname, last_write_timestamp);
Expand All @@ -66,13 +67,15 @@ class Path {
* @param pathname
* @return Path
*/
static Path CreateNewPath(const fs::path &pathname) noexcept {
static Path CreateNewPath(const fs::path &pathname) {
return Path(pathname, 0);
}

// Getters
std::uint64_t GetLastWriteTimestamp() const { return last_write_timestamp_; }
const fs::path &GetPathname() const { return pathname_; }
std::uint64_t GetLastWriteTimestamp() const noexcept {
return last_write_timestamp_;
}
const fs::path &GetPathname() const noexcept { return pathname_; }

/**
* @brief Get fs::path as std::string while keeping the preferred os
Expand Down Expand Up @@ -160,6 +163,10 @@ struct RelationalPathFiles {
RelationalPathFiles(const path_unordered_set &i, const fs_unordered_set &u)
: internal(i), user(u) {}

/**
* @brief Convert from fs_unordered_set to path_unordered_set
* Can assert throw if file does not exist when calling `CreateExistingPath`
*/
void Convert() {
if (done_once) {
return;
Expand Down
5 changes: 5 additions & 0 deletions buildcc/lib/target/include/target/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
#define TARGET_GENERATOR_H_

#include <functional>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>

#include "taskflow/taskflow.hpp"

#include "env/env.h"
#include "env/task_state.h"

#include "command/command.h"

Expand Down Expand Up @@ -97,6 +99,7 @@ class Generator : public BuilderInterface {
tf::Taskflow &GetTaskflow() { return tf_; }

const std::string &GetName() { return name_; }
env::TaskState GetTaskState() { return task_state_; }

const std::string &
GetValueByIdentifier(const std::string &file_identifier) const;
Expand Down Expand Up @@ -132,6 +135,8 @@ class Generator : public BuilderInterface {
bool parallel_{false};

// Internal
std::mutex task_state_mutex_;
env::TaskState task_state_{env::TaskState::SUCCESS};
Command command_;
tf::Taskflow tf_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <functional>
#include <unordered_set>

#include "env/assert_fatal.h"

#include "target/common/path.h"
#include "target/common/util.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class LoaderInterface {
virtual bool Load() = 0;

const fs::path &GetBinaryPath() const { return binary_path_; };
bool IsLoaded() const { return loaded_; };
bool IsLoaded() const noexcept { return loaded_; };

protected:
bool loaded_{false};
Expand Down
36 changes: 0 additions & 36 deletions buildcc/lib/target/mock/generator/task.cpp

This file was deleted.

2 changes: 2 additions & 0 deletions buildcc/lib/target/src/api/sync_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "target/target_info.h"

#include "env/assert_fatal.h"

namespace buildcc::base {

template <typename T>
Expand Down
Loading