Skip to content

Refactor scripts to set up Rust on dev desktops #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ansible/roles/dev-desktop/files/scripts/help.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/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"
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"
14 changes: 4 additions & 10 deletions ansible/roles/dev-desktop/files/scripts/init.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
#!/usr/bin/env bash

username=`id -u -n`
gh_name=${username#"gh-"}

# 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
# Enable strict mode for Bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'

init_git.py
setup_rustup.sh
46 changes: 46 additions & 0 deletions ansible/roles/dev-desktop/files/scripts/link_rust.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

# Enable strict mode for Bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'

# Discover target triple (e.g. "aarch64-unknown-linux-gnu")
target="$(rustc -vV | awk '/host/ { print $2 }')"

rustc_dummy=$(
cat <<EOF
#!/usr/bin/env bash
echo "This is a dummy file to trick rustup into thinking this is a sysroot"
echo 'Run "x.py build --stage 1 library/std" to create a real sysroot you can use with rustup'
EOF
)

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" -gt 2 ]; then
stages=(stage1-sysroot stage2-sysroot)
else
stages=(stage1 stage2)
fi

for stage in "${stages[@]}"; do
directory="build/${target}/${stage}"

if [ ! -d "$directory" ]; then
mkdir -p "${directory}/lib"
mkdir -p "${directory}/bin"
echo "$rustc_dummy" >> "${directory}/bin/rustc"
chmod +x "${directory}/bin/rustc"
fi

rustup toolchain link "${D}_${stage}" "$directory"
done

popd
fi
done
2 changes: 1 addition & 1 deletion ansible/roles/dev-desktop/files/scripts/new_worktree.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ git checkout upstream/master
ln -s ../config.toml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems somewhat confusing, usually I want different config files in different worktrees. but if no one has complained so far it's probably fine

popd

setup_rustup.sh
link_rust.sh
13 changes: 10 additions & 3 deletions ansible/roles/dev-desktop/files/scripts/set_defaults.sh
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions ansible/roles/dev-desktop/files/scripts/setup_rust.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/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
link_rust.sh
25 changes: 13 additions & 12 deletions ansible/roles/dev-desktop/files/scripts/setup_rustup.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/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
# 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