Merge pull request #172769 from ncfavier/wrappers-append-args

makeWrapper,makeBinaryWrapper: implement `--append-flags`
This commit is contained in:
Thiago Kenji Okada 2022-06-12 11:46:03 +01:00 committed by GitHub
commit 299538e834
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 80 additions and 57 deletions

View file

@ -57,7 +57,12 @@ let
]; ];
preFixup = '' preFixup = ''
gappsWrapperArgs+=(--prefix PATH : "${coreutils}/bin:${gawk}/bin") gappsWrapperArgs+=(
--prefix PATH : "${coreutils}/bin:${gawk}/bin"
# fix for https://docs.microsoft.com/en-us/answers/questions/298724/open-teams-meeting-link-on-linux-doens39t-work.html?childToView=309406#comment-309406
--append-flags '--disable-namespace-sandbox --disable-setuid-sandbox'
)
''; '';
@ -118,15 +123,6 @@ let
echo "Adding runtime dependencies to RPATH of Node module $mod" echo "Adding runtime dependencies to RPATH of Node module $mod"
patchelf --set-rpath "$runtime_rpath:$mod_rpath" "$mod" patchelf --set-rpath "$runtime_rpath:$mod_rpath" "$mod"
done; done;
# fix for https://docs.microsoft.com/en-us/answers/questions/298724/open-teams-meeting-link-on-linux-doens39t-work.html?childToView=309406#comment-309406
wrapped=$out/bin/.teams-old
mv "$out/bin/teams" "$wrapped"
cat > "$out/bin/teams" << EOF
#! ${runtimeShell}
exec $wrapped "\$@" --disable-namespace-sandbox --disable-setuid-sandbox
EOF
chmod +x "$out/bin/teams"
''; '';
}; };

View file

