Skip to content

Commit

Permalink
More CMake improvements
Browse files Browse the repository at this point in the history
This is an attempt to solve the CMake errors reported in #130. It uses the new target_sources(FILE_SET) command to set the sources for the `flux` target, which will hopefully avoid the problems we've been seeing. It also adds a `flux::flux` alias target, which apparently is a good thing, although I don't know enough CMake to understand why.

The FILE_SET usage means setting our cmake_minimum_required to 3.23, which is pretty new (March 2022) -- but still older than our oldest supported compiler (GCC 11.3, released April '22). I think the odds of someone using a cutting-edge compiler but an old cmake are pretty slim, so hopefully it won't be a problem.
  • Loading branch information
tcbrindle committed Jan 8, 2024
1 parent 9172161 commit 50bf435
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 43 deletions.
65 changes: 23 additions & 42 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

cmake_minimum_required(VERSION 3.12)
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
cmake_minimum_required(VERSION 3.23)

project(flux CXX)

Expand All @@ -10,9 +8,16 @@ include(CMakePackageConfigHelpers)

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

set(PORT_NAME flux)

add_library(flux INTERFACE)
add_library(${PORT_NAME}::flux ALIAS flux)

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

if (MSVC)
target_compile_features(flux INTERFACE cxx_std_23)
Expand All @@ -22,13 +27,6 @@ else()
endif()
set_target_properties(flux PROPERTIES cxx_standard_required On)

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

add_library(flux-internal INTERFACE)
target_link_libraries(flux-internal INTERFACE flux)
set_target_properties(flux-internal PROPERTIES cxx_extensions Off)
Expand All @@ -53,90 +51,73 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
endif()

option(FLUX_BUILD_DOCS "Build Flux documentation (requires Sphinx)" Off)
option(FLUX_BUILD_EXAMPLES "Build Flux examples" On)
option(FLUX_BUILD_TESTS "Build Flux tests" On)
option(FLUX_BUILD_EXAMPLES "Build Flux examples" ${PROJECT_IS_TOP_LEVEL})
option(FLUX_BUILD_TESTS "Build Flux tests" ${PROJECT_IS_TOP_LEVEL})
option(FLUX_BUILD_BENCHMARKS "Build Flux benchmarks" Off)
option(FLUX_BUILD_TOOLS "Build single-header generator tool" Off)
option(FLUX_BUILD_MODULE "Build C++20 module (experimental)" Off)
option(FLUX_ENABLE_ASAN "Enable Address Sanitizer for tests" Off)
option(FLUX_ENABLE_UBSAN "Enable Undefined Behaviour Sanitizer for tests" Off)

if (${FLUX_BUILD_DOCS})
if (FLUX_BUILD_DOCS)
add_subdirectory(docs)
endif()

if (${FLUX_BUILD_EXAMPLES})
if (FLUX_BUILD_EXAMPLES)
enable_testing()
add_subdirectory(example)
endif()

if (${FLUX_BUILD_BENCHMARKS})
if (FLUX_BUILD_BENCHMARKS)
add_subdirectory(benchmark)
endif()

if (${FLUX_BUILD_TESTS})
if (FLUX_BUILD_TESTS)
enable_testing()
add_subdirectory(test)
endif()

if (${FLUX_BUILD_TOOLS})
if (FLUX_BUILD_TOOLS)
add_subdirectory(tools)
endif()

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

set(PORT_NAME flux)
set(CONFIG_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PORT_NAME}")
set(FLUX_INSTALL_CMAKE_DIR ${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
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
INSTALL_DESTINATION ${FLUX_INSTALL_CMAKE_DIR}
)

# 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}"
FILE_SET HEADERS
)

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

# 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
DESTINATION ${FLUX_INSTALL_CMAKE_DIR}
)
3 changes: 2 additions & 1 deletion cmake/flux-config.cmake.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@PACKAGE_INIT@
include("${CMAKE_CURRENT_LIST_DIR}/flux-targets.cmake")
list(APPEND CMAKE_MODULE_PATH "@PACKAGE_cmakeModulesDir@")

check_required_components(flux)

0 comments on commit 50bf435

Please sign in to comment.