diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e852e9af..f5679216 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,10 +10,14 @@ jobs: - name: Install dependencies run: | - curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg - echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list sudo apt-get update - sudo apt-get install -y redis-server valgrind libevent-dev + sudo apt-get install -y valgrind libevent-dev cmake libssl-dev + + # TODO: Test against Redis and KeyDB in addition to Valkey + - name: Install Valkey + run: | + git clone --depth 1 --branch 7.2.5 https://github.com/valkey-io/valkey.git + cd valkey && sudo BUILD_TLS=yes make install - name: Build using cmake env: @@ -26,10 +30,11 @@ jobs: run: USE_SSL=1 TEST_ASYNC=1 make - name: Run tests + working-directory: tests env: SKIPS_AS_FAILS: 1 TEST_SSL: 1 - run: $GITHUB_WORKSPACE/test.sh + run: ./test.sh # - name: Run tests under valgrind # env: @@ -48,7 +53,12 @@ jobs: run: | yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm yum -y --enablerepo=remi install redis - yum -y install gcc gcc-c++ make openssl openssl-devel cmake3 valgrind libevent-devel + yum -y install git gcc gcc-c++ make openssl openssl-devel cmake3 valgrind libevent-devel + + - name: Install Valkey + run: | + git clone --depth 1 --branch 7.2.5 https://github.com/valkey-io/valkey.git + cd valkey && BUILD_TLS=yes make install - name: Build using cmake env: @@ -61,17 +71,19 @@ jobs: run: USE_SSL=1 TEST_ASYNC=1 make - name: Run tests + working-directory: tests env: SKIPS_AS_FAILS: 1 TEST_SSL: 1 - run: $GITHUB_WORKSPACE/test.sh + run: ./test.sh - name: Run tests under valgrind + working-directory: tests env: SKIPS_AS_FAILS: 1 TEST_SSL: 1 TEST_PREFIX: valgrind --error-exitcode=99 --track-origins=yes --leak-check=full - run: $GITHUB_WORKSPACE/test.sh + run: ./test.sh centos8: name: RockyLinux 8 @@ -88,6 +100,11 @@ jobs: dnf -y group install "Development Tools" dnf -y install openssl-devel cmake valgrind libevent-devel + - name: Install Valkey + run: | + git clone --depth 1 --branch 7.2.5 https://github.com/valkey-io/valkey.git + cd valkey && BUILD_TLS=yes make install + - name: Build using cmake env: EXTRA_CMAKE_OPTS: -DENABLE_EXAMPLES:BOOL=ON -DENABLE_SSL:BOOL=ON -DENABLE_SSL_TESTS:BOOL=ON -DENABLE_ASYNC_TESTS:BOOL=ON @@ -99,17 +116,19 @@ jobs: run: USE_SSL=1 TEST_ASYNC=1 make - name: Run tests + working-directory: tests env: SKIPS_AS_FAILS: 1 TEST_SSL: 1 - run: $GITHUB_WORKSPACE/test.sh + run: ./test.sh - name: Run tests under valgrind + working-directory: tests env: SKIPS_AS_FAILS: 1 TEST_SSL: 1 TEST_PREFIX: valgrind --error-exitcode=99 --track-origins=yes --leak-check=full - run: $GITHUB_WORKSPACE/test.sh + run: ./test.sh freebsd: runs-on: ubuntu-latest @@ -133,16 +152,16 @@ jobs: - name: Install dependencies run: | - brew install openssl redis@7.2 - brew link redis@7.2 --force + brew install openssl valkey - name: Build hiredis run: USE_SSL=1 make - name: Run tests + working-directory: tests env: TEST_SSL: 1 - run: $GITHUB_WORKSPACE/test.sh + run: ./test.sh windows: name: Windows @@ -163,7 +182,7 @@ jobs: - name: Run tests run: | - ./build/hiredis-test.exe + ./build/test.exe - name: Install Cygwin Action uses: cygwin/cygwin-install-action@v2 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1a2c60b7..0e10a1f6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -84,13 +84,13 @@ jobs: - uses: actions/checkout@v3 - name: Run CMake (shared lib) run: cmake -Wno-dev CMakeLists.txt - - name: Build hiredis (shared lib) + - name: Build shared library run: MSBuild hiredis.vcxproj /p:Configuration=Debug - name: Run CMake (static lib) run: cmake -Wno-dev CMakeLists.txt -DBUILD_SHARED_LIBS=OFF - - name: Build hiredis (static lib) + - name: Build static library run: MSBuild hiredis.vcxproj /p:Configuration=Debug - - name: Build hiredis-test + - name: Build test.exe run: MSBuild hiredis-test.vcxproj /p:Configuration=Debug # use memurai, redis compatible server, since it is easy to install. Can't # install official redis containers on the windows runner diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..a621bb70 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +obj +lib +compile_commands.json +.cache diff --git a/libvalkey/CMakeLists.txt b/CMakeLists.txt similarity index 85% rename from libvalkey/CMakeLists.txt rename to CMakeLists.txt index 9cbe2b55..a2c81c5f 100644 --- a/libvalkey/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0) MACRO(getVersionBit name) SET(VERSION_REGEX "^#define ${name} (.+)$") - FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/valkey.h" + FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/valkey/valkey.h" VERSION_BIT REGEX ${VERSION_REGEX}) STRING(REGEX REPLACE ${VERSION_REGEX} "\\1" ${name} "${VERSION_BIT}") ENDMACRO(getVersionBit) @@ -32,13 +32,20 @@ SET(CMAKE_C_STANDARD 99) SET(CMAKE_DEBUG_POSTFIX d) SET(valkey_sources - alloc.c - async.c - valkey.c - net.c - read.c - sds.c - sockcompat.c) + src/adlist.c + src/alloc.c + src/async.c + src/command.c + src/crc16.c + src/dict.c + src/net.c + src/read.c + src/sds.c + src/sockcompat.c + src/valkey.c + src/valkeycluster.c + src/vkarray.c + src/vkutil.c) SET(valkey_sources ${valkey_sources}) @@ -66,7 +73,11 @@ ELSEIF(CMAKE_SYSTEM_NAME MATCHES "SunOS") TARGET_LINK_LIBRARIES(valkey PUBLIC socket) ENDIF() -TARGET_INCLUDE_DIRECTORIES(valkey PUBLIC $ $) +TARGET_INCLUDE_DIRECTORIES(valkey + PUBLIC + $ + $ +) CONFIGURE_FILE(valkey.pc.in valkey.pc @ONLY) @@ -158,10 +169,16 @@ IF(ENABLE_SSL) ENDIF() FIND_PACKAGE(OpenSSL REQUIRED) SET(valkey_ssl_sources - ssl.c) + src/ssl.c) ADD_LIBRARY(valkey_ssl ${valkey_ssl_sources}) ADD_LIBRARY(valkey::valkey_ssl ALIAS valkey_ssl) + TARGET_INCLUDE_DIRECTORIES(valkey_ssl + PRIVATE + $ + $ + ) + IF (APPLE AND BUILD_SHARED_LIBS) SET_PROPERTY(TARGET valkey_ssl PROPERTY LINK_FLAGS "-Wl,-undefined -Wl,dynamic_lookup") ENDIF() @@ -192,7 +209,7 @@ IF(ENABLE_SSL) CONFIGURATIONS Debug RelWithDebInfo) endif() - INSTALL(FILES valkey_ssl.h + INSTALL(FILES valkey_ssl.h valkeycluster_ssl.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/valkey) INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/valkey_ssl.pc @@ -220,21 +237,21 @@ IF(ENABLE_SSL) DESTINATION ${CMAKE_CONF_INSTALL_DIR}) ENDIF() -IF(NOT DISABLE_TESTS) - ENABLE_TESTING() - ADD_EXECUTABLE(libvalkey-test test.c) - TARGET_LINK_LIBRARIES(libvalkey-test valkey) - IF(ENABLE_SSL_TESTS) - ADD_DEFINITIONS(-DVALKEY_TEST_SSL=1) - TARGET_LINK_LIBRARIES(libvalkey-test valkey_ssl) - ENDIF() - IF(ENABLE_ASYNC_TESTS) - ADD_DEFINITIONS(-DVALKEY_TEST_ASYNC=1) - TARGET_LINK_LIBRARIES(libvalkey-test event) - ENDIF() - ADD_TEST(NAME libvalkey-test - COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test.sh) -ENDIF() +#IF(NOT DISABLE_TESTS) +# ENABLE_TESTING() +# ADD_EXECUTABLE(libvalkey-test test.c) +# TARGET_LINK_LIBRARIES(libvalkey-test valkey) +# IF(ENABLE_SSL_TESTS) +# ADD_DEFINITIONS(-DVALKEY_TEST_SSL=1) +# TARGET_LINK_LIBRARIES(libvalkey-test valkey_ssl) +# ENDIF() +# IF(ENABLE_ASYNC_TESTS) +# ADD_DEFINITIONS(-DVALKEY_TEST_ASYNC=1) +# TARGET_LINK_LIBRARIES(libvalkey-test event) +# ENDIF() +# ADD_TEST(NAME libvalkey-test +# COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test.sh) +#ENDIF() # Add examples IF(ENABLE_EXAMPLES) diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..53d9f9df --- /dev/null +++ b/Makefile @@ -0,0 +1,289 @@ +# Libvalkey Makefile +# Copyright (C) 2010-2011 Salvatore Sanfilippo +# Copyright (C) 2010-2011 Pieter Noordhuis +# This file is released under the BSD license, see the COPYING file + +SRC_DIR = src +OBJ_DIR = obj +LIB_DIR = lib +TEST_DIR = tests + +INCLUDE_DIR = include/valkey + +TEST_SRCS = $(wildcard $(TEST_DIR)/*.c) +TEST_OBJS = $(patsubst $(TEST_DIR)/%.c,$(OBJ_DIR)/%.o,$(TEST_SRCS)) +TEST_BINS = $(patsubst $(TEST_DIR)/%.c,$(TEST_DIR)/%,$(TEST_SRCS)) + +SOURCES = $(filter-out $(wildcard $(SRC_DIR)/*ssl*.c), $(wildcard $(SRC_DIR)/*.c)) +HEADERS = $(filter-out $(INCLUDE_DIR)/valkey_ssl.h, $(wildcard $(INCLUDE_DIR)/*.h)) + +OBJS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SOURCES)) + +LIBNAME=libvalkey +PKGCONFNAME=$(LIB_DIR)/valkey.pc + +PKGCONF_TEMPLATE = valkey.pc.in +SSL_PKGCONF_TEMPLATE = valkey_ssl.pc.in + +LIBVALKEY_MAJOR=$(shell grep LIBVALKEY_MAJOR $(INCLUDE_DIR)/valkey.h | awk '{print $$3}') +LIBVALKEY_MINOR=$(shell grep LIBVALKEY_MINOR $(INCLUDE_DIR)/valkey.h | awk '{print $$3}') +LIBVALKEY_PATCH=$(shell grep LIBVALKEY_PATCH $(INCLUDE_DIR)/valkey.h | awk '{print $$3}') +LIBVALKEY_SONAME=$(shell grep LIBVALKEY_SONAME include/valkey/valkey.h | awk '{print $$3}') + +# Installation related variables and target +PREFIX?=/usr/local +INCLUDE_PATH?=include/valkey +LIBRARY_PATH?=lib +PKGCONF_PATH?=pkgconfig +INSTALL_INCLUDE_PATH= $(DESTDIR)$(PREFIX)/$(INCLUDE_PATH) +INSTALL_LIBRARY_PATH= $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH) +INSTALL_PKGCONF_PATH= $(INSTALL_LIBRARY_PATH)/$(PKGCONF_PATH) + +# valkey-server configuration used for testing +VALKEY_PORT=56379 +VALKEY_SERVER=valkey-server +define VALKEY_TEST_CONFIG + daemonize yes + pidfile /tmp/valkey-test-valkey.pid + port $(VALKEY_PORT) + bind 127.0.0.1 + unixsocket /tmp/valkey-test-valkey.sock +endef +export VALKEY_TEST_CONFIG + +# Fallback to gcc when $CC is not in $PATH. +CC:=$(shell sh -c 'type $${CC%% *} >/dev/null 2>/dev/null && echo $(CC) || echo gcc') +CXX:=$(shell sh -c 'type $${CXX%% *} >/dev/null 2>/dev/null && echo $(CXX) || echo g++') +OPTIMIZATION?=-O3 +WARNINGS=-Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers +USE_WERROR?=1 +ifeq ($(USE_WERROR),1) + WARNINGS+=-Werror +endif +DEBUG_FLAGS?= -g -ggdb +REAL_CFLAGS=$(OPTIMIZATION) -fPIC $(CPPFLAGS) $(CFLAGS) $(WARNINGS) $(DEBUG_FLAGS) $(PLATFORM_FLAGS) +REAL_LDFLAGS=$(LDFLAGS) + +DYLIBSUFFIX=so +STLIBSUFFIX=a +DYLIB_MINOR_NAME=$(LIBNAME).$(DYLIBSUFFIX).$(LIBVALKEY_SONAME) +DYLIB_MAJOR_NAME=$(LIBNAME).$(DYLIBSUFFIX).$(LIBVALKEY_MAJOR) +DYLIB_ROOT_NAME=$(LIBNAME).$(DYLIBSUFFIX) +DYLIBNAME=$(LIB_DIR)/$(DYLIB_ROOT_NAME) + +DYLIB_MAKE_CMD=$(CC) $(PLATFORM_FLAGS) -shared -Wl,-soname,$(DYLIB_MINOR_NAME) +STLIB_ROOT_NAME=$(LIBNAME).$(STLIBSUFFIX) +STLIBNAME=$(LIB_DIR)/$(STLIB_ROOT_NAME) +STLIB_MAKE_CMD=$(AR) rcs + +#################### SSL variables start #################### +SSL_LIBNAME=libvalkey_ssl +SSL_PKGCONFNAME=$(LIB_DIR)/valkey_ssl.pc +SSL_INSTALLNAME=install-ssl +SSL_DYLIB_MINOR_NAME=$(SSL_LIBNAME).$(DYLIBSUFFIX).$(LIBVALKEY_SONAME) +SSL_DYLIB_MAJOR_NAME=$(SSL_LIBNAME).$(DYLIBSUFFIX).$(LIBVALKEY_MAJOR) +SSL_ROOT_DYLIB_NAME=$(SSL_LIBNAME).$(DYLIBSUFFIX) +SSL_DYLIBNAME=$(LIB_DIR)/$(SSL_LIBNAME).$(DYLIBSUFFIX) +SSL_STLIBNAME=$(LIB_DIR)/$(SSL_LIBNAME).$(STLIBSUFFIX) +SSL_DYLIB_MAKE_CMD=$(CC) $(PLATFORM_FLAGS) -shared -Wl,-soname,$(SSL_DYLIB_MINOR_NAME) + +USE_SSL?=0 + +ifeq ($(USE_SSL),1) + SSL_SOURCES = $(wildcard $(SRC_DIR)/*ssl*.c) + SSL_OBJS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SSL_SOURCES)) + + # This is required for test.c only + CFLAGS+=-DVALKEY_TEST_SSL + SSL_STLIB=$(SSL_STLIBNAME) + SSL_DYLIB=$(SSL_DYLIBNAME) + SSL_PKGCONF=$(SSL_PKGCONFNAME) + SSL_INSTALL=$(SSL_INSTALLNAME) +else + SSL_STLIB= + SSL_DYLIB= + SSL_PKGCONF= + SSL_INSTALL= +endif +##################### SSL variables end ##################### + +# Platform-specific overrides +uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') + +# This is required for test.c only +ifeq ($(TEST_ASYNC),1) + export CFLAGS+=-DVALKEY_TEST_ASYNC +endif + +ifeq ($(USE_SSL),1) + ifndef OPENSSL_PREFIX + ifeq ($(uname_S),Darwin) + SEARCH_PATH1=/opt/homebrew/opt/openssl + SEARCH_PATH2=/usr/local/opt/openssl + + ifneq ($(wildcard $(SEARCH_PATH1)),) + OPENSSL_PREFIX=$(SEARCH_PATH1) + else ifneq ($(wildcard $(SEARCH_PATH2)),) + OPENSSL_PREFIX=$(SEARCH_PATH2) + endif + endif + endif + + ifdef OPENSSL_PREFIX + CFLAGS+=-I$(OPENSSL_PREFIX)/include + SSL_LDFLAGS+=-L$(OPENSSL_PREFIX)/lib + endif + + SSL_LDFLAGS+=-lssl -lcrypto +endif + +ifeq ($(uname_S),FreeBSD) + LDFLAGS+=-lm + IS_GCC=$(shell sh -c '$(CC) --version 2>/dev/null |egrep -i -c "gcc"') + ifeq ($(IS_GCC),1) + REAL_CFLAGS+=-pedantic + endif +else + REAL_CFLAGS+=-pedantic +endif + +ifeq ($(uname_S),SunOS) + IS_SUN_CC=$(shell sh -c '$(CC) -V 2>&1 |egrep -i -c "sun|studio"') + ifeq ($(IS_SUN_CC),1) + SUN_SHARED_FLAG=-G + else + SUN_SHARED_FLAG=-shared + endif + REAL_LDFLAGS+= -ldl -lnsl -lsocket + DYLIB_MAKE_CMD=$(CC) $(SUN_SHARED_FLAG) -o $(DYLIBNAME) -h $(DYLIB_MINOR_NAME) $(LDFLAGS) + SSL_DYLIB_MAKE_CMD=$(CC) $(SUN_SHARED_FLAG) -o $(SSL_DYLIBNAME) -h $(SSL_DYLIB_MINOR_NAME) $(LDFLAGS) $(SSL_LDFLAGS) +endif +ifeq ($(uname_S),Darwin) + DYLIBSUFFIX=dylib + DYLIB_MINOR_NAME=$(LIBNAME).$(LIBVALKEY_SONAME).$(DYLIBSUFFIX) + DYLIB_MAKE_CMD=$(CC) -dynamiclib -Wl,-install_name,$(PREFIX)/$(LIBRARY_PATH)/$(DYLIB_MINOR_NAME) -o $(DYLIBNAME) $(LDFLAGS) + SSL_DYLIB_MAKE_CMD=$(CC) -dynamiclib -Wl,-install_name,$(PREFIX)/$(LIBRARY_PATH)/$(SSL_DYLIB_MINOR_NAME) -o $(SSL_DYLIBNAME) $(LDFLAGS) $(SSL_LDFLAGS) + DYLIB_PLUGIN=-Wl,-undefined -Wl,dynamic_lookup +endif + +all: dynamic static pkgconfig tests + +$(DYLIBNAME): $(OBJS) | $(LIB_DIR) + $(DYLIB_MAKE_CMD) -o $(DYLIBNAME) $(OBJS) $(REAL_LDFLAGS) + +$(STLIBNAME): $(OBJS) | $(LIB_DIR) + $(STLIB_MAKE_CMD) $(STLIBNAME) $(OBJS) + +$(SSL_DYLIBNAME): $(SSL_OBJS) + $(SSL_DYLIB_MAKE_CMD) $(DYLIB_PLUGIN) -o $(SSL_DYLIBNAME) $(SSL_OBJS) $(REAL_LDFLAGS) $(LDFLAGS) $(SSL_LDFLAGS) + +$(SSL_STLIBNAME): $(SSL_OBJS) + $(STLIB_MAKE_CMD) $(SSL_STLIBNAME) $(SSL_OBJS) + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR) + $(CC) -std=c99 $(REAL_CFLAGS) -I$(INCLUDE_DIR) -MMD -MP -c $< -o $@ + +$(OBJ_DIR)/%.o: $(TEST_DIR)/%.c | $(OBJ_DIR) + $(CC) -std=c99 $(REAL_CFLAGS) -I$(INCLUDE_DIR) -MMD -MP -c $< -o $@ + +$(TEST_DIR)/%: $(OBJ_DIR)/%.o $(STLIBNAME) + $(CC) -o $@ $< $(STLIBNAME) $(SSL_STLIB) $(LDFLAGS) $(SSL_LDLAGS) $(TEST_LDFLAGS) + +$(OBJ_DIR): + mkdir -p $(OBJ_DIR) + +$(LIB_DIR): + mkdir -p $(LIB_DIR) + +dynamic: $(DYLIBNAME) $(SSL_DYLIB) + +static: $(STLIBNAME) $(SSL_STLIB) + +pkgconfig: $(PKGCONFNAME) $(SSL_PKGCONF) + +-include $(OBJS:.o=.d) + +TEST_LIBS = $(STLIBNAME) $(SSL_STLIB) +TEST_LDFLAGS = $(SSL_LDFLAGS) +ifeq ($(USE_SSL),1) + TEST_LDFLAGS += -pthread +endif +ifeq ($(TEST_ASYNC),1) + TEST_LDFLAGS += -levent +endif + +tests: $(TEST_BINS) + +examples: $(STLIBNAME) + $(MAKE) -C examples + +clean: + rm -rf $(OBJ_DIR) $(LIB_DIR) $(TEST_BINS) *.gcda *.gcno *.gcov + make -C examples clean + +INSTALL?= cp -pPR + +$(PKGCONFNAME): $(PKGCONF_TEMPLATE) + @echo "Generating $@ for pkgconfig..." + sed \ + -e 's|@CMAKE_INSTALL_PREFIX@|$(PREFIX)|g' \ + -e 's|@CMAKE_INSTALL_LIBDIR@|$(INSTALL_LIBRARY_PATH)|g' \ + -e 's|@PROJECT_VERSION@|$(LIBVALKEY_SONAME)|g' \ + $< > $@ + +$(SSL_PKGCONFNAME): $(SSL_PKGCONF_TEMPLATE) + @echo "Generating $@ for pkgconfig..." + sed \ + -e 's|@CMAKE_INSTALL_PREFIX@|$(PREFIX)|g' \ + -e 's|@CMAKE_INSTALL_LIBDIR@|$(INSTALL_LIBRARY_PATH)|g' \ + -e 's|@PROJECT_VERSION@|$(LIBVALKEY_SONAME)|g' \ + $< > $@ + +install: $(DYLIBNAME) $(STLIBNAME) $(PKGCONFNAME) $(SSL_INSTALL) + mkdir -p $(INSTALL_INCLUDE_PATH)/adapters $(INSTALL_LIBRARY_PATH) + $(INSTALL) $(HEADERS) $(INSTALL_INCLUDE_PATH) + $(INSTALL) $(INCLUDE_DIR)/adapters/*.h $(INSTALL_INCLUDE_PATH)/adapters + $(INSTALL) $(DYLIBNAME) $(INSTALL_LIBRARY_PATH)/$(DYLIB_MINOR_NAME) + ln -sf $(DYLIB_MINOR_NAME) $(INSTALL_LIBRARY_PATH)/$(DYLIB_ROOT_NAME) + ln -sf $(DYLIB_MINOR_NAME) $(INSTALL_LIBRARY_PATH)/$(DYLIB_MAJOR_NAME) + $(INSTALL) $(STLIBNAME) $(INSTALL_LIBRARY_PATH) + mkdir -p $(INSTALL_PKGCONF_PATH) + $(INSTALL) $(PKGCONFNAME) $(INSTALL_PKGCONF_PATH) + +install-ssl: $(SSL_DYLIBNAME) $(SSL_STLIBNAME) $(SSL_PKGCONFNAME) + mkdir -p $(INSTALL_INCLUDE_PATH) $(INSTALL_LIBRARY_PATH) + $(INSTALL) $(INCLUDE_DIR)/valkey_ssl.h $(INSTALL_INCLUDE_PATH) + $(INSTALL) $(SSL_DYLIBNAME) $(INSTALL_LIBRARY_PATH)/$(SSL_DYLIB_MINOR_NAME) + ln -sf $(SSL_DYLIB_MINOR_NAME) $(INSTALL_LIBRARY_PATH)/$(SSL_ROOT_DYLIB_NAME) + ln -sf $(SSL_DYLIB_MINOR_NAME) $(INSTALL_LIBRARY_PATH)/$(SSL_DYLIB_MAJOR_NAME) + $(INSTALL) $(SSL_STLIBNAME) $(INSTALL_LIBRARY_PATH) + mkdir -p $(INSTALL_PKGCONF_PATH) + $(INSTALL) $(SSL_PKGCONFNAME) $(INSTALL_PKGCONF_PATH) + +32bit: + @echo "" + @echo "WARNING: if this fails under Linux you probably need to install libc6-dev-i386" + @echo "" + $(MAKE) CFLAGS="-m32" LDFLAGS="-m32" + +32bit-vars: + $(eval CFLAGS=-m32) + $(eval LDFLAGS=-m32) + +gprof: + $(MAKE) CFLAGS="-pg" LDFLAGS="-pg" + +gcov: + $(MAKE) CFLAGS+="-fprofile-arcs -ftest-coverage" LDFLAGS="-fprofile-arcs" + +coverage: gcov + make check + mkdir -p tmp/lcov + lcov -d . -c --exclude '/usr*' -o tmp/lcov/valkey.info + lcov -q -l tmp/lcov/valkey.info + genhtml --legend -q -o tmp/lcov/report tmp/lcov/valkey.info + +debug: + $(MAKE) OPTIMIZATION="-O0" + +.PHONY: all test check clean install 32bit 32bit-vars gprof gcov noopt diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 00000000..35db2ca3 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,61 @@ +INCLUDE(FindPkgConfig) +# Check for GLib + +PKG_CHECK_MODULES(GLIB2 glib-2.0) +if (GLIB2_FOUND) + INCLUDE_DIRECTORIES(${GLIB2_INCLUDE_DIRS}) + LINK_DIRECTORIES(${GLIB2_LIBRARY_DIRS}) + ADD_EXECUTABLE(async-glib async-glib.c) + TARGET_LINK_LIBRARIES(async-glib valkey ${GLIB2_LIBRARIES}) +ENDIF(GLIB2_FOUND) + +FIND_PATH(LIBEV ev.h + HINTS /usr/local /usr/opt/local + ENV LIBEV_INCLUDE_DIR) + +if (LIBEV) + # Just compile and link with libev + ADD_EXECUTABLE(async-libev async-libev.c) + TARGET_LINK_LIBRARIES(async-libev valkey ev) +ENDIF() + +FIND_PATH(LIBEVENT event.h) +if (LIBEVENT) + ADD_EXECUTABLE(async-libevent async-libevent.c) + TARGET_LINK_LIBRARIES(async-libevent valkey event) +ENDIF() + +FIND_PATH(LIBHV hv/hv.h) +IF (LIBHV) + ADD_EXECUTABLE(async-libhv async-libhv.c) + TARGET_LINK_LIBRARIES(async-libhv valkey hv) +ENDIF() + +FIND_PATH(LIBUV uv.h) +IF (LIBUV) + ADD_EXECUTABLE(async-libuv async-libuv.c) + TARGET_LINK_LIBRARIES(async-libuv valkey uv) +ENDIF() + +FIND_PATH(LIBSDEVENT systemd/sd-event.h) +IF (LIBSDEVENT) + ADD_EXECUTABLE(async-libsdevent async-libsdevent.c) + TARGET_LINK_LIBRARIES(async-libsdevent valkey systemd) +ENDIF() + +IF (APPLE) + FIND_LIBRARY(CF CoreFoundation) + ADD_EXECUTABLE(async-macosx async-macosx.c) + TARGET_LINK_LIBRARIES(async-macosx valkey ${CF}) +ENDIF() + +IF (ENABLE_SSL) + ADD_EXECUTABLE(example-ssl example-ssl.c) + TARGET_LINK_LIBRARIES(example-ssl valkey valkey_ssl) +ENDIF() + +ADD_EXECUTABLE(example blocking.c) +TARGET_LINK_LIBRARIES(example valkey) + +ADD_EXECUTABLE(blocking-push blocking-push.c) +TARGET_LINK_LIBRARIES(blocking-push valkey) diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 00000000..3521ff19 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,96 @@ +CC?=gcc +CXX?=g++ + +CFLAGS=-Wall -Wextra -g -O2 -I../include/valkey +STLIBNAME=../lib/libvalkey.a + +# Define examples +EXAMPLES=example-blocking example-blocking-push example-async-libevent \ + example-async-libev example-async-glib example-async-poll + +ifeq ($(USE_SSL),1) + EXAMPLES+=blocking-ssl async-libevent-ssl + SSL_STLIBNAME=../lib/libvalkey_ssl.a + SSL_LDFLAGS=-lssl -lcrypto +endif + +.PHONY: all clean + +all: $(EXAMPLES) + +$(STLIBNAME): + $(MAKE) -C ../ + +$(SSL_STLIBNAME): + USE_SSL=1 $(MAKE) -C ../ + +example-blocking: blocking.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) $< $(STLIBNAME) + +example-blocking-push: blocking-push.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) $< $(STLIBNAME) + +example-blocking-ssl: blocking-ssl.c $(STLIBNAME) $(SSL_STLIBNAME) + $(CC) -o $@ $(CFLAGS) $< $(STLIBNAME) $(SSL_STLIBNAME) $(SSL_LDFLAGS) + +example-async-libevent: async-libevent.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) $< -levent $(STLIBNAME) + +example-async-libevent-ssl: async-libevent-ssl.c $(STLIBNAME) $(SSL_STLIBNAME) + $(CC) -o $@ $(CFLAGS) $< -levent $(STLIBNAME) $(SSL_STLIBNAME) $(SSL_LDFLAGS) + +example-async-libev: async-libev.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) $< -lev $(STLIBNAME) + +example-async-libhv: async-libhv.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) $< -lhv $(STLIBNAME) + +example-async-libsdevent: async-libsdevent.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) $< -lsdevent $(STLIBNAME) + +example-async-glib: async-glib.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) $< $(shell pkg-config --cflags --libs glib-2.0) $(STLIBNAME) + +example-async-ivykis: async-ivykis.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) $< -livykis $(STLIBNAME) + +example-async-macosx: async-macosx.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) $< -framework CoreFoundation $(STLIBNAME) + +example-async-poll: async-poll.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) $< $(STLIBNAME) + +ifndef AE_DIR +example-async-ae: + @echo "Please specify AE_DIR (e.g. /src)" + @false +else +example-async-ae: async-ae.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) -I$(AE_DIR) $< $(AE_DIR)/ae.o $(AE_DIR)/zmalloc.o $(AE_DIR)/../deps/jemalloc/lib/libjemalloc.a -pthread $(STLIBNAME) +endif + +ifndef LIBUV_DIR +# dynamic link libuv.so +example-async-libuv: async-libuv.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) -I$(LIBUV_DIR)/include $< -luv -lpthread -lrt $(STLIBNAME) +else +# use user provided static lib +example-async-libuv: async-libuv.c $(STLIBNAME) + $(CC) -o $@ $(CFLAGS) -I$(LIBUV_DIR)/include $< $(LIBUV_DIR)/.libs/libuv.a -lpthread -lrt $(STLIBNAME) +endif + +ifeq ($(and $(QT_MOC),$(QT_INCLUDE_DIR),$(QT_LIBRARY_DIR)),) +example-async-qt: + @echo "Please specify QT_MOC, QT_INCLUDE_DIR AND QT_LIBRARY_DIR" + @false +else +example-async-qt: async-qt.cpp $(STLIBNAME) + $(QT_MOC) .adapters/qt.h -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore | \ + $(CXX) -x c++ -o qt-adapter-moc.o -c - $(CFLAGS) -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore + $(QT_MOC) async-qt.h -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore | \ + $(CXX) -x c++ -o qt-example-moc.o -c - $(CFLAGS) -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore + $(CXX) -o $@ $(CFLAGS) $(LDFLAGS) -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore -L$(QT_LIBRARY_DIR) qt-adapter-moc.o qt-example-moc.o $< -pthread $(STLIBNAME) -lQtCore +endif + +clean: + rm -f example-* *.o diff --git a/libvalkey/examples/example-ae.c b/examples/async-ae.c similarity index 100% rename from libvalkey/examples/example-ae.c rename to examples/async-ae.c diff --git a/libvalkey/examples/example-glib.c b/examples/async-glib.c similarity index 100% rename from libvalkey/examples/example-glib.c rename to examples/async-glib.c diff --git a/libvalkey/examples/example-ivykis.c b/examples/async-ivykis.c similarity index 100% rename from libvalkey/examples/example-ivykis.c rename to examples/async-ivykis.c diff --git a/libvalkey/examples/example-libev.c b/examples/async-libev.c similarity index 100% rename from libvalkey/examples/example-libev.c rename to examples/async-libev.c diff --git a/libvalkey/examples/example-libevent-ssl.c b/examples/async-libevent-ssl.c similarity index 100% rename from libvalkey/examples/example-libevent-ssl.c rename to examples/async-libevent-ssl.c diff --git a/libvalkey/examples/example-libevent.c b/examples/async-libevent.c similarity index 100% rename from libvalkey/examples/example-libevent.c rename to examples/async-libevent.c diff --git a/libvalkey/examples/example-libhv.c b/examples/async-libhv.c similarity index 100% rename from libvalkey/examples/example-libhv.c rename to examples/async-libhv.c diff --git a/libvalkey/examples/example-libsdevent.c b/examples/async-libsdevent.c similarity index 100% rename from libvalkey/examples/example-libsdevent.c rename to examples/async-libsdevent.c diff --git a/libvalkey/examples/example-libuv.c b/examples/async-libuv.c similarity index 100% rename from libvalkey/examples/example-libuv.c rename to examples/async-libuv.c diff --git a/libvalkey/examples/example-macosx.c b/examples/async-macosx.c similarity index 100% rename from libvalkey/examples/example-macosx.c rename to examples/async-macosx.c diff --git a/libvalkey/examples/example-poll.c b/examples/async-poll.c similarity index 100% rename from libvalkey/examples/example-poll.c rename to examples/async-poll.c diff --git a/libvalkey/examples/example-qt.cpp b/examples/async-qt.cpp similarity index 100% rename from libvalkey/examples/example-qt.cpp rename to examples/async-qt.cpp diff --git a/libvalkey/examples/example-qt.h b/examples/async-qt.h similarity index 100% rename from libvalkey/examples/example-qt.h rename to examples/async-qt.h diff --git a/libvalkey/examples/example-redismoduleapi.c b/examples/async-valkeymoduleapi.c similarity index 100% rename from libvalkey/examples/example-redismoduleapi.c rename to examples/async-valkeymoduleapi.c diff --git a/libvalkey/examples/example-push.c b/examples/blocking-push.c similarity index 98% rename from libvalkey/examples/example-push.c rename to examples/blocking-push.c index 3ecceb65..03c0fe72 100644 --- a/libvalkey/examples/example-push.c +++ b/examples/blocking-push.c @@ -63,7 +63,7 @@ static void enableClientTracking(valkeyContext *c) { if (reply->type != VALKEY_REPLY_MAP) { fprintf(stderr, "Error: Can't send HELLO 3 command. Are you sure you're "); - fprintf(stderr, "connected to valkey-server >= 6.0.0?\Server error: %s\n", + fprintf(stderr, "connected to valkey-server >= 6.0.0?\nServer error: %s\n", reply->type == VALKEY_REPLY_ERROR ? reply->str : "(unknown)"); exit(-1); } diff --git a/libvalkey/examples/example-ssl.c b/examples/blocking-ssl.c similarity index 100% rename from libvalkey/examples/example-ssl.c rename to examples/blocking-ssl.c diff --git a/libvalkey/examples/example.c b/examples/blocking.c similarity index 100% rename from libvalkey/examples/example.c rename to examples/blocking.c diff --git a/src/adapters/ae.h b/include/valkey/adapters/ae.h similarity index 99% rename from src/adapters/ae.h rename to include/valkey/adapters/ae.h index 861387b0..db1ec882 100644 --- a/src/adapters/ae.h +++ b/include/valkey/adapters/ae.h @@ -133,6 +133,7 @@ static int valkeyAeAttach_link(valkeyAsyncContext *ac, void *base) { return valkeyAeAttach((aeEventLoop *)base, ac); } +VALKEY_UNUSED static int valkeyClusterAeAttach(aeEventLoop *loop, valkeyClusterAsyncContext *acc) { if (acc == NULL || loop == NULL) { diff --git a/src/adapters/glib.h b/include/valkey/adapters/glib.h similarity index 99% rename from src/adapters/glib.h rename to include/valkey/adapters/glib.h index 51087282..c7b2ab62 100644 --- a/src/adapters/glib.h +++ b/include/valkey/adapters/glib.h @@ -166,6 +166,7 @@ static int valkeyGlibAttach_link(valkeyAsyncContext *ac, void *adapter) { return VALKEY_ERR; } +VALKEY_UNUSED static int valkeyClusterGlibAttach(valkeyClusterAsyncContext *acc, valkeyClusterGlibAdapter *adapter) { if (acc == NULL || adapter == NULL) { diff --git a/src/adapters/ivykis.h b/include/valkey/adapters/ivykis.h similarity index 100% rename from src/adapters/ivykis.h rename to include/valkey/adapters/ivykis.h diff --git a/src/adapters/libev.h b/include/valkey/adapters/libev.h similarity index 99% rename from src/adapters/libev.h rename to include/valkey/adapters/libev.h index b3ba2cc2..dc6670ac 100644 --- a/src/adapters/libev.h +++ b/include/valkey/adapters/libev.h @@ -190,6 +190,7 @@ static int valkeyLibevAttach_link(valkeyAsyncContext *ac, void *loop) { return valkeyLibevAttach((struct ev_loop *)loop, ac); } +VALKEY_UNUSED static int valkeyClusterLibevAttach(valkeyClusterAsyncContext *acc, struct ev_loop *loop) { if (loop == NULL || acc == NULL) { diff --git a/src/adapters/libevent.h b/include/valkey/adapters/libevent.h similarity index 99% rename from src/adapters/libevent.h rename to include/valkey/adapters/libevent.h index a5f53e05..8350bec0 100644 --- a/src/adapters/libevent.h +++ b/include/valkey/adapters/libevent.h @@ -174,10 +174,12 @@ static int valkeyLibeventAttach(valkeyAsyncContext *ac, struct event_base *base) return VALKEY_OK; } +VALKEY_UNUSED static int valkeyLibeventAttach_link(valkeyAsyncContext *ac, void *base) { return valkeyLibeventAttach(ac, (struct event_base *)base); } +VALKEY_UNUSED static int valkeyClusterLibeventAttach(valkeyClusterAsyncContext *acc, struct event_base *base) { if (acc == NULL || base == NULL) { diff --git a/src/adapters/libhv.h b/include/valkey/adapters/libhv.h similarity index 100% rename from src/adapters/libhv.h rename to include/valkey/adapters/libhv.h diff --git a/src/adapters/libsdevent.h b/include/valkey/adapters/libsdevent.h similarity index 100% rename from src/adapters/libsdevent.h rename to include/valkey/adapters/libsdevent.h diff --git a/src/adapters/libuv.h b/include/valkey/adapters/libuv.h similarity index 99% rename from src/adapters/libuv.h rename to include/valkey/adapters/libuv.h index 082d2f6d..80c4b2ae 100644 --- a/src/adapters/libuv.h +++ b/include/valkey/adapters/libuv.h @@ -206,6 +206,7 @@ static int valkeyLibuvAttach_link(valkeyAsyncContext *ac, void *loop) { return valkeyLibuvAttach(ac, (uv_loop_t *)loop); } +VALKEY_UNUSED static int valkeyClusterLibuvAttach(valkeyClusterAsyncContext *acc, uv_loop_t *loop) { if (acc == NULL || loop == NULL) { diff --git a/src/adapters/macosx.h b/include/valkey/adapters/macosx.h similarity index 100% rename from src/adapters/macosx.h rename to include/valkey/adapters/macosx.h diff --git a/src/adapters/poll.h b/include/valkey/adapters/poll.h similarity index 100% rename from src/adapters/poll.h rename to include/valkey/adapters/poll.h diff --git a/src/adapters/qt.h b/include/valkey/adapters/qt.h similarity index 100% rename from src/adapters/qt.h rename to include/valkey/adapters/qt.h diff --git a/src/adapters/valkeymoduleapi.h b/include/valkey/adapters/valkeymoduleapi.h similarity index 100% rename from src/adapters/valkeymoduleapi.h rename to include/valkey/adapters/valkeymoduleapi.h diff --git a/src/alloc.h b/include/valkey/alloc.h similarity index 100% rename from src/alloc.h rename to include/valkey/alloc.h diff --git a/src/async.h b/include/valkey/async.h similarity index 97% rename from src/async.h rename to include/valkey/async.h index 0b73758d..0c17eca2 100644 --- a/src/async.h +++ b/include/valkey/async.h @@ -37,6 +37,13 @@ extern "C" { #endif +/* For the async cluster attach functions. */ +#if defined(__GNUC__) || defined(__clang__) +#define VALKEY_UNUSED __attribute__((unused)) +#else +#define VALKEY_UNUSED +#endif + struct valkeyAsyncContext; /* need forward declaration of valkeyAsyncContext */ struct dict; /* dictionary header is included in async.c */ diff --git a/src/dict.h b/include/valkey/dict.h similarity index 88% rename from src/dict.h rename to include/valkey/dict.h index 6ad0acd8..d059f6b3 100644 --- a/src/dict.h +++ b/include/valkey/dict.h @@ -111,15 +111,15 @@ typedef struct dictIterator { #define dictSize(ht) ((ht)->used) /* API */ -static unsigned int dictGenHashFunction(const unsigned char *buf, int len); -static dict *dictCreate(dictType *type, void *privDataPtr); -static int dictExpand(dict *ht, unsigned long size); -static int dictAdd(dict *ht, void *key, void *val); -static int dictReplace(dict *ht, void *key, void *val); -static int dictDelete(dict *ht, const void *key); -static void dictRelease(dict *ht); -static dictEntry * dictFind(dict *ht, const void *key); -static void dictInitIterator(dictIterator *iter, dict *ht); -static dictEntry *dictNext(dictIterator *iter); +unsigned int dictGenHashFunction(const unsigned char *buf, int len); +dict *dictCreate(dictType *type, void *privDataPtr); +int dictExpand(dict *ht, unsigned long size); +int dictAdd(dict *ht, void *key, void *val); +int dictReplace(dict *ht, void *key, void *val); +int dictDelete(dict *ht, const void *key); +void dictRelease(dict *ht); +dictEntry * dictFind(dict *ht, const void *key); +void dictInitIterator(dictIterator *iter, dict *ht); +dictEntry *dictNext(dictIterator *iter); #endif /* __DICT_H */ diff --git a/src/net.h b/include/valkey/net.h similarity index 100% rename from src/net.h rename to include/valkey/net.h diff --git a/src/read.h b/include/valkey/read.h similarity index 100% rename from src/read.h rename to include/valkey/read.h diff --git a/src/sds.h b/include/valkey/sds.h similarity index 100% rename from src/sds.h rename to include/valkey/sds.h diff --git a/src/sockcompat.h b/include/valkey/sockcompat.h similarity index 100% rename from src/sockcompat.h rename to include/valkey/sockcompat.h diff --git a/src/valkey.h b/include/valkey/valkey.h similarity index 100% rename from src/valkey.h rename to include/valkey/valkey.h diff --git a/src/valkey_ssl.h b/include/valkey/valkey_ssl.h similarity index 100% rename from src/valkey_ssl.h rename to include/valkey/valkey_ssl.h diff --git a/src/valkeycluster.h b/include/valkey/valkeycluster.h similarity index 100% rename from src/valkeycluster.h rename to include/valkey/valkeycluster.h diff --git a/src/valkeycluster_ssl.h b/include/valkey/valkeycluster_ssl.h similarity index 100% rename from src/valkeycluster_ssl.h rename to include/valkey/valkeycluster_ssl.h diff --git a/libvalkey/Makefile b/libvalkey/Makefile deleted file mode 100644 index af861c0f..00000000 --- a/libvalkey/Makefile +++ /dev/null @@ -1,358 +0,0 @@ -# Libvalkey Makefile -# Copyright (C) 2010-2011 Salvatore Sanfilippo -# Copyright (C) 2010-2011 Pieter Noordhuis -# This file is released under the BSD license, see the COPYING file - -OBJ=alloc.o net.o valkey.o sds.o async.o read.o sockcompat.o -EXAMPLES=valkey-example valkey-example-libevent valkey-example-libev valkey-example-glib valkey-example-push valkey-example-poll -TESTS=libvalkey-test -LIBNAME=libvalkey -PKGCONFNAME=valkey.pc - -LIBVALKEY_MAJOR=$(shell grep LIBVALKEY_MAJOR valkey.h | awk '{print $$3}') -LIBVALKEY_MINOR=$(shell grep LIBVALKEY_MINOR valkey.h | awk '{print $$3}') -LIBVALKEY_PATCH=$(shell grep LIBVALKEY_PATCH valkey.h | awk '{print $$3}') -LIBVALKEY_SONAME=$(shell grep LIBVALKEY_SONAME valkey.h | awk '{print $$3}') - -# Installation related variables and target -PREFIX?=/usr/local -INCLUDE_PATH?=include/valkey -LIBRARY_PATH?=lib -PKGCONF_PATH?=pkgconfig -INSTALL_INCLUDE_PATH= $(DESTDIR)$(PREFIX)/$(INCLUDE_PATH) -INSTALL_LIBRARY_PATH= $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH) -INSTALL_PKGCONF_PATH= $(INSTALL_LIBRARY_PATH)/$(PKGCONF_PATH) - -# valkey-server configuration used for testing -VALKEY_PORT=56379 -VALKEY_SERVER=valkey-server -define VALKEY_TEST_CONFIG - daemonize yes - pidfile /tmp/valkey-test-valkey.pid - port $(VALKEY_PORT) - bind 127.0.0.1 - unixsocket /tmp/valkey-test-valkey.sock -endef -export VALKEY_TEST_CONFIG - -# Fallback to gcc when $CC is not in $PATH. -CC:=$(shell sh -c 'type $${CC%% *} >/dev/null 2>/dev/null && echo $(CC) || echo gcc') -CXX:=$(shell sh -c 'type $${CXX%% *} >/dev/null 2>/dev/null && echo $(CXX) || echo g++') -OPTIMIZATION?=-O3 -WARNINGS=-Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -USE_WERROR?=1 -ifeq ($(USE_WERROR),1) - WARNINGS+=-Werror -endif -DEBUG_FLAGS?= -g -ggdb -REAL_CFLAGS=$(OPTIMIZATION) -fPIC $(CPPFLAGS) $(CFLAGS) $(WARNINGS) $(DEBUG_FLAGS) $(PLATFORM_FLAGS) -REAL_LDFLAGS=$(LDFLAGS) - -DYLIBSUFFIX=so -STLIBSUFFIX=a -DYLIB_MINOR_NAME=$(LIBNAME).$(DYLIBSUFFIX).$(LIBVALKEY_SONAME) -DYLIB_MAJOR_NAME=$(LIBNAME).$(DYLIBSUFFIX).$(LIBVALKEY_MAJOR) -DYLIBNAME=$(LIBNAME).$(DYLIBSUFFIX) - -DYLIB_MAKE_CMD=$(CC) $(PLATFORM_FLAGS) -shared -Wl,-soname,$(DYLIB_MINOR_NAME) -STLIBNAME=$(LIBNAME).$(STLIBSUFFIX) -STLIB_MAKE_CMD=$(AR) rcs - -#################### SSL variables start #################### -SSL_OBJ=ssl.o -SSL_LIBNAME=libvalkey_ssl -SSL_PKGCONFNAME=valkey_ssl.pc -SSL_INSTALLNAME=install-ssl -SSL_DYLIB_MINOR_NAME=$(SSL_LIBNAME).$(DYLIBSUFFIX).$(LIBVALKEY_SONAME) -SSL_DYLIB_MAJOR_NAME=$(SSL_LIBNAME).$(DYLIBSUFFIX).$(LIBVALKEY_MAJOR) -SSL_DYLIBNAME=$(SSL_LIBNAME).$(DYLIBSUFFIX) -SSL_STLIBNAME=$(SSL_LIBNAME).$(STLIBSUFFIX) -SSL_DYLIB_MAKE_CMD=$(CC) $(PLATFORM_FLAGS) -shared -Wl,-soname,$(SSL_DYLIB_MINOR_NAME) - -USE_SSL?=0 -ifeq ($(USE_SSL),1) - # This is required for test.c only - CFLAGS+=-DVALKEY_TEST_SSL - EXAMPLES+=valkey-example-ssl valkey-example-libevent-ssl - SSL_STLIB=$(SSL_STLIBNAME) - SSL_DYLIB=$(SSL_DYLIBNAME) - SSL_PKGCONF=$(SSL_PKGCONFNAME) - SSL_INSTALL=$(SSL_INSTALLNAME) -else - SSL_STLIB= - SSL_DYLIB= - SSL_PKGCONF= - SSL_INSTALL= -endif -##################### SSL variables end ##################### - - -# Platform-specific overrides -uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') - -# This is required for test.c only -ifeq ($(TEST_ASYNC),1) - export CFLAGS+=-DVALKEY_TEST_ASYNC -endif - -ifeq ($(USE_SSL),1) - ifndef OPENSSL_PREFIX - ifeq ($(uname_S),Darwin) - SEARCH_PATH1=/opt/homebrew/opt/openssl - SEARCH_PATH2=/usr/local/opt/openssl - - ifneq ($(wildcard $(SEARCH_PATH1)),) - OPENSSL_PREFIX=$(SEARCH_PATH1) - else ifneq ($(wildcard $(SEARCH_PATH2)),) - OPENSSL_PREFIX=$(SEARCH_PATH2) - endif - endif - endif - - ifdef OPENSSL_PREFIX - CFLAGS+=-I$(OPENSSL_PREFIX)/include - SSL_LDFLAGS+=-L$(OPENSSL_PREFIX)/lib - endif - - SSL_LDFLAGS+=-lssl -lcrypto -endif - -ifeq ($(uname_S),FreeBSD) - LDFLAGS+=-lm - IS_GCC=$(shell sh -c '$(CC) --version 2>/dev/null |egrep -i -c "gcc"') - ifeq ($(IS_GCC),1) - REAL_CFLAGS+=-pedantic - endif -else - REAL_CFLAGS+=-pedantic -endif - -ifeq ($(uname_S),SunOS) - IS_SUN_CC=$(shell sh -c '$(CC) -V 2>&1 |egrep -i -c "sun|studio"') - ifeq ($(IS_SUN_CC),1) - SUN_SHARED_FLAG=-G - else - SUN_SHARED_FLAG=-shared - endif - REAL_LDFLAGS+= -ldl -lnsl -lsocket - DYLIB_MAKE_CMD=$(CC) $(SUN_SHARED_FLAG) -o $(DYLIBNAME) -h $(DYLIB_MINOR_NAME) $(LDFLAGS) - SSL_DYLIB_MAKE_CMD=$(CC) $(SUN_SHARED_FLAG) -o $(SSL_DYLIBNAME) -h $(SSL_DYLIB_MINOR_NAME) $(LDFLAGS) $(SSL_LDFLAGS) -endif -ifeq ($(uname_S),Darwin) - DYLIBSUFFIX=dylib - DYLIB_MINOR_NAME=$(LIBNAME).$(LIBVALKEY_SONAME).$(DYLIBSUFFIX) - DYLIB_MAKE_CMD=$(CC) -dynamiclib -Wl,-install_name,$(PREFIX)/$(LIBRARY_PATH)/$(DYLIB_MINOR_NAME) -o $(DYLIBNAME) $(LDFLAGS) - SSL_DYLIB_MAKE_CMD=$(CC) -dynamiclib -Wl,-install_name,$(PREFIX)/$(LIBRARY_PATH)/$(SSL_DYLIB_MINOR_NAME) -o $(SSL_DYLIBNAME) $(LDFLAGS) $(SSL_LDFLAGS) - DYLIB_PLUGIN=-Wl,-undefined -Wl,dynamic_lookup -endif - -all: dynamic static libvalkey-test pkgconfig - -dynamic: $(DYLIBNAME) $(SSL_DYLIB) - -static: $(STLIBNAME) $(SSL_STLIB) - -pkgconfig: $(PKGCONFNAME) $(SSL_PKGCONF) - -# Deps (use make dep to generate this) -alloc.o: alloc.c fmacros.h alloc.h -async.o: async.c fmacros.h alloc.h async.h valkey.h read.h sds.h net.h dict.c dict.h win32.h async_private.h -dict.o: dict.c fmacros.h alloc.h dict.h -valkey.o: valkey.c fmacros.h valkey.h read.h sds.h alloc.h net.h async.h win32.h -net.o: net.c fmacros.h net.h valkey.h read.h sds.h alloc.h sockcompat.h win32.h -read.o: read.c fmacros.h alloc.h read.h sds.h win32.h -sds.o: sds.c sds.h sdsalloc.h alloc.h -sockcompat.o: sockcompat.c sockcompat.h -test.o: test.c fmacros.h valkey.h read.h sds.h alloc.h net.h sockcompat.h win32.h - -$(DYLIBNAME): $(OBJ) - $(DYLIB_MAKE_CMD) -o $(DYLIBNAME) $(OBJ) $(REAL_LDFLAGS) - -$(STLIBNAME): $(OBJ) - $(STLIB_MAKE_CMD) $(STLIBNAME) $(OBJ) - -#################### SSL building rules start #################### -$(SSL_DYLIBNAME): $(SSL_OBJ) - $(SSL_DYLIB_MAKE_CMD) $(DYLIB_PLUGIN) -o $(SSL_DYLIBNAME) $(SSL_OBJ) $(REAL_LDFLAGS) $(LDFLAGS) $(SSL_LDFLAGS) - -$(SSL_STLIBNAME): $(SSL_OBJ) - $(STLIB_MAKE_CMD) $(SSL_STLIBNAME) $(SSL_OBJ) - -$(SSL_OBJ): ssl.c valkey.h read.h sds.h alloc.h async.h win32.h async_private.h -#################### SSL building rules end #################### - -# Binaries: -valkey-example-libevent: examples/example-libevent.c adapters/libevent.h $(STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -levent $(STLIBNAME) $(REAL_LDFLAGS) - -valkey-example-libevent-ssl: examples/example-libevent-ssl.c adapters/libevent.h $(STLIBNAME) $(SSL_STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -levent $(STLIBNAME) $(SSL_STLIBNAME) $(REAL_LDFLAGS) $(SSL_LDFLAGS) - -valkey-example-libev: examples/example-libev.c adapters/libev.h $(STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -lev $(STLIBNAME) $(REAL_LDFLAGS) - -valkey-example-libhv: examples/example-libhv.c adapters/libhv.h $(STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -lhv $(STLIBNAME) $(REAL_LDFLAGS) - -valkey-example-glib: examples/example-glib.c adapters/glib.h $(STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< $(shell pkg-config --cflags --libs glib-2.0) $(STLIBNAME) $(REAL_LDFLAGS) - -valkey-example-ivykis: examples/example-ivykis.c adapters/ivykis.h $(STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -livykis $(STLIBNAME) $(REAL_LDFLAGS) - -valkey-example-macosx: examples/example-macosx.c adapters/macosx.h $(STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -framework CoreFoundation $(STLIBNAME) $(REAL_LDFLAGS) - -valkey-example-ssl: examples/example-ssl.c $(STLIBNAME) $(SSL_STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< $(STLIBNAME) $(SSL_STLIBNAME) $(REAL_LDFLAGS) $(SSL_LDFLAGS) - -valkey-example-poll: examples/example-poll.c adapters/poll.h $(STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< $(STLIBNAME) $(REAL_LDFLAGS) - -ifndef AE_DIR -valkey-example-ae: - @echo "Please specify AE_DIR (e.g. /src)" - @false -else -valkey-example-ae: examples/example-ae.c adapters/ae.h $(STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. -I$(AE_DIR) $< $(AE_DIR)/ae.o $(AE_DIR)/zmalloc.o $(AE_DIR)/../deps/jemalloc/lib/libjemalloc.a -pthread $(STLIBNAME) -endif - -ifndef LIBUV_DIR -# dynamic link libuv.so -valkey-example-libuv: examples/example-libuv.c adapters/libuv.h $(STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. -I$(LIBUV_DIR)/include $< -luv -lpthread -lrt $(STLIBNAME) $(REAL_LDFLAGS) -else -# use user provided static lib -valkey-example-libuv: examples/example-libuv.c adapters/libuv.h $(STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. -I$(LIBUV_DIR)/include $< $(LIBUV_DIR)/.libs/libuv.a -lpthread -lrt $(STLIBNAME) $(REAL_LDFLAGS) -endif - -ifeq ($(and $(QT_MOC),$(QT_INCLUDE_DIR),$(QT_LIBRARY_DIR)),) -valkey-example-qt: - @echo "Please specify QT_MOC, QT_INCLUDE_DIR AND QT_LIBRARY_DIR" - @false -else -valkey-example-qt: examples/example-qt.cpp adapters/qt.h $(STLIBNAME) - $(QT_MOC) adapters/qt.h -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore | \ - $(CXX) -x c++ -o qt-adapter-moc.o -c - $(REAL_CFLAGS) -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore - $(QT_MOC) examples/example-qt.h -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore | \ - $(CXX) -x c++ -o qt-example-moc.o -c - $(REAL_CFLAGS) -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore - $(CXX) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore -L$(QT_LIBRARY_DIR) qt-adapter-moc.o qt-example-moc.o $< -pthread $(STLIBNAME) -lQtCore -endif - -valkey-example: examples/example.c $(STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< $(STLIBNAME) $(REAL_LDFLAGS) - -valkey-example-push: examples/example-push.c $(STLIBNAME) - $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< $(STLIBNAME) $(REAL_LDFLAGS) - -examples: $(EXAMPLES) - -TEST_LIBS = $(STLIBNAME) $(SSL_STLIB) -TEST_LDFLAGS = $(SSL_LDFLAGS) -ifeq ($(USE_SSL),1) - TEST_LDFLAGS += -pthread -endif -ifeq ($(TEST_ASYNC),1) - TEST_LDFLAGS += -levent -endif - -libvalkey-test: test.o $(TEST_LIBS) - $(CC) -o $@ $(REAL_CFLAGS) -I. $^ $(REAL_LDFLAGS) $(TEST_LDFLAGS) - -valkey-%: %.o $(STLIBNAME) - $(CC) $(REAL_CFLAGS) -o $@ $< $(TEST_LIBS) $(REAL_LDFLAGS) - -test: libvalkey-test - ./libvalkey-test - -check: libvalkey-test - TEST_SSL=$(USE_SSL) ./test.sh - -.c.o: - $(CC) -std=c99 -c $(REAL_CFLAGS) $< - -clean: - rm -rf $(DYLIBNAME) $(STLIBNAME) $(SSL_DYLIBNAME) $(SSL_STLIBNAME) $(TESTS) $(PKGCONFNAME) examples/valkey-example* *.o *.gcda *.gcno *.gcov - -dep: - $(CC) $(CPPFLAGS) $(CFLAGS) -MM *.c - -INSTALL?= cp -pPR - -$(PKGCONFNAME): valkey.h - @echo "Generating $@ for pkgconfig..." - @echo prefix=$(PREFIX) > $@ - @echo exec_prefix=\$${prefix} >> $@ - @echo libdir=$(PREFIX)/$(LIBRARY_PATH) >> $@ - @echo includedir=$(PREFIX)/include >> $@ - @echo pkgincludedir=$(PREFIX)/$(INCLUDE_PATH) >> $@ - @echo >> $@ - @echo Name: valkey >> $@ - @echo Description: C client library for Valkey. >> $@ - @echo Version: $(LIBVALKEY_MAJOR).$(LIBVALKEY_MINOR).$(LIBVALKEY_PATCH) >> $@ - @echo Libs: -L\$${libdir} -lvalkey >> $@ - @echo Cflags: -I\$${pkgincludedir} -I\$${includedir} -D_FILE_OFFSET_BITS=64 >> $@ - -$(SSL_PKGCONFNAME): valkey_ssl.h - @echo "Generating $@ for pkgconfig..." - @echo prefix=$(PREFIX) > $@ - @echo exec_prefix=\$${prefix} >> $@ - @echo libdir=$(PREFIX)/$(LIBRARY_PATH) >> $@ - @echo includedir=$(PREFIX)/include >> $@ - @echo pkgincludedir=$(PREFIX)/$(INCLUDE_PATH) >> $@ - @echo >> $@ - @echo Name: valkey_ssl >> $@ - @echo Description: SSL Support for valkey. >> $@ - @echo Version: $(LIBVALKEY_MAJOR).$(LIBVALKEY_MINOR).$(LIBVALKEY_PATCH) >> $@ - @echo Requires: valkey >> $@ - @echo Libs: -L\$${libdir} -lvalkey_ssl >> $@ - @echo Libs.private: -lssl -lcrypto >> $@ - -install: $(DYLIBNAME) $(STLIBNAME) $(PKGCONFNAME) $(SSL_INSTALL) - mkdir -p $(INSTALL_INCLUDE_PATH) $(INSTALL_INCLUDE_PATH)/adapters $(INSTALL_LIBRARY_PATH) - $(INSTALL) valkey.h async.h read.h sds.h alloc.h sockcompat.h $(INSTALL_INCLUDE_PATH) - $(INSTALL) adapters/*.h $(INSTALL_INCLUDE_PATH)/adapters - $(INSTALL) $(DYLIBNAME) $(INSTALL_LIBRARY_PATH)/$(DYLIB_MINOR_NAME) - cd $(INSTALL_LIBRARY_PATH) && ln -sf $(DYLIB_MINOR_NAME) $(DYLIBNAME) && ln -sf $(DYLIB_MINOR_NAME) $(DYLIB_MAJOR_NAME) - $(INSTALL) $(STLIBNAME) $(INSTALL_LIBRARY_PATH) - mkdir -p $(INSTALL_PKGCONF_PATH) - $(INSTALL) $(PKGCONFNAME) $(INSTALL_PKGCONF_PATH) - -install-ssl: $(SSL_DYLIBNAME) $(SSL_STLIBNAME) $(SSL_PKGCONFNAME) - mkdir -p $(INSTALL_INCLUDE_PATH) $(INSTALL_LIBRARY_PATH) - $(INSTALL) valkey_ssl.h $(INSTALL_INCLUDE_PATH) - $(INSTALL) $(SSL_DYLIBNAME) $(INSTALL_LIBRARY_PATH)/$(SSL_DYLIB_MINOR_NAME) - cd $(INSTALL_LIBRARY_PATH) && ln -sf $(SSL_DYLIB_MINOR_NAME) $(SSL_DYLIBNAME) && ln -sf $(SSL_DYLIB_MINOR_NAME) $(SSL_DYLIB_MAJOR_NAME) - $(INSTALL) $(SSL_STLIBNAME) $(INSTALL_LIBRARY_PATH) - mkdir -p $(INSTALL_PKGCONF_PATH) - $(INSTALL) $(SSL_PKGCONFNAME) $(INSTALL_PKGCONF_PATH) - -32bit: - @echo "" - @echo "WARNING: if this fails under Linux you probably need to install libc6-dev-i386" - @echo "" - $(MAKE) CFLAGS="-m32" LDFLAGS="-m32" - -32bit-vars: - $(eval CFLAGS=-m32) - $(eval LDFLAGS=-m32) - -gprof: - $(MAKE) CFLAGS="-pg" LDFLAGS="-pg" - -gcov: - $(MAKE) CFLAGS+="-fprofile-arcs -ftest-coverage" LDFLAGS="-fprofile-arcs" - -coverage: gcov - make check - mkdir -p tmp/lcov - lcov -d . -c --exclude '/usr*' -o tmp/lcov/valkey.info - lcov -q -l tmp/lcov/valkey.info - genhtml --legend -q -o tmp/lcov/report tmp/lcov/valkey.info - -noopt: - $(MAKE) OPTIMIZATION="" - -.PHONY: all test check clean dep install 32bit 32bit-vars gprof gcov noopt diff --git a/libvalkey/examples/CMakeLists.txt b/libvalkey/examples/CMakeLists.txt deleted file mode 100644 index 49901ece..00000000 --- a/libvalkey/examples/CMakeLists.txt +++ /dev/null @@ -1,61 +0,0 @@ -INCLUDE(FindPkgConfig) -# Check for GLib - -PKG_CHECK_MODULES(GLIB2 glib-2.0) -if (GLIB2_FOUND) - INCLUDE_DIRECTORIES(${GLIB2_INCLUDE_DIRS}) - LINK_DIRECTORIES(${GLIB2_LIBRARY_DIRS}) - ADD_EXECUTABLE(example-glib example-glib.c) - TARGET_LINK_LIBRARIES(example-glib valkey ${GLIB2_LIBRARIES}) -ENDIF(GLIB2_FOUND) - -FIND_PATH(LIBEV ev.h - HINTS /usr/local /usr/opt/local - ENV LIBEV_INCLUDE_DIR) - -if (LIBEV) - # Just compile and link with libev - ADD_EXECUTABLE(example-libev example-libev.c) - TARGET_LINK_LIBRARIES(example-libev valkey ev) -ENDIF() - -FIND_PATH(LIBEVENT event.h) -if (LIBEVENT) - ADD_EXECUTABLE(example-libevent example-libevent.c) - TARGET_LINK_LIBRARIES(example-libevent valkey event) -ENDIF() - -FIND_PATH(LIBHV hv/hv.h) -IF (LIBHV) - ADD_EXECUTABLE(example-libhv example-libhv.c) - TARGET_LINK_LIBRARIES(example-libhv valkey hv) -ENDIF() - -FIND_PATH(LIBUV uv.h) -IF (LIBUV) - ADD_EXECUTABLE(example-libuv example-libuv.c) - TARGET_LINK_LIBRARIES(example-libuv valkey uv) -ENDIF() - -FIND_PATH(LIBSDEVENT systemd/sd-event.h) -IF (LIBSDEVENT) - ADD_EXECUTABLE(example-libsdevent example-libsdevent.c) - TARGET_LINK_LIBRARIES(example-libsdevent valkey systemd) -ENDIF() - -IF (APPLE) - FIND_LIBRARY(CF CoreFoundation) - ADD_EXECUTABLE(example-macosx example-macosx.c) - TARGET_LINK_LIBRARIES(example-macosx valkey ${CF}) -ENDIF() - -IF (ENABLE_SSL) - ADD_EXECUTABLE(example-ssl example-ssl.c) - TARGET_LINK_LIBRARIES(example-ssl valkey valkey_ssl) -ENDIF() - -ADD_EXECUTABLE(example example.c) -TARGET_LINK_LIBRARIES(example valkey) - -ADD_EXECUTABLE(example-push example-push.c) -TARGET_LINK_LIBRARIES(example-push valkey) diff --git a/src/async.c b/src/async.c index a18ce8a6..9dd398d1 100644 --- a/src/async.c +++ b/src/async.c @@ -41,7 +41,7 @@ #include #include "async.h" #include "net.h" -#include "dict.c" +#include "dict.h" #include "sds.h" #include "win32.h" diff --git a/src/dict.c b/src/dict.c index 2ba4cf78..9a365a80 100644 --- a/src/dict.c +++ b/src/dict.c @@ -51,7 +51,7 @@ static int _dictInit(dict *ht, dictType *type, void *privDataPtr); /* Generic hash function (a popular one from Bernstein). * I tested a few and this was the best. */ -static unsigned int dictGenHashFunction(const unsigned char *buf, int len) { +unsigned int dictGenHashFunction(const unsigned char *buf, int len) { unsigned int hash = 5381; while (len--) @@ -71,7 +71,7 @@ static void _dictReset(dict *ht) { } /* Create a new hash table */ -static dict *dictCreate(dictType *type, void *privDataPtr) { +dict *dictCreate(dictType *type, void *privDataPtr) { dict *ht = vk_malloc(sizeof(*ht)); if (ht == NULL) return NULL; @@ -89,7 +89,7 @@ static int _dictInit(dict *ht, dictType *type, void *privDataPtr) { } /* Expand or create the hashtable */ -static int dictExpand(dict *ht, unsigned long size) { +int dictExpand(dict *ht, unsigned long size) { dict n; /* the new hashtable */ unsigned long realsize = _dictNextPower(size), i; @@ -138,7 +138,7 @@ static int dictExpand(dict *ht, unsigned long size) { } /* Add an element to the target hash table */ -static int dictAdd(dict *ht, void *key, void *val) { +int dictAdd(dict *ht, void *key, void *val) { int index; dictEntry *entry; @@ -166,7 +166,7 @@ static int dictAdd(dict *ht, void *key, void *val) { * Return 1 if the key was added from scratch, 0 if there was already an * element with such key and dictReplace() just performed a value update * operation. */ -static int dictReplace(dict *ht, void *key, void *val) { +int dictReplace(dict *ht, void *key, void *val) { dictEntry *entry, auxentry; /* Try to add the element. If the key @@ -191,7 +191,7 @@ static int dictReplace(dict *ht, void *key, void *val) { } /* Search and remove an element */ -static int dictDelete(dict *ht, const void *key) { +int dictDelete(dict *ht, const void *key) { unsigned int h; dictEntry *de, *prevde; @@ -247,12 +247,12 @@ static int _dictClear(dict *ht) { } /* Clear & Release the hash table */ -static void dictRelease(dict *ht) { +void dictRelease(dict *ht) { _dictClear(ht); vk_free(ht); } -static dictEntry *dictFind(dict *ht, const void *key) { +dictEntry *dictFind(dict *ht, const void *key) { dictEntry *he; unsigned int h; @@ -267,14 +267,14 @@ static dictEntry *dictFind(dict *ht, const void *key) { return NULL; } -static void dictInitIterator(dictIterator *iter, dict *ht) { +void dictInitIterator(dictIterator *iter, dict *ht) { iter->ht = ht; iter->index = -1; iter->entry = NULL; iter->nextEntry = NULL; } -static dictEntry *dictNext(dictIterator *iter) { +dictEntry *dictNext(dictIterator *iter) { while (1) { if (iter->entry == NULL) { iter->index++; diff --git a/src/valkeycluster-config.cmake.in b/src/valkeycluster-config.cmake.in deleted file mode 100644 index be9a588f..00000000 --- a/src/valkeycluster-config.cmake.in +++ /dev/null @@ -1,13 +0,0 @@ -@PACKAGE_INIT@ - -set_and_check(hiredis_cluster_INCLUDEDIR "@PACKAGE_INCLUDE_INSTALL_DIR@") - -if(NOT TARGET hiredis_cluster::hiredis_cluster) - include(${CMAKE_CURRENT_LIST_DIR}/hiredis_cluster-targets.cmake) -endif() - -set(hiredis_cluster_LIBRARIES hiredis_cluster::hiredis_cluster) -set(hiredis_cluster_INCLUDE_DIRS ${hiredis_cluster_INCLUDEDIR}) - -check_required_components(hiredis_cluster) - diff --git a/src/valkeycluster.c b/src/valkeycluster.c index 0977a218..7abdce87 100644 --- a/src/valkeycluster.c +++ b/src/valkeycluster.c @@ -37,8 +37,8 @@ #include #include #include -#include +#include "alloc.h" #include "adlist.h" #include "command.h" #include "dict.h" @@ -3353,6 +3353,7 @@ int valkeyClusterAppendCommandArgv(valkeyClusterContext *cc, int argc, return ret; } +VALKEY_UNUSED static int valkeyClusterSendAll(valkeyClusterContext *cc) { dictEntry *de; valkeyClusterNode *node; @@ -3388,6 +3389,7 @@ static int valkeyClusterSendAll(valkeyClusterContext *cc) { return VALKEY_OK; } +VALKEY_UNUSED static int valkeyClusterClearAll(valkeyClusterContext *cc) { dictEntry *de; valkeyClusterNode *node; @@ -3532,7 +3534,7 @@ int valkeyClusterGetReply(valkeyClusterContext *cc, void **reply) { } /** - * Resets cluster state after pipeline. + * Resets cluster state after pipeline. * Resets Valkey node connections if pipeline commands were not called beforehand. */ void valkeyClusterReset(valkeyClusterContext *cc) { diff --git a/src/valkeycluster.pc.in b/src/valkeycluster.pc.in deleted file mode 100644 index 0857071b..00000000 --- a/src/valkeycluster.pc.in +++ /dev/null @@ -1,11 +0,0 @@ -prefix=@CMAKE_INSTALL_PREFIX@ -exec_prefix=${prefix} -libdir=${exec_prefix}/lib -includedir=${prefix}/include -pkgincludedir=${includedir}/hiredis_cluster - -Name: hiredis_cluster -Description: Minimalistic C client library for Redis with cluster support. -Version: @PROJECT_VERSION@ -Libs: -L${libdir} -lhiredis_cluster -Cflags: -I${pkgincludedir} -D_FILE_OFFSET_BITS=64 diff --git a/src/valkeycluster_ssl-config.cmake.in b/src/valkeycluster_ssl-config.cmake.in deleted file mode 100644 index 1c0509b3..00000000 --- a/src/valkeycluster_ssl-config.cmake.in +++ /dev/null @@ -1,12 +0,0 @@ -@PACKAGE_INIT@ - -set_and_check(hiredis_cluster_ssl_INCLUDEDIR "@PACKAGE_INCLUDE_INSTALL_DIR@") - -if(NOT TARGET hiredis_cluster::hiredis_cluster_ssl) - include(${CMAKE_CURRENT_LIST_DIR}/hiredis_cluster_ssl-targets.cmake) -endif() - -set(hiredis_cluster_ssl_LIBRARIES hiredis_cluster::hiredis_cluster_ssl) -set(hiredis_cluster_ssl_INCLUDE_DIRS ${hiredis_cluster_ssl_INCLUDEDIR}) - -check_required_components(hiredis_cluster_ssl) diff --git a/src/valkeycluster_ssl.pc.in b/src/valkeycluster_ssl.pc.in deleted file mode 100644 index 61feb7fb..00000000 --- a/src/valkeycluster_ssl.pc.in +++ /dev/null @@ -1,12 +0,0 @@ -prefix=@CMAKE_INSTALL_PREFIX@ -exec_prefix=${prefix} -libdir=${exec_prefix}/lib -includedir=${prefix}/include -pkgincludedir=${includedir}/hiredis_cluster - -Name: hiredis_cluster_ssl -Description: SSL support for hiredis-cluster -Version: @PROJECT_VERSION@ -Requires: hiredis_cluster -Libs: -L${libdir} -lhiredis_cluster_ssl -Libs.private: -lhiredis_ssl -lssl -lcrypto diff --git a/src/test.c b/tests/test.c similarity index 95% rename from src/test.c rename to tests/test.c index 2844f72a..8aa5e048 100644 --- a/src/test.c +++ b/tests/test.c @@ -1,10 +1,11 @@ -#include "fmacros.h" +#define _POSIX_C_SOURCE 200112L #include "sockcompat.h" #include #include #include #ifndef _WIN32 #include +#include #include #endif #include @@ -12,6 +13,7 @@ #include #include #include +#include #include "valkey.h" #include "async.h" @@ -23,8 +25,6 @@ #include "adapters/libevent.h" #include #endif -#include "net.h" -#include "win32.h" enum connection_type { CONN_TCP, @@ -76,12 +76,12 @@ static int tests = 0, fails = 0, skips = 0; #define test_cond(_c) if(_c) printf("\033[0;32mPASSED\033[0;0m\n"); else {printf("\033[0;31mFAILED\033[0;0m\n"); fails++;} #define test_skipped() { printf("\033[01;33mSKIPPED\033[0;0m\n"); skips++; } -static void millisleep(int ms) -{ +static void millisleep(int ms) { #ifdef _MSC_VER Sleep(ms); #else - usleep(ms*1000); + struct timespec ts = { ms / 1000, (ms % 1000) * 1000000 }; + nanosleep(&ts, NULL); #endif } @@ -876,6 +876,18 @@ static void test_free_null(void) { test_cond(reply == NULL); } +/* Wrap malloc to abort on failure so OOM checks don't make the test logic + * harder to follow. */ +static void *vk_malloc_safe(size_t size) { + void *ptr = vk_malloc(size); + if (ptr == NULL) { + fprintf(stderr, "Error: Out of memory\n"); + exit(-1); + } + + return ptr; +} + static void *vk_malloc_fail(size_t size) { (void)size; return NULL; @@ -900,6 +912,18 @@ static void *vk_realloc_fail(void *ptr, size_t size) { return NULL; } +static char *vk_test_strdup(const char *s) { + size_t len; + char *dup; + + len = strlen(s); + dup = vk_malloc_safe(len + 1); + + memcpy(dup,s,len + 1); + + return dup; +} + static void test_allocator_injection(void) { void *ptr; @@ -907,7 +931,7 @@ static void test_allocator_injection(void) { .mallocFn = vk_malloc_fail, .callocFn = vk_calloc_fail, .reallocFn = vk_realloc_fail, - .strdupFn = strdup, + .strdupFn = vk_test_strdup, .freeFn = free, }; @@ -1420,18 +1444,6 @@ static void test_invalid_timeout_errors(struct config config) { valkeyFree(c); } -/* Wrap malloc to abort on failure so OOM checks don't make the test logic - * harder to follow. */ -void *vk_malloc_safe(size_t size) { - void *ptr = vk_malloc(size); - if (ptr == NULL) { - fprintf(stderr, "Error: Out of memory\n"); - exit(-1); - } - - return ptr; -} - static void test_throughput(struct config config) { valkeyContext *c = do_connect(config); valkeyReply **replies; @@ -1521,105 +1533,6 @@ static void test_throughput(struct config config) { disconnect(c, 0); } -// static long __test_callback_flags = 0; -// static void __test_callback(valkeyContext *c, void *privdata) { -// ((void)c); -// /* Shift to detect execution order */ -// __test_callback_flags <<= 8; -// __test_callback_flags |= (long)privdata; -// } -// -// static void __test_reply_callback(valkeyContext *c, valkeyReply *reply, void *privdata) { -// ((void)c); -// /* Shift to detect execution order */ -// __test_callback_flags <<= 8; -// __test_callback_flags |= (long)privdata; -// if (reply) freeReplyObject(reply); -// } -// -// static valkeyContext *__connect_nonblock() { -// /* Reset callback flags */ -// __test_callback_flags = 0; -// return valkeyConnectNonBlock("127.0.0.1", port, NULL); -// } -// -// static void test_nonblocking_connection() { -// valkeyContext *c; -// int wdone = 0; -// -// test("Calls command callback when command is issued: "); -// c = __connect_nonblock(); -// valkeySetCommandCallback(c,__test_callback,(void*)1); -// valkeyCommand(c,"PING"); -// test_cond(__test_callback_flags == 1); -// valkeyFree(c); -// -// test("Calls disconnect callback on valkeyDisconnect: "); -// c = __connect_nonblock(); -// valkeySetDisconnectCallback(c,__test_callback,(void*)2); -// valkeyDisconnect(c); -// test_cond(__test_callback_flags == 2); -// valkeyFree(c); -// -// test("Calls disconnect callback and free callback on valkeyFree: "); -// c = __connect_nonblock(); -// valkeySetDisconnectCallback(c,__test_callback,(void*)2); -// valkeySetFreeCallback(c,__test_callback,(void*)4); -// valkeyFree(c); -// test_cond(__test_callback_flags == ((2 << 8) | 4)); -// -// test("valkeyBufferWrite against empty write buffer: "); -// c = __connect_nonblock(); -// test_cond(valkeyBufferWrite(c,&wdone) == VALKEY_OK && wdone == 1); -// valkeyFree(c); -// -// test("valkeyBufferWrite against not yet connected fd: "); -// c = __connect_nonblock(); -// valkeyCommand(c,"PING"); -// test_cond(valkeyBufferWrite(c,NULL) == VALKEY_ERR && -// strncmp(c->error,"write:",6) == 0); -// valkeyFree(c); -// -// test("valkeyBufferWrite against closed fd: "); -// c = __connect_nonblock(); -// valkeyCommand(c,"PING"); -// valkeyDisconnect(c); -// test_cond(valkeyBufferWrite(c,NULL) == VALKEY_ERR && -// strncmp(c->error,"write:",6) == 0); -// valkeyFree(c); -// -// test("Process callbacks in the right sequence: "); -// c = __connect_nonblock(); -// valkeyCommandWithCallback(c,__test_reply_callback,(void*)1,"PING"); -// valkeyCommandWithCallback(c,__test_reply_callback,(void*)2,"PING"); -// valkeyCommandWithCallback(c,__test_reply_callback,(void*)3,"PING"); -// -// /* Write output buffer */ -// wdone = 0; -// while(!wdone) { -// usleep(500); -// valkeyBufferWrite(c,&wdone); -// } -// -// /* Read until at least one callback is executed (the 3 replies will -// * arrive in a single packet, causing all callbacks to be executed in -// * a single pass). */ -// while(__test_callback_flags == 0) { -// assert(valkeyBufferRead(c) == VALKEY_OK); -// valkeyProcessCallbacks(c); -// } -// test_cond(__test_callback_flags == 0x010203); -// valkeyFree(c); -// -// test("valkeyDisconnect executes pending callbacks with NULL reply: "); -// c = __connect_nonblock(); -// valkeySetDisconnectCallback(c,__test_callback,(void*)1); -// valkeyCommandWithCallback(c,__test_reply_callback,(void*)2,"PING"); -// valkeyDisconnect(c); -// test_cond(__test_callback_flags == 0x0201); -// valkeyFree(c); -// } - #ifdef VALKEY_TEST_ASYNC #pragma GCC diagnostic ignored "-Woverlength-strings" /* required on gcc 4.8.x due to assert statements */ diff --git a/libvalkey/test.sh b/tests/test.sh similarity index 87% rename from libvalkey/test.sh rename to tests/test.sh index d4b47914..b419b9de 100755 --- a/libvalkey/test.sh +++ b/tests/test.sh @@ -1,4 +1,11 @@ #!/bin/sh -ue +# +check_executable() { + if ! command -v "$1" >/dev/null 2>&1; then + echo "Error: $1 is not found or not executable." + exit 1 + fi +} VALKEY_SERVER=${VALKEY_SERVER:-valkey-server} VALKEY_PORT=${VALKEY_PORT:-56379} @@ -10,6 +17,8 @@ SSL_TEST_ARGS= SKIPS_ARG=${SKIPS_ARG:-} VALKEY_DOCKER=${VALKEY_DOCKER:-} +check_executable "$VALKEY_SERVER" + # Enable debug command for redis-server >= 7.0.0 or any version of valkey-server. VALKEY_MAJOR_VERSION="$("$VALKEY_SERVER" --version|awk -F'[^0-9]+' '{ print $2 }')" if [ "$VALKEY_MAJOR_VERSION" -gt "6" ]; then @@ -104,9 +113,13 @@ else fi # Wait until we detect the unix socket echo waiting for server -while [ ! -S "${SOCK_FILE}" ]; do sleep 1; done +while [ ! -S "${SOCK_FILE}" ]; do + 2>&1 echo "Waiting for server..." + ps aux|grep valkey-server + sleep 1; +done # Treat skips as failures if directed [ "$SKIPS_AS_FAILS" = 1 ] && SKIPS_ARG="${SKIPS_ARG} --skips-as-fails" -${TEST_PREFIX:-} ./libvalkey-test -h 127.0.0.1 -p ${VALKEY_PORT} -s ${SOCK_FILE} ${SSL_TEST_ARGS} ${SKIPS_ARG} +${TEST_PREFIX:-} ./test -h 127.0.0.1 -p ${VALKEY_PORT} -s ${SOCK_FILE} ${SSL_TEST_ARGS} ${SKIPS_ARG} diff --git a/libvalkey/valkey-config.cmake.in b/valkey-config.cmake.in similarity index 100% rename from libvalkey/valkey-config.cmake.in rename to valkey-config.cmake.in diff --git a/libvalkey/valkey.pc.in b/valkey.pc.in similarity index 100% rename from libvalkey/valkey.pc.in rename to valkey.pc.in diff --git a/libvalkey/valkey_ssl-config.cmake.in b/valkey_ssl-config.cmake.in similarity index 100% rename from libvalkey/valkey_ssl-config.cmake.in rename to valkey_ssl-config.cmake.in diff --git a/libvalkey/valkey_ssl.pc.in b/valkey_ssl.pc.in similarity index 100% rename from libvalkey/valkey_ssl.pc.in rename to valkey_ssl.pc.in