Skip to content

Commit 33f42d7

Browse files
authored
CMake fixes + README update (#1276)
1 parent 9851006 commit 33f42d7

File tree

7 files changed

+75
-29
lines changed

7 files changed

+75
-29
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.20)
1+
cmake_minimum_required(VERSION 3.10)
22

33
# Must be done first
44
if (APPLE)

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,19 +297,19 @@ Other options supported by Valkey's `CMake` build system:
297297

298298
## Special build flags
299299

300-
- `-DBUILD_TLS=<on|off|module>` enable TLS build for Valkey
301-
- `-DBUILD_RDMA=<off|module>` enable RDMA module build (only module mode supported)
300+
- `-DBUILD_TLS=<yes|no>` enable TLS build for Valkey. Default: `no`
301+
- `-DBUILD_RDMA=<no|module>` enable RDMA module build (only module mode supported). Default: `no`
302302
- `-DBUILD_MALLOC=<libc|jemalloc|tcmalloc|tcmalloc_minimal>` choose the allocator to use. Default on Linux: `jemalloc`, for other OS: `libc`
303-
- `-DBUILD_SANITIZER=<address|thread|undefined>` build with address sanitizer enabled
304-
- `-DBUILD_UNIT_TESTS=[1|0]` when set, the build will produce the executable `valkey-unit-tests`
305-
- `-DBUILD_TEST_MODULES=[1|0]` when set, the build will include the modules located under the `tests/modules` folder
306-
- `-DBUILD_EXAMPLE_MODULES=[1|0]` when set, the build will include the example modules located under the `src/modules` folder
303+
- `-DBUILD_SANITIZER=<address|thread|undefined>` build with address sanitizer enabled. Default: disabled (no sanitizer)
304+
- `-DBUILD_UNIT_TESTS=[yes|no]` when set, the build will produce the executable `valkey-unit-tests`. Default: `no`
305+
- `-DBUILD_TEST_MODULES=[yes|no]` when set, the build will include the modules located under the `tests/modules` folder. Default: `no`
306+
- `-DBUILD_EXAMPLE_MODULES=[yes|no]` when set, the build will include the example modules located under the `src/modules` folder. Default: `no`
307307

308308
## Common flags
309309

310310
- `-DCMAKE_BUILD_TYPE=<Debug|Release...>` define the build type, see CMake manual for more details
311311
- `-DCMAKE_INSTALL_PREFIX=/installation/path` override this value to define a custom install prefix. Default: `/usr/local`
312-
- `-G<Generator Name>` generate build files for "Generator Name". By default, CMake will generate `Makefile`s.
312+
- `-G"<Generator Name>"` generate build files for "Generator Name". By default, CMake will generate `Makefile`s.
313313

314314
## Verbose build
315315

cmake/Modules/Utils.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,16 @@ function (valkey_parse_build_option OPTION_VALUE OUT_ARG_ENUM)
100100
PARENT_SCOPE)
101101
endif ()
102102
endfunction ()
103+
104+
function (valkey_pkg_config PKGNAME OUT_VARIABLE)
105+
if (NOT FOUND_PKGCONFIG)
106+
# Locate pkg-config once
107+
find_package(PkgConfig REQUIRED)
108+
set(FOUND_PKGCONFIG 1)
109+
endif ()
110+
pkg_check_modules(__PREFIX REQUIRED ${PKGNAME})
111+
message(STATUS "Found library for '${PKGNAME}': ${__PREFIX_LIBRARIES}")
112+
set(${OUT_VARIABLE}
113+
"${__PREFIX_LIBRARIES}"
114+
PARENT_SCOPE)
115+
endfunction ()

cmake/Modules/ValkeySetup.cmake

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ endmacro ()
7474
macro (valkey_build_and_install_bin target sources ld_flags libs link_name)
7575
add_executable(${target} ${sources})
7676

77-
if (USE_JEMALLOC)
78-
# Using jemalloc
79-
target_link_libraries(${target} jemalloc)
77+
if (USE_JEMALLOC
78+
OR USE_TCMALLOC
79+
OR USE_TCMALLOC_MINIMAL)
80+
# Using custom allocator
81+
target_link_libraries(${target} ${ALLOCATOR_LIB})
8082
endif ()
8183

