1# SPDX-License-Identifier: LGPL-2.1 2# libtracefs version 3TFS_VERSION = 1 4TFS_PATCHLEVEL = 6 5TFS_EXTRAVERSION = 4 6TRACEFS_VERSION = $(TFS_VERSION).$(TFS_PATCHLEVEL).$(TFS_EXTRAVERSION) 7 8export TFS_VERSION 9export TFS_PATCHLEVEL 10export TFS_EXTRAVERSION 11export TRACEFS_VERSION 12 13LIBTRACEEVENT_MIN_VERSION = 1.3 14 15# taken from trace-cmd 16MAKEFLAGS += --no-print-directory 17 18# Makefiles suck: This macro sets a default value of $(2) for the 19# variable named by $(1), unless the variable has been set by 20# environment or command line. This is necessary for CC and AR 21# because make sets default values, so the simpler ?= approach 22# won't work as expected. 23define allow-override 24 $(if $(or $(findstring environment,$(origin $(1))),\ 25 $(findstring command line,$(origin $(1)))),,\ 26 $(eval $(1) = $(2))) 27endef 28 29# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix. 30$(call allow-override,CC,$(CROSS_COMPILE)gcc) 31$(call allow-override,AR,$(CROSS_COMPILE)ar) 32$(call allow-override,PKG_CONFIG,pkg-config) 33$(call allow-override,LD_SO_CONF_PATH,/etc/ld.so.conf.d/) 34$(call allow-override,LDCONFIG,ldconfig) 35 36EXT = -std=gnu99 37INSTALL = install 38 39# Use DESTDIR for installing into a different root directory. 40# This is useful for building a package. The program will be 41# installed in this directory as if it was the root directory. 42# Then the build tool can move it later. 43DESTDIR ?= 44DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))' 45 46LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1) 47ifeq ($(LP64), 1) 48 libdir_relative_temp = lib64 49else 50 libdir_relative_temp = lib 51endif 52 53libdir_relative ?= $(libdir_relative_temp) 54prefix ?= /usr/local 55man_dir ?= $(prefix)/share/man 56man_dir_SQ = '$(subst ','\'',$(man_dir))' 57libdir ?= $(prefix)/$(libdir_relative) 58libdir_SQ = '$(subst ','\'',$(libdir))' 59includedir_relative ?= include/tracefs 60includedir ?= $(prefix)/$(includedir_relative) 61includedir_SQ = '$(subst ','\'',$(includedir))' 62pkgconfig_dir ?= $(word 1,$(shell $(PKG_CONFIG) \ 63 --variable pc_path pkg-config | tr ":" " ")) 64 65TEST_LIBTRACEEVENT = $(shell sh -c "$(PKG_CONFIG) --atleast-version $(LIBTRACEEVENT_MIN_VERSION) libtraceevent > /dev/null 2>&1 && echo y") 66 67ifeq ("$(TEST_LIBTRACEEVENT)", "y") 68LIBTRACEEVENT_INCLUDES = $(shell sh -c "$(PKG_CONFIG) --cflags libtraceevent") 69LIBTRACEEVENT_LIBS = $(shell sh -c "$(PKG_CONFIG) --libs libtraceevent") 70else 71 ifneq ($(MAKECMDGOALS),clean) 72 $(error libtraceevent.so minimum version of $(LIBTRACEEVENT_MIN_VERSION) not installed) 73 endif 74endif 75 76etcdir ?= /etc 77etcdir_SQ = '$(subst ','\'',$(etcdir))' 78 79export man_dir man_dir_SQ html_install html_install_SQ INSTALL 80export img_install img_install_SQ 81export DESTDIR DESTDIR_SQ 82 83pound := \# 84 85HELP_DIR = -DHELP_DIR=$(html_install) 86HELP_DIR_SQ = '$(subst ','\'',$(HELP_DIR))' 87#' emacs highlighting gets confused by the above escaped quote. 88 89BASH_COMPLETE_DIR ?= $(etcdir)/bash_completion.d 90 91# copy a bit from Linux kbuild 92 93ifeq ("$(origin V)", "command line") 94 VERBOSE = $(V) 95endif 96ifndef VERBOSE 97 VERBOSE = 0 98endif 99 100SILENT := $(if $(findstring s,$(filter-out --%,$(MAKEFLAGS))),1) 101 102# $(call test-build, snippet, ret) -> ret if snippet compiles 103# -> empty otherwise 104test-build = $(if $(shell sh -c 'echo "$(1)" | \ 105 $(CC) -o /dev/null -c -x c - > /dev/null 2>&1 && echo y'), $2) 106 107ifeq ("$(origin O)", "command line") 108 109 saved-output := $(O) 110 BUILD_OUTPUT := $(shell cd $(O) && /bin/pwd) 111 $(if $(BUILD_OUTPUT),, \ 112 $(error output directory "$(saved-output)" does not exist)) 113 114else 115 BUILD_OUTPUT = $(CURDIR) 116endif 117 118srctree := $(if $(BUILD_SRC),$(BUILD_SRC),$(CURDIR)) 119objtree := $(BUILD_OUTPUT) 120src := $(srctree) 121obj := $(objtree) 122bdir := $(obj)/lib 123 124export prefix src obj bdir 125 126LIBTRACEFS_STATIC = $(bdir)/libtracefs.a 127LIBTRACEFS_SHARED = $(bdir)/libtracefs.so.$(TRACEFS_VERSION) 128 129LIBTRACEFS_SHARED_SO = $(bdir)/libtracefs.so 130LIBTRACEFS_SHARED_VERSION = $(bdir)/libtracefs.so.$(TFS_VERSION) 131 132PKG_CONFIG_SOURCE_FILE = libtracefs.pc 133PKG_CONFIG_FILE := $(addprefix $(obj)/,$(PKG_CONFIG_SOURCE_FILE)) 134 135LPTHREAD ?= -lpthread 136LIBS = $(LIBTRACEEVENT_LIBS) $(LPTHREAD) 137 138export LIBS 139export LIBTRACEFS_STATIC LIBTRACEFS_SHARED 140export LIBTRACEEVENT_LIBS LIBTRACEEVENT_INCLUDES 141export LIBTRACEFS_SHARED_SO LIBTRACEFS_SHARED_VERSION 142 143export Q SILENT VERBOSE EXT 144 145# Include the utils 146include scripts/utils.mk 147 148INCLUDES = -I$(src)/include 149INCLUDES += -I$(src)/include/tracefs 150 151include $(src)/scripts/features.mk 152 153# Set compile option CFLAGS if not set elsewhere 154CFLAGS ?= -g -Wall 155CPPFLAGS ?= 156LDFLAGS ?= 157 158CUNIT_INSTALLED := $(shell if (printf "$(pound)include <CUnit/Basic.h>\n void main(){CU_initialize_registry();}" | $(CC) -x c - -lcunit -o /dev/null >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi) 159export CUNIT_INSTALLED 160 161export CFLAGS 162export INCLUDES 163 164# Append required CFLAGS 165override CFLAGS += -D_GNU_SOURCE $(LIBTRACEEVENT_INCLUDES) $(INCLUDES) 166 167# Make sure 32 bit stat() works on large file systems 168override CFLAGS += -D_FILE_OFFSET_BITS=64 169 170all: all_cmd 171 172LIB_TARGET = libtracefs.a libtracefs.so.$(TRACEFS_VERSION) 173LIB_INSTALL = libtracefs.a libtracefs.so* 174LIB_INSTALL := $(addprefix $(bdir)/,$(LIB_INSTALL)) 175 176TARGETS = libtracefs.so libtracefs.a 177 178all_cmd: $(TARGETS) $(PKG_CONFIG_FILE) 179 180libtracefs.a: $(bdir) $(LIBTRACEFS_STATIC) 181libtracefs.so: $(bdir) $(LIBTRACEFS_SHARED) 182 183libs: libtracefs.a libtracefs.so 184 185VALGRIND = $(shell which valgrind) 186UTEST_DIR = utest 187UTEST_BINARY = trace-utest 188 189test: force $(LIBTRACEFS_STATIC) 190ifneq ($(CUNIT_INSTALLED),1) 191 $(error CUnit framework not installed, cannot build unit tests)) 192endif 193 $(Q)$(call descend,$(src)/$(UTEST_DIR),$@) 194 195test_mem: test 196ifeq (, $(VALGRIND)) 197 $(error "No valgrind in $(PATH), cannot run memory test") 198endif 199ifneq ($(shell id -u), 0) 200 $(error "The unit test should be run as root, as it reuqires full access to tracefs") 201endif 202 CK_FORK=no LD_LIBRARY_PATH=$(bdir) $(VALGRIND) \ 203 --show-leak-kinds=all --leak-resolution=high \ 204 --leak-check=full --show-possibly-lost=yes \ 205 --track-origins=yes -s \ 206 $(src)/$(UTEST_DIR)/$(UTEST_BINARY) 207 208define find_tag_files 209 find $(src) -name '\.pc' -prune -o -name '*\.[ch]' -print -o -name '*\.[ch]pp' \ 210 ! -name '\.#' -print 211endef 212 213define do_make_pkgconfig_file 214 cp -f ${PKG_CONFIG_SOURCE_FILE}.template ${PKG_CONFIG_FILE}; \ 215 sed -i "s|INSTALL_PREFIX|${1}|g" ${PKG_CONFIG_FILE}; \ 216 sed -i "s|LIB_VERSION|${TRACEFS_VERSION}|g" ${PKG_CONFIG_FILE}; \ 217 sed -i "s|LIB_DIR|${libdir_relative}|g" ${PKG_CONFIG_FILE}; \ 218 sed -i "s|HEADER_DIR|$(includedir_relative)|g" ${PKG_CONFIG_FILE}; \ 219 sed -i "s|LIBTRACEEVENT_MIN|$(LIBTRACEEVENT_MIN_VERSION)|g" ${PKG_CONFIG_FILE}; 220endef 221 222BUILD_PREFIX := $(BUILD_OUTPUT)/build_prefix 223 224VERSION_FILE = tfs_version.h 225 226$(BUILD_PREFIX): force 227 $(Q)$(call build_prefix,$(prefix)) 228 229$(PKG_CONFIG_FILE) : ${PKG_CONFIG_SOURCE_FILE}.template $(BUILD_PREFIX) $(VERSION_FILE) 230 $(Q) $(call do_make_pkgconfig_file,$(prefix)) 231 232VIM_TAGS = $(obj)/tags 233EMACS_TAGS = $(obj)/TAGS 234CSCOPE_TAGS = $(obj)/cscope 235 236$(VIM_TAGS): force 237 $(RM) $@ 238 $(call find_tag_files) | (cd $(obj) && xargs ctags --extra=+f --c-kinds=+px) 239 240$(EMACS_TAGS): force 241 $(RM) $@ 242 $(call find_tag_files) | (cd $(obj) && xargs etags) 243 244$(CSCOPE_TAGS): force 245 $(RM) $(obj)/cscope* 246 $(call find_tag_files) | cscope -b -q 247 248tags: $(VIM_TAGS) 249TAGS: $(EMACS_TAGS) 250cscope: $(CSCOPE_TAGS) 251 252ifeq ("$(DESTDIR)", "") 253# If DESTDIR is not defined, then test if after installing the library 254# and running ldconfig, if the library is visible by ld.so. 255# If not, add the path to /etc/ld.so.conf.d/trace.conf and run ldconfig again. 256define install_ld_config 257 if $(LDCONFIG); then \ 258 if ! grep -q "^$(libdir)$$" $(LD_SO_CONF_PATH)/* ; then \ 259 $(CC) -o $(objtree)/test $(srctree)/test.c -I $(includedir_SQ) \ 260 -L $(libdir_SQ) -ltracefs &> /dev/null; \ 261 if ! $(objtree)/test &> /dev/null; then \ 262 $(call print_install, trace.conf, $(LD_SO_CONF_PATH)) \ 263 echo $(libdir_SQ) >> $(LD_SO_CONF_PATH)/trace.conf; \ 264 $(LDCONFIG); \ 265 fi; \ 266 $(RM) $(objtree)/test; \ 267 fi; \ 268 fi 269endef 270else 271# If installing to a location for another machine or package, do not bother 272# with running ldconfig. 273define install_ld_config 274endef 275endif # DESTDIR = "" 276 277install_libs: libs install_pkgconfig 278 $(Q)$(call do_install,$(LIBTRACEFS_SHARED),$(libdir_SQ)); \ 279 cp -fpR $(LIB_INSTALL) $(DESTDIR)$(libdir_SQ) 280 $(Q)$(call do_install,$(src)/include/tracefs.h,$(includedir_SQ),644) 281 $(Q)$(call install_ld_config) 282 283install: install_libs 284 285install_pkgconfig: $(PKG_CONFIG_FILE) 286 $(Q)$(call , $(PKG_CONFIG_FILE)) \ 287 $(call do_install_pkgconfig_file,$(prefix)) 288 289doc: check_doc 290 $(Q)$(call descend,$(src)/Documentation,all) 291 292doc_clean: 293 $(Q)$(call descend,$(src)/Documentation,clean) 294 295install_doc: 296 $(Q)$(call descend,$(src)/Documentation,install) 297 298check_doc: force 299 $(Q)$(src)/check-manpages.sh $(src)/Documentation 300 301define build_uninstall_script 302 $(Q)mkdir $(BUILD_OUTPUT)/tmp_build 303 $(Q)$(MAKE) -C $(src) DESTDIR=$(BUILD_OUTPUT)/tmp_build/ O=$(BUILD_OUTPUT) $1 > /dev/null 304 $(Q)find $(BUILD_OUTPUT)/tmp_build ! -type d -printf "%P\n" > $(BUILD_OUTPUT)/build_$2 305 $(Q)$(RM) -rf $(BUILD_OUTPUT)/tmp_build 306endef 307 308build_uninstall: $(BUILD_PREFIX) 309 $(call build_uninstall_script,install,uninstall) 310 311$(BUILD_OUTPUT)/build_uninstall: build_uninstall 312 313define uninstall_file 314 if [ -f $(DESTDIR)/$1 -o -h $(DESTDIR)/$1 ]; then \ 315 $(call print_uninstall,$(DESTDIR)/$1)$(RM) $(DESTDIR)/$1; \ 316 fi; 317endef 318 319uninstall: $(BUILD_OUTPUT)/build_uninstall 320 @$(foreach file,$(shell cat $(BUILD_OUTPUT)/build_uninstall),$(call uninstall_file,$(file))) 321 322PHONY += force 323force: 324 325# Declare the contents of the .PHONY variable as phony. We keep that 326# information in a variable so we can use it in if_changed and friends. 327.PHONY: $(PHONY) 328 329DEFAULT_TARGET = $(LIBTRACEFS_STATIC) 330 331OBJS = 332OBJS += tracefs-utils.o 333OBJS += tracefs-instance.o 334OBJS += tracefs-events.o 335 336OBJS := $(OBJS:%.o=$(bdir)/%.o) 337 338all: $(DEFAULT_TARGET) 339 340$(bdir): 341 @mkdir -p $(bdir) 342 343VERSION = $(TFS_VERSION) 344PATCHLEVEL = $(TFS_PATCHLEVEL) 345EXTRAVERSION = $(TFS_EXTRAVERSION) 346 347define make_version.h 348 (echo '/* This file is automatically generated. Do not modify. */'; \ 349 echo \#define VERSION_CODE $(shell \ 350 expr $(VERSION) \* 256 + $(PATCHLEVEL)); \ 351 echo '#define EXTRAVERSION ' $(EXTRAVERSION); \ 352 echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"'; \ 353 ) > $1 354endef 355 356define update_version.h 357 ($(call make_version.h, $@.tmp); \ 358 if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 359 rm -f $@.tmp; \ 360 else \ 361 echo ' UPDATE $@'; \ 362 mv -f $@.tmp $@; \ 363 fi); 364endef 365 366$(VERSION_FILE): force 367 $(Q)$(call update_version.h) 368 369$(LIBTRACEFS_STATIC): force 370 $(Q)$(call descend,$(src)/src,$@) 371 372$(bdir)/libtracefs.so.$(TRACEFS_VERSION): force 373 $(Q)$(call descend,$(src)/src,libtracefs.so) 374 375samples/sqlhist: libtracefs.a 376 $(Q)$(call descend,$(src)/samples,sqlhist) 377 378sqlhist: samples/sqlhist 379 380samples: libtracefs.a force 381 $(Q)$(call descend,$(src)/samples,all) 382 383clean: 384 $(Q)$(call descend_clean,utest) 385 $(Q)$(call descend_clean,src) 386 $(Q)$(call descend_clean,samples) 387 $(Q)$(call do_clean, \ 388 $(TARGETS) $(bdir)/*.a $(bdir)/*.so $(bdir)/*.so.* $(bdir)/*.o $(bdir)/.*.d \ 389 $(PKG_CONFIG_FILE) \ 390 $(VERSION_FILE) \ 391 $(BUILD_PREFIX)) 392 393.PHONY: clean 394 395# libtracefs.a and libtracefs.so would concurrently enter the same directory - 396# a recipe for collisions. 397.NOTPARALLEL: 398