Skip to content

Commit d8ec02c

Browse files
committed
cmake: rework generation of bitpit CMake configuration files
1 parent 49a082e commit d8ec02c

File tree

10 files changed

+328
-728
lines changed

10 files changed

+328
-728
lines changed

CMakeLists.txt

Lines changed: 240 additions & 236 deletions
Large diffs are not rendered by default.

cmake/BITPITConfig.cmake.in

Lines changed: 25 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,9 @@
1-
# BITPITConfig.cmake - bitpit CMake configuration file for external projects.
2-
# -----------
3-
#
4-
# This file is configured by bitpit and used by the UseBITPIT.cmake module
5-
# to load bitpit's settings for an external project.
6-
7-
# Compute the installation prefix from this BITPITConfig.cmake file location.
8-
@BITPIT_INSTALL_PREFIX_CODE@
9-
10-
# If a different UseBITPIT.cmake was previoulsy loaded a reconfiguration
11-
# is needed
12-
if(NOT ("${BITPIT_INSTALL_PREFIX}/@BITPIT_INSTALL_CMAKEDIR@" STREQUAL BITPIT_INSTALL_CMAKEDIR_PREVIOUS))
13-
set(BITPIT_RECONFIGURE 1)
14-
set(BITPIT_INSTALL_CMAKEDIR_PREVIOUS "${BITPIT_INSTALL_PREFIX}/@BITPIT_INSTALL_CMAKEDIR@" CACHE INTERNAL "Defines the previous bitpit CMake configuration file loaded")
15-
endif()
16-
17-
# The C and C++ flags added by bitpit to the cmake-configured flags.
18-
SET(BITPIT_REQUIRED_C_FLAGS "")
19-
SET(BITPIT_REQUIRED_CXX_FLAGS "")
20-
SET(BITPIT_REQUIRED_EXE_LINKER_FLAGS "")
21-
SET(BITPIT_REQUIRED_SHARED_LINKER_FLAGS "")
22-
SET(BITPIT_REQUIRED_MODULE_LINKER_FLAGS "")
23-
24-
# The bitpit version number
25-
SET(BITPIT_MAJOR_VERSION "@BITPIT_MAJOR_VERSION@")
26-
SET(BITPIT_MINOR_VERSION "@BITPIT_MINOR_VERSION@")
27-
SET(BITPIT_PATCH_VERSION "@BITPIT_PATCH_VERSION@")
28-
SET(BITPIT_VERSION "@BITPIT_VERSION@")
29-
30-
# The location of the UseBITPIT.cmake file.
31-
SET(BITPIT_CMAKE_DIR "${BITPIT_INSTALL_PREFIX}/@BITPIT_INSTALL_CMAKEDIR@")
32-
SET(BITPIT_USE_FILE "${BITPIT_CMAKE_DIR}/UseBITPIT.cmake")
33-
34-
# Flag for shared build
35-
SET(BITPIT_SHARED "@BUILD_SHARED_LIBS@")
36-
37-
# Include macros for finding packages
38-
list(APPEND CMAKE_MODULE_PATH ${BITPIT_CMAKE_DIR})
39-
include(LibFindMacros)
40-
include(FindPackageHandleStandardArgs)
1+
@PACKAGE_INIT@
412

423
#-----------------------------------------------------------------------------
43-
# Find bitpit libraries and headers
4+
# Modules
445
#-----------------------------------------------------------------------------
456

