Skip to content

Commit 5bc93b7

Browse files
committed
Port some bash scripts to pure shell
1 parent 791842f commit 5bc93b7

5 files changed

Lines changed: 52 additions & 36 deletions

File tree

crate_universe/src/metadata/cargo_tree_resolver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,8 +1166,8 @@ mod test {
11661166
} else {
11671167
dedent(
11681168
r#"
1169-
#!/usr/bin/env bash
1170-
set -euo pipefail
1169+
#!/bin/sh
1170+
set -eu
11711171
echo "$@"
11721172
"#,
11731173
)
Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
1-
#!/usr/bin/env bash
1+
#!/bin/sh
22
#
33
# For details, see:
44
# `@rules_rust//crate_universe/src/metadata/cargo_tree_resolver.rs - TreeResolver::create_rustc_wrapper`
55

6-
set -euo pipefail
6+
set -eu
77

88
# When cargo is detecting the host configuration, the host target needs to be
99
# injected into the command.
10-
if [[ "$@" == *"rustc - --crate-name ___ "* && "$@" != *" --target "* ]]; then
11-
exec "$@" --target "${HOST_TRIPLE}"
12-
fi
10+
case "$*" in
11+
*"rustc - --crate-name ___ "*)
12+
case "$*" in
13+
*" --target "*) ;;
14+
*)
15+
exec "$@" --target "${HOST_TRIPLE}"
16+
;;
17+
esac
18+
;;
19+
esac
1320

1421
# When querying info about the compiler, ensure the triple is mocked out to be
1522
# the desired target triple for the host.
16-
if [[ "$@" == *"rustc -Vv" || "$@" == *"rustc -vV" ]]; then
17-
set +e
18-
_RUSTC_OUTPUT="$($@)"
19-
_EXIT_CODE=$?
20-
set -e
23+
case "$*" in
24+
*"rustc -Vv"*|*"rustc -vV"*)
25+
set +e
26+
_RUSTC_OUTPUT="$("$@")"
27+
_EXIT_CODE=$?
28+
set -e
2129

22-
# Loop through each line of the output
23-
while IFS= read -r line; do
24-
# If the line starts with "host:", replace it with the new host value
25-
if [[ "${line}" == host:* ]]; then
26-
echo "host: ${HOST_TRIPLE}"
27-
else
28-
# Print the other lines unchanged
29-
echo "${line}"
30-
fi
31-
done <<<"${_RUSTC_OUTPUT}"
30+
# Loop through each line of the output
31+
while IFS= read -r line; do
32+
# If the line starts with "host:", replace it with the new host value
33+
case "${line}" in
34+
host:*)
35+
echo "host: ${HOST_TRIPLE}"
36+
;;
37+
*)
38+
echo "${line}"
39+
;;
40+
esac
41+
done <<EOF
42+
${_RUSTC_OUTPUT}
43+
EOF
3244

33-
exit ${_EXIT_CODE}
34-
fi
45+
exit ${_EXIT_CODE}
46+
;;
47+
esac
3548

3649
# If there is nothing special to do then simply forward the call
3750
exec "$@"

test/process_wrapper_bootstrap/bootstrap_process_wrapper_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn test_shebang() {
1818

1919
let content = read_to_string(script).unwrap();
2020
assert!(
21-
content.starts_with("#!/usr/bin/env bash"),
21+
content.starts_with("#!/bin/sh"),
2222
"The shell script does not start with the expected shebang."
2323
)
2424
}

util/process_wrapper/private/bootstrap_process_wrapper.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def _bootstrap_process_wrapper_impl_unix(ctx):
1515
substitutions = {
1616
# Replace the shebang with one constructed from the configured
1717
# shell toolchain.
18-
"#!/usr/bin/env bash": shebang,
18+
"#!/bin/sh": shebang,
1919
},
2020
)
2121
else:
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
#!/usr/bin/env bash
1+
#!/bin/sh
22

3-
set -euo pipefail
3+
set -eu
44

55
# Skip the first argument which is expected to be `--`
66
shift
77

8-
args=()
9-
108
for arg in "$@"; do
11-
# Check if the argument contains "${PWD}" and replace it with the actual value of PWD
12-
if [[ "${arg}" == *'${pwd}'* ]]; then
13-
arg="${arg//\$\{pwd\}/$PWD}"
14-
fi
15-
args+=("${arg}")
9+
case "$arg" in
10+
*'${pwd}'*)
11+
# Split on '${pwd}' and rejoin with the actual PWD value
12+
prefix="${arg%%\$\{pwd\}*}"
13+
suffix="${arg#*\$\{pwd\}}"
14+
arg="${prefix}${PWD}${suffix}"
15+
;;
16+
esac
17+
set -- "$@" "$arg"
18+
shift
1619
done
1720

18-
exec "${args[@]}"
21+
exec "$@"

0 commit comments

Comments
 (0)