cmake: Set CMOCKA_LIBRARIES in package config for backwards compatibility

Projects using cmocka could have done this successfully from version 1.0 to
1.1.5 to build against cmocka:

```
find_package(cmocka 1.0 REQUIRED CONFIG)
```

and later

```
target_link_libraries(myapp
	${CMOCKA_LIBRARIES}
)
```

Modern apps should just "link" against 'cmocka::cmocka' instead like it's done in
examples already.  To not break old builds (as it is the case with cmocka
release 1.1.7) we can put that modern target string into the variable
CMOCKA_LIBRARIES and thus trick old projects to just use that, keeping
compatibility until those projects explicitly use the modern version.

Link: https://cmake.org/cmake/help/latest/command/install.html#install-export
Link: https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#package-configuration-file
Link: https://cmake.org/cmake/help/latest/module/CMakePackageConfigHelpers.html#module:CMakePackageConfigHelpers
Fixes: #87

Signed-off-by: Alexander Dahl <ada@thorsis.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Alexander Dahl 2023-03-17 10:06:12 +01:00 committed by Andreas Schneider
parent 408ab76759
commit de67675842
3 changed files with 21 additions and 3 deletions

View File

@ -77,11 +77,19 @@ install(
pkgconfig
)
configure_package_config_file(cmocka-config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/cmocka-config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cmocka"
NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO
)
write_basic_package_version_file(${PROJECT_NAME}-config-version.cmake
COMPATIBILITY
AnyNewerVersion)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/cmocka-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
COMPONENT devel)

10
cmocka-config.cmake.in Normal file
View File

@ -0,0 +1,10 @@
@PACKAGE_INIT@
get_filename_component(cmocka_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
if(NOT TARGET cmocka::cmocka)
include("${cmocka_CMAKE_DIR}/cmocka-targets.cmake")
endif()
# for backwards compatibility
set(CMOCKA_LIBRARY cmocka::cmocka)
set(CMOCKA_LIBRARIES cmocka::cmocka)

View File

@ -60,13 +60,13 @@ set_target_properties(cmocka PROPERTIES
add_library(cmocka::cmocka ALIAS cmocka)
install(TARGETS cmocka
EXPORT cmocka-config
EXPORT cmocka-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT ${PROJECT_NAME})
install(EXPORT cmocka-config
install(EXPORT cmocka-targets
NAMESPACE cmocka::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cmocka)