46-
# Headers
47-
if(BITPIT_RECONFIGURE)
48-
unset(BITPIT_INCLUDE_DIR CACHE)
49-
endif()
50-
51-
find_path(BITPIT_INCLUDE_DIR "@[email protected]"
52-
HINTS "${BITPIT_INSTALL_PREFIX}/@CMAKE_INSTALL_INCLUDEDIR@/@PROJECT_NAME@/")
53-
54-
mark_as_advanced(BITPIT_INCLUDE_DIR)
55-
56-
# Library
57-
if(BITPIT_RECONFIGURE)
58-
unset(BITPIT_LIBRARY_RELEASE CACHE)
59-
unset(BITPIT_LIBRARY_DEBUG CACHE)
60-
endif()
61-
62-
find_library(BITPIT_LIBRARY_RELEASE
63-
NAMES @PROJECT_NAME@@BITPIT_RELEASE_POSTFIX@ @PROJECT_NAME@
64-
HINTS "${BITPIT_INSTALL_PREFIX}/@CMAKE_INSTALL_LIBDIR@")
65-
66-
find_library(BITPIT_LIBRARY_DEBUG
67-
NAMES @PROJECT_NAME@@BITPIT_DEBUG_POSTFIX@ @PROJECT_NAME@
68-
HINTS "${BITPIT_INSTALL_PREFIX}/@CMAKE_INSTALL_LIBDIR@")
69-
70-
mark_as_advanced(BITPIT_LIBRARY_RELEASE)
71-
mark_as_advanced(BITPIT_LIBRARY_DEBUG)
72-
73-
# Choose good values for BITPIT_LIBRARY, BITPIT_LIBRARIES,
74-
# BITPIT_LIBRARY_DEBUG, and BITPIT_LIBRARY_RELEASE depending on what
75-
# has been found and set. If only BITPIT_LIBRARY_RELEASE is defined,
76-
# BITPIT_LIBRARY will be set to the release value, and
77-
# BITPIT_LIBRARY_DEBUG will be set to BITPIT_LIBRARY_DEBUG-NOTFOUND.
78-
# If only BITPIT_LIBRARY_DEBUG is defined, then BITPIT_LIBRARY will
79-
# take the debug value, and BITPIT_LIBRARY_RELEASE will be set to
80-
# BITPIT_LIBRARY_RELEASE-NOTFOUND.
81-
#
82-
# If the generator supports configuration types, then BITPIT_LIBRARY
83-
# and BITPIT_LIBRARIES will be set with debug and optimized flags
84-
# specifying the library to be used for the given configuration. If no
85-
# build type has been set or the generator in use does not support
86-
# configuration types, then BITPIT_LIBRARY and BITPIT_LIBRARIES will
87-
# take only the release value, or the debug value if the release one is
88-
# not set.
89-
if (BITPIT_LIBRARY_DEBUG AND BITPIT_LIBRARY_RELEASE AND
90-
NOT BITPIT_LIBRARY_DEBUG STREQUAL BITPIT_LIBRARY_RELEASE AND
91-
(CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE))
92-
set( BITPIT_LIBRARY "" )
93-
foreach( _libname IN LISTS BITPIT_LIBRARY_RELEASE )
94-
list( APPEND BITPIT_LIBRARY optimized "${_libname}" )
95-
endforeach()
96-
foreach( _libname IN LISTS BITPIT_LIBRARY_DEBUG )
97-
list( APPEND BITPIT_LIBRARY debug "${_libname}" )
98-
endforeach()
99-
elseif (BITPIT_LIBRARY_RELEASE)
100-
set (BITPIT_LIBRARY ${BITPIT_LIBRARY_RELEASE})
101-
elseif (BITPIT_LIBRARY_DEBUG)
102-
set (BITPIT_LIBRARY ${BITPIT_LIBRARY_DEBUG})
103-
else ()
104-
set( BITPIT_LIBRARY "BITPIT_LIBRARY-NOTFOUND")
105-
endif ()
106-
107-
# bitpit Definitions
108-
set(BITPIT_DEFINITIONS "@BITPIT_INTERFACE_COMPILE_DEFINITIONS@")
109-
1107
# List of currently enabled bitpit modules
1118
set(BITPIT_ENABLED_MODULE_LIST "@BITPIT_ENABLED_MODULE_LIST@")
1129

@@ -143,18 +40,24 @@ if(BITPIT_FIND_OPTIONAL_COMPONENTS)
14340
endforeach()
14441
endif()
14542

