-
Notifications
You must be signed in to change notification settings - Fork 18
/
Makefile
209 lines (154 loc) · 7.16 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
CFLAGS = -Wall -Wno-misleading-indentation -Wno-unused-function -Werror -O2 -g -Isrc -Ideps/secp256k1/include -Ideps/lmdb -Ideps/flatcc/include -Isrc/bolt11/ -Iccan/ -DCCAN_TAL_NEVER_RETURN_NULL=1
BOLT11_HDRS := src/bolt11/amount.h src/bolt11/bech32.h src/bolt11/bech32_util.h src/bolt11/bolt11.h src/bolt11/debug.h src/bolt11/error.h src/bolt11/hash_u5.h src/bolt11/node_id.h src/bolt11/overflows.h
CCAN_SRCS := ccan/ccan/utf8/utf8.c ccan/ccan/tal/tal.c ccan/ccan/tal/str/str.c ccan/ccan/list/list.c ccan/ccan/mem/mem.c ccan/ccan/crypto/sha256/sha256.c ccan/ccan/take/take.c
CCAN_HDRS := ccan/ccan/utf8/utf8.h ccan/ccan/container_of/container_of.h ccan/ccan/check_type/check_type.h ccan/ccan/str/str.h ccan/ccan/tal/str/str.h ccan/ccan/tal/tal.h ccan/ccan/list/list.h ccan/ccan/structeq/structeq.h ccan/ccan/typesafe_cb/typesafe_cb.h ccan/ccan/short_types/short_types.h ccan/ccan/mem/mem.h ccan/ccan/likely/likely.h ccan/ccan/alignof/alignof.h ccan/ccan/crypto/sha256/sha256.h ccan/ccan/array_size/array_size.h ccan/ccan/endian/endian.h ccan/ccan/take/take.h ccan/ccan/build_assert/build_assert.h ccan/ccan/cppmagic/cppmagic.h
HEADERS = deps/lmdb/lmdb.h deps/secp256k1/include/secp256k1.h src/nostrdb.h src/cursor.h src/hex.h src/jsmn.h src/config.h src/random.h src/memchr.h src/cpu.h src/nostr_bech32.h src/block.h src/str_block.h $(C_BINDINGS) $(CCAN_HDRS) $(BOLT11_HDRS)
FLATCC_SRCS=deps/flatcc/src/runtime/json_parser.c deps/flatcc/src/runtime/verifier.c deps/flatcc/src/runtime/builder.c deps/flatcc/src/runtime/emitter.c deps/flatcc/src/runtime/refmap.c
BOLT11_SRCS = src/bolt11/bolt11.c src/bolt11/bech32.c src/bolt11/amount.c src/bolt11/hash_u5.c
SRCS = src/nostrdb.c src/invoice.c src/nostr_bech32.c src/content_parser.c src/block.c $(BOLT11_SRCS) $(FLATCC_SRCS) $(CCAN_SRCS)
LDS = $(OBJS) $(ARS)
OBJS = $(SRCS:.c=.o)
DEPS = $(OBJS) $(HEADERS) $(ARS)
ARS = deps/lmdb/liblmdb.a deps/secp256k1/.libs/libsecp256k1.a
LMDB_VER=0.9.31
FLATCC_VER=05dc16dc2b0316e61063bb1fc75426647badce48
PREFIX ?= /usr/local
SUBMODULES = deps/secp256k1
BINDINGS=src/bindings
C_BINDINGS_PROFILE=$(BINDINGS)/c/profile_builder.h $(BINDINGS)/c/profile_reader.h $(BINDINGS)/c/profile_verifier.h $(BINDINGS)/c/profile_json_parser.h
C_BINDINGS_META=$(BINDINGS)/c/meta_builder.h $(BINDINGS)/c/meta_reader.h $(BINDINGS)/c/meta_verifier.h $(BINDINGS)/c/meta_json_parser.h
C_BINDINGS_COMMON=$(BINDINGS)/c/flatbuffers_common_builder.h $(BINDINGS)/c/flatbuffers_common_reader.h
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
$(OBJS): $(HEADERS)
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
libnostrdb.a: $(OBJS)
ar rcs $@ $(OBJS)
lib: libnostrdb.a
ndb: ndb.c $(DEPS)
$(CC) $(CFLAGS) ndb.c $(LDS) $(LDFLAGS) -o $@
bindings: bindings-swift bindings-rust bindings-c
check: test
rm -rf testdata/db/*.mdb
./test
rm -rf testdata/db/*.mdb
clean:
rm -rf test bench bench-ingest bench-ingest-many $(OBJS)
distclean: clean
rm -rf deps
tags:
find src -name '*.c' -or -name '*.h' | xargs ctags
configurator: src/configurator.c
$(CC) $< -o $@
src/config.h: configurator
./configurator > $@
bindings-c: $(C_BINDINGS)
src/bindings/%/.dir:
mkdir -p $(shell dirname $@)
touch $@
src/bindings/c/%_builder.h: schemas/%.fbs $(BINDINGS)/c/.dir
flatcc --builder $< -o $(BINDINGS)/c
src/bindings/c/%_verifier.h bindings/c/%_reader.h: schemas/%.fbs $(BINDINGS)/c/.dir
flatcc --verifier -o $(BINDINGS)/c $<
src/bindings/c/flatbuffers_common_reader.h: $(BINDINGS)/c/.dir
flatcc --common_reader -o $(BINDINGS)/c
src/bindings/c/flatbuffers_common_builder.h: $(BINDINGS)/c/.dir
flatcc --common_builder -o $(BINDINGS)/c
src/bindings/c/%_json_parser.h: schemas/%.fbs $(BINDINGS)/c/.dir
flatcc --json-parser $< -o $(BINDINGS)/c
bindings-rust: $(BINDINGS)/rust/ndb_profile.rs $(BINDINGS)/rust/ndb_meta.rs
$(BINDINGS)/rust/ndb_profile.rs: schemas/profile.fbs $(BINDINGS)/rust
flatc --gen-json-emit --rust $<
@mv profile_generated.rs $@
$(BINDINGS)/rust/ndb_meta.rs: schemas/meta.fbs $(BINDINGS)/swift
flatc --rust $<
@mv meta_generated.rs $@
bindings-swift: $(BINDINGS)/swift/NdbProfile.swift $(BINDINGS)/swift/NdbMeta.swift
$(BINDINGS)/swift/NdbProfile.swift: schemas/profile.fbs $(BINDINGS)/swift
flatc --gen-json-emit --swift $<
@mv profile_generated.swift $@
$(BINDINGS)/swift/NdbMeta.swift: schemas/meta.fbs $(BINDINGS)/swift
flatc --swift $<
@mv meta_generated.swift $@
deps/.dir:
@mkdir -p deps
touch deps/.dir
deps/LMDB_$(LMDB_VER).tar.gz: deps/.dir
curl -L https://github.com/LMDB/lmdb/archive/refs/tags/LMDB_$(LMDB_VER).tar.gz -o $@
deps/flatcc_$(FLATCC_VER).tar.gz: deps/.dir
curl -L https://github.com/jb55/flatcc/archive/$(FLATCC_VER).tar.gz -o $@
#deps/flatcc/src/runtime/json_parser.c: deps/flatcc_$(FLATCC_VER).tar.gz deps/.dir
# tar xf $<
# rm -rf deps/flatcc
# mv flatcc-$(FLATCC_VER) deps/flatcc
# touch $@
#deps/lmdb/lmdb.h: deps/LMDB_$(LMDB_VER).tar.gz deps/.dir
# tar xf $<
# rm -rf deps/lmdb
# mv lmdb-LMDB_$(LMDB_VER)/libraries/liblmdb deps/lmdb
# rm -rf lmdb-LMDB_$(LMDB_VER)
# touch $@
deps/secp256k1/.git: deps/.dir
@devtools/refresh-submodules.sh $(SUBMODULES)
deps/secp256k1/include/secp256k1.h: deps/secp256k1/.git
deps/secp256k1/configure: deps/secp256k1/.git
cd deps/secp256k1; \
./autogen.sh
deps/secp256k1/.libs/libsecp256k1.a: deps/secp256k1/config.log
cd deps/secp256k1; \
make -j libsecp256k1.la
deps/secp256k1/config.log: deps/secp256k1/configure
cd deps/secp256k1; \
./configure --disable-shared --enable-module-ecdh --enable-module-schnorrsig --enable-module-extrakeys
deps/lmdb/liblmdb.a: deps/lmdb/lmdb.h
$(MAKE) -C deps/lmdb liblmdb.a
testdata/db/ndb-v0.tar.zst:
curl https://cdn.jb55.com/s/ndb-v0.tar.zst -o $@
testdata/db/ndb-v0.tar: testdata/db/ndb-v0.tar.zst
zstd -d < $< > $@
testdata/db/v0/data.mdb: testdata/db/ndb-v0.tar
tar xf $<
rm -rf testdata/db/v0
mv v0 testdata/db
testdata/many-events.json.zst:
curl https://cdn.jb55.com/s/many-events.json.zst -o $@
testdata/many-events.json: testdata/many-events.json.zst
zstd -d $<
bench: bench-ingest-many.c $(DEPS)
$(CC) $(CFLAGS) $< $(LDS) $(LDFLAGS) -o $@
perf.out: fake
perf script > $@
perf.folded: perf.out
stackcollapse-perf.pl $< > $@
ndb.svg: perf.folded
flamegraph.pl $< > $@
flamegraph: ndb.svg
browser $<
run-bench: testdata/many-events.json bench
./bench
testdata/db/.dir:
@mkdir -p testdata/db
touch testdata/db/.dir
test: test.c $(DEPS) testdata/db/.dir
$(CC) $(CFLAGS) test.c $(LDS) $(LDFLAGS) -o $@
# Call this with CCAN_NEW="mod1 mod2..." to add new ccan modules.
update-ccan:
mv ccan ccan.old
DIR=$$(pwd)/ccan; cd ../ccan && ./tools/create-ccan-tree -a $$DIR `cd $$DIR.old/ccan && find * -name _info | sed s,/_info,, | LC_ALL=C sort` $(CCAN_NEW)
mkdir -p ccan/tools/configurator
cp ../ccan/tools/configurator/configurator.c ../ccan/doc/configurator.1 ccan/tools/configurator/
$(MAKE) src/config.h
grep -v '^CCAN version:' ccan.old/README > ccan/README
echo CCAN version: `git -C ../ccan describe` >> ccan/README
$(RM) -r ccan/ccan/hash/ ccan/ccan/tal/talloc/ # Unnecessary deps
$(RM) -r ccan.old
.PHONY: tags clean fake update-ccan