util/perl/OpenSSL/config.pm: Fix determine_compiler_settings()
authorRichard Levitte <levitte@openssl.org>
Mon, 22 Feb 2021 06:29:03 +0000 (07:29 +0100)
committerRichard Levitte <levitte@openssl.org>
Tue, 23 Feb 2021 08:35:04 +0000 (09:35 +0100)
There may be times when a compiler can't be detected, in which case
determine_compiler_settings() bailed out too early, before platform
specific fallbacks have a chance to set the record straight.  That
bail out has been moved to be done after the platform specific
fallbacks.

Furthermore, the attempt to check for gcc or clang and get their
version number was done even if no compiler had been automatically
detected or pre-specified via $CC.  It now only does this when there
is a compiler specified or detected.  The platform specific fallbacks
check the versions separately.

Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14270)

util/perl/OpenSSL/config.pm

index 776e448df4d236479fa1a3f58c15988b95237b04..841ef4b6aa167ac4bdb7d889fd3415849250578c 100755 (executable)
@@ -193,6 +193,8 @@ sub maybe_abort {
 
 # Look for ISC/SCO with its unique uname program
 sub is_sco_uname {
+    return undef unless IPC::Cmd::can_run('uname');
+
     open UNAME, "uname -X 2>/dev/null|" or return '';
     my $line = "";
     while ( <UNAME> ) {
@@ -200,9 +202,11 @@ sub is_sco_uname {
         $line = $_ if m@^Release@;
     }
     close UNAME;
-    return "" if $line eq '';
+
+    return undef if $line eq '';
+
     my @fields = split(/\s+/, $line);
-    return $fields[2] // '';
+    return $fields[2];
 }
 
 sub get_sco_type {
@@ -237,7 +241,7 @@ sub guess_system {
 
     # Special-cases for ISC, SCO, Unixware
     my $REL = is_sco_uname();
-    if ( $REL ne "" ) {
+    if ( defined $REL ) {
         my $result = get_sco_type($REL);
         return eval "\"$result\"" if $result ne '';
     }
@@ -276,8 +280,8 @@ sub _pairs (@) {
 
 # Figure out CC, GCCVAR, etc.
 sub determine_compiler_settings {
-    # Make a copy and don't touch it.  That helps determine if we're
-    # finding the compiler here
+    # Make a copy and don't touch it.  That helps determine if we're finding
+    # the compiler here (false), or if it was set by the user (true.
     my $cc = $CC;
 
     # Set certain default
@@ -293,50 +297,45 @@ sub determine_compiler_settings {
         }
     }
 
-    # Find the compiler vendor and version number for certain compilers
-    foreach my $pair (_pairs @cc_version) {
-        # Try to get the version number.
-        # Failure gets us undef or an empty string
-        my ( $k, $v ) = @$pair;
-        $v = $v->();
-
-        # If we got a version number, process it
-        if ($v) {
-            $CCVENDOR = $k;
-
-            # The returned version is expected to be one of
-            #
-            # MAJOR
-            # MAJOR.MINOR
-            # MAJOR.MINOR.{whatever}
-            #
-            # We don't care what comes after MAJOR.MINOR.  All we need is to
-            # have them calculated into a single number, using this formula:
-            #
-            # MAJOR * 100 + MINOR
-            # Here are a few examples of what we should get:
-            #
-            # 2.95.1    => 295
-            # 3.1       => 301
-            # 9         => 900
-            my @numbers = split /\./, $v;
-            my @factors = (100, 1);
-            while (@numbers && @factors) {
-                $CCVER += shift(@numbers) * shift(@factors)
+    if ( $CC ) {
+        # Find the compiler vendor and version number for certain compilers
+        foreach my $pair (_pairs @cc_version) {
+            # Try to get the version number.
+            # Failure gets us undef or an empty string
+            my ( $k, $v ) = @$pair;
+            $v = $v->();
+
+            # If we got a version number, process it
+            if ($v) {
+                $CCVENDOR = $k;
+
+                # The returned version is expected to be one of
+                #
+                # MAJOR
+                # MAJOR.MINOR
+                # MAJOR.MINOR.{whatever}
+                #
+                # We don't care what comes after MAJOR.MINOR.  All we need is
+                # to have them calculated into a single number, using this
+                # formula:
+                #
+                # MAJOR * 100 + MINOR
+                # Here are a few examples of what we should get:
+                #
+                # 2.95.1    => 295
+                # 3.1       => 301
+                # 9         => 900
+                my @numbers = split /\./, $v;
+                my @factors = (100, 1);
+                while (@numbers && @factors) {
+                    $CCVER += shift(@numbers) * shift(@factors)
+                }
+                last;
             }
-            last;
         }
     }
 
-    # If no C compiler has been determined at this point, we die.  Hard.
-    die <<_____
-ERROR!
-No C compiler found, please specify one with the environment variable CC,
-or configure with an explicit configuration target.
-_____
-        unless $CC;
-
-    # Vendor specific overrides, only if we determined the compiler here
+    # Vendor specific overrides, only if we didn't determine the compiler here
     if ( ! $cc ) {
         if ( ${SYSTEM} eq 'AIX' ) {
             # favor vendor cc over gcc
@@ -375,6 +374,14 @@ EOF
         }
     }
 
+    # If no C compiler has been determined at this point, we die.  Hard.
+    die <<_____
+ERROR!
+No C compiler found, please specify one with the environment variable CC,
+or configure with an explicit configuration target.
+_____
+        unless $CC;
+
     # On some systems, we assume a cc vendor if it's not already determined
 
     if ( ! $CCVENDOR ) {