Skip to content

Commit

Permalink
feat: add osx support (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippeboyd authored Feb 25, 2025
1 parent daa7e33 commit 85c5539
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 131 deletions.
156 changes: 77 additions & 79 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,96 +8,94 @@ on:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: 'recursive'
strategy:
matrix:
os:
- "ubuntu-latest"
- "macos-latest"
version:
- "5.0"
- "6.0"
- "6.2"
- "7.0"
- "7.2"
- "7.4"
- "unstable"
compiler:
- "gcc"
- "clang"

- 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
exclude:
- os: macos-latest
compiler: gcc
- os: macos-latest
version: "5.0"
- os: macos-latest
version: "6.0"
- os: macos-latest
version: "7.0"

shell: bash
runs-on: ${{ matrix.os }}

- 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}}
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
- name: Checkout librdb
uses: actions/checkout@v4
with:
submodules: "recursive"

- name: Clone Redis (${{ matrix.version }})
uses: actions/checkout@v4
with:
submodules: 'recursive'
repository: redis/redis
ref: ${{ matrix.version }}
path: redis

- name: Install Prerequisites and clone Redis
- name: Install prerequisites
run: |
sudo apt-get update
sudo apt-get install -y cmake libssl-dev git clang
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
if [ "${RUNNER_OS}" = "Linux" ]; then
sudo apt-get update
sudo apt-get install -y cmake clang libssl-dev valgrind git bc
# Build and install cmocka
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
elif [ "${RUNNER_OS}" = "macOS" ]; then
brew install cmocka bc llvm grep
echo "PATH=$(brew --prefix)/opt/grep/libexec/gnubin:${PATH}" >> "${GITHUB_ENV}"
fi

- name: Test librdb vs. redis-6.0
- name: Build Redis ${{ matrix.version }}
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}}
make -j -C redis
- name: Test librdb vs. redis-6.2
if [ $(bc -l <<< "${{ matrix.version }} >= 6.2") -eq 1 ] || [ "${{ matrix.version }}" = "unstable" ]; then
make -j -C redis/tests/modules
fi
- name: Run tests with shared lib
run: |
# clang is more strict to shared-obj versioning. Satisfy its needs.
pushd ~/redis
git checkout 6.2
make -j 4 -C ~/redis
make -C ~/redis/tests/modules
popd
export LIBRDB_REDIS_FOLDER=~/redis/src
make clean debug test
working-directory: ${{github.workspace}}
LIBRDB_REDIS_FOLDER="$(pwd)/redis/src" make clean debug test
- name: Run tests with static lib and valgrind
if: runner.os == 'Linux'
run: |
LIBRDB_REDIS_FOLDER="$(pwd)/redis/src" make clean all valgrind
34 changes: 17 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

PREFIX?=/usr/local
PREFIX? = /usr/local

DESTDIR?=
INSTALL = /usr/bin/install -c
BINDIR=$(DESTDIR)$(PREFIX)/bin
LIBDIR=$(DESTDIR)$(PREFIX)/lib
INCDIR=$(DESTDIR)$(PREFIX)/include/librdb/
LIBRDB_INSTALL_SHARED:=yes
LIBRDB_INSTALL_STATIC:=yes
BINDIR = $(DESTDIR)$(PREFIX)/bin
LIBDIR = $(DESTDIR)$(PREFIX)/lib
INCDIR = $(DESTDIR)$(PREFIX)/include/librdb/
LIBRDB_INSTALL_SHARED := yes
LIBRDB_INSTALL_STATIC := yes

UNAME := $(shell uname)

Expand All @@ -23,19 +23,19 @@ export LIBRDB_VERSION
# ------------------------- ALL --------------------------------------

all: ./deps/hiredis/hiredis.h
$(MAKE) -C deps all
$(MAKE) -C src/lib all
$(MAKE) -C src/ext all
$(MAKE) -C src/cli all
$(MAKE) -C examples all
$(MAKE) -C deps all
$(MAKE) -C src/lib all
$(MAKE) -C src/ext all
$(MAKE) -C src/cli all
$(MAKE) -C examples all

clean:
$(MAKE) -C deps clean
$(MAKE) -C src/lib clean
$(MAKE) -C src/ext clean
$(MAKE) -C src/cli clean
$(MAKE) -C examples clean
$(MAKE) -C test clean
$(MAKE) -C deps clean
$(MAKE) -C src/lib clean
$(MAKE) -C src/ext clean
$(MAKE) -C src/cli clean
$(MAKE) -C examples clean
$(MAKE) -C test clean
rm -f librdb.pc
rm -f librdb-ext.pc

Expand Down
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
19 changes: 10 additions & 9 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_EXT) -l $(LIB_NAME)

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

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

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

lib_dependency: $(LIB_DIR)/$(LIB_NAME_EXT)
lib_dependency: $(LIB_DIR)/lib$(LIB_NAME_EXT).a

clean:
@rm -rvf $(TARGETS) ./*.o ../../bin/$(TARGET_APP)
Expand Down
23 changes: 17 additions & 6 deletions src/ext/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
default: all

LIB_DIR = ../../lib
LIB_NAME = rdb
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_EXT = $(LIB_DIR)/lib$(LIB_NAME_EXT).a
Expand All @@ -20,8 +19,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 @@ -32,14 +31,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
Loading

0 comments on commit 85c5539

Please sign in to comment.