Skip to content

Commit 09d5632

Browse files
Rust: add the option to install additional targets (#1033)
* Rust: add the option to install additional targets Fixes #1032 * Apply suggestions from code review Co-authored-by: Samruddhi Khandale <[email protected]> * Changed description --------- Co-authored-by: Samruddhi Khandale <[email protected]>
1 parent 9387225 commit 09d5632

File tree

4 files changed

+60
-15
lines changed

4 files changed

+60
-15
lines changed

src/rust/devcontainer-feature.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "rust",
3-
"version": "1.1.3",
3+
"version": "1.2.0",
44
"name": "Rust",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/rust",
66
"description": "Installs Rust, common Rust utilities, and their required dependencies",
@@ -39,6 +39,16 @@
3939
],
4040
"default": "minimal",
4141
"description": "Select a rustup install profile."
42+
},
43+
"targets": {
44+
"type": "string",
45+
"default": "",
46+
"description": "Optional comma separated list of additional Rust targets to install.",
47+
"proposals": [
48+
"aarch64-unknown-linux-gnu",
49+
"armv7-unknown-linux-gnueabihf",
50+
"x86_64-unknown-redox,x86_64-unknown-uefi"
51+
]
4252
}
4353
},
4454
"customizations": {
@@ -70,4 +80,4 @@
7080
"installsAfter": [
7181
"ghcr.io/devcontainers/features/common-utils"
7282
]
73-
}
83+
}

src/rust/install.sh

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
RUST_VERSION="${VERSION:-"latest"}"
1111
RUSTUP_PROFILE="${PROFILE:-"minimal"}"
12+
RUSTUP_TARGETS="${TARGETS:-""}"
1213

1314
export CARGO_HOME="${CARGO_HOME:-"/usr/local/cargo"}"
1415
export RUSTUP_HOME="${RUSTUP_HOME:-"/usr/local/rustup"}"
@@ -19,7 +20,9 @@ UPDATE_RUST="${UPDATE_RUST:-"false"}"
1920
set -e
2021

