xref: /aosp_15_r20/external/trace-cmd/scripts/utils.mk (revision 58e6ee5f017f6a8912852c892d18457e4bafb554)
1# SPDX-License-Identifier: GPL-2.0
2
3# Utils
4
5ifeq ($(BUILDGUI), 1)
6  GUI		= 'GUI '
7  GSPACE	=
8else
9  GUI		=
10  GSPACE	= "    "
11endif
12
13 GOBJ		= $(GSPACE)$(notdir $(strip $@))
14
15
16ifeq ($(VERBOSE),1)
17  Q =
18  S =
19else
20  Q = @
21  S = -s
22endif
23
24# Use empty print_* macros if either SILENT or VERBOSE.
25ifeq ($(findstring 1,$(SILENT)$(VERBOSE)),1)
26  print_compile =
27  print_app_build =
28  print_fpic_compile =
29  print_shared_lib_compile =
30  print_plugin_obj_compile =
31  print_plugin_build =
32  print_install =
33  print_uninstall =
34  print_update =
35  print_asciidoc =
36  print_xsltproc =
37  print_install =
38  hide_xsltproc_output =
39else
40  print_compile =		echo '  $(GUI)COMPILE            '$(GOBJ);
41  print_app_build =		echo '  $(GUI)BUILD              '$(GOBJ);
42  print_fpic_compile =		echo '  $(GUI)COMPILE FPIC       '$(GOBJ);
43  print_shared_lib_compile =	echo '  $(GUI)COMPILE SHARED LIB '$(GOBJ);
44  print_plugin_obj_compile =	echo '  $(GUI)COMPILE PLUGIN OBJ '$(GOBJ);
45  print_plugin_build =		echo '  $(GUI)BUILD PLUGIN       '$(GOBJ);
46  print_static_lib_build =	echo '  $(GUI)BUILD STATIC LIB   '$(GOBJ);
47  print_install =		echo '  $(GUI)INSTALL     '$(GSPACE)$1'	to	$(DESTDIR_SQ)$2';
48  print_update =		echo '  $(GUI)UPDATE             '$(GOBJ);
49  print_uninstall =		echo '  $(GUI)UNINSTALLING $(DESTDIR_SQ)$1';
50  print_asciidoc =		echo '  ASCIIDOC            '`basename $@`;
51  print_xsltproc =		echo '  XSLTPROC            '`basename $@`;
52  print_install =		echo '  INSTALL             '`basename $1`'	to	$(DESTDIR_SQ)'$2;
53  hide_xsltproc_output = 2> /dev/null
54endif
55
56do_fpic_compile =					\
57	($(print_fpic_compile)				\
58	$(CC) -c $(CPPFLAGS) $(CFLAGS) $(EXT) -fPIC $< -o $@)
59
60do_compile =							\
61	($(if $(GENERATE_PIC), $(do_fpic_compile),		\
62	 $(print_compile)					\
63	 $(CC) -c $(CPPFLAGS) $(CFLAGS) $(EXT) $< -o $@))
64
65do_app_build =						\
66	($(print_app_build)				\
67	$(CC) $^ -rdynamic -Wl,-rpath=$(libdir) -o $@ $(LDFLAGS) $(CONFIG_LIBS) $(LIBS))
68
69do_build_static_lib =				\
70	($(print_static_lib_build)		\
71	$(RM) $@;  $(AR) rcs $@ $^)
72
73do_compile_shared_library =			\
74	($(print_shared_lib_compile)		\
75	$(CC) --shared $^ '-Wl,-soname,$(1),-rpath=$$ORIGIN' -o $@ $(LDFLAGS) $(LIBS))
76
77do_compile_plugin_obj =				\
78	($(print_plugin_obj_compile)		\
79	$(CC) -c $(CPPFLAGS) $(CFLAGS) -fPIC -o $@ $<)
80
81do_plugin_build =				\
82	($(print_plugin_build)			\
83	$(CC) $(CFLAGS) $(LDFLAGS) -shared -nostartfiles -o $@ $<)
84
85do_compile_python_plugin_obj =			\
86	($(print_plugin_obj_compile)		\
87	$(CC) -c $(CPPFLAGS) $(CFLAGS) $(PYTHON_DIR_SQ) $(PYTHON_INCLUDES) -fPIC -o $@ $<)
88
89do_python_plugin_build =			\
90	($(print_plugin_build)			\
91	$(CC) $< -shared $(LDFLAGS) $(PYTHON_LDFLAGS) -o $@)
92
93define make_version.h
94	(echo '/* This file is automatically generated. Do not modify. */';		\
95	echo \#define VERSION_CODE $(shell						\
96	expr $(VERSION) \* 256 + $(PATCHLEVEL));					\
97	echo '#define EXTRAVERSION ' $(EXTRAVERSION);					\
98	echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"';	\
99	echo '#define FILE_VERSION '$(FILE_VERSION);					\
100	if [ -d $(src)/.git ]; then							\
101	  d=`git diff`;									\
102	  x="";										\
103	  if [ ! -z "$$d" ]; then x="+"; fi;						\
104	  echo '#define VERSION_GIT "'$(shell 						\
105		git log -1 --pretty=format:"%H" 2>/dev/null)$$x'"';			\
106	else										\
107	  echo '#define VERSION_GIT "not-a-git-repo"';					\
108	fi										\
109	) > $1
110endef
111
112define update_version.h
113	($(call make_version.h, $@.tmp);				\
114	if [ -r $@ ] && cmp -s $@ $@.tmp; then				\
115		rm -f $@.tmp;						\
116	else								\
117		$(print_update)						\
118		mv -f $@.tmp $@;					\
119	fi);
120endef
121
122define update_dir
123	(echo $1 > $@.tmp;	\
124	if [ -r $@ ] && cmp -s $@ $@.tmp; then				\
125		rm -f $@.tmp;						\
126	else								\
127		$(print_update)						\
128		mv -f $@.tmp $@;					\
129	fi);
130endef
131
132define build_prefix
133	(echo $1 > $@.tmp;	\
134	if [ -r $@ ] && cmp -s $@ $@.tmp; then				\
135		rm -f $@.tmp;						\
136	else								\
137		$(print_update)						\
138		mv -f $@.tmp $@;					\
139	fi);
140endef
141
142define do_install
143	$(print_install)				\
144	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\
145		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2';	\
146	fi;						\
147	$(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2'
148endef
149
150define do_install_data
151	$(print_install)				\
152	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\
153		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2';	\
154	fi;						\
155	$(INSTALL) -m 644 $1 '$(DESTDIR_SQ)$2'
156endef
157
158define do_install_pkgconfig_file
159	if [ -n "${pkgconfig_dir}" ]; then 					\
160		$(call do_install,$(PKG_CONFIG_FILE),$(pkgconfig_dir),644); 	\
161	else 									\
162		(echo Failed to locate pkg-config directory) 1>&2;		\
163	fi
164endef
165
166define do_make_pkgconfig_file
167	$(print_app_build)
168	$(Q)cp -f $(srctree)/${PKG_CONFIG_SOURCE_FILE}.template ${PKG_CONFIG_FILE};	\
169	sed -i "s|INSTALL_PREFIX|${1}|g" ${PKG_CONFIG_FILE}; 		\
170	sed -i "s|LIB_VERSION|${LIBTRACECMD_VERSION}|g" ${PKG_CONFIG_FILE}; \
171	sed -i "s|LIB_DIR|$(libdir)|g" ${PKG_CONFIG_FILE}; \
172	sed -i "s|LIBTRACEFS_MIN_VERSION|$(LIBTRACEFS_MIN_VERSION)|g" ${PKG_CONFIG_FILE}; \
173	sed -i "s|HEADER_DIR|$(includedir)/trace-cmd|g" ${PKG_CONFIG_FILE};
174endef
175
176do_asciidoc_build =				\
177	($(print_asciidoc)			\
178	 asciidoc -d manpage -b docbook -o $@ $<)
179
180do_xsltproc_build =				\
181	($(print_xsltproc)			\
182	 xsltproc --nonet -o $@ ${MANPAGE_DOCBOOK_XSL} $< $(hide_xsltproc_output))
183
184#
185# asciidoc requires a synopsis, but file format man pages (5) do
186# not require them. This removes it from the file in the final step.
187define remove_synopsis
188	(sed -e '/^\.SH "SYNOPSIS"/,/ignore/d' $1 > $1.tmp;\
189	 mv $1.tmp $1)
190endef
191
192define do_install_docs
193	$(print_install)				\
194	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\
195		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2';	\
196	fi;						\
197	$(INSTALL) -m 644 $1 '$(DESTDIR_SQ)$2'
198endef
199
200ifneq ($(findstring $(MAKEFLAGS),s),s)
201ifneq ($(V),1)
202	QUIET_ASCIIDOC	= @echo '  ASCIIDOC  '$@;
203	QUIET_XMLTO	= @echo '  XMLTO    '$@;
204	QUIET_SUBDIR0	= +@subdir=
205	QUIET_SUBDIR1	= ;$(NO_SUBDIR) \
206			   echo '  SUBDIR   ' $$subdir; \
207			  $(MAKE) $(PRINT_DIR) -C $$subdir
208	export V
209endif
210endif
211