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