146-
# Unset the unneeded variables
147-
if(BITPIT_RECONFIGURE)
148-
unset(BITPIT_RECONFIGURE)
149-
endif()
43+
#-----------------------------------------------------------------------------
44+
# Location of UseBITPIT.cmake file.
45+
#-----------------------------------------------------------------------------
46+
47+
SET(BITPIT_USE_FILE "@CMAKE_INSTALL_PREFIX@/@BITPIT_INSTALL_CMAKEDIR@/UseBITPIT.cmake")
48+
49+
#-----------------------------------------------------------------------------
50+
# Programming languages
51+
#-----------------------------------------------------------------------------
15052

151-
# Let libfind_process initialize the appropriate variables
152-
libfind_process(BITPIT)
53+
set(BITPIT_LANGUAGES "@BITPIT_LANGUAGES@")
15354

15455
#-----------------------------------------------------------------------------
15556
# Find bitpit external dependencies
15657
#-----------------------------------------------------------------------------
15758

59+
list(APPEND CMAKE_MODULE_PATH "@CMAKE_INSTALL_PREFIX@/@BITPIT_INSTALL_CMAKEDIR@")
60+
15861
# Set external dependencies information
15962
set(_EXTERNAL_DEPENDENCIES "@BITPIT_EXTERNAL_DEPENDENCIES@")
16063
set(_EXTERNAL_VARIABLES_LIBRARIES "@BITPIT_EXTERNAL_VARIABLES_LIBRARIES@")
@@ -199,29 +102,21 @@ foreach (VARIABLE_NAME IN LISTS _EXTERNAL_VARIABLES_INCLUDE_DIRS)
199102
endforeach ()
200103

201104
#-----------------------------------------------------------------------------
202-
# Set programming languages
105+
# Include targets
203106
#-----------------------------------------------------------------------------
204-
set(BITPIT_LANGUAGES "@BITPIT_LANGUAGES@")
107+
include ( "${CMAKE_CURRENT_LIST_DIR}/@BITPIT_CMAKE_TARGETS_FILE@" )
205108

206109
#-----------------------------------------------------------------------------
207-
# Create imported target
110+
# Backwards compatibility
208111
#-----------------------------------------------------------------------------
209-
if(BITPIT_SHARED)
210-
set(LIBRARY_TYPE SHARED)
211-
else()
212-
set(LIBRARY_TYPE STATIC)
213-
endif()
214112

215-
if(NOT TARGET bitpit::bitpit)
216-
add_library(bitpit::bitpit ${LIBRARY_TYPE} IMPORTED GLOBAL)
113+
# Definitions
114+
get_target_property(BITPIT_DEFINITIONS @BITPIT_LIBRARY@::@BITPIT_LIBRARY@ INTERFACE_COMPILE_DEFINITIONS)
217115

218-
set_target_properties(bitpit::bitpit
219-
PROPERTIES
220-
IMPORTED_LOCATION "${BITPIT_LIBRARY}"
221-
INTERFACE_INCLUDE_DIRECTORIES "${BITPIT_INCLUDE_DIRS}"
222-
INTERFACE_COMPILE_DEFINITIONS "${BITPIT_DEFINITIONS}")
116+
# Include directories
117+
get_target_property(BITPIT_INCLUDE_DIRS @BITPIT_LIBRARY@::@BITPIT_LIBRARY@ INTERFACE_INCLUDE_DIRECTORIES)
223118

224-
# The property INTERFACE_LINK_LIBRARIES is set using target_link_libraries so that the debug
225-
# and optimized keywords work.
226-
target_link_libraries(bitpit::bitpit INTERFACE ${BITPIT_LIBRARIES})
227-
endif()
119+
# Include libraries
120+
get_target_property(BITPIT_LIBRARY @BITPIT_LIBRARY@::@BITPIT_LIBRARY@ LOCATION)
121+
get_target_property(BITPIT_LINK_LIBRARIES @BITPIT_LIBRARY@::@BITPIT_LIBRARY@ INTERFACE_LINK_LIBRARIES)
122+
set(BITPIT_LIBRARIES "${BITPIT_LINK_LIBRARIES};${BITPIT_LIBRARY}")

cmake/BITPITConfigVersion.cmake.in

Lines changed: 0 additions & 47 deletions
This file was deleted.

