Skip to content

Commit

Permalink
Better CMake dependency management.
Browse files Browse the repository at this point in the history
  • Loading branch information
niklas-uhl committed Oct 20, 2023
1 parent a68689a commit 2194970
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
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
48 changes: 48 additions & 0 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 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)
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 @@ -119,5 +166,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
2 changes: 1 addition & 1 deletion 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 Down

0 comments on commit 2194970

Please sign in to comment.