================
@@ -51,63 +51,40 @@ sub silent_system {
 # Compiler command setup.
 
##===----------------------------------------------------------------------===##
 
-# Search in the PATH if the compiler exists
-sub SearchInPath {
-    my $file = shift;
-    foreach my $dir (split (':', $ENV{PATH})) {
-        if (-x "$dir/$file") {
-            return 1;
-        }
-    }
-    return 0;
-}
-
-my $Compiler;
-my $Clang;
-my $DefaultCCompiler;
-my $DefaultCXXCompiler;
-my $IsCXX;
-my $AnalyzerTarget;
-
-# If on OSX, use xcrun to determine the SDK root.
-my $UseXCRUN = 0;
-
-if (`uname -s` =~ m/Darwin/) {
-  $DefaultCCompiler = 'clang';
-  $DefaultCXXCompiler = 'clang++';
-  # Older versions of OSX do not have xcrun to
-  # query the SDK location.
-  if (-x "/usr/bin/xcrun") {
-    $UseXCRUN = 1;
-  }
-} elsif (`uname -s` =~ m/(FreeBSD|OpenBSD)/) {
-  $DefaultCCompiler = 'cc';
-  $DefaultCXXCompiler = 'c++';
-} else {
-  $DefaultCCompiler = 'gcc';
-  $DefaultCXXCompiler = 'g++';
-}
-
-if ($FindBin::Script =~ /c\+\+-analyzer/) {
-  $Compiler = $ENV{'CCC_CXX'};
-  if (!defined $Compiler || (! -x $Compiler && ! SearchInPath($Compiler))) { 
$Compiler = $DefaultCXXCompiler; }
-
-  $Clang = $ENV{'CLANG_CXX'};
-  if (!defined $Clang || ! -x $Clang) { $Clang = 'clang++'; }
-
-  $IsCXX = 1
+{
+  my ($DefaultCCompiler, $DefaultCXXCompiler);
+
+  my $os = `uname -s`;
+  if ($os =~ m/Darwin/) {
+    $DefaultCCompiler = 'clang';
+    $DefaultCXXCompiler = 'clang++';
+  } elsif ($os =~ m/(FreeBSD|OpenBSD)/) {
+    $DefaultCCompiler = 'cc';
+    $DefaultCXXCompiler = 'c++';
+  } else {
+    $DefaultCCompiler = 'gcc';
+    $DefaultCXXCompiler = 'g++';
+  }
+
+  sub DetermineCompiler {
+    my ($is_cxx) = @_;
+    my $default = $is_cxx ? $DefaultCXXCompiler : $DefaultCCompiler;
+    my $opt = $ENV{$is_cxx ? 'CCC_CXX' : 'CCC_CC'};
+    return defined $opt ? shellwords($opt) : $default;
+  }
 }
-else {
-  $Compiler = $ENV{'CCC_CC'};
-  if (!defined $Compiler || (! -x $Compiler && ! SearchInPath($Compiler))) { 
$Compiler = $DefaultCCompiler; }
-
-  $Clang = $ENV{'CLANG'};
-  if (!defined $Clang || ! -x $Clang) { $Clang = 'clang'; }
 
-  $IsCXX = 0
+sub DetermineClang {
+  my ($is_cxx) = @_;
+  my $default = $is_cxx ? 'clang++' : 'clang';
+  my $opt = $ENV{$is_cxx ? 'CLANG_CXX' : 'CLANG'};
+  return defined $opt ? $opt : $default;
 }
 
-$AnalyzerTarget = $ENV{'CLANG_ANALYZER_TARGET'};
+my $IsCXX = $FindBin::Script =~ /c\+\+-analyzer/;
+my ($Compiler, @CompilerArgs) = DetermineCompiler($IsCXX);
----------------
rafl wrote:

@ziqingluo-90 that's exactly right. `DetermineCompiler` returns a list of 
values (potentially a singleton list), and the call-site unpacks that return 
value into the first list element `$Compiler` and the rest of the list 
`@CompilerArgs` (which might be empty).

The actual parsing is done using shellwords from 
[Text::ParseWords](https://metacpan.org/pod/Text::ParseWords).

If you'd like to experiment how it parses certain inputs, you can do something 
like this, which prints out the parsed list, one item per line:

```
$ CCC_CC='foo "bar baz" \"quux' perl -MText::ParseWords -le'print for 
shellwords $ENV{CCC_CC}'
foo
bar baz
"quux
```

https://github.com/llvm/llvm-project/pull/131932
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to