Skip to content

Commit 6395133

Browse files
authored
[Tidy] Shift Command to its own lib directory (#97)
1 parent 305bb2b commit 6395133

26 files changed

+236
-139
lines changed

buildcc/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ endif()
3434
# Environment
3535
add_subdirectory(lib/env)
3636

37+
#
38+
add_subdirectory(lib/command)
39+
3740
# Toolchain
3841
add_subdirectory(lib/toolchain)
3942
add_subdirectory(toolchains)

buildcc/lib/command/CMakeLists.txt

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
if (${TESTING})
2+
add_library(mock_command
3+
mock/execute.cpp
4+
src/command.cpp
5+
)
6+
target_include_directories(mock_command PUBLIC
7+
${CMAKE_CURRENT_SOURCE_DIR}/include
8+
${CMAKE_CURRENT_SOURCE_DIR}/mock
9+
)
10+
target_link_libraries(mock_command PUBLIC
11+
fmt::fmt-header-only
12+
mock_env
13+
CppUTest
14+
CppUTestExt
15+
gcov
16+
)
17+
target_compile_options(mock_command PUBLIC ${TEST_COMPILE_FLAGS} ${BUILD_COMPILE_FLAGS})
18+
target_link_options(mock_command PUBLIC ${TEST_LINK_FLAGS} ${BUILD_LINK_FLAGS})
19+
endif()
20+
21+
set(COMMAND_SRCS
22+
src/execute.cpp
23+
src/command.cpp
24+
include/command/command.h
25+
)
26+
27+
if(${BUILDCC_BUILD_AS_SINGLE_LIB})
28+
target_sources(buildcc PRIVATE
29+
${COMMAND_SRCS}
30+
)
31+
target_include_directories(buildcc PUBLIC
32+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
33+
$<INSTALL_INTERFACE:${BUILDCC_INSTALL_HEADER_PREFIX}>
34+
)
35+
endif()
36+
37+
if(${BUILDCC_BUILD_AS_INTERFACE})
38+
m_clangtidy("command")
39+
add_library(command
40+
${COMMAND_SRCS}
41+
)
42+
target_include_directories(command PUBLIC
43+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
44+
$<INSTALL_INTERFACE:${BUILDCC_INSTALL_HEADER_PREFIX}>
45+
)
46+
target_link_libraries(command PRIVATE
47+
fmt::fmt-header-only
48+
tiny-process-library::tiny-process-library
49+
50+
env
51+
)
52+
target_compile_options(command PRIVATE ${BUILD_COMPILE_FLAGS})
53+
target_link_options(command PRIVATE ${BUILD_LINK_FLAGS})
54+
55+
56+
# Command install
57+
if (${BUILDCC_INSTALL})
58+
install(TARGETS command DESTINATION lib EXPORT commandConfig)
59+
install(EXPORT commandConfig DESTINATION "${BUILDCC_INSTALL_LIB_PREFIX}/command")
60+
endif()
61+
endif()
62+
63+
if (${BUILDCC_INSTALL})
64+
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION "${BUILDCC_INSTALL_HEADER_PREFIX}")
65+
endif()

buildcc/lib/target/include/target/command.h buildcc/lib/command/include/command/command.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef TARGET_COMMAND_H_
18-
#define TARGET_COMMAND_H_
17+
#ifndef COMMAND_COMMAND_H_
18+
#define COMMAND_COMMAND_H_
1919

20+
#include <string>
21+
#include <string_view>
2022
#include <unordered_map>
2123

