summaryrefslogtreecommitdiff
path: root/Makefile
blob: d5db551138e91fc6a77f21c9ed7c28661a933cfb (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Simple Makefile for libwapcaplet

LIB := libwapcaplet.a

SRCS := libwapcaplet.c
HDRS := libwapcaplet/libwapcaplet.h

TESTSRCS := testmain.c basictests.c

TARGET ?= debug

BUILDDIR := build-$(TARGET)

all: $(BUILDDIR)/$(LIB)

test: $(BUILDDIR)/testrunner
	$(BUILDDIR)/testrunner

CFLAGS := -Iinclude -Wall -Werror

ifeq ($(TARGET),debug)
CFLAGS += -O0 -g
else
CFLAGS += -O2 -DNDEBUG
endif


clean:
	rm -fr build-*

$(BUILDDIR)/stamp:
	mkdir -p $(BUILDDIR)
	touch $(BUILDDIR)/stamp

define srcfile
src/$1
endef

define objfile
$(BUILDDIR)/$(1:.c=.o)
endef

define depfile
$(BUILDDIR)/$(1:.c=.d)
endef

DEPS :=
OBJS :=

define _depandbuild

$2: $1 $(BUILDDIR)/stamp
	$(CC) -MMD -MP $($5) -o $2 -c $1

$4 += $2
DEPS += $3

endef

define depandbuild
$(call _depandbuild,$(call srcfile,$1),$(call objfile,$1),$(call depfile,$1),OBJS,CFLAGS)
endef

$(eval $(foreach SOURCE,$(SRCS),$(call depandbuild,$(SOURCE))))

ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif

$(BUILDDIR)/$(LIB): $(BUILDDIR)/stamp $(OBJS)
	$(AR) cru $@ $^

define testsrc
test/$1
endef

define depandbuildtest
$(call _depandbuild,$(call testsrc,$1),$(call objfile,test-$1),$(call depfile,test-$1),TOBJS,TESTCFLAGS)
endef

TOBJS :=

TESTCFLAGS := $(CFLAGS) $(shell pkg-config --cflags check)
TESTLDFLAGS := $(LDFLAGS) $(shell pkg-config --libs check)

$(eval $(foreach TESTSRC,$(TESTSRCS),$(call depandbuildtest,$(TESTSRC))))

$(BUILDDIR)/testrunner: $(BUILDDIR)/stamp $(TOBJS) $(BUILDDIR)/$(LIB)
	$(CC) -o $@ $(TOBJS) $(BUILDDIR)/$(LIB) $(TESTLDFLAGS)