Skip to content

Commit 4a97117

Browse files
committed
cmake: Gate test-only libraries behind TESTONLY flag
Add TESTONLY to 16 tcmalloc_cc_library() calls that correspond to testonly targets in the Bazel BUILD files but were being built unconditionally in CMake. This ensures that when TCMALLOC_BUILD_TESTING is OFF, no test-only source code is compiled. Libraries marked TESTONLY: tcmalloc/CMakeLists.txt (7): - tcmalloc_tcmalloc_internal_methods_only - tcmalloc_mock_central_freelist - tcmalloc_mock_static_forwarder - tcmalloc_mock_virtual_allocator - tcmalloc_page_allocator_test_util - tcmalloc_mock_transfer_cache - tcmalloc_mock_huge_page_static_forwarder tcmalloc/internal/CMakeLists.txt (3): - tcmalloc_internal_affinity - tcmalloc_internal_mock_metadata_allocator - tcmalloc_internal_mock_span tcmalloc/testing/CMakeLists.txt (6): - tcmalloc_testing_malloc_hook_recorder - tcmalloc_testing_testutil - tcmalloc_testing_thread_manager - tcmalloc_testing_thread_ctor_test_lib - tcmalloc_testing_benchmark_main - tcmalloc_testing_test_allocator_harness Also fixes the top-level CMakeLists.txt: - Move ABSL_BUILD_TEST_HELPERS, ABSL_USE_EXTERNAL_GOOGLETEST, and ABSL_FIND_GOOGLETEST behind the testing guard so abseil does not attempt to fetch googletest when tests are disabled. - Move protobuf out of the test-only dependency block since profile_builder and profile_marshaler are core (non-testonly) libraries that require it.
1 parent 4bca48b commit 4a97117

4 files changed

Lines changed: 46 additions & 27 deletions

File tree

CMakeLists.txt

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ option(TCMALLOC_BUILD_TEST_HELPERS
5454
"If ON, TCMalloc will build test helper libraries." OFF)
5555

5656
set(ABSL_BUILD_TESTING OFF CACHE BOOL "" FORCE)
57-
set(ABSL_BUILD_TEST_HELPERS ON CACHE BOOL "" FORCE)
58-
set(ABSL_USE_EXTERNAL_GOOGLETEST ON CACHE BOOL "" FORCE)
59-
set(ABSL_FIND_GOOGLETEST OFF CACHE BOOL "" FORCE)
57+
58+
if((BUILD_TESTING AND TCMALLOC_BUILD_TESTING) OR TCMALLOC_BUILD_TEST_HELPERS)
59+
set(ABSL_BUILD_TEST_HELPERS ON CACHE BOOL "" FORCE)
60+
set(ABSL_USE_EXTERNAL_GOOGLETEST ON CACHE BOOL "" FORCE)
61+
set(ABSL_FIND_GOOGLETEST OFF CACHE BOOL "" FORCE)
62+
endif()
6063

6164
include(FetchContent)
6265

@@ -94,6 +97,30 @@ else()
9497
endif()
9598
endif()
9699

100+
# -- Protobuf (required by profile_builder / profile_marshaler) --
101+
option(TCMALLOC_USE_EXTERNAL_PROTOBUF
102+
"If ON, assume Protobuf targets are already provided." OFF)
103+
set(TCMALLOC_LOCAL_PROTOBUF_DIR "" CACHE PATH
104+
"Path to a local protobuf source directory.")
105+
106+
if(TCMALLOC_USE_EXTERNAL_PROTOBUF)
107+
if(NOT TARGET protobuf::libprotobuf)
108+
find_package(Protobuf REQUIRED)
109+
endif()
110+
else()
111+
if(TCMALLOC_LOCAL_PROTOBUF_DIR)
112+
add_subdirectory("${TCMALLOC_LOCAL_PROTOBUF_DIR}" protobuf EXCLUDE_FROM_ALL)
113+
else()
114+
FetchContent_Declare(
115+
protobuf
116+
GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
117+
GIT_TAG main
118+
FIND_PACKAGE_ARGS NAMES Protobuf
119+
)
120+
FetchContent_MakeAvailable(protobuf)
121+
endif()
122+
endif()
123+
97124
# -- Test-only dependencies --
98125
if((BUILD_TESTING AND TCMALLOC_BUILD_TESTING) OR TCMALLOC_BUILD_TEST_HELPERS)
99126

@@ -127,30 +154,6 @@ if((BUILD_TESTING AND TCMALLOC_BUILD_TESTING) OR TCMALLOC_BUILD_TEST_HELPERS)
127154
endif()
128155
endif()
129156

130-
# Protobuf
131-
option(TCMALLOC_USE_EXTERNAL_PROTOBUF
132-
"If ON, assume Protobuf targets are already provided." OFF)
133-
set(TCMALLOC_LOCAL_PROTOBUF_DIR "" CACHE PATH
134-
"Path to a local protobuf source directory.")
135-
136-
if(TCMALLOC_USE_EXTERNAL_PROTOBUF)
137-
if(NOT TARGET protobuf::libprotobuf)
138-
find_package(Protobuf REQUIRED)
139-
endif()
140-
else()
141-
if(TCMALLOC_LOCAL_PROTOBUF_DIR)
142-
add_subdirectory("${TCMALLOC_LOCAL_PROTOBUF_DIR}" protobuf EXCLUDE_FROM_ALL)
143-
else()
144-
FetchContent_Declare(
145-
protobuf
146-
GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
147-
GIT_TAG main
148-
FIND_PACKAGE_ARGS NAMES Protobuf
149-
)
150-
FetchContent_MakeAvailable(protobuf)
151-
endif()
152-
endif()
153-
154157
# Google Benchmark
155158
option(TCMALLOC_USE_EXTERNAL_BENCHMARK
156159
"If ON, assume Google Benchmark targets are already provided." OFF)

