name: opengnb-build
on:
  workflow_dispatch:
    inputs:
      branch:
        description: '编译opengnb源码的分支或版本号，默认主分支master'
        required: true
        default: 'master'
      lite:
        description: '是否将lite模式增加到50个节点'
        required: true
        default: true
        type: boolean
      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-apple-darwin
          os: macos-latest
          
        - target: x86_64-apple-darwin
          os: macos-latest
          
        - target: x86_64-pc-windows
          os: ubuntu-latest
          URL: x86_64-w64-mingw32

        - target: i686-pc-windows
          os: ubuntu-latest
          URL: i686-w64-mingw32

        - target: aarch64-pc-windows
          os: ubuntu-latest
          URL: aarch64-w64-mingw32
          
        - 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: gnbdev/opengnb
        ref: ${{ env.branch }}
     - name: lite模式增加50个节点
       if: github.event.inputs.lite == 'true'
       shell: bash
       run: |
         if [[ "$OSTYPE" == "darwin"* ]]; then
             sed -i  '' 's/1005|10.1.0.5|255.255.0.0/1005|10.1.0.5|255.255.0.0,1006|10.1.0.6|255.255.0.0,1007|10.1.0.7|255.255.0.0,1008|10.1.0.8|255.255.0.0,1009|10.1.0.9|255.255.0.0,1010|10.1.0.10|255.255.0.0,1011|10.1.0.11|255.255.0.0,1012|10.1.0.12|255.255.0.0,1013|10.1.0.13|255.255.0.0,1014|10.1.0.14|255.255.0.0,1015|10.1.0.15|255.255.0.0,1016|10.1.0.16|255.255.0.0,1017|10.1.0.17|255.255.0.0,1018|10.1.0.18|255.255.0.0,1019|10.1.0.19|255.255.0.0,1020|10.1.0.20|255.255.0.0,1021|10.1.0.21|255.255.0.0,1022|10.1.0.22|255.255.0.0,1023|10.1.0.23|255.255.0.0,1024|10.1.0.24|255.255.0.0,1025|10.1.0.25|255.255.0.0,1026|10.1.0.26|255.255.0.0,1027|10.1.0.27|255.255.0.0,1028|10.1.0.28|255.255.0.0,1029|10.1.0.29|255.255.0.0,1030|10.1.0.30|255.255.0.0/g' ./src/gnb_config_lite.c
             sed -i  '' 's/1030|10.1.0.30|255.255.0.0/1030|10.1.0.30|255.255.0.0,1031|10.1.0.31|255.255.0.0,1032|10.1.0.32|255.255.0.0,1033|10.1.0.33|255.255.0.0,1034|10.1.0.34|255.255.0.0,1035|10.1.0.35|255.255.0.0,1036|10.1.0.36|255.255.0.0,1037|10.1.0.37|255.255.0.0,1038|10.1.0.38|255.255.0.0,1039|10.1.0.39|255.255.0.0,1040|10.1.0.40|255.255.0.0,1041|10.1.0.41|255.255.0.0,1042|10.1.0.42|255.255.0.0,1043|10.1.0.43|255.255.0.0,1044|10.1.0.44|255.255.0.0,1045|10.1.0.45|255.255.0.0,1046|10.1.0.46|255.255.0.0,1047|10.1.0.47|255.255.0.0,1048|10.1.0.48|255.255.0.0,1049|10.1.0.49|255.255.0.0,1050|10.1.0.50|255.255.0.0/g' ./src/gnb_config_lite.c
         else
             sed -i 's/1005|10.1.0.5|255.255.0.0/1005|10.1.0.5|255.255.0.0,1006|10.1.0.6|255.255.0.0,1007|10.1.0.7|255.255.0.0,1008|10.1.0.8|255.255.0.0,1009|10.1.0.9|255.255.0.0,1010|10.1.0.10|255.255.0.0,1011|10.1.0.11|255.255.0.0,1012|10.1.0.12|255.255.0.0,1013|10.1.0.13|255.255.0.0,1014|10.1.0.14|255.255.0.0,1015|10.1.0.15|255.255.0.0,1016|10.1.0.16|255.255.0.0,1017|10.1.0.17|255.255.0.0,1018|10.1.0.18|255.255.0.0,1019|10.1.0.19|255.255.0.0,1020|10.1.0.20|255.255.0.0,1021|10.1.0.21|255.255.0.0,1022|10.1.0.22|255.255.0.0,1023|10.1.0.23|255.255.0.0,1024|10.1.0.24|255.255.0.0,1025|10.1.0.25|255.255.0.0,1026|10.1.0.26|255.255.0.0,1027|10.1.0.27|255.255.0.0,1028|10.1.0.28|255.255.0.0,1029|10.1.0.29|255.255.0.0,1030|10.1.0.30|255.255.0.0/g' ./src/gnb_config_lite.c
             sed -i 's/1030|10.1.0.30|255.255.0.0/1030|10.1.0.30|255.255.0.0,1031|10.1.0.31|255.255.0.0,1032|10.1.0.32|255.255.0.0,1033|10.1.0.33|255.255.0.0,1034|10.1.0.34|255.255.0.0,1035|10.1.0.35|255.255.0.0,1036|10.1.0.36|255.255.0.0,1037|10.1.0.37|255.255.0.0,1038|10.1.0.38|255.255.0.0,1039|10.1.0.39|255.255.0.0,1040|10.1.0.40|255.255.0.0,1041|10.1.0.41|255.255.0.0,1042|10.1.0.42|255.255.0.0,1043|10.1.0.43|255.255.0.0,1044|10.1.0.44|255.255.0.0,1045|10.1.0.45|255.255.0.0,1046|10.1.0.46|255.255.0.0,1047|10.1.0.47|255.255.0.0,1048|10.1.0.48|255.255.0.0,1049|10.1.0.49|255.255.0.0,1050|10.1.0.50|255.255.0.0/g' ./src/gnb_config_lite.c
         fi
     - 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: 编译 macOS
       if: ${{ runner.os == 'macOS' }}
       shell: bash
       env:
        TARGET: ${{ matrix.target }}
       run: |
         ARCH=$(uname -m)
         echo "当前机器架构: $ARCH"
         if [[ "$TARGET" == "x86_64-apple-darwin" ]]; then
             export CC="clang -arch x86_64"
             export CXX="clang++ -arch x86_64"
         else
             export CC="clang -arch arm64"
             export CXX="clang++ -arch arm64"
         fi
         make -f Makefile.Darwin install
         file bin/gnb
         otool -L bin/gnb
         bin/gnb -h
     - name: 编译 Liunx Windows
       if: ${{ runner.os == 'Linux' && !endsWith(matrix.target, 'openbsd') && !endsWith(matrix.target, 'freebsd') }}
       run: |
         export CC="$CC -static"
         export CXX="$CXX -static"
         if [[ "${{ matrix.target }}" == "x86_64-pc-windows" ]] ; then
            export WINDRES=x86_64-w64-mingw32-windres
            make -f Makefile.mingw_x86_64 install
            $STRIP bin/*
            file bin/gnb.exe
         elif [[ "${{ matrix.target }}" == "i686-pc-windows" ]] ; then
            export WINDRES=i686-w64-mingw32-windres
            make -f Makefile.mingw_x86_64 install
            $STRIP bin/*
            file bin/gnb.exe
         elif [[ "${{ matrix.target }}" == "aarch64-pc-windows" ]] ; then
            export WINDRES=aarch64-w64-mingw32-windres
            make -f Makefile.mingw_x86_64 install
            $STRIP bin/*
            file bin/gnb.exe
         else
            make -f Makefile.openwrt install
            $STRIP bin/*
            file bin/gnb
         fi
     - 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
              export CC="clang -static"
              export CXX="clang++ -static"
              gmake -f Makefile.freebsd install
              strip bin/*
              file bin/gnb
              ldd bin/gnb || true
              bin/gnb -h
     - 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 gmake
              export CC=clang
              export CXX=clang++
              gmake -f Makefile.openbsd install
              strip bin/*
              file bin/gnb 
              ldd bin/gnb || true
              bin/gnb -h
     - name: 安装 UPX
       if: ${{ runner.os == 'Linux' && !endsWith(matrix.target, 'windows') && !endsWith(matrix.target, 'openbsd') && !endsWith(matrix.target, 'freebsd') && !startsWith(matrix.target, 's390x') && !startsWith(matrix.target, 'loongarch64') && github.event.inputs.upx == 'true' }}
       uses: crazy-max/ghaction-upx@v3
       with:
        version: latest
        install-only: true
     - name: 压缩
       if: ${{ runner.os == 'Linux' && !endsWith(matrix.target, 'windows') && !endsWith(matrix.target, 'openbsd') && !endsWith(matrix.target, 'freebsd') && !startsWith(matrix.target, 's390x') && !startsWith(matrix.target, 'loongarch64') && github.event.inputs.upx == 'true' }}
       run: upx --lzma --best bin/*
     - uses: actions/upload-artifact@v4
       with:
        name: opengnb-${{ env.branch }}-${{ matrix.target }}
        path: bin/*
     - name: 打包
       if: github.event.inputs.rel == 'true'
       shell: bash
       run: |
          echo "build_time=$(date '+%Y-%m-%d %H:%M:%S')" >> $GITHUB_ENV
          if [ -z "$TAG" ]; then
              echo "tag=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
          fi
          echo "haxi=$(git rev-parse HEAD)" >> $GITHUB_ENV
          cp -r examples/node_config_example conf
          zip -r opengnb-$TARGET.zip bin conf
          if [ "${{ github.event.inputs.lite }}" == "true" ]; then
              echo "lite_info=lite模式增加到50个节点[（1001|10.1.0.1|255.255.0.0 至 1050|10.1.0.50|255.255.0.0）](https://github.com/gnbdev/opengnb/blob/${{ env.haxi }}/src/gnb_config_lite.c#L325C48-L325C73)" >> $GITHUB_ENV
          fi
       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: |
           > ### 编译时间 ：${{ env.build_time }} [提交记录](https://github.com/gnbdev/opengnb/tree/${{ env.haxi }})
           
           ${{ env.lite_info }}
           [官方源码](https://github.com/gnbdev/opengnb)
           
          tag_name: ${{env.tag}}
          files: opengnb-${{ matrix.target }}.zip
  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