blob: cb09d3ff8f5856dab489130133ec9c2e2ba73db8 [file] [log] [blame]
Sasha Levin5634bd72013-06-13 18:41:17 -04001# file format version
2FILE_VERSION = 1
3
4MAKEFLAGS += --no-print-directory
Sasha Levinbe227b42014-04-01 11:42:18 -04005LIBLOCKDEP_VERSION=$(shell make -sC ../../.. kernelversion)
Sasha Levin5634bd72013-06-13 18:41:17 -04006
7# Makefiles suck: This macro sets a default value of $(2) for the
8# variable named by $(1), unless the variable has been set by
9# environment or command line. This is necessary for CC and AR
10# because make sets default values, so the simpler ?= approach
11# won't work as expected.
12define allow-override
13 $(if $(or $(findstring environment,$(origin $(1))),\
14 $(findstring command line,$(origin $(1)))),,\
15 $(eval $(1) = $(2)))
16endef
17
18# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
19$(call allow-override,CC,$(CROSS_COMPILE)gcc)
20$(call allow-override,AR,$(CROSS_COMPILE)ar)
21
22INSTALL = install
23
24# Use DESTDIR for installing into a different root directory.
25# This is useful for building a package. The program will be
26# installed in this directory as if it was the root directory.
27# Then the build tool can move it later.
28DESTDIR ?=
29DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
30
31prefix ?= /usr/local
32libdir_relative = lib
33libdir = $(prefix)/$(libdir_relative)
34bindir_relative = bin
35bindir = $(prefix)/$(bindir_relative)
36
37export DESTDIR DESTDIR_SQ INSTALL
38
39# copy a bit from Linux kbuild
40
41ifeq ("$(origin V)", "command line")
42 VERBOSE = $(V)
43endif
44ifndef VERBOSE
45 VERBOSE = 0
46endif
47
48ifeq ("$(origin O)", "command line")
49 BUILD_OUTPUT := $(O)
50endif
51
52ifeq ($(BUILD_SRC),)
53ifneq ($(BUILD_OUTPUT),)
54
55define build_output
56 $(if $(VERBOSE:1=),@)$(MAKE) -C $(BUILD_OUTPUT) \
57 BUILD_SRC=$(CURDIR) -f $(CURDIR)/Makefile $1
58endef
59
60saved-output := $(BUILD_OUTPUT)
61BUILD_OUTPUT := $(shell cd $(BUILD_OUTPUT) && /bin/pwd)
62$(if $(BUILD_OUTPUT),, \
63 $(error output directory "$(saved-output)" does not exist))
64
65all: sub-make
66
67gui: force
68 $(call build_output, all_cmd)
69
70$(filter-out gui,$(MAKECMDGOALS)): sub-make
71
72sub-make: force
73 $(call build_output, $(MAKECMDGOALS))
74
75
76# Leave processing to above invocation of make
77skip-makefile := 1
78
79endif # BUILD_OUTPUT
80endif # BUILD_SRC
81
82# We process the rest of the Makefile if this is the final invocation of make
83ifeq ($(skip-makefile),)
84
Wang Nan7b885342014-02-08 09:43:40 +080085srctree := $(realpath $(if $(BUILD_SRC),$(BUILD_SRC),$(CURDIR)))
86objtree := $(realpath $(CURDIR))
Sasha Levin5634bd72013-06-13 18:41:17 -040087src := $(srctree)
88obj := $(objtree)
89
90export prefix libdir bindir src obj
91
92# Shell quotes
93libdir_SQ = $(subst ','\'',$(libdir))
94bindir_SQ = $(subst ','\'',$(bindir))
95
Sasha Levinbe227b42014-04-01 11:42:18 -040096LIB_FILE = liblockdep.a liblockdep.so.$(LIBLOCKDEP_VERSION)
Sasha Levin5634bd72013-06-13 18:41:17 -040097BIN_FILE = lockdep
98
99CONFIG_INCLUDES =
100CONFIG_LIBS =
101CONFIG_FLAGS =
102
103OBJ = $@
104N =
105
106export Q VERBOSE
107
Ira W. Snyderaef59762014-01-31 13:35:32 -0800108INCLUDES = -I. -I/usr/local/include -I./uinclude -I./include $(CONFIG_INCLUDES)
Sasha Levin5634bd72013-06-13 18:41:17 -0400109
110# Set compile option CFLAGS if not set elsewhere
111CFLAGS ?= -g -DCONFIG_LOCKDEP -DCONFIG_STACKTRACE -DCONFIG_PROVE_LOCKING -DBITS_PER_LONG=__WORDSIZE -DLIBLOCKDEP_VERSION='"$(LIBLOCKDEP_VERSION)"' -rdynamic -O0 -g
112
113override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
114
115ifeq ($(VERBOSE),1)
116 Q =
117 print_compile =
118 print_app_build =
119 print_fpic_compile =
120 print_shared_lib_compile =
121 print_install =
122else
123 Q = @
124 print_compile = echo ' CC '$(OBJ);
125 print_app_build = echo ' BUILD '$(OBJ);
126 print_fpic_compile = echo ' CC FPIC '$(OBJ);
127 print_shared_lib_compile = echo ' BUILD SHARED LIB '$(OBJ);
128 print_static_lib_build = echo ' BUILD STATIC LIB '$(OBJ);
129 print_install = echo ' INSTALL '$1' to $(DESTDIR_SQ)$2';
130endif
131
132do_fpic_compile = \
133 ($(print_fpic_compile) \
134 $(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@)
135
136do_app_build = \
137 ($(print_app_build) \
138 $(CC) $^ -rdynamic -o $@ $(CONFIG_LIBS) $(LIBS))
139
140do_compile_shared_library = \
141 ($(print_shared_lib_compile) \
Sasha Levinbe227b42014-04-01 11:42:18 -0400142 $(CC) --shared $^ -o $@ -lpthread -ldl -Wl,-soname='"$@"';$(shell ln -s $@ liblockdep.so))
Sasha Levin5634bd72013-06-13 18:41:17 -0400143
144do_build_static_lib = \
145 ($(print_static_lib_build) \
146 $(RM) $@; $(AR) rcs $@ $^)
147
148
149define do_compile
150 $(print_compile) \
151 $(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@;
152endef
153
154$(obj)/%.o: $(src)/%.c
155 $(Q)$(call do_compile)
156
157%.o: $(src)/%.c
158 $(Q)$(call do_compile)
159
160PEVENT_LIB_OBJS = common.o lockdep.o preload.o rbtree.o
161
162ALL_OBJS = $(PEVENT_LIB_OBJS)
163
164CMD_TARGETS = $(LIB_FILE)
165
166TARGETS = $(CMD_TARGETS)
167
168
169all: all_cmd
170
171all_cmd: $(CMD_TARGETS)
172
Sasha Levinbe227b42014-04-01 11:42:18 -0400173liblockdep.so.$(LIBLOCKDEP_VERSION): $(PEVENT_LIB_OBJS)
Sasha Levin5634bd72013-06-13 18:41:17 -0400174 $(Q)$(do_compile_shared_library)
175
176liblockdep.a: $(PEVENT_LIB_OBJS)
177 $(Q)$(do_build_static_lib)
178
179$(PEVENT_LIB_OBJS): %.o: $(src)/%.c
180 $(Q)$(do_fpic_compile)
181
182## make deps
183
184all_objs := $(sort $(ALL_OBJS))
185all_deps := $(all_objs:%.o=.%.d)
186
187# let .d file also depends on the source and header files
188define check_deps
189 @set -e; $(RM) $@; \
190 $(CC) -MM $(CFLAGS) $< > $@.$$$$; \
191 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
192 $(RM) $@.$$$$
193endef
194
195$(all_deps): .%.d: $(src)/%.c
196 $(Q)$(call check_deps)
197
198$(all_objs) : %.o : .%.d
199
200dep_includes := $(wildcard $(all_deps))
201
202ifneq ($(dep_includes),)
203 include $(dep_includes)
204endif
205
206### Detect environment changes
207TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE)
208
209tags: force
210 $(RM) tags
211 find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
212 --regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/'
213
214TAGS: force
215 $(RM) TAGS
216 find . -name '*.[ch]' | xargs etags \
217 --regex='/_PE(\([^,)]*\).*/PEVENT_ERRNO__\1/'
218
219define do_install
220 $(print_install) \
221 if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
222 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
223 fi; \
224 $(INSTALL) $1 '$(DESTDIR_SQ)$2'
225endef
226
227install_lib: all_cmd
228 $(Q)$(call do_install,$(LIB_FILE),$(libdir_SQ))
229 $(Q)$(call do_install,$(BIN_FILE),$(bindir_SQ))
230
231install: install_lib
232
233clean:
234 $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d
235 $(RM) tags TAGS
236
237endif # skip-makefile
238
239PHONY += force
240force:
241
242# Declare the contents of the .PHONY variable as phony. We keep that
243# information in a variable so we can use it in if_changed and friends.
244.PHONY: $(PHONY)