name: Build and test

on:
  push:
    branches: [tengine-lite]
    paths:
    paths-ignore: 
    - 'doc/**'
    - 'README.md'
    - 'README_EN.md'
  pull_request:
    branches: [tengine-lite]
    paths-ignore: 
    - 'doc/**'
    - 'README.md'
    - 'README_EN.md'

jobs:
  android-armv7-cpu:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: configure
      run: mkdir build && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_LATEST_HOME/build/cmake/android.toolchain.cmake -DANDROID_ABI="armeabi-v7a" -DANDROID_PLATFORM=android-19 ..
    - name: build
      run: cmake --build build -j 2


  android-armv8-cpu:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: configure
      run: mkdir build && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_LATEST_HOME/build/cmake/android.toolchain.cmake -DANDROID_ABI="arm64-v8a" -DANDROID_PLATFORM=android-21 ..
    - name: build
      run: cmake --build build -j 2


  linux-armv7-cpu-gcc:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v3
    - name: cache-qemu
      id: cache-qemu
      uses: actions/cache@v3
      with:
        path: qemu-install
        key: qemu-arm-install-1
    - name: checkout-qemu
      if: steps.cache-qemu.outputs.cache-hit != 'true'
      uses: actions/checkout@v3
      with:
        repository: qemu/qemu
        path: qemu
        ref: 8746309137ba470d1b2e8f5ce86ac228625db940
    - name: qemu
      if: steps.cache-qemu.outputs.cache-hit != 'true'
      run: |
        cd qemu
        ./configure --prefix=install --target-list=arm-linux-user --disable-system
        make -j2
        make install
        cp -r arm-linux-user/install $GITHUB_WORKSPACE/qemu-install
    - name: arm-gnu-toolchain
      run: |
        sudo apt-get update
        sudo apt-get install g++-arm-linux-gnueabi
    - name: configure
      run: mkdir build && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/arm-linux-gnueabi.toolchain.cmake -DTENGINE_BUILD_TESTS=ON ..
    - name: build
      run: cmake --build build -j 2


  linux-armv8-cpu-gcc:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v3
    - name: cache-qemu
      id: cache-qemu
      uses: actions/cache@v3
      with:
        path: qemu-install
        key: qemu-aarch64-install-1
    - name: checkout-qemu
      if: steps.cache-qemu.outputs.cache-hit != 'true'
      uses: actions/checkout@v3
      with:
        repository: qemu/qemu
        path: qemu
        ref: 8746309137ba470d1b2e8f5ce86ac228625db940
    - name: qemu
      if: steps.cache-qemu.outputs.cache-hit != 'true'
      run: |
        cd qemu
        ./configure --prefix=install --target-list=aarch64-linux-user --disable-system
        make -j2
        make install
        cp -r aarch64-linux-user/install $GITHUB_WORKSPACE/qemu-install
    - name: aarch64-gnu-toolchain
      run: |
        sudo apt-get update
        sudo apt-get install g++-aarch64-linux-gnu
    - name: configure
      run: mkdir build && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/aarch64-linux-gnu.toolchain.cmake -DTENGINE_BUILD_TESTS=ON ..
    - name: build
      run: cmake --build build -j 2
        
  linux-gcc-mipsisa64r6el:
    runs-on: ubuntu-20.04
    steps:
    - name: cancel-previous-runs
      uses: styfle/cancel-workflow-action@0.11.0
      with:
        access_token: ${{ secrets.GITHUB_TOKEN }}
    - uses: actions/checkout@v3
    - name: cache-qemu
      id: cache-qemu
      uses: actions/cache@v3
      with:
        path: qemu-install
        key: qemu-mips64el-install-1
    - name: checkout-qemu
      if: steps.cache-qemu.outputs.cache-hit != 'true'
      uses: actions/checkout@v3
      with:
        repository: qemu/qemu
        path: qemu
        ref: 8746309137ba470d1b2e8f5ce86ac228625db940
    - name: qemu
      if: steps.cache-qemu.outputs.cache-hit != 'true'
      run: |
        cd qemu
        ./configure --prefix=install --target-list=mips64el-linux-user --disable-system
        make -j2
        make install
        cp -r mips64el-linux-user/install $GITHUB_WORKSPACE/qemu-install
    - name: mipsisa64r6el-gnuabi64-toolchain
      run: |
        sudo apt-get update
        sudo apt-get install g++-mipsisa64r6el-linux-gnuabi64
    - name: configure
      run: mkdir build && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/mipsisa64r6el-linux-gnuabi64.toolchain.cmake -DTENGINE_BUILD_EXAMPLES=OFF ..
    - name: build
      run: cmake --build build -j 2


  windows-x64-cpu-vs2017:
    runs-on: windows-2016
    steps:
    - uses: actions/checkout@v3
    - name: build
      run: |
        mkdir build; cd build
        cmake -T v141,host=x64 -A x64 -DTENGINE_OPENMP=OFF -DTENGINE_BUILD_EXAMPLES=OFF ..
        cmake --build . --config Release -j 2


  windows-x64-cpu-vs2019:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v3
    - name: build
      run: |
        mkdir build; cd build
        cmake -A x64 -DTENGINE_BUILD_EXAMPLES=OFF ..
        cmake --build . --config Release -j 2


  windows-x86-cpu-vs2017:
    runs-on: windows-2016
    steps:
    - uses: actions/checkout@v3
    - name: build
      run: |
        mkdir build; cd build
        cmake -T v141,host=x64 -A Win32 -DTENGINE_OPENMP=OFF -DTENGINE_BUILD_EXAMPLES=OFF ..
        cmake --build . --config Release -j 2


  windows-x86-cpu-vs2019:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v3
    - name: build
      run: |
        mkdir build; cd build
        cmake -T v142,host=x64 -A Win32 -DTENGINE_BUILD_EXAMPLES=OFF ..
        cmake --build . --config Release -j 2


  linux-x64-timvx-gcc:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: update
      run: sudo apt-get update
    - name: libopencv
      run: sudo apt-get install libopencv-dev
    - name: tim-vx
      run: |
        git clone https://github.com/VeriSilicon/TIM-VX.git
        export LD_LIBRARY_PATH=${PWD}/3rdparty/tim-vx/lib/x86_64
        mkdir -p ./3rdparty/tim-vx/include
        mkdir -p ./3rdparty/tim-vx/lib/x86_64
        cp -rvf ./TIM-VX/prebuilt-sdk/x86_64_linux/include/*  ./3rdparty/tim-vx/include/
        cp -rvf ./TIM-VX/prebuilt-sdk/x86_64_linux/lib/*      ./3rdparty/tim-vx/lib/x86_64/
        cp -rvf ./TIM-VX/include  ./source/device/tim-vx/
        cp -rvf ./TIM-VX/src      ./source/device/tim-vx/
        rm ./source/device/tim-vx/src/tim/vx/*_test.cc
        rm ./source/device/tim-vx/src/tim/vx/ops/*_test.cc
    - name: configure
      run: mkdir build && cd build && cmake -DTENGINE_ENABLE_TIM_VX=ON ..
    - name: build
      run: cmake --build build -j 2


  linux-aarch64-timvx-gcc:
    runs-on: vim3
    steps:
    - uses: actions/checkout@v3
    - name: tim-vx
      run: |
        git clone https://github.com/VeriSilicon/TIM-VX.git
        cp -rvf ./TIM-VX/include  ./source/device/tim-vx/
        cp -rvf ./TIM-VX/src      ./source/device/tim-vx/
        rm ./source/device/tim-vx/src/tim/vx/*_test.cc
        rm ./source/device/tim-vx/src/tim/vx/ops/*_test.cc
    - name: configure
      run: mkdir build && cd build && cmake -DTENGINE_BUILD_TESTS=ON -DTENGINE_ENABLE_TIM_VX=ON ..
    - name: build
      run: cmake --build build -j 4


  linux-x64-cpu-gcc:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: update
      run: sudo apt-get update
    - name: depends
      run: sudo apt-get install libprotobuf-dev protobuf-compiler libopencv-dev
    - name: configure
      run: mkdir build && cd build && cmake ..
    - name: build
      run: cmake --build build -j 2


  linux-x64-cpu-clang:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: update
      run: sudo apt-get update
    - name: depends
      run: sudo apt-get install libprotobuf-dev protobuf-compiler libopencv-dev
    - name: configure
      env:
        CC: clang
        CXX: clang++    
      run: mkdir build && cd build && cmake -DTENGINE_BUILD_TESTS=ON -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX  ..
    - name: build
      run: cmake --build build -j 2
  
        
  linux-x64-cpu-tools-convert:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: update
      run: |
        sudo apt-get update
        sudo apt-get install libprotobuf-dev protobuf-compiler
    - name: configure
      run: mkdir build && cd build && cmake -DTENGINE_BUILD_CONVERT_TOOL=ON ..
    - name: build
      run: cmake --build build -j 2

  linux-x64-cpu-tools-quant:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: update
      run: |
        sudo apt-get update
        sudo apt-get install libprotobuf-dev protobuf-compiler libopencv-dev
    - name: configure
      run: mkdir build && cd build && cmake -DTENGINE_BUILD_QUANT_TOOL=ON ..
    - name: build
      run: cmake --build build -j2     
  
