From 75d3ea59bafbebee320a64aa2173c324ec55a4e6 Mon Sep 17 00:00:00 2001
From: Yuki Okushi <huyuumi.dev@gmail.com>
Date: Tue, 14 Jul 2020 14:11:00 +0900
Subject: [PATCH] Revive CI for the switch target

---
 ci/azure.yml                | 10 +++++++++
 ci/build.sh                 | 25 +++-------------------
 ci/docker/switch/Dockerfile |  6 ++++++
 ci/run-docker.sh            | 41 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 59 insertions(+), 23 deletions(-)
 create mode 100644 ci/docker/switch/Dockerfile

diff --git a/ci/azure.yml b/ci/azure.yml
index ecdf88d5..66ec3edf 100644
--- a/ci/azure.yml
+++ b/ci/azure.yml
@@ -186,6 +186,16 @@ jobs:
     variables:
       OS: linux
 
+  # devkitpro's pacman needs to be connected from Docker.
+  - job: DockerSwitch
+    dependsOn: StyleAndDocs
+    pool:
+      vmImage: ubuntu-18.04
+    steps:
+      - template: azure-install-rust.yml
+      - bash: LIBC_CI=1 sh ./ci/run-docker.sh switch
+        displayName: Execute run-docker.sh
+
   - job: BuildChannelsOSX
     dependsOn: StyleAndDocs
     pool:
diff --git a/ci/build.sh b/ci/build.sh
index 020986ac..2330914e 100644
--- a/ci/build.sh
+++ b/ci/build.sh
@@ -51,19 +51,19 @@ test_target() {
 
     # Test that libc builds with the `extra_traits` feature
     cargo "+${RUST}" "${BUILD_CMD}" -vv --no-default-features --target "${TARGET}" \
-          --features extra_traits
+            --features extra_traits
 
     # Test the 'const-extern-fn' feature on nightly
     if [ "${RUST}" = "nightly" ]; then
         cargo "+${RUST}" "${BUILD_CMD}" -vv --no-default-features --target "${TARGET}" \
-          --features const-extern-fn
+            --features const-extern-fn
     fi
 
 
     # Also test that it builds with `extra_traits` and default features:
     if [ "$NO_STD" != "1" ]; then
         cargo "+${RUST}" "${BUILD_CMD}" -vv --target "${TARGET}" \
-              --features extra_traits
+            --features extra_traits
     fi
 }
 
@@ -233,25 +233,6 @@ if [ "${RUST}" = "nightly" ] && [ "${OS}" = "linux" ]; then
             test_target xbuild "$TARGET" 1
         fi
     done
-
-    # Nintendo switch
-    # FIXME: Somehow downloads.devkitpro.org returns 403 now.
-    # Temorarily disabled tests for this target.
-    # cargo clean
-    # mkdir -p target
-    # (
-    #     cd target
-    #     wget https://github.com/devkitPro/pacman/releases/download/v1.0.2/devkitpro-pacman.amd64.deb
-    #     sudo apt-get install gdebi-core
-    #     sudo gdebi -nq devkitpro-pacman.amd64.deb
-    #     sudo dkp-pacman -Sy
-    #     sudo dkp-pacman -Syu
-    #     sudo dkp-pacman -S -v --noconfirm switch-dev devkitA64
-    # )
-    # cp ci/switch.json switch.json
-    # PATH="$PATH:/opt/devkitpro/devkitA64/bin"
-    # PATH="$PATH:/opt/devkitpro/tools/bin"
-    # cargo xbuild --target switch.json
 fi
 
 RUST_OSX_NO_CORE_TARGETS="\
diff --git a/ci/docker/switch/Dockerfile b/ci/docker/switch/Dockerfile
new file mode 100644
index 00000000..4118969d
--- /dev/null
+++ b/ci/docker/switch/Dockerfile
@@ -0,0 +1,6 @@
+FROM huyuumi/libc-switch:latest
+
+RUN apt-get update && apt-get install -y --no-install-recommends \
+    gcc libc6-dev ca-certificates
+
+ENV PATH=$PATH:/rust/bin
diff --git a/ci/run-docker.sh b/ci/run-docker.sh
index 059e5377..8440a002 100755
--- a/ci/run-docker.sh
+++ b/ci/run-docker.sh
@@ -50,10 +50,49 @@ run() {
       sh -c "HOME=/tmp PATH=\$PATH:/rust/bin exec ci/run.sh ${1}"
 }
 
+build_switch() {
+    echo "Building docker container for target switch"
+
+    # use -f so we can use ci/ as build context
+    docker build -t libc -f "ci/docker/switch/Dockerfile" ci/
+    mkdir -p target
+    if [ -w /dev/kvm ]; then
+        kvm="--volume /dev/kvm:/dev/kvm"
+    else
+        kvm=""
+    fi
+
+    cargo +nightly install cargo-xbuild
+    cp "$(which rustup)" "$(rustc --print sysroot)/bin"
+
+    docker run \
+      --rm \
+      --user "$(id -u)":"$(id -g)" \
+      --env LIBC_CI \
+      --env CARGO_HOME=/cargo \
+      --env CARGO_TARGET_DIR=/checkout/target \
+      --volume "$CARGO_HOME":/cargo \
+      --volume "$(rustc --print sysroot)":/rust:ro \
+      --volume "$(pwd)":/checkout:ro \
+      --volume "$(pwd)"/target:/checkout/target \
+      --volume ~/.rustup:/.rustup:Z \
+      $kvm \
+      --init \
+      --workdir /checkout \
+      libc \
+      sh -c "HOME=/tmp RUSTUP_HOME=/tmp PATH=\$PATH:/rust/bin rustup default nightly \
+        && rustup component add rust-src --target ci/switch.json \
+        && cargo xbuild --target ci/switch.json"
+}
+
 if [ -z "${1}" ]; then
   for d in ci/docker/*; do
     run "${d}"
   done
 else
-  run "${1}"
+  if [ "${1}" != "switch" ]; then
+    run "${1}"
+  else
+    build_switch
+  fi
 fi
-- 
GitLab