1# SPDX-License-Identifier: LGPL-2.1 2 3# Utils 4 5 PWD := $(shell /bin/pwd) 6 GOBJ = $(notdir $(strip $@)) 7 BASE1 = $(notdir $(strip $1)) 8 BASE2 = $(notdir $(strip $2)) 9 BASEPWD = $(notdir $(strip $(PWD))) 10 11 12ifeq ($(VERBOSE),1) 13 Q = 14 S = 15else 16 Q = @ 17 S = -s 18endif 19 20# Use empty print_* macros if either SILENT or VERBOSE. 21ifeq ($(findstring 1,$(SILENT)$(VERBOSE)),1) 22 print_compile = 23 print_app_build = 24 print_fpic_compile = 25 print_shared_lib_compile = 26 print_plugin_obj_compile = 27 print_plugin_build = 28 print_install = 29 print_uninstall = 30 print_gen = 31 print_update = 32 print_descend = 33 print_clean = 34else 35 print_compile = echo ' COMPILE '$(GOBJ); 36 print_app_build = echo ' BUILD '$(GOBJ); 37 print_fpic_compile = echo ' COMPILE FPIC '$(GOBJ); 38 print_shared_lib_compile = echo ' COMPILE SHARED LIB '$(GOBJ); 39 print_plugin_obj_compile = echo ' COMPILE PLUGIN OBJ '$(GOBJ); 40 print_plugin_build = echo ' BUILD PLUGIN '$(GOBJ); 41 print_static_lib_build = echo ' BUILD STATIC LIB '$(GOBJ); 42 print_install = echo ' INSTALL '$1' to $(DESTDIR_SQ)$2'; 43 print_uninstall = echo ' UNINSTALL $(DESTDIR_SQ)$1'; 44 print_gen = echo ' GEN '$(GOBJ); 45 print_update = echo ' UPDATE '$(GOBJ); 46 print_descend = echo ' DESCEND '$(BASE1) $(BASE2); 47 print_clean = echo ' CLEAN '$(BASEPWD); 48endif 49 50do_fpic_compile = \ 51 ($(print_fpic_compile) \ 52 $(CC) -c $(CPPFLAGS) $(CFLAGS) $(EXT) -fPIC $< -o $@) 53 54do_compile = \ 55 ($(if $(GENERATE_PIC), $(do_fpic_compile), \ 56 $(print_compile) \ 57 $(CC) -c $(CPPFLAGS) $(CFLAGS) $(EXT) $< -o $@)) 58 59do_app_build = \ 60 ($(print_app_build) \ 61 $(CC) $^ -rdynamic -o $@ $(LDFLAGS) $(CONFIG_LIBS) $(LIBS)) 62 63do_build_static_lib = \ 64 ($(print_static_lib_build) \ 65 if [ -f $@ ]; then \ 66 mv $@ $@.rm; $(RM) $@.rm; \ 67 fi; \ 68 $(AR) rcs $@ $^) 69 70do_compile_shared_library = \ 71 ($(print_shared_lib_compile) \ 72 $(CC) --shared $^ '-Wl,-soname,$(1),-rpath=$$ORIGIN' -o $@ $(LDFLAGS) $(LIBS)) 73 74do_compile_plugin_obj = \ 75 ($(print_plugin_obj_compile) \ 76 $(CC) -c $(CPPFLAGS) $(CFLAGS) -fPIC -o $@ $<) 77 78do_plugin_build = \ 79 ($(print_plugin_build) \ 80 $(CC) $(CFLAGS) $(LDFLAGS) -shared -nostartfiles -o $@ $<) 81 82do_compile_python_plugin_obj = \ 83 ($(print_plugin_obj_compile) \ 84 $(CC) -c $(CPPFLAGS) $(CFLAGS) $(PYTHON_DIR_SQ) $(PYTHON_INCLUDES) -fPIC -o $@ $<) 85 86do_python_plugin_build = \ 87 ($(print_plugin_build) \ 88 $(CC) $< -shared $(LDFLAGS) $(PYTHON_LDFLAGS) -o $@) 89 90do_clean = \ 91 ($(print_clean) \ 92 $(RM) $1) 93 94do_sample_build = \ 95 $(Q)($(print_sample_build) \ 96 $(CC) -o $1 $2 $(CFLAGS) $(LIBTRACEEVENT_STATIC) -ldl) 97 98do_sample_obj = \ 99 $(Q)($(print_sample_obj) \ 100 $(CC) -g -Wall -c $(CFLAGS) -o $1 $2 -I../include/) 101 102# 103# Define a callable command for descending to a new directory 104# 105# Call by doing: $(call descend,directory[,target]) 106# 107descend = \ 108 ($(print_descend) \ 109 mkdir -p $(obj)/$(BASE1); \ 110 $(MAKE) $(PRINT_DIR) bdir=$(obj)/$(BASE1) -C $(1) $(2)) 111 112descend_clean = \ 113 $(MAKE) $(PRINT_DIR) bdir=$(obj)/$(BASE1) -C $(1) clean 114 115define make_version.h 116 (echo '/* This file is automatically generated. Do not modify. */'; \ 117 echo \#define VERSION_CODE $(shell \ 118 expr $(VERSION) \* 256 + $(PATCHLEVEL)); \ 119 echo '#define EXTRAVERSION ' $(EXTRAVERSION); \ 120 echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"'; \ 121 echo '#define FILE_VERSION '$(FILE_VERSION); \ 122 if [ -d $(src)/.git ]; then \ 123 d=`git diff`; \ 124 x=""; \ 125 if [ ! -z "$$d" ]; then x="+"; fi; \ 126 echo '#define VERSION_GIT "'$(shell \ 127 git log -1 --pretty=format:"%H" 2>/dev/null)$$x'"'; \ 128 else \ 129 echo '#define VERSION_GIT "not-a-git-repo"'; \ 130 fi \ 131 ) > $1 132endef 133 134define update_version.h 135 ($(call make_version.h, $@.tmp); \ 136 if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 137 rm -f $@.tmp; \ 138 else \ 139 $(print_update) \ 140 mv -f $@.tmp $@; \ 141 fi); 142endef 143 144define update_dir 145 (echo $1 > $@.tmp; \ 146 if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 147 rm -f $@.tmp; \ 148 else \ 149 $(print_update) \ 150 mv -f $@.tmp $@; \ 151 fi); 152endef 153 154define build_prefix 155 (echo $1 > $@.tmp; \ 156 if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 157 rm -f $@.tmp; \ 158 else \ 159 $(print_update) \ 160 mv -f $@.tmp $@; \ 161 fi); 162endef 163 164define do_install_mkdir 165 if [ ! -d '$(DESTDIR_SQ)$1' ]; then \ 166 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \ 167 fi 168endef 169 170define do_install 171 $(print_install) \ 172 $(call do_install_mkdir,$2); \ 173 $(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2' 174endef 175 176define do_install_pkgconfig_file 177 if [ -n "${pkgconfig_dir}" ]; then \ 178 $(call do_install,$(PKG_CONFIG_FILE),$(pkgconfig_dir),644); \ 179 else \ 180 (echo Failed to locate pkg-config directory) 1>&2; \ 181 fi 182endef 183