name: CI

on:
  workflow_dispatch:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:
    runs-on: ${{ matrix.os }}-${{ matrix.osver }}

    name: "${{ matrix.os }} ${{ matrix.arch }}: ${{ matrix.compiler }} + ${{ matrix.config }}"
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu, macos]
        arch: [amd64, i386, aarch64, arm, riscv64]
        config: [debug, release, release-dyn, asan, lsan, msan, tsan, ubsan, valgrind]
        compiler: [gcc, clang]

        include:
          - os: ubuntu
            osver: 22.04
            sys: linux-gnu
          - os: macos
            osver: 14
            sys: apple-darwin23
          - os: macos
            compiler: clang
            sys: apple-darwin23.6.0

          - compiler: clang
            compiler_flags: "-fdebug-default-version=4"

          - arch: amd64
            ccarch: x86_64
            archcflags: ""
            cross: no
          - arch: i386
            ccarch: x86_64
            archcflags: "-m32 -msse2 -mfpmath=sse"
            cross: no
          - arch: aarch64
            ccarch: aarch64
            archcflags: "-Wl,--dynamic-linker=/usr/aarch64-linux-gnu/lib/ld-linux-aarch64.so.1"
            runenv: 'LD_LIBRARY_PATH=/usr/aarch64-linux-gnu/lib:$LD_LIBRARY_PATH'
            cross: yes
          - arch: arm
            ccarch: arm
            sys: linux-gnueabi
            archcflags: "-D_FILE_OFFSET_BITS=64 -latomic -Wl,--dynamic-linker=/usr/arm-linux-gnueabi/lib/ld-linux.so.3"
            runenv: 'LD_LIBRARY_PATH=/usr/arm-linux-gnueabi/lib:$LD_LIBRARY_PATH'
            cross: yes
          - arch: riscv64
            ccarch: riscv64
            archcflags: "-Wl,--dynamic-linker=/usr/riscv64-linux-gnu/lib/ld-linux-riscv64-lp64d.so.1"
            runenv: 'LD_LIBRARY_PATH=/usr/riscv64-linux-gnu/lib:$LD_LIBRARY_PATH'
            cross: yes

          - config: "asan"
            config_cflags: "-fsanitize=address"
            config_env: "ASAN_OPTIONS=detect_leaks=0"

          - config: "lsan"
            config_cflags: "-fsanitize=leak"
            config_env: "LSAN_OPTIONS=fast_unwind_on_malloc=0"

          - config: "msan"
            configenv: "CC=clang CXX=clang++"
            config_cflags: "-fsanitize=memory -fsanitize-memory-track-origins=2"
            settings: "-DUSE_LIBPCAUDIO:BOOL=OFF -DUSE_LIBSONIC:BOOL=OFF"
            config_env: "MSAN_OPTIONS=exitcode=42"

          - config: "tsan"
            config_cflags: "-fsanitize=thread"

          - config: "ubsan"
            config_cflags: "-fsanitize=undefined"
            config_env: "UBSAN_OPTIONS=halt_on_error=1"

          - config: "valgrind"
            config_env: 'VALGRIND="valgrind --track-origins=yes --leak-check=full --error-exitcode=1"'

          - config: "release"
            settings: "-DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo"

          - config: "release-dyn"
            settings: "-DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DBUILD_SHARED_LIBS:BOOL=ON"
          
        exclude:
          - os: macos
            arch: i386
          - os: macos
            arch: aarch64
          - os: macos
            arch: arm
          - os: macos
            arch: riscv64

          # fixme
          - os: macos
            compiler: gcc

          - os: macos
            config: valgrind # Not supported on macOS

          - compiler: gcc
            config: msan # Not supported by GCC

          - os: macos
            compiler: clang
            config: lsan # Not supported by Apple Clang
          - os: macos
            compiler: clang
            config: msan # Not supported by Apple Clang
          - os: macos
            compiler: gcc
            config: tsan # Failing on CI, should be supported?

          # Valgrind is unavailable for cross-builds
          - arch: aarch64
            config: valgrind
          - arch: arm
            config: valgrind
          - arch: riscv64
            config: valgrind

          # Leak sanitizer is unavailable for cross-builds
          - arch: i386
            config: lsan
          - arch: aarch64
            config: lsan
          - arch: arm
            config: lsan
          - arch: riscv64
            config: lsan
          
          # Thread sanitizer is partially unavailable
          - arch: i386
            config: tsan
          - arch: arm
            config: tsan
          - arch: riscv64
            config: tsan
          - arch: aarch64
            compiler: clang
            config: tsan

          # Undefined sanitizer is partially unavailable
          - arch: riscv64
            config: ubsan
          - arch: arm
            compiler: clang
            config: ubsan
          - arch: aarch64
            compiler: clang
            config: ubsan

          # Memory sanitizer is partially unavailable
          - arch: i386
            config: msan
          - arch: riscv64
            config: msan
          - arch: arm
            config: msan
          - arch: aarch64
            config: msan

          # Address sanitizer is partially unavailable
          - arch: riscv64
            config: asan
          - arch: arm
            compiler: clang
            config: asan
          - arch: aarch64
            compiler: clang
            config: asan

          # Disable Thread sanitizer that runs for over 2h on aarch64
          - arch: aarch64
            config: tsan

    steps:

    # Linux - dependencies
    - name: apt-build-deps
      if: matrix.os == 'ubuntu'
      run: |
        [ 'x${{ matrix.cross }}' == 'xno' ] && sudo dpkg --add-architecture ${{ matrix.arch }}
        sudo apt-get update
        sudo apt-get install ronn kramdown python3
    - name: apt-arch-deps
      if: matrix.os == 'ubuntu' && matrix.cross == 'no'
      run: "sudo apt-get install cmake valgrind g++-12-multilib linux-libc-dev:${{ matrix.arch }} libpcaudio-dev:${{ matrix.arch }} libsonic-dev:${{ matrix.arch }} libc6-dbg:${{ matrix.arch }} libgcc-s1:${{ matrix.arch }}"
    - name: apt-cross-deps
      if: matrix.cross == 'yes'
      run: sudo apt-get install qemu-user g++-12-${{ matrix.arch }}-${{ matrix.sys }}
    - name: apt-compile-clang
      if: matrix.os == 'ubuntu' && matrix.compiler == 'clang'
      run: sudo apt-get install clang

    # MacOS - dependencies
    - name: brew-deps
      if: matrix.os == 'macos'
      run: brew install cmake ronn OJFord/homebrew-formulae/kramdown
    - name: brew-compile-deps
      if: matrix.os == 'macos' && matrix.compiler == 'gcc'
      run: brew install gcc@12

    # Checkout code
    - uses: actions/checkout@v3

    # Configure
    - name: configure
      run: |
        [ 'x${{ matrix.compiler }}' = 'xgcc' ] && export CC="${{ matrix.ccarch }}-${{ matrix.sys }}-gcc-12"
        [ 'x${{ matrix.compiler }}' = 'xgcc' ] && export CXX="${{ matrix.ccarch }}-${{ matrix.sys }}-g++-12"
        [ 'x${{ matrix.compiler }}' = 'xclang' ] && export CC="clang --target=${{ matrix.ccarch }}-${{ matrix.sys }}"
        [ 'x${{ matrix.compiler }}' = 'xclang' ] && export CXX="clang++ --target=${{ matrix.ccarch }}-${{ matrix.sys }}"
        [ 'x${{ matrix.cross }}' = 'xyes' ] && export LD="${{ matrix.ccarch }}-${{ matrix.sys }}-ld"
        export CFLAGS="-g -Og -fno-omit-frame-pointer ${{ matrix.compiler_flags }} ${{ matrix.config_cflags }} ${{ matrix.archcflags }}"
        export CXXFLAGS="-g -Og -fno-omit-frame-pointer ${{ matrix.compiler_flags }} ${{ matrix.config_cflags }} ${{ matrix.archcflags }}"
        cmake -Bbuild ${{ matrix.settings }} -DCMAKE_SYSTEM_PROCESSOR=${{ matrix.arch }} -DUSE_ASYNC:BOOL=OFF

    - name: config-failed-upload
      if: ${{ failure() }}
      uses: actions/upload-artifact@v3
      with:
        name: cmake-${{ matrix.os }}-${{ matrix.arch }}-${{ matrix.compiler }}-${{ matrix.config }}-builddir
        path: build

    # Build and test
    - name: make-binary
      run: cmake --build build --target espeak-ng
    - name: make-data
      run: ${{ matrix.config_env }} ${{ matrix.runenv }} cmake --build build --target data
    - name: test
      run: |
        ${{ matrix.config_env }} ${{ matrix.runenv }} cmake --build build --target tests
        ${{ matrix.config_env }} ${{ matrix.runenv }} ctest --test-dir build -T test -j8 --output-on-failure