Skip to content

Commit d1e76b3

Browse files
committed
cmake: support disabling CPM
When using ada as a library and embedding it in our project via FetchContent, we've run into errors like this: ``` [253/1413] Generating ada.cpp, ada.h, ada_c.h, demo.cpp, demo.c, README.md fatal: detected dubious ownership in repository at '/src/redpanda' ``` In our CI environment we've seen flaky HTTP issues as well: ``` CMake Error at build/_deps/ada-src/cmake/CPM.cmake:19 (file): file DOWNLOAD cannot compute hash on failed download status: [22;"HTTP response code said error"] Call Stack (most recent call first): build/_deps/ada-src/CMakeLists.txt:27 (include) CMake Error at build/_deps/ada-src/CMakeLists.txt:32 (CPMAddPackage): Unknown CMake command "CPMAddPackage". ``` Actually building just the library seems very simple and uses vanilla cmake. CPM is only used for testing/benchmarking/etc. We've seen some of the dubious ownership issues just including CPM (hacking around trying to disable GIT failed to fix the issue). To support our usecase, I've added a flag allowing disabling tests, the default value is the value of BUILD_TESTING so hopefully nothing changes for anyone. I tested this via the following commands: ``` cmake -B build && cmake --build build cmake -B build -DADA_BENCHMARKS=ON && cmake --build build cmake -B build -DADA_TESTING=OFF -DADA_TOOLS=OFF -DADA_BENCHMARKS=OFF && cmake --build build ``` Signed-off-by: Tyler Rockwood <[email protected]>
1 parent 1643c72 commit d1e76b3

File tree

2 files changed

+65
-57
lines changed

2 files changed

+65
-57
lines changed

CMakeLists.txt

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,65 +23,73 @@ add_subdirectory(src)
2323
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmake)
2424

