diff --git a/implementations/Crafter.Build-Lint.cpp b/implementations/Crafter.Build-Lint.cpp index 948b9e4..f546ea9 100644 --- a/implementations/Crafter.Build-Lint.cpp +++ b/implementations/Crafter.Build-Lint.cpp @@ -43,6 +43,7 @@ namespace { decltype(&clang_createIndex) createIndex = nullptr; decltype(&clang_disposeIndex) disposeIndex = nullptr; decltype(&clang_parseTranslationUnit) parseTranslationUnit = nullptr; + decltype(&clang_parseTranslationUnit2FullArgv) parseTranslationUnit2FullArgv = nullptr; decltype(&clang_disposeTranslationUnit) disposeTranslationUnit = nullptr; decltype(&clang_getFile) getFile = nullptr; decltype(&clang_getLocationForOffset) getLocationForOffset = nullptr; @@ -133,6 +134,7 @@ namespace { bind(lib.createIndex, "clang_createIndex"); bind(lib.disposeIndex, "clang_disposeIndex"); bind(lib.parseTranslationUnit, "clang_parseTranslationUnit"); + bind(lib.parseTranslationUnit2FullArgv, "clang_parseTranslationUnit2FullArgv"); bind(lib.disposeTranslationUnit, "clang_disposeTranslationUnit"); bind(lib.getFile, "clang_getFile"); bind(lib.getLocationForOffset, "clang_getLocationForOffset"); @@ -705,11 +707,12 @@ namespace { return decls; } - // libclang locates its builtin headers relative to its own install path, - // which need not agree with the clang++ on PATH that produced the PCMs. - // When it disagrees the failure is total and unhelpful — every parse dies - // on "'stddef.h' file not found" — so ask the driver and pass it - // explicitly. Cached like HostTarget, and for the same reason. + // The resource directory, which holds clang's own headers such as + // stddef.h, as the compiler names it. libclang's driver would otherwise + // derive one from argv[0] for libclang's own release, so with a libclang + // of another release than the compiler, a file that includes a header of + // the C++ standard library would fail on "'stddef.h' file not found". + // Cached like HostTarget, and for the same reason. const std::string& ClangResourceDir() { static const std::string Cached = []() -> std::string { CommandResult r = RunCommandChecked(std::format("{} -print-resource-dir", GetToolchain().clangxx.string())); @@ -752,7 +755,26 @@ namespace { // Byte-length preserving, so cursor line/column land on the original. std::string buffer = BlankExportKeywords(content, tokens); std::string path = file.string(); - std::vector args = CommandToArgs(compileCommand); + // In a build for the host whose compile command names no libc++ + // directory (a sysroot or CRAFTER_BUILD_LIBCXX_DIR names one), the + // driver inside libclang looks for libc++'s headers in ../include from + // the directory of argv[0], as the compiler's driver does from its + // own, and then in /usr/local/include and in /usr/include. Left to + // name argv[0] itself, libclang names it plain "clang", in no + // directory, so the first of those was ../include from the working + // directory, and where none of them had a libc++, as in the Flatpak, + // whose compiler keeps its own in /app, no file of such a build that + // includes a header of the C++ standard library could be parsed + // (issue #46). The compile command's own argv[0] would not do either: + // a clang++ from PATH is a bare name, and one reached through a link + // into another directory takes itself to be where the link leads, + // unless the command's last prefix flag is -no-canonical-prefixes. So + // argv[0] is the compiler where clang started with this command takes + // itself to be, in the directory it says it is installed in when run + // with the command's last prefix flag, or, where it names none, + // clangxx as the command has it (Toolchain::InstalledClangxx). + std::vector args = { GetToolchain().InstalledClangxx(compileCommand).string() }; + std::ranges::move(CommandToArgs(compileCommand), std::back_inserter(args)); // Required: libclang will not infer a module interface unit from the // .cppm extension, and silently treats every flag as a linker input if // left to guess. @@ -772,8 +794,8 @@ namespace { result.error = "clang_createIndex failed"; return result; } - CXTranslationUnit tu = lc.parseTranslationUnit(index, path.c_str(), argv.data(), static_cast(argv.size()), &unsaved, 1, CXTranslationUnit_None); - if (!tu) { + CXTranslationUnit tu = nullptr; + if (lc.parseTranslationUnit2FullArgv(index, path.c_str(), argv.data(), static_cast(argv.size()), &unsaved, 1, CXTranslationUnit_None, &tu) != CXError_Success || !tu) { lc.disposeIndex(index); result.error = "clang could not create a translation unit"; return result; diff --git a/implementations/Crafter.Build-Toolchain.cpp b/implementations/Crafter.Build-Toolchain.cpp index 0edbd84..abb8525 100644 --- a/implementations/Crafter.Build-Toolchain.cpp +++ b/implementations/Crafter.Build-Toolchain.cpp @@ -151,6 +151,24 @@ namespace { std::atomic ViewMade = false; } +fs::path Toolchain::InstalledClangxx(std::string_view command) const { + // Every word of the command, as clang's driver.cpp reads its arguments + // before it names its own path; the build quotes neither flag. + bool canonical = true; + for (std::size_t at = 0; at < command.size();) { + const std::size_t end = std::min(command.find(' ', at), command.size()); + const std::string_view word = command.substr(at, end - at); + if (word == "-canonical-prefixes") { + canonical = true; + } else if (word == "-no-canonical-prefixes") { + canonical = false; + } + at = end + 1; + } + const fs::path& dir = canonical ? installedDir : installedDirAsWritten; + return dir.empty() ? clangxx : dir / clangxx.filename(); +} + std::string Toolchain::LibcxxCompileFlags() const { if (libcxxDir.empty()) return {}; return std::format(" -nostdinc++ -isystem {}", (libcxxDir / "include" / "c++" / "v1").string()); @@ -398,11 +416,22 @@ const Toolchain& Crafter::GetToolchain() { return tc; } tc.hostTriple = Trimmed(std::move(triple.output)); + // The InstalledDir line of a `--version` run; empty where the run + // failed or printed none. + const auto installedDirOf = [](const CommandResult& run) { + constexpr std::string_view InstalledDirLine = "\nInstalledDir: "; + const std::size_t at = run.output.find(InstalledDirLine); + if (run.exitCode != 0 || at == std::string::npos) return fs::path(); + const std::size_t begin = at + InstalledDirLine.size(); + return fs::path(Trimmed(run.output.substr(begin, run.output.find('\n', begin) - begin))); + }; CommandResult version = RunCommandChecked(std::format("{} --version", tc.clangxx.string())); if (version.exitCode == 0) { std::string first = version.output.substr(0, version.output.find('\n')); tc.clangVersion = Trimmed(std::move(first)); } + tc.installedDir = installedDirOf(version); + tc.installedDirAsWritten = installedDirOf(RunCommandChecked(std::format("{} -no-canonical-prefixes --version", tc.clangxx.string()))); return tc; }(); return Resolved; diff --git a/interfaces/Crafter.Build-Toolchain.cppm b/interfaces/Crafter.Build-Toolchain.cppm index 24cb930..aa52d6d 100644 --- a/interfaces/Crafter.Build-Toolchain.cppm +++ b/interfaces/Crafter.Build-Toolchain.cppm @@ -100,9 +100,37 @@ export namespace Crafter { // `clang++ --version`; both empty when clang++ could not be run. std::string hostTriple; std::string clangVersion; + // The directory clang++ is installed in, from the InstalledDir line + // of that `clang++ --version`. clang names the directory of the file + // it runs from, so for a clang++ found on PATH, or reached through a + // link into another directory, it is where the compiler really is, + // the directory its driver looks for headers and configuration files + // from (InstalledClangxx). Empty when clang++ could not be run or + // named no directory. + fs::path installedDir; + // The same line of `clang++ -no-canonical-prefixes --version`. With + // that flag clang names the directory of clang++ as written, or, where + // no file is there, as is usual for a bare name, of the one PATH + // gives, a link not followed, and a compile whose last prefix flag is + // that one looks from there. Empty as installedDir is. + fs::path installedDirAsWritten; // Why clang++ could not be resolved or run; empty when it could. std::string error; + // clangxx as a program that runs a clang driver of its own over + // `command`, a compile command that starts with clangxx, has to be + // given it: the same file name in the directory clang started with + // that command takes itself to be in, installedDirAsWritten where the + // last of -canonical-prefixes and -no-canonical-prefixes in the + // command is -no-canonical-prefixes, as clang's own driver decides it, + // and installedDir otherwise. That driver takes the directory of the + // compiler it is given as written, without following a link to where + // the compiler is, and looks from there for what the compiler's own + // driver looks for from its directory: clang's own headers where it is + // not told where they are, libc++'s in a build for the host whose + // command names no libc++ directory, and its default configuration + // files. clangxx itself where that directory is empty. + fs::path InstalledClangxx(std::string_view command) const; // Flags for compiling against libcxxDir; empty without one. std::string LibcxxCompileFlags() const; // Where libc++.so.1 is when clang would not find it itself: an