-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeConfiguration.cpp
More file actions
190 lines (155 loc) · 4.82 KB
/
Copy pathCMakeConfiguration.cpp
File metadata and controls
190 lines (155 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
*
* @file BuildTools.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
* Build graph task model
*
*/
#include <vix/engine/CMakeConfiguration.hpp>
#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <optional>
#include <string>
#include <system_error>
#include "ExecutableLookup.hpp"
namespace vix::engine
{
namespace
{
std::string warning_check_cxx_compiler()
{
const char *cxx = std::getenv("CXX");
if (cxx && *cxx)
return cxx;
if (detail::executable_on_path("clang++"))
return "clang++";
if (detail::executable_on_path("g++"))
return "g++";
return "c++";
}
std::optional<std::string> warning_check_c_compiler(
const std::string &cxxCompiler)
{
const char *cc = std::getenv("CC");
if (cc && *cc)
return std::string(cc);
if (cxxCompiler == "clang++" && detail::executable_on_path("clang"))
return std::string("clang");
if (cxxCompiler == "g++" && detail::executable_on_path("gcc"))
return std::string("gcc");
return std::nullopt;
}
bool warning_check_uses_clang(const std::string &compiler)
{
return compiler.find("clang") != std::string::npos;
}
std::string warning_check_cxx_flags(
const std::string &compiler)
{
std::string flags =
"-Wall "
"-Wextra "
"-Wpedantic "
"-Wshadow "
"-Wconversion "
"-Wsign-conversion "
"-Wformat=2 "
"-Wold-style-cast "
"-Woverloaded-virtual";
if (warning_check_uses_clang(compiler))
{
flags +=
" -Wlogical-op-parentheses"
" -Wunreachable-code";
}
return flags;
}
} // namespace
std::vector<CMakeVariable> make_cmake_variables(
const CMakeConfigurationOptions &options)
{
std::vector<CMakeVariable> vars;
vars.reserve(32);
vars.emplace_back("CMAKE_BUILD_TYPE", options.buildType);
vars.emplace_back("CMAKE_EXPORT_COMPILE_COMMANDS", "ON");
#ifndef _WIN32
{
std::error_code ec;
if (std::filesystem::exists("/usr/bin/ar", ec) && !ec)
vars.emplace_back("CMAKE_AR", "/usr/bin/ar");
}
{
std::error_code ec;
if (std::filesystem::exists("/usr/bin/ranlib", ec) && !ec)
vars.emplace_back("CMAKE_RANLIB", "/usr/bin/ranlib");
}
#endif
if (options.warningCheck)
{
const std::string cxxCompiler = warning_check_cxx_compiler();
const std::optional<std::string> cCompiler =
warning_check_c_compiler(cxxCompiler);
vars.emplace_back("VIX_ENABLE_WARNINGS", "ON");
vars.emplace_back(
"CMAKE_CXX_FLAGS",
warning_check_cxx_flags(cxxCompiler));
if (!std::getenv("CXX"))
vars.emplace_back("CMAKE_CXX_COMPILER", cxxCompiler);
if (!std::getenv("CC") && cCompiler)
vars.emplace_back("CMAKE_C_COMPILER", *cCompiler);
}
if (!options.targetTriple.empty())
vars.emplace_back("CMAKE_TOOLCHAIN_FILE", options.toolchainFile.string());
if (options.linkStatic)
vars.emplace_back("VIX_LINK_STATIC", "ON");
if (!options.targetTriple.empty())
vars.emplace_back("VIX_TARGET_TRIPLE", options.targetTriple);
(void)options.globalPackagesFile;
if (options.dependencyEnvironmentMode == DependencyEnvironmentMode::ManagedSdk &&
!options.sdkConfigDir.empty())
{
vars.emplace_back("Vix_DIR", options.sdkConfigDir.string());
vars.emplace_back("vix_DIR", options.sdkConfigDir.string());
}
if (options.launcher && !options.launcher->empty())
{
vars.emplace_back("CMAKE_C_COMPILER_LAUNCHER", *options.launcher);
vars.emplace_back("CMAKE_CXX_COMPILER_LAUNCHER", *options.launcher);
}
if (options.fastLinkerFlag && !options.fastLinkerFlag->empty())
{
vars.emplace_back("CMAKE_EXE_LINKER_FLAGS", *options.fastLinkerFlag);
vars.emplace_back("CMAKE_SHARED_LINKER_FLAGS", *options.fastLinkerFlag);
vars.emplace_back("CMAKE_MODULE_LINKER_FLAGS", *options.fastLinkerFlag);
}
if (options.withSqlite)
{
vars.emplace_back("VIX_ENABLE_DB", "ON");
vars.emplace_back("VIX_DB_USE_SQLITE", "ON");
vars.emplace_back("VIX_DB_REQUIRE_SQLITE", "ON");
}
if (options.withMySql)
{
vars.emplace_back("VIX_ENABLE_DB", "ON");
vars.emplace_back("VIX_DB_USE_MYSQL", "ON");
vars.emplace_back("VIX_DB_REQUIRE_MYSQL", "ON");
}
std::sort(
vars.begin(),
vars.end(),
[](const auto &a, const auto &b)
{
return a.first < b.first;
});
return vars;
}
} // namespace vix::engine