Skip to content

Commit 6806e42

Browse files
committed
[clang][scan-build] Minor simplification/refactor of environment detection
I want to adjust the logic for CCC_CC and CCC_CXX defaults, so not repeating that logic twice is convenient. As a nice side-effect, we end up with fewer globals and less code. Contains no behavioural changes, except for checking for /usr/bin/xcrun even if we're not on OSX when there's an -isysroot parameter, which should be fine. We also don't call uname -s twice.
1 parent 3bdcfbb commit 6806e42

File tree

1 file changed

+35
-45
lines changed

1 file changed

+35
-45
lines changed

clang/tools/scan-build/libexec/ccc-analyzer

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -62,53 +62,42 @@ sub SearchInPath {
6262
return 0;
6363
}
6464

65-
my $Compiler;
66-
my @CompilerArgs;
67-
my $Clang;
68-
my $DefaultCCompiler;
69-
my $DefaultCXXCompiler;
70-
my $IsCXX;
71-
my $AnalyzerTarget;
72-
73-
# If on OSX, use xcrun to determine the SDK root.
74-
my $UseXCRUN = 0;
75-
76-
if (`uname -s` =~ m/Darwin/) {
77-
$DefaultCCompiler = 'clang';
78-
$DefaultCXXCompiler = 'clang++';
79-
# Older versions of OSX do not have xcrun to
80-
# query the SDK location.
81-
if (-x "/usr/bin/xcrun") {
82-
$UseXCRUN = 1;
83-
}
84-
} elsif (`uname -s` =~ m/(FreeBSD|OpenBSD)/) {
85-
$DefaultCCompiler = 'cc';
86-
$DefaultCXXCompiler = 'c++';
87-
} else {
88-
$DefaultCCompiler = 'gcc';
89-
$DefaultCXXCompiler = 'g++';
90-
}
91-
92-
if ($FindBin::Script =~ /c\+\+-analyzer/) {
93-
($Compiler, @CompilerArgs) = shellwords($ENV{'CCC_CXX'});
94-
if (!defined $Compiler || (! -x $Compiler && ! SearchInPath($Compiler))) { $Compiler = $DefaultCXXCompiler; }
95-
96-
$Clang = $ENV{'CLANG_CXX'};
97-
if (!defined $Clang || ! -x $Clang) { $Clang = 'clang++'; }
98-
99-
$IsCXX = 1
65+
{
66+
my ($DefaultCCompiler, $DefaultCXXCompiler);
67+
68+
my $os = `uname -s`;
69+
if ($os =~ m/Darwin/) {
70+
$DefaultCCompiler = 'clang';
71+
$DefaultCXXCompiler = 'clang++';
72+
} elsif ($os =~ m/(FreeBSD|OpenBSD)/) {
73+
$DefaultCCompiler = 'cc';
74+
$DefaultCXXCompiler = 'c++';
75+
} else {
76+
$DefaultCCompiler = 'gcc';
77+
$DefaultCXXCompiler = 'g++';
78+
}
79+
80+
sub DetermineCompiler {
81+
my ($is_cxx) = @_;
82+
my $default = $is_cxx ? $DefaultCXXCompiler : $DefaultCCompiler;
83+
my $opt = $ENV{$is_cxx ? 'CCC_CXX' : 'CCC_CC'};
84+
return $default unless defined $opt;
85+
my ($comp, @args) = shellwords($opt);
86+
return !-x $comp && !SearchInPath($comp) ? $default : ($comp, @args);
87+
}
10088
}
101-
else {
102-
($Compiler, @CompilerArgs) = shellwords($ENV{'CCC_CC'});
103-
if (!defined $Compiler || (! -x $Compiler && ! SearchInPath($Compiler))) { $Compiler = $DefaultCCompiler; }
104-
105-
$Clang = $ENV{'CLANG'};
106-
if (!defined $Clang || ! -x $Clang) { $Clang = 'clang'; }
10789

108-
$IsCXX = 0
90+
sub DetermineClang {
91+
my ($is_cxx) = @_;
92+
my $default = $is_cxx ? 'clang++' : 'clang';
93+
my $opt = $ENV{$is_cxx ? 'CLANG_CXX' : 'CLANG'};
94+
return !defined $opt || !-x $opt ? $default : $opt;
10995
}
11096

111-
$AnalyzerTarget = $ENV{'CLANG_ANALYZER_TARGET'};
97+
my $IsCXX = $FindBin::Script =~ /c\+\+-analyzer/;
98+
my ($Compiler, @CompilerArgs) = DetermineCompiler($IsCXX);
99+
my $Clang = DetermineClang($IsCXX);
100+
my $AnalyzerTarget = $ENV{'CLANG_ANALYZER_TARGET'};
112101

113102
##===----------------------------------------------------------------------===##
114103
# Cleanup.
@@ -699,8 +688,9 @@ if ($ForceAnalyzeDebugCode) {
699688

700689
# If we are on OSX and have an installation where the
701690
# default SDK is inferred by xcrun use xcrun to infer
702-
# the SDK.
703-
if (not $HasSDK and $UseXCRUN) {
691+
# the SDK. Older versions of OSX do not have xcrun to
692+
# query the SDK location.
693+
if (not $HasSDK and -x '/usr/bin/xcrun') {
704694
my $sdk = `/usr/bin/xcrun --show-sdk-path -sdk macosx`;
705695
chomp $sdk;
706696
push @CompileOpts, "-isysroot", $sdk;

0 commit comments

Comments
 (0)