name: 编译iperf3

on:
  schedule:
    - cron: '0 20 * * *'
  workflow_dispatch:
    inputs:
      rel:
        description: '编译完成后是否发布Release'
        required: true
        default: false
        type: boolean
      bj:
        description: '强制使用手动编译'
        required: true
        default: false
        type: boolean
  
env:
  rel: ${{ github.event.inputs.rel || 'true' }}
  TZ: Asia/Shanghai
permissions:
  contents: write
  actions: write
jobs:
  check:
    runs-on: ubuntu-latest
    outputs:
      ver: ${{ steps.getversion.outputs.ver }}
      cver: ${{ steps.getversion.outputs.cver }}
    steps:
    - name: 检查更新
      id: getversion
      run: |
        ver=`curl https://api.github.com/repos/esnet/iperf/releases/latest | jq -r '.tag_name'`
        cver=`curl https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r '.tag_name'`
        echo "ver=${ver}" >> $GITHUB_OUTPUT
        echo "cver=${cver}" >> $GITHUB_OUTPUT
    -  name: 删除工作流
       uses: Mattraks/delete-workflow-runs@main
       with:
        token: ${{ secrets.GITHUB_TOKEN }}
        retain_days: 0
        keep_minimum_runs: 0
  build:
    needs: check
    name: Build ${{ matrix.target }}
    if: ${{ github.event.inputs.bj == 'true' || (github.event.inputs.bj != 'true' && needs.check.outputs.ver != needs.check.outputs.cver) }}
    strategy:
      fail-fast: false
      matrix:
        include:
        - target: x86_64-pc-windows
          os: windows-latest
          arch: x86_64

        - target: x86-pc-windows
          os: windows-latest
          arch: x86
          
        - target: aarch64-linux-musl
          os: ubuntu-latest
          URL: aarch64-linux-musl
          
        - target: x86_64-linux-musl
          os: ubuntu-latest
          URL: x86_64-linux-musl
            
        - target: i686-linux-musl
          os: ubuntu-latest
          URL: i686-linux-musl
        
        - target: armv7-linux-musleabihf
          os: ubuntu-latest
          URL: armv7l-linux-musleabihf

        - target: armv7-linux-musleabi
          os: ubuntu-latest
          URL: armv7m-linux-musleabi
          
        - target: arm-linux-musleabihf
          os: ubuntu-latest
          URL: arm-linux-musleabihf

        - target: arm-linux-musleabi
          os: ubuntu-latest
          URL: arm-linux-musleabi

        - target: mipsel-linux-musl
          os: ubuntu-latest
          URL: mipsel-linux-muslsf

        - target: mips-linux-musl
          os: ubuntu-latest
          URL: mips-linux-muslsf

        - target: mips64-linux-musl
          os: ubuntu-latest
          URL: mips-linux-musl

        - target: s390x-linux-musl
          os: ubuntu-latest
          URL: s390x-linux-musl

        - target: loongarch64-linux-musl
          os: ubuntu-latest
          URL: loongarch64-linux-musl

        - target: x86_64-freebsd
          os: ubuntu-latest
          arch: x86-64
          version: '14.2'

        - target: x86_64-openbsd
          os: ubuntu-latest
          arch: x86-64
          version: '7.6'

        - target: arm64-freebsd
          os: ubuntu-latest
          arch: arm64
          version: '14.2'

        - target: arm64-openbsd
          os: ubuntu-latest
          arch: arm64
          version: '7.6'

    runs-on: ${{ matrix.os }}
    steps:
     - name: Checkout code
       uses: actions/checkout@v4
       with:
        repository: esnet/iperf
        ref: ${{ needs.check.outputs.ver }}
     - name: 下载linux-musl-gcc
       if: ${{ runner.os == 'Linux' && !endsWith(matrix.target, 'openbsd') && !endsWith(matrix.target, 'freebsd') }}
       uses: lmq8267/dl-musl@main
       with:
        target: ${{ matrix.URL }}
        static: true
        gccpath: /tmp
     - name: 编译 Liunx 
       if: ${{ runner.os == 'Linux' && !endsWith(matrix.target, 'openbsd') && !endsWith(matrix.target, 'freebsd') }}
       run: |
         export CC="$CC -static"
         export CXX="$CXX -static"
         mkdir -p bin
         sudo find /tmp -name "*.la" -delete
         ./bootstrap.sh
         ./configure LDFLAGS="$LDFLAGS -latomic -lpthread" --host=${{ matrix.target }}  --target=${{ matrix.target }} --disable-shared --enable-static --enable-static-bin --prefix="$(pwd)"
         make 
         make install
         $STRIP bin/iperf3
         file bin/iperf3
         mv bin/iperf3 bin/iperf3-${{ matrix.target }}
     - name: 安装 Cygwin
       if: ${{ runner.os == 'Windows' }}
       uses: cygwin/cygwin-install-action@master
       with:
        platform: ${{ matrix.arch }}
        packages: "gcc-core,gcc-g++,make,automake,autoconf,libtool,git,wget,zip"
     - name: 编译 Windows
       if: ${{ runner.os == 'Windows' }}
       shell: C:\cygwin\bin\bash.exe -eo pipefail -o igncr '{0}'
       run: |
         dos2unix ./bootstrap.sh
         dos2unix ./configure
         ./bootstrap.sh 
         ./configure LDFLAGS="$LDFLAGS -static" CFLAGS="$CFLAGS -static" --disable-shared --enable-static --enable-static-bin --prefix=$HOME/iperf3
         make 
         make install 
         file $HOME/iperf3/bin/iperf3 
         mkdir -p iperf3
         $HOME/iperf3/bin/iperf3 -h
         cp /cygdrive/c/cygwin/bin/cygwin1.dll ./iperf3/
         mv $HOME/iperf3/bin/iperf3 ./iperf3/
         zip -r iperf3-${{ matrix.target }}.zip iperf3
         mkdir -p bin
         mv iperf3-${{ matrix.target }}.zip ./bin/
         git config --global --add safe.directory $(pwd)
     - name: 编译 FreeBSD
       if: ${{ endsWith(matrix.target, 'freebsd') }}
       uses: cross-platform-actions/action@master
       with:
         operating_system: freebsd
         architecture: ${{ matrix.arch }}
         version: ${{ matrix.version }}
         shell: bash
         memory: 5G
         cpu_count: 4
         run: |
              sudo pkg install -y gmake autoconf automake libtool pkgconf
              export CC="clang -static"
              export CXX="clang++ -static"
              mkdir -p bin
              ./bootstrap.sh
              ./configure --disable-shared --enable-static --enable-static-bin --prefix="$(pwd)"
              gmake 
              gmake install
              strip bin/iperf3
              file bin/iperf3
              mv bin/iperf3 bin/iperf3-${{ matrix.target }}
     - name: 编译 OpenBSD
       if: ${{ endsWith(matrix.target, 'openbsd') }}
       uses: cross-platform-actions/action@master
       with:
         operating_system: openbsd
         architecture: ${{ matrix.arch }}
         version: ${{ matrix.version }}
         shell: bash
         memory: 5G
         cpu_count: 4
         run: |
              sudo pkg_add make
              export CC=clang
              export CXX=clang++
              mkdir -p bin
              ./bootstrap.sh
              ./configure --disable-shared --enable-static --enable-static-bin --prefix="$(pwd)"
              make 
              make install
              strip bin/iperf3
              file bin/iperf3
              mv bin/iperf3 bin/iperf3-${{ matrix.target }}
     - name: 安装 UPX
       if: ${{ runner.os == 'Linux' && !endsWith(matrix.target, 'openbsd') && !endsWith(matrix.target, 'freebsd') && !startsWith(matrix.target, 's390x') && !startsWith(matrix.target, 'loongarch64') }}
       uses: crazy-max/ghaction-upx@v3
       with:
        version: latest
        install-only: true
     - name: 压缩
       if: ${{ runner.os == 'Linux' && !endsWith(matrix.target, 'openbsd') && !endsWith(matrix.target, 'freebsd') && !startsWith(matrix.target, 's390x') && !startsWith(matrix.target, 'loongarch64') }}
       run: |
        cd bin
        upx --lzma --best iperf3-${{ matrix.target }}
     - uses: actions/upload-artifact@v4
       with:
        name: iperf3-${{ needs.check.outputs.ver }}-${{ matrix.target }}
        path: bin/*
     - name: 设置变量
       if: github.event.inputs.rel == 'true'
       run: |
          echo "build_time=$(date '+%Y-%m-%d %H:%M:%S')" >> $GITHUB_ENV
          echo "haxi=$(git rev-parse HEAD)" >> $GITHUB_ENV
     - name: 发布Release
       if: github.event.inputs.rel == 'true'
       uses: softprops/action-gh-release@v2
       with:
          token: ${{ secrets.GITHUB_TOKEN }}
          body: |
           > ### 编译时间 ：${{ env.build_time }} [提交记录](https://github.com/esnet/iperf/tree/${{ env.haxi }})
           
           [官方更新说明](https://github.com/esnet/iperf/releases)
           
          tag_name: ${{ needs.check.outputs.ver }}
          files: bin/*
  keepalive-workflow:
    # github超过60天未提交将自动停止工作流
    name: 工作流保活 
    runs-on: ubuntu-latest
    permissions:
      actions: write
      contents: write
    steps:
      - uses: actions/checkout@v4
      - uses: gautamkrishnar/keepalive-workflow@v2
        #with:
          #use_api: true
