name: Build and test

on:
  push:
    paths-ignore:
    - 'site/**'
    branches:
    - branch-1.9
  pull_request:
    paths-ignore:
    - 'site/**'
    branches:
    - branch-1.9

# Cancel previous PR build and test
concurrency:
  group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.event.number || github.sha }}
  cancel-in-progress: true

jobs:
  docker:
    name: "Docker ${{ matrix.os }}"
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        os:
          - centos7
          - debian10
          - debian11
          - debian12
          - fedora37
          - oraclelinux9
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: "Test"
      run: |
        cd docker
        ./run-one.sh local branch-1.9 ${{ matrix.os }}

  build:
    name: "Java ${{ matrix.java }} and ${{ matrix.cxx }} on ${{ matrix.os }}"
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-22.04
          - macos-14
        java:
          - 8
          - 11
          - 17
        cxx:
          - clang++
        include:
          - os: ubuntu-22.04
            java: 8
            cxx: g++
          - os: ubuntu-22.04
            java: 21
            cxx: g++
    env:
      MAVEN_OPTS: -Xmx2g
      MAVEN_SKIP_RC: true
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Install Java ${{ matrix.java }}
      uses: actions/setup-java@v4
      with:
        distribution: zulu
        java-version: ${{ matrix.java }}
        cache: 'maven'
    - name: Install CMake
      if: matrix.os == 'macos-14'
      run: |
        # Pin cmake to 3.31.6 due to a backward compatibility issue
        cmake_commit="b4e46db74e74a8c1650b38b1da222284ce1ec5ce"
        tap_name="local/pinned"

        echo "Creating local tap (no git)..."
        brew tap-new --no-git "$tap_name" >/dev/null

        cmake_formula_dir="$(brew --repo "$tap_name")/Formula"
        mkdir -p "$cmake_formula_dir"

        cmake_rb_link="https://raw.githubusercontent.com/Homebrew/homebrew-core/$cmake_commit/Formula/c/cmake.rb"
        cmake_rb_path="$cmake_formula_dir/cmake.rb"

        echo "Downloading cmake.rb from $cmake_rb_link"
        curl -fsSL "$cmake_rb_link" -o "$cmake_rb_path"

        echo "uninstalling existing cmake..."
        brew uninstall cmake

        echo "Installing cmake 3.31.6 from custom tap..."
        brew install "$tap_name/cmake"
    - name: "Test"
      run: |
        mkdir -p ~/.m2
        mkdir build
        cd build
        cmake -DANALYZE_JAVA=ON -DOPENSSL_ROOT_DIR=`brew --prefix openssl@1.1` ..
        make package test-out
    - name: Step on failure
      if: ${{ failure() }}
      run: |
        cat /home/runner/work/orc/orc/build/java/rat.txt

  simdUbuntu:
    name: "SIMD programming using C++ intrinsic functions on ${{ matrix.os }}"
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-22.04
        cxx:
          - clang++
    env:
      ORC_USER_SIMD_LEVEL: AVX512
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: "Test"
      run: |
        mkdir -p ~/.m2
        mkdir build
        cd build
        cmake -DBUILD_JAVA=OFF -DBUILD_ENABLE_AVX512=ON ..
        make package test-out

  doc:
    name: "Javadoc generation"
    runs-on: ubuntu-22.04
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Install Java 8
      uses: actions/setup-java@v4
      with:
        distribution: temurin
        java-version: 8
        cache: 'maven'
    - name: "javadoc"
      run: |
        mkdir -p ~/.m2
        cd java
        ./mvnw install -DskipTests
        ./mvnw javadoc:javadoc

  formatting-check:
    name: "C++ format check"
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        path:
          - 'c++'
          - 'tools'
    steps:
    - uses: actions/checkout@v3
    - name: Run clang-format style check for C++ code
      uses: jidicula/clang-format-action@v4.9.0
      with:
        clang-format-version: '13'
        check-path: ${{ matrix.path }}

  license-check:
    name: "License Check"
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Check license header
        uses: apache/skywalking-eyes@main
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          config: .github/.licenserc.yaml

