Skip to content

Commit f5275b5

Browse files
committed
cmake: Add TCMALLOC_ENABLE_INSTALL with export/config infrastructure
Add a complete install and export system, mirroring abseil-cpp's ABSL_ENABLE_INSTALL mechanism. Key changes: - Add option(TCMALLOC_ENABLE_INSTALL): defaults to OFF when used as a subproject, ON when standalone (exactly matching Abseil's behavior) - Remove the hard-coded CMAKE_SKIP_INSTALL_RULES=ON - Create CMake/tcmallocConfig.cmake.in template (analogous to CMake/abslConfig.cmake.in) that finds Threads and absl dependencies then includes the exported targets file - Add install(EXPORT tcmallocTargets) with tcmalloc:: namespace - Add configure_package_config_file() to generate tcmallocConfig.cmake - Add install(DIRECTORY tcmalloc ...) for public headers, excluding test-only headers (testing/, mock_*) After this change, downstream projects can use: find_package(tcmalloc REQUIRED) target_link_libraries(myapp PRIVATE tcmalloc::tcmalloc) The install rules in tcmalloc_cc_library() (added in the previous commit) are automatically activated when TCMALLOC_ENABLE_INSTALL is ON.
1 parent d74057b commit f5275b5

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

CMake/tcmallocConfig.cmake.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# tcmalloc CMake configuration file.
2+
3+
include(CMakeFindDependencyMacro)
4+
find_dependency(Threads)
5+
find_dependency(absl)
6+
7+
@PACKAGE_INIT@
8+
9+
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")

CMakeLists.txt

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ set(CMAKE_INSTALL_RPATH "$ORIGIN")
2828
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH ON)
2929
set(CMAKE_BUILD_RPATH_USE_ORIGIN ON)
3030

31-
set(CMAKE_SKIP_INSTALL_RULES ON)
31+
# When tcmalloc is included as a subproject (i.e. using
32+
# add_subdirectory(tcmalloc)), install rules are disabled by default.
33+
if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
34+
option(TCMALLOC_ENABLE_INSTALL "Enable install rule" OFF)
35+
else()
36+
option(TCMALLOC_ENABLE_INSTALL "Enable install rule" ON)
37+
endif()
38+
3239
set(ABSL_PROPAGATE_CXX_STD ON)
3340
set(protobuf_ABSL_PROVIDER "package" CACHE STRING "" FORCE)
3441
set(protobuf_INSTALL OFF CACHE BOOL "" FORCE)
@@ -201,3 +208,31 @@ include(TcmallocVariants)
201208
add_subdirectory(tcmalloc/internal)
202209
add_subdirectory(tcmalloc/testing)
203210
add_subdirectory(tcmalloc)
211+
212+
if(TCMALLOC_ENABLE_INSTALL)
213+
# Export targets with tcmalloc:: namespace
214+
install(EXPORT ${PROJECT_NAME}Targets
215+
NAMESPACE tcmalloc::
216+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
217+
)
218+
219+
# Generate and install the config file from template
220+
configure_package_config_file(
221+
CMake/tcmallocConfig.cmake.in
222+
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
223+
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
224+
)
225+
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
226+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
227+
)
228+
229+
# Install public headers
230+
install(DIRECTORY tcmalloc
231+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
232+
FILES_MATCHING
233+
PATTERN "*.h"
234+
PATTERN "*.inc"
235+
PATTERN "testing" EXCLUDE
236+
PATTERN "mock_*" EXCLUDE
237+
)
238+
endif()

0 commit comments

Comments
 (0)