• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# SPDX-License-Identifier: GPL-2.0
2# trace-cmd version
3EP_VERSION = 1
4EP_PATCHLEVEL = 1
5EP_EXTRAVERSION = 0
6
7# file format version
8FILE_VERSION = 6
9
10MAKEFLAGS += --no-print-directory
11
12
13# Makefiles suck: This macro sets a default value of $(2) for the
14# variable named by $(1), unless the variable has been set by
15# environment or command line. This is necessary for CC and AR
16# because make sets default values, so the simpler ?= approach
17# won't work as expected.
18define allow-override
19  $(if $(or $(findstring environment,$(origin $(1))),\
20            $(findstring command line,$(origin $(1)))),,\
21    $(eval $(1) = $(2)))
22endef
23
24# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
25$(call allow-override,CC,$(CROSS_COMPILE)gcc)
26$(call allow-override,AR,$(CROSS_COMPILE)ar)
27$(call allow-override,NM,$(CROSS_COMPILE)nm)
28$(call allow-override,PKG_CONFIG,pkg-config)
29
30EXT = -std=gnu99
31INSTALL = install
32
33# Use DESTDIR for installing into a different root directory.
34# This is useful for building a package. The program will be
35# installed in this directory as if it was the root directory.
36# Then the build tool can move it later.
37DESTDIR ?=
38DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
39
40LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1)
41ifeq ($(LP64), 1)
42  libdir_relative = lib64
43else
44  libdir_relative = lib
45endif
46
47prefix ?= /usr/local
48libdir = $(prefix)/$(libdir_relative)
49man_dir = $(prefix)/share/man
50man_dir_SQ = '$(subst ','\'',$(man_dir))'
51pkgconfig_dir ?= $(word 1,$(shell $(PKG_CONFIG) 		\
52			--variable pc_path pkg-config | tr ":" " "))
53includedir_relative = traceevent
54includedir = $(prefix)/include/$(includedir_relative)
55includedir_SQ = '$(subst ','\'',$(includedir))'
56
57export man_dir man_dir_SQ INSTALL
58export DESTDIR DESTDIR_SQ
59export EVENT_PARSE_VERSION
60
61include ../../scripts/Makefile.include
62
63# copy a bit from Linux kbuild
64
65ifeq ("$(origin V)", "command line")
66  VERBOSE = $(V)
67endif
68ifndef VERBOSE
69  VERBOSE = 0
70endif
71
72ifeq ($(srctree),)
73srctree := $(patsubst %/,%,$(dir $(CURDIR)))
74srctree := $(patsubst %/,%,$(dir $(srctree)))
75srctree := $(patsubst %/,%,$(dir $(srctree)))
76#$(info Determined 'srctree' to be $(srctree))
77endif
78
79export prefix libdir src obj
80
81# Shell quotes
82libdir_SQ = $(subst ','\'',$(libdir))
83libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
84
85CONFIG_INCLUDES =
86CONFIG_LIBS	=
87CONFIG_FLAGS	=
88
89VERSION		= $(EP_VERSION)
90PATCHLEVEL	= $(EP_PATCHLEVEL)
91EXTRAVERSION	= $(EP_EXTRAVERSION)
92
93OBJ		= $@
94N		=
95
96EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION)
97
98LIB_TARGET  = libtraceevent.a libtraceevent.so.$(EVENT_PARSE_VERSION)
99LIB_INSTALL = libtraceevent.a libtraceevent.so*
100LIB_INSTALL := $(addprefix $(OUTPUT),$(LIB_INSTALL))
101
102INCLUDES = -I. -I $(srctree)/tools/include $(CONFIG_INCLUDES)
103
104# Set compile option CFLAGS
105ifdef EXTRA_CFLAGS
106  CFLAGS := $(EXTRA_CFLAGS)
107else
108  CFLAGS := -g -Wall
109endif
110
111# Append required CFLAGS
112override CFLAGS += -fPIC
113override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
114override CFLAGS += $(udis86-flags) -D_GNU_SOURCE
115
116ifeq ($(VERBOSE),1)
117  Q =
118else
119  Q = @
120endif
121
122# Disable command line variables (CFLAGS) override from top
123# level Makefile (perf), otherwise build Makefile will get
124# the same command line setup.
125MAKEOVERRIDES=
126
127export srctree OUTPUT CC LD CFLAGS V
128build := -f $(srctree)/tools/build/Makefile.build dir=. obj
129
130TE_IN      := $(OUTPUT)libtraceevent-in.o
131LIB_TARGET := $(addprefix $(OUTPUT),$(LIB_TARGET))
132
133CMD_TARGETS = $(LIB_TARGET)
134
135TARGETS = $(CMD_TARGETS)
136
137all: all_cmd plugins
138
139all_cmd: $(CMD_TARGETS)
140
141$(TE_IN): force
142	$(Q)$(MAKE) $(build)=libtraceevent
143
144$(OUTPUT)libtraceevent.so.$(EVENT_PARSE_VERSION): $(TE_IN)
145	$(QUIET_LINK)$(CC) --shared $(LDFLAGS) $^ -Wl,-soname,libtraceevent.so.$(EP_VERSION) -o $@
146	@ln -sf $(@F) $(OUTPUT)libtraceevent.so
147	@ln -sf $(@F) $(OUTPUT)libtraceevent.so.$(EP_VERSION)
148
149$(OUTPUT)libtraceevent.a: $(TE_IN)
150	$(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
151
152$(OUTPUT)%.so: $(OUTPUT)%-in.o
153	$(QUIET_LINK)$(CC) $(CFLAGS) -shared $(LDFLAGS) -nostartfiles -o $@ $^
154
155define make_version.h
156  (echo '/* This file is automatically generated. Do not modify. */';		\
157   echo \#define VERSION_CODE $(shell						\
158   expr $(VERSION) \* 256 + $(PATCHLEVEL));					\
159   echo '#define EXTRAVERSION ' $(EXTRAVERSION);				\
160   echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"';	\
161   echo '#define FILE_VERSION '$(FILE_VERSION);					\
162  ) > $1
163endef
164
165define update_version.h
166  ($(call make_version.h, $@.tmp);		\
167    if [ -r $@ ] && cmp -s $@ $@.tmp; then	\
168      rm -f $@.tmp;				\
169    else					\
170      echo '  UPDATE                 $@';	\
171      mv -f $@.tmp $@;				\
172    fi);
173endef
174
175ep_version.h: force
176	$(Q)$(N)$(call update_version.h)
177
178VERSION_FILES = ep_version.h
179
180define update_dir
181  (echo $1 > $@.tmp;				\
182   if [ -r $@ ] && cmp -s $@ $@.tmp; then	\
183     rm -f $@.tmp;				\
184   else						\
185     echo '  UPDATE                 $@';	\
186     mv -f $@.tmp $@;				\
187   fi);
188endef
189
190tags:	force
191	$(RM) tags
192	find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
193	--regex-c++='/_PE\(([^,)]*).*/TEP_ERRNO__\1/'
194
195TAGS:	force
196	$(RM) TAGS
197	find . -name '*.[ch]' | xargs etags \
198	--regex='/_PE(\([^,)]*\).*/TEP_ERRNO__\1/'
199
200define do_install_mkdir
201	if [ ! -d '$(DESTDIR_SQ)$1' ]; then		\
202		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1';	\
203	fi
204endef
205
206define do_install
207	$(call do_install_mkdir,$2);			\
208	$(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2'
209endef
210
211PKG_CONFIG_SOURCE_FILE = libtraceevent.pc
212PKG_CONFIG_FILE := $(addprefix $(OUTPUT),$(PKG_CONFIG_SOURCE_FILE))
213define do_install_pkgconfig_file
214	if [ -n "${pkgconfig_dir}" ]; then 					\
215		cp -f ${PKG_CONFIG_SOURCE_FILE}.template ${PKG_CONFIG_FILE};	\
216		sed -i "s|INSTALL_PREFIX|${1}|g" ${PKG_CONFIG_FILE}; 		\
217		sed -i "s|LIB_VERSION|${EVENT_PARSE_VERSION}|g" ${PKG_CONFIG_FILE}; \
218		sed -i "s|LIB_DIR|${libdir}|g" ${PKG_CONFIG_FILE}; \
219		sed -i "s|HEADER_DIR|$(includedir)|g" ${PKG_CONFIG_FILE}; \
220		$(call do_install,$(PKG_CONFIG_FILE),$(pkgconfig_dir),644); 	\
221	else 									\
222		(echo Failed to locate pkg-config directory) 1>&2;		\
223	fi
224endef
225
226install_lib: all_cmd install_plugins install_headers install_pkgconfig
227	$(call QUIET_INSTALL, $(LIB_TARGET)) \
228		$(call do_install_mkdir,$(libdir_SQ)); \
229		cp -fpR $(LIB_INSTALL) $(DESTDIR)$(libdir_SQ)
230
231install_pkgconfig:
232	$(call QUIET_INSTALL, $(PKG_CONFIG_FILE)) \
233		$(call do_install_pkgconfig_file,$(prefix))
234
235install_headers:
236	$(call QUIET_INSTALL, headers) \
237		$(call do_install,event-parse.h,$(DESTDIR)$(includedir_SQ),644); \
238		$(call do_install,event-utils.h,$(DESTDIR)$(includedir_SQ),644); \
239		$(call do_install,trace-seq.h,$(DESTDIR)$(includedir_SQ),644); \
240		$(call do_install,kbuffer.h,$(DESTDIR)$(includedir_SQ),644)
241
242install: install_lib
243
244clean: clean_plugins
245	$(call QUIET_CLEAN, libtraceevent) \
246		$(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d .*.cmd; \
247		$(RM) TRACEEVENT-CFLAGS tags TAGS; \
248		$(RM) $(PKG_CONFIG_FILE)
249
250PHONY += doc
251doc:
252	$(call descend,Documentation)
253
254PHONY += doc-clean
255doc-clean:
256	$(call descend,Documentation,clean)
257
258PHONY += doc-install
259doc-install:
260	$(call descend,Documentation,install)
261
262PHONY += doc-uninstall
263doc-uninstall:
264	$(call descend,Documentation,uninstall)
265
266PHONY += help
267help:
268	@echo 'Possible targets:'
269	@echo''
270	@echo '  all                 - default, compile the library and the'\
271				      'plugins'
272	@echo '  plugins             - compile the plugins'
273	@echo '  install             - install the library, the plugins,'\
274					'the header and pkgconfig files'
275	@echo '  clean               - clean the library and the plugins object files'
276	@echo '  doc                 - compile the documentation files - man'\
277					'and html pages, in the Documentation directory'
278	@echo '  doc-clean           - clean the documentation files'
279	@echo '  doc-install         - install the man pages'
280	@echo '  doc-uninstall       - uninstall the man pages'
281	@echo''
282
283PHONY += plugins
284plugins:
285	$(call descend,plugins)
286
287PHONY += install_plugins
288install_plugins:
289	$(call descend,plugins,install)
290
291PHONY += clean_plugins
292clean_plugins:
293	$(call descend,plugins,clean)
294
295force:
296
297# Declare the contents of the .PHONY variable as phony.  We keep that
298# information in a variable so we can use it in if_changed and friends.
299.PHONY: $(PHONY)
300