Skip to content

Commit a3d2c7f

Browse files
authored
Toolchain find and verify (#199)
- Reworked Toolchain::Find and Toolchain::Verify APIs - Added support for Custom Toolchain verification
1 parent cc5ad25 commit a3d2c7f

31 files changed

+1008
-574
lines changed

buildcc/lib/args/include/args/args.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,32 @@ struct ArgToolchainState {
4848
struct ArgToolchain {
4949
ArgToolchain(){};
5050
ArgToolchain(ToolchainId initial_id, const std::string &initial_name,
51-
const ToolchainBinaries &initial_binaries)
52-
: id(initial_id), name(initial_name), binaries(initial_binaries) {}
51+
const ToolchainExecutables &initial_executables)
52+
: id(initial_id), name(initial_name), executables(initial_executables) {}
5353
ArgToolchain(ToolchainId initial_id, const std::string &initial_name,
5454
const std::string &initial_assembler,
5555
const std::string &initial_c_compiler,
5656
const std::string &initial_cpp_compiler,
5757
const std::string &initial_archiver,
5858
const std::string &initial_linker)
5959
: ArgToolchain(initial_id, initial_name,
60-
ToolchainBinaries(initial_assembler, initial_c_compiler,
61-
initial_cpp_compiler, initial_archiver,
62-
initial_linker)) {}
60+
ToolchainExecutables(initial_assembler, initial_c_compiler,
61+
initial_cpp_compiler,
62+
initial_archiver, initial_linker)) {}
6363

6464
/**
6565
* @brief Construct a BaseToolchain from the arguments supplied through the
6666
* command line information
6767
*/
6868
// TODO, Update this for lock and ToolchainConfig
6969
BaseToolchain ConstructToolchain() const {
70-
return BaseToolchain(id, name, binaries);
70+
return BaseToolchain(id, name, executables);
7171
}
7272

7373
ArgToolchainState state;
7474
ToolchainId id{ToolchainId::Undefined};
7575
std::string name{""};
76-
ToolchainBinaries binaries;
76+
ToolchainExecutables executables;
7777
};
7878

7979
// NOTE, Incomplete without pch_compile_command

buildcc/lib/args/src/args.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ void Args::AddToolchain(const std::string &name, const std::string &description,
9999
->transform(CLI::CheckedTransformer(kToolchainIdMap, CLI::ignore_case))
100100
->default_val(initial.id);
101101
t_user->add_option(kToolchainNameParam, out.name)->default_val(initial.name);
102-
t_user->add_option(kToolchainAsmCompilerParam, out.binaries.assembler)
103-
->default_val(initial.binaries.assembler);
104-
t_user->add_option(kToolchainCCompilerParam, out.binaries.c_compiler)
105-
->default_val(initial.binaries.c_compiler);
106-
t_user->add_option(kToolchainCppCompilerParam, out.binaries.cpp_compiler)
107-
->default_val(initial.binaries.cpp_compiler);
108-
t_user->add_option(kToolchainArchiverParam, out.binaries.archiver)
109-
->default_val(initial.binaries.archiver);
110-
t_user->add_option(kToolchainLinkerParam, out.binaries.linker)
111-
->default_val(initial.binaries.linker);
102+
t_user->add_option(kToolchainAsmCompilerParam, out.executables.assembler)
103+
->default_val(initial.executables.assembler);
104+
t_user->add_option(kToolchainCCompilerParam, out.executables.c_compiler)
105+
->default_val(initial.executables.c_compiler);
106+
t_user->add_option(kToolchainCppCompilerParam, out.executables.cpp_compiler)
107+
->default_val(initial.executables.cpp_compiler);
108+
t_user->add_option(kToolchainArchiverParam, out.executables.archiver)
109+
->default_val(initial.executables.archiver);
110+
t_user->add_option(kToolchainLinkerParam, out.executables.linker)
111+
->default_val(initial.executables.linker);
112112
}
113113

