stages:
  - checks
  - quick-test
  - test
  - expensive-test
  - release

variables:
  GIT_SSL_NO_VERIFY: "1"

  # Commit of ghc/ci-images repository from which to pull Docker images
  DOCKER_REV: 8d0224e6b2a08157649651e69302380b2bd24e11

  # Sequential version number of all cached things.
  # Bump to invalidate GitLab CI cache.
  CACHE_REV: 1

  GIT_SUBMODULE_STRATEGY: recursive


############################################################
# CI Step
############################################################

.debian:
  image: "registry.gitlab.haskell.org/ghc/ci-images/x86_64-linux-deb10:$DOCKER_REV"
  tags:
    - x86_64-linux
  variables:
    OS: "LINUX"
    ARCH: "64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"
    CROSS: ""

.alpine:64bit:
  image: "alpine:3.12"
  tags:
    - x86_64-linux
  variables:
    OS: "LINUX"
    ARCH: "64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.alpine:32bit:
  image: "i386/alpine:3.12"
  tags:
    - x86_64-linux
  variables:
    OS: "LINUX"
    ARCH: "32"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.linux:armv7:
  image: "registry.gitlab.haskell.org/ghc/ci-images/armv7-linux-deb10:$DOCKER_REV"
  tags:
    - armv7-linux
  variables:
    OS: "LINUX"
    ARCH: "ARM"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"
  retry: 2

.linux:aarch64:
  image: "registry.gitlab.haskell.org/ghc/ci-images/aarch64-linux-deb10:$DOCKER_REV"
  tags:
    - aarch64-linux
  variables:
    OS: "LINUX"
    ARCH: "ARM64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.darwin:
  tags:
    - x86_64-darwin
  variables:
    OS: "DARWIN"
    ARCH: "64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.darwin:aarch64:
  tags:
    - aarch64-darwin-m1
  variables:
    OS: "DARWIN"
    ARCH: "ARM64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.freebsd13:
  tags:
    - x86_64-freebsd13
  variables:
    OS: "FREEBSD"
    ARCH: "64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.freebsd12:
  tags:
    - x86_64-freebsd12
  variables:
    OS: "FREEBSD"
    ARCH: "64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.windows:
  tags:
    - new-x86_64-windows
  variables:
    OS: "WINDOWS"
    ARCH: "64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"
  retry: 2

.root_cleanup:
  after_script:
    - bash ./.gitlab/after_script.sh

.test_ghcup_version:
  script:
    - bash ./.gitlab/script/ghcup_version.sh
  variables:
    JSON_VERSION: "0.0.7"
  artifacts:
    expire_in: 2 week
    paths:
      - test/golden
      - dist-newstyle/cache/
    when: on_failure
  cache:
    key: ghcup-test-$CACHE_REV
    paths:
      - cabal-cache

# .test_ghcup_scoop:
  # script:
    # - cl /O1 scoop-better-shimexe/shim.c

.test_ghcup_version:linux:
  extends:
    - .test_ghcup_version
    - .debian
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh
    - ./.gitlab/script/ci.sh extract_cabal_cache
  after_script:
    - ./.gitlab/script/ci.sh save_cabal_cache
    - bash ./.gitlab/after_script.sh

.test_ghcup_version:linux32:
  extends:
    - .test_ghcup_version
    - .alpine:32bit
  before_script:
    - ./.gitlab/before_script/linux/alpine/install_deps.sh
    - ./.gitlab/script/ci.sh extract_cabal_cache
  after_script:
    - ./.gitlab/script/ci.sh save_cabal_cache
    - bash ./.gitlab/after_script.sh

.test_ghcup_version:armv7:
  extends:
    - .test_ghcup_version
    - .linux:armv7
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh
    - ./.gitlab/script/ci.sh extract_cabal_cache
  after_script:
    - ./.gitlab/script/ci.sh save_cabal_cache
    - bash ./.gitlab/after_script.sh

.test_ghcup_version:aarch64:
  extends:
    - .test_ghcup_version
    - .linux:aarch64
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh
    - ./.gitlab/script/ci.sh extract_cabal_cache
  after_script:
    - ./.gitlab/script/ci.sh save_cabal_cache
    - bash ./.gitlab/after_script.sh

.test_ghcup_version:darwin:
  extends:
    - .test_ghcup_version
    - .darwin
  before_script:
    - ./.gitlab/before_script/darwin/install_deps.sh
    - ./.gitlab/script/ci.sh extract_cabal_cache
  after_script:
    - ./.gitlab/script/ci.sh save_cabal_cache
    - bash ./.gitlab/after_script.sh

