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