1# SPDX-License-Identifier: GPL-2.0
2
3#MAKEFLAGS += --no-print-directory
4
5
6# Makefiles suck: This macro sets a default value of $(2) for the
7# variable named by $(1), unless the variable has been set by
8# environment or command line. This is necessary for CC and AR
9# because make sets default values, so the simpler ?= approach
10# won't work as expected.
11define allow-override
12  $(if $(or $(findstring environment,$(origin $(1))),\
13            $(findstring command line,$(origin $(1)))),,\
14    $(eval $(1) = $(2)))
15endef
16
17# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
18$(call allow-override,CC,$(CROSS_COMPILE)gcc)
19$(call allow-override,AR,$(CROSS_COMPILE)ar)
20$(call allow-override,NM,$(CROSS_COMPILE)nm)
21$(call allow-override,PKG_CONFIG,pkg-config)
22
23EXT = -std=gnu99
24INSTALL = install
25
26# Use DESTDIR for installing into a different root directory.
27# This is useful for building a package. The program will be
28# installed in this directory as if it was the root directory.
29# Then the build tool can move it later.
30DESTDIR ?=
31DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
32
33LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1)
34ifeq ($(LP64), 1)
35  libdir_relative_tmp = lib64
36else
37  libdir_relative_tmp = lib
38endif
39
40libdir_relative ?= $(libdir_relative_tmp)
41prefix ?= /usr/local
42libdir = $(prefix)/$(libdir_relative)
43
44set_plugin_dir := 1
45
46# Set plugin_dir to preffered global plugin location
47# If we install under $HOME directory we go under
48# $(HOME)/.local/lib/traceevent/plugins
49#
50# We dont set PLUGIN_DIR in case we install under $HOME
51# directory, because by default the code looks under:
52# $(HOME)/.local/lib/traceevent/plugins by default.
53#
54ifeq ($(plugin_dir),)
55ifeq ($(prefix),$(HOME))
56override plugin_dir = $(HOME)/.local/lib/traceevent/plugins
57set_plugin_dir := 0
58else
59override plugin_dir = $(libdir)/traceevent/plugins
60endif
61endif
62
63ifeq ($(set_plugin_dir),1)
64PLUGIN_DIR = -DPLUGIN_DIR="$(plugin_dir)"
65PLUGIN_DIR_SQ = '$(subst ','\'',$(PLUGIN_DIR))'
66endif
67
68include ../../../scripts/Makefile.include
69
70# copy a bit from Linux kbuild
71
72ifeq ("$(origin V)", "command line")
73  VERBOSE = $(V)
74endif
75ifndef VERBOSE
76  VERBOSE = 0
77endif
78
79ifeq ($(srctree),)
80srctree := $(patsubst %/,%,$(dir $(CURDIR)))
81srctree := $(patsubst %/,%,$(dir $(srctree)))
82srctree := $(patsubst %/,%,$(dir $(srctree)))
83srctree := $(patsubst %/,%,$(dir $(srctree)))
84#$(info Determined 'srctree' to be $(srctree))
85endif
86
87export prefix libdir src obj
88
89# Shell quotes
90plugin_dir_SQ = $(subst ','\'',$(plugin_dir))
91
92CONFIG_INCLUDES =
93CONFIG_LIBS    =
94CONFIG_FLAGS   =
95
96OBJ            = $@
97N              =
98
99INCLUDES = -I. -I.. -I $(srctree)/tools/include $(CONFIG_INCLUDES)
100
101# Set compile option CFLAGS
102ifdef EXTRA_CFLAGS
103  CFLAGS := $(EXTRA_CFLAGS)
104else
105  CFLAGS := -g -Wall
106endif
107
108# Append required CFLAGS
109override CFLAGS += -fPIC
110override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
111override CFLAGS += $(udis86-flags) -D_GNU_SOURCE
112
113ifeq ($(VERBOSE),1)
114  Q =
115else
116  Q = @
117endif
118
119# Disable command line variables (CFLAGS) override from top
120# level Makefile (perf), otherwise build Makefile will get
121# the same command line setup.
122MAKEOVERRIDES=
123
124export srctree OUTPUT CC LD CFLAGS V
125
126build := -f $(srctree)/tools/build/Makefile.build dir=. obj
127
128DYNAMIC_LIST_FILE := $(OUTPUT)libtraceevent-dynamic-list
129
130PLUGINS  = plugin_jbd2.so
131PLUGINS += plugin_hrtimer.so
132PLUGINS += plugin_kmem.so
133PLUGINS += plugin_kvm.so
134PLUGINS += plugin_mac80211.so
135PLUGINS += plugin_sched_switch.so
136PLUGINS += plugin_function.so
137PLUGINS += plugin_futex.so
138PLUGINS += plugin_xen.so
139PLUGINS += plugin_scsi.so
140PLUGINS += plugin_cfg80211.so
141PLUGINS += plugin_tlb.so
142
143PLUGINS    := $(addprefix $(OUTPUT),$(PLUGINS))
144PLUGINS_IN := $(PLUGINS:.so=-in.o)
145
146plugins: $(PLUGINS) $(DYNAMIC_LIST_FILE)
147
148__plugin_obj = $(notdir $@)
149  plugin_obj = $(__plugin_obj:-in.o=)
150
151$(PLUGINS_IN): force
152	$(Q)$(MAKE) $(build)=$(plugin_obj)
153
154$(OUTPUT)libtraceevent-dynamic-list: $(PLUGINS)
155	$(QUIET_GEN)$(call do_generate_dynamic_list_file, $(PLUGINS), $@)
156
157$(OUTPUT)%.so: $(OUTPUT)%-in.o
158	$(QUIET_LINK)$(CC) $(CFLAGS) -shared $(LDFLAGS) -nostartfiles -o $@ $^
159
160define update_dir
161  (echo $1 > $@.tmp;                           \
162   if [ -r $@ ] && cmp -s $@ $@.tmp; then      \
163     rm -f $@.tmp;                             \
164   else                                                \
165     echo '  UPDATE                 $@';       \
166     mv -f $@.tmp $@;                          \
167   fi);
168endef
169
170tags:	force
171	$(RM) tags
172	find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
173	--regex-c++='/_PE\(([^,)]*).*/TEP_ERRNO__\1/'
174
175TAGS:	force
176	$(RM) TAGS
177	find . -name '*.[ch]' | xargs etags \
178	--regex='/_PE(\([^,)]*\).*/TEP_ERRNO__\1/'
179
180define do_install_mkdir
181	if [ ! -d '$(DESTDIR_SQ)$1' ]; then             \
182		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
183	fi
184endef
185
186define do_install
187	$(call do_install_mkdir,$2);                    \
188	$(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2'
189endef
190
191define do_install_plugins
192       for plugin in $1; do                            \
193         $(call do_install,$$plugin,$(plugin_dir_SQ)); \
194       done
195endef
196
197define do_generate_dynamic_list_file
198	symbol_type=`$(NM) -u -D $1 | awk 'NF>1 {print $$1}' | \
199	xargs echo "U w W" | tr 'w ' 'W\n' | sort -u | xargs echo`;\
200	if [ "$$symbol_type" = "U W" ];then				\
201		(echo '{';                                              \
202		$(NM) -u -D $1 | awk 'NF>1 {sub("@.*", "", $$2); print "\t"$$2";"}' | sort -u;\
203		echo '};';                                              \
204		) > $2;                                                 \
205	else                                                            \
206		(echo Either missing one of [$1] or bad version of $(NM)) 1>&2;\
207		fi
208endef
209
210install: $(PLUGINS)
211	$(call QUIET_INSTALL, trace_plugins) \
212	$(call do_install_plugins, $(PLUGINS))
213
214clean:
215	$(call QUIET_CLEAN, trace_plugins) \
216		$(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d .*.cmd; \
217		$(RM) $(OUTPUT)libtraceevent-dynamic-list \
218		$(RM) TRACEEVENT-CFLAGS tags TAGS;
219
220PHONY += force plugins
221force:
222
223# Declare the contents of the .PHONY variable as phony.  We keep that
224# information in a variable so we can use it in if_changed and friends.
225.PHONY: $(PHONY)
226