Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tidy] Shift Command to its own lib directory #97

Merged
merged 15 commits into from
Aug 20, 2021
Merged
3 changes: 3 additions & 0 deletions buildcc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ endif()
# Environment
add_subdirectory(lib/env)

#
add_subdirectory(lib/command)

# Toolchain
add_subdirectory(lib/toolchain)
add_subdirectory(toolchains)
Expand Down
65 changes: 65 additions & 0 deletions buildcc/lib/command/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
if (${TESTING})
add_library(mock_command
mock/execute.cpp
src/command.cpp
)
target_include_directories(mock_command PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/mock
)
target_link_libraries(mock_command PUBLIC
fmt::fmt-header-only
mock_env
CppUTest
CppUTestExt
gcov
)
target_compile_options(mock_command PUBLIC ${TEST_COMPILE_FLAGS} ${BUILD_COMPILE_FLAGS})
target_link_options(mock_command PUBLIC ${TEST_LINK_FLAGS} ${BUILD_LINK_FLAGS})
endif()

set(COMMAND_SRCS
src/execute.cpp
src/command.cpp
include/command/command.h
)

if(${BUILDCC_BUILD_AS_SINGLE_LIB})
target_sources(buildcc PRIVATE
${COMMAND_SRCS}
)
target_include_directories(buildcc PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${BUILDCC_INSTALL_HEADER_PREFIX}>
)
endif()

if(${BUILDCC_BUILD_AS_INTERFACE})
m_clangtidy("command")
add_library(command
${COMMAND_SRCS}
)
target_include_directories(command PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${BUILDCC_INSTALL_HEADER_PREFIX}>
)
target_link_libraries(command PRIVATE
fmt::fmt-header-only
tiny-process-library::tiny-process-library

env
)
target_compile_options(command PRIVATE ${BUILD_COMPILE_FLAGS})
target_link_options(command PRIVATE ${BUILD_LINK_FLAGS})


# Command install
if (${BUILDCC_INSTALL})
install(TARGETS command DESTINATION lib EXPORT commandConfig)
install(EXPORT commandConfig DESTINATION "${BUILDCC_INSTALL_LIB_PREFIX}/command")
endif()
endif()

if (${BUILDCC_INSTALL})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION "${BUILDCC_INSTALL_HEADER_PREFIX}")
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
* limitations under the License.
*/

#ifndef TARGET_COMMAND_H_
#define TARGET_COMMAND_H_
#ifndef COMMAND_COMMAND_H_
#define COMMAND_COMMAND_H_

#include <string>
#include <string_view>
#include <unordered_map>

#include "target/path.h"

#include "toolchain/toolchain.h"

namespace buildcc::internal {
namespace buildcc {

class Command {
public:
Expand All @@ -45,6 +43,6 @@ class Command {
std::unordered_map<const char *, std::string> default_values_;
};

} // namespace buildcc::internal
} // namespace buildcc

#endif
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "target/command.h"
#include "command/command.h"

#include "CppUTestExt/MockSupport.h"

namespace buildcc::internal {
namespace buildcc {

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

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

} // namespace m

} // namespace buildcc::internal
} // namespace buildcc
10 changes: 10 additions & 0 deletions buildcc/lib/command/mock/expect_command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef COMMAND_MOCK_EXPECT_COMMAND_H_
#define COMMAND_MOCK_EXPECT_COMMAND_H_

namespace buildcc::m {

void CommandExpect_Execute(unsigned int calls, bool expectation);

} // namespace buildcc::m

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
* limitations under the License.
*/

#include "target/command.h"
#include "command/command.h"

#include <algorithm>

#include "env/logging.h"
#include "fmt/format.h"

