Makefile 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. # file format version
  2. FILE_VERSION = 1
  3. LIBLOCKDEP_VERSION=$(shell make --no-print-directory -sC ../../.. kernelversion)
  4. # Makefiles suck: This macro sets a default value of $(2) for the
  5. # variable named by $(1), unless the variable has been set by
  6. # environment or command line. This is necessary for CC and AR
  7. # because make sets default values, so the simpler ?= approach
  8. # won't work as expected.
  9. define allow-override
  10. $(if $(or $(findstring environment,$(origin $(1))),\
  11. $(findstring command line,$(origin $(1)))),,\
  12. $(eval $(1) = $(2)))
  13. endef
  14. # Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
  15. $(call allow-override,CC,$(CROSS_COMPILE)gcc)
  16. $(call allow-override,AR,$(CROSS_COMPILE)ar)
  17. INSTALL = install
  18. # Use DESTDIR for installing into a different root directory.
  19. # This is useful for building a package. The program will be
  20. # installed in this directory as if it was the root directory.
  21. # Then the build tool can move it later.
  22. DESTDIR ?=
  23. DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
  24. prefix ?= /usr/local
  25. libdir_relative = lib
  26. libdir = $(prefix)/$(libdir_relative)
  27. bindir_relative = bin
  28. bindir = $(prefix)/$(bindir_relative)
  29. export DESTDIR DESTDIR_SQ INSTALL
  30. # copy a bit from Linux kbuild
  31. ifeq ("$(origin V)", "command line")
  32. VERBOSE = $(V)
  33. endif
  34. ifndef VERBOSE
  35. VERBOSE = 0
  36. endif
  37. ifeq ("$(origin O)", "command line")
  38. BUILD_OUTPUT := $(O)
  39. endif
  40. ifeq ($(BUILD_SRC),)
  41. ifneq ($(BUILD_OUTPUT),)
  42. define build_output
  43. $(if $(VERBOSE:1=),@)$(MAKE) -C $(BUILD_OUTPUT) \
  44. BUILD_SRC=$(CURDIR) -f $(CURDIR)/Makefile $1
  45. endef
  46. saved-output := $(BUILD_OUTPUT)
  47. BUILD_OUTPUT := $(shell cd $(BUILD_OUTPUT) && /bin/pwd)
  48. $(if $(BUILD_OUTPUT),, \
  49. $(error output directory "$(saved-output)" does not exist))
  50. all: sub-make
  51. gui: force
  52. $(call build_output, all_cmd)
  53. $(filter-out gui,$(MAKECMDGOALS)): sub-make
  54. sub-make: force
  55. $(call build_output, $(MAKECMDGOALS))
  56. # Leave processing to above invocation of make
  57. skip-makefile := 1
  58. endif # BUILD_OUTPUT
  59. endif # BUILD_SRC
  60. # We process the rest of the Makefile if this is the final invocation of make
  61. ifeq ($(skip-makefile),)
  62. srctree := $(realpath $(if $(BUILD_SRC),$(BUILD_SRC),$(CURDIR)))
  63. objtree := $(realpath $(CURDIR))
  64. src := $(srctree)
  65. obj := $(objtree)
  66. export prefix libdir bindir src obj
  67. # Shell quotes
  68. libdir_SQ = $(subst ','\'',$(libdir))
  69. bindir_SQ = $(subst ','\'',$(bindir))
  70. LIB_FILE = liblockdep.a liblockdep.so.$(LIBLOCKDEP_VERSION)
  71. BIN_FILE = lockdep
  72. CONFIG_INCLUDES =
  73. CONFIG_LIBS =
  74. CONFIG_FLAGS =
  75. OBJ = $@
  76. N =
  77. export Q VERBOSE
  78. INCLUDES = -I. -I/usr/local/include -I./uinclude -I./include -I../../include $(CONFIG_INCLUDES)
  79. # Set compile option CFLAGS if not set elsewhere
  80. CFLAGS ?= -g -DCONFIG_LOCKDEP -DCONFIG_STACKTRACE -DCONFIG_PROVE_LOCKING -DBITS_PER_LONG=__WORDSIZE -DLIBLOCKDEP_VERSION='"$(LIBLOCKDEP_VERSION)"' -rdynamic -O0 -g
  81. override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
  82. ifeq ($(VERBOSE),1)
  83. Q =
  84. print_compile =
  85. print_app_build =
  86. print_fpic_compile =
  87. print_shared_lib_compile =
  88. print_install =
  89. else
  90. Q = @
  91. print_compile = echo ' CC '$(OBJ);
  92. print_app_build = echo ' BUILD '$(OBJ);
  93. print_fpic_compile = echo ' CC FPIC '$(OBJ);
  94. print_shared_lib_compile = echo ' BUILD SHARED LIB '$(OBJ);
  95. print_static_lib_build = echo ' BUILD STATIC LIB '$(OBJ);
  96. print_install = echo ' INSTALL '$1' to $(DESTDIR_SQ)$2';
  97. endif
  98. do_fpic_compile = \
  99. ($(print_fpic_compile) \
  100. $(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@)
  101. do_app_build = \
  102. ($(print_app_build) \
  103. $(CC) $^ -rdynamic -o $@ $(CONFIG_LIBS) $(LIBS))
  104. do_compile_shared_library = \
  105. ($(print_shared_lib_compile) \
  106. $(CC) --shared $^ -o $@ -lpthread -ldl -Wl,-soname='"$@"';$(shell ln -s $@ liblockdep.so))
  107. do_build_static_lib = \
  108. ($(print_static_lib_build) \
  109. $(RM) $@; $(AR) rcs $@ $^)
  110. define do_compile
  111. $(print_compile) \
  112. $(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@;
  113. endef
  114. $(obj)/%.o: $(src)/%.c
  115. $(Q)$(call do_compile)
  116. %.o: $(src)/%.c
  117. $(Q)$(call do_compile)
  118. PEVENT_LIB_OBJS = common.o lockdep.o preload.o rbtree.o
  119. ALL_OBJS = $(PEVENT_LIB_OBJS)
  120. CMD_TARGETS = $(LIB_FILE)
  121. TARGETS = $(CMD_TARGETS)
  122. all: all_cmd
  123. all_cmd: $(CMD_TARGETS)
  124. liblockdep.so.$(LIBLOCKDEP_VERSION): $(PEVENT_LIB_OBJS)
  125. $(Q)$(do_compile_shared_library)
  126. liblockdep.a: $(PEVENT_LIB_OBJS)
  127. $(Q)$(do_build_static_lib)
  128. $(PEVENT_LIB_OBJS): %.o: $(src)/%.c
  129. $(Q)$(do_fpic_compile)
  130. ## make deps
  131. all_objs := $(sort $(ALL_OBJS))
  132. all_deps := $(all_objs:%.o=.%.d)
  133. # let .d file also depends on the source and header files
  134. define check_deps
  135. @set -e; $(RM) $@; \
  136. $(CC) -MM $(CFLAGS) $< > $@.$$$$; \
  137. sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
  138. $(RM) $@.$$$$
  139. endef
  140. $(all_deps): .%.d: $(src)/%.c
  141. $(Q)$(call check_deps)
  142. $(all_objs) : %.o : .%.d
  143. dep_includes := $(wildcard $(all_deps))
  144. ifneq ($(dep_includes),)
  145. include $(dep_includes)
  146. endif
  147. ### Detect environment changes
  148. TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE)
  149. tags: force
  150. $(RM) tags
  151. find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
  152. --regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/'
  153. TAGS: force
  154. $(RM) TAGS
  155. find . -name '*.[ch]' | xargs etags \
  156. --regex='/_PE(\([^,)]*\).*/PEVENT_ERRNO__\1/'
  157. define do_install
  158. $(print_install) \
  159. if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
  160. $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
  161. fi; \
  162. $(INSTALL) $1 '$(DESTDIR_SQ)$2'
  163. endef
  164. install_lib: all_cmd
  165. $(Q)$(call do_install,$(LIB_FILE),$(libdir_SQ))
  166. $(Q)$(call do_install,$(BIN_FILE),$(bindir_SQ))
  167. install: install_lib
  168. clean:
  169. $(RM) *.o *~ $(TARGETS) *.a *liblockdep*.so* $(VERSION_FILES) .*.d
  170. $(RM) tags TAGS
  171. endif # skip-makefile
  172. PHONY += force
  173. force:
  174. # Declare the contents of the .PHONY variable as phony. We keep that
  175. # information in a variable so we can use it in if_changed and friends.
  176. .PHONY: $(PHONY)