@ -15,17 +15,19 @@ assertExecutable() {
# makeWrapper EXECUTABLE OUT_PATH ARGS # makeWrapper EXECUTABLE OUT_PATH ARGS
# ARGS: # ARGS:
# --argv0 NAME : set the name of the executed process to NAME # --argv0 NAME : set the name of the executed process to NAME
# (if unset or empty, defaults to EXECUTABLE) # (if unset or empty, defaults to EXECUTABLE)
# --inherit-argv0 : the executable inherits argv0 from the wrapper. # --inherit-argv0 : the executable inherits argv0 from the wrapper.
# (use instead of --argv0 '$0') # (use instead of --argv0 '$0')
# --set VAR VAL : add VAR with value VAL to the executable's environment # --set VAR VAL : add VAR with value VAL to the executable's environment
# --set-default VAR VAL : like --set, but only adds VAR if not already set in # --set-default VAR VAL : like --set, but only adds VAR if not already set in
# the environment # the environment
# --unset VAR : remove VAR from the environment # --unset VAR : remove VAR from the environment
# --chdir DIR : change working directory (use instead of --run "cd DIR") # --chdir DIR : change working directory (use instead of --run "cd DIR")
# --add-flags FLAGS : add FLAGS to invocation of executable # --add-flags ARGS : prepend ARGS to the invocation of the executable
# TODO(@ncfavier): --append-flags # (that is, *before* any arguments passed on the command line)
# --append-flags ARGS : append ARGS to the invocation of the executable
# (that is, *after* any arguments passed on the command line)
# --prefix ENV SEP VAL : suffix/prefix ENV with VAL, separated by SEP # --prefix ENV SEP VAL : suffix/prefix ENV with VAL, separated by SEP
# --suffix # --suffix
@ -83,7 +85,7 @@ makeDocumentedCWrapper() {
# makeCWrapper EXECUTABLE ARGS # makeCWrapper EXECUTABLE ARGS
# ARGS: same as makeWrapper # ARGS: same as makeWrapper
makeCWrapper() { makeCWrapper() {
local argv0 inherit_argv0 n params cmd main flagsBefore flags executable length local argv0 inherit_argv0 n params cmd main flagsBefore flagsAfter flags executable length
local uses_prefix uses_suffix uses_assert uses_assert_success uses_stdio uses_asprintf local uses_prefix uses_suffix uses_assert uses_assert_success uses_stdio uses_asprintf
executable=$(escapeStringLiteral "$1") executable=$(escapeStringLiteral "$1")
params=("$@") params=("$@")
@ -150,6 +152,13 @@ makeCWrapper() {
n=$((n + 1)) n=$((n + 1))
[ $n -ge "$length" ] && main="$main#error makeCWrapper: $p takes 1 argument"$'\n' [ $n -ge "$length" ] && main="$main#error makeCWrapper: $p takes 1 argument"$'\n'
;; ;;
--append-flags)
flags="${params[n + 1]}"
flagsAfter="$flagsAfter $flags"
uses_assert=1
n=$((n + 1))
[ $n -ge "$length" ] && main="$main#error makeCWrapper: $p takes 1 argument"$'\n'
;;
--argv0) --argv0)
argv0=$(escapeStringLiteral "${params[n + 1]}") argv0=$(escapeStringLiteral "${params[n + 1]}")
inherit_argv0= inherit_argv0=
@ -165,8 +174,7 @@ makeCWrapper() {
;; ;;
esac esac
done done
# shellcheck disable=SC2086 [[ -z "$flagsBefore" && -z "$flagsAfter" ]] || main="$main"${main:+$'\n'}$(addFlags "$flagsBefore" "$flagsAfter")$'\n'$'\n'
[ -z "$flagsBefore" ] || main="$main"${main:+$'\n'}$(addFlags $flagsBefore)$'\n'$'\n'
[ -z "$inherit_argv0" ] && main="${main}argv[0] = \"${argv0:-${executable}}\";"$'\n' [ -z "$inherit_argv0" ] && main="${main}argv[0] = \"${argv0:-${executable}}\";"$'\n'
main="${main}return execv(\"${executable}\", argv);"$'\n' main="${main}return execv(\"${executable}\", argv);"$'\n'
@ -184,21 +192,25 @@ makeCWrapper() {
} }
addFlags() { addFlags() {
local result n flag flags var local n flag before after var
# shellcheck disable=SC2086
before=($1) after=($2)
var="argv_tmp" var="argv_tmp"
flags=("$@") printf '%s\n' "char **$var = calloc(${#before[@]} + argc + ${#after[@]} + 1, sizeof(*$var));"
for ((n = 0; n < ${#flags[*]}; n += 1)); do
flag=$(escapeStringLiteral "${flags[$n]}")
result="$result${var}[$((n+1))] = \"$flag\";"$'\n'
done
printf '%s\n' "char **$var = calloc($((n+1)) + argc, sizeof(*$var));"
printf '%s\n' "assert($var != NULL);" printf '%s\n' "assert($var != NULL);"
printf '%s\n' "${var}[0] = argv[0];" printf '%s\n' "${var}[0] = argv[0];"
printf '%s' "$result" for ((n = 0; n < ${#before[@]}; n += 1)); do
flag=$(escapeStringLiteral "${before[n]}")
printf '%s\n' "${var}[$((n + 1))] = \"$flag\";"
done
printf '%s\n' "for (int i = 1; i < argc; ++i) {" printf '%s\n' "for (int i = 1; i < argc; ++i) {"
printf '%s\n' " ${var}[$n + i] = argv[i];" printf '%s\n' " ${var}[${#before[@]} + i] = argv[i];"
printf '%s\n' "}" printf '%s\n' "}"
printf '%s\n' "${var}[$n + argc] = NULL;" for ((n = 0; n < ${#after[@]}; n += 1)); do
flag=$(escapeStringLiteral "${after[n]}")
printf '%s\n' "${var}[${#before[@]} + argc + $n] = \"$flag\";"
done
printf '%s\n' "${var}[${#before[@]} + argc + ${#after[@]}] = NULL;"
printf '%s\n' "argv = $var;" printf '%s\n' "argv = $var;"
} }
@ -366,6 +378,10 @@ formatArgs() {
formatArgsLine 1 "$@" formatArgsLine 1 "$@"
shift 1 shift 1
;; ;;
--append-flags)
formatArgsLine 1 "$@"
shift 1
;;
--argv0) --argv0)
formatArgsLine 1 "$@" formatArgsLine 1 "$@"
shift 1 shift 1

View file

@ -11,18 +11,20 @@ assertExecutable() {
# makeWrapper EXECUTABLE OUT_PATH ARGS # makeWrapper EXECUTABLE OUT_PATH ARGS
# ARGS: # ARGS:
# --argv0 NAME : set the name of the executed process to NAME # --argv0 NAME : set the name of the executed process to NAME
# (if unset or empty, defaults to EXECUTABLE) # (if unset or empty, defaults to EXECUTABLE)
# --inherit-argv0 : the executable inherits argv0 from the wrapper. # --inherit-argv0 : the executable inherits argv0 from the wrapper.
# (use instead of --argv0 '$0') # (use instead of --argv0 '$0')
# --set VAR VAL : add VAR with value VAL to the executable's environment # --set VAR VAL : add VAR with value VAL to the executable's environment
# --set-default VAR VAL : like --set, but only adds VAR if not already set in # --set-default VAR VAL : like --set, but only adds VAR if not already set in
# the environment # the environment
# --unset VAR : remove VAR from the environment # --unset VAR : remove VAR from the environment
# --chdir DIR : change working directory (use instead of --run "cd DIR") # --chdir DIR : change working directory (use instead of --run "cd DIR")
# --run COMMAND : run command before the executable # --run COMMAND : run command before the executable
# --add-flags FLAGS : add FLAGS to invocation of executable # --add-flags ARGS : prepend ARGS to the invocation of the executable
# TODO(@ncfavier): --append-flags # (that is, *before* any arguments passed on the command line)
# --append-flags ARGS : append ARGS to the invocation of the executable
# (that is, *after* any arguments passed on the command line)
# --prefix ENV SEP VAL : suffix/prefix ENV with VAL, separated by SEP # --prefix ENV SEP VAL : suffix/prefix ENV with VAL, separated by SEP
# --suffix # --suffix
@ -36,7 +38,7 @@ makeShellWrapper() {
local original="$1" local original="$1"
local wrapper="$2" local wrapper="$2"
local params varName value command separator n fileNames local params varName value command separator n fileNames
local argv0 flagsBefore flags local argv0 flagsBefore flagsAfter flags
assertExecutable "$original" assertExecutable "$original"
@ -165,6 +167,10 @@ makeShellWrapper() {
flags="${params[$((n + 1))]}" flags="${params[$((n + 1))]}"
n=$((n + 1)) n=$((n + 1))
flagsBefore="$flagsBefore $flags" flagsBefore="$flagsBefore $flags"
elif [[ "$p" == "--append-flags" ]]; then
flags="${params[$((n + 1))]}"
n=$((n + 1))
flagsAfter="$flagsAfter $flags"
elif [[ "$p" == "--argv0" ]]; then elif [[ "$p" == "--argv0" ]]; then
argv0="${params[$((n + 1))]}" argv0="${params[$((n + 1))]}"
n=$((n + 1)) n=$((n + 1))
@ -177,7 +183,7 @@ makeShellWrapper() {
done done
echo exec ${argv0:+-a \"$argv0\"} \""$original"\" \ echo exec ${argv0:+-a \"$argv0\"} \""$original"\" \
"$flagsBefore" '"$@"' >> "$wrapper" "$flagsBefore" '"$@"' "$flagsAfter" >> "$wrapper"
chmod +x "$wrapper" chmod +x "$wrapper"
} }

View file

@ -3,7 +3,7 @@
#include <assert.h> #include <assert.h>
int main(int argc, char **argv) { int main(int argc, char **argv) {
char **argv_tmp = calloc(5 + argc, sizeof(*argv_tmp)); char **argv_tmp = calloc(4 + argc + 2 + 1, sizeof(*argv_tmp));
assert(argv_tmp != NULL); assert(argv_tmp != NULL);
argv_tmp[0] = argv[0]; argv_tmp[0] = argv[0];
argv_tmp[1] = "-x"; argv_tmp[1] = "-x";
@ -13,7 +13,9 @@ int main(int argc, char **argv) {
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
argv_tmp[4 + i] = argv[i]; argv_tmp[4 + i] = argv[i];
} }
argv_tmp[4 + argc] = NULL; argv_tmp[4 + argc + 0] = "-foo";
argv_tmp[4 + argc + 1] = "-bar";
argv_tmp[4 + argc + 2] = NULL;
argv = argv_tmp; argv = argv_tmp;
argv[0] = "/send/me/flags"; argv[0] = "/send/me/flags";

View file

@ -1,2 +1,3 @@
--append-flags "-foo -bar" \
--add-flags "-x -y -z" \ --add-flags "-x -y -z" \
--add-flags -abc --add-flags -abc

View file

@ -4,3 +4,5 @@ SUBST_ARGV0
-y -y
-z -z
-abc -abc
-foo
-bar

View file

@ -36,7 +36,7 @@ int main(int argc, char **argv) {
set_env_suffix("PATH", ":", "/usr/local/bin/"); set_env_suffix("PATH", ":", "/usr/local/bin/");
putenv("MESSAGE2=WORLD"); putenv("MESSAGE2=WORLD");
char **argv_tmp = calloc(4 + argc, sizeof(*argv_tmp)); char **argv_tmp = calloc(3 + argc + 0 + 1, sizeof(*argv_tmp));
assert(argv_tmp != NULL); assert(argv_tmp != NULL);
argv_tmp[0] = argv[0]; argv_tmp[0] = argv[0];
argv_tmp[1] = "-x"; argv_tmp[1] = "-x";
@ -45,7 +45,7 @@ int main(int argc, char **argv) {
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
argv_tmp[3 + i] = argv[i]; argv_tmp[3 + i] = argv[i];
} }
argv_tmp[3 + argc] = NULL; argv_tmp[3 + argc + 0] = NULL;
argv = argv_tmp; argv = argv_tmp;
argv[0] = "my-wrapper"; argv[0] = "my-wrapper";

View file

@ -62,7 +62,7 @@ runCommand "make-wrapper-test"
(mkWrapperBinary { name = "test-unset"; args = [ "--unset" "VAR" ]; }) (mkWrapperBinary { name = "test-unset"; args = [ "--unset" "VAR" ]; })
(mkWrapperBinary { name = "test-run"; args = [ "--run" "echo bar" ]; }) (mkWrapperBinary { name = "test-run"; args = [ "--run" "echo bar" ]; })
(mkWrapperBinary { name = "test-run-and-set"; args = [ "--run" "export VAR=foo" "--set" "VAR" "bar" ]; }) (mkWrapperBinary { name = "test-run-and-set"; args = [ "--run" "export VAR=foo" "--set" "VAR" "bar" ]; })
(mkWrapperBinary { name = "test-args"; args = [ "--add-flags" "abc" ]; wrapped = wrappedBinaryArgs; }) (mkWrapperBinary { name = "test-args"; args = [ "--add-flags" "abc" "--append-flags" "xyz" ]; wrapped = wrappedBinaryArgs; })
(mkWrapperBinary { name = "test-prefix"; args = [ "--prefix" "VAR" ":" "abc" ]; }) (mkWrapperBinary { name = "test-prefix"; args = [ "--prefix" "VAR" ":" "abc" ]; })
(mkWrapperBinary { name = "test-prefix-noglob"; args = [ "--prefix" "VAR" ":" "./*" ]; }) (mkWrapperBinary { name = "test-prefix-noglob"; args = [ "--prefix" "VAR" ":" "./*" ]; })
(mkWrapperBinary { name = "test-suffix"; args = [ "--suffix" "VAR" ":" "abc" ]; }) (mkWrapperBinary { name = "test-suffix"; args = [ "--suffix" "VAR" ":" "abc" ]; })
@ -89,10 +89,10 @@ runCommand "make-wrapper-test"
# --unset works # --unset works
+ mkTest "VAR=foo test-unset" "VAR=" + mkTest "VAR=foo test-unset" "VAR="
# --add-flags works # --add-flags and --append-flags work
+ mkTest "test-args" "abc" + mkTest "test-args" "abc xyz"
# given flags are appended # given flags are kept
+ mkTest "test-args foo" "abc foo" + mkTest "test-args foo" "abc foo xyz"
# --run works # --run works
+ mkTest "test-run" "bar\nVAR=" + mkTest "test-run" "bar\nVAR="