This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile.include
244 lines (191 loc) · 6.37 KB
/
Makefile.include
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# -*- makefile -*-
ifndef TWO
${error TWO not defined! You must specify where two resides}
endif
# Find OS
OS = $(word 1, $(shell uname -m -s))
# Default CFLAGS
CFLAGS = -std=c99 -Wall -Wextra
# Use bsd timers
CFLAGS += -D_DEFAULT_SOURCE
# Configure output directories
# Path for final binaries to be stored
TARGET_DIR ?= bin
# Build dir is for temporary files
BUILD_DIR ?= build
SHELL = bash
# Extra definitions can be added using the DEFINES variable
ifeq ($(DEFINES),)
-include Makefile.defines
ifneq ($(DEFINES),)
${info using saved defines '$(DEFINES)'}
endif
endif
#More debug information when running in CI
ifdef CI
ifeq ($(CI),true)
QUIET = 0
endif
endif
# Remove duplicates
uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
# Path for objects and dependencies
SRC = ${TWO}/src
OBJECTDIR = $(BUILD_DIR)/obj
# Path for test builds
TEST_BUILD = $(BUILD_DIR)/test
# Library paths
LIBRARY = $(BUILD_DIR)/two.a
LIBRARY_OBJECTS = $(addprefix ${OBJECTDIR}/,$(patsubst %.c,%.o,$(LIBRARY_SOURCES)))
# Target objects
OBJECTS = $(addprefix ${OBJECTDIR}/,$(patsubst %.c,%.o,$(SOURCES)))
# Remove implicit rules
.SUFFIXES:
.SUFFIXES: .c .o
# Provide way to recreate necessary directories
DIRS += $(TARGET_DIR) $(TEST_BUILD) $(OBJECTDIR)
$(DIRS):
$(TRACE_MKDIR)
$(Q)mkdir -p $@
# Library module configuration (under src/)
# One big module under src for now
MODULEDIRS = $(SRC) $(SRC)/hpack
UNIQUEMODULES = $(call uniq,$(MODULEDIRS))
MODULES_SOURCES = $(foreach d, $(UNIQUEMODULES), ${subst ${SRC}/,,${wildcard $(d)/*.c}})
# Update library sources
LIBRARY_SOURCES += $(MODULES_SOURCES)
# Include module-specific makefiles
MODULES_INCLUDES = ${wildcard ${foreach d, $(UNIQUEMODULES), $(d)/Makefile.${notdir $(d)}}}
### Perform an immediate expansion of MODULES_INCLUDES and store it in a
### variable. This will allow us to subsequently filter-out module Makefiles
### that were included in the first pass, such that we don't end up including
### them twice.
MODULES_INCLUDED_FIRST_PASS := $(MODULES_INCLUDES)
include $(MODULES_INCLUDED_FIRST_PASS)
# Iterate once more: include the modules added from the previous include.
# Only works with one level of nested module inclusion.
include $(filter-out $(MODULES_INCLUDED_FIRST_PASS),$(MODULES_INCLUDES))
define SET_SOURCE_FOR_TARGET
$$(subst /,_,$$(basename ${1})) := ${1}
endef
define GET_SOURCE_FOR_TARGET
$(if ${${1}},$(basename ${${1}}),${1})
endef
# Test configurations
$(foreach s,${LIBRARY_SOURCES},$(eval $(call SET_SOURCE_FOR_TARGET,${s})))
define INCLUDE_TEST
TEST_SUBDIRS :=
DIR := $${TWO}/${1}
ifneq ($$(wildcard $${DIR}/Makefile.$$(notdir $$(DIR))),)
include $$(DIR)/Makefile.$$(notdir $$(DIR))
TEST_SUBDIRS := $(addprefix $${DIR}/,$${TEST_SUBDIRS})
TESTDIRS += $${TEST_SUBDIRS}
# Include Makefile.<dirname> if it exists
include $$(wildcard $$(foreach d,$${TEST_SUBDIRS},$${d}/Makefile.$$(notdir $${d})))
endif
endef
#Include all tests in TESTDIRS
$(foreach d,${TESTDIRS},$(eval $(call INCLUDE_TEST,${d:%/=%})))
### Verbosity control. Use make QUIET=0 to get verbose builds.
ifeq ($(QUIET),0)
TRACE_CC =
TRACE_LD =
TRACE_AR =
TRACE_AS =
TRACE_MKDIR =
TRACE_RM =
TRACE_CP =
Q=
else
MAKEFLAGS = --no-print-directory
TRACE_CC = @echo " CC " $<
TRACE_LD = @echo " LD " $@
TRACE_AR = @echo " AR " $@
TRACE_AS = @echo " AS " $<
TRACE_MKDIR = @echo " MKDIR " $@
TRACE_RM = @echo " RM " ${1}
TRACE_CP = @echo " CP " $< "-->" $@
Q=@
endif
### Forward comma-separated list of arbitrary defines to the compiler
COMMA := ,
CFLAGS += ${addprefix -D,${subst $(COMMA), ,$(DEFINES)}}
### Where to look for source files
SOURCEDIRS = . $(TARGETDIRS) $(MODULEDIRS)
vpath %.c $(SOURCEDIRS) $(TESTDIRS)
### List of include directories for the compiler
CFLAGS += $(addprefix -I,$(SOURCEDIRS))
### Automatic dependency generation
ifneq ($(MAKECMDGOALS),clean)
-include ${addprefix $(OBJECTDIR)/,$(LIBRARY_SOURCES:.c=.d) \
$(SOURCES:.c=.d)}
endif
### See http://make.paulandlesley.org/autodep.html#advanced
define FINALIZE_DEPENDENCY
cp $(@:.o=.d) $(@:.o=.$$$$); \
sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
-e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.$$$$) >> $(@:.o=.d); \
rm -f $(@:.o=.$$$$)
endef
.PHONY: clean distclean debug release
clean:
$(call TRACE_RM,*.d *.o $(CLEAN) $(OBJECTDIR))
-$(Q)rm -f *.d *.o $(CLEAN)
-$(Q)rm -rf $(OBJECTDIR)
distclean:
$(call TRACE_RM,$(BUILD_DIR) $(TARGET_DIR))
-$(Q)rm -rf $(BUILD_DIR)
-$(Q)rm -rf $(TARGET_DIR)
.PHONY: debug
debug: CFLAGS += -g $(DEBUG_FLAGS)
debug: all
# disable assertions for release build
.PHONY: release
release: CFLAGS += -DNDEBUG
release: al
# Build regular objects
$(OBJECTDIR)/%.o: %.c | $(OBJECTDIR)
@mkdir -p $(dir $@)
$(TRACE_CC)
$(Q)$(strip $(CC) $(CFLAGS) -MMD -c $< -o $@)
@$(FINALIZE_DEPENDENCY)
# Build test object
$(TEST_BUILD)/test_%.o: CFLAGS += -g ${TEST_CFLAGS} $(addprefix -I,$(TESTDIRS))
$(TEST_BUILD)/test_%.o: test_%.c | $(TEST_BUILD)
$(TRACE_CC)
$(Q)$(strip $(CC) $(CFLAGS) -c $< -o $@)
$(TEST_BUILD)/%.o: %.c | $(TEST_BUILD)
@mkdir -p $(dir $@)
$(TRACE_CC)
$(Q)$(strip $(CC) $(CFLAGS) $(TEST_SRC_CFLAGS) -g -DNDEBUG -MMD -c $< -o $@)
@$(FINALIZE_DEPENDENCY)
%.o: %.c
$(TRACE_CC)
$(Q)$(strip $(CC) $(CFLAGS) -c $< -o $@)
$(LIBRARY): $(LIBRARY_OBJECTS)
$(TRACE_AR)
$(Q)$(strip $(AR) $(ARFLAGS) $@ $^)
# Build target rule
$(TARGET_DIR)/%: %.o $(OBJECTS) $(LIBRARY) | $(TARGET_DIR)
$(TRACE_LD)
$(Q)$(strip $(CC) $(LDFLAGS) -o $@ ${filter-out %.a,$^} ${filter %.a,$^} $(LDLIBS))
.PHONY: test_%
test_%: $(TEST_BUILD)/test_%
$(Q)$^
.PRECIOUS: $(TARGET_DIR)/% $(TEST_BUILD)/test_%
# Cancel the predefined implict rule for compiling and linking
# a single C source into a binary to force GNU make to consider
# the match-anything rule below instead.
%: %.c
# Match-anything pattern rule to allow the project makefiles to
# abstract from the actual binary name. It needs to contain some
# command in order to be a rule, not just a prerequisite.
%: $(TARGET_DIR)/%
@
.SECONDEXPANSION:
# Build tests rule
$(TEST_BUILD)/test_%: LDFLAGS += $(TEST_LDFLAGS)
$(TEST_BUILD)/test_%: LDLIBS += $(TEST_LDLIBS)
$(TEST_BUILD)/test_%: $(TEST_BUILD)/test_%.o $(TEST_BUILD)/$$(call GET_SOURCE_FOR_TARGET,%).o $(TEST_PREREQS)
$(TRACE_LD)
$(Q)$(strip $(CC) $(LDFLAGS) -o $@ ${filter-out %.a,$^} ${filter %.a,$^} $(LDLIBS))