Skip to content

Commit

Permalink
feat: add osx support
Browse files Browse the repository at this point in the history
  • Loading branch information
philippeboyd committed Feb 6, 2025
1 parent 664a7be commit b09c668
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 100 deletions.
110 changes: 42 additions & 68 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,96 +8,70 @@ on:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: 'recursive'

- name: Install Prerequisites and clone Redis
run: |
sudo apt-get update
sudo apt-get install -y cmake libssl-dev valgrind git
git clone https://git.cryptomilk.org/projects/cmocka.git
cd cmocka
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
make
ctest
sudo make install
git clone https://github.com/redis/redis.git ~/redis
strategy:
matrix:
os:
# - "macos-latest"
- "ubuntu-20.04"
- "ubuntu-22.04"
- "ubuntu-24.04"
version:
- "5.0"
- "6.0"
- "6.2"
- "7.2"
- "7.4"
- "unstable"
compiler:
- "gcc"
- "clang"

shell: bash

- name: Test librdb vs. redis-5.0
run: |
pushd ~/redis
git checkout 5.0
make distclean
make -j 4 -C ~/redis
popd
export LIBRDB_REDIS_FOLDER=~/redis/src
make test
working-directory: ${{github.workspace}}
runs-on: ${{ matrix.os }}
env:
DEBIAN_FRONTEND: noninteractive
CC: ${{ matrix.compiler }}

- name: Test librdb vs. redis-unstable
run: |
pushd ~/redis
git checkout unstable
make -j 4 -C ~/redis
make -C ~/redis/tests/modules
popd
export LIBRDB_REDIS_FOLDER=~/redis/src
make all example valgrind
working-directory: ${{github.workspace}}
# TODO: would be nice to connect to a redis server instead of building from source
# services:
# redis:
# image: redis:${{ matrix.version }}
# options: >-
# --health-cmd "redis-cli ping"
# --health-interval 10s
# --health-timeout 5s
# --health-retries 5

build-clang:
runs-on: ubuntu-latest
env:
CC: clang
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: 'recursive'

- name: Install Prerequisites and clone Redis
- name: Install Prerequisites and clone Redis ${{ matrix.version }}
shell: bash
run: |
sudo apt-get update
sudo apt-get install -y cmake libssl-dev git clang
sudo apt-get install -y cmake clang libssl-dev git bc
git clone https://git.cryptomilk.org/projects/cmocka.git
cd cmocka
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
make
sudo make install
git clone https://github.com/redis/redis.git ~/redis
shell: bash

- name: Test librdb vs. redis-6.0
run: |
pushd ~/redis
git checkout 6.0
make distclean
make -j 4 -C ~/redis
popd
export LIBRDB_REDIS_FOLDER=~/redis/src
make test
working-directory: ${{github.workspace}}
- name: Test librdb vs. redis-6.2
- name: Test on redis-${{ matrix.version }}
shell: bash
run: |
# clang is more strict to shared-obj versioning. Satisfy its needs.
pushd ~/redis
git checkout 6.2
git checkout ${{ matrix.version }}
make -j 4 -C ~/redis
make -C ~/redis/tests/modules
if [ $(bc -l <<< "${{ matrix.version }} >= 6.2") -eq 1 ] || [ "${{ matrix.version }}" = "unstable" ]; then
make -C ~/redis/tests/modules
fi
popd
export LIBRDB_REDIS_FOLDER=~/redis/src
make clean debug test
working-directory: ${{github.workspace}}
make clean debug test example
working-directory: ${{github.workspace}}
16 changes: 12 additions & 4 deletions deps/redis/listpack_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@

#ifndef LISTPACK_ALLOC_H
#define LISTPACK_ALLOC_H
//#include "zmalloc.h"

#ifdef __APPLE__
#include <malloc/malloc.h>
#else
#include "malloc.h"
/* We use zmalloc_usable/zrealloc_usable instead of zmalloc/zrealloc
* to ensure the safe invocation of 'zmalloc_usable_size().
* See comment in zmalloc_usable_size(). */
#endif

#define lp_malloc(sz) malloc(sz)
#define lp_realloc(ptr,sz) realloc(ptr,sz)
#define lp_free free

#ifdef __APPLE__
#define lp_malloc_size malloc_size
#else
#define lp_malloc_size malloc_usable_size
#endif

#endif
21 changes: 10 additions & 11 deletions src/cli/Makefile
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
default: all

LIB_DIR = ../../lib
LIB_NAME = librdb.a
LIB_NAME_EXT = librdb-ext.a
LIB_NAME = rdb
LIB_NAME_EXT = $(LIB_NAME)-ext

# Artifacts:
TARGET_APP = rdb-cli
TARGET_APP = rdb-cli
TARGET_LIB_STATIC_EXT = $(LIB_DIR)/lib$(LIB_NAME_EXT).a

