From 98b197c3bf60fbbbc6c2cd23554b7c6e3d58ce8d Mon Sep 17 00:00:00 2001 From: Jan David Date: Mon, 10 Oct 2022 18:04:32 +0200 Subject: [PATCH 1/8] Rewrite scripts to set up Rust on dev desktops The scripts to set up Rust for a user on the dev desktops have been refactored for a better onboarding experience and error handling. The init script installs rustup now, and can be extended in the future to also set the user's git username and email. For users who want to set up their own Rust toolchain, the setup_rust script clones their fork of the rust-lang/rust repository, compiles it, and links the build artifacts. This is optional, however, and not required if users want to use the dev desktop to work with different repositories. --- .../roles/dev-desktop/files/scripts/init.sh | 22 ++++++------ .../dev-desktop/files/scripts/set_defaults.sh | 13 +++++-- .../dev-desktop/files/scripts/setup_rust.sh | 36 +++++++++++++++++++ .../dev-desktop/files/scripts/setup_rustup.sh | 14 -------- 4 files changed, 58 insertions(+), 27 deletions(-) create mode 100755 ansible/roles/dev-desktop/files/scripts/setup_rust.sh delete mode 100644 ansible/roles/dev-desktop/files/scripts/setup_rustup.sh diff --git a/ansible/roles/dev-desktop/files/scripts/init.sh b/ansible/roles/dev-desktop/files/scripts/init.sh index 64a35e11e..19e3eb183 100755 --- a/ansible/roles/dev-desktop/files/scripts/init.sh +++ b/ansible/roles/dev-desktop/files/scripts/init.sh @@ -1,15 +1,17 @@ #!/usr/bin/env bash -username=`id -u -n` -gh_name=${username#"gh-"} +# Enable strict mode for Bash +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +set -euo pipefail +IFS=$'\n\t' -# Using https instead of git urls because vscode only handles login on push/pull -git clone https://github.com/$gh_name/rust.git -pushd rust -git remote add upstream https://github.com/rust-lang/rust.git -git fetch upstream -git checkout upstream/master -popd +# Check if rustup is already installed and exit if that's the case. +if command -v rustup &>/dev/null; then + rustup --version + exit 0 +fi init_git.py -setup_rustup.sh + +echo "Installing rustup..." +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y diff --git a/ansible/roles/dev-desktop/files/scripts/set_defaults.sh b/ansible/roles/dev-desktop/files/scripts/set_defaults.sh index d068783c8..ab56eed2c 100755 --- a/ansible/roles/dev-desktop/files/scripts/set_defaults.sh +++ b/ansible/roles/dev-desktop/files/scripts/set_defaults.sh @@ -1,9 +1,16 @@ #!/usr/bin/env bash +# Enable strict mode for Bash +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +set -euo pipefail +IFS=$'\n\t' + for D in rust*; do if [ -d "${D}" ]; then - pushd $D - ln -s ../config.toml - popd + pushd "${D}" || exit + if [[ ! -f config.toml ]]; then + ln -s ~/config.toml . + fi + popd || exit fi done diff --git a/ansible/roles/dev-desktop/files/scripts/setup_rust.sh b/ansible/roles/dev-desktop/files/scripts/setup_rust.sh new file mode 100755 index 000000000..ec57d949c --- /dev/null +++ b/ansible/roles/dev-desktop/files/scripts/setup_rust.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +# Enable strict mode for Bash +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +set -euo pipefail +IFS=$'\n\t' + +username=$(id -u -n) +gh_name=${username#"gh-"} + +set -x + +if [[ ! -d "rust" ]]; then + # Using https instead of git urls because vscode only handles login on push/pull + git clone "https://github.com/${gh_name}/rust.git" +fi + +pushd rust + +if ! git remote | grep upstream; then + git remote add upstream https://github.com/rust-lang/rust.git +fi + +git fetch upstream +git checkout upstream/master +popd + +set_defaults.sh + +for D in rust*; do + if [ -d "${D}" ]; then + cd "${D}" && ./x.py build + rustup toolchain link "$D"_stage1 "$D/build/x86_64-unknown-linux-gnu/stage1" + rustup toolchain link "$D"_stage2 "$D/build/x86_64-unknown-linux-gnu/stage2" + fi +done diff --git a/ansible/roles/dev-desktop/files/scripts/setup_rustup.sh b/ansible/roles/dev-desktop/files/scripts/setup_rustup.sh deleted file mode 100644 index 9a4d26cfd..000000000 --- a/ansible/roles/dev-desktop/files/scripts/setup_rustup.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash - -set -x - -rustup --version || curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y - -source "$HOME/.cargo/env" - -for D in rust*; do - if [ -d "${D}" ]; then - rustup toolchain link "$D"_stage1 "$D/build/x86_64-unknown-linux-gnu/stage1" - rustup toolchain link "$D"_stage2 "$D/build/x86_64-unknown-linux-gnu/stage2" - fi -done From 19edc680878e161771f7a399450a470e5aef23b8 Mon Sep 17 00:00:00 2001 From: Jan David Date: Tue, 11 Oct 2022 13:14:07 +0200 Subject: [PATCH 2/8] Create script to link build artifacts A new script has been created to link the build artifacts. The script is called from the init script that sets up Rust, and from the script that initializes a new worktree. --- .../roles/dev-desktop/files/scripts/help.sh | 4 ++-- .../dev-desktop/files/scripts/link_rust.sh | 24 +++++++++++++++++++ .../dev-desktop/files/scripts/new_worktree.sh | 2 +- .../dev-desktop/files/scripts/setup_rust.sh | 9 +------ 4 files changed, 28 insertions(+), 11 deletions(-) create mode 100755 ansible/roles/dev-desktop/files/scripts/link_rust.sh diff --git a/ansible/roles/dev-desktop/files/scripts/help.sh b/ansible/roles/dev-desktop/files/scripts/help.sh index e8060d4dc..411c6658a 100644 --- a/ansible/roles/dev-desktop/files/scripts/help.sh +++ b/ansible/roles/dev-desktop/files/scripts/help.sh @@ -7,5 +7,5 @@ echo "new_worktree.sh | creates a worktree (shallow copy of the main git ch echo "detach_merged_prs.sh | invokes \"git pull --fast-forward-only\" on all worktrees and detaches those that are equal to the \"master\" branch" echo "" echo "# Rarer commands:" -echo "set_defaults.sh | connects the global config.toml with all worktrees. Use this when your setup is broken" - +echo "set_defaults.sh | connects the global config.toml with all worktrees. Use this when your setup is broken" +echo "setup_rust.sh | Clone your fork of rust-lang/rust, compile, and then link it" diff --git a/ansible/roles/dev-desktop/files/scripts/link_rust.sh b/ansible/roles/dev-desktop/files/scripts/link_rust.sh new file mode 100755 index 000000000..3de066710 --- /dev/null +++ b/ansible/roles/dev-desktop/files/scripts/link_rust.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +# Enable strict mode for Bash +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +set -euo pipefail +IFS=$'\n\t' + +for D in rust*; do + if [ -d "${D}" ]; then + pushd "${D}" + + ./x.py build + + if [[ -d "$D/build/aarch64-unknown-linux-gnu/stage1" ]]; then + rustup toolchain link "$D"_stage1 "$D/build/aarch64-unknown-linux-gnu/stage1" + fi + + if [[ -d "$D/build/aarch64-unknown-linux-gnu/stage2" ]]; then + rustup toolchain link "$D"_stage2 "$D/build/aarch64-unknown-linux-gnu/stage2" + fi + + popd + fi +done diff --git a/ansible/roles/dev-desktop/files/scripts/new_worktree.sh b/ansible/roles/dev-desktop/files/scripts/new_worktree.sh index 1e10a6942..8e1127ffd 100755 --- a/ansible/roles/dev-desktop/files/scripts/new_worktree.sh +++ b/ansible/roles/dev-desktop/files/scripts/new_worktree.sh @@ -13,4 +13,4 @@ git checkout upstream/master ln -s ../config.toml popd -setup_rustup.sh +link_rust.sh diff --git a/ansible/roles/dev-desktop/files/scripts/setup_rust.sh b/ansible/roles/dev-desktop/files/scripts/setup_rust.sh index ec57d949c..5cf0fae11 100755 --- a/ansible/roles/dev-desktop/files/scripts/setup_rust.sh +++ b/ansible/roles/dev-desktop/files/scripts/setup_rust.sh @@ -26,11 +26,4 @@ git checkout upstream/master popd set_defaults.sh - -for D in rust*; do - if [ -d "${D}" ]; then - cd "${D}" && ./x.py build - rustup toolchain link "$D"_stage1 "$D/build/x86_64-unknown-linux-gnu/stage1" - rustup toolchain link "$D"_stage2 "$D/build/x86_64-unknown-linux-gnu/stage2" - fi -done +link_rust.sh From 94072604d3a85a2894d8fcacc5713bdfca02c7ec Mon Sep 17 00:00:00 2001 From: Jan David Date: Tue, 18 Oct 2022 11:19:10 +0200 Subject: [PATCH 3/8] Discover target triple The dev desktops are deployed to machines with different architectures (amd64 and arm64), which makes it necessary to discover the target triple when linking the stages. The current solution was chosen based on the following Stack Overflow post: https://stackoverflow.com/questions/52996949/how-can-i-find-the-current-rust-compilers-default-llvm-target-triple --- ansible/roles/dev-desktop/files/scripts/link_rust.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ansible/roles/dev-desktop/files/scripts/link_rust.sh b/ansible/roles/dev-desktop/files/scripts/link_rust.sh index 3de066710..14763bf70 100755 --- a/ansible/roles/dev-desktop/files/scripts/link_rust.sh +++ b/ansible/roles/dev-desktop/files/scripts/link_rust.sh @@ -5,18 +5,21 @@ set -euo pipefail IFS=$'\n\t' +# Discover target triple (e.g. "aarch64-unknown-linux-gnu") +target="$(rustc -vV | awk '/host/ { print $2 }')" + for D in rust*; do if [ -d "${D}" ]; then pushd "${D}" ./x.py build - if [[ -d "$D/build/aarch64-unknown-linux-gnu/stage1" ]]; then - rustup toolchain link "$D"_stage1 "$D/build/aarch64-unknown-linux-gnu/stage1" + if [[ -d "$D/build/$target/stage1" ]]; then + rustup toolchain link "$D"_stage1 "$D/build/$target/stage1" fi - if [[ -d "$D/build/aarch64-unknown-linux-gnu/stage2" ]]; then - rustup toolchain link "$D"_stage2 "$D/build/aarch64-unknown-linux-gnu/stage2" + if [[ -d "$D/build/$target/stage2" ]]; then + rustup toolchain link "$D"_stage2 "$D/build/$target/stage2" fi popd From bea2384fa32f73285eb2105008ca7c5c31412430 Mon Sep 17 00:00:00 2001 From: Jan David Date: Thu, 20 Oct 2022 11:27:03 +0200 Subject: [PATCH 4/8] Remove build command from link script --- ansible/roles/dev-desktop/files/scripts/link_rust.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/ansible/roles/dev-desktop/files/scripts/link_rust.sh b/ansible/roles/dev-desktop/files/scripts/link_rust.sh index 14763bf70..235f555ad 100755 --- a/ansible/roles/dev-desktop/files/scripts/link_rust.sh +++ b/ansible/roles/dev-desktop/files/scripts/link_rust.sh @@ -12,8 +12,6 @@ for D in rust*; do if [ -d "${D}" ]; then pushd "${D}" - ./x.py build - if [[ -d "$D/build/$target/stage1" ]]; then rustup toolchain link "$D"_stage1 "$D/build/$target/stage1" fi From d52d8bb5241f1f4a6b633a0c1d11744ff6c5e63e Mon Sep 17 00:00:00 2001 From: Jan David Date: Fri, 21 Oct 2022 16:21:20 +0200 Subject: [PATCH 5/8] Restore original name for init script --- ansible/roles/dev-desktop/files/scripts/help.sh | 2 +- ansible/roles/dev-desktop/files/scripts/init.sh | 10 +--------- .../dev-desktop/files/scripts/setup_rustup.sh | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 10 deletions(-) mode change 100755 => 100644 ansible/roles/dev-desktop/files/scripts/init.sh create mode 100755 ansible/roles/dev-desktop/files/scripts/setup_rustup.sh diff --git a/ansible/roles/dev-desktop/files/scripts/help.sh b/ansible/roles/dev-desktop/files/scripts/help.sh index 411c6658a..2be716d77 100644 --- a/ansible/roles/dev-desktop/files/scripts/help.sh +++ b/ansible/roles/dev-desktop/files/scripts/help.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash echo "# Available scripts for managing your Rust checkouts" -echo "init.sh | first time setup, you should only have to execute this once on a new machine" +echo "setup_rustup.sh | first time setup, you should only have to execute this once on a new machine" echo "status.sh | list the branches and git status of all copies of the Rust repo" echo "new_worktree.sh | creates a worktree (shallow copy of the main git checkout of Rust, sharing the .git folder)" echo "detach_merged_prs.sh | invokes \"git pull --fast-forward-only\" on all worktrees and detaches those that are equal to the \"master\" branch" diff --git a/ansible/roles/dev-desktop/files/scripts/init.sh b/ansible/roles/dev-desktop/files/scripts/init.sh old mode 100755 new mode 100644 index 19e3eb183..94c210a69 --- a/ansible/roles/dev-desktop/files/scripts/init.sh +++ b/ansible/roles/dev-desktop/files/scripts/init.sh @@ -5,13 +5,5 @@ set -euo pipefail IFS=$'\n\t' -# Check if rustup is already installed and exit if that's the case. -if command -v rustup &>/dev/null; then - rustup --version - exit 0 -fi - init_git.py - -echo "Installing rustup..." -curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y +setup_rustup.sh diff --git a/ansible/roles/dev-desktop/files/scripts/setup_rustup.sh b/ansible/roles/dev-desktop/files/scripts/setup_rustup.sh new file mode 100755 index 000000000..862309b3f --- /dev/null +++ b/ansible/roles/dev-desktop/files/scripts/setup_rustup.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +# Enable strict mode for Bash +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +set -euo pipefail +IFS=$'\n\t' + +# Check if rustup is already installed and exit if that's the case. +if command -v rustup &>/dev/null; then + rustup --version + exit 0 +fi + +echo "Installing rustup..." +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y From 232dad9df664e0549193c3ad4924a3c7ae528db5 Mon Sep 17 00:00:00 2001 From: Jan David Date: Thu, 27 Oct 2022 14:50:29 +0200 Subject: [PATCH 6/8] Link fake sysroot when setting up Rust --- .../dev-desktop/files/scripts/link_rust.sh | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/ansible/roles/dev-desktop/files/scripts/link_rust.sh b/ansible/roles/dev-desktop/files/scripts/link_rust.sh index 235f555ad..1c61dafeb 100755 --- a/ansible/roles/dev-desktop/files/scripts/link_rust.sh +++ b/ansible/roles/dev-desktop/files/scripts/link_rust.sh @@ -8,17 +8,32 @@ IFS=$'\n\t' # Discover target triple (e.g. "aarch64-unknown-linux-gnu") target="$(rustc -vV | awk '/host/ { print $2 }')" +rustc_dummy=$( + cat <> "${directory}/bin/rustc" + chmod +x "${directory}/bin/rustc" + fi - if [[ -d "$D/build/$target/stage2" ]]; then - rustup toolchain link "$D"_stage2 "$D/build/$target/stage2" - fi + rustup toolchain link "${D}_${stage}" "$directory" + done popd fi From 9287abf3f758361111f4c0617d60ef1331f3c395 Mon Sep 17 00:00:00 2001 From: Jan David Date: Wed, 2 Nov 2022 09:52:28 +0100 Subject: [PATCH 7/8] Detect bootstrap version to link correct paths The directory names for the stages are changing in version 3 of the boostrapping (see https://github.com/rust-lang/rust/pull/103286). We are checking the version now to create the correct dummy directories. --- ansible/roles/dev-desktop/files/scripts/link_rust.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ansible/roles/dev-desktop/files/scripts/link_rust.sh b/ansible/roles/dev-desktop/files/scripts/link_rust.sh index 1c61dafeb..ffc620af5 100755 --- a/ansible/roles/dev-desktop/files/scripts/link_rust.sh +++ b/ansible/roles/dev-desktop/files/scripts/link_rust.sh @@ -16,12 +16,18 @@ echo 'Run "x.py build --stage 1 library/std" to create a real sysroot you can us EOF ) -stages=(stage1 stage2) - for D in rust*; do if [ -d "$D" ]; then pushd "$D" + bootstrap_version=$(grep 'pub const VERSION' src/bootstrap/lib.rs | grep -o '[0-9]*') + + if [ "$bootstrap_version" -eq 3 ]; then + stages=(stage1-sysroot stage2-sysroot) + else + stages=(stage1 stage2) + fi + for stage in "${stages[@]}"; do directory="build/${target}/${stage}" From 0da69946ac274607118b486ba6090eea0f19d03a Mon Sep 17 00:00:00 2001 From: Jan David Date: Wed, 2 Nov 2022 10:58:19 +0100 Subject: [PATCH 8/8] Future-proof bootstrap version check Co-authored-by: Joshua Nelson --- ansible/roles/dev-desktop/files/scripts/link_rust.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible/roles/dev-desktop/files/scripts/link_rust.sh b/ansible/roles/dev-desktop/files/scripts/link_rust.sh index ffc620af5..61092e5d1 100755 --- a/ansible/roles/dev-desktop/files/scripts/link_rust.sh +++ b/ansible/roles/dev-desktop/files/scripts/link_rust.sh @@ -22,7 +22,7 @@ for D in rust*; do bootstrap_version=$(grep 'pub const VERSION' src/bootstrap/lib.rs | grep -o '[0-9]*') - if [ "$bootstrap_version" -eq 3 ]; then + if [ "$bootstrap_version" -gt 2 ]; then stages=(stage1-sysroot stage2-sysroot) else stages=(stage1 stage2)