# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

name: FBGEMM CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

concurrency:
  # Cancel previous runs in the PR if a new commit is pushed
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  build-linux:
    runs-on: ${{ matrix.host-machine }}
    container:
      image: ${{ matrix.container-image }}
      options: --user root
    defaults:
      run:
        shell: bash
    env:
      PRELUDE: .github/scripts/setup_env.bash
      BUILD_DIR: build_${{ matrix.library-type }}
      DEBIAN_FRONTEND: noninteractive
    strategy:
      fail-fast: false
      matrix:
        host-machine: [
          "linux.12xlarge",     # x86 machine
        ]
        container-image: [ "ubuntu:20.04" ]
        library-type: [ static, shared ]

    steps:
    - name: Setup Build Container
      run: |
        apt update -y
        apt install -y binutils build-essential cmake git libblas-dev pciutils python3 sudo wget
        git config --global --add safe.directory '*'

    - name: Checkout the Repository
      uses: actions/checkout@v4
      with:
        submodules: true

    - name: Display System Info
      run: . $PRELUDE; print_system_info

    - name: Build FBGEMM Library (${{ matrix.library-type }})
      run: |
        set -e
        mkdir $BUILD_DIR; cd $BUILD_DIR
        cmake --version
        cmake -DUSE_SANITIZER=address -DFBGEMM_LIBRARY_TYPE=${{ matrix.library-type }} -DPYTHON_EXECUTABLE=/usr/bin/python3 ..
        make -j VERBOSE=1

    - name: Test FBGEMM Library (${{ matrix.library-type }})
      run: |
        set -e
        cd $BUILD_DIR
        ctest --rerun-failed --output-on-failure


  build-macos:
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        shell: bash
    env:
      PRELUDE: .github/scripts/setup_env.bash
      BUILD_DIR: build_${{ matrix.library-type }}
    strategy:
      fail-fast: false
      matrix:
        os: [ macos-latest ]
        library-type: [ static, shared ]

    steps:
    - name: Checkout the Repository
      uses: actions/checkout@v4
      with:
        submodules: true

    - name: Display System Info
      run: . $PRELUDE; print_system_info

    # Build but skip tests due to lack of support for AVX2
    - name: Build FBGEMM Library (${{ matrix.library-type }})
      run: |
        set -e
        mkdir $BUILD_DIR; cd $BUILD_DIR
        cmake --version
        cmake -DUSE_SANITIZER=address -DFBGEMM_LIBRARY_TYPE=${{ matrix.library-type }} ..
        make -j VERBOSE=1


  build-bazel:
    runs-on: linux.12xlarge
    container:
      image: ${{ matrix.container-image }}
      options: --user root
    defaults:
      run:
        shell: bash
    env:
      PRELUDE: .github/scripts/setup_env.bash
      DEBIAN_FRONTEND: noninteractive
    strategy:
      fail-fast: false
      matrix:
        container-image: [ "ubuntu:20.04" ]

    steps:
    - name: Setup Build Container
      run: |
        apt update -y
        apt install -y binutils build-essential cmake git libblas-dev pciutils python3 sudo unzip wget
        git config --global --add safe.directory '*'

    - name: Checkout the Repository
      uses: actions/checkout@v4
      with:
        submodules: true

    - name: Display System Info
      run: . $PRELUDE; print_system_info

    - name: Download bazel
      run: . $PRELUDE; setup_bazel

    - name: Build FBGEMM Library
      run: bazel build -s :*

    - name: Test FBGEMM Library
      run: bazel test -s :*


  build-windows:
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        shell: cmd
    env:
      BUILD_DIR: build_${{ matrix.library-type }}
    strategy:
      fail-fast: false
      matrix:
        os: [ windows-2019 ]
        library-type: [ static, shared ]

    steps:
    - name: Checkout the Repository
      uses: actions/checkout@v4
      with:
        submodules: true

    - name: Get CPU info on Windows
      shell: cmd
      run: |
        wmic cpu list full

    - name: Build FBGEMM Library (${{ matrix.library-type }})
      shell: cmd
      run: |
        call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
        echo "INSTALL NINJA:"
        pip install ninja
        which ninja
        mkdir %BUILD_DIR%
        cd %BUILD_DIR%
        echo "STARTING CMAKE"
        cmake --version
        cmake -G Ninja -DFBGEMM_BUILD_BENCHMARKS=OFF -DFBGEMM_LIBRARY_TYPE=${{ matrix.library-type }} -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER="cl.exe" -DCMAKE_CXX_COMPILER="cl.exe" ..
        ninja -v all
        echo "Build Success"

    - name: Test FBGEMM Library (${{ matrix.library-type }})
      shell: cmd
      run: |
        echo %cd%
        cd %BUILD_DIR%
        set PATH=%PATH%;%cd%;%cd%\asmjit
        echo %PATH%
        ctest --rerun-failed --output-on-failure
        if errorlevel 1 exit /b 1