tcmalloc/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ tcmalloc_cc_library(
8686
tcmalloc_tcmalloc_internal_methods_only
8787
ALIAS
8888
tcmalloc::tcmalloc_internal_methods_only
89+
TESTONLY
8990
HDRS
9091
"tcmalloc.h"
9192
SRCS
@@ -722,6 +723,7 @@ tcmalloc_cc_library(
722723
tcmalloc_mock_central_freelist
723724
ALIAS
724725
tcmalloc::mock_central_freelist
726+
TESTONLY
725727
HDRS
726728
"mock_central_freelist.h"
727729
SRCS
@@ -740,6 +742,7 @@ tcmalloc_cc_library(
740742
tcmalloc_mock_static_forwarder
741743
ALIAS
742744
tcmalloc::mock_static_forwarder
745+
TESTONLY
743746
HDRS
744747
"mock_static_forwarder.h"
745748
DEPS
@@ -756,6 +759,7 @@ tcmalloc_cc_library(
756759
tcmalloc_mock_virtual_allocator
757760
ALIAS
758761
tcmalloc::mock_virtual_allocator
762+
TESTONLY
759763
HDRS
760764
"mock_virtual_allocator.h"
761765
DEPS
@@ -771,6 +775,7 @@ tcmalloc_cc_library(
771775
tcmalloc_page_allocator_test_util
772776
ALIAS
773777
tcmalloc::page_allocator_test_util
778+
TESTONLY
774779
HDRS
775780
"page_allocator_test_util.h"
776781
SRCS
@@ -788,6 +793,7 @@ tcmalloc_cc_library(
788793
tcmalloc_mock_transfer_cache
789794
ALIAS
790795
tcmalloc::mock_transfer_cache
796+
TESTONLY
791797
HDRS
792798
"mock_transfer_cache.h"
793799
SRCS
@@ -809,6 +815,7 @@ tcmalloc_cc_library(
809815
tcmalloc_mock_huge_page_static_forwarder
810816
ALIAS
811817
tcmalloc::mock_huge_page_static_forwarder
818+
TESTONLY
812819
HDRS
813820
"mock_huge_page_static_forwarder.h"
814821
SRCS

tcmalloc/internal/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tcmalloc_cc_library(
1717
tcmalloc_internal_affinity
1818
ALIAS
1919
tcmalloc::internal_affinity
20+
TESTONLY
2021
HDRS
2122
"affinity.h"
2223
SRCS
@@ -497,6 +498,7 @@ tcmalloc_cc_library(
497498
tcmalloc_internal_mock_metadata_allocator
498499
ALIAS
499500
tcmalloc::internal_mock_metadata_allocator
501+
TESTONLY
500502
HDRS
501503
"mock_metadata_allocator.h"
502504
DEPS
@@ -573,6 +575,7 @@ tcmalloc_cc_library(
573575
tcmalloc_internal_mock_span
574576
ALIAS
575577
tcmalloc::internal_mock_span
578+
TESTONLY
576579
HDRS
577580
"mock_span.h"
578581
DEPS

tcmalloc/testing/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tcmalloc_cc_library(
1717
tcmalloc_testing_malloc_hook_recorder
1818
ALIAS
1919
tcmalloc::testing_malloc_hook_recorder
20+
TESTONLY
2021
HDRS
2122
"malloc_hook_recorder.h"
2223
SRCS
@@ -34,6 +35,7 @@ tcmalloc_cc_library(
3435
tcmalloc_testing_testutil
3536
ALIAS
3637
tcmalloc::testing_testutil
38+
TESTONLY
3739
HDRS
3840
"testutil.h"
3941
SRCS
@@ -231,6 +233,7 @@ tcmalloc_cc_library(
231233
tcmalloc_testing_thread_manager
232234
ALIAS
233235
tcmalloc::testing_thread_manager
236+
TESTONLY
234237
HDRS
235238
"thread_manager.h"
236239
DEPS
@@ -353,6 +356,7 @@ tcmalloc_cc_library(
353356
tcmalloc_testing_thread_ctor_test_lib
354357
ALIAS
355358
tcmalloc::testing_thread_ctor_test_lib
359+
TESTONLY
356360
SRCS
357361
"thread_ctor_test_lib.cc"
358362
LINKOPTS
@@ -610,6 +614,7 @@ tcmalloc_cc_library(
610614
tcmalloc_testing_benchmark_main
611615
ALIAS
612616
tcmalloc::testing_benchmark_main
617+
TESTONLY
613618
SRCS
614619
"benchmark_main.cc"
615620
DEPS
@@ -650,6 +655,7 @@ tcmalloc_cc_library(
650655
tcmalloc_testing_test_allocator_harness
651656
ALIAS
652657
tcmalloc::testing_test_allocator_harness
658+
TESTONLY
653659
HDRS
654660
"test_allocator_harness.h"
655661
DEPS

0 commit comments

Comments
 (0)