Skip to content

Target Command module #67

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

Merged
merged 17 commits into from
Jun 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Basic Installation steps
# FAQ

- [Why has _this_ third-party library been chosen?](doc/faq/why_this_lib.md)
- [How do I create my own custom target commands?](doc/custom_target/commands.md)

## Design

Expand Down
3 changes: 2 additions & 1 deletion buildcc/lib/target/cmake/mock_target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ target_sources(mock_target PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/fbs/fbs_storer.cpp

${CMAKE_CURRENT_SOURCE_DIR}/src/util/util.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mock/util/command.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/util/command.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mock/util/execute.cpp
)

target_compile_options(mock_target PUBLIC ${TEST_COMPILE_FLAGS} ${BUILD_COMPILE_FLAGS})
Expand Down
1 change: 1 addition & 0 deletions buildcc/lib/target/cmake/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ target_sources(target PRIVATE

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

include/target.h
include/internal/fbs_loader.h
Expand Down
1 change: 1 addition & 0 deletions buildcc/lib/target/cmake/target_install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/include/internal/fbs_loader.h
${CMAKE_CURRENT_SOURCE_DIR}/include/internal/path.h
${CMAKE_CURRENT_SOURCE_DIR}/include/internal/util.h
${CMAKE_CURRENT_SOURCE_DIR}/include/internal/command.h
DESTINATION "${BUILDCC_INSTALL_HEADER_PREFIX}/internal"
)
install(EXPORT targetConfig DESTINATION "${BUILDCC_INSTALL_LIB_PREFIX}/target")
50 changes: 50 additions & 0 deletions buildcc/lib/target/include/internal/command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2021 Niket Naidu. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef TARGET_INCLUDE_INTERNAL_COMMAND_H_
#define TARGET_INCLUDE_INTERNAL_COMMAND_H_

#include <unordered_map>

#include "internal/path.h"

#include "toolchain.h"

namespace buildcc::internal {

class Command {
public:
explicit Command() = default;

void AddDefaultArguments(
const std::unordered_map<const char *, std::string> &arguments);

std::string Construct(std::string_view format,
const std::unordered_map<const char *, std::string>
&arguments = {}) const;
bool ConstructAndExecute(std::string_view format,
const std::unordered_map<const char *, std::string>
&arguments = {}) const;

static bool Execute(const std::string &command);

private:
std::unordered_map<const char *, std::string> default_values_;
};

} // namespace buildcc::internal

#endif
12 changes: 0 additions & 12 deletions buildcc/lib/target/include/internal/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@

