From 83f52067a4b3a8a418c974d9135492d9a76e8bde Mon Sep 17 00:00:00 2001 From: Timothy Burke Date: Tue, 11 Jun 2024 11:27:20 -0600 Subject: [PATCH] Adds installation logic and updates cmake to use relative paths --- CMakeLists.txt | 76 +++++++++++++++++++++++++++++--- cmake/develop_dependencies.cmake | 55 ++++++++++++++--------- cmake/release_dependencies.cmake | 45 ++++++++++++------- cmake/tools-config.cmake.in | 12 +++++ cmake/unit_testing.cmake | 2 - 5 files changed, 144 insertions(+), 46 deletions(-) create mode 100644 cmake/tools-config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index e57ef77..cf96a5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ # Preamble ######################################################################## -cmake_minimum_required( VERSION 3.14 ) +cmake_minimum_required( VERSION 3.27 ) set( subproject OFF ) if( DEFINED PROJECT_NAME ) @@ -35,6 +35,7 @@ cmake_dependent_option( "Build tools python bindings" ON "NOT ${subproject} OR DEFINED require.tools.python " OFF ) +option( tools.installation "Install njoy::tools" ON ) ######################################################################## # Dependencies @@ -69,11 +70,14 @@ endif() add_library( tools INTERFACE ) add_library( njoy::tools ALIAS tools ) -target_include_directories( tools - INTERFACE - $ - $ + +string( CONCAT prefix + "$" + "$" ) + +target_include_directories( tools INTERFACE ${prefix} ) + target_link_libraries( tools INTERFACE spdlog::spdlog @@ -88,11 +92,10 @@ target_compile_definitions( tools INTERFACE -DNANORANGE_NO_STD_FORWARD_DECLARATI if( tools.python ) - FetchContent_MakeAvailable( pybind11 ) - pybind11_add_module( tools.python python/src/tools.python.cpp ) + add_library( njoy::tools.python ALIAS tools.python ) target_link_libraries( tools.python PRIVATE tools ) target_include_directories( tools.python PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/python/src ) @@ -116,3 +119,62 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR ) include( cmake/unit_testing.cmake ) endif() endif() + +####################################################################### +# Installation +####################################################################### + +if(tools.installation) + include(CMakePackageConfigHelpers) + + install(TARGETS tools EXPORT tools-targets + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + ) + + install(EXPORT tools-targets + FILE "tools-targets.cmake" + NAMESPACE njoy:: + DESTINATION share/cmake/tools + ) + + install(DIRECTORY src/ + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + FILES_MATCHING PATTERN "*.hpp" + PATTERN "*test*" EXCLUDE + ) + + write_basic_package_version_file("tools-config-version.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion) + + configure_package_config_file( + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/tools-config.cmake.in + ${PROJECT_BINARY_DIR}/tools-config.cmake + INSTALL_DESTINATION share/cmake/tools + ) + + install(FILES + "${PROJECT_BINARY_DIR}/tools-config.cmake" + "${PROJECT_BINARY_DIR}/tools-config-version.cmake" + DESTINATION share/cmake/tools + ) + + if(NOT subproject) + set(CPACK_PACKAGE_VENDOR "Los Alamos National Laboratory") + set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE") + include(CPack) + endif() + + if(tools.python) + + install(TARGETS tools.python EXPORT tools-targets + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + ) + + endif() + +endif() diff --git a/cmake/develop_dependencies.cmake b/cmake/develop_dependencies.cmake index a6fad29..97173f1 100644 --- a/cmake/develop_dependencies.cmake +++ b/cmake/develop_dependencies.cmake @@ -1,34 +1,29 @@ -cmake_minimum_required( VERSION 3.14 ) +cmake_minimum_required( VERSION 3.27 ) +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/.cmake) include( FetchContent ) ####################################################################### # Declare project dependencies ####################################################################### -FetchContent_Declare( spdlog - GIT_REPOSITORY https://github.com/gabime/spdlog - GIT_TAG v1.11.0 - GIT_SHALLOW TRUE - ) -set( SPDLOG_BUILD_PIC CACHE INTERNAL BOOL ON ) - -FetchContent_Declare( Catch2 - GIT_REPOSITORY https://github.com/catchorg/Catch2 - GIT_TAG v3.3.2 - GIT_SHALLOW TRUE - ) -FetchContent_Declare( pybind11 - GIT_REPOSITORY https://github.com/pybind/pybind11 - GIT_TAG v2.10.4 +FetchContent_Declare( FastFloat + GIT_REPOSITORY ../../fastfloat/fast_float + GIT_TAG v6.1.1 GIT_SHALLOW TRUE ) -FetchContent_Declare( fast_float - GIT_REPOSITORY https://github.com/fastfloat/fast_float - GIT_TAG v6.1.1 +FetchContent_Declare( spdlog + GIT_REPOSITORY ../../gabime/spdlog + GIT_TAG v1.11.0 GIT_SHALLOW TRUE ) +set( SPDLOG_BUILD_PIC CACHE INTERNAL BOOL ON ) +if (tools.installation) + set( SPDLOG_INSTALL CACHE INTERNAL BOOL ON ) +else() + set( SPDLOG_INSTALL CACHE INTERNAL BOOL OFF ) +endif() ####################################################################### # Load dependencies @@ -36,5 +31,23 @@ FetchContent_Declare( fast_float FetchContent_MakeAvailable( spdlog - fast_float - ) + FastFloat +) + +if (tools.tests) + FetchContent_Declare( Catch2 + GIT_REPOSITORY ../../catchorg/catch2 + GIT_TAG v3.3.2 + GIT_SHALLOW TRUE + ) + FetchContent_MakeAvailable(Catch2) +endif() + +if (tools.python) + FetchContent_Declare( pybind11 + GIT_REPOSITORY ../../pybind/pybind11 + GIT_TAG v2.10.4 + GIT_SHALLOW TRUE + ) + FetchContent_MakeAvailable(pybind11) +endif() diff --git a/cmake/release_dependencies.cmake b/cmake/release_dependencies.cmake index d9a3877..dbf1a84 100644 --- a/cmake/release_dependencies.cmake +++ b/cmake/release_dependencies.cmake @@ -1,36 +1,49 @@ -cmake_minimum_required( VERSION 3.14 ) +cmake_minimum_required( VERSION 3.27 ) +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/.cmake) include( FetchContent ) - ####################################################################### # Declare project dependencies ####################################################################### -FetchContent_Declare( Catch2 - GIT_REPOSITORY https://github.com/catchorg/Catch2 - GIT_TAG 3f0283de7a9c43200033da996ff9093be3ac84dc # tag: v3.3.2 - ) - -FetchContent_Declare( fast_float - GIT_REPOSITORY https://github.com/fastfloat/fast_float +FetchContent_Declare( FastFloat + GIT_REPOSITORY ../../fastfloat/fast_float GIT_TAG f476bc713fda06fbd34dc621b466745a574b3d4c # tag: v6.1.1 ) -FetchContent_Declare( pybind11 - GIT_REPOSITORY https://github.com/pybind/pybind11 - GIT_TAG 5b0a6fc2017fcc176545afe3e09c9f9885283242 # tag: v2.10.4 - ) - FetchContent_Declare( spdlog - GIT_REPOSITORY https://github.com/gabime/spdlog + GIT_REPOSITORY ../../gabime/spdlog GIT_TAG ad0e89cbfb4d0c1ce4d097e134eb7be67baebb36 # tag: v1.11.0 ) set( SPDLOG_BUILD_PIC CACHE INTERNAL BOOL ON ) +if (tools.installation) + set( SPDLOG_INSTALL CACHE INTERNAL BOOL ON ) +else() + set( SPDLOG_INSTALL CACHE INTERNAL BOOL OFF ) +endif() + ####################################################################### # Load dependencies ####################################################################### FetchContent_MakeAvailable( - fast_float + FastFloat spdlog ) + +if (tools.tests) + FetchContent_Declare( Catch2 + GIT_REPOSITORY ../../catchorg/Catch2 + GIT_TAG 3f0283de7a9c43200033da996ff9093be3ac84dc # tag: v3.3.2 + ) + + FetchContent_MakeAvailable(Catch2) +endif() + +if (tools.python) + FetchContent_Declare( pybind11 + GIT_REPOSITORY ../../pybind/pybind11 + GIT_TAG 5b0a6fc2017fcc176545afe3e09c9f9885283242 # tag: v2.10.4 + ) + FetchContent_MakeAvailable(pybind11) +endif() diff --git a/cmake/tools-config.cmake.in b/cmake/tools-config.cmake.in new file mode 100644 index 0000000..e3251f3 --- /dev/null +++ b/cmake/tools-config.cmake.in @@ -0,0 +1,12 @@ +include(CMakeFindDependencyMacro) + +if (NOT TARGET spdlog::spdlog) + find_dependency(spdlog) +endif() + + +if (NOT TARGET FastFloat::fast_float) + find_dependency(FastFloat) +endif() + +include("${CMAKE_CURRENT_LIST_DIR}/tools-targets.cmake") diff --git a/cmake/unit_testing.cmake b/cmake/unit_testing.cmake index 2a3ec9c..c1ab12e 100644 --- a/cmake/unit_testing.cmake +++ b/cmake/unit_testing.cmake @@ -5,8 +5,6 @@ message( STATUS "Adding tools unit testing" ) enable_testing() -FetchContent_MakeAvailable( Catch2 ) - function( add_cpp_test name source ) set( test_name "tools.${name}.test" )