Makefile 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. # trace-cmd version
  2. EP_VERSION = 1
  3. EP_PATCHLEVEL = 1
  4. EP_EXTRAVERSION = 0
  5. # file format version
  6. FILE_VERSION = 6
  7. MAKEFLAGS += --no-print-directory
  8. # Makefiles suck: This macro sets a default value of $(2) for the
  9. # variable named by $(1), unless the variable has been set by
  10. # environment or command line. This is necessary for CC and AR
  11. # because make sets default values, so the simpler ?= approach
  12. # won't work as expected.
  13. define allow-override
  14. $(if $(or $(findstring environment,$(origin $(1))),\
  15. $(findstring command line,$(origin $(1)))),,\
  16. $(eval $(1) = $(2)))
  17. endef
  18. # Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
  19. $(call allow-override,CC,$(CROSS_COMPILE)gcc)
  20. $(call allow-override,AR,$(CROSS_COMPILE)ar)
  21. EXT = -std=gnu99
  22. INSTALL = install
  23. # Use DESTDIR for installing into a different root directory.
  24. # This is useful for building a package. The program will be
  25. # installed in this directory as if it was the root directory.
  26. # Then the build tool can move it later.
  27. DESTDIR ?=
  28. DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
  29. prefix ?= /usr/local
  30. bindir_relative = bin
  31. bindir = $(prefix)/$(bindir_relative)
  32. man_dir = $(prefix)/share/man
  33. man_dir_SQ = '$(subst ','\'',$(man_dir))'
  34. export man_dir man_dir_SQ INSTALL
  35. export DESTDIR DESTDIR_SQ
  36. # copy a bit from Linux kbuild
  37. ifeq ("$(origin V)", "command line")
  38. VERBOSE = $(V)
  39. endif
  40. ifndef VERBOSE
  41. VERBOSE = 0
  42. endif
  43. ifeq ("$(origin O)", "command line")
  44. BUILD_OUTPUT := $(O)
  45. endif
  46. ifeq ($(BUILD_SRC),)
  47. ifneq ($(BUILD_OUTPUT),)
  48. define build_output
  49. $(if $(VERBOSE:1=),@)+$(MAKE) -C $(BUILD_OUTPUT) \
  50. BUILD_SRC=$(CURDIR) -f $(CURDIR)/Makefile $1
  51. endef
  52. saved-output := $(BUILD_OUTPUT)
  53. BUILD_OUTPUT := $(shell cd $(BUILD_OUTPUT) && /bin/pwd)
  54. $(if $(BUILD_OUTPUT),, \
  55. $(error output directory "$(saved-output)" does not exist))
  56. all: sub-make
  57. $(MAKECMDGOALS): sub-make
  58. sub-make: force
  59. $(call build_output, $(MAKECMDGOALS))
  60. # Leave processing to above invocation of make
  61. skip-makefile := 1
  62. endif # BUILD_OUTPUT
  63. endif # BUILD_SRC
  64. # We process the rest of the Makefile if this is the final invocation of make
  65. ifeq ($(skip-makefile),)
  66. srctree := $(if $(BUILD_SRC),$(BUILD_SRC),$(CURDIR))
  67. objtree := $(CURDIR)
  68. src := $(srctree)
  69. obj := $(objtree)
  70. export prefix bindir src obj
  71. # Shell quotes
  72. bindir_SQ = $(subst ','\'',$(bindir))
  73. bindir_relative_SQ = $(subst ','\'',$(bindir_relative))
  74. LIB_FILE = libtraceevent.a libtraceevent.so
  75. CONFIG_INCLUDES =
  76. CONFIG_LIBS =
  77. CONFIG_FLAGS =
  78. VERSION = $(EP_VERSION)
  79. PATCHLEVEL = $(EP_PATCHLEVEL)
  80. EXTRAVERSION = $(EP_EXTRAVERSION)
  81. OBJ = $@
  82. N =
  83. export Q VERBOSE
  84. EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION)
  85. INCLUDES = -I. $(CONFIG_INCLUDES)
  86. # Set compile option CFLAGS if not set elsewhere
  87. CFLAGS ?= -g -Wall
  88. # Append required CFLAGS
  89. override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
  90. override CFLAGS += $(udis86-flags) -D_GNU_SOURCE
  91. ifeq ($(VERBOSE),1)
  92. Q =
  93. print_compile =
  94. print_app_build =
  95. print_fpic_compile =
  96. print_shared_lib_compile =
  97. print_plugin_obj_compile =
  98. print_plugin_build =
  99. print_install =
  100. else
  101. Q = @
  102. print_compile = echo ' CC '$(OBJ);
  103. print_app_build = echo ' BUILD '$(OBJ);
  104. print_fpic_compile = echo ' CC FPIC '$(OBJ);
  105. print_shared_lib_compile = echo ' BUILD SHARED LIB '$(OBJ);
  106. print_plugin_obj_compile = echo ' CC PLUGIN OBJ '$(OBJ);
  107. print_plugin_build = echo ' CC PLUGI '$(OBJ);
  108. print_static_lib_build = echo ' BUILD STATIC LIB '$(OBJ);
  109. print_install = echo ' INSTALL '$1' to $(DESTDIR_SQ)$2';
  110. endif
  111. do_fpic_compile = \
  112. ($(print_fpic_compile) \
  113. $(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@)
  114. do_app_build = \
  115. ($(print_app_build) \
  116. $(CC) $^ -rdynamic -o $@ $(CONFIG_LIBS) $(LIBS))
  117. do_compile_shared_library = \
  118. ($(print_shared_lib_compile) \
  119. $(CC) --shared $^ -o $@)
  120. do_compile_plugin_obj = \
  121. ($(print_plugin_obj_compile) \
  122. $(CC) -c $(CFLAGS) -fPIC -o $@ $<)
  123. do_plugin_build = \
  124. ($(print_plugin_build) \
  125. $(CC) $(CFLAGS) -shared -nostartfiles -o $@ $<)
  126. do_build_static_lib = \
  127. ($(print_static_lib_build) \
  128. $(RM) $@; $(AR) rcs $@ $^)
  129. define do_compile
  130. $(print_compile) \
  131. $(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@;
  132. endef
  133. $(obj)/%.o: $(src)/%.c
  134. $(Q)$(call do_compile)
  135. %.o: $(src)/%.c
  136. $(Q)$(call do_compile)
  137. PEVENT_LIB_OBJS = event-parse.o trace-seq.o parse-filter.o parse-utils.o
  138. PEVENT_LIB_OBJS += kbuffer-parse.o
  139. ALL_OBJS = $(PEVENT_LIB_OBJS)
  140. CMD_TARGETS = $(LIB_FILE)
  141. TARGETS = $(CMD_TARGETS)
  142. all: all_cmd
  143. all_cmd: $(CMD_TARGETS)
  144. libtraceevent.so: $(PEVENT_LIB_OBJS)
  145. $(Q)$(do_compile_shared_library)
  146. libtraceevent.a: $(PEVENT_LIB_OBJS)
  147. $(Q)$(do_build_static_lib)
  148. $(PEVENT_LIB_OBJS): %.o: $(src)/%.c TRACEEVENT-CFLAGS
  149. $(Q)$(do_fpic_compile)
  150. define make_version.h
  151. (echo '/* This file is automatically generated. Do not modify. */'; \
  152. echo \#define VERSION_CODE $(shell \
  153. expr $(VERSION) \* 256 + $(PATCHLEVEL)); \
  154. echo '#define EXTRAVERSION ' $(EXTRAVERSION); \
  155. echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"'; \
  156. echo '#define FILE_VERSION '$(FILE_VERSION); \
  157. ) > $1
  158. endef
  159. define update_version.h
  160. ($(call make_version.h, $@.tmp); \
  161. if [ -r $@ ] && cmp -s $@ $@.tmp; then \
  162. rm -f $@.tmp; \
  163. else \
  164. echo ' UPDATE $@'; \
  165. mv -f $@.tmp $@; \
  166. fi);
  167. endef
  168. ep_version.h: force
  169. $(Q)$(N)$(call update_version.h)
  170. VERSION_FILES = ep_version.h
  171. define update_dir
  172. (echo $1 > $@.tmp; \
  173. if [ -r $@ ] && cmp -s $@ $@.tmp; then \
  174. rm -f $@.tmp; \
  175. else \
  176. echo ' UPDATE $@'; \
  177. mv -f $@.tmp $@; \
  178. fi);
  179. endef
  180. ## make deps
  181. all_objs := $(sort $(ALL_OBJS))
  182. all_deps := $(all_objs:%.o=.%.d)
  183. # let .d file also depends on the source and header files
  184. define check_deps
  185. @set -e; $(RM) $@; \
  186. $(CC) -MM $(CFLAGS) $< > $@.$$$$; \
  187. sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
  188. $(RM) $@.$$$$
  189. endef
  190. $(all_deps): .%.d: $(src)/%.c
  191. $(Q)$(call check_deps)
  192. $(all_objs) : %.o : .%.d
  193. dep_includes := $(wildcard $(all_deps))
  194. ifneq ($(dep_includes),)
  195. include $(dep_includes)
  196. endif
  197. ### Detect environment changes
  198. TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE)
  199. TRACEEVENT-CFLAGS: force
  200. @FLAGS='$(TRACK_CFLAGS)'; \
  201. if test x"$$FLAGS" != x"`cat TRACEEVENT-CFLAGS 2>/dev/null`" ; then \
  202. echo 1>&2 " * new build flags or cross compiler"; \
  203. echo "$$FLAGS" >TRACEEVENT-CFLAGS; \
  204. fi
  205. tags: force
  206. $(RM) tags
  207. find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
  208. --regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/'
  209. TAGS: force
  210. $(RM) TAGS
  211. find . -name '*.[ch]' | xargs etags \
  212. --regex='/_PE(\([^,)]*\).*/PEVENT_ERRNO__\1/'
  213. define do_install
  214. $(print_install) \
  215. if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
  216. $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
  217. fi; \
  218. $(INSTALL) $1 '$(DESTDIR_SQ)$2'
  219. endef
  220. install_lib: all_cmd
  221. $(Q)$(call do_install,$(LIB_FILE),$(bindir_SQ))
  222. install: install_lib
  223. clean:
  224. $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d
  225. $(RM) TRACEEVENT-CFLAGS tags TAGS
  226. endif # skip-makefile
  227. PHONY += force
  228. force:
  229. # Declare the contents of the .PHONY variable as phony. We keep that
  230. # information in a variable so we can use it in if_changed and friends.
  231. .PHONY: $(PHONY)