fix: Don't force pull MacOs libcxx's header

As a fix for issue #247 we added a block of hard-coded paths to the
include search path, which would find libc++ from the MacOS SDK.

While the MacOS SDK is usually present where IWYU is run, on developer
machines, there may be any number of other sysroots providing libc++
that users would prefer to use.

The Nix tool is one such player, where builds are isolated and all
dependencies are gathered in a single root for reproducibility.

Recent experiments with Clang on different Mac systems show that:

* if the MacOS SDK is present, IWYU picks it up automatically via the
  Clang driver
* if the MacOS SDK is not present, no include paths are conjured out of
  thin air
* Clang installed from Homebrew llvm package automatically finds the
  libc++ also provided as part of that package

It seems only IWYU forcefully adds magic paths, and it causes
problems for folks who want to do the right thing and build the sysroot
themselves. So remove these hard-coded paths.

If there is no installation of libc++ discoverable to Clang's probing,
or an alternative libc++ is desired, users can disable probing and
specify relevant paths directly on command-line:

  # for Homebrew version of llvm-14
  include-what-you-use -nostdinc++ \
      -isystem /opt/homebrew/opt/llvm\@14/include/c++/v1 \
      sourcefile.cc

  # for Apple Command Line Tools version of libc++
  include-what-you-use -nostdinc++ \
      -isystem /Library/Developer/CommandLineTools/usr/include/c++/v1 \
      sourcefile.cc

The mechanics of this technique is described in more detail for Clang:
https://libcxx.llvm.org//UsingLibcxx.html#using-a-custom-built-libc.

Co-authored-by: Kim Gräsman <kim.grasman@gmail.com>
This commit is contained in:
Et7f3 2022-10-08 11:59:14 +02:00 committed by GitHub
parent c1d8dd310a
commit 24985e2ecf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 0 additions and 14 deletions

View File

@ -216,20 +216,6 @@ CompilerInstance* CreateCompilerInstance(int argc, const char **argv) {
CompilerInvocation::CreateFromArgs(*invocation, cc_arguments, diagnostics);
invocation->getFrontendOpts().DisableFree = false;
// Use libc++ headers bundled with Xcode.app on macOS.
llvm::Triple triple(invocation->getTargetOpts().Triple);
if (triple.isOSDarwin() && invocation->getHeaderSearchOpts().UseLibcxx) {
invocation->getHeaderSearchOpts().AddPath(
"/Library/Developer/CommandLineTools/usr/include/c++/v1/",
clang::frontend::CXXSystem,
/*IsFramework=*/false, /*IgnoreSysRoot=*/true);
invocation->getHeaderSearchOpts().AddPath(
"/Applications/Xcode.app/Contents/Developer/Toolchains/"
"XcodeDefault.xctoolchain/usr/include/c++/v1",
clang::frontend::CXXSystem,
/*IsFramework=*/false, /*IgnoreSysRoot=*/true);
}
// Show the invocation, with -v.
if (invocation->getHeaderSearchOpts().Verbose) {
errs() << "clang invocation:\n";