Skip to content

Commit c87a8c7

Browse files
Experimental Zig support for cross-compiling
1 parent fe49cb0 commit c87a8c7

File tree

4 files changed

+111
-9
lines changed

4 files changed

+111
-9
lines changed

.github/workflows/release-nightly.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ jobs:
2020
with:
2121
go-version-file: go.mod
2222
check-latest: true
23+
- uses: mlugg/setup-zig@v1
24+
with:
25+
version: 0.12.1
2326
- uses: actions/setup-node@v4
2427
with:
2528
node-version: 22

.github/workflows/release-tag-rc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ jobs:
2121
with:
2222
go-version-file: go.mod
2323
check-latest: true
24+
- uses: mlugg/setup-zig@v1
25+
with:
26+
version: 0.12.1
2427
- uses: actions/setup-node@v4
2528
with:
2629
node-version: 22

.github/workflows/release-tag-version.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ jobs:
2323
with:
2424
go-version-file: go.mod
2525
check-latest: true
26+
- uses: mlugg/setup-zig@v1
27+
with:
28+
version: 0.12.1
2629
- uses: actions/setup-node@v4
2730
with:
2831
node-version: 22

Makefile

Lines changed: 102 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ endif
109109

110110
LDFLAGS := $(LDFLAGS) -X "main.MakeVersion=$(MAKE_VERSION)" -X "main.Version=$(GITEA_VERSION)" -X "main.Tags=$(TAGS)"
111111

112-
LINUX_ARCHS ?= linux/amd64,linux/386,linux/arm-5,linux/arm-6,linux/arm64
113-
114112
GO_TEST_PACKAGES ?= $(filter-out $(shell $(GO) list code.gitea.io/gitea/models/migrations/...) code.gitea.io/gitea/tests/integration/migration-test code.gitea.io/gitea/tests code.gitea.io/gitea/tests/integration code.gitea.io/gitea/tests/e2e,$(shell $(GO) list ./... | grep -v /vendor/))
115113
MIGRATE_TEST_PACKAGES ?= $(shell $(GO) list code.gitea.io/gitea/models/migrations/...)
116114

@@ -805,15 +803,13 @@ $(DIST_DIRS):
805803
mkdir -p $(DIST_DIRS)
806804

