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 051ab21
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 109 deletions.
89 changes: 49 additions & 40 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,80 @@
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.27)

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)

option(BEMAN_ITERATOR_INTERFACE_USE_DEDUCING_THIS
"Make use of deducing this. Turn this off for non-conforming compilers."
${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}
)

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")
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
"${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()
# 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)

install(
EXPORT ${TARGETS_EXPORT_NAME}
NAMESPACE ${CMAKE_PROJECT_NAME}
DESTINATION ${INSTALL_CONFIGDIR}
)

include(CMakePackageConfigHelpers)

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)
add_subdirectory(examples)
if(ITERATOR_INTERFACE_ENABLE_TESTING)
add_subdirectory(tests)
endif()

configure_package_config_file(
"cmake/Config.cmake.in"
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}Config.cmake
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
)
# Coverage
configure_file("cmake/gcovr.cfg.in" gcovr.cfg @ONLY)

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 .
)
42 changes: 20 additions & 22 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
# 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
sample
)

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

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

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

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

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
)
60 changes: 13 additions & 47 deletions src/beman/iterator_interface/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,55 +1,21 @@
# src/beman/iterator_interface/CMakeLists.txt -*-CMake-*-
#
# src/beman/iterator_interface/CMakeLists.txt -*-cmake-*-
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

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}
TARGETS beman_iterator_interface
ARCHIVE
DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT beman_iterator_interface_library
EXCLUDE_FROM_ALL
)

string(TOLOWER ${CMAKE_PROJECT_NAME} CMAKE_LOWER_PROJECT_NAME)

install(
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${CMAKE_LOWER_PROJECT_NAME}
FILES_MATCHING PATTERN "*.hpp"
TARGETS beman_iterator_interface
FILE_SET beman_iterator_interface_headers
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT beman_iterator_interface_development
)

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()
34 changes: 34 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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 051ab21

Please sign in to comment.