name: CI

on:
  push:
    branches: [master, stable, next]
  pull_request:
    branches: [master, stable, next]

  workflow_dispatch:

jobs:
  tcc:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Update packages list
        run: sudo apt-get update

      - name: Install dependencies
        run: |
          sudo apt-get install -y build-essential libtool autoconf automake tcc

      - name: Autogen
        run: ./autogen.sh -s

      - name: Compilation with tcc
        run: |
          env CC=tcc CFLAGS='-w' CPPFLAGS="-DDEV_MODE=1" ./configure --prefix=/tmp --disable-dependency-tracking --disable-shared || cat config.log
          make -j $(nproc) && make check && make install
          env CC=tcc CPPFLAGS='-I/tmp/include' LDFLAGS='-L/tmp/lib' LD_LIBRARY_PATH='/tmp/lib' ./test/constcheck.sh
          make uninstall
          make distclean

  zig:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Update packages list
        run: sudo apt-get update

      - name: Install Zig
        uses: goto-bus-stop/setup-zig@d866436887ad1b24590684f9d00480376663dd36
        with:
          version: 0.11.0

      - name: Autogen
        run: ./autogen.sh -s

      - name: Compilation with zig
        run: |
          zig build
          zig build -Dtarget=x86_64-linux
          zig build -Dtarget=aarch64-linux
          zig build -Dtarget=x86_64-windows
          zig build -Dtarget=aarch64-windows
          zig build -Dtarget=x86_64-macos
          zig build -Dtarget=aarch64-macos
          zig build -Dtarget=wasm32-wasi
          zig build -Doptimize=ReleaseFast
          rm -fr zig-cache zig-out

  regular:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Update packages list
        run: sudo apt-get update

      - name: Install dependencies
        run: sudo apt-get install -y build-essential libtool autoconf automake

      - name: Autogen
        run: ./autogen.sh -s

      - name: C99 compat check
        run: |
          env CPPFLAGS="-DDEV_MODE=1" ./configure --disable-dependency-tracking
          make -j $(nproc) CFLAGS='-g0' > /dev/null && cp src/libsodium/.libs/libsodium.so lib.so && make clean > /dev/null && make CFLAGS='-g0' CPPFLAGS='-DDEV_MODE=1 -DSODIUM_C99\(X\)=' > /dev/null && cp src/libsodium/.libs/libsodium.so lib-oldc.so && cmp lib.so lib-oldc.so && echo No binary changes && make clean > /dev/null
          make distcheck
          make distclean > /dev/null

      - name: Regular compilation
        run: |
          env CPPFLAGS="-DDEV_MODE=1" ./configure --disable-dependency-tracking --enable-minimal
          make -j $(nproc)
          make check
          ( echo '#include <sodium.h>' ; echo 'int main(void) { return sodium_init(); }' ) > /tmp/main.c && gcc -DDEV_MODE=1 -Isrc/libsodium/include -Isrc/libsodium/include/sodium $(find src -name '*.c' -o -name '*.S') /tmp/main.c
          make distclean > /dev/null

  check-globals:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Update packages list
        run: sudo apt-get update

      - name: Install dependencies
        run: sudo apt-get install -y build-essential libtool autoconf automake

      - name: Autogen
        run: ./autogen.sh -s

      - name: Check globals
        run: |
          if [ -x test/rename-globals.sh ]; then test/rename-globals.sh; fi

  other-comp:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Update packages list
        run: sudo apt-get update

      - name: Install dependencies
        run: sudo apt-get install -y build-essential libtool autoconf automake clang

      - name: Autogen
        run: ./autogen.sh -s

      - name: Compilation with g++
        run: |
          env CC=g++ CPPFLAGS="-DDEV_MODE=1" ./configure --disable-dependency-tracking
          make -j $(nproc) check
          make clean > /dev/null

      - name: Compilation with clang
        run: |
          env CC=clang CPPFLAGS="-DDEV_MODE=1" ./configure --disable-dependency-tracking
          make -j $(nproc) check
          make clean > /dev/null

  other-arch:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Update packages list
        run: sudo apt-get update

      - name: Install dependencies
        run: sudo apt-get install -y build-essential libtool autoconf automake gcc-powerpc-linux-gnu

      - name: Autogen
        run: ./autogen.sh -s

      - name: Big-Endian PowerPC compilation
        run: |
          env CPPFLAGS="-DDEV_MODE=1" ./configure --disable-dependency-tracking --host=powerpc-linux-gnu
          make -j $(nproc)
          make clean > /dev/null

  android:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Update packages list
        run: sudo apt-get update

      - name: Install base dependencies
        run: sudo apt-get install -y libtool autoconf automake unzip

      - name: Autogen
        run: ./autogen.sh -s

      - name: Install Android NDK
        run: |
          mkdir /tmp/android && cd /tmp/android
          curl -o ndk.zip -L https://dl.google.com/android/repository/android-ndk-r25c-linux.zip
          unzip ndk.zip && rm -f *.zip && mv android-ndk* ndk

      - name: Android compilation
        run: |
          env ANDROID_NDK_HOME=/tmp/android/ndk ./dist-build/android-aar.sh