Add TESTONLY attribute to CMake tink_cc_library targets

This option excludes the target from being defined when TINK_BUILD_TESTS=OFF.

PiperOrigin-RevId: 527197212
diff --git a/cc/aead/CMakeLists.txt b/cc/aead/CMakeLists.txt
index 05cb170..6ca31e8 100644
--- a/cc/aead/CMakeLists.txt
+++ b/cc/aead/CMakeLists.txt
@@ -304,6 +304,7 @@
     absl::strings
     tink::core::aead
     tink::util::statusor
+  TESTONLY
 )
 
 # tests
@@ -622,6 +623,7 @@
   DEPS
     absl::strings
     tink::core::aead
+  TESTONLY
 )
 
 tink_cc_test(
diff --git a/cc/aead/internal/CMakeLists.txt b/cc/aead/internal/CMakeLists.txt
index f4e1327..67e2b1a 100644
--- a/cc/aead/internal/CMakeLists.txt
+++ b/cc/aead/internal/CMakeLists.txt
@@ -21,6 +21,7 @@
     absl::strings
     tink::subtle::wycheproof_util
     tink::util::statusor
+  TESTONLY
 )
 
 tink_cc_library(
@@ -74,6 +75,7 @@
     gmock
     absl::strings
     tink::util::statusor
+  TESTONLY
 )
 
 tink_cc_library(
diff --git a/cc/hybrid/internal/CMakeLists.txt b/cc/hybrid/internal/CMakeLists.txt
index 6dc37fa..182b12c 100644
--- a/cc/hybrid/internal/CMakeLists.txt
+++ b/cc/hybrid/internal/CMakeLists.txt
@@ -201,6 +201,7 @@
     tink::util::status
     tink::util::statusor
     tink::proto::hpke_cc_proto
+  TESTONLY
   TAGS
     exclude_if_openssl
 )
@@ -253,6 +254,7 @@
     tink::util::secret_data
     tink::util::status
     tink::util::statusor
+  TESTONLY
   TAGS
     exclude_if_openssl
 )
diff --git a/cc/internal/CMakeLists.txt b/cc/internal/CMakeLists.txt
index a2536bc..52ef5b6 100644
--- a/cc/internal/CMakeLists.txt
+++ b/cc/internal/CMakeLists.txt
@@ -42,6 +42,7 @@
     tink::subtle::random
     tink::util::status
     tink::util::test_util
+  TESTONLY
 )
 
 tink_cc_library(
@@ -980,6 +981,7 @@
     tink::core::parameters
     tink::core::secret_key_access_token
     tink::util::statusor
+  TESTONLY
 )
 
 tink_cc_test(
@@ -1008,6 +1010,7 @@
     tink::util::buffer
     tink::util::status
     tink::util::statusor
+  TESTONLY
 )
 
 tink_cc_test(
diff --git a/cc/mac/CMakeLists.txt b/cc/mac/CMakeLists.txt
index 695e59f..51dde3b 100644
--- a/cc/mac/CMakeLists.txt
+++ b/cc/mac/CMakeLists.txt
@@ -152,6 +152,7 @@
   DEPS
     absl::strings
     tink::core::mac
+  TESTONLY
 )
 
 tink_cc_library(
diff --git a/cc/monitoring/CMakeLists.txt b/cc/monitoring/CMakeLists.txt
index dcc1d0b..60ad02c 100644
--- a/cc/monitoring/CMakeLists.txt
+++ b/cc/monitoring/CMakeLists.txt
@@ -18,4 +18,5 @@
   DEPS
     tink::monitoring::monitoring
     gmock
+  TESTONLY
 )
diff --git a/cc/subtle/CMakeLists.txt b/cc/subtle/CMakeLists.txt
index 7eddd2b..ff38349 100644
--- a/cc/subtle/CMakeLists.txt
+++ b/cc/subtle/CMakeLists.txt
@@ -644,6 +644,7 @@
     tink::internal::test_file_util
     tink::util::status
     tink::util::statusor
