-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
248 lines (217 loc) · 8.08 KB
/
Copy pathCMakeLists.txt
File metadata and controls
248 lines (217 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
cmake_minimum_required(VERSION 3.12)
project(mongory-core VERSION 1.0.0 LANGUAGES C)
# Set C standard
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
# Compilation options
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
# Output directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Include directories
include_directories(include)
## Note: cJSON is only needed for tests/benchmarks. Discovery and linkage
## are handled inside the test/benchmark sections below.
# Collect source files
file(GLOB_RECURSE CORE_SOURCES
"src/foundations/*.c"
"src/matchers/*.c"
)
# Exclude test_helper
list(FILTER CORE_SOURCES EXCLUDE REGEX "src/test_helper/.*")
# Create core static library
add_library(mongory-core STATIC ${CORE_SOURCES})
# Link math library for platforms that require -lm (e.g., Linux)
find_library(MATH_LIBRARY m)
if(MATH_LIBRARY)
target_link_libraries(mongory-core ${MATH_LIBRARY})
endif()
# Test options
option(BUILD_TESTS "Build the tests" ON)
if(BUILD_TESTS)
# Unity test framework setup (available to tests and benchmarks)
set(UNITY_DIR ${CMAKE_SOURCE_DIR}/tests/unity)
if(EXISTS ${UNITY_DIR}/unity.c)
add_library(unity STATIC ${UNITY_DIR}/unity.c)
target_include_directories(unity PUBLIC ${UNITY_DIR})
target_compile_definitions(unity PUBLIC UNITY_USE_COLOR UNITY_OUTPUT_COLOR)
else()
message(FATAL_ERROR "Unity framework not found. Run setup_unity.sh first, or disable tests with -DBUILD_TESTS=OFF.")
endif()
enable_testing()
# cJSON discovery (only for tests/benchmarks)
find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
pkg_check_modules(CJSON libcjson)
endif()
if(NOT CJSON_FOUND)
find_path(CJSON_INCLUDE_DIR
NAMES cjson/cJSON.h cJSON.h
PATHS
/usr/include
/usr/local/include
/opt/homebrew/include
PATH_SUFFIXES cjson
)
find_library(CJSON_LIBRARY
NAMES cjson
PATHS
/usr/lib
/usr/local/lib
/opt/homebrew/lib
)
if(CJSON_INCLUDE_DIR AND CJSON_LIBRARY)
set(CJSON_INCLUDE_DIRS ${CJSON_INCLUDE_DIR})
set(CJSON_LIBRARIES ${CJSON_LIBRARY})
set(CJSON_FOUND TRUE)
message(STATUS "Found cJSON manually: ${CJSON_LIBRARY}")
endif()
endif()
if(NOT CJSON_FOUND)
message(FATAL_ERROR "cJSON library not found. Please install libcjson-dev (Ubuntu/Debian) or cjson (Homebrew)")
endif()
# Normalize link items for cJSON (prefer LDFLAGS from pkg-config if present)
if(CJSON_LDFLAGS)
set(CJSON_LINK_ITEMS ${CJSON_LDFLAGS})
else()
set(CJSON_LINK_ITEMS ${CJSON_LIBRARIES})
endif()
# Copy test JSON files to the correct location in build directory
file(COPY tests/jsons DESTINATION ${CMAKE_BINARY_DIR}/tests)
# Collect test files
file(GLOB TEST_SOURCES "tests/mongory_*_test.c")
# Test helper library (for testing)
add_library(test_helper STATIC src/test_helper/test_helper.c)
target_include_directories(test_helper PUBLIC src/test_helper ${CJSON_INCLUDE_DIRS})
target_link_libraries(test_helper mongory-core ${CJSON_LINK_ITEMS})
foreach(test_source ${TEST_SOURCES})
get_filename_component(test_name ${test_source} NAME_WE)
add_executable(${test_name} ${test_source})
target_link_libraries(${test_name}
mongory-core
test_helper
unity
${CJSON_LINK_ITEMS}
)
if(MATH_LIBRARY)
target_link_libraries(${test_name} ${MATH_LIBRARY})
endif()
target_include_directories(${test_name} PRIVATE
tests
${UNITY_DIR}
${CJSON_INCLUDE_DIRS}
)
target_compile_definitions(${test_name} PRIVATE
UNITY_USE_COLOR
UNITY_OUTPUT_COLOR
)
# Add to CTest
add_test(NAME ${test_name} COMMAND ${test_name})
endforeach()
endif()
# Benchmark options
option(BUILD_BENCHMARKS "Build the benchmarks" ON)
if(BUILD_BENCHMARKS)
if(NOT TARGET unity)
message(STATUS "Unity framework not found; skipping benchmarks.")
else()
# cJSON discovery (ensure available when building benchmarks without tests)
if(NOT CJSON_FOUND)
find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
pkg_check_modules(CJSON libcjson)
endif()
if(NOT CJSON_FOUND)
find_path(CJSON_INCLUDE_DIR
NAMES cjson/cJSON.h cJSON.h
PATHS
/usr/include
/usr/local/include
/opt/homebrew/include
PATH_SUFFIXES cjson
)
find_library(CJSON_LIBRARY
NAMES cjson
PATHS
/usr/lib
/usr/local/lib
/opt/homebrew/lib
)
if(CJSON_INCLUDE_DIR AND CJSON_LIBRARY)
set(CJSON_INCLUDE_DIRS ${CJSON_INCLUDE_DIR})
set(CJSON_LIBRARIES ${CJSON_LIBRARY})
set(CJSON_FOUND TRUE)
message(STATUS "Found cJSON manually for benchmarks: ${CJSON_LIBRARY}")
endif()
endif()
endif()
if(NOT CJSON_FOUND)
message(FATAL_ERROR "cJSON library not found for benchmarks. Please install libcjson-dev (Ubuntu/Debian) or cjson (Homebrew)")
endif()
if(CJSON_LDFLAGS)
set(CJSON_LINK_ITEMS ${CJSON_LDFLAGS})
else()
set(CJSON_LINK_ITEMS ${CJSON_LIBRARIES})
endif()
# Collect benchmark files
file(GLOB BENCHMARK_SOURCES "benchmarks/*.c")
foreach(benchmark_source ${BENCHMARK_SOURCES})
get_filename_component(benchmark_name ${benchmark_source} NAME_WE)
set(benchmark_target "benchmark_${benchmark_name}")
add_executable(${benchmark_target} ${benchmark_source})
target_link_libraries(${benchmark_target}
mongory-core
test_helper
unity
${CJSON_LINK_ITEMS}
)
if(MATH_LIBRARY)
target_link_libraries(${benchmark_target} ${MATH_LIBRARY})
endif()
target_include_directories(${benchmark_target} PRIVATE
tests
${UNITY_DIR}
${CJSON_INCLUDE_DIRS}
)
target_compile_definitions(${benchmark_target} PRIVATE
UNITY_USE_COLOR
UNITY_OUTPUT_COLOR
)
endforeach()
endif()
endif()
# Custom target: Setup Unity
add_custom_target(setup-unity
COMMAND chmod +x ${CMAKE_SOURCE_DIR}/scripts/setup_unity.sh
COMMAND ${CMAKE_SOURCE_DIR}/scripts/setup_unity.sh
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Setting up Unity test framework"
)
# Custom target: Format code
add_custom_target(format
COMMAND find ${CMAKE_SOURCE_DIR} -name "*.c" -o -name "*.h" | xargs clang-format -i
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Formatting code with clang-format"
)
# Install rules
install(TARGETS mongory-core
ARCHIVE DESTINATION lib
)
install(DIRECTORY include/
DESTINATION include
FILES_MATCHING PATTERN "*.h"
)
# Display configuration information
message(STATUS "=== MongoDB Core Library Configuration ===")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C compiler: ${CMAKE_C_COMPILER}")
message(STATUS "C flags: ${CMAKE_C_FLAGS}")
message(STATUS "cJSON found: ${CJSON_FOUND}")
if(CJSON_FOUND)
message(STATUS "cJSON include dirs: ${CJSON_INCLUDE_DIRS}")
message(STATUS "cJSON libraries: ${CJSON_LIBRARIES}")
endif()
message(STATUS "Build tests: ${BUILD_TESTS}")
message(STATUS "Build benchmarks: ${BUILD_BENCHMARKS}")
message(STATUS "==========================================")