Skip to content

Commit

Permalink
Merge pull request #272 from timokoch/feature/cmake-usable-from-build
Browse files Browse the repository at this point in the history
Feature/cmake usable from build
  • Loading branch information
dglaeser authored Nov 4, 2024
2 parents fdc353a + a3a2fcd commit 66eb60f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 55 deletions.
121 changes: 67 additions & 54 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@

cmake_minimum_required(VERSION 3.18)

if(NOT DEFINED PROJECT_NAME)
set(GRIDFORMAT_SUBPROJECT OFF)
else()
set(GRIDFORMAT_SUBPROJECT ON)
endif()

project(gridformat VERSION 0.2.1)
set(GRIDFORMAT_NAMESPACE "gridformat")

Expand All @@ -26,58 +20,77 @@ add_library(${GRIDFORMAT_NAMESPACE}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules")
include(cmake/link_external_libs.cmake)

if (NOT GRIDFORMAT_SUBPROJECT)
option(GRIDFORMAT_BUILD_BINARIES "whether or not binaries should be built" OFF)
if (GRIDFORMAT_BUILD_BINARIES)
add_subdirectory(bin)
get_property(BIN_TARGETS DIRECTORY bin PROPERTY BUILDSYSTEM_TARGETS)
install(
TARGETS ${BIN_TARGETS}
DESTINATION ${CMAKE_INSTALL_BINDIR}
)
endif ()

set(GRIDFORMAT_INSTALL_CMAKE_DATA_DIR "${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake")
install(
DIRECTORY ${PROJECT_NAME}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(
DIRECTORY "${PROJECT_SOURCE_DIR}/cmake/modules"
DESTINATION ${GRIDFORMAT_INSTALL_CMAKE_DATA_DIR}
)
option(GRIDFORMAT_BUILD_BINARIES "whether or not binaries should be built" OFF)
if (GRIDFORMAT_BUILD_BINARIES)
add_subdirectory(bin)
get_property(BIN_TARGETS DIRECTORY bin PROPERTY BUILDSYSTEM_TARGETS)
install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}_Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(
EXPORT ${PROJECT_NAME}_Targets
FILE ${PROJECT_NAME}Targets.cmake
NAMESPACE ${GRIDFORMAT_NAMESPACE}::
DESTINATION ${GRIDFORMAT_INSTALL_CMAKE_DATA_DIR}
TARGETS ${BIN_TARGETS}
DESTINATION ${CMAKE_INSTALL_BINDIR}
)
endif ()

include(CMakePackageConfigHelpers)
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in"
set(GRIDFORMAT_INSTALL_CMAKE_DATA_DIR "${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake")
install(
DIRECTORY ${PROJECT_NAME}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(
DIRECTORY "${PROJECT_SOURCE_DIR}/cmake/modules"
DESTINATION ${GRIDFORMAT_INSTALL_CMAKE_DATA_DIR}
)
install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}_Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(
EXPORT ${PROJECT_NAME}_Targets
FILE ${PROJECT_NAME}Targets.cmake
NAMESPACE ${GRIDFORMAT_NAMESPACE}::
DESTINATION ${GRIDFORMAT_INSTALL_CMAKE_DATA_DIR}
)

include(CMakePackageConfigHelpers)

# make find_package(gridformat) work when installed
set(GRIDFORMAT_INCLUDE_DIRS ${CMAKE_INSTALL_INCLUDEDIR})
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in"
"${PROJECT_BINARY_DIR}/cmake/pkg/${PROJECT_NAME}Config.cmake"
PATH_VARS
GRIDFORMAT_INCLUDE_DIRS
GRIDFORMAT_NAMESPACE
GRIDFORMAT_INSTALL_CMAKE_DATA_DIR
INSTALL_DESTINATION
${GRIDFORMAT_INSTALL_CMAKE_DATA_DIR}
)
install(
FILES
"${PROJECT_BINARY_DIR}/cmake/pkg/${PROJECT_NAME}Config.cmake"
PATH_VARS
CMAKE_INSTALL_INCLUDEDIR
GRIDFORMAT_NAMESPACE
GRIDFORMAT_INSTALL_CMAKE_DATA_DIR
INSTALL_DESTINATION
${GRIDFORMAT_INSTALL_CMAKE_DATA_DIR}
)
install(
FILES
"${PROJECT_BINARY_DIR}/cmake/pkg/${PROJECT_NAME}Config.cmake"
DESTINATION
${GRIDFORMAT_INSTALL_CMAKE_DATA_DIR}
)
endif ()
DESTINATION
${GRIDFORMAT_INSTALL_CMAKE_DATA_DIR}
)

# make find_package(gridformat) work when used from build directory
export(
EXPORT ${PROJECT_NAME}_Targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}Targets.cmake"
NAMESPACE ${GRIDFORMAT_NAMESPACE}::
)
set(GRIDFORMAT_INCLUDE_DIRS ${PROJECT_SOURCE_DIR})
set(GRIDFORMAT_INSTALL_CMAKE_DATA_DIR "${PROJECT_SOURCE_DIR}/cmake")
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in"
"${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake"
PATH_VARS
GRIDFORMAT_INCLUDE_DIRS
GRIDFORMAT_NAMESPACE
GRIDFORMAT_INSTALL_CMAKE_DATA_DIR
INSTALL_DESTINATION ${PROJECT_BINARY_DIR}/cmake
)

option(GRIDFORMAT_BUILD_TESTS "whether or not the tests should be built" OFF)
if (GRIDFORMAT_BUILD_TESTS)
Expand Down
2 changes: 1 addition & 1 deletion cmake/gridformat-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@PACKAGE_INIT@

set(@PROJECT_NAME@_LIBRARY @PROJECT_NAME@::@PROJECT_NAME@)
set_and_check(@PROJECT_NAME@_INCLUDE_DIRS "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@")
set_and_check(@PROJECT_NAME@_INCLUDE_DIRS "@PACKAGE_GRIDFORMAT_INCLUDE_DIRS@")
set_and_check(@PROJECT_NAME@_MODULE_DIR "@PACKAGE_GRIDFORMAT_INSTALL_CMAKE_DATA_DIR@/modules")
list(APPEND CMAKE_MODULE_PATH "@PACKAGE_GRIDFORMAT_INSTALL_CMAKE_DATA_DIR@/modules")

Expand Down

0 comments on commit 66eb60f

Please sign in to comment.