2525
option(ADA_BENCHMARKS "Build benchmarks" OFF)
26-
27-
include(cmake/CPM.cmake)
28-
# CPM requires git as an implicit dependency
29-
find_package(Git QUIET)
30-
# We use googletest in the tests
31-
if(Git_FOUND AND BUILD_TESTING)
32-
CPMAddPackage(
33-
NAME GTest
34-
GITHUB_REPOSITORY google/googletest
35-
VERSION 1.14.0
36-
OPTIONS "BUILD_GMOCK OFF" "INSTALL_GTEST OFF"
37-
)
38-
endif()
39-
# We use simdjson in both the benchmarks and tests
40-
if(Git_FOUND AND (BUILD_TESTING OR ADA_BENCHMARKS))
41-
CPMAddPackage("gh:simdjson/[email protected]")
42-
endif()
43-
# We use Google Benchmark, but it does not build under several 32-bit systems.
44-
if(Git_FOUND AND ADA_BENCHMARKS AND (CMAKE_SIZEOF_VOID_P EQUAL 8))
45-
CPMAddPackage(
46-
NAME benchmark
47-
GITHUB_REPOSITORY google/benchmark
48-
GIT_TAG f91b6b4
49-
OPTIONS "BENCHMARK_ENABLE_TESTING OFF"
50-
"BENCHMARK_ENABLE_INSTALL OFF"
51-
"BENCHMARK_ENABLE_WERROR OFF"
52-
53-
)
54-
endif()
55-
56-
if (BUILD_TESTING AND NOT EMSCRIPTEN)
57-
if(Git_FOUND)
58-
message(STATUS "The tests are enabled.")
59-
add_subdirectory(tests)
60-
else()
61-
message(STATUS "The tests are disabled because git was not found.")
26+
option(ADA_TESTING "Build tests" ${BUILD_TESTING})
27+
28+
# There are cases where when embedding ada as a dependency for other CMake
29+
# projects as submodules or subdirectories (via FetchContent) can lead to
30+
# errors due to CPM, so this is here to support disabling all the testing
31+
# and tooling for ada if one only wishes to use the ada library.
32+
if(ADA_TESTING OR ADA_BENCHMARKS OR ADA_TOOLS)
33+
include(cmake/CPM.cmake)
34+
# CPM requires git as an implicit dependency
35+
find_package(Git QUIET)
36+
# We use googletest in the tests
37+
if(Git_FOUND AND ADA_TESTING)
38+
CPMAddPackage(
39+
NAME GTest
40+
GITHUB_REPOSITORY google/googletest
41+
VERSION 1.14.0
42+
OPTIONS "BUILD_GMOCK OFF" "INSTALL_GTEST OFF"
43+
)
6244
endif()
63-
else()
64-
if(is_top_project)
65-
message(STATUS "The tests are disabled.")
45+
# We use simdjson in both the benchmarks and tests
46+
if(Git_FOUND AND (ADA_TESTING OR ADA_BENCHMARKS))
47+
CPMAddPackage("gh:simdjson/[email protected]")
6648
endif()
67-
endif(BUILD_TESTING AND NOT EMSCRIPTEN)
68-
69-
If(ADA_BENCHMARKS AND NOT EMSCRIPTEN)
70-
if(Git_FOUND)
71-
message(STATUS "Ada benchmarks enabled.")
72-
add_subdirectory(benchmarks)
73-
else()
74-
message(STATUS "The benchmarks are disabled because git was not found.")
49+
# We use Google Benchmark, but it does not build under several 32-bit systems.
50+
if(Git_FOUND AND ADA_BENCHMARKS AND (CMAKE_SIZEOF_VOID_P EQUAL 8))
51+
CPMAddPackage(
52+
NAME benchmark
53+
GITHUB_REPOSITORY google/benchmark
54+
GIT_TAG f91b6b4
55+
OPTIONS "BENCHMARK_ENABLE_TESTING OFF"
56+
"BENCHMARK_ENABLE_INSTALL OFF"
57+
"BENCHMARK_ENABLE_WERROR OFF"
58+
59+
)
7560
endif()
76-
else(ADA_BENCHMARKS AND NOT EMSCRIPTEN)
77-
if(is_top_project)
78-
message(STATUS "Ada benchmarks disabled. Set ADA_BENCHMARKS=ON to enable them.")
79-
endif()
80-
endif(ADA_BENCHMARKS AND NOT EMSCRIPTEN)
61+
62+
if (ADA_TESTING AND NOT EMSCRIPTEN)
63+
if(Git_FOUND)
64+
message(STATUS "The tests are enabled.")
65+
add_subdirectory(tests)
66+
else()
67+
message(STATUS "The tests are disabled because git was not found.")
68+
endif()
69+
else()
70+
if(is_top_project)
71+
message(STATUS "The tests are disabled.")
72+
endif()
73+
endif(ADA_TESTING AND NOT EMSCRIPTEN)
74+
75+
If(ADA_BENCHMARKS AND NOT EMSCRIPTEN)
76+
if(Git_FOUND)
77+
message(STATUS "Ada benchmarks enabled.")
78+
add_subdirectory(benchmarks)
79+
else()
80+
message(STATUS "The benchmarks are disabled because git was not found.")
81+
endif()
82+
else(ADA_BENCHMARKS AND NOT EMSCRIPTEN)
83+
if(is_top_project)
84+
message(STATUS "Ada benchmarks disabled. Set ADA_BENCHMARKS=ON to enable them.")
85+
endif()
86+
endif(ADA_BENCHMARKS AND NOT EMSCRIPTEN)
87+
88+
if (ADA_TESTING AND EMSCRIPTEN)
89+
add_subdirectory(tests/wasm)
90+
endif(ADA_TESTING AND EMSCRIPTEN)
91+
endif()
8192

82-
if (BUILD_TESTING AND EMSCRIPTEN)
83-
add_subdirectory(tests/wasm)
84-
endif(BUILD_TESTING AND EMSCRIPTEN)
8593

8694
add_library(ada::ada ALIAS ada)
8795

singleheader/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ if (Python3_Interpreter_FOUND)
5151
target_link_libraries(ada-singleheader-source INTERFACE ada-singleheader-include-source)
5252
add_library(ada-singleheader-lib STATIC $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/ada.cpp>)
5353

54-
if (BUILD_TESTING)
54+
if (ADA_TESTING)
5555
add_executable(demo $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/demo.cpp>)
5656
target_link_libraries(demo ada-singleheader-include-source)
5757

@@ -63,4 +63,4 @@ if (Python3_Interpreter_FOUND)
6363
endif()
6464
else()
6565
MESSAGE( STATUS "Python not found, we are unable to test amalgamate.py." )
66-
endif()
66+
endif()

0 commit comments

Comments
 (0)