Skip to content

Commit 82b22a8

Browse files
committed
feat: add osx support
1 parent 664a7be commit 82b22a8

File tree

8 files changed

+155
-99
lines changed

8 files changed

+155
-99
lines changed

.github/workflows/ci.yml

+84-67
Original file line numberDiff line numberDiff line change
@@ -7,97 +7,114 @@ on:
77
- cron: '0 0 * * 0' # Run every Sunday at midnight UTC
88

99
jobs:
10-
build:
11-
runs-on: ubuntu-latest
10+
build-linux:
11+
strategy:
12+
matrix:
13+
version:
14+
- "5.0"
15+
- "6.0"
16+
- "6.2"
17+
- "7.2"
18+
- "7.4"
19+
- "unstable"
20+
compiler:
21+
- "gcc"
22+
- "clang"
23+
24+
runs-on: "ubuntu-latest"
25+
26+
env:
27+
DEBIAN_FRONTEND: noninteractive
28+
CC: ${{ matrix.compiler }}
29+
30+
# TODO: would be nice to connect to a redis server instead of building from source
31+
# services:
32+
# redis:
33+
# image: redis:${{ matrix.version }}
34+
# options: >-
35+
# --health-cmd "redis-cli ping"
36+
# --health-interval 10s
37+
# --health-timeout 5s
38+
# --health-retries 5
39+
1240
steps:
13-
- name: Checkout
14-
uses: actions/checkout@v3
41+
- name: Checkout librdb
42+
uses: actions/checkout@v4
1543
with:
16-
submodules: 'recursive'
44+
submodules: "recursive"
45+
46+
- name: Clone Redis (${{ matrix.version }})
47+
uses: actions/checkout@v4
48+
with:
49+
repository: redis/redis
50+
ref: ${{ matrix.version }}
51+
path: redis
1752

18-
- name: Install Prerequisites and clone Redis
53+
- name: Install prerequisites
1954
run: |
2055
sudo apt-get update
21-
sudo apt-get install -y cmake libssl-dev valgrind git
56+
sudo apt-get install -y cmake clang libssl-dev git bc
57+
2258
git clone https://git.cryptomilk.org/projects/cmocka.git
2359
cd cmocka
2460
mkdir build
2561
cd build
2662
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
2763
make
28-
ctest
2964
sudo make install
30-
git clone https://github.com/redis/redis.git ~/redis
3165
66+
- name: Test on redis-${{ matrix.version }}
3267
shell: bash
33-
34-
- name: Test librdb vs. redis-5.0
3568
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}}
69+
make -j 4 -C ${{ github.workspace }}/redis
4470
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
71+
if [ $(bc -l <<< "${{ matrix.version }} >= 6.2") -eq 1 ] || [ "${{ matrix.version }}" = "unstable" ]; then
72+
make -C ${{ github.workspace }}/redis/tests/modules
73+
fi
74+
75+
LIBRDB_REDIS_FOLDER="${{ github.workspace }}/redis/src" make clean debug test example
5476
working-directory: ${{github.workspace}}
5577

56-
build-clang:
57-
runs-on: ubuntu-latest
58-
env:
59-
CC: clang
78+
build-osx:
79+
strategy:
80+
matrix:
81+
version:
82+
- "6.2"
83+
- "7.2"
84+
- "7.4"
85+
- "unstable"
86+
87+
runs-on: "macos-latest"
88+
6089
steps:
61-
- name: Checkout
62-
uses: actions/checkout@v3
90+
- name: Checkout librdb
91+
uses: actions/checkout@v4
6392
with:
6493
submodules: 'recursive'
6594

66-
- name: Install Prerequisites and clone Redis
67-
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
95+
- name: Clone Redis (${{ matrix.version }})
96+
uses: actions/checkout@v4
97+
with:
98+
repository: redis/redis
99+
ref: ${{ matrix.version }}
100+
path: redis
80101

