Skip to content

Commit 9871794

Browse files
committed
refactor: compilation database ensures target
1 parent d92de4e commit 9871794

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

src/lib/Lib/CMakeExecution.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ parseBashArgs(std::string_view str)
383383
return args;
384384
}
385385

386+
/* Pushes the CMake arguments to the `args` vector, replacing the
387+
* default generator with Ninja if Visual Studio is the default generator.
388+
*/
386389
Expected<void>
387390
pushCMakeArgs(
388391
std::string const& cmakePath,

src/lib/Lib/MrDocsCompilationDatabase.cpp

+70-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <llvm/Option/ArgList.h>
2222
#include <llvm/Option/OptTable.h>
2323
#include <llvm/Support/FileSystem.h>
24+
#include <llvm/Support/Program.h>
25+
#include <llvm/TargetParser/Host.h>
2426
#include <ranges>
2527

2628
namespace clang {
@@ -243,8 +245,8 @@ static
243245
std::vector<std::string>
244246
adjustCommandLine(
245247
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,
248250
std::unordered_map<std::string, std::vector<std::string>> const& implicitIncludeDirectories,
249251
std::vector<std::string> const& stdlibIncludes,
250252
std::vector<std::string> const& systemIncludes,
@@ -292,6 +294,72 @@ adjustCommandLine(
292294
new_cmdline.emplace_back(is_clang_cl ? "/w" : "-w");
293295
new_cmdline.emplace_back("-fsyntax-only");
294296

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+
295363
// ------------------------------------------------------
296364
// Add additional defines
297365
// ------------------------------------------------------

src/lib/Support/Error.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ call_impl(
258258
e->location().line());
259259
}
260260
os << fmt::format(
261-
" Reported From: `{}` at line {}",
261+
" Reported From: `{}` at line {}\n",
262262
::SourceFileNames::getFileName(loc->file_name()),
263263
loc->line());
264264
// VFALCO attach a stack trace for Level::fatal

0 commit comments

Comments
 (0)