8284
# Place this line last to ensure that ${ld_flags} is placed last on the linker line
@@ -151,16 +153,23 @@ endif ()
151153
if (BUILD_MALLOC)
152154
if ("${BUILD_MALLOC}" STREQUAL "jemalloc")
153155
set(MALLOC_LIB "jemalloc")
156+
set(ALLOCATOR_LIB "jemalloc")
154157
add_valkey_server_compiler_options("-DUSE_JEMALLOC")
155158
set(USE_JEMALLOC 1)
156159
elseif ("${BUILD_MALLOC}" STREQUAL "libc")
157160
set(MALLOC_LIB "libc")
158161
elseif ("${BUILD_MALLOC}" STREQUAL "tcmalloc")
159162
set(MALLOC_LIB "tcmalloc")
163+
valkey_pkg_config(libtcmalloc ALLOCATOR_LIB)
164+
160165
add_valkey_server_compiler_options("-DUSE_TCMALLOC")
166+
set(USE_TCMALLOC 1)
161167
elseif ("${BUILD_MALLOC}" STREQUAL "tcmalloc_minimal")
162168
set(MALLOC_LIB "tcmalloc_minimal")
169+
valkey_pkg_config(libtcmalloc_minimal ALLOCATOR_LIB)
170+
163171
add_valkey_server_compiler_options("-DUSE_TCMALLOC")
172+
set(USE_TCMALLOC_MINIMAL 1)
164173
else ()
165174
message(FATAL_ERROR "BUILD_MALLOC can be one of: jemalloc, libc, tcmalloc or tcmalloc_minimal")
166175
endif ()
@@ -202,16 +211,12 @@ if (BUILD_RDMA)
202211
if (USE_RDMA EQUAL 2) # Module
203212
message(STATUS "Building RDMA as module")
204213
add_valkey_server_compiler_options("-DUSE_RDMA=2")
205-
find_package(PkgConfig REQUIRED)
206214

207215
# Locate librdmacm & libibverbs, fail if we can't find them
208-
pkg_check_modules(RDMACM REQUIRED librdmacm)
209-
pkg_check_modules(IBVERBS REQUIRED libibverbs)
216+
valkey_pkg_config(librdmacm RDMACM_LIBS)
217+
valkey_pkg_config(libibverbs IBVERBS_LIBS)
210218

211-
message(STATUS "${RDMACM_LINK_LIBRARIES};${IBVERBS_LINK_LIBRARIES}")
212-
list(APPEND RDMA_LIBS "${RDMACM_LIBRARIES};${IBVERBS_LIBRARIES}")
213-
unset(RDMACM_LINK_LIBRARIES CACHE)
214-
unset(IBVERBS_LINK_LIBRARIES CACHE)
219+
list(APPEND RDMA_LIBS "${RDMACM_LIBS};${IBVERBS_LIBS}")
215220
set(BUILD_RDMA_MODULE 1)
216221
elseif (USE_RDMA EQUAL 1)
217222
# RDMA can only be built as a module. So disable it
@@ -266,17 +271,18 @@ endif ()
266271

267272
# Sanitizer
268273
if (BUILD_SANITIZER)
269-
# For best results, force libc
270-
set(MALLOC_LIB, "libc")
274+
# Common CFLAGS
275+
list(APPEND VALKEY_SANITAIZER_CFLAGS "-fno-sanitize-recover=all")
276+
list(APPEND VALKEY_SANITAIZER_CFLAGS "-fno-omit-frame-pointer")
271277
if ("${BUILD_SANITIZER}" STREQUAL "address")
272-
add_valkey_server_compiler_options("-fsanitize=address -fno-sanitize-recover=all -fno-omit-frame-pointer")
273-
add_valkey_server_linker_option("-fsanitize=address")
278+
list(APPEND VALKEY_SANITAIZER_CFLAGS "-fsanitize=address")
279+
list(APPEND VALKEY_SANITAIZER_LDFLAGS "-fsanitize=address")
274280
elseif ("${BUILD_SANITIZER}" STREQUAL "thread")
275-
add_valkey_server_compiler_options("-fsanitize=thread -fno-sanitize-recover=all -fno-omit-frame-pointer")
276-
add_valkey_server_linker_option("-fsanitize=thread")
281+
list(APPEND VALKEY_SANITAIZER_CFLAGS "-fsanitize=thread")
282+
list(APPEND VALKEY_SANITAIZER_LDFLAGS "-fsanitize=thread")
277283
elseif ("${BUILD_SANITIZER}" STREQUAL "undefined")
278-
add_valkey_server_compiler_options("-fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer")
279-
add_valkey_server_linker_option("-fsanitize=undefined")
284+
list(APPEND VALKEY_SANITAIZER_CFLAGS "-fsanitize=undefined")
285+
list(APPEND VALKEY_SANITAIZER_LDFLAGS "-fsanitize=undefined")
280286
else ()
281287
message(FATAL_ERROR "Unknown sanitizer: ${BUILD_SANITIZER}")
282288
endif ()
@@ -366,7 +372,6 @@ include(SourceFiles)
366372

