Skip to content

Build Go Windows DLL (Win7 / Go1.20) #5

Build Go Windows DLL (Win7 / Go1.20)

Build Go Windows DLL (Win7 / Go1.20) #5

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: "libopenimsdk"
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:
ref: ${{ inputs.branch }}
fetch-depth: 0
# submodules: recursive
- 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
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: |
go env -w GOPROXY=https://proxy.golang.org,direct
go env -w GOSUMDB=sum.golang.org
go mod download all
- 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 }}
CGO_CFLAGS: "-D_WIN32_WINNT=0x0601 -DWINVER=0x0601"
run: |
set -euo pipefail
NAME="lib${{ inputs.dll_name }}"
OUT_DIR="windows/${{ matrix.arch }}"
rm -rf "$OUT_DIR"
mkdir -p "$OUT_DIR"
echo "Building ${NAME}.dll for ${{ matrix.arch }} ..."
go build -buildmode=c-shared -trimpath -ldflags="-s -w" \
-o "${OUT_DIR}/${NAME}.dll" \
"${{ inputs.package_path }}"
echo "Artifacts in ${OUT_DIR}:"
ls -l "${OUT_DIR}"
- name: Upload artifacts (${{ matrix.arch }})
uses: actions/upload-artifact@v4
with:
name: lib${{ inputs.dll_name }}-${{ matrix.arch }}
path: |
windows/${{ matrix.arch }}/lib${{ inputs.dll_name }}.dll
windows/${{ matrix.arch }}/lib${{ inputs.dll_name }}.h
windows/${{ matrix.arch }}/lib${{ inputs.dll_name }}.dll.a
if-no-files-found: warn