Skip to content

Commit ca1d8ae

Browse files
authored
Generator task (#164)
1 parent 905cb5c commit ca1d8ae

File tree

14 files changed

+295
-70
lines changed

14 files changed

+295
-70
lines changed

buildcc/lib/env/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ set(ENV_SRCS
3737
src/assert_fatal.cpp
3838
src/logging.cpp
3939
include/env/assert_fatal.h
40+
include/env/assert_throw.h
4041
include/env/env.h
4142
include/env/logging.h
4243
include/env/host_os.h
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2021 Niket Naidu. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef ENV_ASSERT_THROW_H_
18+
#define ENV_ASSERT_THROW_H_
19+
20+
#include <string>
21+
22+
#include "logging.h"
23+
24+
namespace buildcc::env {
25+
26+
/**
27+
* @brief Compile time expr asserts fatally when false
28+
*/
29+
template <bool expr> inline void assert_throw(const char *message) {
30+
if constexpr (!expr) {
31+
env::log_critical("assert", message);
32+
// TODO, If needed specialize this
33+
throw std::exception();
34+
}
35+
}
36+
37+
/**
38+
* @brief Compile time expr asserts fatally when false
39+
*/
40+
template <bool expr> inline void assert_throw(const std::string &message) {
41+
assert_throw<expr>(message.c_str());
42+
}
43+
44+
/**
45+
* @brief Runtime expr asserts fatally when false
46+
*/
47+
inline void assert_throw(bool expression, const char *message) {
48+
if (!expression) {
49+
assert_throw<false>(message);
50+
}
51+
}
52+
53+
/**
54+
* @brief Runtime expr asserts fatally when false
55+
*/
56+
inline void assert_throw(bool expression, const std::string &message) {
57+
assert_throw(expression, message.c_str());
58+
}
59+
60+
} // namespace buildcc::env
61+
62+
/**
63+
* @brief Runtime expr assert throws when false
64+
*/
65+
#define ASSERT_THROW(expr, message) \
66+
((expr) ? static_cast<void>(0) : buildcc::env::assert_throw<false>(message))
67+
68+
#endif

buildcc/lib/env/include/env/task_state.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef ENV_PRIVATE_TASK_STATE_H_
18-
#define ENV_PRIVATE_TASK_STATE_H_
17+
#ifndef ENV_TASK_STATE_H_
18+
#define ENV_TASK_STATE_H_
1919

2020
namespace buildcc::env {
2121

buildcc/lib/env/src/task_state.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ buildcc::env::TaskState current_state{buildcc::env::TaskState::SUCCESS};
2828
namespace buildcc::env {
2929

3030
void set_task_state(TaskState state) {
31+
// NOTE, `Avoid resetting` if same state is provided
32+
if (state == get_task_state()) {
33+
return;
34+
}
3135
std::lock_guard<std::mutex> guard(current_state_mutex);
3236
current_state = state;
3337
}

buildcc/lib/target/cmake/mock_target.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ add_library(mock_target STATIC
22
${COMMON_TARGET_SRCS}
33

44
# Generator mocks
5-
mock/generator/task.cpp
5+
src/generator/task.cpp
66
mock/generator/runner.cpp
77
mock/generator/recheck_states.cpp
88

buildcc/lib/target/include/target/base/generator_loader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ class GeneratorLoader : public LoaderInterface {
4141
bool Load() override;
4242

4343
// Getters
44-
const internal::path_unordered_set &GetLoadedInputFiles() const {
44+
const internal::path_unordered_set &GetLoadedInputFiles() const noexcept {
4545
return loaded_input_files_;
4646
}
4747

48-
const internal::fs_unordered_set &GetLoadedOutputFiles() const {
48+
const internal::fs_unordered_set &GetLoadedOutputFiles() const noexcept {
4949
return loaded_output_files_;
5050
}
5151

52-
const std::vector<std::string> &GetLoadedCommands() const {
52+
const std::vector<std::string> &GetLoadedCommands() const noexcept {
5353
return loaded_commands_;
5454
}
5555

buildcc/lib/target/include/target/common/path.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <unordered_set>
2525

2626
// Env
27-
#include "env/assert_fatal.h"
27+
#include "env/assert_throw.h"
2828

2929
// Third party
3030
#include "fmt/format.h"
@@ -43,13 +43,14 @@ class Path {
4343
* @param pathname
4444
* @return Path
4545
*/
46+
// TODO, Discuss if we should return `std::optional` instead of asserting
4647
static Path CreateExistingPath(const fs::path &pathname) {
4748
std::error_code errcode;
4849
uint64_t last_write_timestamp =
4950
std::filesystem::last_write_time(pathname, errcode)
5051
.time_since_epoch()
5152
.count();
52-
env::assert_fatal(errcode.value() == 0,
53+
env::assert_throw(errcode.value() == 0,
5354
fmt::format("{} not found", pathname));
5455

5556
return Path(pathname, last_write_timestamp);
@@ -66,13 +67,15 @@ class Path {
6667
* @param pathname
6768
* @return Path
6869
*/
69-
static Path CreateNewPath(const fs::path &pathname) noexcept {
70+
static Path CreateNewPath(const fs::path &pathname) {
7071
return Path(pathname, 0);
7172
}
7273

7374
// Getters
74-
std::uint64_t GetLastWriteTimestamp() const { return last_write_timestamp_; }
75-
const fs::path &GetPathname() const { return pathname_; }
75+
std::uint64_t GetLastWriteTimestamp() const noexcept {
76+
return last_write_timestamp_;
77+
}
78+
const fs::path &GetPathname() const noexcept { return pathname_; }
7679

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

166+
/**
167+
* @brief Convert from fs_unordered_set to path_unordered_set
168+
* Can assert throw if file does not exist when calling `CreateExistingPath`
169+
*/
163170
void Convert() {
164171
if (done_once) {
165172
return;

buildcc/lib/target/include/target/generator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
#define TARGET_GENERATOR_H_
1919

2020
#include <functional>
21+
#include <mutex>
2122
#include <string>
2223
#include <unordered_map>
2324
#include <vector>
2425

2526
#include "taskflow/taskflow.hpp"
2627

2728
#include "env/env.h"
29+
#include "env/task_state.h"
2830

2931
#include "command/command.h"
3032

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

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

101104
const std::string &
102105
GetValueByIdentifier(const std::string &file_identifier) const;
@@ -132,6 +135,8 @@ class Generator : public BuilderInterface {
132135
bool parallel_{false};
133136

134137
// Internal
138+
std::mutex task_state_mutex_;
139+
env::TaskState task_state_{env::TaskState::SUCCESS};
135140
Command command_;
136141
tf::Taskflow tf_;
137142
};

buildcc/lib/target/include/target/interface/builder_interface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <functional>
2222
#include <unordered_set>
2323

24+
#include "env/assert_fatal.h"
25+
2426
#include "target/common/path.h"
2527
#include "target/common/util.h"
2628

buildcc/lib/target/include/target/interface/loader_interface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class LoaderInterface {
2828
virtual bool Load() = 0;
2929

3030
const fs::path &GetBinaryPath() const { return binary_path_; };
31-
bool IsLoaded() const { return loaded_; };
31+
bool IsLoaded() const noexcept { return loaded_; };
3232

3333
protected:
3434
bool loaded_{false};

buildcc/lib/target/mock/generator/task.cpp

Lines changed: 0 additions & 36 deletions
This file was deleted.

buildcc/lib/target/src/api/sync_api.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include "target/target_info.h"
2020

21+
#include "env/assert_fatal.h"
22+
2123
namespace buildcc::base {
2224

2325
template <typename T>

0 commit comments

Comments
 (0)