22-
#include "target/path.h"
23-
24-
#include "toolchain/toolchain.h"
25-
26-
namespace buildcc::internal {
24+
namespace buildcc {
2725

2826
class Command {
2927
public:
@@ -45,6 +43,6 @@ class Command {
4543
std::unordered_map<const char *, std::string> default_values_;
4644
};
4745

48-
} // namespace buildcc::internal
46+
} // namespace buildcc
4947

5048
#endif

buildcc/lib/target/mock/util/execute.cpp buildcc/lib/command/mock/execute.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#include "target/command.h"
1+
#include "command/command.h"
22

33
#include "CppUTestExt/MockSupport.h"
44

5-
namespace buildcc::internal {
5+
namespace buildcc {
66

77
static constexpr const char *const EXECUTE_FUNCTION = "execute";
88

@@ -20,4 +20,4 @@ void CommandExpect_Execute(unsigned int calls, bool expectation) {
2020

2121
} // namespace m
2222

23-
} // namespace buildcc::internal
23+
} // namespace buildcc
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef COMMAND_MOCK_EXPECT_COMMAND_H_
2+
#define COMMAND_MOCK_EXPECT_COMMAND_H_
3+
4+
namespace buildcc::m {
5+
6+
void CommandExpect_Execute(unsigned int calls, bool expectation);
7+
8+
} // namespace buildcc::m
9+
10+
#endif

buildcc/lib/target/src/util/command.cpp buildcc/lib/command/src/command.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "target/command.h"
17+
#include "command/command.h"
1818

1919
#include <algorithm>
2020

21-
#include "env/logging.h"
2221
#include "fmt/format.h"
2322

24-
namespace buildcc::internal {
23+
#include "env/assert_fatal.h"
24+
#include "env/logging.h"
25+
26+
namespace buildcc {
2527

2628
void Command::AddDefaultArguments(
2729
const std::unordered_map<const char *, std::string> &arguments) {
@@ -61,4 +63,4 @@ bool Command::ConstructAndExecute(
6163
return Execute(constructed_command);
6264
}
6365

64-
} // namespace buildcc::internal
66+
} // namespace buildcc

buildcc/lib/target/src/util/execute.cpp buildcc/lib/command/src/execute.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "target/command.h"
17+
#include "command/command.h"
1818

19+
#include "fmt/format.h"
20+
21+
#include "env/assert_fatal.h"
1922
#include "env/logging.h"
2023

2124
#include "process.hpp"
2225

2326
namespace tpl = TinyProcessLib;
2427

25-
namespace buildcc::internal {
28+
namespace buildcc {
2629

2730
bool Command::Execute(const std::string &command) {
2831
env::assert_fatal(!command.empty(),
@@ -33,4 +36,4 @@ bool Command::Execute(const std::string &command) {
3336
return process.get_exit_status() == 0;
3437
}
3538

36-
} // namespace buildcc::internal
39+
} // namespace buildcc

buildcc/lib/env/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ if (${TESTING})
1010
target_link_libraries(mock_env PUBLIC
1111
fmt::fmt-header-only
1212
)
13+
target_compile_options(mock_env PUBLIC ${TEST_COMPILE_FLAGS} ${BUILD_COMPILE_FLAGS})
14+
target_link_options(mock_env PUBLIC ${TEST_LINK_FLAGS} ${BUILD_LINK_FLAGS})
1315
endif()
1416

1517
set(ENV_SRCS

buildcc/lib/target/cmake/mock_target.cmake

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ add_library(mock_target STATIC
1212
src/fbs/fbs_storer.cpp
1313

1414
src/util/util.cpp
15-
src/util/command.cpp
16-
mock/util/execute.cpp
1715
)
1816
target_include_directories(mock_target PUBLIC
1917
${CMAKE_CURRENT_SOURCE_DIR}/include
@@ -28,6 +26,7 @@ target_link_libraries(mock_target PUBLIC
2826
Taskflow
2927

3028
mock_env
29+
mock_command
3130
toolchain
3231

3332
CppUTest

buildcc/lib/target/cmake/target.cmake

+1-6
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@ set(TARGET_SRCS
1313
src/fbs/fbs_storer.cpp
1414

1515
src/util/util.cpp
16-
src/util/command.cpp
17-
src/util/execute.cpp
1816

1917
include/target/target.h
2018
include/target/fbs_loader.h
2119
include/target/path.h
2220
include/target/util.h
23-
include/target/command.h
2421
)
2522

2623
if(${BUILDCC_BUILD_AS_SINGLE_LIB})
@@ -48,13 +45,11 @@ if(${BUILDCC_BUILD_AS_INTERFACE})
4845
)
4946
target_link_libraries(target PUBLIC
5047
env
48+
command
5149
toolchain
5250
flatbuffers_header_only
5351
Taskflow
5452
)
55-
target_link_libraries(target PRIVATE
56-
tiny-process-library::tiny-process-library
57-
)
5853

5954
target_include_directories(target PRIVATE
6055
${SCHEMA_BUILD_DIR}

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@
2727
#include <vector>
2828

2929
// Internal
30-
#include "target/command.h"
3130
#include "target/fbs_loader.h"
3231
#include "target/path.h"
3332

34-
#include "toolchain/toolchain.h"
35-
36-
// Env
33+
#include "command/command.h"
3734
#include "env/env.h"
35+
#include "toolchain/toolchain.h"
3836

3937
// Third Party
4038
#include "taskflow/taskflow.hpp"
@@ -313,7 +311,7 @@ class Target {
313311
// TODO, Add more internal variables
314312

315313
internal::FbsLoader loader_;
316-
internal::Command command_;
314+
Command command_;
317315

318316
// Build states
319317
bool dirty_ = false;

buildcc/lib/target/mock/expect_target.h

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

66
namespace buildcc {
77

8-
namespace internal::m {
9-
10-
void CommandExpect_Execute(unsigned int calls, bool expectation);
11-
12-
} // namespace internal::m
13-
148
namespace base::m {
159

1610
void TargetExpect_SourceRemoved(unsigned int calls, Target *target);

buildcc/lib/target/src/target/source.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ void Target::RecompileSources(std::vector<fs::path> &compile_sources,
162162
}
163163

164164
void Target::CompileSource(const fs::path &current_source) const {
165-
const bool success =
166-
internal::Command::Execute(CompileCommand(current_source));
165+
const bool success = Command::Execute(CompileCommand(current_source));
167166
env::assert_fatal(success, fmt::format("Compilation failed for: {}",
168167
current_source.string()));
169168
}

buildcc/lib/target/test/target/test_target_c_compile_flags.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "constants.h"
22

3+
#include "expect_command.h"
34
#include "expect_target.h"
5+
46
#include "target/target.h"
57

68
#include "env/env.h"
@@ -48,8 +50,8 @@ TEST(TargetTestCCompileFlagsGroup, Target_AddCompileFlag) {
4850
simple.AddSource(DUMMY_MAIN);
4951
simple.AddCCompileFlag("-std=c11");
5052

51-
buildcc::internal::m::CommandExpect_Execute(1, true);
52-
buildcc::internal::m::CommandExpect_Execute(1, true);
53+
buildcc::m::CommandExpect_Execute(1, true);
54+
buildcc::m::CommandExpect_Execute(1, true);
5355
simple.Build();
5456

5557
mock().checkExpectations();
@@ -78,8 +80,8 @@ TEST(TargetTestCCompileFlagsGroup, Target_ChangedCompileFlag) {
7880
simple.AddSource(DUMMY_MAIN);
7981
simple.AddCCompileFlag("-std=c11");
8082

81-
buildcc::internal::m::CommandExpect_Execute(1, true);
82-
buildcc::internal::m::CommandExpect_Execute(1, true);
83+
buildcc::m::CommandExpect_Execute(1, true);
84+
buildcc::m::CommandExpect_Execute(1, true);
8385
simple.Build();
8486
}
8587
{
@@ -88,8 +90,8 @@ TEST(TargetTestCCompileFlagsGroup, Target_ChangedCompileFlag) {
8890
gcc, "data");
8991
simple.AddSource(DUMMY_MAIN);
9092
buildcc::base::m::TargetExpect_FlagChanged(1, &simple);
91-
buildcc::internal::m::CommandExpect_Execute(1, true);
92-
buildcc::internal::m::CommandExpect_Execute(1, true);
93+
buildcc::m::CommandExpect_Execute(1, true);
94+
buildcc::m::CommandExpect_Execute(1, true);
9395
simple.Build();
9496
}
9597

@@ -100,8 +102,8 @@ TEST(TargetTestCCompileFlagsGroup, Target_ChangedCompileFlag) {
100102
simple.AddSource(DUMMY_MAIN);
101103
simple.AddCCompileFlag("-std=c11");
102104
buildcc::base::m::TargetExpect_FlagChanged(1, &simple);
103-
buildcc::internal::m::CommandExpect_Execute(1, true);
104-
buildcc::internal::m::CommandExpect_Execute(1, true);
105+
buildcc::m::CommandExpect_Execute(1, true);
106+
buildcc::m::CommandExpect_Execute(1, true);
105107
simple.Build();
106108
}
107109

buildcc/lib/target/test/target/test_target_cpp_compile_flags.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "constants.h"
22

3+
#include "expect_command.h"
34
#include "expect_target.h"
5+
46
#include "target/target.h"
57

68
#include "env/env.h"
@@ -48,8 +50,8 @@ TEST(TargetTestCppCompileFlagsGroup, Target_AddCompileFlag) {
4850
simple.AddSource(DUMMY_MAIN);
4951
simple.AddCppCompileFlag("-std=c++17");
5052

51-
buildcc::internal::m::CommandExpect_Execute(1, true);
52-
buildcc::internal::m::CommandExpect_Execute(1, true);
53+
buildcc::m::CommandExpect_Execute(1, true);
54+
buildcc::m::CommandExpect_Execute(1, true);
5355
simple.Build();
5456

5557
mock().checkExpectations();
@@ -78,8 +80,8 @@ TEST(TargetTestCppCompileFlagsGroup, Target_ChangedCompileFlag) {
7880
simple.AddSource(DUMMY_MAIN);
7981
simple.AddCCompileFlag("-std=c++17");
8082

81-
buildcc::internal::m::CommandExpect_Execute(1, true);
82-
buildcc::internal::m::CommandExpect_Execute(1, true);
83+
buildcc::m::CommandExpect_Execute(1, true);
84+
buildcc::m::CommandExpect_Execute(1, true);
8385
simple.Build();
8486
}
8587
{
@@ -88,8 +90,8 @@ TEST(TargetTestCppCompileFlagsGroup, Target_ChangedCompileFlag) {
8890
gcc, "data");
8991
simple.AddSource(DUMMY_MAIN);
9092
buildcc::base::m::TargetExpect_FlagChanged(1, &simple);
91-
buildcc::internal::m::CommandExpect_Execute(1, true);
92-
buildcc::internal::m::CommandExpect_Execute(1, true);
93+
buildcc::m::CommandExpect_Execute(1, true);
94+
buildcc::m::CommandExpect_Execute(1, true);
9395
simple.Build();
9496
}
9597

@@ -100,8 +102,8 @@ TEST(TargetTestCppCompileFlagsGroup, Target_ChangedCompileFlag) {
100102
simple.AddSource(DUMMY_MAIN);
101103
simple.AddCCompileFlag("-std=c++17");
102104
buildcc::base::m::TargetExpect_FlagChanged(1, &simple);
103-
buildcc::internal::m::CommandExpect_Execute(1, true);
104-
buildcc::internal::m::CommandExpect_Execute(1, true);
105+
buildcc::m::CommandExpect_Execute(1, true);
106+
buildcc::m::CommandExpect_Execute(1, true);
105107
simple.Build();
106108
}
107109

0 commit comments

Comments
 (0)