namespace buildcc::internal {

// System
/**
* @brief Executes an external command
* Internally command uses `std::system`
* TODO, Replace with `subprocess`
*
* @param command
* @return true
* @return false
*/
bool command(const std::string &command);

// Additions
/**
* @brief Existing path is stored inside stored_paths
Expand Down
15 changes: 4 additions & 11 deletions buildcc/lib/target/include/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <vector>

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

Expand Down Expand Up @@ -260,29 +261,21 @@ class Target {
std::unordered_set<std::string> current_cpp_compile_flags_;
std::unordered_set<std::string> current_link_flags_;

// TODO, Make appending to this more efficient
// TODO, Might not need to be persistent
// TODO, aggregates might not need to exist, check `std::insert` APIs over
// vector and unordered_set
std::string aggregated_include_dirs_;
std::string aggregated_lib_dirs_;
// NOTE, This contains current_external_lib_deps_ + current_lib_deps_
std::string aggregated_lib_deps_;

std::string aggregated_preprocessor_flags_;
std::string aggregated_c_compile_flags_;
std::string aggregated_cpp_compile_flags_;
std::string aggregated_link_flags_;

// TODO, Add more internal variables

internal::FbsLoader loader_;
bool dirty_ = false;
internal::Command command_;

// Build states
bool dirty_ = false;
bool first_build_ = false;
bool rebuild_ = false;

// Dependency
static constexpr const char *const kCompileTaskName = "Compile";
static constexpr const char *const kLinkTaskName = "Link";

Expand Down
2 changes: 1 addition & 1 deletion buildcc/lib/target/mock/expect_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace buildcc {

namespace internal::m {

void Expect_command(unsigned int calls, bool expectation);
void CommandExpect_Execute(unsigned int calls, bool expectation);

} // namespace internal::m

Expand Down
23 changes: 0 additions & 23 deletions buildcc/lib/target/mock/util/command.cpp

This file was deleted.

23 changes: 23 additions & 0 deletions buildcc/lib/target/mock/util/execute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "internal/command.h"

#include "CppUTestExt/MockSupport.h"

namespace buildcc::internal {

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

// command
bool Command::Execute(const std::string &command) {
(void)command;
return mock().actualCall(EXECUTE_FUNCTION).returnBoolValue();
}

namespace m {

void CommandExpect_Execute(unsigned int calls, bool expectation) {
mock().expectNCalls(calls, EXECUTE_FUNCTION).andReturnValue(expectation);
}

} // namespace m

} // namespace buildcc::internal
48 changes: 25 additions & 23 deletions buildcc/lib/target/src/target/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,31 @@ void Target::Build() {
env::log_trace(name_, __FUNCTION__);

// TODO, Optimize these
aggregated_preprocessor_flags_ =
internal::aggregate(current_preprocessor_flags_);
aggregated_c_compile_flags_ = internal::aggregate(current_c_compile_flags_);
aggregated_cpp_compile_flags_ =
internal::aggregate(current_cpp_compile_flags_);
aggregated_link_flags_ = internal::aggregate(current_link_flags_);

aggregated_lib_deps_ =
fmt::format("{} {}", internal::aggregate(current_external_lib_deps_),
internal::aggregate(current_lib_deps_));
command_.AddDefaultArguments({
{"include_dirs", internal::aggregate_with_prefix(prefix_include_dir_,
current_include_dirs_)},
{"lib_dirs",
internal::aggregate_with_prefix(prefix_lib_dir_, current_lib_dirs_)},

aggregated_include_dirs_ = internal::aggregate_with_prefix(
prefix_include_dir_, current_include_dirs_);
aggregated_lib_dirs_ =
internal::aggregate_with_prefix(prefix_lib_dir_, current_lib_dirs_);
// TODO, Segregate this if needed
{"lib_deps",
fmt::format("{} {}", internal::aggregate(current_external_lib_deps_),
internal::aggregate(current_lib_deps_))},

{"preprocessor_flags", internal::aggregate(current_preprocessor_flags_)},
{"link_flags", internal::aggregate(current_link_flags_)},

// Toolchain executables here
{"asm_compiler", toolchain_.GetAsmCompiler()},
{"c_compiler", toolchain_.GetCCompiler()},
{"cpp_compiler", toolchain_.GetCppCompiler()},
{"archiver", toolchain_.GetArchiver()},
{"linker", toolchain_.GetLinker()},
});

const bool is_loaded = loader_.Load();
// TODO, Add more checks for build files physically present
Expand Down Expand Up @@ -120,19 +130,11 @@ void Target::LinkTarget() {
const std::string output_target =
internal::Path::CreateNewPath(GetTargetPath()).GetPathAsString();

std::string link_command =
fmt::format(Link(), fmt::arg("output", output_target),
fmt::arg("link_flags", aggregated_link_flags_),
fmt::arg("compiled_sources", aggregated_compiled_sources),
fmt::arg("lib_dirs", aggregated_lib_dirs_),
fmt::arg("lib_deps", aggregated_lib_deps_),
// Toolchain executables here
fmt::arg("asm_compiler", toolchain_.GetAsmCompiler()),
fmt::arg("c_compiler", toolchain_.GetCCompiler()),
fmt::arg("cpp_compiler", toolchain_.GetCppCompiler()),
fmt::arg("archiver", toolchain_.GetArchiver()),
fmt::arg("linker", toolchain_.GetLinker()));
bool success = internal::command(link_command);
const bool success = command_.ConstructAndExecute(
Link(), {
{"output", output_target},
{"compiled_sources", aggregated_compiled_sources},
});
env::assert_fatal(success, fmt::format("Compilation failed for: {}", name_));
}

Expand Down
17 changes: 9 additions & 8 deletions buildcc/lib/target/src/target/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ void Target::RecompileSources() {
}

void Target::CompileSource(const fs::path &current_source) const {
const bool success = internal::command(CompileCommand(current_source));
const bool success =
internal::Command::Execute(CompileCommand(current_source));
env::assert_fatal(success, fmt::format("Compilation failed for: {}",
current_source.string()));
}
Expand All @@ -193,13 +194,13 @@ std::string Target::CompileCommand(const fs::path &current_source) const {
: type == FileExtType::Cpp ? aggregated_cpp_compile_flags_
: "";

// Construct the Compile Command
return fmt::format(
CompileCommand(), fmt::arg("compiler", compiler),
fmt::arg("preprocessor_flags", aggregated_preprocessor_flags_),
fmt::arg("compile_flags", aggregated_compile_flags),
fmt::arg("include_dirs", aggregated_include_dirs_),
fmt::arg("output", output), fmt::arg("input", input));
return command_.Construct(CompileCommand(),
{
{"compiler", compiler},
{"compile_flags", aggregated_compile_flags},
{"output", output},
{"input", input},
});
}

std::string_view Target::CompileCommand() const {
Expand Down
46 changes: 38 additions & 8 deletions buildcc/lib/target/src/util/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,51 @@
* limitations under the License.
*/

#include "internal/util.h"
#include "internal/command.h"

#include <algorithm>

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

#include "process.hpp"
namespace buildcc::internal {

namespace tpl = TinyProcessLib;
void Command::AddDefaultArguments(
const std::unordered_map<const char *, std::string> &arguments) {
default_values_.insert(arguments.begin(), arguments.end());
}

namespace buildcc::internal {
std::string Command::Construct(
std::string_view format,
const std::unordered_map<const char *, std::string> &arguments) const {
// Construct your arguments
fmt::dynamic_format_arg_store<fmt::format_context> store;
std::string constructed_string;
try {

bool command(const std::string &command) {
buildcc::env::log_debug("system", command);
std::for_each(default_values_.cbegin(), default_values_.cend(),
[&store](const std::pair<const char *, std::string> &p) {
store.push_back(fmt::arg(p.first, p.second));
});

std::for_each(arguments.cbegin(), arguments.cend(),
[&store](const std::pair<const char *, std::string> &p) {
store.push_back(fmt::arg(p.first, p.second));
});

// Construct your command
constructed_string = fmt::vformat(format, store);
} catch (const std::exception &e) {
env::assert_fatal(false, e.what());
}
return constructed_string;
}

tpl::Process process(command);
return process.get_exit_status() == 0;
bool Command::ConstructAndExecute(
std::string_view format,
const std::unordered_map<const char *, std::string> &arguments) const {
const std::string constructed_command = Construct(format, arguments);
return Execute(constructed_command);
}

} // namespace buildcc::internal
34 changes: 34 additions & 0 deletions buildcc/lib/target/src/util/execute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2021 Niket Naidu. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "internal/command.h"

#include "logging.h"

#include "process.hpp"

namespace tpl = TinyProcessLib;

namespace buildcc::internal {

bool Command::Execute(const std::string &command) {
// Run the process
buildcc::env::log_debug("system", command);
tpl::Process process(command);
return process.get_exit_status() == 0;
}

} // namespace buildcc::internal
Loading