aboutsummaryrefslogtreecommitdiffstats
path: root/Makefile
blob: c6c7e37a58399ac4f579d302848fc7c0e0913005 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Makefile

GPERF = gperf
CC = gcc

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

all: $(BLDDIR) endpoints.so main

.PHONY: all clean run

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

$(BLDDIR)/template:
	mkdir -p $@

$(BLDDIR)/ste: c_headers/ste/ste.c
	$(CC) -o $@ $<


$(BLDDIR)/template/%: template/%.ste $(BLDDIR)/ste $(BLDDIR)/template
	$(BLDDIR)/ste -o $@ $<


# endpoints
$(BLDDIR)/endpoints.o:	src/endpoints.c $(BLDDIR) \
			$(BLDDIR)/template/args.html $(BLDDIR)/template/linkadd.html
	# 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 $(BLDDIR)/rename.ld
	$(CC) -shared -fPIC -Wl,$(BLDDIR)/rename.ld $(CFLAGS) $(BLDDIR)/endpoints.o $(BLDDIR)/common.o $(BLDDIR)/mime.o	$(BLDDIR)/sql.o -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) --recursive $(BLDDIR)/template/* $(BLDDIR)/* main endpoints.o endpoints.so rename.ld include/mime.c.inc

veryclean: clean
	$(RM) jals.db config.env

run: 	main endpoints.so
	env $(shell cat config.env) ./main