Skip to content

Toolchain verify configs #180

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 7 commits into from
Jan 5, 2022
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
21 changes: 21 additions & 0 deletions buildcc/lib/toolchain/include/toolchain/api/toolchain_verify.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define TOOLCHAIN_TOOLCHAIN_VERIFY_H_

#include <filesystem>
#include <optional>
#include <vector>

#include "env/logging.h"
Expand All @@ -42,6 +43,13 @@ namespace buildcc::base {
struct VerifyToolchainConfig {
std::vector<std::string> absolute_search_paths;
std::vector<std::string> env_vars{"PATH"};

std::optional<std::string> compiler_version;
std::optional<std::string> target_arch;

// Updates the toolchain with absolute paths once verified
// If multiple toolchains are found, uses the first in the list
bool update{true};
};

/**
Expand All @@ -60,8 +68,21 @@ struct VerifiedToolchain {

template <typename T> class ToolchainVerify {
public:
/**
* @brief Verify your toolchain executables by searching your operating system
* paths
* Only add the verified path IF all toolchain executables are matched
*
* @param config Search paths to find toolchains
* @return std::vector<VerifiedToolchain> Operating system can contain
* multiple toolchains of similar names with different versions. Collect all
* of them
*/
std::vector<VerifiedToolchain>
Verify(const VerifyToolchainConfig &config = VerifyToolchainConfig());

protected:
VerifiedToolchain verified_toolchain_;
};

} // namespace buildcc::base
Expand Down
3 changes: 3 additions & 0 deletions buildcc/lib/toolchain/include/toolchain/toolchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class Toolchain : public ToolchainVerify<Toolchain> {
const std::string &GetArchiver() const { return archiver_; }
const std::string &GetLinker() const { return linker_; }

private:
friend class ToolchainVerify<Toolchain>;

private:
Id id_;
std::string name_;
Expand Down
44 changes: 42 additions & 2 deletions buildcc/lib/toolchain/src/api/toolchain_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ ToolchainVerify<T>::Verify(const VerifyToolchainConfig &config) {
}

std::vector<VerifiedToolchain> verified_toolchains;
const T &t = static_cast<const T &>(*this);
T &t = static_cast<T &>(*this);

ToolchainMatcher matcher(t);
matcher.FillWithToolchainFilenames();
Expand Down Expand Up @@ -233,13 +233,53 @@ ToolchainVerify<T>::Verify(const VerifyToolchainConfig &config) {
vt.path = p;
vt.compiler_version = env::trim(GetCompilerVersion(p, t).value_or(""));
vt.target_arch = env::trim(GetCompilerArchitecture(p, t).value_or(""));
verified_toolchains.push_back(vt);

// Check add
bool add{true};
if (config.compiler_version.has_value()) {
add = add && (config.compiler_version.value() == vt.compiler_version);
}
if (config.target_arch.has_value()) {
add = add && (config.target_arch.value() == vt.target_arch);
}
if (add) {
verified_toolchains.push_back(vt);
}
}

// Reset
matcher.FillWithToolchainFilenames();
}

if (config.update && !verified_toolchains.empty()) {
constexpr const char *os_executable_ext =
buildcc::env::get_os_executable_extension();
buildcc::env::assert_fatal<os_executable_ext != nullptr>(
"OS not supported");

verified_toolchain_ = verified_toolchains[0];
t.asm_compiler_ = (verified_toolchain_.path /
fmt::format("{}{}", t.asm_compiler_, os_executable_ext))
.make_preferred()
.string();
t.c_compiler_ = (verified_toolchain_.path /
fmt::format("{}{}", t.c_compiler_, os_executable_ext))
.make_preferred()
.string();
t.cpp_compiler_ = (verified_toolchain_.path /
fmt::format("{}{}", t.cpp_compiler_, os_executable_ext))
.make_preferred()
.string();
t.archiver_ = (verified_toolchain_.path /
fmt::format("{}{}", t.archiver_, os_executable_ext))
.make_preferred()
.string();
t.linker_ = (verified_toolchain_.path /
fmt::format("{}{}", t.linker_, os_executable_ext))
.make_preferred()
.string();
}

return verified_toolchains;
}

Expand Down
130 changes: 130 additions & 0 deletions buildcc/lib/toolchain/test/test_toolchain_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,136 @@ TEST(ToolchainTestGroup, VerifyToolchain_LockedFolder) {
}
}

TEST(ToolchainTestGroup, VerifyToolchain_ConditionalAdd_CompilerVersion) {
buildcc::base::Toolchain gcc(buildcc::base::Toolchain::Id::Gcc, "gcc", "as",
"gcc", "g++", "ar", "ld");

buildcc::base::VerifyToolchainConfig config;
config.env_vars.clear();
config.absolute_search_paths.push_back(
(fs::current_path() / "toolchains" / "gcc").string());
config.compiler_version = "10.2.1";

std::vector<std::string> compiler_version{"10.2.1"};
std::vector<std::string> arch{"none"};
buildcc::env::m::CommandExpect_Execute(1, true, &compiler_version, nullptr);
buildcc::env::m::CommandExpect_Execute(1, true, &arch, nullptr);

std::vector<buildcc::base::VerifiedToolchain> verified_toolchains =
gcc.Verify(config);
UT_PRINT(std::to_string(verified_toolchains.size()).c_str());
CHECK_EQUAL(verified_toolchains.size(), 1);
}

