Build Go Windows DLL (Win7 / Go1.20) #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build Go Windows DLL (Win7 / Go1.20) | |
| permissions: | |
| contents: write | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| branch: | |
| description: "Git branch to build (e.g., unity)" | |
| required: true | |
| default: "unity" | |
| dll_name: | |
| description: "Base name for the output DLL (without extension)" | |
| required: true | |
| default: "openimsdk" | |
| package_path: | |
| description: "Go package path to build (relative to repo root, e.g., ./ )" | |
| required: true | |
| default: "./" | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| include: | |
| - arch: amd64 | |
| cc: x86_64-w64-mingw32-gcc | |
| cxx: x86_64-w64-mingw32-g++ | |
| suffix: amd64 | |
| - arch: "386" | |
| cc: i686-w64-mingw32-gcc | |
| cxx: i686-w64-mingw32-g++ | |
| suffix: 386 | |
| steps: | |
| - name: Checkout target branch (e.g., unity) | |
| uses: actions/checkout@v4 | |
| with: | |
| # IMPORTANT: keep the "Use workflow from" dropdown on main; we checkout the target source branch here | |
| ref: ${{ inputs.branch }} | |
| fetch-depth: 0 | |
| # submodules: recursive # uncomment if your repo uses submodules | |
| - name: Verify checked-out ref | |
| run: | | |
| echo "Expected branch input: ${{ inputs.branch }}" | |
| echo "HEAD commit: $(git rev-parse HEAD)" | |
| echo "Remote branches that contain HEAD (detached HEAD is normal on Actions):" | |
| git branch -r --contains HEAD || true | |
| echo "Decorated HEAD:" | |
| git show -s --format='%H %D %s' HEAD | |
| - name: Set up Go 1.20 | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: "1.20" | |
| - name: Cache Go modules | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| ~/go/pkg/mod | |
| ~/.cache/go-build | |
| # Cache key based on go.sum; if go.sum changes, cache busts | |
| key: ${{ runner.os }}-go-${{ hashFiles(format('{0}/go.sum', inputs.package_path)) }} | |
| restore-keys: | | |
| ${{ runner.os }}-go- | |
| - name: Prepare modules (populate go.sum) | |
| working-directory: ${{ inputs.package_path }} | |
| run: | | |
| # Ensure proxy/sumdb are available on GitHub-hosted runners | |
| go env -w GOPROXY=https://proxy.golang.org,direct | |
| go env -w GOSUMDB=sum.golang.org | |
| # Download all required modules to populate go.sum without modifying go.mod | |
| go mod download all | |
| # Optional: tighten graph and ensure 1.20 compatibility (may edit go.mod) | |
| # go mod tidy -compat=1.20 | |
| - name: Install mingw-w64 for CGO cross-compilation | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y --no-install-recommends mingw-w64 | |
| - name: Build Windows DLL (${{ matrix.arch }}) | |
| env: | |
| GOOS: windows | |
| GOARCH: ${{ matrix.arch }} | |
| CGO_ENABLED: "1" | |
| CC: ${{ matrix.cc }} | |
| CXX: ${{ matrix.cxx }} | |
| # Win7 compatibility macros (adjust or remove if not needed) | |
| CGO_CFLAGS: "-D_WIN32_WINNT=0x0601 -DWINVER=0x0601" | |
| run: | | |
| mkdir -p windows | |
| OUT="windows/${{ inputs.dll_name }}_${{ matrix.suffix }}.dll" | |
| echo "Building ${OUT} ..." | |
| go build -buildmode=c-shared -trimpath -ldflags="-s -w" \ | |
| -o "${OUT}" \ | |
| "${{ inputs.package_path }}" | |
| echo "Listing artifacts in windows/:" | |
| ls -l windows | |
| - name: Upload artifacts | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: ${{ inputs.dll_name }}-win-dlls | |
| path: | | |
| windows/${{ inputs.dll_name }}_*.dll | |
| windows/${{ inputs.dll_name }}_*.h |