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