From 8e0416b05d86783f7c052d511c9d3a5c5691084d Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Thu, 12 Oct 2023 23:43:03 +0200 Subject: [PATCH 1/5] stdenv: substituteStream: deprecate --replace in favor of --replace-{fail,warn,quiet} --- .../emscripten.section.md | 6 ++--- doc/languages-frameworks/rust.section.md | 2 +- doc/stdenv/platform-notes.chapter.md | 2 +- doc/stdenv/stdenv.chapter.md | 23 +++++++++++----- pkgs/stdenv/generic/setup.sh | 27 ++++++++++++++++++- 5 files changed, 48 insertions(+), 12 deletions(-) diff --git a/doc/languages-frameworks/emscripten.section.md b/doc/languages-frameworks/emscripten.section.md index 20d358f2e9e3..9ce48db2c2de 100644 --- a/doc/languages-frameworks/emscripten.section.md +++ b/doc/languages-frameworks/emscripten.section.md @@ -86,9 +86,9 @@ One advantage is that when `pkgs.zlib` is updated, it will automatically update postPatch = pkgs.lib.optionalString pkgs.stdenv.isDarwin '' substituteInPlace configure \ - --replace '/usr/bin/libtool' 'ar' \ - --replace 'AR="libtool"' 'AR="ar"' \ - --replace 'ARFLAGS="-o"' 'ARFLAGS="-r"' + --replace-fail '/usr/bin/libtool' 'ar' \ + --replace-fail 'AR="libtool"' 'AR="ar"' \ + --replace-fail 'ARFLAGS="-o"' 'ARFLAGS="-r"' ''; }) ``` diff --git a/doc/languages-frameworks/rust.section.md b/doc/languages-frameworks/rust.section.md index d18b048b911b..6ba2463e150e 100644 --- a/doc/languages-frameworks/rust.section.md +++ b/doc/languages-frameworks/rust.section.md @@ -700,7 +700,7 @@ with import {}; hello = attrs: lib.optionalAttrs (lib.versionAtLeast attrs.version "1.0") { postPatch = '' substituteInPlace lib/zoneinfo.rs \ - --replace "/usr/share/zoneinfo" "${tzdata}/share/zoneinfo" + --replace-fail "/usr/share/zoneinfo" "${tzdata}/share/zoneinfo" ''; }; }; diff --git a/doc/stdenv/platform-notes.chapter.md b/doc/stdenv/platform-notes.chapter.md index b47f5af349b8..409c9f2e7b2e 100644 --- a/doc/stdenv/platform-notes.chapter.md +++ b/doc/stdenv/platform-notes.chapter.md @@ -54,7 +54,7 @@ Some common issues when packaging software for Darwin: # ... prePatch = '' substituteInPlace Makefile \ - --replace '/usr/bin/xcrun clang' clang + --replace-fail '/usr/bin/xcrun clang' clang ''; } ``` diff --git a/doc/stdenv/stdenv.chapter.md b/doc/stdenv/stdenv.chapter.md index 03bb8a9ff790..7f62af5997b5 100644 --- a/doc/stdenv/stdenv.chapter.md +++ b/doc/stdenv/stdenv.chapter.md @@ -230,9 +230,9 @@ stdenv.mkDerivation rec { postInstall = '' substituteInPlace $out/bin/solo5-virtio-mkimage \ - --replace "/usr/lib/syslinux" "${syslinux}/share/syslinux" \ - --replace "/usr/share/syslinux" "${syslinux}/share/syslinux" \ - --replace "cp " "cp --no-preserve=mode " + --replace-fail "/usr/lib/syslinux" "${syslinux}/share/syslinux" \ + --replace-fail "/usr/share/syslinux" "${syslinux}/share/syslinux" \ + --replace-fail "cp " "cp --no-preserve=mode " wrapProgram $out/bin/solo5-virtio-mkimage \ --prefix PATH : ${lib.makeBinPath [ dosfstools mtools parted syslinux ]} @@ -1217,9 +1217,20 @@ postInstall = '' Performs string substitution on the contents of \, writing the result to \. The substitutions in \ are of the following form: -#### `--replace` \ \ {#fun-substitute-replace} +#### `--replace-fail` \ \ {#fun-substitute-replace-fail} Replace every occurrence of the string \ by \. +Will error if no change is made. + +#### `--replace-warn` \ \ {#fun-substitute-replace-warn} + +Replace every occurrence of the string \ by \. +Will print a warning if no change is made. + +#### `--replace-quiet` \ \ {#fun-substitute-replace-quiet} + +Replace every occurrence of the string \ by \. +Will do nothing if no change can be made. #### `--subst-var` \ {#fun-substitute-subst-var} @@ -1233,8 +1244,8 @@ Example: ```shell substitute ./foo.in ./foo.out \ - --replace /usr/bin/bar $bar/bin/bar \ - --replace "a string containing spaces" "some other text" \ + --replace-fail /usr/bin/bar $bar/bin/bar \ + --replace-fail "a string containing spaces" "some other text" \ --subst-var someVar ``` diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh index 780ef709683b..a3dc2e8378a2 100644 --- a/pkgs/stdenv/generic/setup.sh +++ b/pkgs/stdenv/generic/setup.sh @@ -815,6 +815,8 @@ fi ###################################################################### # Textual substitution functions. +# only log once, due to max logging limit on hydra +_substituteStream_has_warned_replace_deprecation="" substituteStream() { local var=$1 @@ -822,8 +824,24 @@ substituteStream() { shift 2 while (( "$#" )); do + local is_required=1 + local is_quiet="" case "$1" in + --replace-quiet) + is_quiet=1 + ;& --replace) + # deprecated 2023-11-22 + # this will either get removed, or switch to the behaviour of --replace-fail in the future + if [ -z "$_substituteStream_has_warned_replace_deprecation" ]; then + echo "substituteStream(): WARNING: '--replace' is deprecated, use --replace-{fail,warn,quiet}. ($description)" >&2 + _substituteStream_has_warned_replace_deprecation=1 + fi + ;& + --replace-warn) + is_required="" + ;& + --replace-fail) pattern="$2" replacement="$3" shift 3 @@ -832,7 +850,14 @@ substituteStream() { eval "$var"'=${'"$var"'//"$pattern"/"$replacement"}' if [ "$pattern" != "$replacement" ]; then if [ "${!var}" == "$savedvar" ]; then - echo "substituteStream(): WARNING: pattern '$pattern' doesn't match anything in $description" >&2 + if [ -z "$is_required" ]; then + if [ -z "$is_quiet" ]; then + echo "substituteStream(): WARNING: pattern '$pattern' doesn't match anything in $description" >&2 + fi + else + echo "substituteStream(): ERROR: pattern '$pattern' doesn't match anything in $description" >&2 + return 1 + fi fi fi ;; From 407b3d1a92c53c23829f8fb887b3172539b81066 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Wed, 22 Nov 2023 19:48:45 +0100 Subject: [PATCH 2/5] stdenv: substituteStream: escape echoed pattern in --replace mismatch warning --- pkgs/stdenv/generic/setup.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh index a3dc2e8378a2..71c0f32cd963 100644 --- a/pkgs/stdenv/generic/setup.sh +++ b/pkgs/stdenv/generic/setup.sh @@ -852,10 +852,10 @@ substituteStream() { if [ "${!var}" == "$savedvar" ]; then if [ -z "$is_required" ]; then if [ -z "$is_quiet" ]; then - echo "substituteStream(): WARNING: pattern '$pattern' doesn't match anything in $description" >&2 + printf "substituteStream(): WARNING: pattern %q doesn't match anything in %s\n" "$pattern" "$description" >&2 fi else - echo "substituteStream(): ERROR: pattern '$pattern' doesn't match anything in $description" >&2 + printf "substituteStream(): ERROR: pattern %q doesn't match anything in %s\n" "$pattern" "$description" >&2 return 1 fi fi From 54724f2c5f67c0a88968f9e36f8ed088370b99df Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Mon, 5 Feb 2024 17:28:02 +0100 Subject: [PATCH 3/5] stdenv: fix `substituteStream --replace-quiet` deprecation warning --- pkgs/stdenv/generic/setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh index 71c0f32cd963..51065a9a7476 100644 --- a/pkgs/stdenv/generic/setup.sh +++ b/pkgs/stdenv/generic/setup.sh @@ -833,7 +833,7 @@ substituteStream() { --replace) # deprecated 2023-11-22 # this will either get removed, or switch to the behaviour of --replace-fail in the future - if [ -z "$_substituteStream_has_warned_replace_deprecation" ]; then + if [ -z "$is_quiet" ] && [ -z "$_substituteStream_has_warned_replace_deprecation" ]; then echo "substituteStream(): WARNING: '--replace' is deprecated, use --replace-{fail,warn,quiet}. ($description)" >&2 _substituteStream_has_warned_replace_deprecation=1 fi From 59ccadbd4c8beea42a5cf0210278b4dd7a9fb684 Mon Sep 17 00:00:00 2001 From: Kait Lam Date: Sun, 11 Feb 2024 10:48:37 +1000 Subject: [PATCH 4/5] stdenv: refactor of --replace-{quiet,warn,fail} logic This is a small simplification of the control flow surrounding these cases. It should make it more obvious when each case happens, and also explicitly defines the current behaviour of --replace. --- pkgs/stdenv/generic/setup.sh | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh index 51065a9a7476..61171c49da11 100644 --- a/pkgs/stdenv/generic/setup.sh +++ b/pkgs/stdenv/generic/setup.sh @@ -816,7 +816,7 @@ fi # Textual substitution functions. # only log once, due to max logging limit on hydra -_substituteStream_has_warned_replace_deprecation="" +_substituteStream_has_warned_replace_deprecation=false substituteStream() { local var=$1 @@ -824,24 +824,18 @@ substituteStream() { shift 2 while (( "$#" )); do - local is_required=1 - local is_quiet="" + local replace_mode="$1" case "$1" in - --replace-quiet) - is_quiet=1 - ;& --replace) # deprecated 2023-11-22 # this will either get removed, or switch to the behaviour of --replace-fail in the future - if [ -z "$is_quiet" ] && [ -z "$_substituteStream_has_warned_replace_deprecation" ]; then + if ! "$_substituteStream_has_warned_replace_deprecation"; then echo "substituteStream(): WARNING: '--replace' is deprecated, use --replace-{fail,warn,quiet}. ($description)" >&2 - _substituteStream_has_warned_replace_deprecation=1 + _substituteStream_has_warned_replace_deprecation=true fi + replace_mode='--replace-warn' ;& - --replace-warn) - is_required="" - ;& - --replace-fail) + --replace-quiet|--replace-warn|--replace-fail) pattern="$2" replacement="$3" shift 3 @@ -850,11 +844,9 @@ substituteStream() { eval "$var"'=${'"$var"'//"$pattern"/"$replacement"}' if [ "$pattern" != "$replacement" ]; then if [ "${!var}" == "$savedvar" ]; then - if [ -z "$is_required" ]; then - if [ -z "$is_quiet" ]; then - printf "substituteStream(): WARNING: pattern %q doesn't match anything in %s\n" "$pattern" "$description" >&2 - fi - else + if [ "$replace_mode" == --replace-warn ]; then + printf "substituteStream(): WARNING: pattern %q doesn't match anything in %s\n" "$pattern" "$description" >&2 + elif [ "$replace_mode" == --replace-fail ]; then printf "substituteStream(): ERROR: pattern %q doesn't match anything in %s\n" "$pattern" "$description" >&2 return 1 fi From 1160998253d8e25f0cf770c89ff7f6f39ba3f88f Mon Sep 17 00:00:00 2001 From: misuzu Date: Thu, 29 Feb 2024 08:44:22 +0200 Subject: [PATCH 5/5] stdenv: remove `substituteStream --replace` deprecation warning for stable --- pkgs/stdenv/generic/setup.sh | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh index 61171c49da11..a503266eb555 100644 --- a/pkgs/stdenv/generic/setup.sh +++ b/pkgs/stdenv/generic/setup.sh @@ -815,9 +815,6 @@ fi ###################################################################### # Textual substitution functions. -# only log once, due to max logging limit on hydra -_substituteStream_has_warned_replace_deprecation=false - substituteStream() { local var=$1 local description=$2 @@ -827,12 +824,6 @@ substituteStream() { local replace_mode="$1" case "$1" in --replace) - # deprecated 2023-11-22 - # this will either get removed, or switch to the behaviour of --replace-fail in the future - if ! "$_substituteStream_has_warned_replace_deprecation"; then - echo "substituteStream(): WARNING: '--replace' is deprecated, use --replace-{fail,warn,quiet}. ($description)" >&2 - _substituteStream_has_warned_replace_deprecation=true - fi replace_mode='--replace-warn' ;& --replace-quiet|--replace-warn|--replace-fail)