name: Codon CI

on:
  push:
    branches:
      - master
      - develop
    tags:
      - '*'
  pull_request:
    branches:
      - develop

jobs:
  create_release:
    name: Create GitHub Release
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    permissions:
      contents: write
    steps:
      - name: Create Release
        if: contains(github.ref, 'tags/v')
        id: create_release
        uses: ncipollo/release-action@v1

  manylinux:
    strategy:
      matrix:
        arch:
          - x86_64
          # - aarch64
    runs-on: ${{ matrix.arch == 'aarch64' && 'ubuntu-arm-latest' || 'ubuntu-latest' }}
    name: Codon CI (manylinux)
    needs: create_release
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      - name: Cache Dependencies
        id: cache-deps
        uses: actions/cache@v4
        with:
          path: llvm
          key: manylinux-${{ matrix.arch }}-llvm

      - name: Main x86_64
        if: matrix.arch == 'x86_64'
        uses: ./.github/actions/build-manylinux-x86_64

      - name: Main aarch64
        if: matrix.arch == 'aarch64'
        uses: ./.github/actions/build-manylinux-aarch64

      - name: Upload Release Asset
        if: contains(github.ref, 'tags/v')
        uses: actions/upload-release-asset@v1.0.2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_path: ./codon-linux-x86_64.tar.gz
          asset_name: codon-linux-x86_64.tar.gz
          asset_content_type: application/gzip

      - name: Upload Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: manylinux-x86_64
          path: codon-linux-x86_64.tar.gz

  main:
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          # - ubuntu-arm-latest
    runs-on: ${{ matrix.os }}
    name: Codon CI
    needs: create_release
    permissions:
      contents: write
      id-token: write
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.9'

      - name: x86_64 Linux Setup
        if: startsWith(matrix.os, 'ubuntu') && matrix.os != 'ubuntu-arm-latest'
        run: |
          sudo apt update
          sudo apt install -y gfortran libgfortran5 lsb-release wget software-properties-common gnupg
          wget https://apt.llvm.org/llvm.sh
          sudo chmod +x llvm.sh
          sudo ./llvm.sh 17
          echo "LIBEXT=so" >> $GITHUB_ENV
          echo "OS_NAME=linux" >> $GITHUB_ENV
          echo "CODON_SYSTEM_LIBRARIES=/usr/lib/x86_64-linux-gnu" >> $GITHUB_ENV
          echo "CC=clang-17" >> $GITHUB_ENV
          echo "CXX=clang++-17" >> $GITHUB_ENV

      - name: Arm Linux Setup
        if: matrix.os == 'ubuntu-arm-latest'
        run: |
          sudo apt update
          sudo apt install -y gfortran libgfortran5 lsb-release wget software-properties-common gnupg
          wget https://apt.llvm.org/llvm.sh
          sudo chmod +x llvm.sh
          sudo ./llvm.sh 17
          echo "LIBEXT=so" >> $GITHUB_ENV
          echo "OS_NAME=linux" >> $GITHUB_ENV
          echo "CODON_SYSTEM_LIBRARIES=/usr/lib/aarch64-linux-gnu" >> $GITHUB_ENV
          echo "CC=clang-17" >> $GITHUB_ENV
          echo "CXX=clang++-17" >> $GITHUB_ENV

      - name: macOS Setup
        if: startsWith(matrix.os, 'macos')
        run: |
          echo "LIBEXT=dylib" >> $GITHUB_ENV
          echo "OS_NAME=osx" >> $GITHUB_ENV
          echo "CODON_SYSTEM_LIBRARIES=$(brew --prefix gcc)/lib/gcc/current" >> $GITHUB_ENV
          echo "CC=clang" >> $GITHUB_ENV
          echo "CXX=clang++" >> $GITHUB_ENV
          echo "FC=gfortran-12" >> $GITHUB_ENV

      - name: Set up Python
        run: |
          python -m pip install --upgrade pip setuptools wheel
          python -m pip install cython wheel astunparse
          python -m pip install --force-reinstall -v "numpy==1.26.4"
          which python
          which pip
          echo "CODON_PYTHON=$(python test/python/find-python-library.py)" >> $GITHUB_ENV

      - name: Cache Dependencies
        id: cache-deps
        uses: actions/cache@v4
        with:
          path: llvm
          key: ${{ runner.os }}-${{ matrix.os }}-llvm

      - name: Build Dependencies
        if: steps.cache-deps.outputs.cache-hit != 'true'
        run: ./scripts/deps.sh 2

      - name: Build
        run: |
          mkdir build
          export LLVM_DIR=$(llvm/bin/llvm-config --cmakedir)
          (cd build && cmake .. -DCMAKE_BUILD_TYPE=Release \
                                -DCMAKE_C_COMPILER=${CC} \
                                -DCMAKE_CXX_COMPILER=${CXX})
          cmake --build build --config Release -- VERBOSE=1
          cmake --install build --prefix=codon-deploy

      - name: Build Cython
        run: |
          (cd codon-deploy/python && python3 setup.py sdist)
          CODON_DIR=$(pwd)/codon-deploy python -m pip install -v codon-deploy/python/dist/*.gz
          CODON_PATH=$(pwd)/codon-deploy/lib/codon/stdlib python test/python/cython_jit.py

      - name: Test
        run: |
          ln -s build/libcodonrt.${LIBEXT} .
          build/codon_test
          test/app/test.sh build
          (cd test/python && CODON_DIR=$(pwd)/../../codon-deploy python3 setup.py build_ext --inplace && python3 pyext.py)
        env:
          CODON_PATH: ./stdlib
          PYTHONPATH: .:./test/python
          LD_LIBRARY_PATH: ./build

      - name: Artifact Environment
        run: |
          echo "CODON_BUILD_ARCHIVE=codon-$(uname -s | awk '{print tolower($0)}')-$(uname -m).tar.gz" >> $GITHUB_ENV

      - name: Codesign (macOS)
        if: startsWith(matrix.os, 'macos')
        run: |
          codesign -f -s - codon-deploy/bin/codon codon-deploy/lib/codon/*.dylib

      - name: Prepare Artifacts
        run: |
          cp -rf codon-deploy/python/dist .
          rm -rf codon-deploy/lib/libfmt.a codon-deploy/lib/pkgconfig codon-deploy/lib/cmake codon-deploy/python/codon_jit.egg-info codon-deploy/python/build
          tar -czf ${CODON_BUILD_ARCHIVE} codon-deploy
          du -sh codon-deploy

      - name: Upload Release Asset
        if: contains(github.ref, 'tags/v') && startsWith(matrix.os, 'macos')
        uses: actions/upload-release-asset@v1.0.2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_path: ./codon-darwin-arm64.tar.gz
          asset_name: codon-darwin-arm64.tar.gz
          asset_content_type: application/gzip

      - name: Upload Artifacts
        if: startsWith(matrix.os, 'macos')
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.os }}-arm64
          path: codon-darwin-arm64.tar.gz

      - name: Upload Artifacts
        if: startsWith(matrix.os, 'ubuntu') && matrix.os != 'ubuntu-arm-latest'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.os }}-x86_64
          path: codon-linux-x86_64.tar.gz

      - name: Upload Artifacts
        if: matrix.os == 'ubuntu-arm-latest'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.os }}-arm64
          path: codon-linux-arm64.tar.gz

      - name: Publish Package
        if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') && startsWith(matrix.os, 'ubuntu')
        uses: pypa/gh-action-pypi-publish@release/v1