Skip to content
Merged
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
46 changes: 46 additions & 0 deletions cmake/CPMFindGTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Description:
#
# Find / build GTest. Prefers a system install if one is found,
# otherwise downloads sources with CPM (https://github.com/cpm-cmake/CPM.cmake)
# and builds GTest.
#
# Targets:
#
# GTest::gtest
# Gtest::mock
#
# Usage:
#
# include(CPMFindGTest.cmake)
#

function(_treelite_find_gtest gtest_version)

# Prefer a system-installed GTest if one is found
find_package(GTest ${gtest_version})
if(GTEST_FOUND)
return()
endif()

# not found, use CPM to download sources and build it
message(STATUS "GTest not found, fetching GTest via CPM.")

# populate local CPM cache with googletest source (if not already populated), build targets
include(${treelite_SOURCE_DIR}/cmake/CPMSetup.cmake)

# prefer statically linking GTest, so test executables are relocatable
CPMAddPackage(
NAME googletest
GITHUB_REPOSITORY google/googletest
GIT_TAG v${gtest_version}
VERSION ${gtest_version}
EXCLUDE_FROM_ALL YES
OPTIONS
"BUILD_GMOCK ON"
"BUILD_SHARED_LIBS OFF"
"CMAKE_POSITION_INDEPENDENT_CODE ON"
"INSTALL_GTEST OFF"
)
endfunction()

_treelite_find_gtest(1.18.0)
33 changes: 33 additions & 0 deletions cmake/CPMSetup.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Ensure CPM ("CMake Package Manager") is available.
#
# ref: https://github.com/cpm-cmake/cpm.cmake

function(_setup_cpm cpm_version)
# look for already-downloaded CPM source in this order:
#
# 1. already-defined CMake variable 'CPM_SOURCE_CACHE'
# 2. environment variable 'CPM_SOURCE_CACHE'
# 3. path relative to the top-level build directory
#
if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${cpm_version}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${cpm_version}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${cpm_version}.cmake")
endif()

# download source if necessary
if(NOT EXISTS ${CPM_DOWNLOAD_LOCATION})
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${cpm_version}/CPM.cmake
${CPM_DOWNLOAD_LOCATION}
)
endif()

set(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} PARENT_SCOPE)
endfunction()

_setup_cpm(0.43.1)
include(${CPM_DOWNLOAD_LOCATION})
28 changes: 2 additions & 26 deletions cmake/ExternalLibs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,8 @@ endif()

# Google C++ tests
if(BUILD_CPP_TEST)
find_package(GTest 1.14.0)
if(NOT GTest_FOUND)
message(STATUS "Did not find Google Test in the system root. Fetching Google Test now...")
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz
)
set(gtest_force_shared_crt ${DMLC_FORCE_SHARED_CRT} CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

add_library(GTest::gtest ALIAS gtest)
add_library(GTest::gmock ALIAS gmock)
target_compile_definitions(gtest PRIVATE ${ENABLE_GNU_EXTENSION_FLAGS})
target_compile_definitions(gmock PRIVATE ${ENABLE_GNU_EXTENSION_FLAGS})
foreach(target gtest gmock)
target_compile_features(${target} PUBLIC cxx_std_14)
if(MSVC)
set_target_properties(${target} PROPERTIES
MSVC_RUNTIME_LIBRARY "${Treelite_MSVC_RUNTIME_LIBRARY}")
endif()
endforeach()
if(IS_DIRECTORY "${googletest_SOURCE_DIR}")
# Do not install gtest
set_property(DIRECTORY ${googletest_SOURCE_DIR} PROPERTY EXCLUDE_FROM_ALL YES)
endif()
endif()
# find/build GTest
include(${treelite_SOURCE_DIR}/cmake/CPMFindGTest.cmake)
endif()

# fmtlib
Expand Down
Loading