From c08be0cebcb66668b2b4757ae695f10c01a76a99 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Sun, 11 Feb 2024 11:03:12 -0800 Subject: [PATCH] fix build on macos and iOS Signed-off-by: William Casarin --- Makefile | 12 ++++++++++-- src/nostrdb.c | 8 +++++--- src/random.h | 10 ++++++---- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 35de0f6..ddc5922 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,14 @@ C_BINDINGS_COMMON=$(BINDINGS)/c/flatbuffers_common_builder.h $(BINDINGS)/c/flatb C_BINDINGS=$(C_BINDINGS_COMMON) $(C_BINDINGS_PROFILE) $(C_BINDINGS_META) BIN=ndb +# Detect operating system +UNAME_S := $(shell uname -s) + +# macOS-specific flags +ifeq ($(UNAME_S),Darwin) + LDFLAGS += -framework Security +endif + CHECKDATA=testdata/db/v0/data.mdb all: $(BIN) lib bench @@ -31,7 +39,7 @@ libnostrdb.a: $(OBJS) lib: libnostrdb.a ndb: ndb.c $(DEPS) - $(CC) $(CFLAGS) ndb.c $(LDS) -o $@ + $(CC) $(CFLAGS) ndb.c $(LDS) $(LDFLAGS) -o $@ bindings: bindings-swift bindings-rust bindings-c @@ -179,6 +187,6 @@ testdata/db/.dir: touch testdata/db/.dir test: test.c $(DEPS) testdata/db/.dir - $(CC) $(CFLAGS) test.c $(LDS) -o $@ + $(CC) $(CFLAGS) test.c $(LDS) $(LDFLAGS) -o $@ .PHONY: tags clean fake diff --git a/src/nostrdb.c b/src/nostrdb.c index dc31136..74c6063 100644 --- a/src/nostrdb.c +++ b/src/nostrdb.c @@ -2612,7 +2612,7 @@ static int ndb_query_plan_execute_ids(struct ndb_txn *txn, struct ndb_query_result res; struct ndb_tsid tsid, *ptsid; uint64_t note_id, until, *pint; - uint64_t note_size; + size_t note_size; unsigned char *id; matched = 0; @@ -2717,7 +2717,8 @@ static int ndb_query_plan_execute_tags(struct ndb_txn *txn, MDB_dbi db; MDB_val k, v; int len, taglen, rc, i; - uint64_t *pint, until, note_id, note_size; + uint64_t *pint, until, note_id; + size_t note_size; unsigned char key_buffer[255]; struct ndb_note *note; struct ndb_filter_elements *tags; @@ -2800,7 +2801,8 @@ static int ndb_query_plan_execute_kinds(struct ndb_txn *txn, struct ndb_u64_tsid tsid, *ptsid; struct ndb_filter_elements *kinds; struct ndb_query_result res; - uint64_t kind, note_id, note_size, until, *pint; + uint64_t kind, note_id, until, *pint; + size_t note_size; int i, rc; // we should have kinds in a kinds filter! diff --git a/src/random.h b/src/random.h index 14e2533..736ed88 100644 --- a/src/random.h +++ b/src/random.h @@ -24,10 +24,12 @@ #include #include #include -#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) +#elif defined(__linux__) || defined(__FreeBSD__) #include #elif defined(__OpenBSD__) #include +#elif defined(__APPLE__) +#include #else #error "Couldn't identify the OS" #endif @@ -66,7 +68,7 @@ static int fill_random(unsigned char* data, size_t size) { } close(fd); return 1; -#elif defined(__linux__) || defined(__FreeBSD__) +#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) /* If `getrandom(2)` is not available you should fallback to /dev/urandom */ ssize_t res = getrandom(data, size, 0); if (res < 0 || (size_t)res != size ) { @@ -74,10 +76,10 @@ static int fill_random(unsigned char* data, size_t size) { } else { return 1; } -#elif defined(__APPLE__) || defined(__OpenBSD__) +#elif defined(__APPLE__) /* If `getentropy(2)` is not available you should fallback to either * `SecRandomCopyBytes` or /dev/urandom */ - int res = getentropy(data, size); + int res = SecRandomCopyBytes(kSecRandomDefault, size, data); if (res == 0) { return 1; } else {