From 95faf1f8a5514e8ab54db1c7c422c3d3bc2de47a Mon Sep 17 00:00:00 2001 From: Eero Aaltonen Date: Fri, 14 Oct 2022 17:35:54 +0300 Subject: [PATCH 1/3] gitignore CMake build directories Using a separate build directory is a very common way of using CMake so add an ignore pattern for those. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 6d7eb1c..c98b187 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,9 @@ naval_fate run_testcase run_testcase.exe +# CMake build directories +/*build*/ + # CMake temporary files CMakeCache.txt CMakeFiles From a820d385662d5725a13e3acfe35d00344be53532 Mon Sep 17 00:00:00 2001 From: Eero Aaltonen Date: Fri, 14 Oct 2022 17:38:42 +0300 Subject: [PATCH 2/3] Write CMake config-file package using helper Write our CMake config-file package using the `configure_package_config_file` function defined in CMakePackageConfigHelpers.cmake and export our library in a namespace so that it can be used as an imported target. --- CMakeLists.txt | 16 +++++++++++++--- docopt-config.cmake => docopt-config.cmake.in | 4 ++++ 2 files changed, 17 insertions(+), 3 deletions(-) rename docopt-config.cmake => docopt-config.cmake.in (53%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 28da614..da29dde 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,9 +101,19 @@ install(FILES ${docopt_HEADERS} DESTINATION include/docopt) # CMake Package include(CMakePackageConfigHelpers) -write_basic_package_version_file("${PROJECT_BINARY_DIR}/docopt-config-version.cmake" COMPATIBILITY SameMajorVersion) -install(FILES docopt-config.cmake ${PROJECT_BINARY_DIR}/docopt-config-version.cmake DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/docopt") -install(EXPORT ${export_name} DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/docopt") +set(ConfigPackageLocation "cmake/docopt") +# Unix layout, ref https://cmake.org/cmake/help/latest/command/find_package.html#config-mode-search-procedure +configure_package_config_file(docopt-config.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/${ConfigPackageLocation}/docopt-config.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/${ConfigPackageLocation}") +write_basic_package_version_file("${PROJECT_BINARY_DIR}/${ConfigPackageLocation}/docopt-config-version.cmake" + COMPATIBILITY SameMajorVersion) +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${ConfigPackageLocation}/docopt-config.cmake" + ${PROJECT_BINARY_DIR}/${ConfigPackageLocation}/docopt-config-version.cmake + DESTINATION "${CMAKE_INSTALL_LIBDIR}/${ConfigPackageLocation}") +install(EXPORT ${export_name} + DESTINATION "${CMAKE_INSTALL_LIBDIR}/${ConfigPackageLocation}" + NAMESPACE Docopt::) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docopt.pc.in ${CMAKE_CURRENT_BINARY_DIR}/docopt.pc @ONLY) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/docopt.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) diff --git a/docopt-config.cmake b/docopt-config.cmake.in similarity index 53% rename from docopt-config.cmake rename to docopt-config.cmake.in index 33c36c0..1782482 100644 --- a/docopt-config.cmake +++ b/docopt-config.cmake.in @@ -1 +1,5 @@ +@PACKAGE_INIT@ + include("${CMAKE_CURRENT_LIST_DIR}/docopt-targets.cmake") + +check_required_components(docopt) From 1ea7bd5558df2bbbbf858662791756d67a47bd9f Mon Sep 17 00:00:00 2001 From: Eero Aaltonen Date: Fri, 14 Oct 2022 17:52:45 +0300 Subject: [PATCH 3/3] Add alias target with same Prefix as namespace With the alias, the `Docopt::docopt` target can be linked to regardless of whether the consuming project uses `find_package` or `add_subdirectory`. --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index da29dde..552e043 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,6 +114,7 @@ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${ConfigPackageLocation}/docopt-confi install(EXPORT ${export_name} DESTINATION "${CMAKE_INSTALL_LIBDIR}/${ConfigPackageLocation}" NAMESPACE Docopt::) +add_library(Docopt::docopt ALIAS docopt) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docopt.pc.in ${CMAKE_CURRENT_BINARY_DIR}/docopt.pc @ONLY) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/docopt.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)