81-
- name: Test librdb vs. redis-6.0
102+
- name: Install prerequisites
82103
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}}
104+
brew install cmocka bc llvm grep
91105
92-
- name: Test librdb vs. redis-6.2
106+
- name: Test on redis-${{ matrix.version }}
107+
shell: bash
93108
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}}
109+
export PATH="$(brew --prefix)/opt/grep/libexec/gnubin:${PATH}"
110+
111+
make -j 4 -C ${{ github.workspace }}/redis
103112
113+
if [ $(bc -l <<< "${{ matrix.version }} >= 6.2") -eq 1 ] || [ "${{ matrix.version }}" = "unstable" ]; then
114+
make -C ${{ github.workspace }}/redis/tests/modules
115+
fi
116+
117+
# FIXME NOT WORKING
118+
# LIBRDB_REDIS_FOLDER="${{ github.workspace }}/redis/src"
119+
make clean debug test example
120+
working-directory: ${{github.workspace}}

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-11
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) -l $(LIB_NAME_EXT)
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,12 +33,10 @@ all: $(TARGET_APP)
3233
cp $(TARGET_APP) ../../bin/
3334
@echo "Done.";
3435

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

38-
lib_dependency: $(LIB_DIR)/$(LIB_NAME_EXT)
39-
4039
clean:
4140
@rm -rvf $(TARGETS) ./*.o ../../bin/$(TARGET_APP)
4241

43-
.PHONY: all clean lib_dependency
42+
.PHONY: all clean

src/ext/Makefile

+15-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ LIB_NAME_EXT = $(LIB_NAME)-ext
55
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)
10+
TARGET_LIB_STATIC = $(LIB_DIR)/lib$(LIB_NAME).a
1111
TARGET_LIB_STATIC_EXT = $(LIB_DIR)/lib$(LIB_NAME_EXT).a
1212

1313
#########################################################################################
@@ -32,14 +32,26 @@ LDFLAGS =
3232
LIBS = -L $(LIB_DIR) -l $(LIB_NAME)
3333

3434
ifeq ($(BUILD_TLS),yes)
35-
CFLAGS+=-DUSE_OPENSSL=1
35+
CFLAGS += -DUSE_OPENSSL=1
36+
endif
37+
38+
# Platform-specific overrides
39+
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
40+
41+
ifeq ($(uname_S),Darwin)
42+
SONAME_FLAG = -install_name
43+
SHARED_FLAG = -dynamiclib
44+
else
45+
SONAME_FLAG = -soname
46+
SHARED_FLAG = -shared
3647
endif
48+
3749
######################################### RULES #######################################
3850
all: $(TARGET_LIB_EXT) $(TARGET_LIB_STATIC_EXT)
3951
@echo "Done.";
4052

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

4456
$(TARGET_LIB_STATIC_EXT): $(OBJECTS) $(REDIS_OBJECTS)
4557
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 $@ $^

test/Makefile

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

3-
LIB_NAME = rdb
43
LIB_DIR = ../lib
4+
LIB_NAME = rdb
55
LIB_NAME_EXT = $(LIB_NAME)-ext
6-
LIBHIREDIS=../deps/hiredis/libhiredis.a
6+
LIBHIREDIS = ../deps/hiredis/libhiredis.a
77

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

20-
OPTIMIZATION?=-O0
20+
OPTIMIZATION ?= -O0
2121

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

2727
ifeq ($(BUILD_TLS),yes)
28-
CFLAGS+=-DUSE_OPENSSL=1
29-
LIBS+=-lssl -lcrypto
28+
CFLAGS += -DUSE_OPENSSL=1
29+
LIBS += -lssl -lcrypto
30+
endif
31+
32+
# Platform-specific overrides
33+
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
34+
35+
ifeq ($(uname_S),Darwin)
36+
CFLAGS += -I$(shell brew --prefix)/include
37+
LIBS += -L$(shell brew --prefix)/lib
38+
LIBS_STATIC += -L$(shell brew --prefix)/lib
3039
endif
3140

3241
######################################### RULES #######################################
@@ -37,7 +46,7 @@ $(TARGET_TEST): $(OBJECTS)
3746
$(CC) $(OBJECTS) -o $@ $(DEBUG) $(CFLAGS) $(LIBS)
3847

3948
$(TARGET_TEST_STATIC): $(OBJECTS)
40-
$(CC) $(OBJECTS) -o $@ $(DEBUG) $(CFLAGS) $(LIBS) $(LIBS_STATIC)
49+
$(CC) $(OBJECTS) -o $@ $(DEBUG) $(CFLAGS) $(LIBS_STATIC)
4150

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

0 commit comments

Comments
 (0)