Makefile.params revision 1.1
11.1Sapb#	$NetBSD: Makefile.params,v 1.1 2012/12/03 13:53:28 apb Exp $
21.1Sapb#
31.1Sapb# Makefile fragment for printing build parameters.
41.1Sapb#
51.1Sapb# Public variables:
61.1Sapb#	RELEASEVARS
71.1Sapb#		List of variables whose value should be printed.
81.1Sapb#
91.1Sapb#	PRINT_PARAMS
101.1Sapb#		A command to print the desired variables and values.
111.1Sapb#		Values are printed as single-quoted strings, with
121.1Sapb#		embedded quotes and newlines escaped in a way that's
131.1Sapb#		acceptable to sh(1).  Undefined values are printed
141.1Sapb#		as "(undefined)" (without quotation marks).
151.1Sapb#
161.1Sapb# Internal targets:
171.1Sapb# 	_params:
181.1Sapb# 		Prints the names and values of all the variables
191.1Sapb# 		listed in ${RELEASEVARS}.  The output may be intermixed
201.1Sapb# 		with debugging information, which can be removed by the
211.1Sapb# 		${_PARAMS_POSTPROCESS} command.
221.1Sapb#
231.1Sapb# Internal variables:
241.1Sapb#	_PARAMS_POSTPROCESS
251.1Sapb#		A command to postprocess the output from "make _params",
261.1Sapb#		to remove debugging information and other noise.
271.1Sapb#
281.1Sapb# Example:
291.1Sapb#	. ${NETBSDSRCDIR}/etc/Makefile.params
301.1Sapb#	show-params: .MAKE .PHONY # print params to stdout
311.1Sapb#		@${PRINT_PARAMS}
321.1Sapb#
331.1Sapb
341.1Sapb.include <bsd.sys.mk>		# for TOOL_AWK, ...
351.1Sapb
361.1SapbRELEASEVARS=	BSDOBJDIR BSDSRCDIR BUILDID \
371.1Sapb		DESTDIR DISTRIBVER EXTERNAL_TOOLCHAIN HAVE_GCC HAVE_GDB \
381.1Sapb		INSTALLWORLDDIR \
391.1Sapb		KERNARCHDIR KERNCONFDIR KERNOBJDIR KERNSRCDIR \
401.1Sapb		MACHINE MACHINE_ARCH MAKE MAKECONF MAKEFLAGS \
411.1Sapb		MAKEOBJDIR MAKEOBJDIRPREFIX MAKEVERBOSE \
421.1Sapb		MKBFD MKBINUTILS MKCATPAGES \
431.1Sapb		MKCRYPTO MKCRYPTO_RC5 MKCVS \
441.1Sapb		MKDEBUG MKDEBUGLIB MKDOC MKDTRACE MKDYNAMICROOT \
451.1Sapb		MKGCC MKGCCCMDS MKGDB \
461.1Sapb		MKHESIOD MKHTML MKIEEEFP MKINET6 MKINFO MKIPFILTER \
471.1Sapb		MKKERBEROS MKLDAP MKLINKLIB MKLINT \
481.1Sapb		MKMAN MKMANZ MKMDNS MKNLS MKNPF MKOBJ MKOBJDIRS \
491.1Sapb		MKPAM MKPF MKPIC MKPICINSTALL MKPICLIB MKPOSTFIX MKPROFILE \
501.1Sapb		MKSHARE MKSKEY MKSOFTFLOAT MKSTATICLIB \
511.1Sapb		MKUNPRIVED MKUPDATE MKX11 MKYP \
521.1Sapb		NBUILDJOBS NETBSDSRCDIR \
531.1Sapb		NOCLEANDIR NODISTRIBDIRS NOINCLUDES \
541.1Sapb		OBJMACHINE \
551.1Sapb		RELEASEDIR RELEASEMACHINEDIR TOOLCHAIN_MISSING TOOLDIR \
561.1Sapb		USE_HESIOD USE_INET6 USE_JEMALLOC USE_KERBEROS USE_LDAP \
571.1Sapb		USE_PAM USE_SKEY USE_YP \
581.1Sapb		USETOOLS USR_OBJMACHINE \
591.1Sapb		X11SRCDIR X11FLAVOUR
601.1Sapb
611.1SapbPRINT_PARAMS= (cd ${.CURDIR}; ${MAKE} _params) | ${_PARAMS_POSTPROCESS}
621.1Sapb
631.1Sapb_params: .PHONY
641.1Sapb.for var in ${RELEASEVARS}
651.1Sapb.if defined(${var})
661.1Sapb	@printf "%20s = '%-s'\n" ${var} ${${var}:C/'/'\\\\''/gW:Q}
671.1Sapb.else
681.1Sapb	@printf "%20s = (undefined)\n" ${var}
691.1Sapb.endif
701.1Sapb.endfor
711.1Sapb
721.1Sapb# _PARAMS_POSTPROCESS:
731.1Sapb#
741.1Sapb# The output from the "make _params" can include the following types of
751.1Sapb# unwanted lines:
761.1Sapb#
771.1Sapb#     make -j prints "--- params ---";
781.1Sapb#
791.1Sapb#     if MAKEVERBOSE is set to 3 or more then make prints each "printf"
801.1Sapb#     command in addition to executing it;
811.1Sapb#
821.1Sapb#     if MAKEVERBOSE is set to 4 then the shell prints each command
831.1Sapb#     (prefixed with "+").
841.1Sapb#
851.1Sapb# So the resulting output can look like this:
861.1Sapb#
871.1Sapb#	--- params ---
881.1Sapb#	+ echo 'printf "%20s = '\''%-s'\''\n" BSDOBJDIR /usr/obj'
891.1Sapb#	printf "%20s = '%-s'\n" BSDOBJDIR /usr/obj
901.1Sapb#	+ printf '%20s = '\''%-s'\''\n' BSDOBJDIR /usr/obj
911.1Sapb#	           BSDOBJDIR = '/usr/obj'
921.1Sapb#	+ echo 'printf "%20s = '\''%-s'\''\n" BSDSRCDIR /usr/src'
931.1Sapb#	printf "%20s = '%-s'\n" BSDSRCDIR /usr/src
941.1Sapb#	+ printf '%20s = '\''%-s'\''\n' BSDSRCDIR /usr/src
951.1Sapb#	           BSDSRCDIR = '/usr/src'
961.1Sapb#	[...]
971.1Sapb#
981.1Sapb# where what we want is just this:
991.1Sapb#
1001.1Sapb#	           BSDOBJDIR = '/usr/obj'
1011.1Sapb#	           BSDSRCDIR = '/usr/src'
1021.1Sapb#	           [...]
1031.1Sapb#
1041.1Sapb# The awk program in ${PARAMS_POSTPROCESS} removes the unwanted noise,
1051.1Sapb# taking care with variables whose values contain embedded newlines
1061.1Sapb# (assuming that embedded newlines appear only inside single quotes).
1071.1Sapb#
1081.1Sapb_PARAMS_POSTPROCESS= ${TOOL_AWK} '\
1091.1Sapb	BEGIN { single_quote = "'\''"; \
1101.1Sapb		NORMAL = 0; \
1111.1Sapb		SKIP_HEADING = 1; \
1121.1Sapb		SKIP_MULTILINE = 2; \
1131.1Sapb		PRINT_MULTILINE = 3; \
1141.1Sapb		state = SKIP_HEADING; \
1151.1Sapb	} \
1161.1Sapb	function quotes_balanced_p(line) { \
1171.1Sapb		return (line ~ /^([^\\"'\'']|\\.|'\''[^'\'']*'\''|"(\\.|[^\\"])*")*$$/); \
1181.1Sapb	} \
1191.1Sapb	state == SKIP_MULTILINE { \
1201.1Sapb		if (quotes_balanced_p(single_quote $$0)) { \
1211.1Sapb			state = NORMAL; \
1221.1Sapb		} \
1231.1Sapb		next; \
1241.1Sapb	} \
1251.1Sapb	state == PRINT_MULTILINE { \
1261.1Sapb		if (quotes_balanced_p(single_quote $$0)) { \
1271.1Sapb			state = NORMAL; \
1281.1Sapb		} \
1291.1Sapb		print; next; \
1301.1Sapb	} \
1311.1Sapb	state == SKIP_HEADING && $$0 ~ /^--- .* ---$$/ { next; } \
1321.1Sapb	state == SKIP_HEADING && $$0 ~ / ===> / { next; } \
1331.1Sapb	/^(\+ )?(echo ["'\''])?printf.* = / { \
1341.1Sapb		if (quotes_balanced_p($$0)) { \
1351.1Sapb			state = NORMAL; \
1361.1Sapb		} else { \
1371.1Sapb			state = SKIP_MULTILINE; \
1381.1Sapb		} \
1391.1Sapb		next; \
1401.1Sapb	} \
1411.1Sapb	// { \
1421.1Sapb		if (quotes_balanced_p($$0)) { \
1431.1Sapb			state = NORMAL; \
1441.1Sapb		} else { \
1451.1Sapb			state = PRINT_MULTILINE; \
1461.1Sapb		} \
1471.1Sapb		print; next; \
1481.1Sapb	} \
1491.1Sapb	'
150