Skip to content

Commit 70ca258

Browse files
authored
Refactor env.h free functions to static Project class (#201)
1 parent a3d2c7f commit 70ca258

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+277
-179
lines changed

bootstrap/main.buildcc.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ int main(int argc, char **argv) {
4444
BaseToolchain toolchain = custom_toolchain_arg.ConstructToolchain();
4545

4646
BuildBuildCC buildcc(
47-
reg, toolchain,
48-
TargetEnv(env::get_project_root_dir(), env::get_project_build_dir()));
47+
reg, toolchain, TargetEnv(Project::GetRootDir(), Project::GetBuildDir()));
4948
buildcc.Setup(custom_toolchain_arg.state);
5049

5150
const auto &buildcc_lib = buildcc.GetBuildcc();

buildcc/lib/args/src/register.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ void Register::BuildStoreTask(const std::string &unique_id,
119119
void Register::Initialize() { Env(); }
120120

121121
void Register::Env() {
122-
env::init(fs::current_path() / args_.GetProjectRootDir(),
123-
fs::current_path() / args_.GetProjectBuildDir());
122+
Project::Init(fs::current_path() / args_.GetProjectRootDir(),
123+
fs::current_path() / args_.GetProjectBuildDir());
124124
env::set_log_level(args_.GetLogLevel());
125125
}
126126

buildcc/lib/args/test/test_persistent_storage.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ TEST(PersistentStorageTestGroup, NullptrDelete) {
5555
}
5656

5757
int main(int ac, char **av) {
58-
buildcc::env::init(fs::current_path(), fs::current_path());
58+
buildcc::Project::Init(fs::current_path(), fs::current_path());
5959
return CommandLineTestRunner::RunAllTests(ac, av);
6060
}

buildcc/lib/args/test/test_register.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ TEST(RegisterTestGroup, Register_Build) {
9999
CHECK_TRUE(args.Clean());
100100

101101
// Make dummy toolchain and target
102-
buildcc::env::init(fs::current_path(), fs::current_path());
102+
buildcc::Project::Init(fs::current_path(), fs::current_path());
103103
buildcc::Toolchain toolchain(buildcc::ToolchainId::Gcc, "", "", "", "", "",
104104
"");
105105
buildcc::BaseTarget target("dummyT", buildcc::TargetType::Executable,
@@ -122,7 +122,7 @@ TEST(RegisterTestGroup, Register_Build) {
122122
state, [](buildcc::BaseTarget &target) { (void)target; }, target);
123123
}
124124

125-
buildcc::env::deinit();
125+
buildcc::Project::Deinit();
126126
mock().checkExpectations();
127127
}
128128

@@ -147,7 +147,7 @@ TEST(RegisterTestGroup, Register_NoBuildAndDep) {
147147
CHECK_TRUE(args.Clean());
148148

149149
// Make dummy toolchain and target
150-
buildcc::env::init(fs::current_path(), fs::current_path());
150+
buildcc::Project::Init(fs::current_path(), fs::current_path());
151151
buildcc::Toolchain toolchain(buildcc::ToolchainId::Gcc, "", "", "", "", "",
152152
"");
153153
buildcc::BaseTarget target("dummyT", buildcc::TargetType::Executable,
@@ -206,7 +206,7 @@ TEST(RegisterTestGroup, Register_NoBuildAndDep) {
206206
reg.Dep(target, dependency);
207207
}
208208

209-
buildcc::env::deinit();
209+
buildcc::Project::Deinit();
210210
mock().checkExpectations();
211211
}
212212

@@ -231,7 +231,7 @@ TEST(RegisterTestGroup, Register_BuildAndDep) {
231231
CHECK_TRUE(args.Clean());
232232

233233
// Make dummy toolchain and target
234-
buildcc::env::init(fs::current_path(), fs::current_path());
234+
buildcc::Project::Init(fs::current_path(), fs::current_path());
235235
buildcc::Toolchain toolchain(buildcc::ToolchainId::Gcc, "", "", "", "", "",
236236
"");
237237
buildcc::BaseTarget target("dummyT", buildcc::TargetType::Executable,
@@ -301,7 +301,7 @@ TEST(RegisterTestGroup, Register_BuildAndDep) {
301301
reg.Dep(target, dependency);
302302
}
303303

304-
buildcc::env::deinit();
304+
buildcc::Project::Deinit();
305305
mock().checkExpectations();
306306
}
307307

@@ -326,7 +326,7 @@ TEST(RegisterTestGroup, Register_DepDuplicate) {
326326
CHECK_TRUE(args.Clean());
327327

328328
// Make dummy toolchain and target
329-
buildcc::env::init(fs::current_path(), fs::current_path());
329+
buildcc::Project::Init(fs::current_path(), fs::current_path());
330330
buildcc::Toolchain toolchain(buildcc::ToolchainId::Gcc, "", "", "", "", "",
331331
"");
332332
buildcc::BaseTarget target("dummyT", buildcc::TargetType::Executable,
@@ -377,7 +377,7 @@ TEST(RegisterTestGroup, Register_DepDuplicate) {
377377
CHECK_THROWS(std::exception, reg.Dep(target, dependency2));
378378
}
379379

380-
buildcc::env::deinit();
380+
buildcc::Project::Deinit();
381381
mock().checkExpectations();
382382
}
383383

@@ -402,7 +402,7 @@ TEST(RegisterTestGroup, Register_DepCyclic) {
402402
CHECK_TRUE(args.Clean());
403403

404404
// Make dummy toolchain and target
405-
buildcc::env::init(fs::current_path(), fs::current_path());
405+
buildcc::Project::Init(fs::current_path(), fs::current_path());
406406
buildcc::Toolchain toolchain(buildcc::ToolchainId::Gcc, "", "", "", "", "",
407407
"");
408408
buildcc::BaseTarget target("dummyT", buildcc::TargetType::Executable,
@@ -452,7 +452,7 @@ TEST(RegisterTestGroup, Register_DepCyclic) {
452452
CHECK_THROWS(std::exception, reg.Dep(dependency2, target));
453453
}
454454

455-
buildcc::env::deinit();
455+
buildcc::Project::Deinit();
456456
mock().checkExpectations();
457457
}
458458

@@ -478,7 +478,7 @@ TEST(RegisterTestGroup, Register_Test) {
478478
CHECK_TRUE(args.Clean());
479479

480480
// Make dummy toolchain and target
481-
buildcc::env::init(fs::current_path(), fs::current_path());
481+
buildcc::Project::Init(fs::current_path(), fs::current_path());
482482
buildcc::Toolchain toolchain(buildcc::ToolchainId::Gcc, "", "", "", "", "",
483483
"");
484484
buildcc::BaseTarget target("dummyT", buildcc::TargetType::Executable,
@@ -537,7 +537,7 @@ TEST(RegisterTestGroup, Register_Test) {
537537
reg.RunTest();
538538
}
539539

540-
buildcc::env::deinit();
540+
buildcc::Project::Deinit();
541541
mock().checkExpectations();
542542
}
543543

@@ -563,7 +563,7 @@ TEST(RegisterTestGroup, Register_TestWithOutput) {
563563
CHECK_TRUE(args.Clean());
564564

565565
// Make dummy toolchain and target
566-
buildcc::env::init(fs::current_path(), fs::current_path());
566+
buildcc::Project::Init(fs::current_path(), fs::current_path());
567567
buildcc::Toolchain toolchain(buildcc::ToolchainId::Gcc, "", "", "", "", "",
568568
"");
569569
buildcc::BaseTarget target("dummyT", buildcc::TargetType::Executable,
@@ -676,7 +676,7 @@ TEST(RegisterTestGroup, Register_TestWithOutput) {
676676
CHECK_THROWS(std::exception, reg.RunTest());
677677
}
678678

679-
buildcc::env::deinit();
679+
buildcc::Project::Deinit();
680680
mock().checkExpectations();
681681
}
682682

buildcc/lib/env/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ if (${TESTING})
2626
target_link_options(mock_env PUBLIC ${TEST_LINK_FLAGS} ${BUILD_LINK_FLAGS})
2727

2828
# Tests
29+
add_executable(test_static_project test/test_static_project.cpp)
30+
target_link_libraries(test_static_project PRIVATE mock_env)
31+
2932
add_executable(test_env_util test/test_env_util.cpp)
3033
target_link_libraries(test_env_util PRIVATE mock_env)
3134

@@ -35,6 +38,7 @@ if (${TESTING})
3538
add_executable(test_command test/test_command.cpp)
3639
target_link_libraries(test_command PRIVATE mock_env)
3740

41+
add_test(NAME test_static_project COMMAND test_static_project)
3842
add_test(NAME test_env_util COMMAND test_env_util)
3943
add_test(NAME test_task_state COMMAND test_task_state)
4044
add_test(NAME test_command COMMAND test_command)

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

+16-14
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,24 @@
2323

2424
namespace fs = std::filesystem;
2525

26-
namespace buildcc::env {
26+
namespace buildcc {
2727

28-
/**
29-
* @brief Initialize project environment
30-
*
31-
* @param project_root_dir Root directory for source files
32-
* @param project_build_dir Directory for intermediate build files
33-
*/
34-
void init(const fs::path &project_root_dir, const fs::path &project_build_dir);
35-
void deinit();
28+
class Project {
29+
public:
30+
static void Init(const fs::path &project_root_dir,
31+
const fs::path &project_build_dir);
32+
static void Deinit();
33+
34+
static bool IsInit();
35+
static const fs::path &GetRootDir();
36+
static const fs::path &GetBuildDir();
3637

37-
// Getters
38-
bool is_init();
39-
const fs::path &get_project_root_dir();
40-
const fs::path &get_project_build_dir();
38+
private:
39+
static bool &GetStaticInit();
40+
static fs::path &GetStaticRootDir();
41+
static fs::path &GetStaticBuildDir();
42+
};
4143

42-
} // namespace buildcc::env
44+
} // namespace buildcc
4345

4446
#endif

buildcc/lib/env/src/env.cpp

+33-26
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,45 @@
1717
#include "env/env.h"
1818
#include "env/logging.h"
1919

20-
namespace {
20+
namespace buildcc {
2121

22-
fs::path root_dir_{""};
23-
fs::path build_dir_{""};
24-
bool init_ = false;
25-
26-
} // namespace
27-
28-
namespace buildcc::env {
29-
30-
void init(const fs::path &project_root_dir, const fs::path &project_build_dir) {
22+
void Project::Init(const fs::path &project_root_dir,
23+
const fs::path &project_build_dir) {
3124
// State
32-
root_dir_ = project_root_dir;
33-
build_dir_ = project_build_dir;
34-
root_dir_.make_preferred();
35-
build_dir_.make_preferred();
25+
fs::path root_dir = project_root_dir;
26+
fs::path build_dir = project_build_dir;
27+
root_dir.make_preferred();
28+
build_dir.make_preferred();
3629

37-
init_ = true;
30+
GetStaticRootDir() = root_dir;
31+
GetStaticBuildDir() = build_dir;
32+
GetStaticInit() = true;
3833

3934
// Logging
40-
set_log_pattern("%^[%l]%$ %v");
41-
set_log_level(LogLevel::Info);
35+
env::set_log_pattern("%^[%l]%$ %v");
36+
env::set_log_level(env::LogLevel::Info);
4237
}
43-
44-
void deinit() {
45-
root_dir_ = "";
46-
build_dir_ = "";
47-
init_ = false;
38+
void Project::Deinit() {
39+
GetStaticRootDir() = "";
40+
GetStaticBuildDir() = "";
41+
GetStaticInit() = false;
4842
}
4943

50-
bool is_init(void) { return init_; }
51-
const fs::path &get_project_root_dir() { return root_dir_; }
52-
const fs::path &get_project_build_dir() { return build_dir_; }
44+
bool Project::IsInit() { return GetStaticInit(); }
45+
const fs::path &Project::GetRootDir() { return GetStaticRootDir(); }
46+
const fs::path &Project::GetBuildDir() { return GetStaticBuildDir(); }
47+
48+
bool &Project::GetStaticInit() {
49+
static bool is_init = false;
50+
return is_init;
51+
}
52+
fs::path &Project::GetStaticRootDir() {
53+
static fs::path root_dir = "";
54+
return root_dir;
55+
}
56+
fs::path &Project::GetStaticBuildDir() {
57+
static fs::path build_dir = "";
58+
return build_dir;
59+
}
5360

54-
} // namespace buildcc::env
61+
} // namespace buildcc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "env/env.h"
2+
3+
// NOTE, Make sure all these includes are AFTER the system and header includes
4+
#include "CppUTest/CommandLineTestRunner.h"
5+
#include "CppUTest/MemoryLeakDetectorNewMacros.h"
6+
#include "CppUTest/TestHarness.h"
7+
#include "CppUTest/Utest.h"
8+
9+
// clang-format off
10+
TEST_GROUP(StaticProjectTestGroup)
11+
{
12+
void setup() {
13+
}
14+
};
15+
// clang-format on
16+
17+
TEST(StaticProjectTestGroup, ProjectInitialized) {
18+
CHECK_FALSE(buildcc::Project::IsInit());
19+
buildcc::Project::Init(fs::current_path(), fs::current_path());
20+
CHECK_TRUE(buildcc::Project::IsInit());
21+
buildcc::Project::Deinit();
22+
CHECK_FALSE(buildcc::Project::IsInit());
23+
}
24+
25+
int main(int ac, char **av) {
26+
MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
27+
return CommandLineTestRunner::RunAllTests(ac, av);
28+
}

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ class TargetEnv {
3535
* @brief Change the relative root path for a particular Generator / Target
3636
*
3737
* Absolute root now changes to
38-
* `env::get_project_root_dir() / target_relative_to_env_root`
38+
* `Project::GetRootDir() / target_relative_to_env_root`
3939
*
4040
* Absolute build dir remains the same.
4141
*
4242
* Can be used implicitly
4343
*
4444
* @param target_relative_to_env_root Change root dir with respect to
45-
* env::get_project_root_dir()
45+
* Project::GetRootDir()
4646
*/
4747
TargetEnv(const char *target_relative_to_env_root)
4848
: TargetEnv(fs::path(target_relative_to_env_root)) {}
@@ -53,12 +53,11 @@ class TargetEnv {
5353
* Only explicit usage allowed
5454
*
5555
* @param target_relative_to_env_root Change root dir with respect to
56-
* env::get_project_root_dir()
56+
* Project::GetRootDir()
5757
*/
5858
explicit TargetEnv(const fs::path &target_relative_to_env_root)
59-
: target_root_dir_(env::get_project_root_dir() /
60-
target_relative_to_env_root),
61-
target_build_dir_(env::get_project_build_dir()), relative_(true) {}
59+
: target_root_dir_(Project::GetRootDir() / target_relative_to_env_root),
60+
target_build_dir_(Project::GetBuildDir()), relative_(true) {}
6261

6362
/**
6463
* @brief Change the absolute root and build path for a particular Generator /

buildcc/lib/target/src/generator/generator.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ Generator::GetValueByIdentifier(const std::string &file_identifier) const {
7979
void Generator::Initialize() {
8080
// Checks
8181
env::assert_fatal(
82-
env::is_init(),
83-
"Environment is not initialized. Use the buildcc::env::init API");
82+
Project::IsInit(),
83+
"Environment is not initialized. Use the buildcc::Project::Init API");
8484

8585
//
8686
fs::create_directories(generator_build_dir_);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ void Generator::GenerateTask() {
7979
// Graph Generation
8080
for (const auto &i : user_.inputs) {
8181
std::string name =
82-
fmt::format("{}", i.lexically_relative(env::get_project_root_dir()));
82+
fmt::format("{}", i.lexically_relative(Project::GetRootDir()));
8383
tf::Task task = subflow.placeholder().name(name);
8484
task.precede(command_task);
8585
}
8686

8787
for (const auto &o : user_.outputs) {
8888
std::string name =
89-
fmt::format("{}", o.lexically_relative(env::get_project_root_dir()));
89+
fmt::format("{}", o.lexically_relative(Project::GetRootDir()));
9090
tf::Task task = subflow.placeholder().name(name);
9191
task.succeed(command_task);
9292
}

buildcc/lib/target/src/target/friend/compile_object.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ CompileObject::GetObjectData(const fs::path &absolute_source) const {
8282
// PRIVATE
8383

8484
// NOTE: If RELATIVE TargetEnv supplied
85-
// {target_root_dir} => `env::get_project_root_dir()` /
85+
// {target_root_dir} => `Project::GetRootDir()` /
8686
// `target_relative_to_root`
87-
// {target_build_dir} => `env::get_project_build_dir()` / `toolchain.GetName()`
87+
// {target_build_dir} => `Project::GetBuildDir()` / `toolchain.GetName()`
8888
// / `name`
8989

9090
// Scenarios

0 commit comments

Comments
 (0)