Skip to content

Commit 85c5539

Browse files
authored
feat: add osx support (#65)
1 parent daa7e33 commit 85c5539

File tree

9 files changed

+169
-131
lines changed

9 files changed

+169
-131
lines changed

.github/workflows/ci.yml

+77-79
Original file line numberDiff line numberDiff line change
@@ -8,96 +8,94 @@ on:
88

99
jobs:
1010
build:
11-
runs-on: ubuntu-latest
12-
steps:
13-
- name: Checkout
14-
uses: actions/checkout@v3
15-
with:
16-
submodules: 'recursive'
11+
strategy:
12+
matrix:
13+
os:
14+
- "ubuntu-latest"
15+
- "macos-latest"
16+
version:
17+
- "5.0"
18+
- "6.0"
19+
- "6.2"
20+
- "7.0"
21+
- "7.2"
22+
- "7.4"
23+
- "unstable"
24+
compiler:
25+
- "gcc"
26+
- "clang"
1727

18-
- name: Install Prerequisites and clone Redis
19-
run: |
20-
sudo apt-get update
21-
sudo apt-get install -y cmake libssl-dev valgrind git
22-
git clone https://git.cryptomilk.org/projects/cmocka.git
23-
cd cmocka
24-
mkdir build
25-
cd build
26-
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
27-
make
28-
ctest
29-
sudo make install
30-
git clone https://github.com/redis/redis.git ~/redis
28+
exclude:
29+
- os: macos-latest
30+
compiler: gcc
31+
- os: macos-latest
32+
version: "5.0"
33+
- os: macos-latest
34+
version: "6.0"
35+
- os: macos-latest
36+
version: "7.0"
3137

32-
shell: bash
38+
runs-on: ${{ matrix.os }}
3339

34-
- name: Test librdb vs. redis-5.0
35-
run: |
36-
pushd ~/redis
37-
git checkout 5.0
38-
make distclean
39-
make -j 4 -C ~/redis
40-
popd
41-
export LIBRDB_REDIS_FOLDER=~/redis/src
42-
make test
43-
working-directory: ${{github.workspace}}
40+
env:
41+
DEBIAN_FRONTEND: noninteractive
42+
CC: ${{ matrix.compiler }}
4443

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

56-
build-clang:
57-
runs-on: ubuntu-latest
58-
env:
59-
CC: clang
6054
steps:
61-
- name: Checkout
62-
uses: actions/checkout@v3
55+
- name: Checkout librdb
56+
uses: actions/checkout@v4
57+
with:
58+
submodules: "recursive"
59+
60+
- name: Clone Redis (${{ matrix.version }})
61+
uses: actions/checkout@v4
6362
with:
64-
submodules: 'recursive'
63+
repository: redis/redis
64+
ref: ${{ matrix.version }}
65+
path: redis
6566

66-
- name: Install Prerequisites and clone Redis
67+
- name: Install prerequisites
6768
run: |
68-
sudo apt-get update
69-
sudo apt-get install -y cmake libssl-dev git clang
70-
git clone https://git.cryptomilk.org/projects/cmocka.git
71-
cd cmocka
72-
mkdir build
73-
cd build
74-
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
75-
make
76-
sudo make install
77-
78-
git clone https://github.com/redis/redis.git ~/redis
79-
shell: bash
69+
if [ "${RUNNER_OS}" = "Linux" ]; then
70+
sudo apt-get update
71+
sudo apt-get install -y cmake clang libssl-dev valgrind git bc
72+
73+
# Build and install cmocka
74+
git clone https://git.cryptomilk.org/projects/cmocka.git
75+
cd cmocka
76+
mkdir build
77+
cd build
78+
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
79+
make
80+
sudo make install
81+
elif [ "${RUNNER_OS}" = "macOS" ]; then
82+
brew install cmocka bc llvm grep
83+
echo "PATH=$(brew --prefix)/opt/grep/libexec/gnubin:${PATH}" >> "${GITHUB_ENV}"
84+
fi
8085

81-
- name: Test librdb vs. redis-6.0
86+
- name: Build Redis ${{ matrix.version }}
8287
run: |
83-
pushd ~/redis
84-
git checkout 6.0
85-
make distclean
86-
make -j 4 -C ~/redis
87-
popd
88-
export LIBRDB_REDIS_FOLDER=~/redis/src
89-
make test
90-
working-directory: ${{github.workspace}}
88+
make -j -C redis
9189
92-
- name: Test librdb vs. redis-6.2
90+
if [ $(bc -l <<< "${{ matrix.version }} >= 6.2") -eq 1 ] || [ "${{ matrix.version }}" = "unstable" ]; then
91+
make -j -C redis/tests/modules
92+
fi
93+
94+
- name: Run tests with shared lib
9395
run: |
94-
# clang is more strict to shared-obj versioning. Satisfy its needs.
95-
pushd ~/redis
96-
git checkout 6.2
97-
make -j 4 -C ~/redis
98-
make -C ~/redis/tests/modules
99-
popd
100-
export LIBRDB_REDIS_FOLDER=~/redis/src
101-
make clean debug test
102-
working-directory: ${{github.workspace}}
96+
LIBRDB_REDIS_FOLDER="$(pwd)/redis/src" make clean debug test
10397
98+
- name: Run tests with static lib and valgrind
99+
if: runner.os == 'Linux'
100+
run: |
101+
LIBRDB_REDIS_FOLDER="$(pwd)/redis/src" make clean all valgrind

Makefile

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

2-
PREFIX?=/usr/local
2+
PREFIX? = /usr/local
33

44
DESTDIR?=
55
INSTALL = /usr/bin/install -c
6-
BINDIR=$(DESTDIR)$(PREFIX)/bin
7-
LIBDIR=$(DESTDIR)$(PREFIX)/lib
8-
INCDIR=$(DESTDIR)$(PREFIX)/include/librdb/
9-
LIBRDB_INSTALL_SHARED:=yes
10-
LIBRDB_INSTALL_STATIC:=yes
6+
BINDIR = $(DESTDIR)$(PREFIX)/bin
7+
LIBDIR = $(DESTDIR)$(PREFIX)/lib
8+
INCDIR = $(DESTDIR)$(PREFIX)/include/librdb/
9+
LIBRDB_INSTALL_SHARED := yes
10+
LIBRDB_INSTALL_STATIC := yes
1111

1212
UNAME := $(shell uname)
1313

@@ -23,19 +23,19 @@ export LIBRDB_VERSION
2323
# ------------------------- ALL --------------------------------------
2424

2525
all: ./deps/hiredis/hiredis.h
26-
$(MAKE) -C deps all
27-
$(MAKE) -C src/lib all
28-
$(MAKE) -C src/ext all
29-
$(MAKE) -C src/cli all
30-
$(MAKE) -C examples all
26+
$(MAKE) -C deps all
27+
$(MAKE) -C src/lib all
28+
$(MAKE) -C src/ext all
29+
$(MAKE) -C src/cli all
30+
$(MAKE) -C examples all
3131

3232
clean:
33-
$(MAKE) -C deps clean
34-
$(MAKE) -C src/lib clean
35-
$(MAKE) -C src/ext clean
36-
$(MAKE) -C src/cli clean
37-
$(MAKE) -C examples clean
38-
$(MAKE) -C test clean
33+
$(MAKE) -C deps clean
34+
$(MAKE) -C src/lib clean
35+
$(MAKE) -C src/ext clean
36+
$(MAKE) -C src/cli clean
37+
$(MAKE) -C examples clean
38+
$(MAKE) -C test clean
3939
rm -f librdb.pc
4040
rm -f librdb-ext.pc
4141

deps/redis/listpack_malloc.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,21 @@
3838

3939
#ifndef LISTPACK_ALLOC_H
4040
#define LISTPACK_ALLOC_H
41-
//#include "zmalloc.h"
41+
42+
#ifdef __APPLE__
43+
#include <malloc/malloc.h>
44+
#else
4245
#include "malloc.h"
43-
/* We use zmalloc_usable/zrealloc_usable instead of zmalloc/zrealloc
44-
* to ensure the safe invocation of 'zmalloc_usable_size().
45-
* See comment in zmalloc_usable_size(). */
46+
#endif
47+
4648
#define lp_malloc(sz) malloc(sz)
4749
#define lp_realloc(ptr,sz) realloc(ptr,sz)
4850
#define lp_free free
51+
52+
#ifdef __APPLE__
53+
#define lp_malloc_size malloc_size
54+
#else
4955
#define lp_malloc_size malloc_usable_size
5056
#endif
57+
58+
#endif

src/cli/Makefile

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
default: all
22

33
LIB_DIR = ../../lib
4-
LIB_NAME = librdb.a
5-
LIB_NAME_EXT = librdb-ext.a
4+
LIB_NAME = rdb
5+
LIB_NAME_EXT = $(LIB_NAME)-ext
66

77
# Artifacts:
8-
TARGET_APP = rdb-cli
8+
TARGET_APP = rdb-cli
9+
TARGET_LIB_STATIC_EXT = $(LIB_DIR)/lib$(LIB_NAME_EXT).a
910

1011
#########################################################################################
1112
SOURCES = $(notdir $(basename $(wildcard *.c)))
1213
OBJECTS = $(patsubst %,%.o,$(SOURCES))
1314
TARGETS = $(basename $(SOURCES))
1415

15-
OPTIMIZATION?=-O3
16+
OPTIMIZATION ?= -O3
1617

1718
STD = -std=c99
1819
STACK = -fstack-protector-all -Wstack-protector
1920
WARNS = -Wall -Wextra -pedantic -Werror
2021
CFLAGS = -fPIC $(OPTIMIZATION) $(STD) $(STACK) $(WARNS)
2122
DEBUG = -g3 -DDEBUG=1
22-
LIBS = -L /usr/lib -L $(LIB_DIR) -l:$(LIB_NAME_EXT) -l:$(LIB_NAME)
23+
LIBS = -L /usr/lib -L $(LIB_DIR) -l $(LIB_NAME_EXT) -l $(LIB_NAME)
2324

2425
ifeq ($(BUILD_TLS),yes)
25-
CFLAGS+=-DUSE_OPENSSL=1
26-
LIBS+=-lssl -lcrypto
26+
CFLAGS += -DUSE_OPENSSL=1
27+
LIBS += -lssl -lcrypto
2728
endif
2829

2930
######################################### RULES #######################################
@@ -32,10 +33,10 @@ all: $(TARGET_APP)
3233
cp $(TARGET_APP) ../../bin/
3334
@echo "Done.";
3435

35-
$(TARGET_APP): %: %.c lib_dependency
36+
$(TARGET_APP): %: %.c lib_dependency
3637
$(CC) $(CFLAGS) -o $@ $< $(DEBUG) $(LIBS)
3738

38-
lib_dependency: $(LIB_DIR)/$(LIB_NAME_EXT)
39+
lib_dependency: $(LIB_DIR)/lib$(LIB_NAME_EXT).a
3940

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

src/ext/Makefile

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
default: all
22

3+
LIB_DIR = ../../lib
34
LIB_NAME = rdb
45
LIB_NAME_EXT = $(LIB_NAME)-ext
5-
LIB_DIR = ../../lib
66
LIBRDB_SONAME_EXT = lib$(LIB_NAME_EXT).so.${LIBRDB_VERSION}
77

8-
TARGET_LIB_STATIC = $(LIB_DIR)/lib$(LIB_NAME).a
98
# Artifacts:
109
TARGET_LIB_EXT = $(LIB_DIR)/$(LIBRDB_SONAME_EXT)
1110
TARGET_LIB_STATIC_EXT = $(LIB_DIR)/lib$(LIB_NAME_EXT).a
@@ -20,8 +19,8 @@ OBJECTS = $(patsubst %,%.o,$(SOURCES))
2019
REDIS_SOURCES = $(notdir $(basename $(wildcard ../../deps/redis/*.c)))
2120
REDIS_OBJECTS = $(patsubst %,../../deps/redis/%.o,$(REDIS_SOURCES))
2221

23-
OPTIMIZATION?=-O3
24-
LIBRDB_DEBUG?=0
22+
OPTIMIZATION? = -O3
23+
LIBRDB_DEBUG? = 0
2524

2625
STD = -std=c99
2726
STACK = -fstack-protector-all -Wstack-protector
@@ -32,14 +31,26 @@ LDFLAGS =
3231
LIBS = -L $(LIB_DIR) -l $(LIB_NAME)
3332

3433
ifeq ($(BUILD_TLS),yes)
35-
CFLAGS+=-DUSE_OPENSSL=1
34+
CFLAGS += -DUSE_OPENSSL=1
35+
endif
36+
37+
# Platform-specific overrides
38+
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
39+
40+
ifeq ($(uname_S),Darwin)
41+
SONAME_FLAG = -install_name
42+
SHARED_FLAG = -dynamiclib
43+
else
44+
SONAME_FLAG = -soname
45+
SHARED_FLAG = -shared
3646
endif
47+
3748
######################################### RULES #######################################
3849
all: $(TARGET_LIB_EXT) $(TARGET_LIB_STATIC_EXT)
3950
@echo "Done.";
4051

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

4455
$(TARGET_LIB_STATIC_EXT): $(OBJECTS) $(REDIS_OBJECTS)
4556
ar rcs $@ $^

src/lib/Makefile

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
default: all
22

3-
LIB_NAME = rdb
43
LIB_DIR = ../../lib
4+
LIB_NAME = rdb
55
LIBRDB_SONAME = lib$(LIB_NAME).so.${LIBRDB_VERSION}
66

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

21-
OPTIMIZATION?=-O3
22-
LIBRDB_DEBUG?=0
21+
OPTIMIZATION ?= -O3
22+
LIBRDB_DEBUG ?= 0
2323

2424
STD = -std=c99
2525
STACK = -fstack-protector-all -Wstack-protector
@@ -34,12 +34,23 @@ else
3434
CFLAGS += -DNDEBUG=1
3535
endif
3636

37+
# Platform-specific overrides
38+
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
39+
40+
ifeq ($(uname_S),Darwin)
41+
SONAME_FLAG = -install_name
42+
SHARED_FLAG = -dynamiclib
43+
else
44+
SONAME_FLAG = -soname
45+
SHARED_FLAG = -shared
46+
endif
47+
3748
######################################### RULES #######################################
3849
all: $(TARGET_LIB) $(TARGET_LIB_STATIC)
3950
@echo "Done.";
4051

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

4455
$(TARGET_LIB_STATIC): $(OBJECTS) $(REDIS_OBJECTS)
4556
ar rcs $@ $^

0 commit comments

Comments
 (0)