Makefile 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. gui: force
  58. $(call build_output, all_cmd)
  59. $(filter-out gui,$(MAKECMDGOALS)): sub-make
  60. sub-make: force
  61. $(call build_output, $(MAKECMDGOALS))
  62. # Leave processing to above invocation of make
  63. skip-makefile := 1
  64. endif # BUILD_OUTPUT
  65. endif # BUILD_SRC
  66. # We process the rest of the Makefile if this is the final invocation of make
  67. ifeq ($(skip-makefile),)
  68. srctree := $(if $(BUILD_SRC),$(BUILD_SRC),$(CURDIR))
  69. objtree := $(CURDIR)
  70. src := $(srctree)
  71. obj := $(objtree)
  72. export prefix bindir src obj
  73. # Shell quotes
  74. bindir_SQ = $(subst ','\'',$(bindir))
  75. bindir_relative_SQ = $(subst ','\'',$(bindir_relative))
  76. LIB_FILE = libtraceevent.a libtraceevent.so
  77. CONFIG_INCLUDES =
  78. CONFIG_LIBS =
  79. CONFIG_FLAGS =
  80. VERSION = $(EP_VERSION)
  81. PATCHLEVEL = $(EP_PATCHLEVEL)
  82. EXTRAVERSION = $(EP_EXTRAVERSION)
  83. OBJ = $@
  84. N =
  85. export Q VERBOSE
  86. EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION)
  87. INCLUDES = -I. $(CONFIG_INCLUDES)
  88. # Set compile option CFLAGS if not set elsewhere
  89. CFLAGS ?= -g -Wall
  90. # Append required CFLAGS
  91. override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
  92. override CFLAGS += $(udis86-flags) -D_GNU_SOURCE
  93. ifeq ($(VERBOSE),1)
  94. Q =
  95. print_compile =
  96. print_app_build =
  97. print_fpic_compile =
  98. print_shared_lib_compile =
  99. print_plugin_obj_compile =
  100. print_plugin_build =
  101. print_install =
  102. else
  103. Q = @
  104. print_compile = echo ' CC '$(OBJ);
  105. print_app_build = echo ' BUILD '$(OBJ);
  106. print_fpic_compile = echo ' CC FPIC '$(OBJ);
  107. print_shared_lib_compile = echo ' BUILD SHARED LIB '$(OBJ);
  108. print_plugin_obj_compile = echo ' CC PLUGIN OBJ '$(OBJ);
  109. print_plugin_build = echo ' CC PLUGI '$(OBJ);
  110. print_static_lib_build = echo ' BUILD STATIC LIB '$(OBJ);
  111. print_install = echo ' INSTALL '$1' to $(DESTDIR_SQ)$2';
  112. endif
  113. do_fpic_compile = \
  114. ($(print_fpic_compile) \
  115. $(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@)
  116. do_app_build = \
  117. ($(print_app_build) \
  118. $(CC) $^ -rdynamic -o $@ $(CONFIG_LIBS) $(LIBS))
  119. do_compile_shared_library = \
  120. ($(print_shared_lib_compile) \
  121. $(CC) --shared $^ -o $@)
  122. do_compile_plugin_obj = \
  123. ($(print_plugin_obj_compile) \
  124. $(CC) -c $(CFLAGS) -fPIC -o $@ $<)
  125. do_plugin_build = \
  126. ($(print_plugin_build) \
  127. $(CC) $(CFLAGS) -shared -nostartfiles -o $@ $<)
  128. do_build_static_lib = \
  129. ($(print_static_lib_build) \
  130. $(RM) $@; $(AR) rcs $@ $^)
  131. define do_compile
  132. $(print_compile) \
  133. $(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@;
  134. endef
  135. $(obj)/%.o: $(src)/%.c
  136. $(Q)$(call do_compile)
  137. %.o: $(src)/%.c
  138. $(Q)$(call do_compile)
  139. PEVENT_LIB_OBJS = event-parse.o trace-seq.o parse-filter.o parse-utils.o
  140. ALL_OBJS = $(PEVENT_LIB_OBJS)
  141. CMD_TARGETS = $(LIB_FILE)
  142. TARGETS = $(CMD_TARGETS)
  143. all: all_cmd
  144. all_cmd: $(CMD_TARGETS)
  145. libtraceevent.so: $(PEVENT_LIB_OBJS)
  146. $(Q)$(do_compile_shared_library)
  147. libtraceevent.a: $(PEVENT_LIB_OBJS)
  148. $(Q)$(do_build_static_lib)
  149. $(PEVENT_LIB_OBJS): %.o: $(src)/%.c TRACEEVENT-CFLAGS
  150. $(Q)$(do_fpic_compile)
  151. define make_version.h
  152. (echo '/* This file is automatically generated. Do not modify. */'; \
  153. echo \#define VERSION_CODE $(shell \
  154. expr $(VERSION) \* 256 + $(PATCHLEVEL)); \
  155. echo '#define EXTRAVERSION ' $(EXTRAVERSION); \
  156. echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"'; \
  157. echo '#define FILE_VERSION '$(FILE_VERSION); \
  158. ) > $1
  159. endef
  160. define update_version.h
  161. ($(call make_version.h, $@.tmp); \
  162. if [ -r $@ ] && cmp -s $@ $@.tmp; then \
  163. rm -f $@.tmp; \
  164. else \
  165. echo ' UPDATE $@'; \
  166. mv -f $@.tmp $@; \
  167. fi);
  168. endef
  169. ep_version.h: force
  170. $(Q)$(N)$(call update_version.h)
  171. VERSION_FILES = ep_version.h
  172. define update_dir
  173. (echo $1 > $@.tmp; \
  174. if [ -r $@ ] && cmp -s $@ $@.tmp; then \
  175. rm -f $@.tmp; \
  176. else \
  177. echo ' UPDATE $@'; \
  178. mv -f $@.tmp $@; \
  179. fi);
  180. endef
  181. ## make deps
  182. all_objs := $(sort $(ALL_OBJS))
  183. all_deps := $(all_objs:%.o=.%.d)
  184. # let .d file also depends on the source and header files
  185. define check_deps
  186. @set -e; $(RM) $@; \
  187. $(CC) -MM $(CFLAGS) $< > $@.$$$$; \
  188. sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
  189. $(RM) $@.$$$$
  190. endef
  191. $(gui_deps): ks_version.h
  192. $(non_gui_deps): tc_version.h
  193. $(all_deps): .%.d: $(src)/%.c
  194. $(Q)$(call check_deps)
  195. $(all_objs) : %.o : .%.d
  196. dep_includes := $(wildcard $(all_deps))
  197. ifneq ($(dep_includes),)
  198. include $(dep_includes)
  199. endif
  200. ### Detect environment changes
  201. TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE)
  202. TRACEEVENT-CFLAGS: force
  203. @FLAGS='$(TRACK_CFLAGS)'; \
  204. if test x"$$FLAGS" != x"`cat TRACEEVENT-CFLAGS 2>/dev/null`" ; then \
  205. echo 1>&2 " * new build flags or cross compiler"; \
  206. echo "$$FLAGS" >TRACEEVENT-CFLAGS; \
  207. fi
  208. tags: force
  209. $(RM) tags
  210. find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
  211. --regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/'
  212. TAGS: force
  213. $(RM) TAGS
  214. find . -name '*.[ch]' | xargs etags \
  215. --regex='/_PE(\([^,)]*\).*/PEVENT_ERRNO__\1/'
  216. define do_install
  217. $(print_install) \
  218. if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
  219. $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
  220. fi; \
  221. $(INSTALL) $1 '$(DESTDIR_SQ)$2'
  222. endef
  223. install_lib: all_cmd
  224. $(Q)$(call do_install,$(LIB_FILE),$(bindir_SQ))
  225. install: install_lib
  226. clean:
  227. $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d
  228. $(RM) TRACEEVENT-CFLAGS tags TAGS
  229. endif # skip-makefile
  230. PHONY += force
  231. force:
  232. # Declare the contents of the .PHONY variable as phony. We keep that
  233. # information in a variable so we can use it in if_changed and friends.
  234. .PHONY: $(PHONY)