Makefile 7.8 KB

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