Makefile 7.6 KB

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