Skip to content

Commit 20f124a

Browse files
authored
Rollup merge of #99992 - jyn514:shell-scripts, r=Mark-Simulacrum
Add `x.sh` and `x.ps1` shell scripts This is a more ambitious version of #98716. It still changes the x.py shebang back to python3, for compatibility with non-Unix systems, but also adds alternative entrypoints for systems without `python3` installed. These scripts will be necessary for the rust entrypoint (#94829), so I see little downside in adding them early. I'll update the dev-guide to suggest using these instead of x.py once this is merged. Fixes #98650 r? `@Mark-Simulacrum` cc `@dtolnay` `@CAD97` `@yoshuawuyts`
2 parents 37efd55 + 775c3c0 commit 20f124a

File tree

6 files changed

+94
-34
lines changed

6 files changed

+94
-34
lines changed

src/bootstrap/mk/Makefile.in

+8-3
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,21 @@ TESTS_IN_2 := \
6666
src/test/ui \
6767
src/tools/linkchecker
6868

69+
## MSVC native builders
70+
71+
# these intentionally don't use `$(BOOTSTRAP)` so we can test the shebang on Windows
6972
ci-subset-1:
70-
$(Q)$(BOOTSTRAP) test --stage 2 $(TESTS_IN_2:%=--exclude %)
73+
$(Q)$(CFG_SRC_DIR)/x.py test --stage 2 $(TESTS_IN_2:%=--exclude %)
7174
ci-subset-2:
72-
$(Q)$(BOOTSTRAP) test --stage 2 $(TESTS_IN_2)
75+
$(Q)$(CFG_SRC_DIR)/x.ps1 test --stage 2 $(TESTS_IN_2)
76+
77+
## MingW native builders
7378

7479
TESTS_IN_MINGW_2 := \
7580
src/test/ui
7681

7782
ci-mingw-subset-1:
78-
$(Q)$(BOOTSTRAP) test --stage 2 $(TESTS_IN_MINGW_2:%=--exclude %)
83+
$(Q)$(CFG_SRC_DIR)/x.sh test --stage 2 $(TESTS_IN_MINGW_2:%=--exclude %)
7984
ci-mingw-subset-2:
8085
$(Q)$(BOOTSTRAP) test --stage 2 $(TESTS_IN_MINGW_2)
8186

src/ci/docker/host-x86_64/x86_64-gnu-llvm-12/Dockerfile

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
FROM ubuntu:20.04
22

33
ARG DEBIAN_FRONTEND=noninteractive
4+
5+
# NOTE: intentionally installs both python2 and python3 so we can test support for both.
46
RUN apt-get update && apt-get install -y --no-install-recommends \
57
g++ \
68
gcc-multilib \
@@ -10,6 +12,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1012
curl \
1113
ca-certificates \
1214
python2.7 \
15+
python3.9 \
1316
git \
1417
cmake \
1518
sudo \
@@ -23,6 +26,14 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
2326
xz-utils \
2427
nodejs
2528

29+
# Install powershell so we can test x.ps1 on Linux
30+
RUN apt-get update && \
31+
apt-get install -y apt-transport-https software-properties-common && \
32+
curl -s "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb" > packages-microsoft-prod.deb && \
33+
dpkg -i packages-microsoft-prod.deb && \
34+
apt-get update && \
35+
apt-get install -y powershell
36+
2637
COPY scripts/sccache.sh /scripts/
2738
RUN sh /scripts/sccache.sh
2839

@@ -33,21 +44,22 @@ ENV RUST_CONFIGURE_ARGS \
3344
--enable-llvm-link-shared \
3445
--set rust.thin-lto-import-instr-limit=10
3546

36-
ENV SCRIPT python2.7 ../x.py --stage 2 test --exclude src/tools/tidy && \
47+
# NOTE: intentionally uses all of `x.py`, `x.sh`, and `x.ps1` to make sure they all work on Linux.
48+
ENV SCRIPT ../x.py --stage 2 test --exclude src/tools/tidy && \
3749
# Run the `mir-opt` tests again but this time for a 32-bit target.
3850
# This enforces that tests using `// EMIT_MIR_FOR_EACH_BIT_WIDTH` have
3951
# both 32-bit and 64-bit outputs updated by the PR author, before
4052
# the PR is approved and tested for merging.
4153
# It will also detect tests lacking `// EMIT_MIR_FOR_EACH_BIT_WIDTH`,
4254
# despite having different output on 32-bit vs 64-bit targets.
43-
python2.7 ../x.py --stage 2 test src/test/mir-opt \
55+
../x.sh --stage 2 test src/test/mir-opt \
4456
--host='' --target=i686-unknown-linux-gnu && \
4557
# Run the UI test suite again, but in `--pass=check` mode
4658
#
4759
# This is intended to make sure that both `--pass=check` continues to
4860
# work.
4961
#
50-
python2.7 ../x.py --stage 2 test src/test/ui --pass=check \
62+
../x.ps1 --stage 2 test src/test/ui --pass=check \
5163
--host='' --target=i686-unknown-linux-gnu && \
5264
# Run tidy at the very end, after all the other tests.
5365
python2.7 ../x.py --stage 2 test src/tools/tidy

