Skip to content

Commit

Permalink
fix build on macos and iOS
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Feb 11, 2024
1 parent 81a2dcf commit c08be0c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
8 changes: 5 additions & 3 deletions src/nostrdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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!
Expand Down
10 changes: 6 additions & 4 deletions src/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
#elif defined(__linux__) || defined(__FreeBSD__)
#include <sys/random.h>
#elif defined(__OpenBSD__)
#include <unistd.h>
#elif defined(__APPLE__)
#include <Security/SecRandom.h>
#else
#error "Couldn't identify the OS"
#endif
Expand Down Expand Up @@ -66,18 +68,18 @@ 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 ) {
return 0;
} 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 {
Expand Down

0 comments on commit c08be0c

Please sign in to comment.