#########################################################################################
SOURCES = $(notdir $(basename $(wildcard *.c)))
OBJECTS = $(patsubst %,%.o,$(SOURCES))
TARGETS = $(basename $(SOURCES))

OPTIMIZATION?=-O3
OPTIMIZATION ?= -O3

STD = -std=c99
STACK = -fstack-protector-all -Wstack-protector
WARNS = -Wall -Wextra -pedantic -Werror
CFLAGS = -fPIC $(OPTIMIZATION) $(STD) $(STACK) $(WARNS)
DEBUG = -g3 -DDEBUG=1
LIBS = -L /usr/lib -L $(LIB_DIR) -l:$(LIB_NAME_EXT) -l:$(LIB_NAME)
LIBS = -L /usr/lib -L $(LIB_DIR) -l $(LIB_NAME) -l $(LIB_NAME_EXT)

ifeq ($(BUILD_TLS),yes)
CFLAGS+=-DUSE_OPENSSL=1
LIBS+=-lssl -lcrypto
CFLAGS += -DUSE_OPENSSL=1
LIBS += -lssl -lcrypto
endif

######################################### RULES #######################################
Expand All @@ -32,12 +33,10 @@ all: $(TARGET_APP)
cp $(TARGET_APP) ../../bin/
@echo "Done.";

$(TARGET_APP): %: %.c lib_dependency
$(TARGET_APP): %: %.c $(TARGET_LIB_STATIC_EXT)
$(CC) $(CFLAGS) -o $@ $< $(DEBUG) $(LIBS)

lib_dependency: $(LIB_DIR)/$(LIB_NAME_EXT)

