From 47ff6611ac199411d30f08dda9fd9f24b791a698 Mon Sep 17 00:00:00 2001 From: doodspav Date: Sat, 11 Mar 2023 05:57:31 +0000 Subject: [PATCH] GHI #11 Setup CMake for installing and packaging project --- CMakeLists.txt | 7 ++++ cmake/util/InstallConfig.cmake | 1 + cmake/util/InstallRules.cmake | 77 ++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 cmake/util/InstallConfig.cmake create mode 100644 cmake/util/InstallRules.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 9acfaa22d..16f1fdcbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,3 +71,10 @@ target_include_directories( ) target_compile_features(patomic_patomic PUBLIC c_std_90) + + +# ---- Install Rules ---- + +if(NOT CMAKE_SKIP_INSTALL_RULES) + include(cmake/util/InstallRules.cmake) +endif() diff --git a/cmake/util/InstallConfig.cmake b/cmake/util/InstallConfig.cmake new file mode 100644 index 000000000..30d8d5813 --- /dev/null +++ b/cmake/util/InstallConfig.cmake @@ -0,0 +1 @@ +include("${CMAKE_CURRENT_LIST_DIR}/patomicTargets.cmake") diff --git a/cmake/util/InstallRules.cmake b/cmake/util/InstallRules.cmake new file mode 100644 index 000000000..eae60635e --- /dev/null +++ b/cmake/util/InstallRules.cmake @@ -0,0 +1,77 @@ +if(PROJECT_IS_TOP_LEVEL) + set( + CMAKE_INSTALL_INCLUDEDIR "include/patomic-${PROJECT_VERSION}" + CACHE PATH "" + ) +endif() + +include(CMakePackageConfigHelpers) +include(GNUInstallDirs) + +# find_package() call for consumers to find this project +set(package patomic) + +# copy header files to CMAKE_INSTALL_INCLUDEDIR +install( + DIRECTORY + "${PROJECT_SOURCE_DIR}/include/" + "${PROJECT_BINARY_DIR}/include/" + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + COMPONENT patomic_Development +) + +# copy target build output artifacts to OS dependent locations +install( + TARGETS patomic_patomic + EXPORT patomicTargets + RUNTIME # + COMPONENT patomic_Runtime + LIBRARY # + COMPONENT patomic_Runtime + NAMELINK_COMPONENT patomic_Development + ARCHIVE # + COMPONENT patomic_Development + INCLUDES # + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" +) + +# allow package maintainers to freely override the path for the configs +set( + patomic_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${package}" + CACHE PATH "CMake package config location relative to the install prefix" +) +mark_as_advanced(patomic_INSTALL_CMAKE_DIR) + +# copy config file for find_package to find +install( + FILES "${PROJECT_SOURCE_DIR}/cmake/util/InstallConfig.cmake" + DESTINATION "${patomic_INSTALL_CMAKEDIR}" + RENAME "${package}Config.cmake" + COMPONENT patomic_Development +) + +# create version file for consumer to check version in CMake +write_basic_package_version_file( + "${package}ConfigVersion.cmake" + COMPATIBILITY SameMajorVersion +) + +# copy version file for find_package to find +install( + FILES "${PROJECT_BINARY_DIR}/${package}ConfigVersion.cmake" + DESTINATION "${patomic_INSTALL_CMAKEDIR}" + COMPONENT patomic_Development +) + +# create core configuration file detailing targets for consumer +install( + EXPORT patomicTargets + NAMESPACE patomic:: + DESTINATION "${patomic_INSTALL_CMAKEDIR}" + COMPONENT patomic_Development +) + +# support packaging library +if(PROJECT_IS_TOP_LEVEL) + include(CPack) +endif()