2122
# Clean up
22-
rm -rf /var/lib/apt/lists/*
23+
if [ "$(ls -1 /var/lib/apt/lists/ | wc -l)" -gt -1 ]; then
24+
rm -rf /var/lib/apt/lists/*
25+
fi
2326

2427
if [ "$(id -u)" -ne 0 ]; then
2528
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
@@ -36,15 +39,15 @@ if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
3639
USERNAME=""
3740
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
3841
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
39-
if id -u ${CURRENT_USER} > /dev/null 2>&1; then
42+
if id -u "${CURRENT_USER}" > /dev/null 2>&1; then
4043
USERNAME=${CURRENT_USER}
4144
break
4245
fi
4346
done
4447
if [ "${USERNAME}" = "" ]; then
4548
USERNAME=root
4649
fi
47-
elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
50+
elif [ "${USERNAME}" = "none" ] || ! id -u "${USERNAME}" > /dev/null 2>&1; then
4851
USERNAME=root
4952
fi
5053

@@ -56,7 +59,7 @@ find_version_from_git_tags() {
5659
local repository=$2
5760
local prefix=${3:-"tags/v"}
5861
local separator=${4:-"."}
59-
local last_part_optional=${5:-"false"}
62+
local last_part_optional=${5:-"false"}
6063
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
6164
local escaped_separator=${separator//./\\.}
6265
local last_part
@@ -66,7 +69,7 @@ find_version_from_git_tags() {
6669
last_part="${escaped_separator}[0-9]+"
6770
fi
6871
local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
69-
local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
72+
local version_list="$(git ls-remote --tags "${repository}" | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
7073
if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then
7174
declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)"
7275
else
@@ -89,13 +92,13 @@ check_nightly_version_formatting() {
8992

9093
local version_date=$(echo ${requested_version} | sed -e "s/^nightly-//")
9194

92-
date -d ${version_date} &>/dev/null
93-
if [ $? != 0 ]; then
95+
96+
if ! date -d "${version_date}" &>/dev/null; then
9497
echo -e "Invalid ${variable_name} value: ${requested_version}\nNightly version should be in the format nightly-YYYY-MM-DD" >&2
9598
exit 1
9699
fi
97100

98-
if [ $(date -d ${version_date} +%s) -ge $(date +%s) ]; then
101+
if [ "$(date -d "${version_date}" +%s)" -ge "$(date +%s)" ]; then
99102
echo -e "Invalid ${variable_name} value: ${requested_version}\nNightly version should not exceed current date" >&2
100103
exit 1
101104
fi
@@ -141,10 +144,10 @@ fi
141144
architecture="$(dpkg --print-architecture)"
142145
download_architecture="${architecture}"
143146
case ${download_architecture} in
144-
amd64)
147+
amd64)
145148
download_architecture="x86_64"
146149
;;
147-
arm64)
150+
arm64)
148151
download_architecture="aarch64"
149152
;;
150153
*) echo "(!) Architecture ${architecture} not supported."
@@ -154,7 +157,7 @@ esac
154157

155158
# Install Rust
156159
umask 0002
157-
if ! cat /etc/group | grep -e "^rustlang:" > /dev/null 2>&1; then
160+
if ! grep -e "^rustlang:" /etc/group > /dev/null 2>&1; then
158161
groupadd -r rustlang
159162
fi
160163
usermod -a -G rustlang "${USERNAME}"
@@ -172,7 +175,7 @@ else
172175
fi
173176

174177
is_nightly=0
175-
echo ${RUST_VERSION} | grep -q "nightly" || is_nightly=$?
178+
echo "${RUST_VERSION}" | grep -q "nightly" || is_nightly=$?
176179
if [ $is_nightly = 0 ]; then
177180
check_nightly_version_formatting RUST_VERSION
178181
else
@@ -189,7 +192,7 @@ else
189192
cp /tmp/rustup/target/${download_architecture}-unknown-linux-gnu/release/rustup-init /tmp/rustup/rustup-init
190193
sha256sum -c rustup-init.sha256
191194
chmod +x target/${download_architecture}-unknown-linux-gnu/release/rustup-init
192-
target/${download_architecture}-unknown-linux-gnu/release/rustup-init -y --no-modify-path --profile ${RUSTUP_PROFILE} ${default_toolchain_arg}
195+
target/${download_architecture}-unknown-linux-gnu/release/rustup-init -y --no-modify-path --profile "${RUSTUP_PROFILE}" ${default_toolchain_arg}
193196
cd ~
194197
rm -rf /tmp/rustup
195198
fi
@@ -202,6 +205,14 @@ fi
202205
echo "Installing common Rust dependencies..."
203206
rustup component add rls rust-analysis rust-src rustfmt clippy 2>&1
204207

208+
if [ -n "${RUSTUP_TARGETS}" ]; then
209+
IFS=',' read -ra targets <<< "${RUSTUP_TARGETS}"
210+
for target in "${targets[@]}"; do
211+
echo "Installing additional Rust target $target"
212+
rustup target add "$target" 2>&1
213+
done
214+
fi
215+
205216
# Add CARGO_HOME, RUSTUP_HOME and bin directory into bashrc/zshrc files (unless disabled)
206217
updaterc "$(cat << EOF
207218
export RUSTUP_HOME="${RUSTUP_HOME}"

test/rust/rust_with_target.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Definition specific tests
9+
check "cargo version" cargo --version
10+
check "rustc version" rustc --version
11+
check "correct rust version" rustup target list | grep aarch64-unknown-linux-gnu
12+
13+
14+
# Report result
15+
reportResults

test/rust/scenarios.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@
66
"version": "1.62.0"
77
}
88
}
9+
},
10+
"rust_with_target": {
11+
"image": "ubuntu:focal",
12+
"features": {
13+
"rust": {
14+
"version": "latest",
15+
"targets": "aarch64-unknown-linux-gnu"
16+
}
17+
}
918
}
1019
}

0 commit comments

Comments
 (0)