clean:
@rm -rvf $(TARGETS) ./*.o ../../bin/$(TARGET_APP)

.PHONY: all clean lib_dependency
.PHONY: all clean
18 changes: 15 additions & 3 deletions src/ext/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ LIB_NAME_EXT = $(LIB_NAME)-ext
LIB_DIR = ../../lib
LIBRDB_SONAME_EXT = lib$(LIB_NAME_EXT).so.${LIBRDB_VERSION}

TARGET_LIB_STATIC = $(LIB_DIR)/lib$(LIB_NAME).a
# Artifacts:
TARGET_LIB_EXT = $(LIB_DIR)/$(LIBRDB_SONAME_EXT)
TARGET_LIB_STATIC = $(LIB_DIR)/lib$(LIB_NAME).a
TARGET_LIB_STATIC_EXT = $(LIB_DIR)/lib$(LIB_NAME_EXT).a

#########################################################################################
Expand All @@ -32,14 +32,26 @@ LDFLAGS =
LIBS = -L $(LIB_DIR) -l $(LIB_NAME)

ifeq ($(BUILD_TLS),yes)
CFLAGS+=-DUSE_OPENSSL=1
CFLAGS += -DUSE_OPENSSL=1
endif

# Platform-specific overrides
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')

ifeq ($(uname_S),Darwin)
SONAME_FLAG = -install_name
SHARED_FLAG = -dynamiclib
else
SONAME_FLAG = -soname
SHARED_FLAG = -shared
endif

######################################### RULES #######################################
all: $(TARGET_LIB_EXT) $(TARGET_LIB_STATIC_EXT)
@echo "Done.";

$(TARGET_LIB_EXT): $(OBJECTS) $(REDIS_OBJECTS)
$(CC) -o $@ -shared -Wl,-soname,${LIBRDB_SONAME_EXT} ${LDFLAGS} $^ $(LIBS)
$(CC) -o $@ $(SHARED_FLAG) -Wl,$(SONAME_FLAG),${LIBRDB_SONAME_EXT} ${LDFLAGS} $^ $(LIBS)

$(TARGET_LIB_STATIC_EXT): $(OBJECTS) $(REDIS_OBJECTS)
ar rcs $@ $^
Expand Down
19 changes: 15 additions & 4 deletions src/lib/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
default: all

LIB_NAME = rdb
LIB_DIR = ../../lib
LIB_NAME = rdb
LIBRDB_SONAME = lib$(LIB_NAME).so.${LIBRDB_VERSION}

# Artifacts:
Expand All @@ -18,8 +18,8 @@ OBJECTS = $(patsubst %,%.o,$(SOURCES))
REDIS_SOURCES = $(notdir $(basename $(wildcard ../../deps/redis/*.c)))
REDIS_OBJECTS = $(patsubst %,../../deps/redis/%.o,$(REDIS_SOURCES))

OPTIMIZATION?=-O3
LIBRDB_DEBUG?=0
OPTIMIZATION ?= -O3
LIBRDB_DEBUG ?= 0

STD = -std=c99
STACK = -fstack-protector-all -Wstack-protector
Expand All @@ -34,12 +34,23 @@ else
CFLAGS += -DNDEBUG=1
endif

# Platform-specific overrides
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')

ifeq ($(uname_S),Darwin)
SONAME_FLAG = -install_name
SHARED_FLAG = -dynamiclib
else
SONAME_FLAG = -soname
SHARED_FLAG = -shared
endif

######################################### RULES #######################################
all: $(TARGET_LIB) $(TARGET_LIB_STATIC)
@echo "Done.";

$(TARGET_LIB): $(OBJECTS) $(REDIS_OBJECTS)
$(CC) -o $@ -shared -Wl,-soname,${LIBRDB_SONAME} ${LDFLAGS} $^
$(CC) -o $@ $(SHARED_FLAG) -Wl,$(SONAME_FLAG),${LIBRDB_SONAME} ${LDFLAGS} $^

$(TARGET_LIB_STATIC): $(OBJECTS) $(REDIS_OBJECTS)
ar rcs $@ $^
Expand Down
31 changes: 23 additions & 8 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
default: all

LIB_NAME = rdb
LIB_DIR = ../lib
LIB_NAME = rdb
LIB_NAME_EXT = $(LIB_NAME)-ext
LIBHIREDIS=../deps/hiredis/libhiredis.a
LIBHIREDIS = ../deps/hiredis/libhiredis.a

# Artifacts:
TARGET_TEST = test_lib
Expand All @@ -17,16 +17,25 @@ STD = -std=c99
STACK = -fstack-protector-all -Wstack-protector
WARNS = -Wall -Wextra -pedantic -Werror -Wno-unused-function

OPTIMIZATION?=-O0
OPTIMIZATION ?= -O0

CFLAGS = -D_DEFAULT_SOURCE -fPIC $(OPTIMIZATION) $(STD) $(STACK) $(WARNS)
DEBUG = -g3 -DDEBUG=1
LIBS = -l cmocka -L /usr/lib -L $(LIB_DIR) -l $(LIB_NAME) -l $(LIB_NAME_EXT) -l:$(LIBHIREDIS)
LIBS_STATIC = -l cmocka -L /usr/lib -L $(LIB_DIR) -l:lib$(LIB_NAME_EXT).a -l:$(LIBHIREDIS)
LIBS = -l cmocka -L /usr/lib -L $(LIB_DIR) -l $(LIB_NAME) -l $(LIB_NAME_EXT) $(LIBHIREDIS)
LIBS_STATIC = -l cmocka -L /usr/lib -L $(LIB_DIR) -l $(LIB_NAME) -l:lib$(LIB_NAME_EXT).a -l:$(LIBHIREDIS)

ifeq ($(BUILD_TLS),yes)
CFLAGS+=-DUSE_OPENSSL=1
LIBS+=-lssl -lcrypto
CFLAGS += -DUSE_OPENSSL=1
LIBS += -lssl -lcrypto
endif

# Platform-specific overrides
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')

ifeq ($(uname_S),Darwin)
CFLAGS += -I$(shell brew --prefix)/include
LIBS += -L$(shell brew --prefix)/lib
LIBS_STATIC += -L$(shell brew --prefix)/lib
endif

######################################### RULES #######################################
Expand All @@ -37,7 +46,13 @@ $(TARGET_TEST): $(OBJECTS)
$(CC) $(OBJECTS) -o $@ $(DEBUG) $(CFLAGS) $(LIBS)

$(TARGET_TEST_STATIC): $(OBJECTS)
$(CC) $(OBJECTS) -o $@ $(DEBUG) $(CFLAGS) $(LIBS) $(LIBS_STATIC)
ifeq ($(uname_S),Darwin)
@echo "***********************************************************************"
@echo "Skipping $(TARGET_TEST_STATIC) on OSX due to a complex compilation bug."
@echo "***********************************************************************"
else
$(CC) $(OBJECTS) -o $@ $(DEBUG) $(CFLAGS) $(LIBS_STATIC)
endif

-include $(OBJECTS:.o=.d)

Expand Down
2 changes: 1 addition & 1 deletion test/test_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void cleanTmpFolder(void) {
closedir(dir);
}

void setEnvVar (const char *name, const char *val) {
void setEnvVar(const char *name, const char *val) {
setenv(name, val, 1);
}

Expand Down
2 changes: 1 addition & 1 deletion test/test_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void *xrealloc(void *ptr, size_t size);

char *readFile(const char *filename, size_t *len, char *ignoredCh);
void cleanTmpFolder(void);
void setEnvVar (const char *name, const char *val);
void setEnvVar(const char *name, const char *val);
char *substring(char *str, size_t len, char *substr);
void assert_file_payload(const char *filename, char *expData, int expLen, MatchType matchType, int expMatch);

Expand Down

0 comments on commit b09c668

Please sign in to comment.