summaryrefslogtreecommitdiff
path: root/build/Makefile.common
blob: 418a5a8e1ec303d291a71e1b6ca4bf48b2682d15 (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
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
# Top-level Makefile fragment

# Default target
all: release

# Name of component
COMPONENT := libparserutils

# Environment
EXPORT := $(CURDIR)/dist
TOP := $(CURDIR)
RELEASEDIR := build/Release
DEBUGDIR := build/Debug
COVERAGEDIR := build/coverage

# List of items to delete on clean
ITEMS_CLEAN :=
# List of items to delete on distclean
ITEMS_DISTCLEAN :=

# List of targets to run for testing
TARGET_TESTS :=

# Source files
SOURCES :=

# Include configuration Makefile fragment
include build/Makefile.config

# Include Makefile fragments in subdirectories

define do_include
DIR := $$(dir $(1))
include $(1)

endef

MAKE_INCLUDES := $(wildcard */Makefile)
$(eval $(foreach INC, $(MAKE_INCLUDES), $(call do_include,$(INC))))

# Calculate objects to build
OBJECTS := $(subst /,_,$(subst .c,.o,$(SOURCES)))

.PHONY: release debug test coverage profile \
	clean distclean setup export install uninstall

# Rules
release: setup $(addprefix $(RELEASEDIR)/,$(OBJECTS))
	@$(AR) $(ARFLAGS) $(COMPONENT).a $(RELEASEDIR)/*

debug: setup $(addprefix $(DEBUGDIR)/,$(OBJECTS))
	@$(AR) $(ARFLAGS) $(COMPONENT)-debug.a $(DEBUGDIR)/*

test: debug $(TARGET_TESTS)

coverage: clean
	@$(LCOV) --directory . --zerocounters
	@$(MAKE) test CFLAGS="$(CFLAGS) -fprofile-arcs -ftest-coverage" \
		LDFLAGS="$(LDFLAGS) -lgcov"
	@$(LCOV) --directory $(DEBUGDIR) --base-directory $(TOP) \
		--capture --output-file $(COVERAGEDIR)/$(COMPONENT)_tmp.info
	@$(LCOV) --extract $(COVERAGEDIR)/$(COMPONENT)_tmp.info "$(TOP)/src*" \
		-o $(COVERAGEDIR)/$(COMPONENT).info
	@$(RM) $(RMFLAGS) $(COVERAGEDIR)/$(COMPONENT)_tmp.info
	@$(GENHTML) -o $(COVERAGEDIR) --num-spaces 2 \
		$(COVERAGEDIR)/$(COMPONENT).info

profile: clean
	@$(MAKE) test CFLAGS="$(CFLAGS) -pg" LDFLAGS="-pg $(LDFLAGS)"

clean:
	-@$(RM) $(RMFLAGS) $(ITEMS_CLEAN)
	-@$(RM) $(RMFLAGS) gmon.out
	-@$(RM) $(RMFLAGS) -r $(COVERAGEDIR)
	-@$(RM) $(RMFLAGS) -r $(RELEASEDIR)
	-@$(RM) $(RMFLAGS) -r $(DEBUGDIR)
	-@$(RM) $(RMFLAGS) $(COMPONENT).a
	-@$(RM) $(RMFLAGS) $(COMPONENT)-debug.a
	-@$(RM) $(RMFLAGS) $(COMPONENT).pc

distclean: clean
	-@$(RM) $(RMFLAGS) $(ITEMS_DISTCLEAN)
	-@$(RM) $(RMFLAGS) -r $(TOP)/dist

setup:
	@$(MKDIR) $(MKDIRFLAGS) $(RELEASEDIR)
	@$(MKDIR) $(MKDIRFLAGS) $(DEBUGDIR)
	@$(MKDIR) $(MKDIRFLAGS) $(COVERAGEDIR)

export: release
	@$(MKDIR) $(MKDIRFLAGS) $(TOP)/dist/lib
	@$(CP) $(CPFLAGS) -r include $(EXPORT)/
	@${CP} ${CPFLAGS} $(COMPONENT).a ${EXPORT}/lib/

install: release
	@$(MKDIR) $(MKDIRFLAGS) -p $(PREFIX)/lib/pkgconfig
	@$(MKDIR) $(MKDIRFLAGS) -p $(PREFIX)/include/parserutils
	@$(MKDIR) $(MKDIRFLAGS) -p $(PREFIX)/include/parserutils/charset
	@$(MKDIR) $(MKDIRFLAGS) -p $(PREFIX)/include/parserutils/input
	@$(MKDIR) $(MKDIRFLAGS) -p $(PREFIX)/include/parserutils/utils
	@$(SED) -e 's#PREFIX#$(PREFIX)#' $(COMPONENT).pc.in >$(COMPONENT).pc
	@$(INSTALL) --mode=644 -t $(PREFIX)/lib $(COMPONENT).a
	@$(INSTALL) --mode=644 -t $(PREFIX)/lib/pkgconfig $(COMPONENT).pc
	@$(INSTALL) --mode=644 -t $(PREFIX)/include/parserutils $(filter %.h, $(wildcard include/parserutils/*))
	@$(INSTALL) --mode=644 -t $(PREFIX)/include/parserutils/charset $(filter %.h, $(wildcard include/parserutils/charset/*))
	@$(INSTALL) --mode=644 -t $(PREFIX)/include/parserutils/input $(filter %.h, $(wildcard include/parserutils/input/*))
	@$(INSTALL) --mode=644 -t $(PREFIX)/include/parserutils/utils $(filter %.h, $(wildcard include/parserutils/utils/*))


uninstall:
	@$(RM) $(RMFLAGS) $(PREFIX)/lib/$(COMPONENT).a
	@$(RM) $(RMFLAGS) $(PREFIX)/lib/pkgconfig/$(COMPONENT).pc
	@$(RM) $(RMFLAGS) -r $(PREFIX)/include/parserutils

# Finally, build rules for compilation
define do_compile
$$(RELEASEDIR)/$(2): $(1)
	@$$(ECHO) $$(ECHOFLAGS) "==> $(1)"
	@$$(CC) -c $$(RELEASECFLAGS) -o $$@ $(1)

$$(DEBUGDIR)/$(2): $(1)
	@$$(ECHO) $$(ECHOFLAGS) "==> $(1)"
	@$$(CC) -c $$(DEBUGCFLAGS) -o $$@ $(1)

endef

$(eval $(foreach SOURCE,$(filter %.c,$(SOURCES)), \
	$(call do_compile,$(SOURCE),$(subst /,_,$(SOURCE:.c=.o)))))