namespace buildcc::internal {
#include "env/assert_fatal.h"
#include "env/logging.h"

namespace buildcc {

void Command::AddDefaultArguments(
const std::unordered_map<const char *, std::string> &arguments) {
Expand Down Expand Up @@ -61,4 +63,4 @@ bool Command::ConstructAndExecute(
return Execute(constructed_command);
}

} // namespace buildcc::internal
} // namespace buildcc
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
* limitations under the License.
*/

#include "target/command.h"
#include "command/command.h"

#include "fmt/format.h"

#include "env/assert_fatal.h"
#include "env/logging.h"

#include "process.hpp"

namespace tpl = TinyProcessLib;

namespace buildcc::internal {
namespace buildcc {

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

} // namespace buildcc::internal
} // namespace buildcc
2 changes: 2 additions & 0 deletions buildcc/lib/env/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ if (${TESTING})
target_link_libraries(mock_env PUBLIC
fmt::fmt-header-only
)
target_compile_options(mock_env PUBLIC ${TEST_COMPILE_FLAGS} ${BUILD_COMPILE_FLAGS})
target_link_options(mock_env PUBLIC ${TEST_LINK_FLAGS} ${BUILD_LINK_FLAGS})
endif()

set(ENV_SRCS
Expand Down
3 changes: 1 addition & 2 deletions buildcc/lib/target/cmake/mock_target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ add_library(mock_target STATIC
src/fbs/fbs_storer.cpp

src/util/util.cpp
src/util/command.cpp
mock/util/execute.cpp
)
target_include_directories(mock_target PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
Expand All @@ -28,6 +26,7 @@ target_link_libraries(mock_target PUBLIC
Taskflow

mock_env
mock_command
toolchain

CppUTest
Expand Down
7 changes: 1 addition & 6 deletions buildcc/lib/target/cmake/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ set(TARGET_SRCS
src/fbs/fbs_storer.cpp

src/util/util.cpp
src/util/command.cpp
src/util/execute.cpp

include/target/target.h
include/target/fbs_loader.h
include/target/path.h
include/target/util.h
include/target/command.h
)

if(${BUILDCC_BUILD_AS_SINGLE_LIB})
Expand Down Expand Up @@ -48,13 +45,11 @@ if(${BUILDCC_BUILD_AS_INTERFACE})
)
target_link_libraries(target PUBLIC
env
command
toolchain
flatbuffers_header_only
Taskflow
)
target_link_libraries(target PRIVATE
tiny-process-library::tiny-process-library
)

target_include_directories(target PRIVATE
${SCHEMA_BUILD_DIR}
Expand Down
8 changes: 3 additions & 5 deletions buildcc/lib/target/include/target/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
#include <vector>

// Internal
#include "target/command.h"
#include "target/fbs_loader.h"
#include "target/path.h"

#include "toolchain/toolchain.h"

// Env
#include "command/command.h"
#include "env/env.h"
#include "toolchain/toolchain.h"

// Third Party
#include "taskflow/taskflow.hpp"
Expand Down Expand Up @@ -313,7 +311,7 @@ class Target {
// TODO, Add more internal variables

internal::FbsLoader loader_;
internal::Command command_;
Command command_;

// Build states
bool dirty_ = false;
Expand Down
6 changes: 0 additions & 6 deletions buildcc/lib/target/mock/expect_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@

namespace buildcc {

namespace internal::m {

void CommandExpect_Execute(unsigned int calls, bool expectation);

} // namespace internal::m

namespace base::m {

void TargetExpect_SourceRemoved(unsigned int calls, Target *target);
Expand Down
3 changes: 1 addition & 2 deletions buildcc/lib/target/src/target/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ void Target::RecompileSources(std::vector<fs::path> &compile_sources,
}

void Target::CompileSource(const fs::path &current_source) const {
const bool success =
internal::Command::Execute(CompileCommand(current_source));
const bool success = Command::Execute(CompileCommand(current_source));
env::assert_fatal(success, fmt::format("Compilation failed for: {}",
current_source.string()));
}
Expand Down
18 changes: 10 additions & 8 deletions buildcc/lib/target/test/target/test_target_c_compile_flags.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "constants.h"

#include "expect_command.h"
#include "expect_target.h"

#include "target/target.h"

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

buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
simple.Build();

mock().checkExpectations();
Expand Down Expand Up @@ -78,8 +80,8 @@ TEST(TargetTestCCompileFlagsGroup, Target_ChangedCompileFlag) {
simple.AddSource(DUMMY_MAIN);
simple.AddCCompileFlag("-std=c11");

buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
simple.Build();
}
{
Expand All @@ -88,8 +90,8 @@ TEST(TargetTestCCompileFlagsGroup, Target_ChangedCompileFlag) {
gcc, "data");
simple.AddSource(DUMMY_MAIN);
buildcc::base::m::TargetExpect_FlagChanged(1, &simple);
buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
simple.Build();
}

Expand All @@ -100,8 +102,8 @@ TEST(TargetTestCCompileFlagsGroup, Target_ChangedCompileFlag) {
simple.AddSource(DUMMY_MAIN);
simple.AddCCompileFlag("-std=c11");
buildcc::base::m::TargetExpect_FlagChanged(1, &simple);
buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
simple.Build();
}

Expand Down
18 changes: 10 additions & 8 deletions buildcc/lib/target/test/target/test_target_cpp_compile_flags.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "constants.h"

#include "expect_command.h"
#include "expect_target.h"

#include "target/target.h"

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

buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
simple.Build();

mock().checkExpectations();
Expand Down Expand Up @@ -78,8 +80,8 @@ TEST(TargetTestCppCompileFlagsGroup, Target_ChangedCompileFlag) {
simple.AddSource(DUMMY_MAIN);
simple.AddCCompileFlag("-std=c++17");

buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
simple.Build();
}
{
Expand All @@ -88,8 +90,8 @@ TEST(TargetTestCppCompileFlagsGroup, Target_ChangedCompileFlag) {
gcc, "data");
simple.AddSource(DUMMY_MAIN);
buildcc::base::m::TargetExpect_FlagChanged(1, &simple);
buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
simple.Build();
}

Expand All @@ -100,8 +102,8 @@ TEST(TargetTestCppCompileFlagsGroup, Target_ChangedCompileFlag) {
simple.AddSource(DUMMY_MAIN);
simple.AddCCompileFlag("-std=c++17");
buildcc::base::m::TargetExpect_FlagChanged(1, &simple);
buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::internal::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
buildcc::m::CommandExpect_Execute(1, true);
simple.Build();
}

Expand Down
Loading