aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: fe01fb61d041df69f8da8c7907fbc25c65e719d8 (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
CC := gcc
CFLAGS := -Wall -Wextra -std=c11 -g -O2 -fwrapv -D_DEFAULT_SOURCE
LDFLAGS := -ldl
CFLAGS += $(shell pkg-config --cflags sqlite3 libsodium)
LDFLAGS += $(shell pkg-config --libs sqlite3 libsodium)

TARGETS := tomsg_server
TEST_TARGET := test/main

PLUGINDIR := plugins

PLUGINS := $(filter-out _%,$(patsubst $(PLUGINDIR)/%,%,$(shell find $(PLUGINDIR)/ -maxdepth 1 -type d)))

SOURCES := $(wildcard *.c)
OBJECTS := $(SOURCES:.c=.o)
TEST_ONLY_SOURCES := $(wildcard test/*.c)
TEST_ONLY_OBJECTS := $(TEST_ONLY_SOURCES:.c=.o)
TEST_SOURCES := $(TEST_ONLY_SOURCES) $(filter-out main.c,$(SOURCES))
TEST_OBJECTS := $(TEST_SOURCES:.c=.o)

.PHONY: all clean test_build test

# Clear all implicit suffix rules
.SUFFIXES:

# Don't delete intermediate files
.SECONDARY:

all: $(TARGETS)
	for pl in $(PLUGINS); do $(MAKE) --no-print-directory -C $(PLUGINDIR) $@ PLUGINNAME=$$pl; done

clean:
	rm -f $(TARGETS) $(TEST_TARGET) *.o *.sql.h test/test_declarations.h
	for pl in $(PLUGINS); do $(MAKE) --no-print-directory -C $(PLUGINDIR) $@ PLUGINNAME=$$pl; done

test_build: $(TEST_TARGET)

test: test_build
	$(TEST_TARGET)


$(TARGETS): $(OBJECTS)
	$(CC) -o $@ $^ $(LDFLAGS)

$(OBJECTS) $(TEST_ONLY_OBJECTS): $(wildcard *.h) $(patsubst %.sql,%.sql.h,$(wildcard *.sql))
$(TEST_ONLY_OBJECTS): test/test_declarations.h
%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

%.sql.h: %.sql
	xxd -i $^ $@


$(TEST_TARGET): $(TEST_OBJECTS) $(wildcard *.h test/*.h)
	$(CC) -o $@ $(filter %.o,$^) $(LDFLAGS)

test/test_declarations.h: $(wildcard test/*.c)
	$(CC) -E -D TEST_HEADER_GENERATION=1 test/*.c | sed -n 's/^XXTEST_DECLARATION(\([^)]*\)).*/int \1(void);/p' >$@