114114
void Args::AddTarget(const std::string &name, const std::string &description,

buildcc/lib/args/test/test_args.cpp

+30-30
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ TEST(ArgsTestGroup, Args_CustomToolchain) {
6868
CHECK_FALSE(gcc_toolchain.state.test);
6969
CHECK(gcc_toolchain.id == buildcc::ToolchainId::Gcc);
7070
STRCMP_EQUAL(gcc_toolchain.name.c_str(), "gcc");
71-
STRCMP_EQUAL(gcc_toolchain.binaries.assembler.c_str(), "as");
72-
STRCMP_EQUAL(gcc_toolchain.binaries.c_compiler.c_str(), "gcc");
73-
STRCMP_EQUAL(gcc_toolchain.binaries.cpp_compiler.c_str(), "g++");
74-
STRCMP_EQUAL(gcc_toolchain.binaries.archiver.c_str(), "ar");
75-
STRCMP_EQUAL(gcc_toolchain.binaries.linker.c_str(), "ld");
71+
STRCMP_EQUAL(gcc_toolchain.executables.assembler.c_str(), "as");
72+
STRCMP_EQUAL(gcc_toolchain.executables.c_compiler.c_str(), "gcc");
73+
STRCMP_EQUAL(gcc_toolchain.executables.cpp_compiler.c_str(), "g++");
74+
STRCMP_EQUAL(gcc_toolchain.executables.archiver.c_str(), "ar");
75+
STRCMP_EQUAL(gcc_toolchain.executables.linker.c_str(), "ld");
7676
}
7777

7878
TEST(ArgsTestGroup, Args_MultipleCustomToolchain) {
@@ -106,22 +106,22 @@ TEST(ArgsTestGroup, Args_MultipleCustomToolchain) {
106106
CHECK_FALSE(gcc_toolchain.state.test);
107107
CHECK(gcc_toolchain.id == buildcc::ToolchainId::Gcc);
108108
STRCMP_EQUAL(gcc_toolchain.name.c_str(), "gcc");
109-
STRCMP_EQUAL(gcc_toolchain.binaries.assembler.c_str(), "as");
110-
STRCMP_EQUAL(gcc_toolchain.binaries.c_compiler.c_str(), "gcc");
111-
STRCMP_EQUAL(gcc_toolchain.binaries.cpp_compiler.c_str(), "g++");
112-
STRCMP_EQUAL(gcc_toolchain.binaries.archiver.c_str(), "ar");
113-
STRCMP_EQUAL(gcc_toolchain.binaries.linker.c_str(), "ld");
109+
STRCMP_EQUAL(gcc_toolchain.executables.assembler.c_str(), "as");
110+
STRCMP_EQUAL(gcc_toolchain.executables.c_compiler.c_str(), "gcc");
111+
STRCMP_EQUAL(gcc_toolchain.executables.cpp_compiler.c_str(), "g++");
112+
STRCMP_EQUAL(gcc_toolchain.executables.archiver.c_str(), "ar");
113+
STRCMP_EQUAL(gcc_toolchain.executables.linker.c_str(), "ld");
114114

115115
// MSVC
116116
CHECK_TRUE(msvc_toolchain.state.build);
117117
CHECK_TRUE(msvc_toolchain.state.test);
118118
CHECK(msvc_toolchain.id == buildcc::ToolchainId::Msvc);
119119
STRCMP_EQUAL(msvc_toolchain.name.c_str(), "msvc");
120-
STRCMP_EQUAL(msvc_toolchain.binaries.assembler.c_str(), "cl");
121-
STRCMP_EQUAL(msvc_toolchain.binaries.c_compiler.c_str(), "cl");
122-
STRCMP_EQUAL(msvc_toolchain.binaries.cpp_compiler.c_str(), "cl");
123-
STRCMP_EQUAL(msvc_toolchain.binaries.archiver.c_str(), "lib");
124-
STRCMP_EQUAL(msvc_toolchain.binaries.linker.c_str(), "link");
120+
STRCMP_EQUAL(msvc_toolchain.executables.assembler.c_str(), "cl");
121+
STRCMP_EQUAL(msvc_toolchain.executables.c_compiler.c_str(), "cl");
122+
STRCMP_EQUAL(msvc_toolchain.executables.cpp_compiler.c_str(), "cl");
123+
STRCMP_EQUAL(msvc_toolchain.executables.archiver.c_str(), "lib");
124+
STRCMP_EQUAL(msvc_toolchain.executables.linker.c_str(), "link");
125125
}
126126

127127
TEST(ArgsTestGroup, Args_DuplicateCustomToolchain) {
@@ -167,11 +167,11 @@ TEST(ArgsTestGroup, Args_CustomTarget) {
167167
CHECK_FALSE(gcc_toolchain.state.test);
168168
CHECK(gcc_toolchain.id == buildcc::ToolchainId::Gcc);
169169
STRCMP_EQUAL(gcc_toolchain.name.c_str(), "gcc");
170-
STRCMP_EQUAL(gcc_toolchain.binaries.assembler.c_str(), "as");
171-
STRCMP_EQUAL(gcc_toolchain.binaries.c_compiler.c_str(), "gcc");
172-
STRCMP_EQUAL(gcc_toolchain.binaries.cpp_compiler.c_str(), "g++");
173-
STRCMP_EQUAL(gcc_toolchain.binaries.archiver.c_str(), "ar");
174-
STRCMP_EQUAL(gcc_toolchain.binaries.linker.c_str(), "ld");
170+
STRCMP_EQUAL(gcc_toolchain.executables.assembler.c_str(), "as");
171+
STRCMP_EQUAL(gcc_toolchain.executables.c_compiler.c_str(), "gcc");
172+
STRCMP_EQUAL(gcc_toolchain.executables.cpp_compiler.c_str(), "g++");
173+
STRCMP_EQUAL(gcc_toolchain.executables.archiver.c_str(), "ar");
174+
STRCMP_EQUAL(gcc_toolchain.executables.linker.c_str(), "ld");
175175

176176
// Target
177177
STRCMP_EQUAL(gcc_target.compile_command.c_str(),
@@ -221,11 +221,11 @@ TEST(ArgsTestGroup, Args_MultipleCustomTarget) {
221221
CHECK_FALSE(gcc_toolchain.state.test);
222222
CHECK(gcc_toolchain.id == buildcc::ToolchainId::Gcc);
223223
STRCMP_EQUAL(gcc_toolchain.name.c_str(), "gcc");
224-
STRCMP_EQUAL(gcc_toolchain.binaries.assembler.c_str(), "as");
225-
STRCMP_EQUAL(gcc_toolchain.binaries.c_compiler.c_str(), "gcc");
226-
STRCMP_EQUAL(gcc_toolchain.binaries.cpp_compiler.c_str(), "g++");
227-
STRCMP_EQUAL(gcc_toolchain.binaries.archiver.c_str(), "ar");
228-
STRCMP_EQUAL(gcc_toolchain.binaries.linker.c_str(), "ld");
224+
STRCMP_EQUAL(gcc_toolchain.executables.assembler.c_str(), "as");
225+
STRCMP_EQUAL(gcc_toolchain.executables.c_compiler.c_str(), "gcc");
226+
STRCMP_EQUAL(gcc_toolchain.executables.cpp_compiler.c_str(), "g++");
227+
STRCMP_EQUAL(gcc_toolchain.executables.archiver.c_str(), "ar");
228+
STRCMP_EQUAL(gcc_toolchain.executables.linker.c_str(), "ld");
229229

230230
// Target
231231
STRCMP_EQUAL(gcc_target.compile_command.c_str(),
@@ -242,11 +242,11 @@ TEST(ArgsTestGroup, Args_MultipleCustomTarget) {
242242
CHECK_TRUE(msvc_toolchain.state.test);
243243
CHECK(msvc_toolchain.id == buildcc::ToolchainId::Msvc);
244244
STRCMP_EQUAL(msvc_toolchain.name.c_str(), "msvc");
245-
STRCMP_EQUAL(msvc_toolchain.binaries.assembler.c_str(), "cl");
246-
STRCMP_EQUAL(msvc_toolchain.binaries.c_compiler.c_str(), "cl");
247-
STRCMP_EQUAL(msvc_toolchain.binaries.cpp_compiler.c_str(), "cl");
248-
STRCMP_EQUAL(msvc_toolchain.binaries.archiver.c_str(), "lib");
249-
STRCMP_EQUAL(msvc_toolchain.binaries.linker.c_str(), "link");
245+
STRCMP_EQUAL(msvc_toolchain.executables.assembler.c_str(), "cl");
246+
STRCMP_EQUAL(msvc_toolchain.executables.c_compiler.c_str(), "cl");
247+
STRCMP_EQUAL(msvc_toolchain.executables.cpp_compiler.c_str(), "cl");
248+
STRCMP_EQUAL(msvc_toolchain.executables.archiver.c_str(), "lib");
249+
STRCMP_EQUAL(msvc_toolchain.executables.linker.c_str(), "link");
250250

251251
// Target
252252
STRCMP_EQUAL(msvc_target.compile_command.c_str(),

buildcc/lib/target/test/target/test_toolchain_flag_api.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,6 @@ TEST(ToolchainFlagApiTestGroup, BasicTargetTest) {
8585
}
8686

8787
int main(int ac, char **av) {
88+
MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
8889
return CommandLineTestRunner::RunAllTests(ac, av);
8990
}

buildcc/lib/toolchain/CMakeLists.txt

+26-8
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ set(TOOLCHAIN_SRCS
66
include/toolchain/common/function_lock.h
77

88
# API
9+
src/api/toolchain_find.cpp
10+
src/api/toolchain_verify.cpp
11+
include/toolchain/api/toolchain_find.h
12+
include/toolchain/api/toolchain_verify.h
913
include/toolchain/api/flag_api.h
1014

1115
src/toolchain/toolchain.cpp
1216
include/toolchain/toolchain.h
13-
14-
# Features
15-
src/api/toolchain_verify.cpp
16-
include/toolchain/api/toolchain_verify.h
1717
)
1818
if (${TESTING})
1919
add_library(mock_toolchain
@@ -32,10 +32,10 @@ if (${TESTING})
3232
${TEST_LINK_LIBS}
3333
)
3434

35-
add_executable(test_toolchain_verify
36-
test/test_toolchain_verify.cpp
35+
add_executable(test_toolchain_id
36+
test/test_toolchain_id.cpp
3737
)
38-
target_link_libraries(test_toolchain_verify PRIVATE
38+
target_link_libraries(test_toolchain_id PRIVATE
3939
mock_toolchain
4040
)
4141

@@ -46,10 +46,28 @@ if (${TESTING})
4646
mock_toolchain
4747
)
4848

49+
add_executable(test_toolchain_find
50+
test/test_toolchain_find.cpp
51+
)
52+
target_link_libraries(test_toolchain_find PRIVATE
53+
mock_toolchain
54+
)
55+
56+
add_executable(test_toolchain_verify
57+
test/test_toolchain_verify.cpp
58+
)
59+
target_link_libraries(test_toolchain_verify PRIVATE
60+
mock_toolchain
61+
)
62+
63+
add_test(NAME test_toolchain_id COMMAND test_toolchain_id)
64+
add_test(NAME test_toolchain_config COMMAND test_toolchain_config)
65+
add_test(NAME test_toolchain_find COMMAND test_toolchain_find
66+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
67+
)
4968
add_test(NAME test_toolchain_verify COMMAND test_toolchain_verify
5069
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
5170
)
52-
add_test(NAME test_toolchain_config COMMAND test_toolchain_config)
5371
endif()
5472

5573
if(${BUILDCC_BUILD_AS_SINGLE_LIB})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2021-2022 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 TOOLCHAIN_TOOLCHAIN_FIND_H_
18+
#define TOOLCHAIN_TOOLCHAIN_FIND_H_
19+
20+
#include <filesystem>
21+
#include <string>
22+
#include <unordered_set>
23+
#include <vector>
24+
25+
#include "schema/path.h"
26+
27+
namespace fs = std::filesystem;
28+
29+
namespace buildcc {
30+
31+
/**
32+
* @brief Configure the behaviour of Toolchain::Find API. By default searches
33+
* the directories mentioned in the ENV{PATH} variable to find the toolchain.
34+
* @param absolute_search_paths absolute_search_paths expect directories that
35+
* are iterated for exact toolchain matches
36+
* @param env_vars env_vars contain paths that are seperated by OS delimiter.
37+
* These are converted to paths and searched similarly to absolute_search_paths
38+
* <br>
39+
* NOTE: env_vars must contain single absolute paths or multiple absolute
40+
* paths seperated by OS delimiter <br>
41+
* Example: [Windows] "absolute_path_1;absolute_path_2;..." <br>
42+
* Example: [Linux] "absolute_path_1:absolute_path_2:..." <br>
43+
*/
44+
struct ToolchainFindConfig {
45+
ToolchainFindConfig(
46+
const std::unordered_set<std::string> &env_vars = {"PATH"},
47+
const fs_unordered_set &absolute_search_paths = {})
48+
: env_vars(env_vars), absolute_search_paths(absolute_search_paths) {}
49+
50+
std::unordered_set<std::string> env_vars;
51+
fs_unordered_set absolute_search_paths;
52+
};
53+
54+
template <typename T> class ToolchainFind {
55+
public:
56+
std::vector<fs::path>
57+
Find(const ToolchainFindConfig &config = ToolchainFindConfig()) const;
58+
};
59+
60+
} // namespace buildcc
61+
62+
#endif

0 commit comments

Comments
 (0)