Skip to content

Commit

Permalink
Move libvalkeycluster examples to new file structure (#28)
Browse files Browse the repository at this point in the history
Includes updates to
- Build all examples successfully with CMake.
- Build all examples successfully with Make.
  • Loading branch information
bjosv authored Jun 26, 2024
1 parent 4b601d9 commit cdd5d81
Show file tree
Hide file tree
Showing 26 changed files with 181 additions and 217 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ SET(valkey_sources
src/vkarray.c
src/vkutil.c)

SET(valkey_sources ${valkey_sources})

IF(WIN32)
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -DWIN32_LEAN_AND_MEAN)
ENDIF()
Expand Down Expand Up @@ -168,7 +166,8 @@ IF(ENABLE_SSL)
ENDIF()
FIND_PACKAGE(OpenSSL REQUIRED)
SET(valkey_ssl_sources
src/ssl.c)
src/ssl.c
src/valkeycluster_ssl.c)
ADD_LIBRARY(valkey_ssl ${valkey_ssl_sources})
ADD_LIBRARY(valkey::valkey_ssl ALIAS valkey_ssl)

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ examples: $(STLIBNAME)

clean:
rm -rf $(OBJ_DIR) $(LIB_DIR) $(TEST_BINS) *.gcda *.gcno *.gcov
make -C examples clean
$(MAKE) -C examples clean

INSTALL?= cp -pPR

Expand Down
138 changes: 84 additions & 54 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,61 +1,91 @@
cmake_minimum_required(VERSION 3.0.0)
project(examples LANGUAGES C)

# Add path to enable <valkey/x.h> includes
include_directories(${CMAKE_SOURCE_DIR}/include)

# Check for GLib
find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
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})
add_executable(example-async-glib async-glib.c)
target_include_directories(example-async-glib PUBLIC ${GLIB2_INCLUDE_DIRS})
target_link_libraries(example-async-glib valkey ${GLIB2_LIBRARIES})
endif()
endif()

find_path(LIBEV ev.h
HINTS /usr/local /usr/opt/local
ENV LIBEV_INCLUDE_DIR)
if(LIBEV)
add_executable(example-async-libev async-libev.c)
target_link_libraries(example-async-libev valkey ev)
endif()

find_path(LIBEVENT event.h)
if(LIBEVENT)
add_executable(example-async-libevent async-libevent.c)
target_link_libraries(example-async-libevent valkey event)

if(ENABLE_SSL)
add_executable(example-async-libevent-ssl async-libevent-ssl.c)
target_link_libraries(example-async-libevent-ssl valkey valkey_ssl event)
endif()
endif()

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)
find_path(LIBHV hv/hv.h)
if(LIBHV)
add_executable(example-async-libhv async-libhv.c)
target_link_libraries(example-async-libhv valkey hv)
endif()

find_path(LIBUV uv.h)
if(LIBUV)
add_executable(example-async-libuv async-libuv.c)
target_link_libraries(example-async-libuv valkey uv)
endif()

find_path(LIBSDEVENT systemd/sd-event.h)
if(LIBSDEVENT)
add_executable(example-async-libsdevent async-libsdevent.c)
target_link_libraries(example-async-libsdevent valkey systemd)
endif()

if(APPLE)
find_library(CF CoreFoundation)
add_executable(example-async-macosx async-macosx.c)
target_link_libraries(example-async-macosx valkey ${CF})
endif()

if(ENABLE_SSL)
add_executable(example-blocking-ssl blocking-ssl.c)
target_link_libraries(example-blocking-ssl valkey valkey_ssl)
endif()

add_executable(example-blocking blocking.c)
target_link_libraries(example-blocking valkey)

add_executable(example-blocking-push blocking-push.c)
target_link_libraries(example-blocking-push valkey)

if(LIBEVENT)
add_executable(example-cluster-async cluster-async.c)
target_link_libraries(example-cluster-async valkey event)

if(ENABLE_SSL)
add_executable(example-cluster-async-tls cluster-async-tls.c)
target_link_libraries(example-cluster-async-tls valkey valkey_ssl event)
endif()

