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*
100
101INCLUDES = -I. -I $(srctree)/tools/include $(CONFIG_INCLUDES)
102
103# Set compile option CFLAGS
104ifdef EXTRA_CFLAGS
105  CFLAGS := $(EXTRA_CFLAGS)
106else
107  CFLAGS := -g -Wall
108endif
109
110# Append required CFLAGS
111override CFLAGS += -fPIC
112override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
113override CFLAGS += $(udis86-flags) -D_GNU_SOURCE
114
115ifeq ($(VERBOSE),1)
116  Q =
117else
118  Q = @
119endif
120
121# Disable command line variables (CFLAGS) override from top
122# level Makefile (perf), otherwise build Makefile will get
123# the same command line setup.
124MAKEOVERRIDES=
125
126export srctree OUTPUT CC LD CFLAGS V
127build := -f $(srctree)/tools/build/Makefile.build dir=. obj
128
129TE_IN      := $(OUTPUT)libtraceevent-in.o
130LIB_TARGET := $(addprefix $(OUTPUT),$(LIB_TARGET))
131
132CMD_TARGETS = $(LIB_TARGET)
133
134TARGETS = $(CMD_TARGETS)
135
136all: all_cmd plugins
137
138all_cmd: $(CMD_TARGETS)
139
140$(TE_IN): force
141	$(Q)$(MAKE) $(build)=libtraceevent
142
143$(OUTPUT)libtraceevent.so.$(EVENT_PARSE_VERSION): $(TE_IN)
144	$(QUIET_LINK)$(CC) --shared $(LDFLAGS) $^ -Wl,-soname,libtraceevent.so.$(EP_VERSION) -o $@
145	@ln -sf $(@F) $(OUTPUT)libtraceevent.so
146	@ln -sf $(@F) $(OUTPUT)libtraceevent.so.$(EP_VERSION)
147
148$(OUTPUT)libtraceevent.a: $(TE_IN)
149	$(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
150
151$(OUTPUT)%.so: $(OUTPUT)%-in.o
152	$(QUIET_LINK)$(CC) $(CFLAGS) -shared $(LDFLAGS) -nostartfiles -o $@ $^
153
154define make_version.h
155  (echo '/* This file is automatically generated. Do not modify. */';		\
156   echo \#define VERSION_CODE $(shell						\
157   expr $(VERSION) \* 256 + $(PATCHLEVEL));					\
158   echo '#define EXTRAVERSION ' $(EXTRAVERSION);				\
159   echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"';	\
160   echo '#define FILE_VERSION '$(FILE_VERSION);					\
161  ) > $1
162endef
163
164define update_version.h
165  ($(call make_version.h, $@.tmp);		\
166    if [ -r $@ ] && cmp -s $@ $@.tmp; then	\
167      rm -f $@.tmp;				\
168    else					\
169      echo '  UPDATE                 $@';	\
170      mv -f $@.tmp $@;				\
171    fi);
172endef
173
174ep_version.h: force
175	$(Q)$(N)$(call update_version.h)
176
177VERSION_FILES = ep_version.h
178
179define update_dir
180  (echo $1 > $@.tmp;				\
181   if [ -r $@ ] && cmp -s $@ $@.tmp; then	\
182     rm -f $@.tmp;				\
183   else						\
184     echo '  UPDATE                 $@';	\
185     mv -f $@.tmp $@;				\
186   fi);
187endef
188
189tags:	force
190	$(RM) tags
191	find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
192	--regex-c++='/_PE\(([^,)]*).*/TEP_ERRNO__\1/'
193
194TAGS:	force
195	$(RM) TAGS
196	find . -name '*.[ch]' | xargs etags \
197	--regex='/_PE(\([^,)]*\).*/TEP_ERRNO__\1/'
198
199define do_install_mkdir
200	if [ ! -d '$(DESTDIR_SQ)$1' ]; then		\
201		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1';	\
202	fi
203endef
204
205define do_install
206	$(call do_install_mkdir,$2);			\
207	$(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2'
208endef
209
210PKG_CONFIG_FILE = libtraceevent.pc
211define do_install_pkgconfig_file
212	if [ -n "${pkgconfig_dir}" ]; then 					\
213		cp -f ${PKG_CONFIG_FILE}.template ${PKG_CONFIG_FILE}; 		\
214		sed -i "s|INSTALL_PREFIX|${1}|g" ${PKG_CONFIG_FILE}; 		\
215		sed -i "s|LIB_VERSION|${EVENT_PARSE_VERSION}|g" ${PKG_CONFIG_FILE}; \
216		sed -i "s|LIB_DIR|${libdir}|g" ${PKG_CONFIG_FILE}; \
217		sed -i "s|HEADER_DIR|$(includedir)|g" ${PKG_CONFIG_FILE}; \
218		$(call do_install,$(PKG_CONFIG_FILE),$(pkgconfig_dir),644); 	\
219	else 									\
220		(echo Failed to locate pkg-config directory) 1>&2;		\
221	fi
222endef
223
224install_lib: all_cmd install_plugins install_headers install_pkgconfig
225	$(call QUIET_INSTALL, $(LIB_TARGET)) \
226		$(call do_install_mkdir,$(libdir_SQ)); \
227		cp -fpR $(LIB_INSTALL) $(DESTDIR)$(libdir_SQ)
228
229install_pkgconfig:
230	$(call QUIET_INSTALL, $(PKG_CONFIG_FILE)) \
231		$(call do_install_pkgconfig_file,$(prefix))
232
233install_headers:
234	$(call QUIET_INSTALL, headers) \
235		$(call do_install,event-parse.h,$(DESTDIR)$(includedir_SQ),644); \
236		$(call do_install,event-utils.h,$(DESTDIR)$(includedir_SQ),644); \
237		$(call do_install,trace-seq.h,$(DESTDIR)$(includedir_SQ),644); \
238		$(call do_install,kbuffer.h,$(DESTDIR)$(includedir_SQ),644)
239
240install: install_lib
241
242clean: clean_plugins
243	$(call QUIET_CLEAN, libtraceevent) \
244		$(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d .*.cmd; \
245		$(RM) TRACEEVENT-CFLAGS tags TAGS; \
246		$(RM) $(PKG_CONFIG_FILE)
247
248PHONY += doc
249doc:
250	$(call descend,Documentation)
251
252PHONY += doc-clean
253doc-clean:
254	$(call descend,Documentation,clean)
255
256PHONY += doc-install
257doc-install:
258	$(call descend,Documentation,install)
259
260PHONY += doc-uninstall
261doc-uninstall:
262	$(call descend,Documentation,uninstall)
263
264PHONY += help
265help:
266	@echo 'Possible targets:'
267	@echo''
268	@echo '  all                 - default, compile the library and the'\
269				      'plugins'
270	@echo '  plugins             - compile the plugins'
271	@echo '  install             - install the library, the plugins,'\
272					'the header and pkgconfig files'
273	@echo '  clean               - clean the library and the plugins object files'
274	@echo '  doc                 - compile the documentation files - man'\
275					'and html pages, in the Documentation directory'
276	@echo '  doc-clean           - clean the documentation files'
277	@echo '  doc-install         - install the man pages'
278	@echo '  doc-uninstall       - uninstall the man pages'
279	@echo''
280
281PHONY += plugins
282plugins:
283	$(call descend,plugins)
284
285PHONY += install_plugins
286install_plugins:
287	$(call descend,plugins,install)
288
289PHONY += clean_plugins
290clean_plugins:
291	$(call descend,plugins,clean)
292
293force:
294
295# Declare the contents of the .PHONY variable as phony.  We keep that
296# information in a variable so we can use it in if_changed and friends.
297.PHONY: $(PHONY)
298