|
21 | 21 | #include <llvm/Option/ArgList.h>
|
22 | 22 | #include <llvm/Option/OptTable.h>
|
23 | 23 | #include <llvm/Support/FileSystem.h>
|
| 24 | +#include <llvm/Support/Program.h> |
| 25 | +#include <llvm/TargetParser/Host.h> |
24 | 26 | #include <ranges>
|
25 | 27 |
|
26 | 28 | namespace clang {
|
@@ -243,8 +245,8 @@ static
|
243 | 245 | std::vector<std::string>
|
244 | 246 | adjustCommandLine(
|
245 | 247 | llvm::StringRef workingDir,
|
246 |
| - const std::vector<std::string>& cmdline, |
247 |
| - const std::vector<std::string>& additional_defines, |
| 248 | + std::vector<std::string> const& cmdline, |
| 249 | + std::vector<std::string> const& additional_defines, |
248 | 250 | std::unordered_map<std::string, std::vector<std::string>> const& implicitIncludeDirectories,
|
249 | 251 | std::vector<std::string> const& stdlibIncludes,
|
250 | 252 | std::vector<std::string> const& systemIncludes,
|
@@ -292,6 +294,72 @@ adjustCommandLine(
|
292 | 294 | new_cmdline.emplace_back(is_clang_cl ? "/w" : "-w");
|
293 | 295 | new_cmdline.emplace_back("-fsyntax-only");
|
294 | 296 |
|
| 297 | + // ------------------------------------------------------ |
| 298 | + // Target architecture |
| 299 | + // ------------------------------------------------------ |
| 300 | + constexpr auto is_target_option = [](std::string_view opt) { |
| 301 | + return opt == "-target" || opt == "--target"; |
| 302 | + }; |
| 303 | + if (std::ranges::find_if(cmdline, is_target_option) == cmdline.end()) |
| 304 | + { |
| 305 | + auto getCommandCompilerTarget = [&]() -> std::string { |
| 306 | + ScopedTempFile const outputPath("compiler-triple", "txt"); |
| 307 | + if (!outputPath) { |
| 308 | + return {}; |
| 309 | + } |
| 310 | + std::vector<llvm::StringRef> args = { |
| 311 | + progName, "--print-target-triple" |
| 312 | + }; |
| 313 | + std::optional<llvm::StringRef> const redirects[] = { |
| 314 | + llvm::StringRef(), |
| 315 | + outputPath.path(), |
| 316 | + llvm::StringRef() |
| 317 | + }; |
| 318 | + int const result = llvm::sys::ExecuteAndWait( |
| 319 | + progName, args, std::nullopt, redirects); |
| 320 | + if (result != 0) |
| 321 | + { |
| 322 | + return {}; |
| 323 | + } |
| 324 | + |
| 325 | + auto const bufferOrError = llvm::MemoryBuffer::getFile( |
| 326 | + outputPath.path()); |
| 327 | + if (!bufferOrError) { |
| 328 | + return {}; |
| 329 | + } |
| 330 | + return bufferOrError.get()->getBuffer().trim().str(); |
| 331 | + }; |
| 332 | + |
| 333 | + [&]() { |
| 334 | + std::string target = llvm::sys::getDefaultTargetTriple(); |
| 335 | + |
| 336 | + if (target.empty()) |
| 337 | + { |
| 338 | + target = llvm::sys::getProcessTriple(); |
| 339 | + } |
| 340 | + |
| 341 | + if (target.empty()) |
| 342 | + { |
| 343 | + target = getCommandCompilerTarget(); |
| 344 | + } |
| 345 | + |
| 346 | +#if defined(__APPLE__) |
| 347 | + if (target.empty()) |
| 348 | + { |
| 349 | + target = "arm64-apple-darwin24.0.0"; |
| 350 | + } |
| 351 | +#else |
| 352 | + if (target.empty()) |
| 353 | + { |
| 354 | + return; |
| 355 | + } |
| 356 | +#endif |
| 357 | + |
| 358 | + new_cmdline.emplace_back("-target"); |
| 359 | + new_cmdline.emplace_back(target); |
| 360 | + }(); |
| 361 | + } |
| 362 | + |
295 | 363 | // ------------------------------------------------------
|
296 | 364 | // Add additional defines
|
297 | 365 | // ------------------------------------------------------
|
|
0 commit comments