.test_ghcup_version:darwin:aarch64:
  extends:
    - .test_ghcup_version
    - .darwin:aarch64
  cache:
    key: darwin-brew-$CACHE_REV
    paths:
      - brew_cache
    key: ghcup-test-$CACHE_REV
    paths:
      - cabal-cache
  before_script:
    # extract brew cache
    - ./.gitlab/script/ci.sh extract_brew_cache
    # otherwise we seem to get intel binaries
    - export HOMEBREW_CHANGE_ARCH_TO_ARM=1
    # update and install packages
    - /bin/bash ./.gitlab/script/brew.sh llvm autoconf automake coreutils
    # extract cabal cache
    - ./.gitlab/script/ci.sh extract_cabal_cache
  script: |
    export PATH="$CI_PROJECT_DIR/.brew/opt/llvm/bin:$CI_PROJECT_DIR/.brew/bin:$CI_PROJECT_DIR/.brew/sbin:$PATH"
    export CC=$CI_PROJECT_DIR/.brew/opt/llvm/bin/clang
    export CXX=$CI_PROJECT_DIR/.brew/opt/llvm/bin/clang++
    export LD=ld
    export AR=$CI_PROJECT_DIR/.brew/opt/llvm/bin/llvm-ar
    export RANLIB=$CI_PROJECT_DIR/.brew/opt/llvm/bin/llvm-ranlib
    ./.gitlab/before_script/darwin/install_deps.sh
    ./.gitlab/script/ghcup_version.sh
  after_script:
    - ./.gitlab/script/ci.sh save_cabal_cache
    - ./.gitlab/script/ci.sh save_brew_cache
    - bash ./.gitlab/after_script.sh

.test_ghcup_version:freebsd12:
  extends:
    - .test_ghcup_version
    - .freebsd12
  before_script:
    - ./.gitlab/before_script/freebsd/install_deps.sh
    - ./.gitlab/script/ci.sh extract_cabal_cache
  after_script:
    - ./.gitlab/script/ci.sh save_cabal_cache
    - bash ./.gitlab/after_script.sh

.test_ghcup_version:freebsd13:
  extends:
    - .test_ghcup_version
    - .freebsd13
  before_script:
    - sudo pkg update
    - sudo pkg install --yes compat12x-amd64
    - sudo ln -s libncurses.so.6 /usr/local/lib/libncurses.so.6.2
    - ./.gitlab/before_script/freebsd/install_deps.sh
    - ./.gitlab/script/ci.sh extract_cabal_cache
  after_script:
    - ./.gitlab/script/ci.sh save_cabal_cache
    - bash ./.gitlab/after_script.sh

.test_ghcup_version:windows:
  extends:
    - .test_ghcup_version
    - .windows
  before_script:
    - bash ./.gitlab/before_script/windows/install_deps.sh
    - bash ./.gitlab/script/ci.sh extract_cabal_cache
  after_script:
    - bash ./.gitlab/script/ci.sh save_cabal_cache
    - bash ./.gitlab/after_script.sh

# .test_ghcup_scoop:windows:
  # extends:
    # - .windows
    # - .test_ghcup_scoop

.release_ghcup:
  script:
    - bash ./.gitlab/script/ghcup_release.sh
  artifacts:
    expire_in: 2 week
    paths:
      - out
      - dist-newstyle/cache/
  only:
    - tags
  variables:
    JSON_VERSION: "0.0.7"

######## stack test ########

test:linux:stack:
  stage: test
  before_script:
    - ./.gitlab/script/ci.sh extract_stack_cache
    - ./.gitlab/before_script/linux/install_deps_minimal.sh
  script:
    - ./.gitlab/script/ghcup_stack.sh
  after_script:
    - ./.gitlab/script/ci.sh save_stack_cache
  extends:
    - .debian
  needs: []

######## bootstrap test ########

test:linux:bootstrap_script:
  stage: quick-test
  before_script:
    - ./.gitlab/before_script/linux/install_deps_minimal.sh
  script:
    - ./.gitlab/script/ghcup_bootstrap.sh
  variables:
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
  extends:
    - .debian
    - .root_cleanup
  needs: []

