Makefile 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. $(call allow-override,PKG_CONFIG,pkg-config)
  24. EXT = -std=gnu99
  25. INSTALL = install
  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. LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1)
  33. ifeq ($(LP64), 1)
  34. libdir_relative_temp = lib64
  35. else
  36. libdir_relative_temp = lib
  37. endif
  38. libdir_relative ?= $(libdir_relative_temp)
  39. prefix ?= /usr/local
  40. libdir = $(prefix)/$(libdir_relative)
  41. man_dir = $(prefix)/share/man
  42. man_dir_SQ = '$(subst ','\'',$(man_dir))'
  43. pkgconfig_dir ?= $(word 1,$(shell $(PKG_CONFIG) \
  44. --variable pc_path pkg-config | tr ":" " "))
  45. includedir_relative = traceevent
  46. includedir = $(prefix)/include/$(includedir_relative)
  47. includedir_SQ = '$(subst ','\'',$(includedir))'
  48. export man_dir man_dir_SQ INSTALL
  49. export DESTDIR DESTDIR_SQ
  50. export EVENT_PARSE_VERSION
  51. include ../../scripts/Makefile.include
  52. # copy a bit from Linux kbuild
  53. ifeq ("$(origin V)", "command line")
  54. VERBOSE = $(V)
  55. endif
  56. ifndef VERBOSE
  57. VERBOSE = 0
  58. endif
  59. ifeq ($(srctree),)
  60. srctree := $(patsubst %/,%,$(dir $(CURDIR)))
  61. srctree := $(patsubst %/,%,$(dir $(srctree)))
  62. srctree := $(patsubst %/,%,$(dir $(srctree)))
  63. #$(info Determined 'srctree' to be $(srctree))
  64. endif
  65. export prefix libdir src obj
  66. # Shell quotes
  67. libdir_SQ = $(subst ','\'',$(libdir))
  68. libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
  69. CONFIG_INCLUDES =
  70. CONFIG_LIBS =
  71. CONFIG_FLAGS =
  72. VERSION = $(EP_VERSION)
  73. PATCHLEVEL = $(EP_PATCHLEVEL)
  74. EXTRAVERSION = $(EP_EXTRAVERSION)
  75. OBJ = $@
  76. N =
  77. EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION)
  78. LIB_TARGET = libtraceevent.a libtraceevent.so.$(EVENT_PARSE_VERSION)
  79. LIB_INSTALL = libtraceevent.a libtraceevent.so*
  80. LIB_INSTALL := $(addprefix $(OUTPUT),$(LIB_INSTALL))
  81. INCLUDES = -I. -I $(srctree)/tools/include $(CONFIG_INCLUDES)
  82. # Set compile option CFLAGS
  83. ifdef EXTRA_CFLAGS
  84. CFLAGS := $(EXTRA_CFLAGS)
  85. else
  86. CFLAGS := -g -Wall
  87. endif
  88. # Append required CFLAGS
  89. override CFLAGS += -fPIC
  90. override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
  91. override CFLAGS += $(udis86-flags) -D_GNU_SOURCE
  92. ifeq ($(VERBOSE),1)
  93. Q =
  94. else
  95. Q = @
  96. endif
  97. # Disable command line variables (CFLAGS) override from top
  98. # level Makefile (perf), otherwise build Makefile will get
  99. # the same command line setup.
  100. MAKEOVERRIDES=
  101. export srctree OUTPUT CC LD CFLAGS V
  102. build := -f $(srctree)/tools/build/Makefile.build dir=. obj
  103. TE_IN := $(OUTPUT)libtraceevent-in.o
  104. LIB_TARGET := $(addprefix $(OUTPUT),$(LIB_TARGET))
  105. CMD_TARGETS = $(LIB_TARGET)
  106. TARGETS = $(CMD_TARGETS)
  107. all: all_cmd plugins
  108. all_cmd: $(CMD_TARGETS)
  109. $(TE_IN): force
  110. $(Q)$(MAKE) $(build)=libtraceevent
  111. $(OUTPUT)libtraceevent.so.$(EVENT_PARSE_VERSION): $(TE_IN)
  112. $(QUIET_LINK)$(CC) --shared $(LDFLAGS) $^ -Wl,-soname,libtraceevent.so.$(EP_VERSION) -o $@
  113. @ln -sf $(@F) $(OUTPUT)libtraceevent.so
  114. @ln -sf $(@F) $(OUTPUT)libtraceevent.so.$(EP_VERSION)
  115. $(OUTPUT)libtraceevent.a: $(TE_IN)
  116. $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
  117. $(OUTPUT)%.so: $(OUTPUT)%-in.o
  118. $(QUIET_LINK)$(CC) $(CFLAGS) -shared $(LDFLAGS) -nostartfiles -o $@ $^
  119. define make_version.h
  120. (echo '/* This file is automatically generated. Do not modify. */'; \
  121. echo \#define VERSION_CODE $(shell \
  122. expr $(VERSION) \* 256 + $(PATCHLEVEL)); \
  123. echo '#define EXTRAVERSION ' $(EXTRAVERSION); \
  124. echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"'; \
  125. echo '#define FILE_VERSION '$(FILE_VERSION); \
  126. ) > $1
  127. endef
  128. define update_version.h
  129. ($(call make_version.h, [email protected]); \
  130. if [ -r $@ ] && cmp -s $@ [email protected]; then \
  131. rm -f [email protected]; \
  132. else \
  133. echo ' UPDATE $@'; \
  134. mv -f [email protected] $@; \
  135. fi);
  136. endef
  137. ep_version.h: force
  138. $(Q)$(N)$(call update_version.h)
  139. VERSION_FILES = ep_version.h
  140. define update_dir
  141. (echo $1 > [email protected]; \
  142. if [ -r $@ ] && cmp -s $@ [email protected]; then \
  143. rm -f [email protected]; \
  144. else \
  145. echo ' UPDATE $@'; \
  146. mv -f [email protected] $@; \
  147. fi);
  148. endef
  149. tags: force
  150. $(RM) tags
  151. find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
  152. --regex-c++='/_PE\(([^,)]*).*/TEP_ERRNO__\1/'
  153. TAGS: force
  154. $(RM) TAGS
  155. find . -name '*.[ch]' | xargs etags \
  156. --regex='/_PE(\([^,)]*\).*/TEP_ERRNO__\1/'
  157. define do_install_mkdir
  158. if [ ! -d '$(DESTDIR_SQ)$1' ]; then \
  159. $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
  160. fi
  161. endef
  162. define do_install
  163. $(call do_install_mkdir,$2); \
  164. $(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2'
  165. endef
  166. PKG_CONFIG_SOURCE_FILE = libtraceevent.pc
  167. PKG_CONFIG_FILE := $(addprefix $(OUTPUT),$(PKG_CONFIG_SOURCE_FILE))
  168. define do_install_pkgconfig_file
  169. if [ -n "${pkgconfig_dir}" ]; then \
  170. cp -f ${PKG_CONFIG_SOURCE_FILE}.template ${PKG_CONFIG_FILE}; \
  171. sed -i "s|INSTALL_PREFIX|${1}|g" ${PKG_CONFIG_FILE}; \
  172. sed -i "s|LIB_VERSION|${EVENT_PARSE_VERSION}|g" ${PKG_CONFIG_FILE}; \
  173. sed -i "s|LIB_DIR|${libdir}|g" ${PKG_CONFIG_FILE}; \
  174. sed -i "s|HEADER_DIR|$(includedir)|g" ${PKG_CONFIG_FILE}; \
  175. $(call do_install,$(PKG_CONFIG_FILE),$(pkgconfig_dir),644); \
  176. else \
  177. (echo Failed to locate pkg-config directory) 1>&2; \
  178. fi
  179. endef
  180. install_lib: all_cmd install_plugins install_headers install_pkgconfig
  181. $(call QUIET_INSTALL, $(LIB_TARGET)) \
  182. $(call do_install_mkdir,$(libdir_SQ)); \
  183. cp -fpR $(LIB_INSTALL) $(DESTDIR)$(libdir_SQ)
  184. install_pkgconfig:
  185. $(call QUIET_INSTALL, $(PKG_CONFIG_FILE)) \
  186. $(call do_install_pkgconfig_file,$(prefix))
  187. install_headers:
  188. $(call QUIET_INSTALL, headers) \
  189. $(call do_install,event-parse.h,$(includedir_SQ),644); \
  190. $(call do_install,event-utils.h,$(includedir_SQ),644); \
  191. $(call do_install,trace-seq.h,$(includedir_SQ),644); \
  192. $(call do_install,kbuffer.h,$(includedir_SQ),644)
  193. install: install_lib
  194. clean: clean_plugins
  195. $(call QUIET_CLEAN, libtraceevent) \
  196. $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d .*.cmd; \
  197. $(RM) TRACEEVENT-CFLAGS tags TAGS; \
  198. $(RM) $(PKG_CONFIG_FILE)
  199. PHONY += doc
  200. doc:
  201. $(call descend,Documentation)
  202. PHONY += doc-clean
  203. doc-clean:
  204. $(call descend,Documentation,clean)
  205. PHONY += doc-install
  206. doc-install:
  207. $(call descend,Documentation,install)
  208. PHONY += doc-uninstall
  209. doc-uninstall:
  210. $(call descend,Documentation,uninstall)
  211. PHONY += help
  212. help:
  213. @echo 'Possible targets:'
  214. @echo''
  215. @echo ' all - default, compile the library and the'\
  216. 'plugins'
  217. @echo ' plugins - compile the plugins'
  218. @echo ' install - install the library, the plugins,'\
  219. 'the header and pkgconfig files'
  220. @echo ' clean - clean the library and the plugins object files'
  221. @echo ' doc - compile the documentation files - man'\
  222. 'and html pages, in the Documentation directory'
  223. @echo ' doc-clean - clean the documentation files'
  224. @echo ' doc-install - install the man pages'
  225. @echo ' doc-uninstall - uninstall the man pages'
  226. @echo''
  227. PHONY += plugins
  228. plugins:
  229. $(call descend,plugins)
  230. PHONY += install_plugins
  231. install_plugins:
  232. $(call descend,plugins,install)
  233. PHONY += clean_plugins
  234. clean_plugins:
  235. $(call descend,plugins,clean)
  236. force:
  237. # Declare the contents of the .PHONY variable as phony. We keep that
  238. # information in a variable so we can use it in if_changed and friends.
  239. .PHONY: $(PHONY)