src/tools/tidy/src/bins.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ mod os_impl {
9696

9797
#[cfg(unix)]
9898
pub fn check(path: &Path, bad: &mut bool) {
99+
use std::ffi::OsStr;
100+
99101
const ALLOWED: &[&str] = &["configure"];
100102

101103
crate::walk_no_read(
@@ -117,9 +119,9 @@ mod os_impl {
117119
},
118120
&mut |entry| {
119121
let file = entry.path();
120-
let filename = file.file_name().unwrap().to_string_lossy();
121-
let extensions = [".py", ".sh"];
122-
if extensions.iter().any(|e| filename.ends_with(e)) {
122+
let extension = file.extension();
123+
let scripts = ["py", "sh", "ps1"];
124+
if scripts.into_iter().any(|e| extension == Some(OsStr::new(e))) {
123125
return;
124126
}
125127

x.ps1

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env pwsh
2+
3+
# See x.sh for why these scripts exist.
4+
5+
$xpy = Join-Path $PSScriptRoot x.py
6+
# Start-Process for some reason splits arguments on spaces. (Isn't powershell supposed to be simpler than bash?)
7+
# Double-quote all the arguments so it doesn't do that.
8+
$xpy_args = @("""$xpy""")
9+
foreach ($arg in $args) {
10+
$xpy_args += """$arg"""
11+
}
12+
13+
foreach ($python in "py", "python3", "python", "python2") {
14+
# NOTE: this only tests that the command exists in PATH, not that it's actually
15+
# executable. The latter is not possible in a portable way, see
16+
# https://github.com/PowerShell/PowerShell/issues/12625.
17+
if (Get-Command $python -ErrorAction SilentlyContinue) {
18+
if ($python -eq "py") {
19+
# Use python3, not python2
20+
$xpy_args = @("-3") + $xpy_args
21+
}
22+
$process = Start-Process -NoNewWindow -Wait -PassThru $python $xpy_args
23+
Exit $process.ExitCode
24+
}
25+
}
26+
27+
Write-Error "${PSCommandPath}: error: did not find python installed"
28+
Exit 1

x.py

+5-25
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,16 @@
1-
#!/usr/bin/env bash
1+
#!/usr/bin/env python3
2+
# Some systems don't have `python3` in their PATH. This isn't supported by x.py directly;
3+
# they should use `x.sh` or `x.ps1` instead.
24

3-
# Modern Linux and macOS systems commonly only have a thing called `python3` and
4-
# not `python`, while Windows commonly does not have `python3`, so we cannot
5-
# directly use python in the shebang and have it consistently work. Instead we
6-
# embed some bash to look for a python to run the rest of the script.
7-
#
8-
# On Windows, `py -3` sometimes works. We need to try it first because `python3`
9-
# sometimes tries to launch the app store on Windows.
10-
'''':
11-
for PYTHON in "py -3" python3 python python2; do
12-
if command -v $PYTHON >/dev/null; then
13-
exec $PYTHON "$0" "$@"
14-
break
15-
fi
16-
done
17-
echo "$0: error: did not find python installed" >&2
18-
exit 1
19-
'''
20-
21-
# The rest of this file is Python.
22-
#
235
# This file is only a "symlink" to bootstrap.py, all logic should go there.
246

257
import os
268
import sys
279

2810
# If this is python2, check if python3 is available and re-execute with that
29-
# interpreter.
11+
# interpreter. Only python3 allows downloading CI LLVM.
3012
#
31-
# `./x.py` would not normally benefit from this because the bash above tries
32-
# python3 before 2, but this matters if someone ran `python x.py` and their
33-
# system's `python` is python2.
13+
# This matters if someone's system `python` is python2.
3414
if sys.version_info.major < 3:
3515
try:
3616
os.execvp("py", ["py", "-3"] + sys.argv)

x.sh

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
# Modern Linux and macOS systems commonly only have a thing called `python3` and
4+
# not `python`, while Windows commonly does not have `python3`, so we cannot
5+
# directly use python in the x.py shebang and have it consistently work. Instead we
6+
# have a shell script to look for a python to run x.py.
7+
8+
set -eu
9+
10+
realpath() {
11+
if [ -d "$1" ]; then
12+
CDPATH='' command cd "$1" && pwd -P
13+
else
14+
echo "$(realpath "$(dirname "$1")")/$(basename "$1")"
15+
fi
16+
}
17+
18+
xpy=$(dirname "$(realpath "$0")")/x.py
19+
20+
# On Windows, `py -3` sometimes works. We need to try it first because `python3`
21+
# sometimes tries to launch the app store on Windows.
22+
for SEARCH_PYTHON in py python3 python python2; do
23+
if python=$(command -v $SEARCH_PYTHON) && [ -x "$python" ]; then
24+
if [ $SEARCH_PYTHON = py ]; then
25+
extra_arg="-3"
26+
else
27+
extra_arg=""
28+
fi
29+
exec "$python" $extra_arg "$xpy" "$@"
30+
fi
31+
done
32+
echo "$0: error: did not find python installed" >&2
33+
exit 1

0 commit comments

Comments
 (0)