Skip to content

Commit d74057b

Browse files
committed
cmake: Enrich tcmalloc_cc_library() and tcmalloc_cc_test()
Rewrite tcmalloc_cc_library() and tcmalloc_cc_test() to match the feature set of abseil-cpp's absl_cc_library() and absl_cc_test(): tcmalloc_cc_library() now supports: - PUBLIC flag: marks target as public API (affects IDE folder placement) - TESTONLY flag: only builds when testing is enabled, mirroring abseil-cpp's TESTONLY semantics - DISABLE_INSTALL flag: skips installation rules for specific targets - DEFINES parameter: public compile definitions - Header-only auto-detection: automatically strips .h/.inc files from SRCS to determine if library should be INTERFACE - IDE folder organization: places targets in TCMalloc/, TCMalloc/test/, or TCMalloc/internal/ folders (configurable via TCMALLOC_IDE_FOLDER) - Per-target RPATH: sets INSTALL_RPATH per platform (Darwin/Unix) - SOVERSION and OUTPUT_NAME: set when TCMALLOC_ENABLE_INSTALL is ON - Install rules via install(TARGETS ... EXPORT) when enabled - TCMALLOC_DEFAULT_LINKOPTS applied to all targets tcmalloc_cc_test() now supports: - DEFINES parameter - IDE folder placement in TCMalloc/test/ - Cleaner variable naming (TCMALLOC_CC_TEST_ prefix) These changes maintain full backward compatibility with existing CMakeLists.txt files that use the NAME/ALIAS/SRCS/HDRS/COPTS/DEPS parameters.
1 parent c3a3dda commit d74057b

1 file changed

Lines changed: 165 additions & 52 deletions

File tree

CMake/TcmallocHelpers.cmake

