scons: Don't use PROTOC for the protoc command and to flag its presence.

Commands that blindly use PROTOC will try to execute "False" which is
very confusing for someone looking at the console output and error
messages. Instead, create a new environment setting HAVE_PROTOC which
is either true or false depending on if the protoc command exists and
passes muster.

Also, if there's an error running protoc, catch that and use it to
mark protoc as unavailable. The previous behavior was to supress errors
and just return an empty string instead, I assume with the expectation
that that would be an invalid version and fail later checks.

Change-Id: I1251b4e7e0e9894cdd3343e59498cc653b648b26
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/22883
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Maintainer: Jason Lowe-Power <jason@lowepower.com>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/SConstruct b/SConstruct
index 39e7ccc..3951a8e 100755
--- a/SConstruct
+++ b/SConstruct
@@ -595,44 +595,49 @@
 have_pkg_config = readCommand(['pkg-config', '--version'], exception='')
 
 # Check for the protobuf compiler
-protoc_version = readCommand([main['PROTOC'], '--version'],
-                             exception='').split()
+try:
+    main['HAVE_PROTOC'] = True
+    protoc_version = readCommand([main['PROTOC'], '--version']).split()
 
-# First two words should be "libprotoc x.y.z"
-if len(protoc_version) < 2 or protoc_version[0] != 'libprotoc':
-    print(termcap.Yellow + termcap.Bold +
-        'Warning: Protocol buffer compiler (protoc) not found.\n' +
-        '         Please install protobuf-compiler for tracing support.' +
-        termcap.Normal)
-    main['PROTOC'] = False
-else:
-    # Based on the availability of the compress stream wrappers,
-    # require 2.1.0
-    min_protoc_version = '2.1.0'
-    if compareVersions(protoc_version[1], min_protoc_version) < 0:
+    # First two words should be "libprotoc x.y.z"
+    if len(protoc_version) < 2 or protoc_version[0] != 'libprotoc':
         print(termcap.Yellow + termcap.Bold +
-            'Warning: protoc version', min_protoc_version,
-            'or newer required.\n' +
-            '         Installed version:', protoc_version[1],
+            'Warning: Protocol buffer compiler (protoc) not found.\n' +
+            '         Please install protobuf-compiler for tracing support.' +
             termcap.Normal)
-        main['PROTOC'] = False
+        main['HAVE_PROTOC'] = False
     else:
-        # Attempt to determine the appropriate include path and
-        # library path using pkg-config, that means we also need to
-        # check for pkg-config. Note that it is possible to use
-        # protobuf without the involvement of pkg-config. Later on we
-        # check go a library config check and at that point the test
-        # will fail if libprotobuf cannot be found.
-        if have_pkg_config:
-            try:
-                # Attempt to establish what linking flags to add for protobuf
-                # using pkg-config
-                main.ParseConfig('pkg-config --cflags --libs-only-L protobuf')
-            except:
-                print(termcap.Yellow + termcap.Bold +
-                    'Warning: pkg-config could not get protobuf flags.' +
-                    termcap.Normal)
-
+        # Based on the availability of the compress stream wrappers,
+        # require 2.1.0
+        min_protoc_version = '2.1.0'
+        if compareVersions(protoc_version[1], min_protoc_version) < 0:
+            print(termcap.Yellow + termcap.Bold +
+                'Warning: protoc version', min_protoc_version,
+                'or newer required.\n' +
+                '         Installed version:', protoc_version[1],
+                termcap.Normal)
+            main['HAVE_PROTOC'] = False
+        else:
+            # Attempt to determine the appropriate include path and
+            # library path using pkg-config, that means we also need to
+            # check for pkg-config. Note that it is possible to use
+            # protobuf without the involvement of pkg-config. Later on we
+            # check go a library config check and at that point the test
+            # will fail if libprotobuf cannot be found.
+            if have_pkg_config:
+                try:
+                    # Attempt to establish what linking flags to add for
+                    # protobuf
+                    # using pkg-config
+                    main.ParseConfig(
+                            'pkg-config --cflags --libs-only-L protobuf')
+                except:
+                    print(termcap.Yellow + termcap.Bold +
+                        'Warning: pkg-config could not get protobuf flags.' +
+                        termcap.Normal)
+except Exception as e:
+    print(termcap.Yellow + termcap.Bold + str(e) + termcap.Normal)
+    main['HAVE_PROTOC'] = False
 
 # Check for 'timeout' from GNU coreutils. If present, regressions will
 # be run with a time limit. We require version 8.13 since we rely on
@@ -787,7 +792,7 @@
 # automatically added to the LIBS environment variable. After
 # this, we can use the HAVE_PROTOBUF flag to determine if we have
 # got both protoc and libprotobuf available.
-main['HAVE_PROTOBUF'] = main['PROTOC'] and \
+main['HAVE_PROTOBUF'] = main['HAVE_PROTOC'] and \
     conf.CheckLibWithHeader('protobuf', 'google/protobuf/message.h',
                             'C++', 'GOOGLE_PROTOBUF_VERIFY_VERSION;')
 
@@ -796,7 +801,7 @@
 main['HAVE_VALGRIND'] = conf.CheckCHeader('valgrind/valgrind.h')
 
 # If we have the compiler but not the library, print another warning.
-if main['PROTOC'] and not main['HAVE_PROTOBUF']:
+if main['HAVE_PROTOC'] and not main['HAVE_PROTOBUF']:
     print(termcap.Yellow + termcap.Bold +
         'Warning: did not find protocol buffer library and/or headers.\n' +
     '       Please install libprotobuf-dev for tracing support.' +