2020-03-23 12:27:29 +01:00
|
|
|
# $NetBSD: subst.mk,v 1.72 2020/03/23 11:27:29 jperkin Exp $
|
2003-08-27 13:24:37 +02:00
|
|
|
#
|
2019-04-28 14:31:15 +02:00
|
|
|
# The subst framework replaces text in one or more files in the WRKSRC
|
|
|
|
# directory. Packages can define several ``classes'' of replacements.
|
|
|
|
# Each such class defines:
|
|
|
|
#
|
|
|
|
# - in which stage of the build process the replacement happens
|
|
|
|
# - which files are affected by the replacement
|
|
|
|
# - which text or pattern is replaced by which replacement text
|
|
|
|
#
|
|
|
|
# A typical example is:
|
|
|
|
#
|
|
|
|
# SUBST_CLASSES+= prefix
|
|
|
|
# SUBST_STAGE.prefix= pre-configure
|
|
|
|
# SUBST_FILES.prefix= ./configure doc/*.html
|
|
|
|
# SUBST_SED.prefix= -e 's,/usr/local,${PREFIX},g'
|
2003-08-27 13:24:37 +02:00
|
|
|
#
|
2020-03-21 14:30:35 +01:00
|
|
|
# User-settable variables:
|
|
|
|
#
|
|
|
|
# SUBST_SHOW_DIFF
|
|
|
|
# Whether to log each changed file as a unified diff, for all
|
|
|
|
# SUBST classes. Defaults to "no".
|
|
|
|
#
|
2006-11-09 03:06:29 +01:00
|
|
|
# Package-settable variables:
|
2003-08-27 13:24:37 +02:00
|
|
|
#
|
2004-03-01 10:57:10 +01:00
|
|
|
# SUBST_CLASSES
|
2008-01-23 02:44:28 +01:00
|
|
|
# A list of class names. When adding new classes to this list, be
|
|
|
|
# sure to append them (+=) instead of overriding them (=).
|
2004-03-01 10:57:10 +01:00
|
|
|
#
|
2003-08-27 13:24:37 +02:00
|
|
|
# SUBST_STAGE.<class>
|
2005-08-22 00:00:07 +02:00
|
|
|
# "stage" at which we do the text replacement. Should be one of
|
|
|
|
# {pre,do,post}-{extract,patch,configure,build,install}.
|
2003-08-27 13:24:37 +02:00
|
|
|
#
|
|
|
|
# SUBST_MESSAGE.<class>
|
2008-01-18 12:26:11 +01:00
|
|
|
# The message to display before doing the substitution.
|
2004-06-06 06:22:23 +02:00
|
|
|
#
|
2003-08-27 13:24:37 +02:00
|
|
|
# SUBST_FILES.<class>
|
2005-08-22 00:00:07 +02:00
|
|
|
# A list of file patterns on which to run the substitution;
|
2006-01-06 00:16:01 +01:00
|
|
|
# the filenames are either absolute or relative to ${WRKSRC}.
|
2003-08-27 13:24:37 +02:00
|
|
|
#
|
2020-03-19 17:57:35 +01:00
|
|
|
# Starting with 2020Q1, it is an error if any of these patterns
|
|
|
|
# has no effect at all, to catch typos and outdated definitions.
|
|
|
|
# To prevent this, see SUBST_NOOP_OK.<class> below.
|
|
|
|
#
|
2020-03-20 10:00:44 +01:00
|
|
|
# In most cases the filename patterns are given directly.
|
|
|
|
# If that is not flexible enough, use the :sh variable modifier.
|
|
|
|
# See mk/configure/replace-localedir.mk for an example.
|
|
|
|
#
|
2003-08-27 13:24:37 +02:00
|
|
|
# SUBST_SED.<class>
|
2013-10-13 23:38:36 +02:00
|
|
|
# List of sed(1) arguments to run on the specified files.
|
|
|
|
# Multiple commands can be specified using the -e option of sed.
|
|
|
|
# Do not use non-standard sed options (e.g. -E).
|
2003-10-07 12:19:09 +02:00
|
|
|
#
|
2007-01-23 07:05:39 +01:00
|
|
|
# SUBST_VARS.<class>
|
|
|
|
# List of variables that are substituted whenever they appear in
|
2019-04-28 14:31:15 +02:00
|
|
|
# the form @VARNAME@. This is basically a shortcut for
|
2007-01-23 07:05:39 +01:00
|
|
|
#
|
|
|
|
# -e 's,@VARNAME@,${VARNAME},g'
|
|
|
|
#
|
2019-04-28 14:31:15 +02:00
|
|
|
# that even works when ${VARNAME} contains arbitrary characters.
|
2020-03-19 17:57:35 +01:00
|
|
|
# SUBST_SED and SUBST_VARS can combined freely.
|
2007-01-23 07:05:39 +01:00
|
|
|
#
|
2003-10-07 12:19:09 +02:00
|
|
|
# SUBST_FILTER_CMD.<class>
|
2005-08-22 00:00:07 +02:00
|
|
|
# Filter used to perform the actual substitution on the specified
|
2003-10-07 12:19:09 +02:00
|
|
|
# files. Defaults to ${SED} ${SUBST_SED.<class>}.
|
2004-06-06 06:22:23 +02:00
|
|
|
#
|
2007-01-14 18:05:02 +01:00
|
|
|
# SUBST_SKIP_TEXT_CHECK.<class>
|
|
|
|
# By default, each file is checked whether it really is a text file
|
|
|
|
# before any substitutions are done to it. Since that test is not
|
|
|
|
# perfect, it can be disabled by setting this variable to "yes".
|
|
|
|
#
|
2018-01-02 22:18:48 +01:00
|
|
|
# SUBST_SHOW_DIFF.<class>
|
|
|
|
# During development of a package, this can be set to "yes" to see
|
|
|
|
# the actual changes as a unified diff.
|
|
|
|
#
|
2020-03-19 17:57:35 +01:00
|
|
|
# SUBST_NOOP_OK.<class>
|
|
|
|
# Whether to fail when a SUBST_FILES pattern has no effect.
|
|
|
|
# In most cases, "yes" is appropriate, to catch typos and outdated
|
|
|
|
# definitions.
|
|
|
|
#
|
|
|
|
# Default: no (up to 2019Q4), yes (starting with 2020Q1)
|
|
|
|
#
|
2008-01-26 16:23:21 +01:00
|
|
|
# See also:
|
|
|
|
# PLIST_SUBST
|
|
|
|
#
|
2007-01-04 00:35:13 +01:00
|
|
|
# Keywords: subst
|
|
|
|
#
|
2003-08-27 13:24:37 +02:00
|
|
|
|
2007-03-15 23:54:24 +01:00
|
|
|
_VARGROUPS+= subst
|
|
|
|
_PKG_VARS.subst= SUBST_CLASSES
|
2020-03-21 14:30:35 +01:00
|
|
|
SUBST_SHOW_DIFF?= no
|
2007-03-15 23:54:24 +01:00
|
|
|
.for c in ${SUBST_CLASSES}
|
|
|
|
. for pv in SUBST_STAGE SUBST_MESSAGE SUBST_FILES SUBST_SED SUBST_VARS \
|
2020-03-19 17:57:35 +01:00
|
|
|
SUBST_FILTER_CMD SUBST_SKIP_TEXT_CHECK SUBST_NOOP_OK
|
2007-03-15 23:54:24 +01:00
|
|
|
_PKG_VARS.subst+= ${pv}.${c}
|
|
|
|
. endfor
|
|
|
|
.endfor
|
2019-09-08 11:06:06 +02:00
|
|
|
_DEF_VARS.subst= ECHO_SUBST_MSG
|
|
|
|
_USE_VARS.subst= WRKDIR WRKSRC
|
|
|
|
_IGN_VARS.subst= _SUBST_IS_TEXT_FILE_CMD
|
2018-11-30 19:38:19 +01:00
|
|
|
_SORTED_VARS.subst= SUBST_CLASSES SUBST_FILES.* SUBST_VARS.*
|
|
|
|
_LISTED_VARS.subst= SUBST_SED.* SUBST_FILTER_CMD.*
|
2007-03-15 23:54:24 +01:00
|
|
|
|
2006-06-18 00:42:00 +02:00
|
|
|
ECHO_SUBST_MSG?= ${STEP_MSG}
|
2003-08-27 13:24:37 +02:00
|
|
|
|
2019-09-08 11:06:06 +02:00
|
|
|
# _SUBST_IS_TEXT_FILE_CMD returns 0 if $$file is a text file.
|
|
|
|
_SUBST_IS_TEXT_FILE_CMD?= \
|
2019-11-22 19:04:49 +01:00
|
|
|
[ -z "`LC_ALL=C ${TR} -cd '\\0' < "$$file" | ${TR} '\\0' 'x'`" ]
|
2004-08-23 18:35:11 +02:00
|
|
|
|
2020-03-19 17:57:35 +01:00
|
|
|
.if ${SUBST_CLASSES:U:O} != ${SUBST_CLASSES:U:O:u}
|
2020-03-21 13:22:31 +01:00
|
|
|
PKG_FAIL_REASON+= "[subst.mk] duplicate SUBST class in: ${SUBST_CLASSES:O}"
|
2020-03-19 17:57:35 +01:00
|
|
|
.endif
|
|
|
|
|
2020-03-21 13:22:31 +01:00
|
|
|
.for _class_ in ${SUBST_CLASSES:O:u}
|
2003-08-27 13:24:37 +02:00
|
|
|
_SUBST_COOKIE.${_class_}= ${WRKDIR}/.subst_${_class_}_done
|
|
|
|
|
2019-11-22 19:04:49 +01:00
|
|
|
SUBST_FILTER_CMD.${_class_}?= LC_ALL=C ${SED} ${SUBST_SED.${_class_}}
|
2007-01-23 07:05:39 +01:00
|
|
|
SUBST_VARS.${_class_}?= # none
|
2008-01-18 12:26:11 +01:00
|
|
|
SUBST_MESSAGE.${_class_}?= Substituting "${_class_}" in ${SUBST_FILES.${_class_}}
|
2007-03-07 22:27:59 +01:00
|
|
|
. for v in ${SUBST_VARS.${_class_}}
|
2019-03-17 13:01:14 +01:00
|
|
|
SUBST_FILTER_CMD.${_class_}+= -e s,@${v:C|[^A-Za-z0-9_]|\\\\&|gW:Q}@,${${v}:S|\\|\\\\|gW:S|,|\\,|gW:S|&|\\\&|gW:S|${.newline}|\\${.newline}|gW:Q},g
|
2007-03-07 22:27:59 +01:00
|
|
|
. endfor
|
2020-03-21 14:30:35 +01:00
|
|
|
. if ${SUBST_SHOW_DIFF.${_class_}:U${SUBST_SHOW_DIFF}:tl} == yes
|
2020-03-21 20:26:12 +01:00
|
|
|
_SUBST_KEEP.${_class_}?= LC_ALL=C ${DIFF} -u "$$file" "$$tmpfile" || true
|
2018-01-02 22:18:48 +01:00
|
|
|
. endif
|
2016-01-31 18:27:41 +01:00
|
|
|
_SUBST_KEEP.${_class_}?= ${DO_NADA}
|
2007-01-14 18:05:02 +01:00
|
|
|
SUBST_SKIP_TEXT_CHECK.${_class_}?= no
|
2020-03-19 17:57:35 +01:00
|
|
|
SUBST_NOOP_OK.${_class_}?= yes # TODO: change to no after 2020Q1
|
2020-03-22 13:15:59 +01:00
|
|
|
_SUBST_WARN.${_class_}= ${${SUBST_NOOP_OK.${_class_}:tl} == yes:?${INFO_MSG}:${WARNING_MSG}} "[subst.mk:${_class_}]"
|
2007-01-14 18:05:02 +01:00
|
|
|
|
|
|
|
.if !empty(SUBST_SKIP_TEXT_CHECK.${_class_}:M[Yy][Ee][Ss])
|
2019-09-08 11:06:06 +02:00
|
|
|
_SUBST_IS_TEXT_FILE_CMD.${_class_}= ${TRUE}
|
2007-01-14 18:05:02 +01:00
|
|
|
.else
|
2019-09-08 11:06:06 +02:00
|
|
|
_SUBST_IS_TEXT_FILE_CMD.${_class_}= ${_SUBST_IS_TEXT_FILE_CMD}
|
2007-01-14 18:05:02 +01:00
|
|
|
.endif
|
2003-10-07 12:19:09 +02:00
|
|
|
|
|
|
|
. if defined(SUBST_STAGE.${_class_})
|
2003-08-27 13:24:37 +02:00
|
|
|
${SUBST_STAGE.${_class_}}: subst-${_class_}
|
2006-06-18 00:42:00 +02:00
|
|
|
. else
|
2006-06-22 22:45:48 +02:00
|
|
|
# SUBST_STAGE.* does not need to be defined.
|
|
|
|
#PKG_FAIL_REASON+= "SUBST_STAGE missing for ${_class_}."
|
2003-10-07 12:19:09 +02:00
|
|
|
. endif
|
2003-08-27 13:24:37 +02:00
|
|
|
|
|
|
|
.PHONY: subst-${_class_}
|
2006-06-18 00:42:00 +02:00
|
|
|
subst-${_class_}: ${_SUBST_COOKIE.${_class_}}
|
2003-08-27 13:24:37 +02:00
|
|
|
|
2006-06-18 00:42:00 +02:00
|
|
|
${_SUBST_COOKIE.${_class_}}:
|
2020-03-20 10:00:44 +01:00
|
|
|
${RUN} message=${SUBST_MESSAGE.${_class_}:Q}; \
|
|
|
|
if [ "$$message" ]; then ${ECHO_SUBST_MSG} "$$message"; fi
|
2020-03-20 07:17:48 +01:00
|
|
|
${RUN} \
|
|
|
|
basedir=${WRKSRC:Q}; \
|
|
|
|
emptydir="$$basedir/.subst-empty"; \
|
|
|
|
patterns=${SUBST_FILES.${_class_}:Q}; \
|
|
|
|
${MKDIR} "$$emptydir"; cd "$$emptydir"; \
|
|
|
|
for pattern in $$patterns; do \
|
2020-03-19 17:57:35 +01:00
|
|
|
changed=no; \
|
2020-03-20 07:17:48 +01:00
|
|
|
cd "$$basedir"; \
|
|
|
|
for file in $$pattern; do \
|
2005-12-06 18:17:30 +01:00
|
|
|
case $$file in /*) ;; *) file="./$$file";; esac; \
|
2019-09-08 11:06:06 +02:00
|
|
|
tmpfile="$$file.subst.sav"; \
|
2007-11-01 14:12:48 +01:00
|
|
|
if [ ! -f "$$file" ]; then \
|
2020-03-22 14:19:50 +01:00
|
|
|
[ -d "$$file" ] || ${_SUBST_WARN.${_class_}} "Ignoring non-existent file \"$$file\"."; \
|
2019-09-08 11:06:06 +02:00
|
|
|
elif ${_SUBST_IS_TEXT_FILE_CMD.${_class_}}; then \
|
2006-01-01 23:52:16 +01:00
|
|
|
${SUBST_FILTER_CMD.${_class_}} \
|
2016-01-31 18:27:41 +01:00
|
|
|
< "$$file" \
|
|
|
|
> "$$tmpfile"; \
|
|
|
|
if ${TEST} -x "$$file"; then \
|
|
|
|
${CHMOD} +x "$$tmpfile"; \
|
2003-08-27 13:24:37 +02:00
|
|
|
fi; \
|
2005-05-20 23:36:05 +02:00
|
|
|
if ${CMP} -s "$$tmpfile" "$$file"; then \
|
2020-03-22 13:15:59 +01:00
|
|
|
${_SUBST_WARN.${_class_}} "Nothing changed in $$file."; \
|
2016-01-31 18:27:41 +01:00
|
|
|
${RM} -f "$$tmpfile"; \
|
2005-05-20 23:36:05 +02:00
|
|
|
else \
|
2020-03-19 17:57:35 +01:00
|
|
|
changed=yes; \
|
2016-01-31 18:27:41 +01:00
|
|
|
${_SUBST_KEEP.${_class_}}; \
|
|
|
|
${MV} -f "$$tmpfile" "$$file"; \
|
2020-03-22 19:43:46 +01:00
|
|
|
${ECHO} "$$file" >> ${.TARGET}.tmp; \
|
2005-05-20 23:36:05 +02:00
|
|
|
fi; \
|
2005-11-24 21:02:40 +01:00
|
|
|
else \
|
2020-03-22 13:15:59 +01:00
|
|
|
${_SUBST_WARN.${_class_}} "Ignoring non-text file \"$$file\"."; \
|
2005-05-20 23:36:05 +02:00
|
|
|
fi; \
|
2020-03-19 17:57:35 +01:00
|
|
|
done; \
|
|
|
|
\
|
|
|
|
if test "$$changed,${SUBST_NOOP_OK.${_class_}:tl}" = no,no; then \
|
2020-03-20 07:17:48 +01:00
|
|
|
${FAIL_MSG} "[subst.mk:${_class_}] The pattern $$pattern has no effect."; \
|
|
|
|
fi; \
|
|
|
|
done; \
|
2020-03-23 12:27:29 +01:00
|
|
|
cd ${WRKDIR}; ${RMDIR} "$$emptydir"
|
2020-03-22 19:43:46 +01:00
|
|
|
${RUN} ${TOUCH} ${TOUCH_FLAGS} ${.TARGET}.tmp && ${MV} ${.TARGET}.tmp ${.TARGET}
|
2003-08-27 13:24:37 +02:00
|
|
|
.endfor
|