Skip to content

Commit 38e43a9

Browse files
authored
Cmake improvement (#348)
* Used target_compile_definitions instead of global CMake compile definition * Prepends to cmake module path instead of appending * Added namespace to exported targets * Do not overwrite COMPILE_DEFITIONS with set_properties
1 parent 733eae0 commit 38e43a9

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

CMakeLists.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ if(CMAKE_CXX_COMPILER_LAUNCHER)
4747
message(STATUS "Using C++ compiler launcher: ${CMAKE_CXX_COMPILER_LAUNCHER}")
4848
endif()
4949

50-
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
50+
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
5151
message(DEBUG "CMake module path: ${CMAKE_MODULE_PATH}")
5252

5353
# Versionning
@@ -133,16 +133,17 @@ include(sanitizers)
133133
# ============
134134

135135
set(SPARROW_INTERFACE_DEPENDENCIES "" CACHE STRING "List of dependencies to be linked to the sparrow target")
136+
set(SPARROW_COMPILE_DEFINITIONS "" CACHE STRING "List of public compile definitions of the sparrow target")
136137

137138
if (USE_DATE_POLYFILL)
138139
find_package(date CONFIG REQUIRED)
139140
list(APPEND SPARROW_INTERFACE_DEPENDENCIES date::date date::date-tz)
140-
add_compile_definitions(SPARROW_USE_DATE_POLYFILL)
141+
list(APPEND SPARROW_COMPILE_DEFINITIONS SPARROW_USE_DATE_POLYFILL)
141142
endif()
142143

143144
if(USE_LARGE_INT_PLACEHOLDERS)
144145
message(STATUS "Using large int placeholders")
145-
add_compile_definitions(SPARROW_USE_LARGE_INT_PLACEHOLDERS)
146+
list(APPEND SPARROW_COMPILE_DEFINITIONS SPARROW_USE_LARGE_INT_PLACEHOLDERS)
146147
endif()
147148

148149
# Build
@@ -261,6 +262,7 @@ else()
261262
endif()
262263

263264
add_library(sparrow SHARED ${SPARROW_HEADERS} ${SPARROW_SRC})
265+
target_compile_definitions(sparrow PUBLIC ${SPARROW_COMPILE_DEFINITIONS})
264266

265267
# TODO: handle static lib
266268
if (UNIX)
@@ -275,16 +277,16 @@ if (UNIX)
275277
PROPERTIES
276278
VERSION "${SPARROW_BINARY_COMPATIBLE}.${SPARROW_BINARY_REVISION}.${SPARROW_BINARY_AGE}"
277279
SOVERSION ${SPARROW_BINARY_COMPATIBLE}
278-
COMPILE_OPTIONS "-fvisibility=hidden"
279280
)
281+
target_compile_options(sparrow PRIVATE "-fvisibility=hidden")
280282
else ()
281283
set_target_properties(
282284
sparrow
283285
PROPERTIES
284286
VERSION ${SPARROW_BINARY_VERSION}
285287
SOVERSION ${SPARROW_BINARY_CURRENT}
286-
COMPILE_DEFINITIONS "SPARROW_EXPORTS"
287288
)
289+
target_compile_definitions(sparrow PRIVATE SPARROW_EXPORTS)
288290
endif ()
289291
target_include_directories(sparrow PUBLIC
290292
$<BUILD_INTERFACE:${SPARROW_INCLUDE_DIR}>
@@ -328,7 +330,8 @@ install(TARGETS sparrow
328330

329331
# Makes the project importable from the build directory
330332
export(EXPORT ${PROJECT_NAME}-targets
331-
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")
333+
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake"
334+
NAMESPACE sparrow::)
332335

333336
install(DIRECTORY ${SPARROW_INCLUDE_DIR}/sparrow
334337
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
@@ -356,5 +359,6 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
356359
DESTINATION ${SPARROW_CMAKECONFIG_INSTALL_DIR})
357360
install(EXPORT ${PROJECT_NAME}-targets
358361
FILE ${PROJECT_NAME}Targets.cmake
362+
NAMESPACE sparrow::
359363
DESTINATION ${SPARROW_CMAKECONFIG_INSTALL_DIR})
360364

sparrowConfig.cmake.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919
include(CMakeFindDependencyMacro)
2020

21-
if(NOT TARGET @PROJECT_NAME@)
21+
if(NOT TARGET sparrow::sparrow)
2222
include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
23-
get_target_property(@PROJECT_NAME@_INCLUDE_DIRS @PROJECT_NAME@ INTERFACE_INCLUDE_DIRECTORIES)
23+
get_target_property(@PROJECT_NAME@_INCLUDE_DIRS sparrow::sparrow INTERFACE_INCLUDE_DIRECTORIES)
24+
get_target_property(@PROJECT_NAME@_LIBRARY sparrow::sparrow LOCATION)
2425
endif()
2526

0 commit comments

Comments
 (0)