name: udp2raw
on:
  workflow_dispatch:
    inputs:
      branch:
        description: '编译udp2raw源码的分支或版本号，默认主分支fake-http'
        required: true
        default: 'fake-http'
      del:
        description: '是否删除历史编译的流程记录'
        required: true
        default: true
        type: boolean
      rel:
        description: '编译完成后是否发布Release'
        required: true
        default: true
        type: boolean
      tag:
        description: '指定Release的版本号（留空为源码提交哈希值）'
        required: false
        default: ''
      upx:
        description: '是否使用upx压缩linux的二进制'
        required: true
        default: true
        type: boolean
  
env:
  branch: "${{ github.event.inputs.branch }}"
  tag: "${{ github.event.inputs.tag }}"
  rel: "${{ github.event.inputs.rel }}"
  TZ: Asia/Shanghai
permissions:
  contents: write
  actions: write
jobs:
  build:
    name: Build ${{ matrix.target }}
    strategy:
      fail-fast: false
      matrix:
        include:
        
        - 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

    runs-on: ${{ matrix.os }}
    steps:
     - name: Checkout code
       uses: actions/checkout@v4
       with:
        repository: MikeWang000000/udp2raw
        ref: ${{ env.branch }}
     - name: 下载linux-musl-gcc
       uses: lmq8267/dl-musl@main
       with:
        target: ${{ matrix.URL }}
        static: true
        gccpath: /tmp
     - name: 编译
       run: |
         echo "const char *gitversion = \"$(shell git rev-parse HEAD)\";" > git_version.h
         sed -i 's/-fsanitize=address,undefined/-static/' CMakeLists.txt
         export CC="$CC -static"
         export CXX="$CXX -static"
         mkdir -p build && cd build
         cmake .. -DCMAKE_BUILD_TYPE=Release
         make -j$(nproc)
         $STRIP udp2raw
         file udp2raw
     
     - name: 安装 UPX
       if: ${{ github.event.inputs.upx == 'true' && !startsWith(matrix.target, 's390x') && !startsWith(matrix.target, 'loongarch64') }}
       uses: crazy-max/ghaction-upx@v3
       with:
        version: v4.2.4
        install-only: true
     - name: 压缩
       if: ${{ github.event.inputs.upx == 'true' && !startsWith(matrix.target, 's390x') && !startsWith(matrix.target, 'loongarch64') }}
       run: upx --lzma --best build/udp2raw
     - uses: actions/upload-artifact@v4
       with:
        name: udp2raw-${{ matrix.target }}
        path: build/udp2raw
     - name: 日期
       if: github.event.inputs.rel == 'true'
       shell: bash
       run: |
          echo "build_time=$(date '+%Y年%m月%d日%H:%M:%S' | jq -sRr @uri)" >> $GITHUB_ENV
          if [ -z "$TAG" ]; then
              echo "tag=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
          fi
          echo "haxi=$(git rev-parse HEAD)" >> $GITHUB_ENV
          mv -f build/udp2raw build/udp2raw-${{ matrix.target }}
       env:
        TAG: ${{ env.tag }}
        TARGET: ${{ matrix.target }}
     - name: 发布Release
       if: github.event.inputs.rel == 'true'
       uses: softprops/action-gh-release@v2
       with:
          token: ${{ secrets.GITHUB_TOKEN }}
          body: |
           > ### ![](https://img.shields.io/badge/%E7%BC%96%E8%AF%91%E6%97%B6%E9%97%B4-${{ env.build_time }}-8267?logo=github&labelColor=%E9%A1%BB)![](https://img.shields.io/github/downloads/${{ github.repository }}/${{env.tag}}/total?label=%E4%B8%8B%E8%BD%BD%E6%AC%A1%E6%95%B0&logo=github)
           
           [提交记录](https://github.com/MikeWang000000/udp2raw/tree/${{ env.haxi }})
           
           [官方源码](https://github.com/MikeWang000000/udp2raw)
           
          tag_name: ${{env.tag}}
          files: build/udp2raw-${{ matrix.target }}
  del:
    runs-on: ubuntu-latest
    steps:
      - name: 删除流程记录
        if: github.event.inputs.del == 'true'
        uses: Mattraks/delete-workflow-runs@main
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          retain_days: 0
          keep_minimum_runs: 0
