diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2025-02-18 15:45:20 +0100 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2025-02-18 15:45:20 +0100 |
| commit | 1f9762cd1ce1172f9efc88e8394e232df14a2f9d (patch) | |
| tree | 692ec6b53f78d6268c7fdb13e5f9162684511bbf | |
| parent | 36e03e11c921f5c0d49c94d7fe2f272e7dafa553 (diff) | |
| parent | 802570a5521d41ec9a90dae28977386a5d6cc7f9 (diff) | |
| download | mullvadvpn-1f9762cd1ce1172f9efc88e8394e232df14a2f9d.tar.xz mullvadvpn-1f9762cd1ce1172f9efc88e8394e232df14a2f9d.zip | |
Merge branch 'add-script-and-git-hook-to-automate-installation-of-rust-droid-1786'
| -rw-r--r-- | BuildInstructions.md | 9 | ||||
| -rw-r--r-- | android/BuildInstructions.md | 8 | ||||
| -rw-r--r-- | ios/BuildInstructions.md | 10 | ||||
| -rwxr-xr-x | scripts/setup-rust | 92 | ||||
| -rwxr-xr-x | scripts/setup-rust-post-checkout | 28 |
5 files changed, 145 insertions, 2 deletions
diff --git a/BuildInstructions.md b/BuildInstructions.md index b81ba0d9c7..db04a7f048 100644 --- a/BuildInstructions.md +++ b/BuildInstructions.md @@ -9,6 +9,15 @@ on your platform please submit an issue or a pull request. ## All platforms - Get the latest **stable** Rust toolchain via [rustup.rs](https://rustup.rs/). + - Install default targets and components needed for desktop + ```bash + ./scripts/setup-rust desktop + ``` + - (Optional) Run the following to install a git `post-checkout` hook that will automatically + run the `setup-rust` script when the Rust version specified in the `rust-toolchain.toml` file changes: + ```bash + .scripts/setup-rust install-hook + ``` - You need Node.js and npm. You can find the exact versions in the `volta` section of `desktop/package.json`. The toolchain is managed by volta. diff --git a/android/BuildInstructions.md b/android/BuildInstructions.md index 5755737df8..0c7b40907f 100644 --- a/android/BuildInstructions.md +++ b/android/BuildInstructions.md @@ -151,7 +151,13 @@ environment variables: - Install Android targets ```bash - rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android + ./scripts/setup-rust android + ``` + +- (Optional) Run the following to install a git `post-checkout` hook that will automatically + run the `setup-rust` script when the Rust version specified in the `rust-toolchain.toml` file changes: + ```bash + .scripts/setup-rust install-hook ``` #### 6. Download wireguard-go-rs submodule diff --git a/ios/BuildInstructions.md b/ios/BuildInstructions.md index bc586d00a5..2ba1cf9e9b 100644 --- a/ios/BuildInstructions.md +++ b/ios/BuildInstructions.md @@ -10,8 +10,16 @@ Rust should be installed via [rustup](https://rustup.rs). Once rust is installed, do not forget to install the iOS targets: ```bash -rustup target install aarch64-apple-ios aarch64-apple-ios-sim +./scripts/setup-rust ios ``` + +(Optional) Run the following to install a git `post-checkout` hook that will automatically +run the `setup-rust` script when the Rust version specified in the `rust-toolchain.toml` file changes: + +```bash +.scripts/setup-rust install-hook +``` + You only need to install the ARM simulator target, which matches the current Apple Silicon architecture. Once both rust and go are installed, ensure they are available in your path. diff --git a/scripts/setup-rust b/scripts/setup-rust new file mode 100755 index 0000000000..e853935131 --- /dev/null +++ b/scripts/setup-rust @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +# +# Installs the default toolchains and components for different platforms. +# To use this script rustup must be installed first. + +set -eu + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR/.." + +source scripts/utils/log + +ANDROID_TARGETS="x86_64-linux-android i686-linux-android aarch64-linux-android armv7-linux-androideabi" +ANDROID_COMPONENTS="rust-analyzer" + +DESKTOP_TARGETS="x86_64-pc-windows-msvc x86_64-pc-windows-gnu i686-pc-windows-msvc" +DESKTOP_COMPONENTS="rust-analyzer" + +IOS_TARGETS="aarch64-apple-ios-sim aarch64-apple-ios x86_64-apple-ios" +IOS_COMPONENTS="rust-analyzer" + +function main { + if [[ $# -eq 0 ]]; then + print_usage + exit 1 + fi + + case "$1" in + "android") + setup "Android" "$ANDROID_TARGETS" "$ANDROID_COMPONENTS" + ;; + "desktop") + setup "Desktop" "$DESKTOP_TARGETS" "$DESKTOP_COMPONENTS" + ;; + "ios") + setup "Android" "$IOS_TARGETS" "$IOS_COMPONENTS" + ;; + "install-hook") + install_hook + ;; + "--help") + print_usage + exit 0 + ;; + *) + log_error "Invalid argument: \`$1\`" + print_usage + exit 1 + ;; + esac +} + +function print_usage { + log "Setup default Rust targets and components for different platforms" + log "" + log "Usage: setup-rust android|desktop|ios|install-hook" + log " android Run Android-specific setup" + log " desktop Run Desktop-specific setup" + log " ios Run iOS-specific setup" + log " install-hook Copies the setup-rust-post-checkout file to .git/hooks/post-checkout" +} + +function setup { + local platform=$1 + local targets=$2 + local components=$3 + + log "Installing default Rust targets/components" + log "platform: $platform" + log "targets: $targets" + log "components: $components" + # shellcheck disable=SC2086 + rustup target add $targets + # shellcheck disable=SC2086 + rustup component add $components +} + +function install_hook { + local hook=$SCRIPT_DIR/../.git/hooks/post-checkout + if [[ -f "$hook" ]]; then + log_error "$(realpath "$hook") file already exists - will not overwrite" + exit 1 + else + cp "$SCRIPT_DIR/setup-rust-post-checkout" "$hook" + chmod +x "$hook" + log "Hook installed. You must now set the environment variable MULLVAD_SETUP_PLATFORM to " + log "\`android\`, \`desktop\` or \`ios\` in your shell environment." + fi +} + +# Run script +main "$@" diff --git a/scripts/setup-rust-post-checkout b/scripts/setup-rust-post-checkout new file mode 100755 index 0000000000..ef9275f6eb --- /dev/null +++ b/scripts/setup-rust-post-checkout @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +# +# To install the hook copy this file to `.git/hooks/post-checkout` or run +# scripts/setup-rust install_hook + +set -u + +# Set to "android", "desktop", or "ios". +MULLVAD_SETUP_PLATFORM="${MULLVAD_SETUP_PLATFORM:-}" + +SETUP_RUST_SCRIPT="scripts/setup-rust" + +if [[ ! -f "$SETUP_RUST_SCRIPT" ]]; then + exit 0 +fi + +if [[ -z ${MULLVAD_SETUP_PLATFORM+x} ]]; then + echo "MULLVAD_SETUP_PLATFORM is not set, must be set to " >&2 + echo "\`android\`, \`desktop\` or \`ios\`" >&2 + exit 1 +fi + +git diff-tree --exit-code "$1".."$2" --quiet -- rust-toolchain.toml + +# Exit code 1 means there was a change, 0 means no change. +if [[ $? -eq 1 ]]; then + $SETUP_RUST_SCRIPT "$MULLVAD_SETUP_PLATFORM" +fi |