test:windows:bootstrap_powershell_script:
  stage: quick-test
  script:
    - ./scripts/bootstrap/bootstrap-haskell.ps1 -InstallDir $CI_PROJECT_DIR -BootstrapUrl $CI_PROJECT_DIR/bootstrap-haskell -InBash
  after_script:
    - "[Environment]::SetEnvironmentVariable('GHCUP_INSTALL_BASE_PREFIX', $null, [System.EnvironmentVariableTarget]::User)"
    - "[Environment]::SetEnvironmentVariable('GHCUP_MSYS2', $null, [System.EnvironmentVariableTarget]::User)"
    - "[Environment]::SetEnvironmentVariable('CABAL_DIR', $null, [System.EnvironmentVariableTarget]::User)"
    - bash ./.gitlab/after_script.sh
    - bash ./.gitlab/script/ci.sh save_cabal_cache
  variables:
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
  extends:
    - .windows
  needs: []

######## linux test ########

test:linux:
  stage: test
  extends: .test_ghcup_version:linux
  variables:
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
  needs: []

test:linux:hls:
  stage: expensive-test
  extends:
    - .test_ghcup_version
    - .debian
  variables:
    GHC_VERSION: "8.10.7"
    HLS_TARGET_VERSION: "1.4.0"
    CABAL_VERSION: "3.6.2.0"
  needs: []
  when: manual
  allow_failure: true
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh
  script:
    - ./.gitlab/script/ghcup_hls.sh

test:linux:cross-armv7:
  stage: expensive-test
  extends:
    - .test_ghcup_version
    - .debian
  variables:
    GHC_VERSION: "8.10.6"
    GHC_TARGET_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
    CROSS: "arm-linux-gnueabihf"
  needs: []
  when: manual
  allow_failure: true
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh
  script:
    - ./.gitlab/script/ghcup_cross.sh

test:linux:git:hadrian:
  stage: expensive-test
  extends:
    - .test_ghcup_version
    - .debian
  variables:
    GHC_VERSION: "8.10.7"
    GHC_GIT_TAG: "ghc-9.0.1-release"
    GHC_GIT_VERSION: "9.0.1"
    CABAL_VERSION: "3.6.2.0"
    CROSS: ""
  needs: []
  when: manual
  allow_failure: true
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh
  script:
    - ./.gitlab/script/ghcup_git.sh


######## linux 32bit test ########

test:linux:32bit:
  stage: test
  extends: .test_ghcup_version:linux32
  variables:
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
  needs: []

######## arm tests ########

test:linux:armv7:
  stage: test
  extends: .test_ghcup_version:armv7
  variables:
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
    CROSS: ""
  when: manual
  needs: []

test:linux:aarch64:
  stage: test
  extends: .test_ghcup_version:aarch64
  variables:
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
    CROSS: ""
  when: manual
  needs: []

######## darwin test ########

test:mac:
  stage: test
  extends: .test_ghcup_version:darwin
  variables:
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
  needs: []

test:mac:aarch64:
  stage: test
  extends: .test_ghcup_version:darwin:aarch64
  variables:
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
  needs: []
  allow_failure: true
  when: manual


######## freebsd test ########

test:freebsd12:
  stage: test
  extends: .test_ghcup_version:freebsd12
  variables:
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
  allow_failure: true # freebsd runners are unreliable
  when: manual
  needs: []

test:freebsd13:
  stage: test
  extends: .test_ghcup_version:freebsd13
  variables:
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
  allow_failure: true # freebsd runners are unreliable
  when: manual
  needs: []

######## windows test ########

test:windows:
  stage: test
  extends: .test_ghcup_version:windows
  variables:
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
  needs: []

# test:windows:scoop:
  # stage: test
  # extends: .test_ghcup_scoop:windows
  # needs: []

######## linux release ########

release:linux:64bit:
  stage: release
  needs: ["test:linux"]
  extends:
    - .alpine:64bit
    - .release_ghcup
  before_script:
    - ./.gitlab/before_script/linux/alpine/install_deps.sh
  variables:
    ARTIFACT: "x86_64-linux-ghcup"
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"


release:linux:32bit:
  stage: release
  needs: ["test:linux:32bit"]
  extends:
    - .alpine:32bit
    - .release_ghcup
  before_script:
    - ./.gitlab/before_script/linux/alpine/install_deps.sh
  variables:
    ARTIFACT: "i386-linux-ghcup"
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"

release:linux:armv7:
  stage: release
  needs: ["test:linux:armv7"]
  extends:
    - .linux:armv7
    - .release_ghcup
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh
  variables:
    ARTIFACT: "armv7-linux-ghcup"
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
    CROSS: ""

release:linux:aarch64:
  stage: release
  needs: ["test:linux:aarch64"]
  extends:
    - .linux:aarch64
    - .release_ghcup
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh
  variables:
    ARTIFACT: "aarch64-linux-ghcup"
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
    CROSS: ""