367373
# Clear the below variables from the cache
368374
unset(CMAKE_C_FLAGS CACHE)
369-
unset(BUILD_SANITIZER CACHE)
370375
unset(VALKEY_SERVER_LDFLAGS CACHE)
371376
unset(VALKEY_SERVER_CFLAGS CACHE)
372377
unset(PYTHON_EXE CACHE)

deps/jemalloc/CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@ if (NOT EXISTS ${JEMALLOC_INSTALL_DIR}/lib/libjemalloc.a)
1212
COMMAND sh -c "${JEMALLOC_SRC_DIR}/configure --disable-cxx \
1313
--with-version=5.3.0-0-g0 --with-lg-quantum=3 --disable-cache-oblivious --with-jemalloc-prefix=je_ \
1414
--enable-static --disable-shared --prefix=${JEMALLOC_INSTALL_DIR}"
15-
WORKING_DIRECTORY ${JEMALLOC_SRC_DIR} COMMAND_ERROR_IS_FATAL ANY)
15+
WORKING_DIRECTORY ${JEMALLOC_SRC_DIR} RESULTS_VARIABLE CONFIGURE_RESULT)
16+
17+
if (NOT ${CONFIGURE_RESULT} EQUAL 0)
18+
message(FATAL_ERROR "Jemalloc configure failed")
19+
endif ()
20+
1621
execute_process(COMMAND make -j${VALKEY_PROCESSOR_COUNT} lib/libjemalloc.a install
17-
WORKING_DIRECTORY "${JEMALLOC_SRC_DIR}")
22+
WORKING_DIRECTORY "${JEMALLOC_SRC_DIR}" RESULTS_VARIABLE MAKE_RESULT)
23+
24+
if (NOT ${MAKE_RESULT} EQUAL 0)
25+
message(FATAL_ERROR "Jemalloc build failed")
26+
endif ()
1827
endif ()
1928

2029
# Import the compiled library as a CMake target

deps/lua/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
project(lualib)
22

3+
include(CheckFunctionExists)
4+
35
set(LUA_SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/src")
46
set(LUA_SRCS
57
${LUA_SRC_DIR}/fpconv.c
@@ -42,3 +44,10 @@ set(LUA_SRCS
4244
add_library(lualib STATIC "${LUA_SRCS}")
4345
target_include_directories(lualib PUBLIC "${LUA_SRC_DIR}")
4446
target_compile_definitions(lualib PRIVATE ENABLE_CJSON_GLOBAL)
47+
48+
# Use mkstemp if available
49+
check_function_exists(mkstemp HAVE_MKSTEMP)
50+
if (HAVE_MKSTEMP)
51+
target_compile_definitions(lualib PRIVATE LUA_USE_MKSTEMP)
52+
endif ()
53+
unset(HAVE_MKSTEMP CACHE)

src/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ if (VALKEY_RELEASE_BUILD)
2222
set_property(TARGET valkey-server PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
2323
endif ()
2424

25+
if (BUILD_SANITIZER)
26+
# 'BUILD_SANITIZER' is defined in ValkeySetup module (based on user input)
27+
# If defined, the variables 'VALKEY_SANITAIZER_CFLAGS' and 'VALKEY_SANITAIZER_LDFLAGS'
28+
# are set with the link & compile flags required
29+
message(STATUS "Adding sanitizer flags for target valkey-server")
30+
target_compile_options(valkey-server PRIVATE ${VALKEY_SANITAIZER_CFLAGS})
31+
target_link_options(valkey-server PRIVATE ${VALKEY_SANITAIZER_LDFLAGS})
32+
endif ()
33+
unset(BUILD_SANITIZER CACHE)
34+
2535
# Target: valkey-cli
2636
list(APPEND CLI_LIBS "linenoise")
2737
valkey_build_and_install_bin(valkey-cli "${VALKEY_CLI_SRCS}" "${VALKEY_SERVER_LDFLAGS}" "${CLI_LIBS}" "redis-cli")

0 commit comments

Comments
 (0)