aboutsummaryrefslogtreecommitdiffstats
path: root/Makefile
blob: 372caed242b9b1af5657be46f55a8debf7b9069b (plain)
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
# Makefile

GPERF = gperf
CC = gcc

CFLAGS += -Ic_headers/include -Iinclude -ggdb -Wall -Wextra -DDEFAULT_ENDPOINTS_PATH=\"./endpoints.so\"
BLDDIR ?= build

all: $(BLDDIR) endpoints.so main

.PHONY: all clean $(BLDDIR)

$(BLDDIR):
	mkdir -p $(BLDDIR)


# endpoints
$(BLDDIR)/endpoints.o: src/endpoints.c $(BLDDIR)/rename.ld $(BLDDIR)
	# PIC here is important
	$(CC) -fPIC -c $< $(CFLAGS) -o $@

$(BLDDIR)/sql.o: src/sql.c
	$(CC) -fPIC -c $< $(CFLAGS) -o $@

$(BLDDIR)/rename.ld: $(BLDDIR)/endpoints.o $(BLDDIR)
	# Looks terrible
	# All it does is generating ld script that creates aliases for functions 
	echo SECTIONS { > $@
	@readelf --syms --wide $(BLDDIR)/endpoints.o | awk '/FUNC/ && /GLOBAL/ && /ENDP/ {old = $$8; gsub(/_/, "/", $$8); gsub(/ENDP/, "", $$8); printf "\t%s = %s;\n", $$8, old }' >> $@
	echo } >> $@

endpoints.so: $(BLDDIR)/endpoints.o $(BLDDIR)/common.o $(BLDDIR)/mime.o	$(BLDDIR)/sql.o
	$(CC) -shared -fPIC -Wl,$(BLDDIR)/rename.ld $(CFLAGS) $^ -lmicrohttpd -lsqlite3 -o $@


# main
src/mime.c.inc: src/mime_gen.c.gperf
	$(GPERF) -IGCt -K key -H __mimetype_hash -N __mimetype_get_helper -W mimetype_words $< > $@

$(BLDDIR)/mime.o: src/mime.c src/mime.c.inc
	$(CC) -c $(CFLAGS) -o $@ $<

$(BLDDIR)/%.o: src/%.c
	$(CC) -c $(CFLAGS) -o $@ $^

main: $(BLDDIR)/main.o $(BLDDIR)/mime.o $(BLDDIR)/common.o
	$(CC) $(CFLAGS) $^ -ldl -lmicrohttpd -lsqlite3 -o $@

clean:
	$(RM) $(BLDDIR)/* main endpoints.o endpoints.so rename.ld include/mime.h.inc