name: CMake

on: [push, pull_request]

jobs:
  CMakeVersionTest:
    name: Test build with CMake 3.16.0
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Download models
        run: ./autogen.sh
      - name: Install CMake 3.16
        run: |
          curl -sL https://github.com/Kitware/CMake/releases/download/v3.16.0/cmake-3.16.0-Linux-x86_64.sh -o cmakeinstall.sh
          chmod +x cmakeinstall.sh
          sudo ./cmakeinstall.sh --prefix=/usr/local --exclude-subdir
          rm cmakeinstall.sh
          sudo apt-get install libidn11
      - name: Create Work Dir
        run: mkdir build
      - name: Configure
        working-directory: ./build
        run: cmake .. -DOPUS_BUILD_PROGRAMS=ON -DBUILD_TESTING=ON
      - name: Build
        working-directory: ./build
        run: make -j 2 -s
      - name: Test
        working-directory: ./build
        run: ctest -j 2

  CMakeMINGW:
    name: CMake MINGW
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Download models
        run: ./autogen.sh
      - name: Install MINGW
        run: sudo apt-get install -y mingw-w64
      - name: Create Work Dir
        run: mkdir build
      - name: Configure
        working-directory: ./build
        run: cmake .. -DOPUS_BUILD_PROGRAMS=ON -DBUILD_TESTING=ON -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc
      - name: Build
        working-directory: ./build
        run: cmake --build . -j 2 --config Release --target package

  CMakeBuild:
    name: CMake/${{ matrix.config.name }}
    runs-on: ${{ matrix.config.os }}
    strategy:
      fail-fast: false
      matrix:
        config:
        - {
            name: "Android/So/ARMv8/Release",
            os: ubuntu-latest,
            config: Release,
            args: "-DCMAKE_TOOLCHAIN_FILE=${ANDROID_HOME}/ndk/25.2.9519653/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DBUILD_SHARED_LIBS=ON"
          }
        # TODO: Android ARMv7
        # - {
        #     name: "Android/So/ARMv7/Release",
        #     os: ubuntu-latest,
        #     config: Release,
        #     args: "-DCMAKE_TOOLCHAIN_FILE=${ANDROID_HOME}/ndk/25.2.9519653/build/cmake/android.toolchain.cmake -DANDROID_ABI=armeabi-v7a -DBUILD_SHARED_LIBS=ON"
        #   }
        - {
            name: "Android/So/X86/Release",
            os: ubuntu-latest,
            config: Release,
            args: "-DCMAKE_TOOLCHAIN_FILE=${ANDROID_HOME}/ndk/25.2.9519653/build/cmake/android.toolchain.cmake -DANDROID_ABI=x86 -DBUILD_SHARED_LIBS=ON"
          }
        - {
            name: "Android/So/X64/Release",
            os: ubuntu-latest,
            config: Release,
            args: "-DCMAKE_TOOLCHAIN_FILE=${ANDROID_HOME}/ndk/25.2.9519653/build/cmake/android.toolchain.cmake -DANDROID_ABI=x86_64 -DBUILD_SHARED_LIBS=ON"
          }
        - {
            name: "Windows/Dll/X86/Release",
            os: windows-latest,
            config: Release,
            args: -G "Visual Studio 17 2022" -A Win32 -DBUILD_SHARED_LIBS=ON
          }
        - {
            name: "Windows/Dll/X64/Release",
            os: windows-latest,
            config: Release,
            args: -G "Visual Studio 17 2022" -A Win32 -DBUILD_SHARED_LIBS=ON
          }
        - {
            name: "Windows/Dll/ARMv8/Release",
            os: windows-latest,
            config: Release,
            args: -G "Visual Studio 17 2022" -A ARM64 -DBUILD_SHARED_LIBS=ON
          }
        - {
            name: "Linux/So/X64/Release",
            os: ubuntu-latest,
            config: Release,
            args: -DBUILD_SHARED_LIBS=ON
          }
        - {
            name: "MacOSX/So/X64/Release",
            os: macos-latest,
            config: Release,
            args: -DBUILD_SHARED_LIBS=ON
          }
        - {
            name: "MacOSX/Framework/X64/Release",
            os: macos-latest,
            config: Release,
            args: -DBUILD_FRAMEWORK=ON -DCMAKE_INSTALL_PREFIX=install
          }
          # use unix makefiles for iOS to avoid Xcode to complain about signing.
        - {
            name: "iOS/Dll/arm64/Release",
            os: macos-latest,
            config: Release,
            args: -G "Unix Makefiles" -DBUILD_SHARED_LIBS=ON -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_ARCHITECTURES=arm64
          }
          # use unix makefiles for iOS to avoid Xcode to complain about signing.
        - {
            name: "iOS/Framework/arm64/Release",
            os: macos-latest,
            config: Release,
            args: -G "Unix Makefiles" -DBUILD_FRAMEWORK=ON -DCMAKE_INSTALL_PREFIX=install -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_ARCHITECTURES=arm64
          }
        - {
            name: "Windows/Lib/X86/Release",
            os: windows-latest,
            config: Release,
            args: -G "Visual Studio 17 2022" -A Win32
          }
        - {
            name: "Windows/Lib/X64/Release",
            os: windows-latest,
            config: Release,
            args: -G "Visual Studio 17 2022" -A Win32
          }
        - {
            name: "Windows/Lib/armv8/Release",
            os: windows-latest,
            config: Release,
            args: -G "Visual Studio 17 2022" -A ARM64
          }
        - {
            name: "Linux/Lib/X64/Release",
            os: ubuntu-latest,
            config: Release,
            args: ""
          }
        - {
            name: "MacOSX/Lib/X64/Release",
            os: macos-latest,
            config: Release,
            args: ""
          }
          # use unix makefiles for iOS to avoid Xcode to complain about signing.
        - {
            name: "iOS/Lib/arm64/Release",
            os: macos-latest,
            config: Release,
            args: -G "Unix Makefiles" -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_ARCHITECTURES=arm64
          }
        - {
            name: "Android/Lib/ARMv8/Release",
            os: ubuntu-latest,
            config: Release,
            args: "-DCMAKE_TOOLCHAIN_FILE=${ANDROID_HOME}/ndk/25.2.9519653/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a"
          }
        # TODO: Android ARMv7
        # - {
        #     name: "Android/Lib/ARMv7/Release",
        #     os: ubuntu-latest,
        #     config: Release,
        #     args: "-DCMAKE_TOOLCHAIN_FILE=${ANDROID_HOME}/ndk/25.2.9519653/build/cmake/android.toolchain.cmake -DANDROID_ABI=armeabi-v7a"
        #   }
        - {
            name: "Android/Lib/X86/Release",
            os: ubuntu-latest,
            config: Release,
            args: "-DCMAKE_TOOLCHAIN_FILE=${ANDROID_HOME}/ndk/25.2.9519653/build/cmake/android.toolchain.cmake -DANDROID_ABI=x86"
          }
        - {
            name: "Android/Lib/X64/Release",
            os: ubuntu-latest,
            config: Release,
            args: "-DCMAKE_TOOLCHAIN_FILE=${ANDROID_HOME}/ndk/25.2.9519653/build/cmake/android.toolchain.cmake -DANDROID_ABI=x86_64"
          }
        - {
            name: "CustomModes/Linux/Lib/X64/Release",
            os: ubuntu-latest,
            config: Release,
            args: "-DOPUS_CUSTOM_MODES=ON"
          }
        - {
            name: "AssertionsFuzz/Windows/Lib/X64/Release",
            os: windows-latest,
            config: Release,
            args: -G "Visual Studio 17 2022" -A Win32 -DOPUS_ASSERTIONS=ON -DOPUS_FUZZING=ON
          }
        - {
            name: "AssertionsFuzz/Linux/Lib/X64/Release",
            os: ubuntu-latest,
            config: Release,
            args: -DOPUS_ASSERTIONS=ON -DOPUS_FUZZING=ON
          }
        - {
            name: "AssertionsFuzz/MacOSX/Lib/X64/Release",
            os: macos-latest,
            config: Release,
            args: -DOPUS_ASSERTIONS=ON -DOPUS_FUZZING=ON
          }

    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Install AutoConf, AutoMake and LibTool # Needed for autogen.sh
        if: matrix.config.os == 'macos-latest'
        run: brew install autoconf automake libtool
      - name: Download models Windows
        if: contains(matrix.config.name, 'Windows')
        run: .\autogen.bat
      - name: Download models
        if: contains(matrix.config.name, 'MacOSX') ||
            contains(matrix.config.name, 'Linux') ||
            contains(matrix.config.name, 'Android') ||
            contains(matrix.config.name, 'iOS')
        run: ./autogen.sh
      - name: Create Work Dir
        run: mkdir build
      - name: Configure
        working-directory: ./build
        run: cmake .. ${{ matrix.config.args }} -DCMAKE_BUILD_TYPE=${{ matrix.config.config }} -DOPUS_BUILD_PROGRAMS=ON -DBUILD_TESTING=ON
      - name: Build
        working-directory: ./build
        run: cmake --build . -j 2 --config ${{ matrix.config.config }} --target package
      - name: Test
        if: contains(matrix.config.name, 'Windows') && !contains(matrix.config.name, 'ARM') && !contains(matrix.config.name, 'Dll') ||
            contains(matrix.config.name, 'MacOSX') && !contains(matrix.config.name, 'ARM') && !contains(matrix.config.name, 'Dll') ||
            contains(matrix.config.name, 'Linux') && !contains(matrix.config.name, 'ARM') && !contains(matrix.config.name, 'Dll')
        working-directory: ./build
        run: ctest -j 2 -C ${{ matrix.config.config }} --output-on-failure