######## darwin release ########

release:darwin:
  stage: release
  needs: ["test:mac"]
  extends:
    - .darwin
    - .release_ghcup
    - .root_cleanup
  before_script:
    - ./.gitlab/before_script/darwin/install_deps.sh
  variables:
    ARTIFACT: "x86_64-apple-darwin-ghcup"
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
    MACOSX_DEPLOYMENT_TARGET: "10.7"

release:darwin:aarch64:
  stage: release
  needs: ["test:mac:aarch64"]
  extends:
    - .darwin:aarch64
    - .release_ghcup
    - .root_cleanup
  cache:
    key: darwin-brew-$CACHE_REV
    paths:
      - brew_cache
    key: ghcup-test-$CACHE_REV
    paths:
      - cabal-cache
  before_script:
    - ./.gitlab/script/ci.sh extract_brew_cache
    - ./.gitlab/script/ci.sh extract_cabal_cache
    # otherwise we seem to get intel binaries
    - export HOMEBREW_CHANGE_ARCH_TO_ARM=1
    # update and install packages
    - /bin/bash ./.gitlab/script/brew.sh llvm autoconf automake coreutils
  script: |
    export PATH="$CI_PROJECT_DIR/.brew/opt/llvm/bin:$CI_PROJECT_DIR/.brew/bin:$CI_PROJECT_DIR/.brew/sbin:$PATH"
    export CC=$CI_PROJECT_DIR/.brew/opt/llvm/bin/clang
    export CXX=$CI_PROJECT_DIR/.brew/opt/llvm/bin/clang++
    export LD=ld
    export AR=$CI_PROJECT_DIR/.brew/opt/llvm/bin/llvm-ar
    export RANLIB=$CI_PROJECT_DIR/.brew/opt/llvm/bin/llvm-ranlib
    ./.gitlab/before_script/darwin/install_deps.sh
    ./.gitlab/script/ghcup_release.sh
  after_script:
    - ./.gitlab/script/ci.sh save_cabal_cache
    - ./.gitlab/script/ci.sh save_brew_cache
  variables:
    ARTIFACT: "aarch64-apple-darwin-ghcup"
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
    MACOSX_DEPLOYMENT_TARGET: "10.7"
  allow_failure: true
  when: manual


######## freebsd release ########

release:freebsd12:
  stage: release
  needs: ["test:freebsd12"]
  extends:
    - .freebsd12
    - .release_ghcup
    - .root_cleanup
  before_script:
    - ./.gitlab/before_script/freebsd/install_deps.sh
  variables:
    ARTIFACT: "x86_64-portbld-freebsd-ghcup"
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
  allow_failure: true

release:freebsd13:
  stage: release
  needs: ["test:freebsd13"]
  extends:
    - .freebsd13
    - .release_ghcup
    - .root_cleanup
  before_script:
    - sudo pkg update
    - sudo pkg install --yes compat12x-amd64
    - sudo ln -s libncurses.so.6 /usr/local/lib/libncurses.so.6.2
    - ./.gitlab/before_script/freebsd/install_deps.sh
  variables:
    ARTIFACT: "x86_64-portbld-freebsd-ghcup"
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"
  allow_failure: true

######## windows release ########

release:windows:
  stage: release
  needs: ["test:windows"]
  extends:
    - .windows
    - .release_ghcup
    - .root_cleanup
  before_script:
    - bash ./.gitlab/before_script/windows/install_deps.sh
  variables:
    ARTIFACT: "x86_64-mingw64-ghcup"
    GHC_VERSION: "8.10.7"
    CABAL_VERSION: "3.6.2.0"

######## hlint ########

hlint:
  stage: checks
  extends:
    - .debian
  script:
    - curl -sSL https://raw.github.com/ndmitchell/hlint/master/misc/run.sh | sh -s -- -r lib/ test/
  allow_failure: true
  artifacts:
    expire_in: 2 week
    paths:
      - report.html
    when: on_failure

######## mkdocs ########

mkdocs:
  stage: checks
  extends:
    - .debian
  before_script:
    - sudo apt-get update -y
    - sudo apt-get install -y python3-pip
    - pip3 install mkdocs
  script:
    - ~/.local/bin/mkdocs build
  allow_failure: true

######## shellcheck ########

shellcheck:
  image: "koalaman/shellcheck-alpine"
  tags:
    - x86_64-linux
  stage: checks
  script:
    - shellcheck scripts/bootstrap/bootstrap-haskell
  allow_failure: true