blob: fd7510d99583a1ef2ca7145409114d86ae6dda3e [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
74gui: force
75 $(call build_output, all_cmd)
76
77$(filter-out gui,$(MAKECMDGOALS)): sub-make
78
79sub-make: force
80 $(call build_output, $(MAKECMDGOALS))
81
82
83# Leave processing to above invocation of make
84skip-makefile := 1
85
86endif # BUILD_OUTPUT
87endif # BUILD_SRC
88
89# We process the rest of the Makefile if this is the final invocation of make
90ifeq ($(skip-makefile),)
91
92srctree := $(if $(BUILD_SRC),$(BUILD_SRC),$(CURDIR))
93objtree := $(CURDIR)
94src := $(srctree)
95obj := $(objtree)
96
97export prefix bindir src obj
98
99# Shell quotes
100bindir_SQ = $(subst ','\'',$(bindir))
101bindir_relative_SQ = $(subst ','\'',$(bindir_relative))
102
103LIB_FILE = libtraceevent.a libtraceevent.so
104
105CONFIG_INCLUDES =
106CONFIG_LIBS =
107CONFIG_FLAGS =
108
109VERSION = $(EP_VERSION)
110PATCHLEVEL = $(EP_PATCHLEVEL)
111EXTRAVERSION = $(EP_EXTRAVERSION)
112
113OBJ = $@
114N =
115
116export Q VERBOSE
117
118EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION)
119
Jack Mitchellb9e8c372013-03-08 11:21:52 +0000120INCLUDES = -I. $(CONFIG_INCLUDES)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200121
122# Set compile option CFLAGS if not set elsewhere
123CFLAGS ?= -g -Wall
124
125# Append required CFLAGS
126override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
Arnaldo Carvalho de Meloc0558752012-09-12 14:29:40 -0300127override CFLAGS += $(udis86-flags) -D_GNU_SOURCE
Steven Rostedtf7d82352012-04-06 00:47:53 +0200128
129ifeq ($(VERBOSE),1)
130 Q =
131 print_compile =
132 print_app_build =
133 print_fpic_compile =
134 print_shared_lib_compile =
135 print_plugin_obj_compile =
136 print_plugin_build =
137 print_install =
138else
139 Q = @
140 print_compile = echo ' CC '$(OBJ);
141 print_app_build = echo ' BUILD '$(OBJ);
142 print_fpic_compile = echo ' CC FPIC '$(OBJ);
143 print_shared_lib_compile = echo ' BUILD SHARED LIB '$(OBJ);
144 print_plugin_obj_compile = echo ' CC PLUGIN OBJ '$(OBJ);
145 print_plugin_build = echo ' CC PLUGI '$(OBJ);
146 print_static_lib_build = echo ' BUILD STATIC LIB '$(OBJ);
147 print_install = echo ' INSTALL '$1' to $(DESTDIR_SQ)$2';
148endif
149
150do_fpic_compile = \
151 ($(print_fpic_compile) \
152 $(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@)
153
154do_app_build = \
155 ($(print_app_build) \
156 $(CC) $^ -rdynamic -o $@ $(CONFIG_LIBS) $(LIBS))
157
158do_compile_shared_library = \
159 ($(print_shared_lib_compile) \
160 $(CC) --shared $^ -o $@)
161
162do_compile_plugin_obj = \
163 ($(print_plugin_obj_compile) \
164 $(CC) -c $(CFLAGS) -fPIC -o $@ $<)
165
166do_plugin_build = \
167 ($(print_plugin_build) \
168 $(CC) $(CFLAGS) -shared -nostartfiles -o $@ $<)
169
170do_build_static_lib = \
171 ($(print_static_lib_build) \
172 $(RM) $@; $(AR) rcs $@ $^)
173
174
175define do_compile
176 $(print_compile) \
177 $(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@;
178endef
179
180$(obj)/%.o: $(src)/%.c
181 $(Q)$(call do_compile)
182
183%.o: $(src)/%.c
184 $(Q)$(call do_compile)
185
186PEVENT_LIB_OBJS = event-parse.o trace-seq.o parse-filter.o parse-utils.o
187
188ALL_OBJS = $(PEVENT_LIB_OBJS)
189
190CMD_TARGETS = $(LIB_FILE)
191
192TARGETS = $(CMD_TARGETS)
193
194
195all: all_cmd
196
197all_cmd: $(CMD_TARGETS)
198
199libtraceevent.so: $(PEVENT_LIB_OBJS)
200 $(Q)$(do_compile_shared_library)
201
202libtraceevent.a: $(PEVENT_LIB_OBJS)
203 $(Q)$(do_build_static_lib)
204
Namhyung Kim52b5c0d2012-07-06 16:21:32 +0900205$(PEVENT_LIB_OBJS): %.o: $(src)/%.c TRACEEVENT-CFLAGS
Steven Rostedtf7d82352012-04-06 00:47:53 +0200206 $(Q)$(do_fpic_compile)
207
208define make_version.h
209 (echo '/* This file is automatically generated. Do not modify. */'; \
210 echo \#define VERSION_CODE $(shell \
211 expr $(VERSION) \* 256 + $(PATCHLEVEL)); \
212 echo '#define EXTRAVERSION ' $(EXTRAVERSION); \
213 echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"'; \
214 echo '#define FILE_VERSION '$(FILE_VERSION); \
215 ) > $1
216endef
217
218define update_version.h
219 ($(call make_version.h, $@.tmp); \
220 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
221 rm -f $@.tmp; \
222 else \
223 echo ' UPDATE $@'; \
224 mv -f $@.tmp $@; \
225 fi);
226endef
227
228ep_version.h: force
229 $(Q)$(N)$(call update_version.h)
230
231VERSION_FILES = ep_version.h
232
233define update_dir
234 (echo $1 > $@.tmp; \
235 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
236 rm -f $@.tmp; \
237 else \
238 echo ' UPDATE $@'; \
239 mv -f $@.tmp $@; \
240 fi);
241endef
242
243## make deps
244
245all_objs := $(sort $(ALL_OBJS))
246all_deps := $(all_objs:%.o=.%.d)
247
Namhyung Kim860df582012-06-22 14:37:36 +0900248# let .d file also depends on the source and header files
Steven Rostedtf7d82352012-04-06 00:47:53 +0200249define check_deps
Namhyung Kim860df582012-06-22 14:37:36 +0900250 @set -e; $(RM) $@; \
Namhyung Kimb6f4f802012-10-26 17:55:48 +0900251 $(CC) -MM $(CFLAGS) $< > $@.$$$$; \
Namhyung Kim860df582012-06-22 14:37:36 +0900252 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
253 $(RM) $@.$$$$
Steven Rostedtf7d82352012-04-06 00:47:53 +0200254endef
255
256$(gui_deps): ks_version.h
257$(non_gui_deps): tc_version.h
258
259$(all_deps): .%.d: $(src)/%.c
260 $(Q)$(call check_deps)
261
262$(all_objs) : %.o : .%.d
263
264dep_includes := $(wildcard $(all_deps))
265
266ifneq ($(dep_includes),)
267 include $(dep_includes)
268endif
269
Namhyung Kim52b5c0d2012-07-06 16:21:32 +0900270### Detect environment changes
271TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE)
272
273TRACEEVENT-CFLAGS: force
274 @FLAGS='$(TRACK_CFLAGS)'; \
275 if test x"$$FLAGS" != x"`cat TRACEEVENT-CFLAGS 2>/dev/null`" ; then \
276 echo 1>&2 " * new build flags or cross compiler"; \
277 echo "$$FLAGS" >TRACEEVENT-CFLAGS; \
278 fi
279
Steven Rostedtf7d82352012-04-06 00:47:53 +0200280tags: force
281 $(RM) tags
Namhyung Kim6545e3a2012-06-22 17:10:14 +0900282 find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
283 --regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/'
Steven Rostedtf7d82352012-04-06 00:47:53 +0200284
285TAGS: force
286 $(RM) TAGS
Namhyung Kim6545e3a2012-06-22 17:10:14 +0900287 find . -name '*.[ch]' | xargs etags \
288 --regex='/_PE(\([^,)]*\).*/PEVENT_ERRNO__\1/'
Steven Rostedtf7d82352012-04-06 00:47:53 +0200289
290define do_install
291 $(print_install) \
292 if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
293 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
294 fi; \
295 $(INSTALL) $1 '$(DESTDIR_SQ)$2'
296endef
297
Namhyung Kim9f1efa82013-06-04 14:20:16 +0900298install_lib: all_cmd
Steven Rostedtf7d82352012-04-06 00:47:53 +0200299 $(Q)$(call do_install,$(LIB_FILE),$(bindir_SQ))
300
301install: install_lib
302
303clean:
Konstantin Stepanyukf526a4c2012-06-27 15:52:14 -0600304 $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d
Namhyung Kim52b5c0d2012-07-06 16:21:32 +0900305 $(RM) TRACEEVENT-CFLAGS tags TAGS
Steven Rostedtf7d82352012-04-06 00:47:53 +0200306
307endif # skip-makefile
308
309PHONY += force
310force:
311
312# Declare the contents of the .PHONY variable as phony. We keep that
313# information in a variable so we can use it in if_changed and friends.
314.PHONY: $(PHONY)