Skip to content

Commit dd3f1f4

Browse files
authored
Generator (#98)
One stage generator for one or more inputs generating one or more outputs using custom commands - Complete rework to Targets - Interfaces design for common functionality between Generator and Target
1 parent 6395133 commit dd3f1f4

33 files changed

+1431
-259
lines changed

buildcc/buildcc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
#include "env/assert_fatal.h"
2525
#include "env/logging.h"
2626
#include "env/host_os.h"
27+
#include "env/host_compiler.h"
2728
#include "env/util.h"
2829

30+
//
31+
#include "command/command.h"
32+
2933
// Base
3034
#include "toolchain/toolchain.h"
3135
#include "target/target.h"

buildcc/lib/target/cmake/mock_target.cmake

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
add_library(mock_target STATIC
2+
# Utils
3+
src/util/util.cpp
4+
5+
# Generator
6+
src/generator/generator_loader.cpp
7+
src/generator/generator_storer.cpp
8+
src/generator/generator.cpp
9+
10+
# Generator mocks
11+
mock/generator/task.cpp
12+
mock/generator/recheck_states.cpp
13+
14+
# Target
15+
src/target/target_loader.cpp
16+
src/target/target_storer.cpp
217
src/target/target.cpp
318
src/target/source.cpp
419
src/target/include_dir.cpp
520
src/target/lib.cpp
621
src/target/build.cpp
722
src/target/flags.cpp
23+
24+
# Target mocks
825
mock/target/recheck_states.cpp
926
mock/target/tasks.cpp
10-
11-
src/fbs/fbs_loader.cpp
12-
src/fbs/fbs_storer.cpp
13-
14-
src/util/util.cpp
1527
)
1628
target_include_directories(mock_target PUBLIC
1729
${CMAKE_CURRENT_SOURCE_DIR}/include

buildcc/lib/target/cmake/target.cmake

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
set(TARGET_SRCS
2+
# Interfaces
3+
include/target/loader_interface.h
4+
include/target/builder_interface.h
5+
6+
# Utils
7+
src/util/util.cpp
8+
include/target/path.h
9+
include/target/util.h
10+
11+
# Generator
12+
src/generator/generator_loader.cpp
13+
src/generator/generator_storer.cpp
14+
src/generator/generator.cpp
15+
src/generator/task.cpp
16+
src/generator/recheck_states.cpp
17+
include/target/generator_loader.h
18+
include/target/generator.h
19+
20+
# Target
21+
src/target/target_loader.cpp
22+
src/target/target_storer.cpp
223
src/target/target.cpp
324
src/target/source.cpp
425
src/target/include_dir.cpp
526
src/target/lib.cpp
627
src/target/build.cpp
728
src/target/flags.cpp
8-
929
src/target/recheck_states.cpp
1030
src/target/tasks.cpp
11-
12-
src/fbs/fbs_loader.cpp
13-
src/fbs/fbs_storer.cpp
14-
15-
src/util/util.cpp
16-
31+
include/target/target_loader.h
1732
include/target/target.h
18-
include/target/fbs_loader.h
19-
include/target/path.h
20-
include/target/util.h
2133
)
2234

2335
if(${BUILDCC_BUILD_AS_SINGLE_LIB})
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 TARGET_BUILDER_INTERFACE_H_
18+
#define TARGET_BUILDER_INTERFACE_H_
19+
20+
#include <unordered_set>
21+
22+
#include "target/path.h"
23+
#include "target/util.h"
24+
25+
namespace buildcc::base {
26+
27+
class BuilderInterface {
28+
29+
public:
30+
virtual void Build() = 0;
31+
32+
protected:
33+
template <typename T>
34+
void RecheckChanged(
35+
const T &previous, const T &current,
36+
const std::function<void(void)> &callback = []() {}) {
37+
if (dirty_) {
38+
return;
39+
}
40+
41+
if (previous != current) {
42+
callback();
43+
dirty_ = true;
44+
}
45+
}
46+
47+
void RecheckPaths(const internal::path_unordered_set &previous_path,
48+
const internal::path_unordered_set &current_path,
49+
const std::function<void(void)> &path_removed_cb,
50+
const std::function<void(void)> &path_added_cb,
51+
const std::function<void(void)> &path_updated_cb) {
52+
if (dirty_) {
53+
return;
54+
}
55+
56+
// * Old path is removed
57+
const bool removed =
58+
internal::is_previous_paths_different(previous_path, current_path);
59+
if (removed) {
60+
path_removed_cb();
61+
dirty_ = true;
62+
return;
63+
}
64+
65+
for (const auto &path : current_path) {
66+
auto iter = previous_path.find(path);
67+
68+
if (iter == previous_path.end()) {
69+
// * New path added
70+
path_added_cb();
71+
dirty_ = true;
72+
} else {
73+
// * Path is updated
74+
if (path.GetLastWriteTimestamp() > iter->GetLastWriteTimestamp()) {
75+
path_updated_cb();
76+
dirty_ = true;
77+
} else {
78+
// * Do nothing
79+
}
80+
}
81+
82+
if (dirty_) {
83+
break;
84+
}
85+
}
86+
}
87+
88+
private:
89+
virtual bool Store() = 0;
90+
91+
protected:
92+
bool dirty_{false};
93+
};
94+
95+
} // namespace buildcc::base
96+
97+
#endif
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 TARGET_GENERATOR_H_
18+
#define TARGET_GENERATOR_H_
19+
20+
#include <functional>
21+
#include <string>
22+
#include <vector>
23+
24+
#include "taskflow/taskflow.hpp"
25+
26+
#include "env/env.h"
27+
28+
#include "target/builder_interface.h"
29+
30+
#include "target/generator_loader.h"
31+
#include "target/path.h"
32+
33+
namespace buildcc::base {
34+
35+
struct UserGenInfo {
36+
std::string name;
37+
internal::fs_unordered_set inputs;
38+
internal::fs_unordered_set outputs;
39+
std::vector<std::string> commands;
40+
bool parallel{false};
41+
42+
explicit UserGenInfo(const std::string &n,
43+
const internal::fs_unordered_set &i,
44+
const internal::fs_unordered_set &o,
45+
const std::vector<std::string> &c, bool p)
46+
: name(n), inputs(i), outputs(o), commands(c), parallel(p) {}
47+
};
48+
49+
class Generator : public BuilderInterface {
50+
public:
51+
Generator(const std::string &name, const fs::path &path)
52+
: loader_(name, path) {}
53+
Generator(const Generator &generator) = delete;
54+
55+
void AddGenInfo(const UserGenInfo &info);
56+
void Build() override;
57+
58+
// Getter
59+
fs::path GetBinaryPath() const { return loader_.GetBinaryPath(); }
60+
tf::Taskflow &GetTaskflow() { return tf_; }
61+
62+
private:
63+
void GenerateTask();
64+
// Convert from UserGenInfo to internal::GenInfo
65+
void Convert();
66+
std::vector<const internal::GenInfo *> BuildGenerate();
67+
bool Regenerate(std::vector<const internal::GenInfo *> &generated_files);
68+
69+
bool Store() override;
70+
71+
// Recheck states
72+
void InputRemoved();
73+
void InputAdded();
74+
void InputUpdated();
75+
76+
void OutputChanged();
77+
void CommandChanged();
78+
79+
private:
80+
std::string name_;
81+
std::unordered_map<std::string, UserGenInfo> user_info_;
82+
std::unordered_map<std::string, internal::GenInfo> current_info_;
83+
84+
internal::GeneratorLoader loader_;
85+
86+
tf::Taskflow tf_;
87+
tf::Task build_task_;
88+
};
89+
90+
} // namespace buildcc::base
91+
92+
#endif
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 TARGET_GENERATOR_LOADER_H_
18+
#define TARGET_GENERATOR_LOADER_H_
19+
20+
#include "target/loader_interface.h"
21+
22+
#include <string>
23+
#include <unordered_map>
24+
#include <unordered_set>
25+
26+
#include "fmt/format.h"
27+
28+
#include "target/path.h"
29+
30+
namespace buildcc::internal {
31+
32+
struct GenInfo {
33+
std::string name;
34+
path_unordered_set inputs;
35+
fs_unordered_set outputs;
36+
std::vector<std::string> commands;
37+
bool parallel{false};
38+
39+
explicit GenInfo(const std::string &n, const path_unordered_set &i,
40+
const fs_unordered_set &o, const std::vector<std::string> &c,
41+
bool p)
42+
: name(n), inputs(i), outputs(o), commands(c), parallel(p) {}
43+
};
44+
45+
typedef std::unordered_map<std::string, GenInfo> geninfo_unordered_map;
46+
47+
class GeneratorLoader : public LoaderInterface {
48+
public:
49+
GeneratorLoader(const std::string &name, const fs::path &path)
50+
: name_(name), path_(path) {}
51+
52+
GeneratorLoader(const GeneratorLoader &loader) = delete;
53+
54+
bool Load() override;
55+
56+
// Getters
57+
fs::path GetBinaryPath() const override {
58+
return path_ / fmt::format("{}.bin", name_);
59+
}
60+
61+
const geninfo_unordered_map &GetLoadedInfo() { return loaded_info_; }
62+
63+
private:
64+
std::string name_;
65+
fs::path path_;
66+
67+
geninfo_unordered_map loaded_info_;
68+
};
69+
70+
} // namespace buildcc::internal
71+
72+
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 TARGET_LOADER_INTERFACE_H_
18+
#define TARGET_LOADER_INTERFACE_H_
19+
20+
#include <filesystem>
21+
22+
namespace fs = std::filesystem;
23+
24+
namespace buildcc::internal {
25+
26+
class LoaderInterface {
27+
public:
28+
virtual bool Load() = 0;
29+
virtual fs::path GetBinaryPath() const = 0;
30+
31+
bool IsLoaded() const { return loaded_; };
32+
33+
protected:
34+
bool loaded_{false};
35+
};
36+
37+
} // namespace buildcc::internal
38+
39+
#endif

0 commit comments

Comments
 (0)