add_executable(example-cluster-clientside-caching-async cluster-clientside-caching-async.c)
target_link_libraries(example-cluster-clientside-caching-async valkey event)
endif()

add_executable(example-cluster-simple cluster-simple.c)
target_link_libraries(example-cluster-simple valkey)

if(ENABLE_SSL)
add_executable(example-cluster-tls cluster-tls.c)
target_link_libraries(example-cluster-tls valkey valkey_ssl)
endif()
24 changes: 21 additions & 3 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
CC?=gcc
CXX?=g++

CFLAGS=-Wall -Wextra -g -O2 -I../include/valkey
CFLAGS=-Wall -Wextra -g -O2 -I../include
STLIBNAME=../lib/libvalkey.a

# Define examples
EXAMPLES=example-blocking example-blocking-push example-async-libevent \
example-async-libev example-async-glib example-async-poll
example-async-libev example-async-glib example-async-poll \
example-cluster-async example-cluster-clientside-caching-async \
example-cluster-simple

ifeq ($(USE_SSL),1)
EXAMPLES+=blocking-ssl async-libevent-ssl
EXAMPLES+=example-blocking-ssl example-async-libevent-ssl \
example-cluster-async-tls example-cluster-tls
SSL_STLIBNAME=../lib/libvalkey_ssl.a
SSL_LDFLAGS=-lssl -lcrypto
endif
Expand Down Expand Up @@ -92,5 +95,20 @@ example-async-qt: async-qt.cpp $(STLIBNAME)
$(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

example-cluster-async: cluster-async.c $(STLIBNAME)
$(CC) -o $@ $(CFLAGS) $< -levent $(STLIBNAME)

example-cluster-async-tls: cluster-async-tls.c $(STLIBNAME) $(SSL_STLIBNAME)
$(CC) -o $@ $(CFLAGS) $< -levent $(STLIBNAME) $(SSL_STLIBNAME) $(SSL_LDFLAGS)

example-cluster-clientside-caching-async: cluster-clientside-caching-async.c $(STLIBNAME)
$(CC) -o $@ $(CFLAGS) $< -levent $(STLIBNAME)

example-cluster-simple: cluster-simple.c $(STLIBNAME)
$(CC) -o $@ $(CFLAGS) $< $(STLIBNAME)

example-cluster-tls: cluster-tls.c $(STLIBNAME) $(SSL_STLIBNAME)
$(CC) -o $@ $(CFLAGS) $< $(STLIBNAME) $(SSL_STLIBNAME) $(SSL_LDFLAGS)

clean:
rm -f example-* *.o
6 changes: 3 additions & 3 deletions examples/async-ae.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <string.h>
#include <signal.h>

#include <valkey.h>
#include <async.h>
#include <adapters/ae.h>
#include <valkey/valkey.h>
#include <valkey/async.h>
#include <valkey/adapters/ae.h>

/* Put event loop in the global scope, so it can be explicitly stopped */
static aeEventLoop *loop;
Expand Down
6 changes: 3 additions & 3 deletions examples/async-glib.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <stdlib.h>

#include <valkey.h>
#include <async.h>
#include <adapters/glib.h>
#include <valkey/valkey.h>
#include <valkey/async.h>
#include <valkey/adapters/glib.h>

static GMainLoop *mainloop;

Expand Down
6 changes: 3 additions & 3 deletions examples/async-ivykis.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <string.h>
#include <signal.h>

#include <valkey.h>
#include <async.h>
#include <adapters/ivykis.h>
#include <valkey/valkey.h>
#include <valkey/async.h>
#include <valkey/adapters/ivykis.h>

void getCallback(valkeyAsyncContext *c, void *r, void *privdata) {
valkeyReply *reply = r;
Expand Down
6 changes: 3 additions & 3 deletions examples/async-libev.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <string.h>
#include <signal.h>

#include <valkey.h>
#include <async.h>
#include <adapters/libev.h>
#include <valkey/valkey.h>
#include <valkey/async.h>
#include <valkey/adapters/libev.h>

void getCallback(valkeyAsyncContext *c, void *r, void *privdata) {
valkeyReply *reply = r;
Expand Down
8 changes: 4 additions & 4 deletions examples/async-libevent-ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <string.h>
#include <signal.h>

#include <valkey.h>
#include <valkey_ssl.h>
#include <async.h>
#include <adapters/libevent.h>
#include <valkey/valkey.h>
#include <valkey/valkey_ssl.h>
#include <valkey/async.h>
#include <valkey/adapters/libevent.h>

void getCallback(valkeyAsyncContext *c, void *r, void *privdata) {
valkeyReply *reply = r;
Expand Down
6 changes: 3 additions & 3 deletions examples/async-libevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <string.h>
#include <signal.h>

#include <valkey.h>
#include <async.h>
#include <adapters/libevent.h>
#include <valkey/valkey.h>
#include <valkey/async.h>
#include <valkey/adapters/libevent.h>

void getCallback(valkeyAsyncContext *c, void *r, void *privdata) {
valkeyReply *reply = r;
Expand Down
6 changes: 3 additions & 3 deletions examples/async-libhv.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <string.h>
#include <signal.h>

#include <valkey.h>
#include <async.h>
#include <adapters/libhv.h>
#include <valkey/valkey.h>
#include <valkey/async.h>
#include <valkey/adapters/libhv.h>

void getCallback(valkeyAsyncContext *c, void *r, void *privdata) {
valkeyReply *reply = r;
Expand Down
6 changes: 3 additions & 3 deletions examples/async-libsdevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <string.h>
#include <signal.h>

#include <valkey.h>
#include <async.h>
#include <adapters/libsdevent.h>
#include <valkey/valkey.h>
#include <valkey/async.h>
#include <valkey/adapters/libsdevent.h>

void debugCallback(valkeyAsyncContext *c, void *r, void *privdata) {
(void)privdata;
Expand Down
6 changes: 3 additions & 3 deletions examples/async-libuv.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <string.h>
#include <signal.h>

#include <valkey.h>
#include <async.h>
#include <adapters/libuv.h>
#include <valkey/valkey.h>
#include <valkey/async.h>
#include <valkey/adapters/libuv.h>

void debugCallback(valkeyAsyncContext *c, void *r, void *privdata) {
(void)privdata; //unused
Expand Down
6 changes: 3 additions & 3 deletions examples/async-macosx.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

#include <stdio.h>

#include <valkey.h>
#include <async.h>
#include <adapters/macosx.h>
#include <valkey/valkey.h>
#include <valkey/async.h>
#include <valkey/adapters/macosx.h>

void getCallback(valkeyAsyncContext *c, void *r, void *privdata) {
valkeyReply *reply = r;
Expand Down
4 changes: 2 additions & 2 deletions examples/async-poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <signal.h>
#include <unistd.h>

#include <async.h>
#include <adapters/poll.h>
#include <valkey/async.h>
#include <valkey/adapters/poll.h>

/* Put in the global scope, so that loop can be explicitly stopped */
static int exit_loop = 0;
Expand Down
2 changes: 1 addition & 1 deletion examples/async-qt.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef VALKEY_EXAMPLE_QT_H
#define VALKEY_EXAMPLE_QT_H

#include <adapters/qt.h>
#include <valkey/adapters/qt.h>

class ExampleQt : public QObject {

Expand Down
2 changes: 1 addition & 1 deletion examples/blocking-push.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <valkey.h>
#include <valkey/valkey.h>

#define KEY_COUNT 5

Expand Down
4 changes: 2 additions & 2 deletions examples/blocking-ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <stdlib.h>
#include <string.h>

#include <valkey.h>
#include <valkey_ssl.h>
#include <valkey/valkey.h>
#include <valkey/valkey_ssl.h>

#ifdef _MSC_VER
#include <winsock2.h> /* For struct timeval */
Expand Down
2 changes: 1 addition & 1 deletion examples/blocking.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <valkey.h>
#include <valkey/valkey.h>

#ifdef _MSC_VER
#include <winsock2.h> /* For struct timeval */
Expand Down
Loading

0 comments on commit cdd5d81

Please sign in to comment.