Lines changed: 165 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,84 +17,197 @@ include(CMakeParseArguments)
1717
# include current path
1818
list(APPEND TCMALLOC_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
1919

20+
# The IDE folder for TCMalloc that will be used if TCMalloc is included in a
21+
# CMake project that sets
22+
# set_property(GLOBAL PROPERTY USE_FOLDERS ON)
23+
if(NOT DEFINED TCMALLOC_IDE_FOLDER)
24+
set(TCMALLOC_IDE_FOLDER TCMalloc)
25+
endif()
26+
27+
# tcmalloc_cc_library()
28+
#
29+
# CMake function to imitate Bazel's cc_library rule, analogous to
30+
# abseil-cpp's absl_cc_library().
31+
#
32+
# Parameters:
33+
# NAME: name of target
34+
# ALIAS: alias target name (e.g. tcmalloc::foo)
35+
# HDRS: List of public header files for the library
36+
# SRCS: List of source files for the library
37+
# DEPS: List of other libraries to be linked in
38+
# COPTS: List of private compile options
39+
# DEFINES: List of public defines
40+
# LINKOPTS: List of link options
41+
# PUBLIC: Mark this as a public API target (affects IDE folder)
42+
# TESTONLY: Only build when BUILD_TESTING AND TCMALLOC_BUILD_TESTING
43+
# DISABLE_INSTALL: Skip installation rules for this target
44+
#
2045
function(tcmalloc_cc_library)
21-
cmake_parse_arguments(TCMALLOC "" "NAME;ALIAS" "SRCS;HDRS;COPTS;LINKOPTS;DEPS" ${ARGN})
22-
if(TCMALLOC_SRCS)
23-
if(TCMALLOC_NAME MATCHES ".*_main$")
24-
add_library(${TCMALLOC_NAME} OBJECT "")
25-
else()
26-
add_library(${TCMALLOC_NAME} STATIC "")
27-
endif()
28-
set_target_properties(${TCMALLOC_NAME} PROPERTIES LINKER_LANGUAGE CXX)
29-
target_sources(${TCMALLOC_NAME} PRIVATE ${TCMALLOC_SRCS})
30-
if(TCMALLOC_HDRS)
31-
target_sources(${TCMALLOC_NAME} PRIVATE ${TCMALLOC_HDRS})
32-
endif()
33-
if(TCMALLOC_COPTS)
34-
target_compile_options(${TCMALLOC_NAME} PRIVATE ${TCMALLOC_COPTS})
35-
endif()
36-
if(TCMALLOC_LINKOPTS)
37-
target_link_options(${TCMALLOC_NAME} PRIVATE ${TCMALLOC_LINKOPTS})
46+
cmake_parse_arguments(TCMALLOC_CC_LIB
47+
"DISABLE_INSTALL;PUBLIC;TESTONLY"
48+
"NAME;ALIAS"
49+
"SRCS;HDRS;COPTS;DEFINES;LINKOPTS;DEPS"
50+
${ARGN}
51+
)
52+
53+
if(TCMALLOC_CC_LIB_TESTONLY AND
54+
NOT ((BUILD_TESTING AND TCMALLOC_BUILD_TESTING) OR
55+
(TCMALLOC_BUILD_TEST_HELPERS AND TCMALLOC_CC_LIB_PUBLIC)))
56+
return()
57+
endif()
58+
59+
set(_NAME "${TCMALLOC_CC_LIB_NAME}")
60+
61+
# Check if this is a header-only library by stripping .h/.inc files from SRCS
62+
set(TCMALLOC_CC_SRCS "${TCMALLOC_CC_LIB_SRCS}")
63+
foreach(src_file IN LISTS TCMALLOC_CC_SRCS)
64+
if(${src_file} MATCHES ".*\\.(h|inc)")
65+
list(REMOVE_ITEM TCMALLOC_CC_SRCS "${src_file}")
3866
endif()
39-
if(TCMALLOC_DEPS)
40-
target_link_libraries(${TCMALLOC_NAME} PUBLIC ${TCMALLOC_DEPS})
67+
endforeach()
68+
69+
if(TCMALLOC_CC_SRCS STREQUAL "")
70+
set(TCMALLOC_CC_LIB_IS_INTERFACE 1)
71+
else()
72+
set(TCMALLOC_CC_LIB_IS_INTERFACE 0)
73+
endif()
74+
75+
if(NOT TCMALLOC_CC_LIB_IS_INTERFACE)
76+
if(_NAME MATCHES ".*_main$")
77+
add_library(${_NAME} OBJECT "")
78+
else()
79+
add_library(${_NAME} "")
4180
endif()
42-
target_include_directories(${TCMALLOC_NAME}
81+
82+
set_property(TARGET ${_NAME} PROPERTY LINKER_LANGUAGE "CXX")
83+
84+
target_sources(${_NAME} PRIVATE ${TCMALLOC_CC_LIB_SRCS} ${TCMALLOC_CC_LIB_HDRS})
85+
86+
target_include_directories(${_NAME}
4387
PUBLIC
4488
"$<BUILD_INTERFACE:${TCMALLOC_COMMON_INCLUDE_DIRS}>"
4589
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
4690
)
47-
else()
48-
add_library(${TCMALLOC_NAME} INTERFACE)
49-
if(TCMALLOC_HDRS)
50-
target_sources(${TCMALLOC_NAME} INTERFACE ${TCMALLOC_HDRS})
51-
endif()
52-
if(TCMALLOC_COPTS)
53-
target_compile_options(${TCMALLOC_NAME} INTERFACE ${TCMALLOC_COPTS})
91+
92+
target_compile_options(${_NAME}
93+
PRIVATE ${TCMALLOC_CC_LIB_COPTS})
94+
target_compile_definitions(${_NAME} PUBLIC ${TCMALLOC_CC_LIB_DEFINES})
95+
96+
target_link_libraries(${_NAME}
97+
PUBLIC ${TCMALLOC_CC_LIB_DEPS}
98+
PRIVATE
99+
${TCMALLOC_CC_LIB_LINKOPTS}
100+
)
101+
102+
if(APPLE)
103+
set_target_properties(${_NAME} PROPERTIES
104+
INSTALL_RPATH "@loader_path")
105+
elseif(UNIX)
106+
set_target_properties(${_NAME} PROPERTIES
107+
INSTALL_RPATH "$ORIGIN")
54108
endif()
55-
if(TCMALLOC_LINKOPTS)
56-
target_link_options(${TCMALLOC_NAME} INTERFACE ${TCMALLOC_LINKOPTS})
109+
110+
# IDE folder organization
111+
if(TCMALLOC_CC_LIB_PUBLIC)
112+
set_property(TARGET ${_NAME} PROPERTY FOLDER ${TCMALLOC_IDE_FOLDER})
113+
elseif(TCMALLOC_CC_LIB_TESTONLY)
114+
set_property(TARGET ${_NAME} PROPERTY FOLDER ${TCMALLOC_IDE_FOLDER}/test)
115+
else()
116+
set_property(TARGET ${_NAME} PROPERTY FOLDER ${TCMALLOC_IDE_FOLDER}/internal)
57117
endif()
58-
if(TCMALLOC_DEPS)
59-
target_link_libraries(${TCMALLOC_NAME} INTERFACE ${TCMALLOC_DEPS})
118+
119+
# When being installed, set proper output name and SOVERSION
120+
if(TCMALLOC_ENABLE_INSTALL)
121+
set_target_properties(${_NAME} PROPERTIES
122+
OUTPUT_NAME "tcmalloc_${_NAME}"
123+
SOVERSION "${TCMALLOC_SOVERSION}"
124+
)
60125
endif()
61-
target_include_directories(${TCMALLOC_NAME}
126+
else()
127+
# Header-only (interface) library
128+
add_library(${_NAME} INTERFACE)
129+
target_include_directories(${_NAME}
62130
INTERFACE
63131
"$<BUILD_INTERFACE:${TCMALLOC_COMMON_INCLUDE_DIRS}>"
64132
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
65133
)
134+
target_link_libraries(${_NAME}
135+
INTERFACE
136+
${TCMALLOC_CC_LIB_DEPS}
137+
${TCMALLOC_CC_LIB_LINKOPTS}
138+
)
139+
target_compile_definitions(${_NAME} INTERFACE ${TCMALLOC_CC_LIB_DEFINES})
140+
endif()
141+
142+
# Install target (will be activated in the TCMALLOC_ENABLE_INSTALL commit)
143+
if(TCMALLOC_ENABLE_INSTALL AND NOT TCMALLOC_CC_LIB_DISABLE_INSTALL
144+
AND NOT TCMALLOC_CC_LIB_TESTONLY)
145+
install(TARGETS ${_NAME} EXPORT ${PROJECT_NAME}Targets
146+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
147+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
148+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
149+
)
66150
endif()
67-
if(TCMALLOC_ALIAS)
68-
add_library(${TCMALLOC_ALIAS} ALIAS ${TCMALLOC_NAME})
151+
152+
if(TCMALLOC_CC_LIB_ALIAS)
153+
add_library(${TCMALLOC_CC_LIB_ALIAS} ALIAS ${_NAME})
69154
endif()
70155
endfunction()
71156

157+
# tcmalloc_cc_test()
158+
#
159+
# CMake function to imitate Bazel's cc_test rule, analogous to
160+
# abseil-cpp's absl_cc_test().
161+
#
162+
# Parameters:
163+
# NAME: name of target (creates executable with this name)
164+
# SRCS: List of source files for the binary
165+
# DEPS: List of other libraries to be linked in
166+
# COPTS: List of private compile options
167+
# DEFINES: List of public defines
168+
# LINKOPTS: List of link options
169+
#
72170
function(tcmalloc_cc_test)
73171
if(NOT (BUILD_TESTING AND TCMALLOC_BUILD_TESTING))
74172
return()
75173
endif()
76174

77-
cmake_parse_arguments(TCMALLOC "" "NAME;ALIAS" "SRCS;HDRS;COPTS;LINKOPTS;DEPS" ${ARGN})
78-
add_executable(${TCMALLOC_NAME} "")
79-
if(TCMALLOC_SRCS)
80-
target_sources(${TCMALLOC_NAME} PRIVATE ${TCMALLOC_SRCS})
81-
endif()
82-
if(TCMALLOC_HDRS)
83-
target_sources(${TCMALLOC_NAME} PRIVATE ${TCMALLOC_HDRS})
84-
endif()
85-
if(TCMALLOC_COPTS)
86-
target_compile_options(${TCMALLOC_NAME} PRIVATE ${TCMALLOC_COPTS})
87-
endif()
88-
if(TCMALLOC_LINKOPTS)
89-
target_link_options(${TCMALLOC_NAME} PRIVATE ${TCMALLOC_LINKOPTS})
175+
cmake_parse_arguments(TCMALLOC_CC_TEST
176+
""
177+
"NAME;ALIAS"
178+
"SRCS;HDRS;COPTS;DEFINES;LINKOPTS;DEPS"
179+
${ARGN}
180+
)
181+
182+
set(_NAME "${TCMALLOC_CC_TEST_NAME}")
183+
184+
add_executable(${_NAME} "")
185+
if(TCMALLOC_CC_TEST_SRCS)
186+
target_sources(${_NAME} PRIVATE ${TCMALLOC_CC_TEST_SRCS})
90187
endif()
91-
if(TCMALLOC_DEPS)
92-
target_link_libraries(${TCMALLOC_NAME} PUBLIC ${TCMALLOC_DEPS})
188+
if(TCMALLOC_CC_TEST_HDRS)
189+
target_sources(${_NAME} PRIVATE ${TCMALLOC_CC_TEST_HDRS})
93190
endif()
94-
target_include_directories(${TCMALLOC_NAME}
191+
192+
target_compile_options(${_NAME}
193+
PRIVATE ${TCMALLOC_CC_TEST_COPTS})
194+
target_compile_definitions(${_NAME}
195+
PUBLIC ${TCMALLOC_CC_TEST_DEFINES})
196+
197+
target_link_libraries(${_NAME}
198+
PUBLIC ${TCMALLOC_CC_TEST_DEPS}
199+
PRIVATE ${TCMALLOC_CC_TEST_LINKOPTS}
200+
)
201+
202+
target_include_directories(${_NAME}
95203
PUBLIC "$<BUILD_INTERFACE:${TCMALLOC_COMMON_INCLUDE_DIRS}>")
96-
add_test(NAME ${TCMALLOC_NAME} COMMAND ${TCMALLOC_NAME})
97-
set_tests_properties(${TCMALLOC_NAME} PROPERTIES ENVIRONMENT "TEST_TMPDIR=${CMAKE_CURRENT_BINARY_DIR};TEST_SRCDIR=${CMAKE_SOURCE_DIR}")
204+
205+
# IDE folder organization
206+
set_property(TARGET ${_NAME} PROPERTY FOLDER ${TCMALLOC_IDE_FOLDER}/test)
207+
208+
add_test(NAME ${_NAME} COMMAND ${_NAME})
209+
set_tests_properties(${_NAME} PROPERTIES
210+
ENVIRONMENT "TEST_TMPDIR=${CMAKE_CURRENT_BINARY_DIR};TEST_SRCDIR=${CMAKE_SOURCE_DIR}")
98211
endfunction()
99212

100213
function(tcmalloc_cc_binary)

0 commit comments

Comments
 (0)