name: ci

env:
  R2V: 6.2.0

on:
  push:
    branches:
      - master
    tags:
      - '[0-9]*.*.*'
  pull_request:

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  r2git:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
    steps:
    - name: Checkout
      uses: actions/checkout@v7
    - name: Installing radare2 from git
      run: ( git clone --depth=1 https://github.com/radareorg/radare2 && cd radare2 && sys/install.sh /usr )
    - name: Building r2ghidra with acr
      run: ./preconfigure && ./configure --prefix=/usr && make -j4 ; make user-install
    - name: Testing r2ghidra oneliner on home setup
      run: R2_DEBUG=1 r2 -qc 'af;pdg' /bin/ls | tee a.txt && grep entry0 a.txt
    - name: System-wide installation
      run: make user-uninstall && sudo make install
    - name: Testing r2ghidra oneliner on system wide install
      run: R2_DEBUG=1 r2 -qc 'af;pdg' /bin/ls | tee a.txt && grep entry0 a.txt
    - name: Packaging r2ghidra for Debian
      run: make -C dist/debian
#    - name: Running Testsuite
#      run: make -C test

  linux-deb:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
    steps:
    - name: Checkout
      uses: actions/checkout@v7
    - name: Installing radare2 from deb
      run: |
        wget https://github.com/radareorg/radare2/releases/download/${{env.R2V}}/radare2_${{env.R2V}}_amd64.deb
        wget https://github.com/radareorg/radare2/releases/download/${{env.R2V}}/radare2-dev_${{env.R2V}}_amd64.deb
        sudo dpkg -i *.deb
    - name: Building r2ghidra with acr
      run: ./preconfigure && ./configure --prefix=/usr && make -j4 ; make user-install
    - name: Testing r2ghidra oneliner on home setup
      run: R2_DEBUG=1 r2 -qc 'af;pdg' /bin/ls | tee a.txt && grep entry0 a.txt
    - name: System-wide installation
      run: make user-uninstall && sudo make install
    - name: Testing r2ghidra oneliner on system wide install
      run: R2_DEBUG=1 r2 -qc 'af;pdg' /bin/ls | tee a.txt && grep entry0 a.txt
    - name: Running testsuite
      timeout-minutes: 30
      run: make -C test
    - name: Packaging r2ghidra for Debian using Dockcross
      run: make -C dist/debian
    - name: Uploading debs
      uses: actions/upload-artifact@v7
      with:
        # path: radare2/dist/debian/*/*.deb r2ghidra/dist/debian/*.deb
        name: linux-deb
        path: dist/debian/*.deb
    - name: Precompiling the sleigh
      run: make dist
    - name: Uploading sleigh
      uses: actions/upload-artifact@v7
      with:
        name: linux-acr-ok
        path: r2ghidra_sleigh-*.zip

  tarball:
    runs-on: ubuntu-22.04
    steps:
    - name: Checkout
      uses: actions/checkout@v7
    - name: Large >99KB files in test/bins are not allowed
      run: \[ "$(find test/bins -type f -size +99123c | wc -l)" -eq 0 \]
    - name: Installing radare2 from deb
      run: |
        wget https://github.com/radareorg/radare2/releases/download/${{env.R2V}}/radare2_${{env.R2V}}_amd64.deb
        wget https://github.com/radareorg/radare2/releases/download/${{env.R2V}}/radare2-dev_${{env.R2V}}_amd64.deb
        sudo dpkg -i *.deb
        rm *.deb
    - name: Building tars
      run: make -C dist/tarball
    - name: Uploading balls
      uses: actions/upload-artifact@v7
      with:
        name: tarball
        path: dist/tarball/r2ghidra-*

  linux-asan:
    if: ${{ false }}  # disable for now
    # The type of runner that the job will run on
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    - name: Checkout
      uses: actions/checkout@v7
    - name: Installing radare2
    #  run: ( git clone --depth=1 --branch "${R2V}" https://github.com/radareorg/radare2 && cd radare2 && sys/sanitize.sh --install /usr )
      run: ( git clone --depth=1 --branch "${R2V}" https://github.com/radareorg/radare2 && cd radare2 && sys/install.sh --install )
    - name: Building r2ghidra with acr
      run: ./preconfigure && ./configure && make asan && make -C src sleigh-build && make user-install
    - name: Testing r2ghidra oneliner
      run: |
        r2 -qc 'af;pdg' /bin/ls | tee a.txt
        grep entry0 a.txt

  linux-meson:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
    steps:
    - name: Checkout
      uses: actions/checkout@v7
    - name: Downloading Radare2 Debian packages
      run: |
        wget -O r2.deb     https://github.com/radareorg/radare2/releases/download/${{env.R2V}}/radare2_${{env.R2V}}_amd64.deb
        wget -O r2-dev.deb https://github.com/radareorg/radare2/releases/download/${{env.R2V}}/radare2-dev_${{env.R2V}}_amd64.deb
    - name: Installing Radare2
      run: sudo dpkg -i r2.deb r2-dev.deb
    - name: Installing User's Meson Ninja
      run: pip install ninja meson && ninja --version && meson --version
    - name: Installing System's Meson Ninja
      run: sudo pip install ninja meson && sudo ninja --version && sudo meson --version
    - name: Building r2ghidra with meson
      run: ./preconfigure && meson b && ninja -C b && sudo ninja -C b install
    - name: Cannot test meson builds, because those cant be installed yet
      run: echo nada
#    - name: Testing r2ghidra oneliner
#      run: |
#        r2 -H
#        r2 -L
#        R2_DEBUG=1 r2 -qc 'af;pdg' /bin/ls | tee a.txt
#        grep entry0 a.txt

  macos-pkg:
    # The type of runner that the job will run on
    runs-on: macos-latest
    # Build for both architectures
    strategy:
      matrix:
        arch: [x86_64, arm64]
    env:
      ARCH: ${{ matrix.arch }}

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    - name: Checkout
      uses: actions/checkout@v7
    - name: Cloning radare2
      run: git clone --depth=1 --branch "${R2V}" https://github.com/radareorg/radare2
    - name: Building Radare2 pkg for macOS
      run: |
        echo "Building radare2 for architecture: $ARCH"
        make -C radare2/dist/macos ARCH=$ARCH
    - name: Install radare2 Package
      run: sudo installer -pkg radare2/dist/macos/*.pkg -target /
    - name: Building r2ghidra with acr
      run: |
        ./preconfigure
        ./configure
        make -j4
        make user-install
    - name: Packaging r2ghidra
      run: |
        make -C dist/macos ARCH=$ARCH
        make install DESTDIR=/tmp/r2ghidra
        cd /tmp/r2ghidra
        tar czvf ../r2ghidra-macos-${ARCH}.tar.gz *
    - name: Testing r2ghidra
      run: |
        r2 -qc 'af;pdg' test/bins/rec | tee a.txt
        grep csu_init a.txt
    - name: Uploading artifacts
      uses: actions/upload-artifact@v7
      with:
        name: macos-pkg-${{ env.ARCH }}
        path: dist/macos/r2ghidra-${{ env.R2V }}-macos-${{ env.ARCH }}.zip

  w64:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v7
    - uses: actions/setup-python@v7
      with:
        python-version: 3.12.x
    - name: Preparing msvc toolchain
      uses: TheMrMilchmann/setup-msvc-dev@v4
      with:
        arch: x64
    - name: dependencies
      shell: bash
      run: |
        pip install ninja meson wget
    - name: install r2
      shell: bash
      run: |
        python -m wget https://github.com/radareorg/radare2/releases/download/${{env.R2V}}/radare2-${{env.R2V}}-w64.zip -o r2.zip
        python3 -m zipfile -e r2.zip .
        mv radare2-${{env.R2V}}-w64 radare2
        pwd
    - name: batmake
      shell: cmd
      run: |
        call preconfigure.bat
        call configure.bat
        call make.bat
    - name: Rename artifact with version
      shell: bash
      run: mv destdir/r2ghidra-w64.zip destdir/r2ghidra-${{env.R2V}}-w64.zip
    - uses: actions/upload-artifact@v7
      with:
        name: w64
        path: destdir/r2ghidra-${{env.R2V}}-w64.zip

  check_release:
    # Run on tag pushes; filter tags starting with a digit in the release step
    if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') }}
    outputs:
      is_release: ${{ steps.release.outputs.is }}
      tag_name: ${{ steps.release.outputs.tag }}
    needs:
      - linux-deb
      - macos-pkg
      - tarball
      - w64
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v7
        with:
          fetch-depth: 0 # Download all git history and tags
      - name: Check if is a release
        run: git describe --exact-match --tags ${{ github.sha }} | awk 'BEGIN{tag="-";r="no"}/^[0-9]+\.[0-9]+/{tag=$0;r="yes"};END{print "is="r;print "tag="tag}' >> ${GITHUB_OUTPUT}
        id: release
  release:
    # Only create a release for numeric tags that passed check_release
    if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') && needs.check_release.outputs.is_release == 'yes' }}
    needs:
      - check_release
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v7
        with:
          fetch-depth: 0 # Download all git history and tags
      - name: Extract r2g version
        run: echo "branch=`./configure -qV`" >> $GITHUB_OUTPUT
        id: r2gv
      - name: Prepare release notes
        run: echo Release > ./RELEASE_NOTES.md
      - name: Download artifacts
        uses: actions/download-artifact@v8
        with:
          path: dist/artifacts
      - name: Create GitHub release and upload assets
        uses: softprops/action-gh-release@v3
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ needs.check_release.outputs.tag_name }}
          name: ${{ steps.r2gv.outputs.branch }}
          body_path: ./RELEASE_NOTES.md
          draft: false
          prerelease: false
          files: |
            dist/artifacts/linux-deb/r2ghidra_${{ steps.r2gv.outputs.branch }}_amd64.deb
            dist/artifacts/macos-pkg-x86_64/r2ghidra-${{ steps.r2gv.outputs.branch }}-macos-x86_64.zip
            dist/artifacts/macos-pkg-arm64/r2ghidra-${{ steps.r2gv.outputs.branch }}-macos-arm64.zip
            dist/artifacts/w64/r2ghidra-${{ steps.r2gv.outputs.branch }}-w64.zip
            dist/artifacts/tarball/r2ghidra-${{ steps.r2gv.outputs.branch }}.zip
            dist/artifacts/tarball/r2ghidra-${{ steps.r2gv.outputs.branch }}.tar.xz
            dist/artifacts/linux-acr-ok/r2ghidra_sleigh-${{ steps.r2gv.outputs.branch }}.zip
