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 = ''
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"
patchelf --set-rpath "$runtime_rpath:$mod_rpath" "$mod"
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
# ARGS:
# --argv0 NAME : set the name of the executed process to NAME
# (if unset or empty, defaults to EXECUTABLE)
# --inherit-argv0 : the executable inherits argv0 from the wrapper.
# (use instead of --argv0 '$0')
# --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
# the environment
# --unset VAR : remove VAR from the environment
# --chdir DIR : change working directory (use instead of --run "cd DIR")
# --add-flags FLAGS : add FLAGS to invocation of executable
# TODO(@ncfavier): --append-flags
# --argv0 NAME : set the name of the executed process to NAME
# (if unset or empty, defaults to EXECUTABLE)
# --inherit-argv0 : the executable inherits argv0 from the wrapper.
# (use instead of --argv0 '$0')
# --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
# the environment
# --unset VAR : remove VAR from the environment
# --chdir DIR : change working directory (use instead of --run "cd DIR")
# --add-flags ARGS : prepend ARGS to the invocation of the executable
# (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
# --suffix
@ -83,7 +85,7 @@ makeDocumentedCWrapper() {
# makeCWrapper EXECUTABLE ARGS
# ARGS: same as makeWrapper
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
executable=$(escapeStringLiteral "$1")
params=("$@")
@ -150,6 +152,13 @@ makeCWrapper() {
n=$((n + 1))
[ $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=$(escapeStringLiteral "${params[n + 1]}")
inherit_argv0=
@ -165,8 +174,7 @@ makeCWrapper() {
;;
esac
done
# shellcheck disable=SC2086
[ -z "$flagsBefore" ] || main="$main"${main:+$'\n'}$(addFlags $flagsBefore)$'\n'$'\n'
[[ -z "$flagsBefore" && -z "$flagsAfter" ]] || main="$main"${main:+$'\n'}$(addFlags "$flagsBefore" "$flagsAfter")$'\n'$'\n'
[ -z "$inherit_argv0" ] && main="${main}argv[0] = \"${argv0:-${executable}}\";"$'\n'
main="${main}return execv(\"${executable}\", argv);"$'\n'
@ -184,21 +192,25 @@ makeCWrapper() {
}
addFlags() {
local result n flag flags var
local n flag before after var
# shellcheck disable=SC2086
before=($1) after=($2)
var="argv_tmp"
flags=("$@")
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' "char **$var = calloc(${#before[@]} + argc + ${#after[@]} + 1, sizeof(*$var));"
printf '%s\n' "assert($var != NULL);"
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' " ${var}[$n + i] = argv[i];"
printf '%s\n' " ${var}[${#before[@]} + i] = argv[i];"
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;"
}
@ -366,6 +378,10 @@ formatArgs() {
formatArgsLine 1 "$@"
shift 1
;;
--append-flags)
formatArgsLine 1 "$@"
shift 1
;;
--argv0)
formatArgsLine 1 "$@"
shift 1

View File

@ -11,18 +11,20 @@ assertExecutable() {
# makeWrapper EXECUTABLE OUT_PATH ARGS
# ARGS:
# --argv0 NAME : set the name of the executed process to NAME
# (if unset or empty, defaults to EXECUTABLE)
# --inherit-argv0 : the executable inherits argv0 from the wrapper.
# (use instead of --argv0 '$0')
# --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
# the environment
# --unset VAR : remove VAR from the environment
# --chdir DIR : change working directory (use instead of --run "cd DIR")
# --run COMMAND : run command before the executable
# --add-flags FLAGS : add FLAGS to invocation of executable
# TODO(@ncfavier): --append-flags
# --argv0 NAME : set the name of the executed process to NAME
# (if unset or empty, defaults to EXECUTABLE)
# --inherit-argv0 : the executable inherits argv0 from the wrapper.
# (use instead of --argv0 '$0')
# --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
# the environment
# --unset VAR : remove VAR from the environment
# --chdir DIR : change working directory (use instead of --run "cd DIR")
# --run COMMAND : run command before the executable
# --add-flags ARGS : prepend ARGS to the invocation of the executable
# (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
# --suffix
@ -36,7 +38,7 @@ makeShellWrapper() {
local original="$1"
local wrapper="$2"
local params varName value command separator n fileNames
local argv0 flagsBefore flags
local argv0 flagsBefore flagsAfter flags
assertExecutable "$original"
@ -165,6 +167,10 @@ makeShellWrapper() {
flags="${params[$((n + 1))]}"
n=$((n + 1))
flagsBefore="$flagsBefore $flags"
elif [[ "$p" == "--append-flags" ]]; then
flags="${params[$((n + 1))]}"
n=$((n + 1))
flagsAfter="$flagsAfter $flags"
elif [[ "$p" == "--argv0" ]]; then
argv0="${params[$((n + 1))]}"
n=$((n + 1))
@ -177,7 +183,7 @@ makeShellWrapper() {
done
echo exec ${argv0:+-a \"$argv0\"} \""$original"\" \
"$flagsBefore" '"$@"' >> "$wrapper"
"$flagsBefore" '"$@"' "$flagsAfter" >> "$wrapper"
chmod +x "$wrapper"
}

View File

@ -3,7 +3,7 @@
#include <assert.h>
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);
argv_tmp[0] = argv[0];
argv_tmp[1] = "-x";
@ -13,7 +13,9 @@ int main(int argc, char **argv) {
for (int i = 1; i < argc; ++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[0] = "/send/me/flags";

View File

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

View File

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

View File

@ -36,7 +36,7 @@ int main(int argc, char **argv) {
set_env_suffix("PATH", ":", "/usr/local/bin/");
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);
argv_tmp[0] = argv[0];
argv_tmp[1] = "-x";
@ -45,7 +45,7 @@ int main(int argc, char **argv) {
for (int i = 1; i < argc; ++i) {
argv_tmp[3 + i] = argv[i];
}
argv_tmp[3 + argc] = NULL;
argv_tmp[3 + argc + 0] = NULL;
argv = argv_tmp;
argv[0] = "my-wrapper";

View File

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