# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions

name: Vanilla LLVM
on:
  - pull_request
  - push

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        include:
          - job_name: Ubuntu 18.04, LLVM 14, latest LDC beta
            os: ubuntu-18.04
            host_dc: ldc-beta
            llvm_version: 14.0.0
            # the compiler-rt libs installation is somehow non-standard
            cmake_flags: -DLDC_INSTALL_LLVM_RUNTIME_LIBS_OS=x86_64-unknown-linux-gnu -DLDC_INSTALL_LLVM_RUNTIME_LIBS_ARCH=""
          - job_name: Ubuntu 18.04, LLVM 12, latest LDC beta
            os: ubuntu-18.04
            host_dc: ldc-beta
            llvm_version: 12.0.1
            cmake_flags: -DLIB_SUFFIX=64
          - job_name: Ubuntu 18.04, LLVM 11, latest LDC beta
            os: ubuntu-18.04
            host_dc: ldc-beta
            llvm_version: 11.1.0
            cmake_flags: -DBUILD_SHARED_LIBS=ON -DRT_SUPPORT_SANITIZERS=ON
          - job_name: Ubuntu 18.04, LLVM 9, latest DMD beta
            os: ubuntu-18.04
            host_dc: dmd-beta
            llvm_version: 9.0.1
            cmake_flags: -DBUILD_SHARED_LIBS=OFF -DRT_SUPPORT_SANITIZERS=ON -DLDC_LINK_MANUALLY=ON
          - job_name: macOS 10.15, LLVM 10, latest DMD beta
            os: macos-10.15
            host_dc: dmd-beta
            llvm_version: 10.0.1
            cmake_flags: -DBUILD_SHARED_LIBS=ON -DRT_SUPPORT_SANITIZERS=ON -DLDC_LINK_MANUALLY=ON
          - job_name: macOS 10.15, LLVM 13, latest LDC beta
            os: macos-10.15
            host_dc: ldc-beta
            llvm_version: 13.0.1
            cmake_flags: -DBUILD_SHARED_LIBS=OFF
    name: ${{ matrix.job_name }}
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true
          fetch-depth: 50
      - name: Install ninja
        uses: seanmiddleditch/gha-setup-ninja@v3
      - name: Install D host compiler
        uses: dlang-community/setup-dlang@v1
        with:
          compiler: ${{ matrix.host_dc }}
      - name: Clear LD_LIBRARY_PATH to prevent loading host compiler libs
        run: echo "LD_LIBRARY_PATH=" >> $GITHUB_ENV
      - name: Install lit
        run: |
          set -euxo pipefail
          python3 -m pip install --user setuptools wheel
          python3 -m pip install --user lit
          python3 -c "import lit.main; lit.main.main();" --version . | head -n 1
      - name: 'Linux: Install gdb'
        if: runner.os == 'Linux'
        run: |
          set -eux
          sudo apt-get update
          # Don't use latest gdb v10 from Ubuntu toolchain PPA with regressions, use official v8
          sudo apt-get install gdb=8.1.1-0ubuntu1

      - name: Try to restore cached LLVM
        uses: actions/cache@v2
        with:
          path: llvm
          key: llvm-${{ matrix.llvm_version }}-${{ runner.os }}

      - name: Download & extract prebuilt vanilla LLVM ${{ matrix.llvm_version }}
        run: |
          set -eux
          if [[ -d llvm ]]; then
            echo "Already cached"
            exit 0
          fi
          version='${{ matrix.llvm_version }}'
          if [[ '${{ runner.os }}' == macOS ]]; then
            suffix='x86_64-apple-darwin'
          elif [[ "$version" =~ ^1[3-9]\. ]]; then
            suffix='x86_64-linux-gnu-ubuntu-18.04' # LLVM 13.0.1+
          else
            suffix='x86_64-linux-gnu-ubuntu-16.04'
          fi
          curl -fL --retry 3 --max-time 300 -o llvm.tar.xz \
            https://github.com/llvm/llvm-project/releases/download/llvmorg-$version/clang+llvm-$version-$suffix.tar.xz
          mkdir llvm
          tar -xf llvm.tar.xz --strip 1 -C llvm
          rm llvm.tar.xz

      - name: 'Linux: Make ld.gold or lld the default linker'
        if: runner.os == 'Linux'
        run: |
          set -eux
          if [[ '${{ matrix.llvm_version }}' =~ ^1[3-9]\. ]]; then
            echo "Using lld for LLVM 13+ to work around sporadic failures"
            sudo ln -sf "$PWD/llvm/bin/ld.lld" /usr/bin/ld
          else
            sudo ln -sf /usr/bin/ld.gold /usr/bin/ld
          fi
          ld --version

      - name: Build LDC & LDC D unittests & defaultlib unittest runners with extra '${{ matrix.cmake_flags }}'
        run: |
          set -eux
          cmake -G Ninja . \
            -DCMAKE_BUILD_TYPE=Release \
            -DLLVM_ROOT_DIR="$PWD/llvm" \
            -DLDC_LINK_MANUALLY=OFF \
            ${{ matrix.cmake_flags }}
          ninja all ldc2-unittest all-test-runners
          bin/ldc2 --version

      - name: Run LDC D unittests
        if: success() || failure()
        run: ctest --output-on-failure -R "ldc2-unittest"
      - name: Run LIT testsuite
        if: success() || failure()
        run: |
          set -eux
          # LLVM 9: libclang_rt.fuzzer-x86_64.a not compiled with -fPIC
          if [[ '${{ matrix.llvm_version }}' = 9.* ]]; then
            echo "config.available_features.remove('Fuzzer')" >> tests/sanitizers/lit.local.cfg
          fi
          ctest -V -R "lit-tests"
      - name: Run DMD testsuite
        if: success() || failure()
        run: ctest -V -R "dmd-testsuite"
      - name: Run defaultlib unittests & druntime integration tests
        if: success() || failure()
        run: |
          set -eux
          excludes='dmd-testsuite|lit-tests|ldc2-unittest'
          if [[ '${{ runner.os }}' == macOS ]]; then
            N=$(sysctl -n hw.logicalcpu)
            # https://github.com/ldc-developers/ldc/issues/3280
            excludes+='|druntime-test-exceptions-debug'
          else
            N=$(nproc)
          fi
          ctest -j$N --output-on-failure -E "$excludes"
