From e1f54039fedf4abe88894566d90f424e448a6dda Mon Sep 17 00:00:00 2001
From: Peng Fan <fanpeng@loongson.cn>
Date: Thu, 20 Mar 2025 11:55:49 +0000
Subject: [PATCH] CI: add cross compiling and testing for LoongArch64

Starting to implement CI pipelines for LoongArch64 cross compiling
and testing.
---
 .gitlab-ci.yml                |   2 +-
 .gitlab/generate-ci/gen_ci.hs |   7 ++
 .gitlab/jobs.yaml             | 127 ++++++++++++++++++++++++++++++++++
 3 files changed, 135 insertions(+), 1 deletion(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 3ede1c7a0909..afc7d3d137de 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,7 +2,7 @@ variables:
   GIT_SSL_NO_VERIFY: "1"
 
   # Commit of ghc/ci-images repository from which to pull Docker images
-  DOCKER_REV: 94df7d589f0ded990826bc7a4d7f5a40d6055a4f
+  DOCKER_REV: a9e5d4f29a2e307142954e2516368ea6d6a9bb88
 
   # Sequential version number of all cached things.
   # Bump to invalidate GitLab CI cache.
diff --git a/.gitlab/generate-ci/gen_ci.hs b/.gitlab/generate-ci/gen_ci.hs
index b363a033846c..92672adbea41 100644
--- a/.gitlab/generate-ci/gen_ci.hs
+++ b/.gitlab/generate-ci/gen_ci.hs
@@ -119,6 +119,7 @@ data LinuxDistro
   | Debian9
   | Fedora33
   | Fedora38
+  | Ubuntu2404LoongArch64
   | Ubuntu2204
   | Ubuntu2004
   | Ubuntu1804
@@ -321,6 +322,7 @@ distroName Fedora38      = "fedora38"
 distroName Ubuntu1804    = "ubuntu18_04"
 distroName Ubuntu2004    = "ubuntu20_04"
 distroName Ubuntu2204    = "ubuntu22_04"
+distroName Ubuntu2404LoongArch64 = "ubuntu24_04-loongarch64"
 distroName Alpine312     = "alpine3_12"
 distroName Alpine318     = "alpine3_18"
 distroName Alpine320     = "alpine3_20"
@@ -722,6 +724,7 @@ data ValidateRule
   | IpeData      -- ^ Run this job when the "IPE" label is set
   | TestPrimops  -- ^ Run this job when "test-primops" label is set
   | I386Backend  -- ^ Run this job when the "i386" label is set
+  | LoongArch64  -- ^ Run this job when the "LoongArch64" label is set
   deriving (Show, Ord, Eq)
 
 -- | Convert the state of the rule into a string that gitlab understand.
@@ -766,6 +769,7 @@ validateRuleString NonmovingGc  = labelString "non-moving GC"
 validateRuleString IpeData      = labelString "IPE"
 validateRuleString TestPrimops  = labelString "test-primops"
 validateRuleString I386Backend  = labelString "i386"
+validateRuleString LoongArch64  = labelString "LoongArch64"
 
 ---------------------------------------------------------------------
 -- The Job type
@@ -1253,6 +1257,9 @@ cross_jobs = [
     -- x86_64 -> riscv
   , addValidateRule RiscV (validateBuilds Amd64 (Linux Debian12Riscv) (crossConfig "riscv64-linux-gnu" (Emulator "qemu-riscv64 -L /usr/riscv64-linux-gnu") Nothing))
 
+    -- x86_64 -> loongarch64
+  , addValidateRule LoongArch64 (validateBuilds Amd64 (Linux Ubuntu2404LoongArch64) (crossConfig "loongarch64-linux-gnu" (Emulator "qemu-loongarch64 -L /usr/loongarch64-linux-gnu") Nothing))
+
     -- Javascript
   , addValidateRule JSBackend (validateBuilds Amd64 (Linux Debian11Js) javascriptConfig)
 
diff --git a/.gitlab/jobs.yaml b/.gitlab/jobs.yaml
index e2cd85898463..f1857bf86125 100644
--- a/.gitlab/jobs.yaml
+++ b/.gitlab/jobs.yaml
@@ -2234,6 +2234,70 @@
       "XZ_OPT": "-9"
     }
   },
