name: cross compile with android-ndk on ubuntu

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

jobs:
  cross-compile:
    strategy:
      fail-fast: false
      matrix:
        build-machine-os: [ubuntu-22.04, ubuntu-20.04]

    runs-on: ${{ matrix.build-machine-os }}

    steps:
      - uses: actions/checkout@v4

      - run: sudo apt-get -y -o APT::Immediate-Configure=false update
      - run: sudo apt-get -y -o APT::Immediate-Configure=false install gcc make automake autoconf file

      - run: ./autogen.sh

      - name: Run ./configure ...
        run: |
          BUILD_MACHINE_OS_TYPE=$(uname | tr A-Z a-z)
          BUILD_MACHINE_OS_ARCH=$(uname -m)

          TOOLCHAIN_BASE_DIR=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/$BUILD_MACHINE_OS_TYPE-$BUILD_MACHINE_OS_ARCH
          TOOLCHAIN_BIN_DIR=$TOOLCHAIN_BASE_DIR/bin
          SYSROOT=$TOOLCHAIN_BASE_DIR/sysroot

          export CC=$TOOLCHAIN_BIN_DIR/armv7a-linux-androideabi24-clang
          export AR=$TOOLCHAIN_BIN_DIR/llvm-ar
          export RANLIB=$TOOLCHAIN_BIN_DIR/llvm-ranlib

          export CFLAGS="--sysroot $SYSROOT -Qunused-arguments -Os -fpic"
          export CPPFLAGS="--sysroot $SYSROOT -Qunused-arguments"
          export LDFLAGS="--sysroot $SYSROOT"

          TARGET=armv7a-linux-androideabi

          COLOR_PURPLE='\033[0;35m'       # Purple
          COLOR_GREEN='\033[0;32m'        # Green
          COLOR_OFF='\033[0m'             # Reset

          echo() {
            printf "%b\n" "$*"
          }

          run() {
            echo "$COLOR_PURPLE==>$COLOR_OFF $COLOR_GREEN$@$COLOR_OFF"
            eval "$*"
          }

          run ./configure \
              --host=$TARGET \
              --disable-iconv \
              --disable-xml \
              --disable-json \
              --disable-yaml \
              --disable-pcre2 \
              CC="$CC" \
              CFLAGS="\"$CFLAGS\"" \
              CPPFLAGS="\"$CPPFLAGS\"" \
              LDFLAGS="\"$LDFLAGS\"" \
              AR=$AR \
              RANLIB=$RANLIB

      - run: make V=1

      # pie executable or shared object
      - run: file ctags | grep 'ELF 32-bit LSB .*, ARM, EABI5'
