• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_update =
31  print_descend =
32  print_clean =
33  print_extract =
34  print_sample_build =
35  print_sample_obj =
36else
37  print_compile =		echo '  COMPILE            '$(GOBJ);
38  print_app_build =		echo '  BUILD              '$(GOBJ);
39  print_fpic_compile =		echo '  COMPILE FPIC       '$(GOBJ);
40  print_shared_lib_compile =	echo '  COMPILE SHARED LIB '$(GOBJ);
41  print_plugin_obj_compile =	echo '  COMPILE PLUGIN OBJ '$(GOBJ);
42  print_plugin_build =		echo '  BUILD PLUGIN       '$(GOBJ);
43  print_static_lib_build =	echo '  BUILD STATIC LIB   '$(GOBJ);
44  print_install =		echo '  INSTALL     '$1'	to	$(DESTDIR_SQ)$2';
45  print_uninstall =		echo '  UNINSTALL     $(DESTDIR_SQ)$1';
46  print_update =		echo '  UPDATE             '$(GOBJ);
47  print_descend =		echo '  DESCEND            '$(BASE1) $(BASE2);
48  print_clean =			echo '  CLEAN              '$(BASEPWD);
49  print_extract =		echo '  EXTRACT            '$(GOBJ);
50  print_sample_build =		echo '  COMPILE SAMPLE     '$(GOBJ);
51  print_sample_obj =		echo '  COMPILE SAMPLE OBJ '$(GOBJ);
52endif
53
54do_fpic_compile =					\
55	($(print_fpic_compile)				\
56	$(CC) -Wp,-MMD,$(@D)/.$(@F).d,-MT,$@ -MP -c $(CPPFLAGS) $(CFLAGS) $(EXT) -fPIC $< -o $@)
57
58do_compile =							\
59	($(if $(GENERATE_PIC), $(do_fpic_compile),		\
60	 $(print_compile)					\
61	 $(CC) -Wp,-MMD,$(@D)/.$(@F).d,-MT,$@ -MP -c $(CPPFLAGS) $(CFLAGS) $(EXT) $< -o $@))
62
63do_app_build =						\
64	($(print_app_build)				\
65	$(CC) $^ -rdynamic -o $@ $(LDFLAGS) $(CONFIG_LIBS) $(LIBS))
66
67do_build_static_lib =				\
68	($(print_static_lib_build)		\
69	if [ -f $@ ]; then			\
70	    mv $@ $@.rm; $(RM) $@.rm;		\
71	fi;					\
72	$(AR) rcs $@ $^)
73
74do_compile_shared_library =			\
75	($(print_shared_lib_compile)		\
76	$(CC) --shared $^ '-Wl,-soname,$(1),-rpath=$$ORIGIN' -o $@ $(LDFLAGS) $(LIBS))
77
78do_compile_plugin_obj =				\
79	($(print_plugin_obj_compile)		\
80	$(CC) -c $(CPPFLAGS) $(CFLAGS) -fPIC -o $@ $<)
81
82do_plugin_build =				\
83	($(print_plugin_build)			\
84	$(CC) $(CFLAGS) $(LDFLAGS) -shared -nostartfiles -o $@ $<)
85
86do_compile_python_plugin_obj =			\
87	($(print_plugin_obj_compile)		\
88	$(CC) -c $(CPPFLAGS) $(CFLAGS) $(PYTHON_DIR_SQ) $(PYTHON_INCLUDES) -fPIC -o $@ $<)
89
90do_python_plugin_build =			\
91	($(print_plugin_build)			\
92	$(CC) $< -shared $(LDFLAGS) $(PYTHON_LDFLAGS) -o $@)
93
94do_clean =					\
95	($(print_clean)				\
96	$(RM) $1)
97
98extract_example =				\
99	$(Q)($(print_extract)			\
100	cat $1 | sed -ne '/^EXAMPLE/,/FILES/ { /EXAMPLE/,+2d ; /^FILES/d ;  /^--/d ; p}' > $2)
101
102do_sample_build =							\
103	$(Q)($(print_sample_build)					\
104	$(CC) -o $1 $2 $(CFLAGS) $(LIBTRACEFS_STATIC) $(LIBTRACEEVENT_LIBS) -lpthread)
105
106do_sample_obj =									\
107	$(Q)($(print_sample_obj)						\
108	$(CC) -g -Wall -c $(CFLAGS) -o $1 $2 -I../include/ $(LIBTRACEEVENT_INCLUDES))
109
110ifneq ($(findstring $(MAKEFLAGS), w),w)
111PRINT_DIR = --no-print-directory
112else
113NO_SUBDIR = :
114endif
115
116#
117# Define a callable command for descending to a new directory
118#
119# Call by doing: $(call descend,directory[,target])
120#
121descend = \
122	($(print_descend)		\
123	mkdir -p $(obj)/$(BASE1); \
124	$(MAKE) $(PRINT_DIR) bdir=$(obj)/$(BASE1) -C $(1) $(2))
125
126descend_clean = \
127	$(MAKE) $(PRINT_DIR) bdir=$(obj)/$(BASE1) -C $(1) clean
128
129define make_version.h
130	(echo '/* This file is automatically generated. Do not modify. */';		\
131	echo \#define VERSION_CODE $(shell						\
132	expr $(VERSION) \* 256 + $(PATCHLEVEL));					\
133	echo '#define EXTRAVERSION ' $(EXTRAVERSION);					\
134	echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"';	\
135	echo '#define FILE_VERSION '$(FILE_VERSION);					\
136	if [ -d $(src)/.git ]; then							\
137	  d=`git diff`;									\
138	  x="";										\
139	  if [ ! -z "$$d" ]; then x="+"; fi;						\
140	  echo '#define VERSION_GIT "'$(shell 						\
141		git log -1 --pretty=format:"%H" 2>/dev/null)$$x'"';			\
142	else										\
143	  echo '#define VERSION_GIT "not-a-git-repo"';					\
144	fi										\
145	) > $1
146endef
147
148define update_version.h
149	($(call make_version.h, $@.tmp);				\
150	if [ -r $@ ] && cmp -s $@ $@.tmp; then				\
151		rm -f $@.tmp;						\
152	else								\
153		$(print_update)						\
154		mv -f $@.tmp $@;					\
155	fi);
156endef
157
158define update_dir
159	(echo $1 > $@.tmp;	\
160	if [ -r $@ ] && cmp -s $@ $@.tmp; then				\
161		rm -f $@.tmp;						\
162	else								\
163		$(print_update)						\
164		mv -f $@.tmp $@;					\
165	fi);
166endef
167
168define build_prefix
169	(echo $1 > $@.tmp;	\
170	if [ -r $@ ] && cmp -s $@ $@.tmp; then				\
171		rm -f $@.tmp;						\
172	else								\
173		$(print_update)						\
174		mv -f $@.tmp $@;					\
175	fi);
176endef
177
178define do_install_mkdir
179	if [ ! -d '$(DESTDIR_SQ)$1' ]; then		\
180		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1';	\
181	fi
182endef
183
184define do_install
185	$(print_install)				\
186	$(call do_install_mkdir,$2);			\
187	$(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2'
188endef
189
190define do_install_pkgconfig_file
191	if [ -n "${pkgconfig_dir}" ]; then 					\
192		$(call do_install,$(PKG_CONFIG_FILE),$(pkgconfig_dir),644); 	\
193	else 									\
194		(echo Failed to locate pkg-config directory) 1>&2;		\
195	fi
196endef
197