+  TESTONLY
 )
 
 tink_cc_library(
@@ -724,6 +725,7 @@
     tink::core::output_stream
     tink::util::status
     tink::util::statusor
+  TESTONLY
 )
 
 tink_cc_library(
@@ -737,6 +739,7 @@
     tink::core::aead
     tink::aead::cord_aead
     tink::util::status
+  TESTONLY
 )
 
 tink_cc_library(
@@ -754,6 +757,7 @@
     tink::util::istream_input_stream
     tink::util::ostream_output_stream
     tink::util::status
+  TESTONLY
 )
 
 tink_cc_library(
@@ -766,6 +770,7 @@
     tink::core::hybrid_decrypt
     tink::core::hybrid_encrypt
     tink::util::status
+  TESTONLY
 )
 
 tink_cc_library(
diff --git a/cc/util/CMakeLists.txt b/cc/util/CMakeLists.txt
index bed5f9f..c2e5ef5 100644
--- a/cc/util/CMakeLists.txt
+++ b/cc/util/CMakeLists.txt
@@ -205,6 +205,7 @@
     tink::proto::hmac_cc_proto
     tink::proto::tink_cc_proto
     tink::proto::xchacha20_poly1305_cc_proto
+  TESTONLY
 )
 
 tink_cc_library(
@@ -216,6 +217,7 @@
     tink::util::statusor
     gmock
     absl::status
+  TESTONLY
 )
 
 tink_cc_library(
@@ -235,6 +237,7 @@
     absl::memory
     tink::core::keyset_handle
     tink::proto::tink_cc_proto
+  TESTONLY
 )
 
 tink_cc_library(
@@ -502,6 +505,7 @@
     tink::core::kms_client
     tink::core::kms_clients
     tink::aead::aead_key_templates
+  TESTONLY
 )
 
 tink_cc_test(
diff --git a/cmake/TinkBuildRules.cmake b/cmake/TinkBuildRules.cmake
index d64e7ac..9fd4328 100644
--- a/cmake/TinkBuildRules.cmake
+++ b/cmake/TinkBuildRules.cmake
@@ -75,10 +75,12 @@
 # a way to organise code and speed up compilation.
 #
 # Arguments:
-#   NAME base name of the target. See below for target naming conventions.
-#   SRCS list of source files, including headers.
-#   DEPS list of dependency targets.
-#   PUBLIC flag, signal that this target is intended for external use.
+#   NAME      base name of the target. See below for target naming conventions.
+#   SRCS      list of source files, including headers.
+#   DEPS      list of dependency targets.
+#   PUBLIC    flag, signals that this target is intended for external use.
+#   TESTONLY  flag, signals that this target should be ignored if
+#             TINK_BUILD_TESTS=OFF.
 #
 # If SRCS contains only headers, an INTERFACE rule is created. This rule carries
 # include path and link library information, but is not directly buildable.
@@ -95,11 +97,15 @@
 #
 function(tink_cc_library)
   cmake_parse_arguments(PARSE_ARGV 0 tink_cc_library
-    "PUBLIC"
+    "PUBLIC;TESTONLY"
     "NAME"
     "SRCS;DEPS;TAGS"
   )
 
+  if (tink_cc_library_TESTONLY AND NOT TINK_BUILD_TESTS)
+    return()
+  endif()
+
   if (NOT DEFINED TINK_MODULE)
     message(FATAL_ERROR
             "TINK_MODULE not defined, perhaps you are missing a tink_module() statement?")
@@ -162,10 +168,10 @@
 # Declare a Tink test using googletest, with a syntax similar to Bazel.
 #
 # Parameters:
-#   NAME base name of the test.
-#   SRCS list of test source files, headers included.
-#   DEPS list of dependencies, see tink_cc_library above.
-#   DATA list of non-code dependencies, such as test vectors.
+#   NAME  base name of the test.
+#   SRCS  list of test source files, headers included.
+#   DEPS  list of dependencies, see tink_cc_library above.
+#   DATA  list of non-code dependencies, such as test vectors.
 #
 # Tests added with this macro are automatically registered.
 # Each test produces a build target named tink_test_<MODULE>_<NAME>.
@@ -285,8 +291,8 @@
 # to group dependencies that are logically related and give them a single name.
 #
 # Parameters:
-#   NAME base name of the target.
-#   DEPS list of dependencies to group.
+#   NAME  base name of the target.
+#   DEPS  list of dependencies to group.
 #
 # Each tink_target_group produces a target named tink_<MODULE>_<NAME>.
 function(tink_target_group)