Don't include custom builds of libc++ in CMake installs

Also stick the very verbose default install directory in a variable so
we don't have to repeat it everywhere.

Change-Id: I1a6a85c4d42d3a6e766e52b2d0ecd8e81c6ed4e3
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/56607
Reviewed-by: Bob Beck <bbe@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9fa818c..ced650d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -24,6 +24,24 @@
 
 include(GNUInstallDirs)
 
+# CMake versions before 3.14 do not have default destination values. Executable
+# and library targets that use a default destination should include this
+# variable.
+if(CMAKE_VERSION VERSION_LESS "3.14")
+  set(INSTALL_DESTINATION_DEFAULT
+      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+      RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
+endif()
+
+# Wrap the CMake install function so we can disable it.
+set(INSTALL_ENABLED 1)
+function(install_if_enabled)
+  if(INSTALL_ENABLED)
+    install(${ARGV})
+  endif()
+endfunction()
+
 # This is a dummy target which all other targets depend on (manually - see other
 # CMakeLists.txt files). This gives us a hook to add any targets which need to
 # run before all other targets.
@@ -400,6 +418,12 @@
     message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
   endif()
 
+  # CMake does not allow installing a library without installing dependencies.
+  # If we installed libcrypto, we'd have to install our custom libc++, which
+  # does not make sense. As this is a test-only configuration, disable
+  # installing.
+  set(INSTALL_ENABLED 0)
+
   # CMAKE_CXX_FLAGS ends up in the linker flags as well, so use
   # add_compile_options. There does not appear to be a way to set
   # language-specific compile-only flags.
@@ -433,7 +457,6 @@
     libcxxabi PRIVATE
     -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
   )
-  install(TARGETS libcxxabi EXPORT OpenSSLTargets)
 
   add_library(libcxx ${LIBCXX_SOURCES})
   if(ASAN OR MSAN OR TSAN)
@@ -458,7 +481,6 @@
   # libc++abi depends on libc++ internal headers.
   set_property(TARGET libcxx libcxxabi APPEND PROPERTY INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/util/bot/libcxx/src")
   target_link_libraries(libcxx libcxxabi)
-  install(TARGETS libcxx EXPORT OpenSSLTargets DESTINATION ${CMAKE_INSTALL_LIBDIR})
 endif()
 
 # Add minimal googletest targets. The provided one has many side-effects, and
@@ -562,11 +584,11 @@
     DEPENDS all_tests bssl_shim handshaker fips_specific_tests_if_any
     USES_TERMINAL)
 
-install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
+install_if_enabled(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
 
-install(EXPORT OpenSSLTargets
+install_if_enabled(EXPORT OpenSSLTargets
   FILE OpenSSLTargets.cmake
   NAMESPACE OpenSSL::
   DESTINATION lib/cmake/OpenSSL
 )
-install(FILES cmake/OpenSSLConfig.cmake DESTINATION lib/cmake/OpenSSL)
+install_if_enabled(FILES cmake/OpenSSLConfig.cmake DESTINATION lib/cmake/OpenSSL)
diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt
index df3f919..e4208ac 100644
--- a/crypto/CMakeLists.txt
+++ b/crypto/CMakeLists.txt
@@ -300,11 +300,7 @@
   $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
   $<INSTALL_INTERFACE:include>
 )
-install(TARGETS crypto EXPORT OpenSSLTargets
-  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
-  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
-  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
-)
+install_if_enabled(TARGETS crypto EXPORT OpenSSLTargets ${INSTALL_DESTINATION_DEFAULT})
 set_property(TARGET crypto PROPERTY EXPORT_NAME Crypto)
 
 if(FIPS_SHARED)
diff --git a/ssl/CMakeLists.txt b/ssl/CMakeLists.txt
index ca42579..beaf61a 100644
--- a/ssl/CMakeLists.txt
+++ b/ssl/CMakeLists.txt
@@ -42,11 +42,7 @@
 # Although libssl also provides headers that require an include directory, the
 # flag is already specified by libcrypto, so we omit target_include_directories
 # here.
-install(TARGETS ssl EXPORT OpenSSLTargets
-  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
-  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
-  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
-)
+install_if_enabled(TARGETS ssl EXPORT OpenSSLTargets ${INSTALL_DESTINATION_DEFAULT})
 set_property(TARGET ssl PROPERTY EXPORT_NAME SSL)
 add_dependencies(ssl global_target)
 target_link_libraries(ssl crypto)
diff --git a/tool/CMakeLists.txt b/tool/CMakeLists.txt
index 9cc7d28..df5d9a7 100644
--- a/tool/CMakeLists.txt
+++ b/tool/CMakeLists.txt
@@ -19,6 +19,6 @@
   tool.cc
   transport_common.cc
 )
-install(TARGETS bssl DESTINATION ${CMAKE_INSTALL_BINDIR})
+install_if_enabled(TARGETS bssl DESTINATION ${INSTALL_DESTINATION_DEFAULT})
 add_dependencies(bssl global_target)
 target_link_libraries(bssl ssl crypto)