Skip to content

Commit 7a33367

Browse files
Include up-rust devcontainer improvements for multi-platform capability (eclipse-uprotocol#30)
Dynamic install of rust toolchain based on host arch and OS (as has been done in eclipse-uprotocol/up-rust#275) * dynamic install of rust toolchain based on host arch and OS * wrap mac workaround script in OS check
1 parent 9bd493b commit 7a33367

File tree

5 files changed

+80
-17
lines changed

5 files changed

+80
-17
lines changed

.devcontainer/Dockerfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,3 @@ USER ${USERNAME}
4545

4646
# Install cargo cli tools
4747
RUN cargo install cargo-nextest cargo-deny cargo-tarpaulin --locked
48-
49-
# Install cargo tools for cross compilation
50-
RUN rustup target add aarch64-unknown-linux-gnu \
51-
&& rustup toolchain install stable-aarch64-unknown-linux-gnu

.devcontainer/devcontainer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"mounts": [
1414
"source=rust-volume,target=/rust-volume,type=volume"
1515
],
16+
"containerEnv": {
17+
"WORKSPACE_FOLDER": "${containerWorkspaceFolder}"
18+
},
1619
"postCreateCommand": "sudo .devcontainer/post-create.sh",
1720
"customizations": {
1821
"vscode": {

.devcontainer/post-create.sh

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
#!/bin/bash
22

3-
# This is a workaround which is againt necessary on MacOS 14.0, it looks like this bug is back:
4-
# https://github.com/microsoft/vscode-dev-containers/issues/1487#issuecomment-1143907307
5-
6-
# grant permissions to mounted rust volume
7-
chown vscode:vscode /rust-volume
8-
9-
# create /.cargo/config.toml in root folder
10-
mkdir /.cargo/
11-
touch /.cargo/config.toml
12-
cat <<EOF >/.cargo/config.toml
13-
[build]
14-
target-dir = "/rust-volume/target"
15-
EOF
3+
#region sudo stuff
4+
5+
HOST_OS=""
6+
ARCH_AND_OS_FUNC="${WORKSPACE_FOLDER}.devcontainer/scripts/functions/get-arch-and-os.sh"
7+
if [[ -f "$ARCH_AND_OS_FUNC" ]]; then
8+
source $ARCH_AND_OS_FUNC
9+
read -r _ HOST_OS <<< "$(get_arch_and_os)"
10+
fi
11+
if [[ "$HOST_OS" == "darwin" ]]; then # darwin == Mac OS
12+
# This is a workaround which is againt necessary on MacOS 14.0, it looks like this bug is back:
13+
# https://github.com/microsoft/vscode-dev-containers/issues/1487#issuecomment-1143907307
14+
# grant permissions to mounted rust volume
15+
echo "(Mac OS only) Granting permissions to mounted rust volume"
16+
sudo chown vscode:vscode /rust-volume
17+
18+
# create /.cargo/config.toml in root folder
19+
sudo mkdir /.cargo/
20+
sudo touch /.cargo/config.toml
21+
sudo bash -c "cat << EOF > /.cargo/config.toml
22+
[build]
23+
target-dir = \"/rust-volume/target\"
24+
EOF"
25+
fi
26+
27+
#endregion
28+
29+
for arg in "$@"; do
30+
if [[ "$arg" == --workspace-folder=* ]]; then
31+
WORKSPACE_FOLDER="${arg#--workspace-folder=}"
32+
fi
33+
done
34+
INSTALL_RUST_TOOLCHAIN_FUNC="${WORKSPACE_FOLDER}.devcontainer/scripts/functions/install-rust-toolchain.sh"
35+
if [[ -f "$INSTALL_RUST_TOOLCHAIN_FUNC" ]]; then
36+
source $INSTALL_RUST_TOOLCHAIN_FUNC
37+
install_rust_toolchain "$@"
38+
fi
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
get_arch_and_os() {
3+
ARCH=$(uname -m)
4+
OS=$(uname -s | tr "[:upper:]" "[:lower:]")
5+
6+
case "$ARCH" in
7+
x86_64)
8+
TARGET_ARCH="x86_64"
9+
;;
10+
aarch64 | arm64)
11+
TARGET_ARCH="aarch64"
12+
;;
13+
*)
14+
echo "Unsupported architecture: $ARCH"
15+
exit 1
16+
;;
17+
esac
18+
19+
case "$OS" in
20+
linux)
21+
TARGET_OS="unknown-linux-gnu"
22+
;;
23+
*)
24+
echo "OS is unsupported or not implemented in this script: $OS"
25+
exit 1
26+
;;
27+
esac
28+
echo "$TARGET_ARCH $TARGET_OS"
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
install_rust_toolchain() {
3+
source ${WORKSPACE_FOLDER}/.devcontainer/scripts/functions/get-arch-and-os.sh
4+
read -r TARGET_ARCH TARGET_OS <<< "$(get_arch_and_os)"
5+
ARCH_AND_OS="${TARGET_ARCH}-${TARGET_OS}"
6+
echo "Detected architecture and OS: $ARCH_AND_OS"
7+
RUST_TOOLCHAIN="stable-$ARCH_AND_OS"
8+
echo "Adding rustup target '$ARCH_AND_OS'"
9+
rustup target add "$ARCH_AND_OS"
10+
echo "Installing Rust toolchain for '$ARCH_AND_OS'"
11+
rustup toolchain install "$RUST_TOOLCHAIN"
12+
}

0 commit comments

Comments
 (0)