Skip to content

Commit 512e9d0

Browse files
authored
cmake: add USE_SYSTEM_* options (#153)
Similar to the work done in pytorch/pytorch#37137, this adds the following CMake options: - `USE_SYSTEM_LIBS` - `USE_SYSTEM_GOOGLEBENCHMARK` - `USE_SYSTEM_GOOGLETEST` This is particularly useful in the context of Nix, where we can build these libraries once and then re-use them elsewhere to avoid rebuilding vendors dependencies.
1 parent 5366f69 commit 512e9d0

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

CMakeLists.txt

+13-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ OPTION(CPUINFO_BUILD_UNIT_TESTS "Build cpuinfo unit tests" ON)
2323
OPTION(CPUINFO_BUILD_MOCK_TESTS "Build cpuinfo mock tests" ON)
2424
OPTION(CPUINFO_BUILD_BENCHMARKS "Build cpuinfo micro-benchmarks" ON)
2525
OPTION(CPUINFO_BUILD_PKG_CONFIG "Build pkg-config manifest" ON)
26+
OPTION(USE_SYSTEM_LIBS "Use system libraries instead of downloading and building them" OFF)
27+
OPTION(USE_SYSTEM_GOOGLEBENCHMARK "Use system Google Benchmark library instead of downloading and building it" ${USE_SYSTEM_LIBS})
28+
OPTION(USE_SYSTEM_GOOGLETEST "Use system Google Test library instead of downloading and building it" ${USE_SYSTEM_LIBS})
2629

2730
# ---[ CMake options
2831
INCLUDE(GNUInstallDirs)
@@ -117,8 +120,10 @@ SET(CONFU_DEPENDENCIES_SOURCE_DIR ${CMAKE_SOURCE_DIR}/deps
117120
SET(CONFU_DEPENDENCIES_BINARY_DIR ${CMAKE_BINARY_DIR}/deps
118121
CACHE PATH "Confu-style dependencies binary directory")
119122

120-
IF(CPUINFO_BUILD_MOCK_TESTS OR CPUINFO_BUILD_UNIT_TESTS)
121-
IF(CPUINFO_SUPPORTED_PLATFORM AND NOT DEFINED GOOGLETEST_SOURCE_DIR)
123+
IF(CPUINFO_SUPPORTED_PLATFORM AND (CPUINFO_BUILD_MOCK_TESTS OR CPUINFO_BUILD_UNIT_TESTS))
124+
IF(USE_SYSTEM_GOOGLETEST)
125+
FIND_PACKAGE(GTest REQUIRED)
126+
ELSEIF(NOT DEFINED GOOGLETEST_SOURCE_DIR)
122127
MESSAGE(STATUS "Downloading Google Test to ${CONFU_DEPENDENCIES_SOURCE_DIR}/googletest (define GOOGLETEST_SOURCE_DIR to avoid it)")
123128
CONFIGURE_FILE(cmake/DownloadGoogleTest.cmake "${CONFU_DEPENDENCIES_BINARY_DIR}/googletest-download/CMakeLists.txt")
124129
EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
@@ -129,8 +134,10 @@ IF(CPUINFO_BUILD_MOCK_TESTS OR CPUINFO_BUILD_UNIT_TESTS)
129134
ENDIF()
130135
ENDIF()
131136

132-
IF(CPUINFO_BUILD_BENCHMARKS)
133-
IF(CPUINFO_SUPPORTED_PLATFORM AND NOT DEFINED GOOGLEBENCHMARK_SOURCE_DIR)
137+
IF(CPUINFO_SUPPORTED_PLATFORM AND CPUINFO_BUILD_BENCHMARKS)
138+
IF(USE_SYSTEM_GOOGLEBENCHMARK)
139+
FIND_PACKAGE(benchmark REQUIRED)
140+
ELSEIF(NOT DEFINED GOOGLEBENCHMARK_SOURCE_DIR)
134141
MESSAGE(STATUS "Downloading Google Benchmark to ${CONFU_DEPENDENCIES_SOURCE_DIR}/googlebenchmark (define GOOGLEBENCHMARK_SOURCE_DIR to avoid it)")
135142
CONFIGURE_FILE(cmake/DownloadGoogleBenchmark.cmake "${CONFU_DEPENDENCIES_BINARY_DIR}/googlebenchmark-download/CMakeLists.txt")
136143
EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
@@ -308,7 +315,7 @@ INSTALL(EXPORT cpuinfo-targets
308315
# ---[ cpuinfo micro-benchmarks
309316
IF(CPUINFO_SUPPORTED_PLATFORM AND CPUINFO_BUILD_BENCHMARKS)
310317
# ---[ Build google benchmark
311-
IF(NOT TARGET benchmark)
318+
IF(NOT TARGET benchmark AND NOT USE_SYSTEM_GOOGLEBENCHMARK)
312319
SET(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "")
313320
ADD_SUBDIRECTORY(
314321
"${GOOGLEBENCHMARK_SOURCE_DIR}"
@@ -327,7 +334,7 @@ ENDIF()
327334
IF(CPUINFO_SUPPORTED_PLATFORM)
328335
IF(CPUINFO_BUILD_MOCK_TESTS OR CPUINFO_BUILD_UNIT_TESTS)
329336
# ---[ Build google test
330-
IF(NOT TARGET gtest)
337+
IF(NOT TARGET gtest AND NOT USE_SYSTEM_GOOGLETEST)
331338
IF(MSVC AND NOT CPUINFO_RUNTIME_TYPE STREQUAL "static")
332339
SET(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
333340
ENDIF()

deps/clog/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ ELSE()
1414
OPTION(CLOG_LOG_TO_STDIO "Log errors, warnings, and information to stdout/stderr" ON)
1515
ENDIF()
1616
OPTION(CLOG_BUILD_TESTS "Build clog tests" ON)
17+
OPTION(USE_SYSTEM_LIBS "Use system libraries instead of downloading and building them" OFF)
18+
OPTION(USE_SYSTEM_GOOGLETEST "Use system Google Test library instead of downloading and building it" ${USE_SYSTEM_LIBS})
1719

1820
# ---[ CMake options
1921
IF(CLOG_BUILD_TESTS)
@@ -39,7 +41,9 @@ SET(CONFU_DEPENDENCIES_BINARY_DIR ${CMAKE_BINARY_DIR}/deps
3941
CACHE PATH "Confu-style dependencies binary directory")
4042

4143
IF(CLOG_BUILD_TESTS)
42-
IF(NOT DEFINED GOOGLETEST_SOURCE_DIR)
44+
IF(USE_SYSTEM_GOOGLETEST)
45+
FIND_PACKAGE(GTest REQUIRED)
46+
ELSEIF(NOT DEFINED GOOGLETEST_SOURCE_DIR)
4347
MESSAGE(STATUS "Downloading Google Test to ${CONFU_DEPENDENCIES_SOURCE_DIR}/googletest (define GOOGLETEST_SOURCE_DIR to avoid it)")
4448
CONFIGURE_FILE(cmake/DownloadGoogleTest.cmake "${CONFU_DEPENDENCIES_BINARY_DIR}/googletest-download/CMakeLists.txt")
4549
EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
@@ -78,7 +82,7 @@ INSTALL(TARGETS clog
7882
# ---[ clog tests
7983
IF(CLOG_BUILD_TESTS)
8084
# ---[ Build google test
81-
IF(NOT TARGET gtest)
85+
IF(NOT TARGET gtest AND NOT USE_SYSTEM_GOOGLETEST)
8286
IF(MSVC AND NOT CLOG_RUNTIME_TYPE STREQUAL "static")
8387
SET(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
8488
ENDIF()

0 commit comments

Comments
 (0)