fix(lint): look for libc++ where the compiler does
In crafter-build's Flatpak, whose compiler keeps libc++ in /app and whose /usr has none, a file that includes a header of the C++ standard library could not be parsed for the lint rules that read its AST in a build like that of #46: the file built, and `crafter-build lint` then ran none of those rules on it, printed "'algorithm' file not found" and exited 1. libclang parses through a clang driver of its own. In a build for the host whose compile command names no libc++ directory, as #46's names none, where a sysroot or CRAFTER_BUILD_LIBCXX_DIR would name one, that driver looks for libc++'s headers under ../include from the directory of its argv[0], then under /usr/local/include and /usr/include. ParseAst gave libclang the compile command without its compiler, and clang_parseTranslationUnit names argv[0] plain "clang", in no directory. So the parse of such a build, natively as in the Flatpak, took the first libc++ it found under ../include from the working directory, /usr/local/include and /usr/include, whichever compiler the build used. The -resource-dir ParseAst passes is for clang's own headers and changes nothing for libc++. The compile command's argv[0] would not do on its own: a clang++ from PATH is the bare name clang++, in no directory either, and a clang++ reached through a link into another directory takes itself to be where the link leads, as clang finds its installation from the file it runs as. The compiler says where that is, on the InstalledDir line of `clang++ --version`, which GetToolchain() already runs for clangVersion. Toolchain::installedDir keeps it, InstalledClangxx(command) names the compiler in it, and ParseAst passes that as argv[0], through clang_parseTranslationUnit2FullArgv, which takes argv[0] from its caller. With argv[0] there, the parse finds libc++ where the compile does, whatever the working directory. A compile command can have the compiler look from elsewhere: with -no-canonical-prefixes, where the last of it and -canonical-prefixes is that one, clang takes itself to be at clangxx as written, or, where no file is there, as is usual for a bare name, at the one PATH gives, a link not followed, and `clang++ -no-canonical-prefixes --version` names that directory. GetToolchain() runs that too, Toolchain::installedDirAsWritten keeps what it names, and InstalledClangxx(command) names the compiler there for a command whose last prefix flag is -no-canonical-prefixes, as clang's own driver reads them. argv[0] also gives libclang's driver its mode, by its file name, and the configuration files it reads by default, from its directory. The parse now runs in the g++ mode the compile runs in, where it ran in clang's gcc mode, and reads the default configuration files beside the compiler that the compile reads, clang++.cfg among them; before, it read none from there, as the driver skips the directory of an argv[0] that has none. -resource-dir stays, for a libclang of another release than the compiler, whose driver would derive the directory from argv[0] for its own release. The comment on it said libclang found clang's own headers relative to its own install path; its driver derived them from argv[0], as it does libc++'s. Fixes #46
This commit is contained in:
parent
578a1da600
commit
8019f9d44f
3 changed files with 87 additions and 8 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue