rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | # SPDX-License-Identifier: GPL-2.0 |
| 2 | # trace-cmd version |
| 3 | EP_VERSION = 1 |
| 4 | EP_PATCHLEVEL = 1 |
| 5 | EP_EXTRAVERSION = 0 |
| 6 | |
| 7 | # file format version |
| 8 | FILE_VERSION = 6 |
| 9 | |
| 10 | MAKEFLAGS += --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. |
| 18 | define allow-override |
| 19 | $(if $(or $(findstring environment,$(origin $(1))),\ |
| 20 | $(findstring command line,$(origin $(1)))),,\ |
| 21 | $(eval $(1) = $(2))) |
| 22 | endef |
| 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 | |
| 29 | EXT = -std=gnu99 |
| 30 | INSTALL = install |
| 31 | |
| 32 | # Use DESTDIR for installing into a different root directory. |
| 33 | # This is useful for building a package. The program will be |
| 34 | # installed in this directory as if it was the root directory. |
| 35 | # Then the build tool can move it later. |
| 36 | DESTDIR ?= |
| 37 | DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))' |
| 38 | |
| 39 | LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1) |
| 40 | ifeq ($(LP64), 1) |
| 41 | libdir_relative = lib64 |
| 42 | else |
| 43 | libdir_relative = lib |
| 44 | endif |
| 45 | |
| 46 | prefix ?= /usr/local |
| 47 | libdir = $(prefix)/$(libdir_relative) |
| 48 | man_dir = $(prefix)/share/man |
| 49 | man_dir_SQ = '$(subst ','\'',$(man_dir))' |
| 50 | |
| 51 | export man_dir man_dir_SQ INSTALL |
| 52 | export DESTDIR DESTDIR_SQ |
| 53 | |
| 54 | set_plugin_dir := 1 |
| 55 | |
| 56 | # Set plugin_dir to preffered global plugin location |
| 57 | # If we install under $HOME directory we go under |
| 58 | # $(HOME)/.local/lib/traceevent/plugins |
| 59 | # |
| 60 | # We dont set PLUGIN_DIR in case we install under $HOME |
| 61 | # directory, because by default the code looks under: |
| 62 | # $(HOME)/.local/lib/traceevent/plugins by default. |
| 63 | # |
| 64 | ifeq ($(plugin_dir),) |
| 65 | ifeq ($(prefix),$(HOME)) |
| 66 | override plugin_dir = $(HOME)/.local/lib/traceevent/plugins |
| 67 | set_plugin_dir := 0 |
| 68 | else |
| 69 | override plugin_dir = $(libdir)/traceevent/plugins |
| 70 | endif |
| 71 | endif |
| 72 | |
| 73 | ifeq ($(set_plugin_dir),1) |
| 74 | PLUGIN_DIR = -DPLUGIN_DIR="$(plugin_dir)" |
| 75 | PLUGIN_DIR_SQ = '$(subst ','\'',$(PLUGIN_DIR))' |
| 76 | endif |
| 77 | |
| 78 | include ../../scripts/Makefile.include |
| 79 | |
| 80 | # copy a bit from Linux kbuild |
| 81 | |
| 82 | ifeq ("$(origin V)", "command line") |
| 83 | VERBOSE = $(V) |
| 84 | endif |
| 85 | ifndef VERBOSE |
| 86 | VERBOSE = 0 |
| 87 | endif |
| 88 | |
| 89 | ifeq ($(srctree),) |
| 90 | srctree := $(patsubst %/,%,$(dir $(CURDIR))) |
| 91 | srctree := $(patsubst %/,%,$(dir $(srctree))) |
| 92 | srctree := $(patsubst %/,%,$(dir $(srctree))) |
| 93 | #$(info Determined 'srctree' to be $(srctree)) |
| 94 | endif |
| 95 | |
| 96 | export prefix libdir src obj |
| 97 | |
| 98 | # Shell quotes |
| 99 | libdir_SQ = $(subst ','\'',$(libdir)) |
| 100 | libdir_relative_SQ = $(subst ','\'',$(libdir_relative)) |
| 101 | plugin_dir_SQ = $(subst ','\'',$(plugin_dir)) |
| 102 | |
| 103 | CONFIG_INCLUDES = |
| 104 | CONFIG_LIBS = |
| 105 | CONFIG_FLAGS = |
| 106 | |
| 107 | VERSION = $(EP_VERSION) |
| 108 | PATCHLEVEL = $(EP_PATCHLEVEL) |
| 109 | EXTRAVERSION = $(EP_EXTRAVERSION) |
| 110 | |
| 111 | OBJ = $@ |
| 112 | N = |
| 113 | |
| 114 | EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION) |
| 115 | |
| 116 | LIB_TARGET = libtraceevent.a libtraceevent.so.$(EVENT_PARSE_VERSION) |
| 117 | LIB_INSTALL = libtraceevent.a libtraceevent.so* |
| 118 | LIB_INSTALL := $(addprefix $(OUTPUT),$(LIB_INSTALL)) |
| 119 | |
| 120 | INCLUDES = -I. -I $(srctree)/tools/include $(CONFIG_INCLUDES) |
| 121 | |
| 122 | # Set compile option CFLAGS |
| 123 | ifdef EXTRA_CFLAGS |
| 124 | CFLAGS := $(EXTRA_CFLAGS) |
| 125 | else |
| 126 | CFLAGS := -g -Wall |
| 127 | endif |
| 128 | |
| 129 | # Append required CFLAGS |
| 130 | override CFLAGS += -fPIC |
| 131 | override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ) |
| 132 | override CFLAGS += $(udis86-flags) -D_GNU_SOURCE |
| 133 | |
| 134 | ifeq ($(VERBOSE),1) |
| 135 | Q = |
| 136 | else |
| 137 | Q = @ |
| 138 | endif |
| 139 | |
| 140 | # Disable command line variables (CFLAGS) override from top |
| 141 | # level Makefile (perf), otherwise build Makefile will get |
| 142 | # the same command line setup. |
| 143 | MAKEOVERRIDES= |
| 144 | |
| 145 | export srctree OUTPUT CC LD CFLAGS V |
| 146 | build := -f $(srctree)/tools/build/Makefile.build dir=. obj |
| 147 | |
| 148 | PLUGINS = plugin_jbd2.so |
| 149 | PLUGINS += plugin_hrtimer.so |
| 150 | PLUGINS += plugin_kmem.so |
| 151 | PLUGINS += plugin_kvm.so |
| 152 | PLUGINS += plugin_mac80211.so |
| 153 | PLUGINS += plugin_sched_switch.so |
| 154 | PLUGINS += plugin_function.so |
| 155 | PLUGINS += plugin_xen.so |
| 156 | PLUGINS += plugin_scsi.so |
| 157 | PLUGINS += plugin_cfg80211.so |
| 158 | |
| 159 | PLUGINS := $(addprefix $(OUTPUT),$(PLUGINS)) |
| 160 | PLUGINS_IN := $(PLUGINS:.so=-in.o) |
| 161 | |
| 162 | TE_IN := $(OUTPUT)libtraceevent-in.o |
| 163 | LIB_TARGET := $(addprefix $(OUTPUT),$(LIB_TARGET)) |
| 164 | DYNAMIC_LIST_FILE := $(OUTPUT)libtraceevent-dynamic-list |
| 165 | |
| 166 | CMD_TARGETS = $(LIB_TARGET) $(PLUGINS) $(DYNAMIC_LIST_FILE) |
| 167 | |
| 168 | TARGETS = $(CMD_TARGETS) |
| 169 | |
| 170 | all: all_cmd |
| 171 | |
| 172 | all_cmd: $(CMD_TARGETS) |
| 173 | |
| 174 | $(TE_IN): force |
| 175 | $(Q)$(MAKE) $(build)=libtraceevent |
| 176 | |
| 177 | $(OUTPUT)libtraceevent.so.$(EVENT_PARSE_VERSION): $(TE_IN) |
| 178 | $(QUIET_LINK)$(CC) --shared $^ -Wl,-soname,libtraceevent.so.$(EP_VERSION) -o $@ |
| 179 | @ln -sf $(@F) $(OUTPUT)libtraceevent.so |
| 180 | @ln -sf $(@F) $(OUTPUT)libtraceevent.so.$(EP_VERSION) |
| 181 | |
| 182 | $(OUTPUT)libtraceevent.a: $(TE_IN) |
| 183 | $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ |
| 184 | |
| 185 | $(OUTPUT)libtraceevent-dynamic-list: $(PLUGINS) |
| 186 | $(QUIET_GEN)$(call do_generate_dynamic_list_file, $(PLUGINS), $@) |
| 187 | |
| 188 | plugins: $(PLUGINS) |
| 189 | |
| 190 | __plugin_obj = $(notdir $@) |
| 191 | plugin_obj = $(__plugin_obj:-in.o=) |
| 192 | |
| 193 | $(PLUGINS_IN): force |
| 194 | $(Q)$(MAKE) $(build)=$(plugin_obj) |
| 195 | |
| 196 | $(OUTPUT)%.so: $(OUTPUT)%-in.o |
| 197 | $(QUIET_LINK)$(CC) $(CFLAGS) -shared -nostartfiles -o $@ $^ |
| 198 | |
| 199 | define make_version.h |
| 200 | (echo '/* This file is automatically generated. Do not modify. */'; \ |
| 201 | echo \#define VERSION_CODE $(shell \ |
| 202 | expr $(VERSION) \* 256 + $(PATCHLEVEL)); \ |
| 203 | echo '#define EXTRAVERSION ' $(EXTRAVERSION); \ |
| 204 | echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"'; \ |
| 205 | echo '#define FILE_VERSION '$(FILE_VERSION); \ |
| 206 | ) > $1 |
| 207 | endef |
| 208 | |
| 209 | define update_version.h |
| 210 | ($(call make_version.h, $@.tmp); \ |
| 211 | if [ -r $@ ] && cmp -s $@ $@.tmp; then \ |
| 212 | rm -f $@.tmp; \ |
| 213 | else \ |
| 214 | echo ' UPDATE $@'; \ |
| 215 | mv -f $@.tmp $@; \ |
| 216 | fi); |
| 217 | endef |
| 218 | |
| 219 | ep_version.h: force |
| 220 | $(Q)$(N)$(call update_version.h) |
| 221 | |
| 222 | VERSION_FILES = ep_version.h |
| 223 | |
| 224 | define update_dir |
| 225 | (echo $1 > $@.tmp; \ |
| 226 | if [ -r $@ ] && cmp -s $@ $@.tmp; then \ |
| 227 | rm -f $@.tmp; \ |
| 228 | else \ |
| 229 | echo ' UPDATE $@'; \ |
| 230 | mv -f $@.tmp $@; \ |
| 231 | fi); |
| 232 | endef |
| 233 | |
| 234 | tags: force |
| 235 | $(RM) tags |
| 236 | find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \ |
| 237 | --regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/' |
| 238 | |
| 239 | TAGS: force |
| 240 | $(RM) TAGS |
| 241 | find . -name '*.[ch]' | xargs etags \ |
| 242 | --regex='/_PE(\([^,)]*\).*/PEVENT_ERRNO__\1/' |
| 243 | |
| 244 | define do_install_mkdir |
| 245 | if [ ! -d '$(DESTDIR_SQ)$1' ]; then \ |
| 246 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \ |
| 247 | fi |
| 248 | endef |
| 249 | |
| 250 | define do_install |
| 251 | $(call do_install_mkdir,$2); \ |
| 252 | $(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2' |
| 253 | endef |
| 254 | |
| 255 | define do_install_plugins |
| 256 | for plugin in $1; do \ |
| 257 | $(call do_install,$$plugin,$(plugin_dir_SQ)); \ |
| 258 | done |
| 259 | endef |
| 260 | |
| 261 | define do_generate_dynamic_list_file |
| 262 | symbol_type=`$(NM) -u -D $1 | awk 'NF>1 {print $$1}' | \ |
| 263 | xargs echo "U w W" | tr 'w ' 'W\n' | sort -u | xargs echo`;\ |
| 264 | if [ "$$symbol_type" = "U W" ];then \ |
| 265 | (echo '{'; \ |
| 266 | $(NM) -u -D $1 | awk 'NF>1 {print "\t"$$2";"}' | sort -u;\ |
| 267 | echo '};'; \ |
| 268 | ) > $2; \ |
| 269 | else \ |
| 270 | (echo Either missing one of [$1] or bad version of $(NM)) 1>&2;\ |
| 271 | fi |
| 272 | endef |
| 273 | |
| 274 | install_lib: all_cmd install_plugins |
| 275 | $(call QUIET_INSTALL, $(LIB_TARGET)) \ |
| 276 | $(call do_install_mkdir,$(libdir_SQ)); \ |
| 277 | cp -fpR $(LIB_INSTALL) $(DESTDIR)$(libdir_SQ) |
| 278 | |
| 279 | install_plugins: $(PLUGINS) |
| 280 | $(call QUIET_INSTALL, trace_plugins) \ |
| 281 | $(call do_install_plugins, $(PLUGINS)) |
| 282 | |
| 283 | install_headers: |
| 284 | $(call QUIET_INSTALL, headers) \ |
| 285 | $(call do_install,event-parse.h,$(prefix)/include/traceevent,644); \ |
| 286 | $(call do_install,event-utils.h,$(prefix)/include/traceevent,644); \ |
| 287 | $(call do_install,kbuffer.h,$(prefix)/include/traceevent,644) |
| 288 | |
| 289 | install: install_lib |
| 290 | |
| 291 | clean: |
| 292 | $(call QUIET_CLEAN, libtraceevent) \ |
| 293 | $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d .*.cmd \ |
| 294 | $(RM) TRACEEVENT-CFLAGS tags TAGS |
| 295 | |
| 296 | PHONY += force plugins |
| 297 | force: |
| 298 | |
| 299 | # Declare the contents of the .PHONY variable as phony. We keep that |
| 300 | # information in a variable so we can use it in if_changed and friends. |
| 301 | .PHONY: $(PHONY) |