From 5fb572368b9c1a2358f73a1d0f75d964cc5826ca Mon Sep 17 00:00:00 2001
From: Zubin Duggal <zubin.duggal@gmail.com>
Date: Mon, 13 Dec 2021 18:04:41 +0530
Subject: [PATCH] ci: test bootstrapping and use hadrian for source dists

---
 .gitlab-ci.yml | 41 ++++++++++++++++++++++++++++++-----------
 .gitlab/ci.sh  | 17 ++++++++++-------
 2 files changed, 40 insertions(+), 18 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index ddd9de6b1a19..0aeff2f59113 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1239,27 +1239,46 @@ doc-tarball:
 
 source-tarball:
   stage: packaging
-  needs: [validate-x86_64-linux-deb9-unreg-hadrian]
   tags:
     - x86_64-linux
   image: "registry.gitlab.haskell.org/ghc/ci-images/x86_64-linux-deb10:$DOCKER_REV"
   dependencies: []
-  rules:
-    - if: '$RELEASE_JOB == "yes"'
-      when: always
   artifacts:
     paths:
       - ghc-*.tar.xz
-      - version
+      - hadrian-bootstrap-sources-*.tar.gz
   script:
-    - python3 mk/get-win32-tarballs.py download all
     - ./boot
     - ./configure
-    - make sdist
-    - mv sdistprep/*.xz  .
-    - make show! --quiet VALUE=ProjectVersion > version
-    - source version
-    - echo "$ProjectVersion" > version
+    - ./hadrian/build source-dist
+    - mv _build/source-dist/*.xz  .
+    - python3 ./hadrian/bootstrap/bootstrap.py -w $GHC fetch -o hadrian-bootstrap-sources-$GHC_VERSION
+
+test-bootstrap:
+  stage: packaging
+  needs: [source-tarball]
+  tags:
+    - x86_64-linux
+  image: "registry.gitlab.haskell.org/ghc/ci-images/x86_64-linux-deb10:$DOCKER_REV"
+  dependencies: [source-tarball]
+  script:
+    - mkdir test-bootstrap
+    - tar -xf ghc-*[0-9]-src.tar.xz -C test-bootstrap
+    - tar -xf ghc-*-testsuite.tar.xz -C test-bootstrap
+    - cp hadrian-bootstrap-sources-*.tar.gz test-bootstrap/ghc-*
+    - pushd test-bootstrap/ghc-*
+    - python3 ./hadrian/bootstrap/bootstrap.py -w $GHC --bootstrap-sources hadrian-bootstrap-sources-*.tar.gz
+    - export HADRIAN_PATH="$PWD/_build/bin/hadrian"
+    - .gitlab/ci.sh setup
+    - .gitlab/ci.sh configure
+    - .gitlab/ci.sh build_hadrian
+    - .gitlab/ci.sh test_hadrian
+    - popd
+    - rm -Rf test-bootstrap
+  variables:
+    TEST_ENV: "x86_64-linux-deb10-hadrian"
+    BIN_DIST_NAME: "ghc-x86_64-deb10-linux"
+    BUILD_FLAVOUR: "validate"
 
 
 ############################################################
diff --git a/.gitlab/ci.sh b/.gitlab/ci.sh
index 22bcada3b03e..30dd0ff75793 100755
--- a/.gitlab/ci.sh
+++ b/.gitlab/ci.sh
@@ -581,13 +581,16 @@ function run_hadrian() {
   if [ -z "${BIGNUM_BACKEND:-}" ]; then BIGNUM_BACKEND="gmp"; fi
   read -r -a args <<< "${HADRIAN_ARGS:-}"
   if [ -n "${VERBOSE:-}" ]; then args+=("-V"); fi
-  run hadrian/build-cabal \
-    --flavour="$BUILD_FLAVOUR" \
-    -j"$cores" \
-    --broken-test="${BROKEN_TESTS:-}" \
-    --bignum=$BIGNUM_BACKEND \
-    "${args[@]+"${args[@]}"}" \
-    "$@"
+  # Before running the compiler, unset variables gitlab env vars as these
+  # can destabilise the performance test (see #20341)
+  (unset $(compgen -v | grep CI_*);
+    run "${HADRIAN_PATH:-hadrian/build-cabal}" \
+      --flavour="$BUILD_FLAVOUR" \
+      -j"$cores" \
+      --broken-test="${BROKEN_TESTS:-}" \
+      --bignum=$BIGNUM_BACKEND \
+      "${args[@]+"${args[@]}"}" \
+      "$@")
 }
 
 # A convenience function to allow debugging in the CI environment.
-- 
GitLab