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