TEST(ToolchainTestGroup,
VerifyToolchain_ConditionalAdd_CompilerVersionFailure) {
buildcc::base::Toolchain gcc(buildcc::base::Toolchain::Id::Gcc, "gcc", "as",
"gcc", "g++", "ar", "ld");

buildcc::base::VerifyToolchainConfig config;
config.env_vars.clear();
config.absolute_search_paths.push_back(
(fs::current_path() / "toolchains" / "gcc").string());
config.compiler_version = "11.0.0";

std::vector<std::string> compiler_version{"10.2.1"};
std::vector<std::string> arch{"none"};
buildcc::env::m::CommandExpect_Execute(1, true, &compiler_version, nullptr);
buildcc::env::m::CommandExpect_Execute(1, true, &arch, nullptr);

std::vector<buildcc::base::VerifiedToolchain> verified_toolchains =
gcc.Verify(config);
UT_PRINT(std::to_string(verified_toolchains.size()).c_str());
CHECK_EQUAL(verified_toolchains.size(), 0);
}

TEST(ToolchainTestGroup, VerifyToolchain_ConditionalAdd_TargetArch) {
buildcc::base::Toolchain gcc(buildcc::base::Toolchain::Id::Gcc, "gcc", "as",
"gcc", "g++", "ar", "ld");

buildcc::base::VerifyToolchainConfig config;
config.env_vars.clear();
config.absolute_search_paths.push_back(
(fs::current_path() / "toolchains" / "gcc").string());
config.target_arch = "arm-none-eabi";

std::vector<std::string> compiler_version{"10.2.1"};
std::vector<std::string> arch{"arm-none-eabi"};
buildcc::env::m::CommandExpect_Execute(1, true, &compiler_version, nullptr);
buildcc::env::m::CommandExpect_Execute(1, true, &arch, nullptr);

std::vector<buildcc::base::VerifiedToolchain> verified_toolchains =
gcc.Verify(config);
UT_PRINT(std::to_string(verified_toolchains.size()).c_str());
CHECK_EQUAL(verified_toolchains.size(), 1);
}

TEST(ToolchainTestGroup, VerifyToolchain_ConditionalAdd_TargetArchFailure) {
buildcc::base::Toolchain gcc(buildcc::base::Toolchain::Id::Gcc, "gcc", "as",
"gcc", "g++", "ar", "ld");

buildcc::base::VerifyToolchainConfig config;
config.env_vars.clear();
config.absolute_search_paths.push_back(
(fs::current_path() / "toolchains" / "gcc").string());
config.target_arch = "none";

std::vector<std::string> compiler_version{"10.2.1"};
std::vector<std::string> arch{"arm-none-eabi"};
buildcc::env::m::CommandExpect_Execute(1, true, &compiler_version, nullptr);
buildcc::env::m::CommandExpect_Execute(1, true, &arch, nullptr);

std::vector<buildcc::base::VerifiedToolchain> verified_toolchains =
gcc.Verify(config);
UT_PRINT(std::to_string(verified_toolchains.size()).c_str());
CHECK_EQUAL(verified_toolchains.size(), 0);
}

TEST(ToolchainTestGroup, VerifyToolchain_ConditionalAdd_BothFailure) {
buildcc::base::Toolchain gcc(buildcc::base::Toolchain::Id::Gcc, "gcc", "as",
"gcc", "g++", "ar", "ld");

buildcc::base::VerifyToolchainConfig config;
config.env_vars.clear();
config.absolute_search_paths.push_back(
(fs::current_path() / "toolchains" / "gcc").string());
config.compiler_version = "none";
config.target_arch = "none";

std::vector<std::string> compiler_version{"10.2.1"};
std::vector<std::string> arch{"arm-none-eabi"};
buildcc::env::m::CommandExpect_Execute(1, true, &compiler_version, nullptr);
buildcc::env::m::CommandExpect_Execute(1, true, &arch, nullptr);

std::vector<buildcc::base::VerifiedToolchain> verified_toolchains =
gcc.Verify(config);
UT_PRINT(std::to_string(verified_toolchains.size()).c_str());
CHECK_EQUAL(verified_toolchains.size(), 0);
}

TEST(ToolchainTestGroup, VerifyToolchain_UpdateFalse) {
buildcc::base::Toolchain gcc(buildcc::base::Toolchain::Id::Gcc, "gcc", "as",
"gcc", "g++", "ar", "ld");

buildcc::base::VerifyToolchainConfig config;
config.env_vars.clear();
config.absolute_search_paths.push_back(
(fs::current_path() / "toolchains" / "gcc").string());
// config.compiler_version = "none";
// config.target_arch = "none";
config.update = false;

std::vector<std::string> compiler_version{"10.2.1"};
std::vector<std::string> arch{"arm-none-eabi"};
buildcc::env::m::CommandExpect_Execute(1, true, &compiler_version, nullptr);
buildcc::env::m::CommandExpect_Execute(1, true, &arch, nullptr);

std::vector<buildcc::base::VerifiedToolchain> verified_toolchains =
gcc.Verify(config);
UT_PRINT(std::to_string(verified_toolchains.size()).c_str());
CHECK_EQUAL(verified_toolchains.size(), 1);
}

int main(int ac, char **av) {
buildcc::env::m::VectorStringCopier copier;
mock().installCopier(TEST_VECTOR_STRING_TYPE, copier);
Expand Down