blob: 0794acca46a39d433cdfee0d51d4aea25fc5ffe8 [file] [log] [blame]
Steven Rostedtf7d82352012-04-06 00:47:53 +02001# trace-cmd version
2EP_VERSION = 1
3EP_PATCHLEVEL = 1
4EP_EXTRAVERSION = 0
5
6# file format version
7FILE_VERSION = 6
8
9MAKEFLAGS += --no-print-directory
10
11
12# Makefiles suck: This macro sets a default value of $(2) for the
13# variable named by $(1), unless the variable has been set by
14# environment or command line. This is necessary for CC and AR
15# because make sets default values, so the simpler ?= approach
16# won't work as expected.
17define allow-override
18 $(if $(or $(findstring environment,$(origin $(1))),\
19 $(findstring command line,$(origin $(1)))),,\
20 $(eval $(1) = $(2)))
21endef
22
23# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
24$(call allow-override,CC,$(CROSS_COMPILE)gcc)
25$(call allow-override,AR,$(CROSS_COMPILE)ar)
26
27EXT = -std=gnu99
28INSTALL = install
29
30# Use DESTDIR for installing into a different root directory.
31# This is useful for building a package. The program will be
32# installed in this directory as if it was the root directory.
33# Then the build tool can move it later.
34DESTDIR ?=
35DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
36
37prefix ?= /usr/local
38bindir_relative = bin
39bindir = $(prefix)/$(bindir_relative)
40man_dir = $(prefix)/share/man
41man_dir_SQ = '$(subst ','\'',$(man_dir))'
Steven Rostedtf7d82352012-04-06 00:47:53 +020042
Namhyung Kim9f1efa82013-06-04 14:20:16 +090043export man_dir man_dir_SQ INSTALL
Steven Rostedtf7d82352012-04-06 00:47:53 +020044export DESTDIR DESTDIR_SQ
45
46# copy a bit from Linux kbuild
47
48ifeq ("$(origin V)", "command line")
49 VERBOSE = $(V)
50endif
51ifndef VERBOSE
52 VERBOSE = 0
53endif
54
55ifeq ("$(origin O)", "command line")
56 BUILD_OUTPUT := $(O)
57endif
58
59ifeq ($(BUILD_SRC),)
60ifneq ($(BUILD_OUTPUT),)
61
62define build_output
63 $(if $(VERBOSE:1=),@)$(MAKE) -C $(BUILD_OUTPUT) \
64 BUILD_SRC=$(CURDIR) -f $(CURDIR)/Makefile $1
65endef
66
67saved-output := $(BUILD_OUTPUT)
68BUILD_OUTPUT := $(shell cd $(BUILD_OUTPUT) && /bin/pwd)
69$(if $(BUILD_OUTPUT),, \
70 $(error output directory "$(saved-output)" does not exist))
71
72all: sub-make
73
Namhyung Kim4ccdf572013-06-04 14:20:17 +090074$(MAKECMDGOALS): sub-make
Steven Rostedtf7d82352012-04-06 00:47:53 +020075
76sub-make: force
77 $(call build_output, $(MAKECMDGOALS))
78
79
80# Leave processing to above invocation of make
81skip-makefile := 1
82
83endif # BUILD_OUTPUT
84endif # BUILD_SRC
85
86# We process the rest of the Makefile if this is the final invocation of make
87ifeq ($(skip-makefile),)
88
89srctree := $(if $(BUILD_SRC),$(BUILD_SRC),$(CURDIR))
90objtree := $(CURDIR)
91src := $(srctree)
92obj := $(objtree)
93
94export prefix bindir src obj
95
96# Shell quotes
97bindir_SQ = $(subst ','\'',$(bindir))
98bindir_relative_SQ = $(subst ','\'',$(bindir_relative))
99
100LIB_FILE = libtraceevent.a libtraceevent.so
101
102CONFIG_INCLUDES =
103CONFIG_LIBS =
104CONFIG_FLAGS =
105
106VERSION = $(EP_VERSION)
107PATCHLEVEL = $(EP_PATCHLEVEL)
108EXTRAVERSION = $(EP_EXTRAVERSION)
109
110OBJ = $@
111N =
112
113export Q VERBOSE
114
115EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION)
116
Jack Mitchellb9e8c372013-03-08 11:21:52 +0000117INCLUDES = -I. $(CONFIG_INCLUDES)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200118
119# Set compile option CFLAGS if not set elsewhere
120CFLAGS ?= -g -Wall
121
122# Append required CFLAGS
123override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
Arnaldo Carvalho de Meloc0558752012-09-12 14:29:40 -0300124override CFLAGS += $(udis86-flags) -D_GNU_SOURCE
Steven Rostedtf7d82352012-04-06 00:47:53 +0200125
126ifeq ($(VERBOSE),1)
127 Q =
128 print_compile =
129 print_app_build =
130 print_fpic_compile =
131 print_shared_lib_compile =
132 print_plugin_obj_compile =
133 print_plugin_build =
134 print_install =
135else
136 Q = @
137 print_compile = echo ' CC '$(OBJ);
138 print_app_build = echo ' BUILD '$(OBJ);
139 print_fpic_compile = echo ' CC FPIC '$(OBJ);
140 print_shared_lib_compile = echo ' BUILD SHARED LIB '$(OBJ);
141 print_plugin_obj_compile = echo ' CC PLUGIN OBJ '$(OBJ);
142 print_plugin_build = echo ' CC PLUGI '$(OBJ);
143 print_static_lib_build = echo ' BUILD STATIC LIB '$(OBJ);
144 print_install = echo ' INSTALL '$1' to $(DESTDIR_SQ)$2';
145endif
146
147do_fpic_compile = \
148 ($(print_fpic_compile) \
149 $(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@)
150
151do_app_build = \
152 ($(print_app_build) \
153 $(CC) $^ -rdynamic -o $@ $(CONFIG_LIBS) $(LIBS))
154
155do_compile_shared_library = \
156 ($(print_shared_lib_compile) \
157 $(CC) --shared $^ -o $@)
158
159do_compile_plugin_obj = \
160 ($(print_plugin_obj_compile) \
161 $(CC) -c $(CFLAGS) -fPIC -o $@ $<)
162
163do_plugin_build = \
164 ($(print_plugin_build) \
165 $(CC) $(CFLAGS) -shared -nostartfiles -o $@ $<)
166
167do_build_static_lib = \
168 ($(print_static_lib_build) \
169 $(RM) $@; $(AR) rcs $@ $^)
170
171
172define do_compile
173 $(print_compile) \
174 $(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@;
175endef
176
177$(obj)/%.o: $(src)/%.c
178 $(Q)$(call do_compile)
179
180%.o: $(src)/%.c
181 $(Q)$(call do_compile)
182
183PEVENT_LIB_OBJS = event-parse.o trace-seq.o parse-filter.o parse-utils.o
Namhyung Kimd6c25222013-06-04 14:20:21 +0900184PEVENT_LIB_OBJS += kbuffer-parse.o
Steven Rostedtf7d82352012-04-06 00:47:53 +0200185
186ALL_OBJS = $(PEVENT_LIB_OBJS)
187
188CMD_TARGETS = $(LIB_FILE)
189
190TARGETS = $(CMD_TARGETS)
191
192
193all: all_cmd
194
195all_cmd: $(CMD_TARGETS)
196
197libtraceevent.so: $(PEVENT_LIB_OBJS)
198 $(Q)$(do_compile_shared_library)
199
200libtraceevent.a: $(PEVENT_LIB_OBJS)
201 $(Q)$(do_build_static_lib)
202
Namhyung Kim52b5c0d2012-07-06 16:21:32 +0900203$(PEVENT_LIB_OBJS): %.o: $(src)/%.c TRACEEVENT-CFLAGS
Steven Rostedtf7d82352012-04-06 00:47:53 +0200204 $(Q)$(do_fpic_compile)
205
206define make_version.h
207 (echo '/* This file is automatically generated. Do not modify. */'; \
208 echo \#define VERSION_CODE $(shell \
209 expr $(VERSION) \* 256 + $(PATCHLEVEL)); \
210 echo '#define EXTRAVERSION ' $(EXTRAVERSION); \
211 echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"'; \
212 echo '#define FILE_VERSION '$(FILE_VERSION); \
213 ) > $1
214endef
215
216define update_version.h
217 ($(call make_version.h, $@.tmp); \
218 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
219 rm -f $@.tmp; \
220 else \
221 echo ' UPDATE $@'; \
222 mv -f $@.tmp $@; \
223 fi);
224endef
225
226ep_version.h: force
227 $(Q)$(N)$(call update_version.h)
228
229VERSION_FILES = ep_version.h
230
231define update_dir
232 (echo $1 > $@.tmp; \
233 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
234 rm -f $@.tmp; \
235 else \
236 echo ' UPDATE $@'; \
237 mv -f $@.tmp $@; \
238 fi);
239endef
240
241## make deps
242
243all_objs := $(sort $(ALL_OBJS))
244all_deps := $(all_objs:%.o=.%.d)
245
Namhyung Kim860df582012-06-22 14:37:36 +0900246# let .d file also depends on the source and header files
Steven Rostedtf7d82352012-04-06 00:47:53 +0200247define check_deps
Namhyung Kim860df582012-06-22 14:37:36 +0900248 @set -e; $(RM) $@; \
Namhyung Kimb6f4f802012-10-26 17:55:48 +0900249 $(CC) -MM $(CFLAGS) $< > $@.$$$$; \
Namhyung Kim860df582012-06-22 14:37:36 +0900250 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
251 $(RM) $@.$$$$
Steven Rostedtf7d82352012-04-06 00:47:53 +0200252endef
253
Steven Rostedtf7d82352012-04-06 00:47:53 +0200254$(all_deps): .%.d: $(src)/%.c
255 $(Q)$(call check_deps)
256
257$(all_objs) : %.o : .%.d
258
259dep_includes := $(wildcard $(all_deps))
260
261ifneq ($(dep_includes),)
262 include $(dep_includes)
263endif
264
Namhyung Kim52b5c0d2012-07-06 16:21:32 +0900265### Detect environment changes
266TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE)
267
268TRACEEVENT-CFLAGS: force
269 @FLAGS='$(TRACK_CFLAGS)'; \
270 if test x"$$FLAGS" != x"`cat TRACEEVENT-CFLAGS 2>/dev/null`" ; then \
271 echo 1>&2 " * new build flags or cross compiler"; \
272 echo "$$FLAGS" >TRACEEVENT-CFLAGS; \
273 fi
274
Steven Rostedtf7d82352012-04-06 00:47:53 +0200275tags: force
276 $(RM) tags
Namhyung Kim6545e3a2012-06-22 17:10:14 +0900277 find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
278 --regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/'
Steven Rostedtf7d82352012-04-06 00:47:53 +0200279
280TAGS: force
281 $(RM) TAGS
Namhyung Kim6545e3a2012-06-22 17:10:14 +0900282 find . -name '*.[ch]' | xargs etags \
283 --regex='/_PE(\([^,)]*\).*/PEVENT_ERRNO__\1/'
Steven Rostedtf7d82352012-04-06 00:47:53 +0200284
285define do_install
286 $(print_install) \
287 if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
288 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
289 fi; \
290 $(INSTALL) $1 '$(DESTDIR_SQ)$2'
291endef
292
Namhyung Kim9f1efa82013-06-04 14:20:16 +0900293install_lib: all_cmd
Steven Rostedtf7d82352012-04-06 00:47:53 +0200294 $(Q)$(call do_install,$(LIB_FILE),$(bindir_SQ))
295
296install: install_lib
297
298clean:
Konstantin Stepanyukf526a4c2012-06-27 15:52:14 -0600299 $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d
Namhyung Kim52b5c0d2012-07-06 16:21:32 +0900300 $(RM) TRACEEVENT-CFLAGS tags TAGS
Steven Rostedtf7d82352012-04-06 00:47:53 +0200301
302endif # skip-makefile
303
304PHONY += force
305force:
306
307# Declare the contents of the .PHONY variable as phony. We keep that
308# information in a variable so we can use it in if_changed and friends.
309.PHONY: $(PHONY)