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

Feature/cmake dependencies #48

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "extern/googletest"]
path = extern/googletest
url = https://github.com/google/googletest.git
[submodule "extern/doxygen-awesome-css"]
path = extern/doxygen-awesome-css
url = https://github.com/jothepro/doxygen-awesome-css.git
107 changes: 74 additions & 33 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,53 @@ project(
LANGUAGES CXX
)

# dependencies
include(FetchContent)
if (POLICY CMP0135)
cmake_policy(SET CMP0135 OLD)
endif ()
set(fetch_content_deps googletest)

set(fetch_content_supported_flags "")
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
list(APPEND fetch_content_supported_flags OVERRIDE_FIND_PACKAGE)
endif ()
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.25)
list(APPEND fetch_content_supported_flags SYSTEM)
endif ()
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.28)
list(APPEND fetch_content_supported_flags EXCLUDE_FROM_ALL)
endif ()

# Using CMake >= 3.24 we override find_package and it "just works". For older versions we simulate the same behavior by
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we automatically test this for different CMake versions in our GitHub action, i.e., add different CMake versions to our build matrix?

# first making the dependency available and then writing an empty `googletest-config.cmake` such that find_package does
# not fail.
#
# This does not allow for easy overriding of dependencies in top-level projects because then the toplevel also has to
# override the config file, but this is the price to pay for using old CMake versions.
#
# When using CMake >= 3.24 a library user can override the depencies by providing an earlier FetchContent_Declare
# declaration with the `OVERRIDE_FIND_PACKAGE` flag enabled. Only the first call to FetchContent_Declare for each
# dependency will be registered.

FetchContent_Declare(
googletest URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
${fetch_content_supported_flags}
)

function (ensure_googletest)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should move utility functionality like this in a different file to keep the main CMakeLists.txt short.

if (CMAKE_VERSION VERSION_LESS 3.24)
if (NOT googletest_POPULATED)
FetchContent_MakeAvailable(googletest)
set(googletest_DIR
${CMAKE_BINARY_DIR}/my_pkgRedirects/
PARENT_SCOPE
)
file(WRITE ${CMAKE_BINARY_DIR}/my_pkgRedirects/googletest-config.cmake "")
endif ()
endif ()
endfunction ()

if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# folder support for IDEs
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
Expand Down Expand Up @@ -53,51 +100,44 @@ target_include_directories(kassert_base INTERFACE include)

# set C++ standard to C++17
target_compile_features(kassert_base INTERFACE cxx_std_17)

list(
APPEND
KASSERT_WARNING_FLAGS
"-Wall"
"-Wextra"
"-Wconversion"
"-Wnon-virtual-dtor"
"-Woverloaded-virtual"
"-Wshadow"
"-Wsign-conversion"
"-Wundef"
"-Wunreachable-code"
"-Wunused"
add_library(kassert_warnings INTERFACE)
target_compile_options(
kassert_warnings
INTERFACE "-Wall"
"-Wextra"
"-Wconversion"
"-Wnon-virtual-dtor"
"-Woverloaded-virtual"
"-Wshadow"
"-Wsign-conversion"
"-Wundef"
"-Wunreachable-code"
"-Wunused"
)

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
list(
APPEND
KASSERT_WARNING_FLAGS
"-Wcast-align"
"-Wnull-dereference"
"-Wpedantic"
"-Wextra-semi"
"-Wno-gnu-zero-variadic-macro-arguments"
target_compile_options(
kassert_warnings INTERFACE "-Wcast-align" "-Wnull-dereference" "-Wpedantic" "-Wextra-semi"
"-Wno-gnu-zero-variadic-macro-arguments"
)
endif ()

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
list(
APPEND
KASSERT_WARNING_FLAGS
"-Wcast-align"
"-Wnull-dereference"
"-Wpedantic"
"-Wnoexcept"
"-Wsuggest-attribute=const"
"-Wsuggest-attribute=noreturn"
"-Wsuggest-override"
target_compile_options(
kassert_warnings
INTERFACE "-Wcast-align"
"-Wnull-dereference"
"-Wpedantic"
"-Wnoexcept"
"-Wsuggest-attribute=const"
"-Wsuggest-attribute=noreturn"
"-Wsuggest-override"
)
endif ()

# OFF by default.
if (KASSERT_WARNINGS_ARE_ERRORS)
list(APPEND KASSERT_WARNING_FLAGS "-Werror")
target_compile_options(kassert_warnings INTERFACE "-Werror")
endif ()

# Actual library target with compile definitions
Expand Down Expand Up @@ -125,5 +165,6 @@ add_library(kassert::kassert ALIAS kassert)

# Testing and examples are only built if this is the main project or if KASSERT_BUILD_TESTS is set (OFF by default)
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR KASSERT_BUILD_TESTS)
ensure_googletest()
add_subdirectory(tests)
endif ()
1 change: 0 additions & 1 deletion extern/googletest
Submodule googletest deleted from af29db
4 changes: 2 additions & 2 deletions tests/cmake/KassertTestHelper.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_subdirectory("${PROJECT_SOURCE_DIR}/extern/googletest" "extern/googletest")
find_package(googletest REQUIRED)

include(GoogleTest)

Expand All @@ -10,7 +10,7 @@ function (kassert_register_test KASSERT_TARGET_NAME)
cmake_parse_arguments("KASSERT" "EXCEPTION_MODE" "" "FILES" ${ARGN})
add_executable(${KASSERT_TARGET_NAME} ${KASSERT_FILES})
target_link_libraries(${KASSERT_TARGET_NAME} PRIVATE gtest gtest_main gmock kassert_base)
target_compile_options(${KASSERT_TARGET_NAME} PRIVATE ${KASSERT_WARNING_FLAGS})
target_link_libraries(${KASSERT_TARGET_NAME} PRIVATE kassert_warnings)
gtest_discover_tests(${KASSERT_TARGET_NAME} WORKING_DIRECTORY ${PROJECT_DIR})

if (KASSERT_EXCEPTION_MODE)
Expand Down
Loading