Skip to content

Commit

Permalink
Apply more Beman Standard tweaks for cmake files
Browse files Browse the repository at this point in the history
  • Loading branch information
neatudarius committed Nov 10, 2024
1 parent 4b87aac commit 4888aa1
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 113 deletions.
102 changes: 58 additions & 44 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,72 +1,86 @@
# cmake-format: off
# CMakeLists.txt -*-CMake-*-
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# cmake-format: on

cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.27)

project(beman_iterator_interface VERSION 0.0.0 LANGUAGES CXX)
project(
beman_iterator_interface
VERSION 0.0.0
LANGUAGES CXX)

# Local helpers: required to include CompilerFeatureTest.
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

# Includes
include(CTest)
include(FetchContent)
include(CompilerFeatureTest)

# Prechecks.
beman_iterator_check_deducing_this(COMPILER_SUPPORTS_DEDUCING_THIS)

set(TARGETS_EXPORT_NAME ${CMAKE_PROJECT_NAME}Targets)

option(BEMAN_ITERATOR_INTERFACE_USE_DEDUCING_THIS
"Make use of deducing this. Turn this off for non-conforming compilers."
${COMPILER_SUPPORTS_DEDUCING_THIS})
"Make use of deducing this. Turn this off for non-conforming compilers."
${COMPILER_SUPPORTS_DEDUCING_THIS})

option(BEMAN_ITERATOR_INTERFACE_ENABLE_TESTING "Build beman.iterator_interface tests" ${PROJECT_IS_TOP_LEVEL})
option(ITERATOR_INTERFACE_ENABLE_TESTING
"Enable building tests and test infrastructure" ${PROJECT_IS_TOP_LEVEL})

if(BEMAN_ITERATOR_INTERFACE_USE_DEDUCING_THIS AND NOT COMPILER_SUPPORTS_DEDUCING_THIS)
message(WARNING "Building with deducing this support despite of the compiler's lack of support for it")
if(BEMAN_ITERATOR_INTERFACE_USE_DEDUCING_THIS
AND NOT COMPILER_SUPPORTS_DEDUCING_THIS)
message(
WARNING
"Building with deducing this support despite of the compiler's lack of support for it"
)
endif()

configure_file(
"${PROJECT_SOURCE_DIR}/include/beman/iterator_interface/config.hpp.in"
"${PROJECT_BINARY_DIR}/include/beman/iterator_interface/config.hpp"
@ONLY
)

if(BEMAN_ITERATOR_INTERFACE_ENABLE_TESTING)
enable_testing()
"${PROJECT_BINARY_DIR}/include/beman/iterator_interface/config.hpp" @ONLY)

# Build the tests if enabled via the option ITERATOR_INTERFACE_ENABLE_TESTING
if(ITERATOR_INTERFACE_ENABLE_TESTING)
# Fetch GoogleTest
FetchContent_Declare(
googletest
EXCLUDE_FROM_ALL
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG e39786088138f2749d64e9e90e0f9902daa77c40 # release-1.15.0
)
FetchContent_MakeAvailable(googletest)
endif()

set(TARGETS_EXPORT_NAME ${CMAKE_PROJECT_NAME}Targets)
# Create the library target and named header set for beman_iterator_interface
add_library(beman_iterator_interface STATIC)
target_sources(
beman_iterator_interface
PUBLIC FILE_SET
beman_iterator_interface_headers
TYPE
HEADERS
BASE_DIRS
src
include
"${PROJECT_BINARY_DIR}/include/")

add_subdirectory(extern)
add_subdirectory(src/beman/iterator_interface)
add_subdirectory(examples)

include(GNUInstallDirs)
add_subdirectory(include/beman/iterator_interface)

set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake)
add_subdirectory(examples)
if(ITERATOR_INTERFACE_ENABLE_TESTING)
add_subdirectory(tests)
endif()

install(
EXPORT ${TARGETS_EXPORT_NAME}
NAMESPACE ${CMAKE_PROJECT_NAME}
DESTINATION ${INSTALL_CONFIGDIR}
)
# Coverage
configure_file("cmake/gcovr.cfg.in" gcovr.cfg @ONLY)

include(CMakePackageConfigHelpers)

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)

configure_package_config_file(
"cmake/Config.cmake.in"
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}Config.cmake
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
)

install(FILES
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}ConfigVersion.cmake
DESTINATION ${INSTALL_CONFIGDIR}
)
add_custom_target(
process_coverage
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Running gcovr to process coverage results"
COMMAND mkdir -p coverage
COMMAND gcovr --config gcovr.cfg .)
32 changes: 12 additions & 20 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
# examples/CMakeLists.txt -*-CMake-*-
#
# cmake-format: off
# examples/CMakeLists.txt -*-makefile-*-
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

