Make Go an optional build dependency for the CMake build

Matching the various pre-generated builds, the CMake build no longer
actually requires Go. The only things that need it are:

- Running tests
- Builds with -DFIPS=1
- Builds with the experimental symbol prefixing thing

Bug: 542
Change-Id: I52bb427f54dd6e5719cfe77773e87fc394410380
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/67367
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/BUILDING.md b/BUILDING.md
index e10d964..d78d283 100644
--- a/BUILDING.md
+++ b/BUILDING.md
@@ -31,14 +31,6 @@
     GCC (6.1+) and Clang should work on non-Windows platforms, and maybe on
     Windows too.
 
-  * The most recent stable version of [Go](https://golang.org/dl/) is required.
-    Note Go is exempt from the five year support window. If not found by CMake,
-    the go executable may be configured explicitly by setting `GO_EXECUTABLE`.
-
-  * On x86_64 Linux, the tests have an optional
-    [libunwind](https://www.nongnu.org/libunwind/) dependency to test the
-    assembly more thoroughly.
-
 ## Building
 
 Using Ninja (note the 'N' is capitalized in the cmake invocation):
@@ -127,7 +119,8 @@
 
 BoringSSL's build system has experimental support for adding a custom prefix to
 all symbols. This can be useful when linking multiple versions of BoringSSL in
-the same project to avoid symbol conflicts.
+the same project to avoid symbol conflicts. Symbol prefixing requires the most
+recent stable version of [Go](https://go.dev/).
 
 In order to build with prefixed symbols, the `BORINGSSL_PREFIX` CMake variable
 should specify the prefix to add to all symbols, and the
@@ -195,6 +188,16 @@
 
 # Running Tests
 
+There are two additional dependencies for running tests:
+
+  * The most recent stable version of [Go](https://go.dev/) is required.
+    Note Go is exempt from the five year support window. If not found by CMake,
+    the go executable may be configured explicitly by setting `GO_EXECUTABLE`.
+
+  * On x86_64 Linux, the tests have an optional
+    [libunwind](https://www.nongnu.org/libunwind/) dependency to test the
+    assembly more thoroughly.
+
 There are two sets of tests: the C/C++ tests and the blackbox tests. For former
 are built by Ninja and can be run from the top-level directory with `go run
 util/all_tests.go`. The latter have to be run separately by running `go test`
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 869dfac..39352d6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -59,6 +59,7 @@
 endforeach()
 
 if(BORINGSSL_PREFIX AND BORINGSSL_PREFIX_SYMBOLS)
+  require_go()
   add_definitions(-DBORINGSSL_PREFIX=${BORINGSSL_PREFIX})
   # CMake automatically connects include_directories to the NASM command-line,
   # but not add_definitions.
@@ -309,6 +310,7 @@
 endif()
 
 if(FIPS)
+  require_go()
   add_definitions(-DBORINGSSL_FIPS)
   if(FIPS_BREAK_TEST)
     add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
@@ -726,22 +728,29 @@
 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
              util/go_tests.txt)
 
-add_custom_target(
-    run_tests
-    COMMAND ${CMAKE_COMMAND} -E echo "Running Go tests"
-    COMMAND ${GO_EXECUTABLE} test ${GO_TESTS}
-    COMMAND ${CMAKE_COMMAND} -E echo
-    COMMAND ${CMAKE_COMMAND} -E echo "Running unit tests"
-    COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
-            ${CMAKE_CURRENT_BINARY_DIR}
-    COMMAND ${CMAKE_COMMAND} -E echo
-    COMMAND ${CMAKE_COMMAND} -E echo "Running SSL tests"
-    COMMAND cd ssl/test/runner &&
-            ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
-              ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
-    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-    DEPENDS all_tests bssl_shim handshaker fips_specific_tests_if_any
-    USES_TERMINAL)
+if(GO_EXECUTABLE)
+  add_custom_target(
+      run_tests
+      COMMAND ${CMAKE_COMMAND} -E echo "Running Go tests"
+      COMMAND ${GO_EXECUTABLE} test ${GO_TESTS}
+      COMMAND ${CMAKE_COMMAND} -E echo
+      COMMAND ${CMAKE_COMMAND} -E echo "Running unit tests"
+      COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
+              ${CMAKE_CURRENT_BINARY_DIR}
+      COMMAND ${CMAKE_COMMAND} -E echo
+      COMMAND ${CMAKE_COMMAND} -E echo "Running SSL tests"
+      COMMAND cd ssl/test/runner &&
+              ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
+                ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
+      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+      DEPENDS all_tests bssl_shim handshaker fips_specific_tests_if_any
+      USES_TERMINAL)
+else()
+  add_custom_target(
+      run_tests
+      COMMAND ${CMAKE_COMMAND} -E echo "Running tests requires Go"
+      COMMAND ${CMAKE_COMMAND} -E false)
+endif()
 
 if(INSTALL_ENABLED)
   # CMake versions before 3.14 do not have default destination values. Executable
diff --git a/cmake/go.cmake b/cmake/go.cmake
index 51ecb45..76fcfeb 100644
--- a/cmake/go.cmake
+++ b/cmake/go.cmake
@@ -1,9 +1,15 @@
+# Go is an optional dependency. It's a necessary dependency if running tests or
+# the FIPS build, which will check these.
 find_program(GO_EXECUTABLE go)
-if(NOT GO_EXECUTABLE)
-  message(FATAL_ERROR "Could not find Go")
-endif()
+
+function(require_go)
+  if(NOT GO_EXECUTABLE)
+    message(FATAL_ERROR "Could not find Go")
+  endif()
+endfunction()
 
 function(go_executable dest package)
+  require_go()
   set(godeps "${PROJECT_SOURCE_DIR}/util/godeps.go")
   if(NOT CMAKE_GENERATOR STREQUAL "Ninja")
     # The DEPFILE parameter to add_custom_command only works with Ninja. Query