Skip to content

Commit c3a3dda

Browse files
committed
cmake: Use generator expression include directories
Replace raw ${CMAKE_SOURCE_DIR} include directory references with proper CMake generator expressions using BUILD_INTERFACE and INSTALL_INTERFACE, mirroring abseil-cpp's ABSL_COMMON_INCLUDE_DIRS pattern: - Define TCMALLOC_COMMON_INCLUDE_DIRS at module scope - In tcmalloc_cc_library(): use $<BUILD_INTERFACE:...> for build tree and $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> for installs - In tcmalloc_cc_test(): use $<BUILD_INTERFACE:...> (tests are never installed so no INSTALL_INTERFACE needed) This fixes correctness when tcmalloc is used as a subdirectory of another project (where CMAKE_SOURCE_DIR points to the parent) and enables proper include paths in installed config files.
1 parent fcd9b33 commit c3a3dda

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

CMake/TcmallocHelpers.cmake

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
include(CMakeParseArguments)
16+
17+
# include current path
18+
list(APPEND TCMALLOC_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
19+
1520
function(tcmalloc_cc_library)
1621
cmake_parse_arguments(TCMALLOC "" "NAME;ALIAS" "SRCS;HDRS;COPTS;LINKOPTS;DEPS" ${ARGN})
1722
if(TCMALLOC_SRCS)
@@ -34,7 +39,11 @@ function(tcmalloc_cc_library)
3439
if(TCMALLOC_DEPS)
3540
target_link_libraries(${TCMALLOC_NAME} PUBLIC ${TCMALLOC_DEPS})
3641
endif()
37-
target_include_directories(${TCMALLOC_NAME} PUBLIC ${CMAKE_SOURCE_DIR})
42+
target_include_directories(${TCMALLOC_NAME}
43+
PUBLIC
44+
"$<BUILD_INTERFACE:${TCMALLOC_COMMON_INCLUDE_DIRS}>"
45+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
46+
)
3847
else()
3948
add_library(${TCMALLOC_NAME} INTERFACE)
4049
if(TCMALLOC_HDRS)
@@ -49,7 +58,11 @@ function(tcmalloc_cc_library)
4958
if(TCMALLOC_DEPS)
5059
target_link_libraries(${TCMALLOC_NAME} INTERFACE ${TCMALLOC_DEPS})
5160
endif()
52-
target_include_directories(${TCMALLOC_NAME} INTERFACE ${CMAKE_SOURCE_DIR})
61+
target_include_directories(${TCMALLOC_NAME}
62+
INTERFACE
63+
"$<BUILD_INTERFACE:${TCMALLOC_COMMON_INCLUDE_DIRS}>"
64+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
65+
)
5366
endif()
5467
if(TCMALLOC_ALIAS)
5568
add_library(${TCMALLOC_ALIAS} ALIAS ${TCMALLOC_NAME})
@@ -78,7 +91,8 @@ function(tcmalloc_cc_test)
7891
if(TCMALLOC_DEPS)
7992
target_link_libraries(${TCMALLOC_NAME} PUBLIC ${TCMALLOC_DEPS})
8093
endif()
81-
target_include_directories(${TCMALLOC_NAME} PUBLIC ${CMAKE_SOURCE_DIR})
94+
target_include_directories(${TCMALLOC_NAME}
95+
PUBLIC "$<BUILD_INTERFACE:${TCMALLOC_COMMON_INCLUDE_DIRS}>")
8296
add_test(NAME ${TCMALLOC_NAME} COMMAND ${TCMALLOC_NAME})
8397
set_tests_properties(${TCMALLOC_NAME} PROPERTIES ENVIRONMENT "TEST_TMPDIR=${CMAKE_CURRENT_BINARY_DIR};TEST_SRCDIR=${CMAKE_SOURCE_DIR}")
8498
endfunction()

0 commit comments

Comments
 (0)