Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a CMake target upon install #123

Merged
merged 5 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 70 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@

cmake_minimum_required(VERSION 3.3)
cmake_policy(SET CMP0092 NEW)
cmake_minimum_required(VERSION 3.5)
cmake_policy(SET CMP0076 NEW) # remove warning for target_sources(flux...) for < 3.13
cmake_policy(SET CMP0092 NEW) # remove warning for CMAKE_LANG_FLAG MSVC for < 3.15

project(libflux CXX)

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)


set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

add_library(flux INTERFACE)
target_sources(flux INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/include/flux.hpp
)
file(GLOB_RECURSE FLUX_HPPS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp" )
target_sources(flux INTERFACE $<BUILD_INTERFACE:${FLUX_HPPS}>)

if (MSVC)
target_compile_features(flux INTERFACE cxx_std_23)
Expand All @@ -18,7 +22,12 @@ else()
target_compile_features(flux INTERFACE cxx_std_20)
endif()

target_include_directories(flux INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(
flux
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

set(CMAKE_CXX_EXTENSIONS Off)

Expand Down Expand Up @@ -55,4 +64,58 @@ endif()

if (${FLUX_BUILD_MODULE})
add_subdirectory(module)
endif()
endif()

set(PORT_NAME flux)
set(CONFIG_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PORT_NAME}")

# header-only doesn't need architeture differences so clear CMAKE_SIZEOF_VOIDP
# temporarily when creating the version file.
set(ORIGINAL_CMAKE_SIZEOF_VOIDP ${CMAKE_SIZEOF_VOIDP})
set(CMAKE_SIZEOF_VOIDP "")
write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/${PORT_NAME}-version.cmake"
VERSION -1 # When there is a PROJECT_VERSION, remove this line
COMPATIBILITY SameMajorVersion
# ARCH_INDEPENDENT # showed up in CMake 3.14 and gets rid of the need to do the CMAKE_SIZEOF_VOIDP thing
)
set(CMAKE_SIZEOF_VOIDP ${ORIGINAL_CMAKE_SIZEOF_VOIDP})

configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/${PORT_NAME}-config.cmake.in"
"${PROJECT_BINARY_DIR}/${PORT_NAME}-config.cmake"
INSTALL_DESTINATION "${CONFIG_DESTINATION}"
NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO
)

# set target installation location properties and associates it with the targets files
install(
TARGETS ${PORT_NAME}
EXPORT ${PORT_NAME}-targets
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
)

#install the targets files
install(
EXPORT ${PORT_NAME}-targets
NAMESPACE ${PORT_NAME}::
DESTINATION "${CONFIG_DESTINATION}"
)

# install the config and version files
install(
FILES
"${PROJECT_BINARY_DIR}/${PORT_NAME}-config.cmake"
"${PROJECT_BINARY_DIR}/${PORT_NAME}-version.cmake"
DESTINATION "${CONFIG_DESTINATION}"
)

# install the headers
install(
DIRECTORY "${PROJECT_SOURCE_DIR}/include"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/.."
PATTERN "build2file" EXCLUDE
)
3 changes: 3 additions & 0 deletions cmake/flux-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@PACKAGE_INIT@
include("${CMAKE_CURRENT_LIST_DIR}/flux-targets.cmake")
list(APPEND CMAKE_MODULE_PATH "@PACKAGE_cmakeModulesDir@")