include(GNUInstallDirs)
# cmake-format: on

# List of all buildable examples.
set(EXAMPLES
sample
)
set(EXAMPLES sample)

foreach(EXAMPLE ${EXAMPLES})
foreach(example ${EXAMPLES})
# Add example executable.
add_executable(${EXAMPLE} "")
add_executable(${example} "")

# Add example source file.
target_sources(
${EXAMPLE}
PRIVATE
${EXAMPLE}.cpp
)
target_sources(${example} PRIVATE ${example}.cpp)

# Link example with the library.
target_link_libraries(${EXAMPLE} beman.iterator_interface)
target_link_libraries(${example} beman_iterator_interface)

# Install .
# Install.
install(
TARGETS ${EXAMPLE}
EXPORT ${TARGETS_EXPORT_NAME}
DESTINATION ${CMAKE_INSTALL_BINDIR}
)
TARGETS ${example}
COMPONENT beman_iterator_interface_examples
DESTINATION ${CMAKE_INSTALL_BINDIR})
endforeach()
17 changes: 17 additions & 0 deletions include/beman/iterator_interface/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# cmake-format: off
# include/beman/iterator_interface/CMakeLists.txt -*-cmake-*-
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# cmake-format: on

target_sources(
beman_iterator_interface
PUBLIC FILE_SET
beman_iterator_interface_headers
TYPE
HEADERS
FILES
iterator_interface.hpp
iterator_interface_access.hpp
detail/stl_interfaces/config.hpp
detail/stl_interfaces/fwd.hpp
detail/stl_interfaces/iterator_interface.hpp)
62 changes: 13 additions & 49 deletions src/beman/iterator_interface/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
# src/beman/iterator_interface/CMakeLists.txt -*-CMake-*-
#
# cmake-format: off
# src/beman/iterator_interface/CMakeLists.txt -*-cmake-*-
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# cmake-format: on

add_library(beman.iterator_interface)
add_library(beman::iterator_interface ALIAS beman.iterator_interface)

target_sources(
beman.iterator_interface
PRIVATE
iterator_interface.cpp
)

include(GNUInstallDirs)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

target_include_directories(beman.iterator_interface PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${CMAKE_LOWER_PROJECT_NAME}> # <prefix>/include/scratch
)
# Ensure that iterator_interface gets compiled at least once.
target_sources(beman_iterator_interface PUBLIC iterator_interface.cpp)

# The library is empty -- exclude it
install(
TARGETS beman.iterator_interface
EXPORT ${TARGETS_EXPORT_NAME}1
DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

string(TOLOWER ${CMAKE_PROJECT_NAME} CMAKE_LOWER_PROJECT_NAME)
TARGETS beman_iterator_interface
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT beman_iterator_interface_library
EXCLUDE_FROM_ALL)

install(
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${CMAKE_LOWER_PROJECT_NAME}
FILES_MATCHING PATTERN "*.hpp"
)

target_link_libraries(beman.iterator_interface)

## Tests
if(BEMAN_ITERATOR_INTERFACE_ENABLE_TESTING)
add_executable(iterator_interface_test "")

target_sources(
iterator_interface_test
PRIVATE
iterator_interface.t.cpp
)

target_link_libraries(iterator_interface_test beman.iterator_interface)
target_link_libraries(iterator_interface_test GTest::gtest)
target_link_libraries(iterator_interface_test GTest::gtest_main)

include(GoogleTest)
gtest_discover_tests(iterator_interface_test)
endif()
TARGETS beman_iterator_interface FILE_SET beman_iterator_interface_headers
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT beman_iterator_interface_development)
27 changes: 27 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# cmake-format: off
# src/beman/iterator_interface/tests/CMakeLists.txt -*-makefile-*-
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# cmake-format: on

include(GoogleTest)

# Tests
add_executable(beman_iterator_interface_test)

target_sources(beman_iterator_interface_test
PRIVATE iterator_interface.test.cpp)

target_sources(
beman_iterator_interface_test
PRIVATE FILE_SET beman_iterator_interface_test_headers TYPE HEADERS)

target_link_libraries(
beman_iterator_interface_test PRIVATE beman_iterator_interface GTest::gtest
GTest::gtest_main)

# Issue #32: Re-enable ASAN run CI/clang-19.
#
# Note: clang-19 + gtest_discover_tests + Asan setup causes errors on some
# platforms. Temporary switch to gtest_add_tests and skip some Asan checks.
# Change also applied for CI flows.
gtest_add_tests(TARGET beman_iterator_interface_test "" AUTO)

0 comments on commit 4888aa1

Please sign in to comment.