Skip to content

Commit 64468d8

Browse files
authored
Merge pull request #26 from x1unix/feat/arm64
feat: Windows on ARM support
2 parents e055697 + 39937af commit 64468d8

File tree

18 files changed

+294
-127
lines changed

18 files changed

+294
-127
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.gitignore
22
.idea
33
.vscode
4+
.github
45
Makefile
56
README.md
67
tools

.github/workflows/release.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ on:
55
release:
66
types:
77
- created
8-
repository_dispatch:
9-
types: manual-deploy
108
jobs:
119
build:
1210
name: Build image

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.vscode
22
.idea
3-
3+
.DS_Store

Dockerfile

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
ARG GO_VERSION=1.15
2-
FROM golang:${GO_VERSION}-bullseye
1+
ARG GO_VERSION=1.20
2+
FROM golang:${GO_VERSION}-bookworm as builder
3+
ARG GO_VERSION
4+
ARG LLVM_MINGW64_VER=20240606
5+
ARG LLVM_MINGW64_SRC="https://github.com/mstorsjo/llvm-mingw/releases/download"
6+
ENV LLVM_MINGW64_VER="${LLVM_MINGW64_VER}"
7+
ENV LLVM_MINGW64_SRC="$LLVM_MINGW64_SRC"
8+
9+
WORKDIR /tmp
10+
COPY --chmod=0755 scripts/setup-llvm-mingw64.sh /tmp/
11+
RUN /tmp/setup-llvm-mingw64.sh
12+
13+
ARG GO_VERSION
14+
FROM golang:${GO_VERSION}-bookworm
315

416
RUN apt update &&\
517
apt install \
618
make mingw-w64 bash --yes
7-
ADD docker-entrypoint.sh /usr/bin/docker-entrypoint.sh
8-
RUN chmod +x /usr/bin/docker-entrypoint.sh
19+
COPY --chmod=0755 scripts/docker-entrypoint.sh /usr/bin/docker-entrypoint.sh
20+
COPY --chmod=0755 scripts/install-llvm-mingw64.sh /tmp/install-llvm-mingw64.sh
21+
COPY --from=builder /tmp/llvm-mingw64 /tmp/llvm-mingw64
22+
RUN /tmp/install-llvm-mingw64.sh /tmp/llvm-mingw64
923
ENV PATH=/go/bin:$PATH \
1024
CGO_ENABLED=1 \
1125
GOOS=windows

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
IMG_NAME = x1unix/go-mingw-test
2+
DOCKER_OUT_TYPE=docker
23

34
.PHONY: image
45
image:
56
@if [ -z $(GO_VERSION) ]; then echo "usage: 'make image GO_VERSION=[GO VERSION NUMBER]'" && exit 1; fi; \
67
echo ":: Building image..." &&\
7-
docker build -t $(IMG_NAME):$(GO_VERSION) -f Dockerfile . --build-arg GO_VERSION=$(GO_VERSION)
8+
docker build --output=type=$(DOCKER_OUT_TYPE) -t $(IMG_NAME):$(GO_VERSION) -f Dockerfile . --build-arg GO_VERSION=$(GO_VERSION)

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ Docker image for building Go binaries for **Windows** with MinGW-w64 toolchain b
77

88
Image provides simple cross-compilation environment for windows 32 and 64bit builds.
99

10+
**Supports Windows on Arm!**
11+
12+
## Supported Architectures
13+
14+
Here is a list of supported host and target architectures:
15+
16+
| Host Architecture | Win x86 | Win x86-64 | Win Arm |
17+
| ------------------- | ------- | ---------- | ------- |
18+
| **arm64 / aarch64** ||||
19+
| **amd64** ||||
20+
1021
## Usage
1122

1223
You can pull Docker image with desired Go version from Docker Hub:
@@ -16,7 +27,7 @@ docker pull x1unix/go-mingw:latest # or "1.21" for specific Go version
1627
```
1728

1829
> [!TIP]
19-
> Please take a look at [full project build example](example/sqlite-app) before starting to work.
30+
> Please take a look at [examples](example/) before starting to work.
2031
2132
### Building Go applications inside container
2233

@@ -30,7 +41,17 @@ docker run --rm -it -v /YourPackageSrc:/go/work \
3041

3142
You will get compiled Windows binary.
3243

33-
**For 32-bit toolchain**
44+
#### Windows On Arm
45+
46+
Set `GOARCH=arm64` to build ARM Windows binary:
47+
48+
```shell
49+
docker run --rm -it -e GOARCH=arm64 -v /YourPackageSrc:/go/work \
50+
-w /go/work \
51+
x1unix/go-mingw go build .
52+
```
53+
54+
#### For 32-bit toolchain
3455

3556
To build a 32-bit executable, set `GOARCH=386` variable:
3657

@@ -41,7 +62,7 @@ docker run --rm -it -e GOARCH=386 -v /YourPackageSrc:/go/work \
4162
```
4263

4364
> [!TIP]
44-
> See full project build example [here](example/sqlite-app).
65+
> See check project build examples [here](example).
4566
4667
### Go linker flags override
4768

@@ -124,3 +145,8 @@ make image GO_VERSION=1.20
124145
> [!IMPORTANT]
125146
> Replace `1.20` with desired Go version.
126147
148+
## Credits
149+
150+
* [llvm-mingw](https://github.com/mstorsjo/llvm-mingw) for Windows on Arm support.
151+
* [mingw-w64](https://www.mingw-w64.org/) - for Windows on x86 and amd64 support.
152+
* The Go maintainers.

example/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Examples
2+
3+
This directory contains various examples of Go projects with CGO and cross-compilation scripts that use this Docker image.
4+
5+
* [hello](hello) - Simple app that uses CGO with WinAPI.
6+
* [sqlite-app](sqlite-app) - Example that uses CGO for SQLite driver.

example/hello/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.vscode
2+
.idea
3+
*.exe
4+
*.db

example/hello/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
GO_VERSION := 1.21 # or "latest" for bleeding-edge Go version
2+
GO_BUILD_FLAGS := -x
3+
GOCACHE ?= $(shell go env GOCACHE)
4+
GOPATH ?= $(shell go env GOPATH)
5+
6+
define docker_build
7+
@echo ":: Building for $(1) in MinGW container..."
8+
docker run --rm -it \
9+
-e GOCACHE=/go/.cache \
10+
-e GOARCH=$(1) \
11+
-v "$(GOCACHE)":/go/.cache \
12+
-v "$(GOPATH)/src":/go/src \
13+
-v "$(GOPATH)/pkg":/go/pkg \
14+
-v "$(PWD)":/go/work \
15+
-w /go/work \
16+
x1unix/go-mingw:$(GO_VERSION) \
17+
go build $(GO_BUILD_FLAGS) -o hello-$(1).exe .
18+
endef
19+
20+
.PHONY:all
21+
all: build-amd64 build-i386 build-arm64
22+
23+
.PHONY: build-amd64 build-i386 build-arm64
24+
build-amd64:
25+
$(call docker_build,amd64)
26+
27+
build-i386:
28+
$(call docker_build,386)
29+
30+
build-arm64:
31+
$(call docker_build,arm64)

example/hello/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# CGO Hello World
2+
3+
![demo](demo.png)
4+
5+
This is a simple app with CGO and invokes `MessageBox` function from WinAPI.
6+
7+
Run `make` to cross-compile program.

0 commit comments

Comments
 (0)