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 | |
Jiri Olsa | e0e96d0 | 2013-12-03 14:09:17 +0100 | [diff] [blame^] | 46 | set_plugin_dir := 1 |
| 47 | |
| 48 | # Set plugin_dir to preffered global plugin location |
| 49 | # If we install under $HOME directory we go under |
| 50 | # $(HOME)/.traceevent/plugins |
| 51 | # |
| 52 | # We dont set PLUGIN_DIR in case we install under $HOME |
| 53 | # directory, because by default the code looks under: |
| 54 | # $(HOME)/.traceevent/plugins by default. |
| 55 | # |
| 56 | ifeq ($(plugin_dir),) |
| 57 | ifeq ($(prefix),$(HOME)) |
| 58 | override plugin_dir = $(HOME)/.traceevent/plugins |
| 59 | set_plugin_dir := 0 |
| 60 | else |
| 61 | override plugin_dir = $(prefix)/lib/traceevent/plugins |
| 62 | endif |
| 63 | endif |
| 64 | |
| 65 | ifeq ($(set_plugin_dir),1) |
| 66 | PLUGIN_DIR = -DPLUGIN_DIR="$(DESTDIR)/$(plugin_dir)" |
| 67 | PLUGIN_DIR_SQ = '$(subst ','\'',$(PLUGIN_DIR))' |
| 68 | endif |
| 69 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 70 | # copy a bit from Linux kbuild |
| 71 | |
| 72 | ifeq ("$(origin V)", "command line") |
| 73 | VERBOSE = $(V) |
| 74 | endif |
| 75 | ifndef VERBOSE |
| 76 | VERBOSE = 0 |
| 77 | endif |
| 78 | |
| 79 | ifeq ("$(origin O)", "command line") |
| 80 | BUILD_OUTPUT := $(O) |
| 81 | endif |
| 82 | |
| 83 | ifeq ($(BUILD_SRC),) |
| 84 | ifneq ($(BUILD_OUTPUT),) |
| 85 | |
| 86 | define build_output |
Arnaldo Carvalho de Melo | 456da53 | 2013-08-26 13:04:04 -0300 | [diff] [blame] | 87 | $(if $(VERBOSE:1=),@)+$(MAKE) -C $(BUILD_OUTPUT) \ |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 88 | BUILD_SRC=$(CURDIR) -f $(CURDIR)/Makefile $1 |
| 89 | endef |
| 90 | |
| 91 | saved-output := $(BUILD_OUTPUT) |
| 92 | BUILD_OUTPUT := $(shell cd $(BUILD_OUTPUT) && /bin/pwd) |
| 93 | $(if $(BUILD_OUTPUT),, \ |
| 94 | $(error output directory "$(saved-output)" does not exist)) |
| 95 | |
| 96 | all: sub-make |
| 97 | |
Namhyung Kim | 4ccdf57 | 2013-06-04 14:20:17 +0900 | [diff] [blame] | 98 | $(MAKECMDGOALS): sub-make |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 99 | |
| 100 | sub-make: force |
| 101 | $(call build_output, $(MAKECMDGOALS)) |
| 102 | |
| 103 | |
| 104 | # Leave processing to above invocation of make |
| 105 | skip-makefile := 1 |
| 106 | |
| 107 | endif # BUILD_OUTPUT |
| 108 | endif # BUILD_SRC |
| 109 | |
| 110 | # We process the rest of the Makefile if this is the final invocation of make |
| 111 | ifeq ($(skip-makefile),) |
| 112 | |
| 113 | srctree := $(if $(BUILD_SRC),$(BUILD_SRC),$(CURDIR)) |
| 114 | objtree := $(CURDIR) |
| 115 | src := $(srctree) |
| 116 | obj := $(objtree) |
| 117 | |
| 118 | export prefix bindir src obj |
| 119 | |
| 120 | # Shell quotes |
| 121 | bindir_SQ = $(subst ','\'',$(bindir)) |
| 122 | bindir_relative_SQ = $(subst ','\'',$(bindir_relative)) |
Jiri Olsa | e0e96d0 | 2013-12-03 14:09:17 +0100 | [diff] [blame^] | 123 | plugin_dir_SQ = $(subst ','\'',$(plugin_dir)) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 124 | |
| 125 | LIB_FILE = libtraceevent.a libtraceevent.so |
| 126 | |
| 127 | CONFIG_INCLUDES = |
| 128 | CONFIG_LIBS = |
| 129 | CONFIG_FLAGS = |
| 130 | |
| 131 | VERSION = $(EP_VERSION) |
| 132 | PATCHLEVEL = $(EP_PATCHLEVEL) |
| 133 | EXTRAVERSION = $(EP_EXTRAVERSION) |
| 134 | |
| 135 | OBJ = $@ |
| 136 | N = |
| 137 | |
| 138 | export Q VERBOSE |
| 139 | |
| 140 | EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION) |
| 141 | |
Jack Mitchell | b9e8c37 | 2013-03-08 11:21:52 +0000 | [diff] [blame] | 142 | INCLUDES = -I. $(CONFIG_INCLUDES) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 143 | |
| 144 | # Set compile option CFLAGS if not set elsewhere |
| 145 | CFLAGS ?= -g -Wall |
| 146 | |
| 147 | # Append required CFLAGS |
| 148 | override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ) |
Arnaldo Carvalho de Melo | c055875 | 2012-09-12 14:29:40 -0300 | [diff] [blame] | 149 | override CFLAGS += $(udis86-flags) -D_GNU_SOURCE |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 150 | |
| 151 | ifeq ($(VERBOSE),1) |
| 152 | Q = |
| 153 | print_compile = |
| 154 | print_app_build = |
| 155 | print_fpic_compile = |
| 156 | print_shared_lib_compile = |
| 157 | print_plugin_obj_compile = |
| 158 | print_plugin_build = |
| 159 | print_install = |
| 160 | else |
| 161 | Q = @ |
Ingo Molnar | 65fb099 | 2013-10-09 11:49:27 +0200 | [diff] [blame] | 162 | print_compile = echo ' CC '$(OBJ); |
| 163 | print_app_build = echo ' BUILD '$(OBJ); |
| 164 | print_fpic_compile = echo ' CC FPIC '$(OBJ); |
| 165 | print_shared_lib_compile = echo ' BUILD SHARED LIB '$(OBJ); |
Jiri Olsa | e0e96d0 | 2013-12-03 14:09:17 +0100 | [diff] [blame^] | 166 | print_plugin_obj_compile = echo ' CC FPIC '$(OBJ); |
| 167 | print_plugin_build = echo ' BUILD PLUGIN '$(OBJ); |
Ingo Molnar | 65fb099 | 2013-10-09 11:49:27 +0200 | [diff] [blame] | 168 | print_static_lib_build = echo ' BUILD STATIC LIB '$(OBJ); |
| 169 | print_install = echo ' INSTALL '$1' to $(DESTDIR_SQ)$2'; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 170 | endif |
| 171 | |
| 172 | do_fpic_compile = \ |
| 173 | ($(print_fpic_compile) \ |
| 174 | $(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@) |
| 175 | |
| 176 | do_app_build = \ |
| 177 | ($(print_app_build) \ |
| 178 | $(CC) $^ -rdynamic -o $@ $(CONFIG_LIBS) $(LIBS)) |
| 179 | |
| 180 | do_compile_shared_library = \ |
| 181 | ($(print_shared_lib_compile) \ |
| 182 | $(CC) --shared $^ -o $@) |
| 183 | |
| 184 | do_compile_plugin_obj = \ |
| 185 | ($(print_plugin_obj_compile) \ |
| 186 | $(CC) -c $(CFLAGS) -fPIC -o $@ $<) |
| 187 | |
| 188 | do_plugin_build = \ |
| 189 | ($(print_plugin_build) \ |
| 190 | $(CC) $(CFLAGS) -shared -nostartfiles -o $@ $<) |
| 191 | |
| 192 | do_build_static_lib = \ |
| 193 | ($(print_static_lib_build) \ |
| 194 | $(RM) $@; $(AR) rcs $@ $^) |
| 195 | |
| 196 | |
| 197 | define do_compile |
| 198 | $(print_compile) \ |
| 199 | $(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@; |
| 200 | endef |
| 201 | |
| 202 | $(obj)/%.o: $(src)/%.c |
| 203 | $(Q)$(call do_compile) |
| 204 | |
| 205 | %.o: $(src)/%.c |
| 206 | $(Q)$(call do_compile) |
| 207 | |
Jiri Olsa | c877bbd | 2013-12-03 14:09:16 +0100 | [diff] [blame] | 208 | PEVENT_LIB_OBJS = event-parse.o |
| 209 | PEVENT_LIB_OBJS += event-plugin.o |
| 210 | PEVENT_LIB_OBJS += trace-seq.o |
| 211 | PEVENT_LIB_OBJS += parse-filter.o |
| 212 | PEVENT_LIB_OBJS += parse-utils.o |
Namhyung Kim | d6c2522 | 2013-06-04 14:20:21 +0900 | [diff] [blame] | 213 | PEVENT_LIB_OBJS += kbuffer-parse.o |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 214 | |
Jiri Olsa | e0e96d0 | 2013-12-03 14:09:17 +0100 | [diff] [blame^] | 215 | PLUGINS := $(PLUGIN_OBJS:.o=.so) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 216 | |
Jiri Olsa | e0e96d0 | 2013-12-03 14:09:17 +0100 | [diff] [blame^] | 217 | ALL_OBJS = $(PEVENT_LIB_OBJS) $(PLUGIN_OBJS) |
| 218 | |
| 219 | CMD_TARGETS = $(LIB_FILE) $(PLUGINS) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 220 | |
| 221 | TARGETS = $(CMD_TARGETS) |
| 222 | |
| 223 | |
| 224 | all: all_cmd |
| 225 | |
| 226 | all_cmd: $(CMD_TARGETS) |
| 227 | |
| 228 | libtraceevent.so: $(PEVENT_LIB_OBJS) |
| 229 | $(Q)$(do_compile_shared_library) |
| 230 | |
| 231 | libtraceevent.a: $(PEVENT_LIB_OBJS) |
| 232 | $(Q)$(do_build_static_lib) |
| 233 | |
Jiri Olsa | e0e96d0 | 2013-12-03 14:09:17 +0100 | [diff] [blame^] | 234 | plugins: $(PLUGINS) |
| 235 | |
Namhyung Kim | 52b5c0d | 2012-07-06 16:21:32 +0900 | [diff] [blame] | 236 | $(PEVENT_LIB_OBJS): %.o: $(src)/%.c TRACEEVENT-CFLAGS |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 237 | $(Q)$(do_fpic_compile) |
| 238 | |
Jiri Olsa | e0e96d0 | 2013-12-03 14:09:17 +0100 | [diff] [blame^] | 239 | $(PLUGIN_OBJS): %.o : $(src)/%.c |
| 240 | $(Q)$(do_compile_plugin_obj) |
| 241 | |
| 242 | $(PLUGINS): %.so: %.o |
| 243 | $(Q)$(do_plugin_build) |
| 244 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 245 | define make_version.h |
| 246 | (echo '/* This file is automatically generated. Do not modify. */'; \ |
| 247 | echo \#define VERSION_CODE $(shell \ |
| 248 | expr $(VERSION) \* 256 + $(PATCHLEVEL)); \ |
| 249 | echo '#define EXTRAVERSION ' $(EXTRAVERSION); \ |
| 250 | echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"'; \ |
| 251 | echo '#define FILE_VERSION '$(FILE_VERSION); \ |
| 252 | ) > $1 |
| 253 | endef |
| 254 | |
| 255 | define update_version.h |
| 256 | ($(call make_version.h, $@.tmp); \ |
| 257 | if [ -r $@ ] && cmp -s $@ $@.tmp; then \ |
| 258 | rm -f $@.tmp; \ |
| 259 | else \ |
| 260 | echo ' UPDATE $@'; \ |
| 261 | mv -f $@.tmp $@; \ |
| 262 | fi); |
| 263 | endef |
| 264 | |
| 265 | ep_version.h: force |
| 266 | $(Q)$(N)$(call update_version.h) |
| 267 | |
| 268 | VERSION_FILES = ep_version.h |
| 269 | |
| 270 | define update_dir |
| 271 | (echo $1 > $@.tmp; \ |
| 272 | if [ -r $@ ] && cmp -s $@ $@.tmp; then \ |
| 273 | rm -f $@.tmp; \ |
| 274 | else \ |
| 275 | echo ' UPDATE $@'; \ |
| 276 | mv -f $@.tmp $@; \ |
| 277 | fi); |
| 278 | endef |
| 279 | |
| 280 | ## make deps |
| 281 | |
| 282 | all_objs := $(sort $(ALL_OBJS)) |
| 283 | all_deps := $(all_objs:%.o=.%.d) |
| 284 | |
Namhyung Kim | 860df58 | 2012-06-22 14:37:36 +0900 | [diff] [blame] | 285 | # let .d file also depends on the source and header files |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 286 | define check_deps |
Namhyung Kim | 860df58 | 2012-06-22 14:37:36 +0900 | [diff] [blame] | 287 | @set -e; $(RM) $@; \ |
Namhyung Kim | b6f4f80 | 2012-10-26 17:55:48 +0900 | [diff] [blame] | 288 | $(CC) -MM $(CFLAGS) $< > $@.$$$$; \ |
Namhyung Kim | 860df58 | 2012-06-22 14:37:36 +0900 | [diff] [blame] | 289 | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ |
| 290 | $(RM) $@.$$$$ |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 291 | endef |
| 292 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 293 | $(all_deps): .%.d: $(src)/%.c |
| 294 | $(Q)$(call check_deps) |
| 295 | |
| 296 | $(all_objs) : %.o : .%.d |
| 297 | |
| 298 | dep_includes := $(wildcard $(all_deps)) |
| 299 | |
| 300 | ifneq ($(dep_includes),) |
| 301 | include $(dep_includes) |
| 302 | endif |
| 303 | |
Namhyung Kim | 52b5c0d | 2012-07-06 16:21:32 +0900 | [diff] [blame] | 304 | ### Detect environment changes |
| 305 | TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE) |
| 306 | |
| 307 | TRACEEVENT-CFLAGS: force |
| 308 | @FLAGS='$(TRACK_CFLAGS)'; \ |
| 309 | if test x"$$FLAGS" != x"`cat TRACEEVENT-CFLAGS 2>/dev/null`" ; then \ |
Ingo Molnar | 65fb099 | 2013-10-09 11:49:27 +0200 | [diff] [blame] | 310 | echo 1>&2 " FLAGS: * new build flags or cross compiler"; \ |
Namhyung Kim | 52b5c0d | 2012-07-06 16:21:32 +0900 | [diff] [blame] | 311 | echo "$$FLAGS" >TRACEEVENT-CFLAGS; \ |
| 312 | fi |
| 313 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 314 | tags: force |
| 315 | $(RM) tags |
Namhyung Kim | 6545e3a | 2012-06-22 17:10:14 +0900 | [diff] [blame] | 316 | find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \ |
| 317 | --regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/' |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 318 | |
| 319 | TAGS: force |
| 320 | $(RM) TAGS |
Namhyung Kim | 6545e3a | 2012-06-22 17:10:14 +0900 | [diff] [blame] | 321 | find . -name '*.[ch]' | xargs etags \ |
| 322 | --regex='/_PE(\([^,)]*\).*/PEVENT_ERRNO__\1/' |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 323 | |
| 324 | define do_install |
| 325 | $(print_install) \ |
| 326 | if [ ! -d '$(DESTDIR_SQ)$2' ]; then \ |
| 327 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \ |
| 328 | fi; \ |
| 329 | $(INSTALL) $1 '$(DESTDIR_SQ)$2' |
| 330 | endef |
| 331 | |
Jiri Olsa | e0e96d0 | 2013-12-03 14:09:17 +0100 | [diff] [blame^] | 332 | install_lib: all_cmd install_plugins |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 333 | $(Q)$(call do_install,$(LIB_FILE),$(bindir_SQ)) |
| 334 | |
Jiri Olsa | e0e96d0 | 2013-12-03 14:09:17 +0100 | [diff] [blame^] | 335 | PLUGINS_INSTALL = $(subst .so,.install,$(PLUGINS)) |
| 336 | |
| 337 | $(PLUGINS_INSTALL): %.install : %.so force |
| 338 | $(Q)$(call do_install,$<,$(plugin_dir_SQ)) |
| 339 | |
| 340 | install_plugins: $(PLUGINS_INSTALL) |
| 341 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 342 | install: install_lib |
| 343 | |
| 344 | clean: |
Konstantin Stepanyuk | f526a4c | 2012-06-27 15:52:14 -0600 | [diff] [blame] | 345 | $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d |
Namhyung Kim | 52b5c0d | 2012-07-06 16:21:32 +0900 | [diff] [blame] | 346 | $(RM) TRACEEVENT-CFLAGS tags TAGS |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 347 | |
| 348 | endif # skip-makefile |
| 349 | |
| 350 | PHONY += force |
| 351 | force: |
| 352 | |
| 353 | # Declare the contents of the .PHONY variable as phony. We keep that |
| 354 | # information in a variable so we can use it in if_changed and friends. |
| 355 | .PHONY: $(PHONY) |