1# trace-cmd version 2EP_VERSION = 1 3EP_PATCHLEVEL = 1 4EP_EXTRAVERSION = 0 5 6# file format version 7FILE_VERSION = 6 8 9MAKEFLAGS += --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. 17define allow-override 18 $(if $(or $(findstring environment,$(origin $(1))),\ 19 $(findstring command line,$(origin $(1)))),,\ 20 $(eval $(1) = $(2))) 21endef 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 27EXT = -std=gnu99 28INSTALL = 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. 34DESTDIR ?= 35DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))' 36 37prefix ?= /usr/local 38bindir_relative = bin 39bindir = $(prefix)/$(bindir_relative) 40man_dir = $(prefix)/share/man 41man_dir_SQ = '$(subst ','\'',$(man_dir))' 42 43export man_dir man_dir_SQ INSTALL 44export DESTDIR DESTDIR_SQ 45 46# copy a bit from Linux kbuild 47 48ifeq ("$(origin V)", "command line") 49 VERBOSE = $(V) 50endif 51ifndef VERBOSE 52 VERBOSE = 0 53endif 54 55ifeq ("$(origin O)", "command line") 56 BUILD_OUTPUT := $(O) 57endif 58 59ifeq ($(BUILD_SRC),) 60ifneq ($(BUILD_OUTPUT),) 61 62define build_output 63 $(if $(VERBOSE:1=),@)+$(MAKE) -C $(BUILD_OUTPUT) \ 64 BUILD_SRC=$(CURDIR) -f $(CURDIR)/Makefile $1 65endef 66 67saved-output := $(BUILD_OUTPUT) 68BUILD_OUTPUT := $(shell cd $(BUILD_OUTPUT) && /bin/pwd) 69$(if $(BUILD_OUTPUT),, \ 70 $(error output directory "$(saved-output)" does not exist)) 71 72all: sub-make 73 74$(MAKECMDGOALS): sub-make 75 76sub-make: force 77 $(call build_output, $(MAKECMDGOALS)) 78 79 80# Leave processing to above invocation of make 81skip-makefile := 1 82 83endif # BUILD_OUTPUT 84endif # BUILD_SRC 85 86# We process the rest of the Makefile if this is the final invocation of make 87ifeq ($(skip-makefile),) 88 89srctree := $(if $(BUILD_SRC),$(BUILD_SRC),$(CURDIR)) 90objtree := $(CURDIR) 91src := $(srctree) 92obj := $(objtree) 93 94export prefix bindir src obj 95 96# Shell quotes 97bindir_SQ = $(subst ','\'',$(bindir)) 98bindir_relative_SQ = $(subst ','\'',$(bindir_relative)) 99 100LIB_FILE = libtraceevent.a libtraceevent.so 101 102CONFIG_INCLUDES = 103CONFIG_LIBS = 104CONFIG_FLAGS = 105 106VERSION = $(EP_VERSION) 107PATCHLEVEL = $(EP_PATCHLEVEL) 108EXTRAVERSION = $(EP_EXTRAVERSION) 109 110OBJ = $@ 111N = 112 113export Q VERBOSE 114 115EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION) 116 117INCLUDES = -I. $(CONFIG_INCLUDES) 118 119# Set compile option CFLAGS if not set elsewhere 120CFLAGS ?= -g -Wall 121 122# Append required CFLAGS 123override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ) 124override CFLAGS += $(udis86-flags) -D_GNU_SOURCE 125 126ifeq ($(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 = 135else 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'; 145endif 146 147do_fpic_compile = \ 148 ($(print_fpic_compile) \ 149 $(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@) 150 151do_app_build = \ 152 ($(print_app_build) \ 153 $(CC) $^ -rdynamic -o $@ $(CONFIG_LIBS) $(LIBS)) 154 155do_compile_shared_library = \ 156 ($(print_shared_lib_compile) \ 157 $(CC) --shared $^ -o $@) 158 159do_compile_plugin_obj = \ 160 ($(print_plugin_obj_compile) \ 161 $(CC) -c $(CFLAGS) -fPIC -o $@ $<) 162 163do_plugin_build = \ 164 ($(print_plugin_build) \ 165 $(CC) $(CFLAGS) -shared -nostartfiles -o $@ $<) 166 167do_build_static_lib = \ 168 ($(print_static_lib_build) \ 169 $(RM) $@; $(AR) rcs $@ $^) 170 171 172define do_compile 173 $(print_compile) \ 174 $(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@; 175endef 176 177$(obj)/%.o: $(src)/%.c 178 $(Q)$(call do_compile) 179 180%.o: $(src)/%.c 181 $(Q)$(call do_compile) 182 183PEVENT_LIB_OBJS = event-parse.o trace-seq.o parse-filter.o parse-utils.o 184PEVENT_LIB_OBJS += kbuffer-parse.o 185 186ALL_OBJS = $(PEVENT_LIB_OBJS) 187 188CMD_TARGETS = $(LIB_FILE) 189 190TARGETS = $(CMD_TARGETS) 191 192 193all: all_cmd 194 195all_cmd: $(CMD_TARGETS) 196 197libtraceevent.so: $(PEVENT_LIB_OBJS) 198 $(Q)$(do_compile_shared_library) 199 200libtraceevent.a: $(PEVENT_LIB_OBJS) 201 $(Q)$(do_build_static_lib) 202 203$(PEVENT_LIB_OBJS): %.o: $(src)/%.c TRACEEVENT-CFLAGS 204 $(Q)$(do_fpic_compile) 205 206define 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 214endef 215 216define 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); 224endef 225 226ep_version.h: force 227 $(Q)$(N)$(call update_version.h) 228 229VERSION_FILES = ep_version.h 230 231define 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); 239endef 240 241## make deps 242 243all_objs := $(sort $(ALL_OBJS)) 244all_deps := $(all_objs:%.o=.%.d) 245 246# let .d file also depends on the source and header files 247define check_deps 248 @set -e; $(RM) $@; \ 249 $(CC) -MM $(CFLAGS) $< > $@.$$$$; \ 250 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ 251 $(RM) $@.$$$$ 252endef 253 254$(all_deps): .%.d: $(src)/%.c 255 $(Q)$(call check_deps) 256 257$(all_objs) : %.o : .%.d 258 259dep_includes := $(wildcard $(all_deps)) 260 261ifneq ($(dep_includes),) 262 include $(dep_includes) 263endif 264 265### Detect environment changes 266TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE) 267 268TRACEEVENT-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 275tags: force 276 $(RM) tags 277 find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \ 278 --regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/' 279 280TAGS: force 281 $(RM) TAGS 282 find . -name '*.[ch]' | xargs etags \ 283 --regex='/_PE(\([^,)]*\).*/PEVENT_ERRNO__\1/' 284 285define 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' 291endef 292 293install_lib: all_cmd 294 $(Q)$(call do_install,$(LIB_FILE),$(bindir_SQ)) 295 296install: install_lib 297 298clean: 299 $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d 300 $(RM) TRACEEVENT-CFLAGS tags TAGS 301 302endif # skip-makefile 303 304PHONY += force 305force: 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) 310