cmake/CMakeLists.txt

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,23 @@
2222
#
2323
#---------------------------------------------------------------------------*/
2424

25+
include(CMakePackageConfigHelpers)
26+
27+
string(TOUPPER ${BITPIT_LIBRARY} UPPER_BITPIT_LIBRARY_NAME)
28+
2529
#------------------------------------------------------------------------------------#
26-
# Generate BITPITConfig.cmake
30+
# CMake configuration file
2731
#------------------------------------------------------------------------------------#
28-
set(BITPIT_INSTALL_PREFIX_CODE
29-
"set(_bitpit_installed_prefix \"${CMAKE_INSTALL_PREFIX}/${BITPIT_INSTALL_CMAKEDIR}\")
30-
set(_bitpit_requested_prefix \"\${CMAKE_CURRENT_LIST_DIR}\")
31-
get_filename_component(_bitpit_installed_prefix_full \"\${_bitpit_installed_prefix}\" REALPATH)
32-
get_filename_component(_bitpit_requested_prefix_full \"\${_bitpit_requested_prefix}\" REALPATH)
33-
if (_bitpit_installed_prefix_full STREQUAL _bitpit_requested_prefix_full)
34-
set(BITPIT_INSTALL_PREFIX \"${CMAKE_INSTALL_PREFIX}\")
35-
else ()
36-
set(BITPIT_INSTALL_PREFIX \"\${CMAKE_CURRENT_LIST_DIR}\")")
37-
38-
# Construct the proper number of get_filename_component(... PATH)
39-
# calls to compute the installation prefix.
40-
string(REGEX REPLACE "/" ";" _count "${BITPIT_INSTALL_CMAKEDIR}")
41-
foreach(p ${_count})
42-
set(BITPIT_INSTALL_PREFIX_CODE "${BITPIT_INSTALL_PREFIX_CODE}
43-
get_filename_component(BITPIT_INSTALL_PREFIX \"\${BITPIT_INSTALL_PREFIX}\" PATH)")
44-
endforeach()
4532

46-
set(BITPIT_INSTALL_PREFIX_CODE "${BITPIT_INSTALL_PREFIX_CODE}
47-
endif ()")
33+
set (BITPIT_CMAKE_TARGETS_FILE "${UPPER_BITPIT_LIBRARY_NAME}Targets.cmake")
4834

49-
get_target_property(BITPIT_DEBUG_POSTFIX ${BITPIT_LIBRARY} DEBUG_POSTFIX)
50-
get_target_property(BITPIT_RELEASE_POSTFIX ${BITPIT_LIBRARY} RELEASE_POSTFIX)
51-
get_target_property(BITPIT_INTERFACE_COMPILE_DEFINITIONS ${BITPIT_LIBRARY} INTERFACE_COMPILE_DEFINITIONS)
35+
install(TARGETS ${BITPIT_LIBRARY} EXPORT ${BITPIT_LIBRARY}Targets)
36+
37+
install(EXPORT ${BITPIT_LIBRARY}Targets
38+
FILE ${BITPIT_CMAKE_TARGETS_FILE}
39+
NAMESPACE ${BITPIT_LIBRARY}::
40+
DESTINATION ${BITPIT_INSTALL_CMAKEDIR}
41+
)
5242

5343
set(BITPIT_ENABLED_MODULE_LIST "")
5444
foreach(MODULE_NAME IN LISTS BITPIT_MODULE_LIST)
@@ -58,32 +48,50 @@ foreach(MODULE_NAME IN LISTS BITPIT_MODULE_LIST)
5848
endif()
5949
endforeach()
6050

61-
configure_file("BITPITConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/BITPITConfig.cmake" @ONLY)
51+
set (BITPIT_CMAKE_PACKAGE_FILE "${UPPER_BITPIT_LIBRARY_NAME}Config.cmake")
52+
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/${BITPIT_CMAKE_PACKAGE_FILE}.in
53+
"${CMAKE_CURRENT_BINARY_DIR}/${BITPIT_CMAKE_PACKAGE_FILE}"
54+
INSTALL_DESTINATION "${BITPIT_INSTALL_CMAKEDIR}"
55+
NO_SET_AND_CHECK_MACRO
56+
NO_CHECK_REQUIRED_COMPONENTS_MACRO
57+
)
6258

6359
unset(BITPIT_ENABLED_MODULE_LIST)
64-
unset(BITPIT_INTERFACE_COMPILE_DEFINITIONS)
65-
unset(BITPIT_RELEASE_POSTFIX)
66-
unset(BITPIT_DEBUG_POSTFIX)
67-
unset(BITPIT_INSTALL_PREFIX_CODE)
60+
61+
install(FILES
62+
${CMAKE_CURRENT_BINARY_DIR}/${BITPIT_CMAKE_PACKAGE_FILE}
63+
DESTINATION "${BITPIT_INSTALL_CMAKEDIR}"
64+
)
6865

6966
#------------------------------------------------------------------------------------#
70-
# Generate BITPITConfigVersion.cmake.in
67+
# CMake version file
7168
#------------------------------------------------------------------------------------#
7269

73-
get_target_property(BITPIT_INTERFACE_COMPILE_DEFINITIONS ${BITPIT_LIBRARY} INTERFACE_COMPILE_DEFINITIONS)
70+
set (BITPIT_CMAKE_CONFIG_VERSION_FILE "${UPPER_BITPIT_LIBRARY_NAME}ConfigVersion.cmake")
71+
write_basic_package_version_file(
72+
${CMAKE_CURRENT_BINARY_DIR}/${BITPIT_CMAKE_CONFIG_VERSION_FILE}
73+
VERSION ${BITPIT_VERSION}
74+
COMPATIBILITY AnyNewerVersion
75+
)
7476

75-
configure_file("BITPITConfigVersion.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/BITPITConfigVersion.cmake" @ONLY)
76-
77-
unset(BITPIT_INTERFACE_COMPILE_DEFINITIONS)
77+
install(FILES
78+
${CMAKE_CURRENT_BINARY_DIR}/${BITPIT_CMAKE_CONFIG_VERSION_FILE}
79+
DESTINATION "${BITPIT_INSTALL_CMAKEDIR}"
80+
)
7881

7982
#------------------------------------------------------------------------------------#
80-
# CMake targets
83+
# CMake find package file
8184
#------------------------------------------------------------------------------------#
82-
add_custom_target(clean-cmake COMMAND ${CMAKE_MAKE_PROGRAM} clean WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
83-
84-
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/BITPITConfig.cmake" DESTINATION "${BITPIT_INSTALL_CMAKEDIR}")
85-
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/BITPITConfigVersion.cmake" DESTINATION "${BITPIT_INSTALL_CMAKEDIR}")
8685

8786
install(FILES "FindBITPIT.cmake" DESTINATION "${BITPIT_INSTALL_CMAKEDIR}")
87+
88+
#------------------------------------------------------------------------------------#
89+
# CMake use package file
90+
#------------------------------------------------------------------------------------#
91+
8892
install(FILES "UseBITPIT.cmake" DESTINATION "${BITPIT_INSTALL_CMAKEDIR}")
89-
install(FILES "LibFindMacros.cmake" DESTINATION "${BITPIT_INSTALL_CMAKEDIR}")
93+
94+
#------------------------------------------------------------------------------------#
95+
# CMake targets
96+
#------------------------------------------------------------------------------------#
97+
add_custom_target(clean-cmake COMMAND ${CMAKE_MAKE_PROGRAM} clean WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

cmake/FindBITPIT.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
# Assume not found.
3535
set(BITPIT_FOUND 0)
3636

37+
# Warn that the usage of this file is deprecated.
38+
message(WARNING "FindBITPIT.cmake is deprecated and should not be used in new projects.")
39+
3740
# Use the Config mode of the find_package() command to find BITPITConfig.
3841
# If this succeeds (possibly because BITPIT_DIR is already set), the
3942
# command will have already loaded BITPITConfig.cmake and set BITPIT_FOUND.

0 commit comments

Comments
 (0)