name: Build

on: [push, pull_request, workflow_dispatch]

jobs:
  regular:
    runs-on: ubuntu-22.04
    if: "!contains(github.event.head_commit.message, '[ci skip]') && !contains(github.event.head_commit.message, '[skip ci]')"
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Requirements
        run: |
          sudo apt update
          sudo apt install -y gcc g++ libtiff-dev libwebp-dev libzstd-dev

      - name: Build and run tests
        run: |
            ./tools/travis-ci/install.sh
            ./tools/travis-ci/run.sh
        env:
            OPJ_CI_ARCH: x86_64
            OPJ_CI_BUILD_CONFIGURATION: Release
            OPJ_CI_PERF_TESTS: 1
            OPJ_CI_BUILD_FUZZERS: 1
            OPJ_CI_INCLUDE_IF_DEPLOY: 1
            OPJ_NONCOMMERCIAL: 1

      - name: Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: build/openjpeg-*.tar.gz

  asan:
    runs-on: ubuntu-22.04
    if: "!contains(github.event.head_commit.message, '[ci skip]') && !contains(github.event.head_commit.message, '[skip ci]')"
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Requirements
        run: |
          sudo apt update
          sudo apt install -y gcc g++ python-is-python3

      - name: Build and run tests
        run: |
            ./tools/travis-ci/install.sh
            ./tools/travis-ci/run.sh
        env:
            OPJ_CI_ARCH: x86_64
            OPJ_CI_BUILD_CONFIGURATION: Debug
            OPJ_CI_ASAN: 1
            OPJ_NONCOMMERCIAL: 1

  clang_32bit:
    runs-on: ubuntu-22.04
    if: "!contains(github.event.head_commit.message, '[ci skip]') && !contains(github.event.head_commit.message, '[skip ci]')"
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Requirements
        run: |
          sudo apt update
          sudo apt install -y clang gcc-multilib

      - name: Build and run tests
        run: |
            ./tools/travis-ci/install.sh
            ./tools/travis-ci/run.sh
        env:
            OPJ_CI_CC: clang
            OPJ_CI_CXX: clang++
            OPJ_CI_INSTRUCTION_SETS: "-mavx2"
            OPJ_CI_BUILD_CONFIGURATION: Release
            OPJ_CI_ARCH: i386
            OPJ_NUM_THREADS: 2
            OPJ_NONCOMMERCIAL: 1

  mingw_64:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, '[ci skip]') && !contains(github.event.head_commit.message, '[skip ci]')"
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Requirements
        run: |
          sudo apt update
          sudo apt install -y gcc-mingw-w64-base binutils-mingw-w64-x86-64 gcc-mingw-w64-x86-64 gcc-mingw-w64 g++-mingw-w64-x86-64

      - name: Build and run tests
        run: |
            ./tools/travis-ci/install.sh
            ./tools/travis-ci/run.sh
        env:
            OPJ_CI_CC: x86_64-w64-mingw32-gcc
            OPJ_CI_CXX: x86_64-w64-mingw32-g++
            OPJ_CI_ARCH: x86_64
            OPJ_CI_BUILD_CONFIGURATION: Release
            OPJ_NONCOMMERCIAL: 1

  macos_x86_64:
    runs-on: macos-13
    if: "!contains(github.event.head_commit.message, '[ci skip]') && !contains(github.event.head_commit.message, '[skip ci]')"
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Build and run tests
        run: |
            ./tools/travis-ci/install.sh
            ./tools/travis-ci/run.sh
        env:
            CMAKE_OSX_ARCHITECTURES: x86_64
            OPJ_CI_ARCH: x86_64
            OPJ_CI_BUILD_CONFIGURATION: Release
            OPJ_CI_INCLUDE_IF_DEPLOY: 1
            #OPJ_NONCOMMERCIAL: 1

      - name: Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: build/openjpeg-*.zip

  macos_arm64:
    runs-on: macos-14
    if: "!contains(github.event.head_commit.message, '[ci skip]') && !contains(github.event.head_commit.message, '[skip ci]')"
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Build and run tests
        run: |
            ./tools/travis-ci/install.sh
            ./tools/travis-ci/run.sh
        env:
            CMAKE_OSX_ARCHITECTURES: arm64
            OPJ_CI_ARCH: arm64
            OPJ_CI_BUILD_CONFIGURATION: Release
            OPJ_CI_INCLUDE_IF_DEPLOY: 1
            #OPJ_NONCOMMERCIAL: 1

      - name: Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: build/openjpeg-*.zip

  windows_build:
    runs-on: windows-2019
    if: "!contains(github.event.head_commit.message, '[ci skip]') && !contains(github.event.head_commit.message, '[skip ci]')"

    strategy:
      matrix:
        include:
          - VS_VER: 2019
            OPJ_CI_ARCH: x64
            OPJ_CI_INCLUDE_IF_DEPLOY: 1
          - VS_VER: 2019
            OPJ_CI_ARCH: x86
            OPJ_CI_INCLUDE_IF_DEPLOY: 1
          - VS_VER: 2019
            OPJ_CI_ARCH: x64
            # We do have specific failures noted in tools/travis-ci/knownfailures-windows-vs2015-x64-avx2-Release-3rdP.txt
            # not sure why that happens
            OPJ_CI_INSTRUCTION_SETS: "/arch:AVX2"

    env:
        VS_VER: ${{ matrix.VS_VER }}
        OPJ_CI_ARCH: ${{ matrix.OPJ_CI_ARCH }}
        OPJ_CI_INCLUDE_IF_DEPLOY: ${{ matrix.OPJ_CI_INCLUDE_IF_DEPLOY }}
        OPJ_CI_INSTRUCTION_SETS: ${{ matrix.OPJ_CI_INSTRUCTION_SETS }}
        #OPJ_NONCOMMERCIAL: 1

    steps:

      - name: Setup git
        run: |
          git config --global core.autocrlf false
          git config --system core.longpaths true

      - name: Checkout
        uses: actions/checkout@v4

      - name: Set compiler environment
        shell: cmd
        run: |
            if "%VS_VER%" == "2019" CALL "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\VsDevCmd.bat" -arch=%OPJ_CI_ARCH%
            echo PATH=%PATH%>> %GITHUB_ENV%
            echo INCLUDE=%INCLUDE%>> %GITHUB_ENV%
            echo LIB=%LIB%>> %GITHUB_ENV%
            echo LIBPATH=%LIBPATH%>> %GITHUB_ENV%

      - name: Install wget
        shell: cmd
        run: |
            choco install wget --no-progress

      - name: Build and run tests
        run: |
            ./tools/travis-ci/install.sh
            ./tools/travis-ci/run.sh
        shell: bash

      - name: Release
        uses: softprops/action-gh-release@v2
        if: ${{startsWith(github.ref, 'refs/tags/') && env.OPJ_CI_INCLUDE_IF_DEPLOY == 1}}
        with:
          files: build/openjpeg-*.zip
