Skip to content

Commit 905cb5c

Browse files
authored
Env task states (#163)
1 parent a3cbff2 commit 905cb5c

16 files changed

+310
-41
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,15 @@ Build C, C++ and ASM files in C++
8989

9090
## Dependency Chart
9191

92-
![Depedency Chart](doc/software_architecture/uml/dependency_graph.png)
92+
![Dependency Chart](doc/software_architecture/uml/dependency_graph.png)
9393

94-
- See also [how to create uml diagrams using VSCode](doc/software_architecture/create_uml_diagrams.md)
94+
## State Diagram
95+
96+
### [Generator State Diagram](doc/software_architecture/uml/generator_tasks.png)
97+
98+
### [Target State Diagram](doc/software_architecture/uml/target_tasks.png)
99+
100+
> See also [how to create uml diagrams using VSCode](doc/software_architecture/create_uml_diagrams.md)
95101
96102
## Community Plugin
97103

buildcc/lib/env/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ if (${TESTING})
33
add_library(mock_env STATIC
44
mock/logging.cpp
55
mock/assert_fatal.cpp
6+
67
src/env.cpp
8+
src/task_state.cpp
79
)
810
target_include_directories(mock_env PUBLIC
911
${CMAKE_CURRENT_SOURCE_DIR}/include
1012
)
1113
target_link_libraries(mock_env PUBLIC
1214
fmt::fmt-header-only
15+
Taskflow
16+
1317
CppUTest
1418
CppUTestExt
1519
gcov
@@ -21,7 +25,11 @@ if (${TESTING})
2125
add_executable(test_env_util test/test_env_util.cpp)
2226
target_link_libraries(test_env_util PRIVATE mock_env)
2327

28+
add_executable(test_task_state test/test_task_state.cpp)
29+
target_link_libraries(test_task_state PRIVATE mock_env)
30+
2431
add_test(NAME test_env_util COMMAND test_env_util)
32+
add_test(NAME test_task_state COMMAND test_task_state)
2533
endif()
2634

2735
set(ENV_SRCS
@@ -34,6 +42,9 @@ set(ENV_SRCS
3442
include/env/host_os.h
3543
include/env/host_compiler.h
3644
include/env/util.h
45+
46+
src/task_state.cpp
47+
include/env/task_state.h
3748
)
3849

3950
if(${BUILDCC_BUILD_AS_SINGLE_LIB})
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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_PRIVATE_TASK_STATE_H_
18+
#define ENV_PRIVATE_TASK_STATE_H_
19+
20+
namespace buildcc::env {
21+
22+
enum class TaskState {
23+
SUCCESS,
24+
FAILURE,
25+
// TODO, Add more states here
26+
};
27+
28+
void set_task_state(TaskState state);
29+
TaskState get_task_state();
30+
31+
} // namespace buildcc::env
32+
33+
#endif

buildcc/lib/env/src/task_state.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
#include "env/task_state.h"
18+
19+
#include <mutex>
20+
21+
namespace {
22+
23+
std::mutex current_state_mutex;
24+
buildcc::env::TaskState current_state{buildcc::env::TaskState::SUCCESS};
25+
26+
} // namespace
27+
28+
namespace buildcc::env {
29+
30+
void set_task_state(TaskState state) {
31+
std::lock_guard<std::mutex> guard(current_state_mutex);
32+
current_state = state;
33+
}
34+
35+
TaskState get_task_state() { return current_state; }
36+
37+
} // namespace buildcc::env
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "env/task_state.h"
2+
3+
#include "taskflow/taskflow.hpp"
4+
5+
// NOTE, Make sure all these includes are AFTER the system and header includes
6+
#include "CppUTest/CommandLineTestRunner.h"
7+
#include "CppUTest/MemoryLeakDetectorNewMacros.h"
8+
#include "CppUTest/TestHarness.h"
9+
#include "CppUTest/Utest.h"
10+
11+
// clang-format off
12+
TEST_GROUP(TaskStateTestGroup)
13+
{
14+
void setup() {
15+
buildcc::env::set_task_state(buildcc::env::TaskState::SUCCESS);
16+
}
17+
};
18+
// clang-format on
19+
20+
TEST(TaskStateTestGroup, OneTask) {
21+
CHECK(buildcc::env::get_task_state() == buildcc::env::TaskState::SUCCESS);
22+
23+
tf::Taskflow tf;
24+
bool completed = false;
25+
tf.emplace([&]() {
26+
buildcc::env::set_task_state(buildcc::env::TaskState::FAILURE);
27+
completed = true;
28+
});
29+
tf::Executor executor(2);
30+
executor.run(tf);
31+
executor.wait_for_all();
32+
33+
CHECK(buildcc::env::get_task_state() == buildcc::env::TaskState::FAILURE);
34+
CHECK_TRUE(completed);
35+
}
36+
37+
TEST(TaskStateTestGroup, MultipleTasks) {
38+
CHECK(buildcc::env::get_task_state() == buildcc::env::TaskState::SUCCESS);
39+
40+
tf::Taskflow tf;
41+
bool completed1 = false;
42+
tf.emplace([&]() {
43+
buildcc::env::set_task_state(buildcc::env::TaskState::FAILURE);
44+
completed1 = true;
45+
});
46+
47+
bool completed2 = false;
48+
tf.emplace([&]() {
49+
buildcc::env::set_task_state(buildcc::env::TaskState::FAILURE);
50+
completed2 = true;
51+
});
52+
53+
bool completed3 = false;
54+
tf.emplace([&]() {
55+
buildcc::env::set_task_state(buildcc::env::TaskState::FAILURE);
56+
completed3 = true;
57+
});
58+
59+
tf::Executor executor(2);
60+
executor.run(tf);
61+
executor.wait_for_all();
62+
63+
CHECK(buildcc::env::get_task_state() == buildcc::env::TaskState::FAILURE);
64+
CHECK_TRUE(completed1);
65+
CHECK_TRUE(completed2);
66+
CHECK_TRUE(completed3);
67+
}
68+
69+
int main(int ac, char **av) {
70+
return CommandLineTestRunner::RunAllTests(ac, av);
71+
}

buildcc/lib/target/cmake/mock_target.cmake

+2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ add_library(mock_target STATIC
33

44
# Generator mocks
55
mock/generator/task.cpp
6+
mock/generator/runner.cpp
67
mock/generator/recheck_states.cpp
78

89
# Target mocks
910
mock/target/tasks.cpp
11+
mock/target/runner.cpp
1012
mock/target/recheck_states.cpp
1113
)
1214
target_include_directories(mock_target PUBLIC

buildcc/lib/target/mock/expect_generator.h

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace buildcc::base::m {
77

8+
void GeneratorRunner(Generator &generator);
9+
810
void GeneratorExpect_InputRemoved(unsigned int calls, Generator *generator);
911
void GeneratorExpect_InputAdded(unsigned int calls, Generator *generator);
1012
void GeneratorExpect_InputUpdated(unsigned int calls, Generator *generator);

buildcc/lib/target/mock/expect_target.h

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace buildcc {
77

88
namespace base::m {
99

10+
void TargetRunner(Target &target);
11+
1012
void TargetExpect_SourceRemoved(unsigned int calls, Target *target);
1113
void TargetExpect_SourceAdded(unsigned int calls, Target *target);
1214
void TargetExpect_SourceUpdated(unsigned int calls, Target *target);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "target/generator.h"
2+
3+
#include "expect_generator.h"
4+
5+
namespace buildcc::base::m {
6+
7+
void GeneratorRunner(Generator &generator) {
8+
tf::Executor executor(1);
9+
executor.run(generator.GetTaskflow());
10+
executor.wait_for_all();
11+
}
12+
13+
} // namespace buildcc::base::m
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "target/target.h"
2+
3+
#include "expect_target.h"
4+
5+
namespace buildcc::base::m {
6+
7+
void TargetRunner(Target &target) {
8+
tf::Executor executor(1);
9+
executor.run(target.GetTaskflow());
10+
executor.wait_for_all();
11+
}
12+
13+
} // namespace buildcc::base::m
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
@startuml
22

3-
4-
usecase IncludeDirs
5-
usecase PreprocessorFlags
6-
usecase CompilerFlags
7-
8-
usecase LinkFlags
9-
usecase LinkDirs
10-
11-
file SourceFile
12-
file HeaderFile
13-
file ObjectFile
14-
file PrecompileHeader
15-
file Executable
16-
file Library
17-
file Modules
18-
19-
queue Compiler
20-
queue Linker
21-
22-
HeaderFile ..> Compiler
23-
IncludeDirs ..> Compiler
24-
PreprocessorFlags ..> Compiler
25-
CompilerFlags ..> Compiler
26-
SourceFile ..> Compiler
27-
28-
Compiler ..> ObjectFile
29-
Compiler ..> PrecompileHeader
30-
31-
PrecompileHeader ..> ObjectFile
32-
33-
LinkFlags ..> Linker
34-
LinkDirs ..> Linker
35-
ObjectFile ..> Linker
36-
Library ..> Linker
37-
Modules ..> Linker
38-
39-
Linker ..> Executable
40-
Linker ..> Library
41-
Linker ..> Modules
3+
[*] --> Compile
4+
Compile --> Link
5+
Link --> [*]
6+
7+
state Compile {
8+
SourceFile --> Compiler
9+
HeaderFile --> Compiler
10+
IncludeDirs --> Compiler
11+
PreprocessorFlags --> Compiler
12+
CompilerFlags --> Compiler
13+
14+
Compiler --> ObjectFile
15+
Compiler --> PrecompileHeader
16+
17+
PrecompileHeader --> ObjectFile
18+
19+
SourceFile : Path + Timestamp
20+
HeaderFile : Path + Timestamp
21+
IncludeDirs : Path
22+
PreprocessorFlags : String
23+
CompilerFlags : String
24+
Compiler : Toolchain
25+
PrecompileHeader : Path
26+
ObjectFile : Path
27+
}
28+
29+
Compile : {1 ... N}
30+
31+
state Link {
32+
Library --> Linker
33+
Module --> Linker
34+
LinkDirs --> Linker
35+
LinkFlags --> Linker
36+
37+
Linker --> Executable
38+
Linker --> Library
39+
Linker --> Module
40+
41+
Library : Path + Timestamp
42+
Module : Path + Timestamp
43+
Executable : Path + Timestamp
44+
Linker : Toolchain
45+
LinkDirs : Path
46+
LinkFlags : String
47+
}
4248

4349
@enduml
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@startuml
2+
3+
[*] --> GeneratorStartTask
4+
GeneratorStartTask --> GenerateTask : Success
5+
GenerateTask --> GeneratorEndTask : Success / Failure
6+
GeneratorEndTask --> [*]
7+
8+
state GenerateTask {
9+
}
10+
11+
GeneratorStartTask --> GeneratorEndTask : Failure
12+
13+
GeneratorStartTask : GetEnvTaskState
14+
GenerateTask : PreGenerate
15+
GenerateTask : Generate
16+
GenerateTask : PostGenerate
17+
GeneratorEndTask : Store
18+
GeneratorEndTask : UpdateEnvTaskStateIfFailure
19+
20+
@enduml
Loading

0 commit comments

Comments
 (0)