From a61641ef4009e113fd74eaaf8966aacf1eba2924 Mon Sep 17 00:00:00 2001 From: Devin Coughlin Date: Sat, 16 Mar 2019 01:01:29 +0000 Subject: [PATCH] [analyzer] Teach scan-build to find clang when installed in /usr/local/bin/ Change scan-build to support the scenario where scan-build is installed in $TOOLCHAIN/usr/local/bin/ but clang itself is installed in $TOOLCHAIN/usr/bin/. This is restricted to when 'xcrun' is present; that is, on the Mac. rdar://problem/48914634 Differential Revision: https://reviews.llvm.org/D59406 llvm-svn: 356308 --- clang/tools/scan-build/bin/scan-build | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/clang/tools/scan-build/bin/scan-build b/clang/tools/scan-build/bin/scan-build index ed68b3b..903e19a 100755 --- a/clang/tools/scan-build/bin/scan-build +++ b/clang/tools/scan-build/bin/scan-build @@ -1460,6 +1460,16 @@ sub ShellEscape { } ##----------------------------------------------------------------------------## +# FindXcrun - searches for the 'xcrun' executable. Returns "" if not found. +##----------------------------------------------------------------------------## + +sub FindXcrun { + my $xcrun = `which xcrun`; + chomp $xcrun; + return $xcrun; +} + +##----------------------------------------------------------------------------## # FindClang - searches for 'clang' executable. ##----------------------------------------------------------------------------## @@ -1468,6 +1478,16 @@ sub FindClang { $Clang = Cwd::realpath("$RealBin/bin/clang") if (-f "$RealBin/bin/clang"); if (!defined $Clang || ! -x $Clang) { $Clang = Cwd::realpath("$RealBin/clang") if (-f "$RealBin/clang"); + if (!defined $Clang || ! -x $Clang) { + # When an Xcode toolchain is present, look for a clang in the sibling bin + # of the parent of the bin directory. So if scan-build is at + # $TOOLCHAIN/usr/local/bin/scan-build look for clang at + # $TOOLCHAIN/usr/bin/clang. + my $has_xcode_toolchain = FindXcrun() ne ""; + if ($has_xcode_toolchain && -f "$RealBin/../../bin/clang") { + $Clang = Cwd::realpath("$RealBin/../../bin/clang"); + } + } } if (!defined $Clang || ! -x $Clang) { return "error: Cannot find an executable 'clang' relative to" . @@ -1477,8 +1497,7 @@ sub FindClang { } else { if ($Options{AnalyzerDiscoveryMethod} =~ /^[Xx]code$/) { - my $xcrun = `which xcrun`; - chomp $xcrun; + my $xcrun = FindXcrun(); if ($xcrun eq "") { return "Cannot find 'xcrun' to find 'clang' for analysis.\n"; } -- 2.7.4