807805
.PHONY: release-windows
808-
release-windows: | $(DIST_DIRS)
809-
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -buildmode exe -dest $(DIST)/binaries -tags 'osusergo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'windows/*' -out gitea-$(VERSION) .
810-
ifeq (,$(findstring gogit,$(TAGS)))
811-
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -buildmode exe -dest $(DIST)/binaries -tags 'osusergo gogit $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'windows/*' -out gitea-$(VERSION)-gogit .
812-
endif
806+
release-windows: | $(DIST_DIRS) zig-release-windows-amd64 zig-release-windows-arm64 zig-release-windows-amd64-gogit zig-release-windows-arm64-gogit
807+
@echo "Windows binaries built"
813808

814809
.PHONY: release-linux
815-
release-linux: | $(DIST_DIRS)
816-
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets '$(LINUX_ARCHS)' -out gitea-$(VERSION) .
810+
release-linux: | $(DIST_DIRS) zig-release-linux-amd64 zig-release-linux-arm64 zig-release-linux-arm6 zig-release-linux-linux-386
811+
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'linux/arm-5' -out gitea-$(VERSION) .
812+
echo "Linux binaries built"
817813

818814
.PHONY: release-darwin
819815
release-darwin: | $(DIST_DIRS)
@@ -986,6 +982,103 @@ docker:
986982
docker build --disable-content-trust=false -t $(DOCKER_REF) .
987983
# support also build args docker build --build-arg GITEA_VERSION=v1.2.3 --build-arg TAGS="bindata sqlite sqlite_unlock_notify" .
988984

985+
## Experimental Zig-based cross-compiling
986+
987+
# Platform definitions
988+
LINUX_PLATFORMS := linux-amd64 linux-arm64 linux-386 linux-arm6
989+
WINDOWS_PLATFORMS := windows-amd64 windows-arm64 windows-386
990+
DARWIN_PLATFORMS := darwin-amd64 darwin-arm64
991+
992+
PLATFORMS := $(LINUX_PLATFORMS) $(WINDOWS_PLATFORMS) $(DARWIN_PLATFORMS)
993+
994+
# Zig target mappings
995+
ZIG_TARGET.linux-amd64 := x86_64-linux-musl
996+
ZIG_TARGET.linux-arm64 := aarch64-linux-musl
997+
ZIG_TARGET.linux-386 := x86-linux-musl
998+
ZIG_TARGET.linux-arm6 := arm-linux-musleabihf
999+
ZIG_TARGET.windows-amd64 := x86_64-windows-gnu
1000+
ZIG_TARGET.windows-arm64 := aarch64-windows-gnu
1001+
ZIG_TARGET.windows-386 := x86-windows-gnu
1002+
ZIG_TARGET.darwin-amd64 := x86_64-macos
1003+
ZIG_TARGET.darwin-arm64 := aarch64-macos
1004+
1005+
# Output filename components
1006+
DARWIN_VERSION := 10.12
1007+
WINDOWS_VERSION := 4.0
1008+
1009+
# Function to get OS, ARCH from platform
1010+
OS = $(word 1,$(subst -, ,$1))
1011+
ARCH = $(word 2,$(subst -, ,$1))
1012+
1013+
# Function to generate output filename
1014+
define GET_FILENAME
1015+
$(strip \
1016+
$(if $(findstring windows,$1), \
1017+
gitea-$(VERSION)-$(if $2,gogit-)windows-$(WINDOWS_VERSION)-$(call ARCH,$1).exe, \
1018+
$(if $(findstring darwin,$1), \
1019+
gitea-$(VERSION)-darwin-$(DARWIN_VERSION)-$(call ARCH,$1), \
1020+
$(if $(filter %arm6,$(1)), \
1021+
gitea-$(VERSION)-linux-arm-6, \
1022+
gitea-$(VERSION)-linux-$(call ARCH,$1)) \
1023+
) \
1024+
) \
1025+
)
1026+
endef
1027+
1028+
# Common build flags
1029+
COMMON_TAGS := netgo osusergo $(TAGS)
1030+
1031+
# Platform-specific flags
1032+
CGO_FLAGS.linux := $(CGO_CFLAGS)
1033+
CGO_FLAGS.windows := $(CGO_CFLAGS)
1034+
CGO_FLAGS.darwin := $(CGO_CFLAGS)
1035+
1036+
# Platform-specific linker flags
1037+
EXTRA_LDFLAGS.linux := -static
1038+
# EXTRA_LDFLAGS.darwin := -dead_strip
1039+
1040+
GOARCH.linux-arm6 := arm
1041+
1042+
define GET_GOARCH
1043+
$(or $(GOARCH.$1),$(call ARCH,$1))
1044+
endef
1045+
1046+
# For ARM builds, we need to specify additional compiler flags
1047+
ZIG_ARM_FLAGS.linux-arm6 := -mcpu=arm1176jzf_s
1048+
1049+
# Generic release target
1050+
define RELEASE_template
1051+
zig-release-$(1)$(if $(2),-gogit): $$(DIST_DIRS)
1052+
CC="zig cc -target $$(ZIG_TARGET.$(1)) $$(ZIG_ARM_FLAGS.$(1))" \
1053+
CXX="zig c++ -target $$(ZIG_TARGET.$(1)) $$(ZIG_ARM_FLAGS.$(1))" \
1054+
CGO_ENABLED=1 \
1055+
CGO_CFLAGS="$$(CGO_FLAGS.$$(call OS,$(1)))" \
1056+
GOOS=$$(call OS,$(1)) \
1057+
GOARCH=$$(call GET_GOARCH,$(1)) \
1058+
$(if $(filter %arm6,$(1)),GOARM=6,) \
1059+
$$(GO) build $$(GOFLAGS) -tags '$$(COMMON_TAGS)$(if $(2), gogit,)' \
1060+
-ldflags '-s -w $(if $(findstring darwin,$(1)),,-linkmode external) -extldflags "$$(EXTRA_LDFLAGS.$$(call OS,$(1)))" $$(LDFLAGS)' \
1061+
-o $$(DIST)/binaries/$$(call GET_FILENAME,$(1),$(2)) .
1062+
endef
1063+
1064+
# Generate release targets for all platforms
1065+
$(foreach platform,$(PLATFORMS),$(eval $(call RELEASE_template,$(platform),)))
1066+
1067+
# Generate additional gogit targets for Windows
1068+
$(foreach platform,$(WINDOWS_PLATFORMS),$(eval $(call RELEASE_template,$(platform),1)))
1069+
1070+
# Target to build all platforms
1071+
zig-release-all: $(foreach platform,$(PLATFORMS),zig-release-$(platform)) \
1072+
$(foreach platform,$(WINDOWS_PLATFORMS),zig-release-$(platform)-gogit)
1073+
1074+
# Helper to list all available targets
1075+
.PHONY: list-targets
1076+
zig-list-targets:
1077+
@echo "Available targets:"
1078+
@echo "Linux targets: $(addprefix zig-release-,$(LINUX_PLATFORMS))"
1079+
@echo "Windows targets: $(addprefix zig-release-,$(WINDOWS_PLATFORMS)) $(addprefix zig-release-,$(addsuffix -gogit,$(WINDOWS_PLATFORMS)))"
1080+
@echo "Darwin targets: $(addprefix zig-release-,$(DARWIN_PLATFORMS))"
1081+
9891082
# This endif closes the if at the top of the file
9901083
endif
9911084

0 commit comments

Comments
 (0)