+  "nightly-x86_64-linux-ubuntu24_04-loongarch64-cross_loongarch64-linux-gnu-validate": {
+    "after_script": [
+      ".gitlab/ci.sh save_cache",
+      ".gitlab/ci.sh save_test_output",
+      ".gitlab/ci.sh clean",
+      "cat ci_timings"
+    ],
+    "allow_failure": false,
+    "artifacts": {
+      "expire_in": "8 weeks",
+      "paths": [
+        "ghc-x86_64-linux-ubuntu24_04-loongarch64-cross_loongarch64-linux-gnu-validate.tar.xz",
+        "junit.xml",
+        "unexpected-test-output.tar.gz"
+      ],
+      "reports": {
+        "junit": "junit.xml"
+      },
+      "when": "always"
+    },
+    "cache": {
+      "key": "x86_64-linux-ubuntu24_04-loongarch64-$CACHE_REV",
+      "paths": [
+        "cabal-cache",
+        "toolchain"
+      ]
+    },
+    "dependencies": [],
+    "image": "registry.gitlab.haskell.org:443/lrzlin/ci-images/x86_64-linux-ubuntu24_04-loongarch64:$DOCKER_REV",
+    "needs": [
+      {
+        "artifacts": false,
+        "job": "hadrian-ghc-in-ghci"
+      }
+    ],
+    "rules": [
+      {
+        "if": "(\"true\" == \"true\") && ($RELEASE_JOB != \"yes\") && ($NIGHTLY)",
+        "when": "on_success"
+      }
+    ],
+    "script": [
+      "sudo chown ghc:ghc -R .",
+      ".gitlab/ci.sh setup",
+      ".gitlab/ci.sh configure",
+      ".gitlab/ci.sh build_hadrian",
+      ".gitlab/ci.sh test_hadrian"
+    ],
+    "stage": "full-build",
+    "tags": [
+      "x86_64-linux"
+    ],
+    "variables": {
+      "BIGNUM_BACKEND": "gmp",
+      "BIN_DIST_NAME": "ghc-x86_64-linux-ubuntu24_04-loongarch64-cross_loongarch64-linux-gnu-validate",
+      "BUILD_FLAVOUR": "validate",
+      "CONFIGURE_ARGS": "--with-intree-gmp",
+      "CROSS_EMULATOR": "qemu-loongarch64 -L /usr/loongarch64-linux-gnu",
+      "CROSS_TARGET": "loongarch64-linux-gnu",
+      "RUNTEST_ARGS": "",
+      "TEST_ENV": "x86_64-linux-ubuntu24_04-loongarch64-cross_loongarch64-linux-gnu-validate",
+      "XZ_OPT": "-9"
+    }
+  },
   "nightly-x86_64-linux-deb12-unreg-validate": {
     "after_script": [
       ".gitlab/ci.sh save_cache",
@@ -6362,6 +6426,69 @@
       "TEST_ENV": "x86_64-linux-deb12-riscv-cross_riscv64-linux-gnu-validate"
     }
   },
+  "x86_64-linux-ubuntu24_04-loongarch64-cross_loongarch64-linux-gnu-validate": {
+    "after_script": [
+      ".gitlab/ci.sh save_cache",
+      ".gitlab/ci.sh save_test_output",
+      ".gitlab/ci.sh clean",
+      "cat ci_timings"
+    ],
+    "allow_failure": false,
+    "artifacts": {
+      "expire_in": "2 weeks",
+      "paths": [
+        "ghc-x86_64-linux-ubuntu24_04-loongarch64-cross_loongarch64-linux-gnu-validate.tar.xz",
+        "junit.xml",
+        "unexpected-test-output.tar.gz"
+      ],
+      "reports": {
+        "junit": "junit.xml"
+      },
+      "when": "always"
+    },
+    "cache": {
+      "key": "x86_64-linux-ubuntu24_04-loongarch64-$CACHE_REV",
+      "paths": [
+        "cabal-cache",
+        "toolchain"
+      ]
+    },
+    "dependencies": [],
+    "image": "registry.gitlab.haskell.org:443/lrzlin/ci-images/x86_64-linux-ubuntu24_04-loongarch64:$DOCKER_REV",
+    "needs": [
+      {
+        "artifacts": false,
+        "job": "hadrian-ghc-in-ghci"
+      }
+    ],
+    "rules": [
+      {
+        "if": "((($ONLY_JOBS) && ($ONLY_JOBS =~ /.*\\bx86_64-linux-ubuntu24_04-loongarch64-cross_loongarch64-linux-gnu-validate(\\s|$).*/)) || (($ONLY_JOBS == null) && ((($CI_MERGE_REQUEST_LABELS =~ /.*full-ci.*/) || ($CI_MERGE_REQUEST_LABELS =~ /.*marge_bot_batch_merge_job.*/) || ($CI_COMMIT_BRANCH == \"master\") || ($CI_COMMIT_BRANCH =~ /ghc-[0-9]+\\.[0-9]+/)) || ($CI_MERGE_REQUEST_LABELS =~ /.*LoongArch64.*/)))) && ($RELEASE_JOB != \"yes\") && ($NIGHTLY == null)",
+        "when": "on_success"
+      }
+    ],
+    "script": [
+      "sudo chown ghc:ghc -R .",
+      ".gitlab/ci.sh setup",
+      ".gitlab/ci.sh configure",
+      ".gitlab/ci.sh build_hadrian",
+      ".gitlab/ci.sh test_hadrian"
+    ],
+    "stage": "full-build",
+    "tags": [
+      "x86_64-linux"
+    ],
+    "variables": {
+      "BIGNUM_BACKEND": "gmp",
+      "BIN_DIST_NAME": "ghc-x86_64-linux-ubuntu24_04-loongarch64-cross_loongarch64-linux-gnu-validate",
+      "BUILD_FLAVOUR": "validate",
+      "CONFIGURE_ARGS": "--with-intree-gmp",
+      "CROSS_EMULATOR": "qemu-loongarch64 -L /usr/loongarch64-linux-gnu",
+      "CROSS_TARGET": "loongarch64-linux-gnu",
+      "RUNTEST_ARGS": "",
+      "TEST_ENV": "x86_64-linux-ubuntu24_04-loongarch64-cross_loongarch64-linux-gnu-validate"
+    }
+  },
   "x86_64-linux-deb12-unreg-validate": {
     "after_script": [
       ".gitlab/ci.sh save_cache",
-- 
GitLab