From e8279fcefdbf986cefd03dbf024a66f3823991b0 Mon Sep 17 00:00:00 2001 From: Giteabot Date: Tue, 30 Apr 2024 18:51:38 +0800 Subject: [PATCH] Fix cross-compilation errors when CGO_CFLAGS/CGO_LDFLAGS is set (#30749) (#30774) Backport #30749 by @sryze When you cross-compile Gitea and you specify one of the envrionment variables related to C flags, cgo will fail to build the generator programs (e.g. generate-bindata) because GOOS and GOARCH are unset, but those additional flags variables are not unset together with those. To solve this issue, the simplest way that I've found is to disable cgo in the `go generate` command as it's not really used there. For example, I've had this problem with cross-compiling Gitea on FreeBSD x86_64 to ARMv7 where it's necessary to pass `--target` to `clang` via `CGO_CFLAGS`: ``` GOOS=freebsd \ GOARCH=arm \ GGOARM=7 \ CGO_ENABLED=1 \ SYSROOT=/usr/local/freebsd-sysroot/armv7 \ CC=clang \ CGO_CFLAGS="--target=armv7-unknown-freebsd13.2-gnueabihf" \ TAGS="bindata sqlite sqlite_unlock_notify" \ make SHELL='sh -x' build ``` ``` Running go generate... # runtime/cgo In file included from gcc_freebsd_amd64.c:9: In file included from /usr/include/signal.h:42: /usr/include/sys/_ucontext.h:44:2: error: unknown type name 'mcontext_t' modules/migration/schemas_bindata.go:8: running "go": exit status 1 # runtime/cgo In file included from gcc_freebsd_amd64.c:9: In file included from /usr/include/signal.h:42: /usr/include/sys/_ucontext.h:44:2: error: unknown type name 'mcontext_t' modules/options/options_bindata.go:8: running "go": exit status 1 # runtime/cgo In file included from gcc_freebsd_amd64.c:9: In file included from /usr/include/signal.h:42: /usr/include/sys/_ucontext.h:44:2: error: unknown type name 'mcontext_t' modules/public/public_bindata.go:8: running "go": exit status 1 # runtime/cgo In file included from gcc_freebsd_amd64.c:9: In file included from /usr/include/signal.h:42: /usr/include/sys/_ucontext.h:44:2: error: unknown type name 'mcontext_t' modules/templates/templates_bindata.go:8: running "go": exit status 1 gmake[1]: *** [Makefile:781: generate-go] Error 1 *** Error code 2 Stop. ``` But with this fix Gitea compiles successfully. Co-authored-by: Sergey Zolotarev <4525736+sryze@users.noreply.github.com> --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0cd6abb81..e8006e403 100644 --- a/Makefile +++ b/Makefile @@ -778,7 +778,7 @@ generate-backend: $(TAGS_PREREQ) generate-go .PHONY: generate-go generate-go: $(TAGS_PREREQ) @echo "Running go generate..." - @CC= GOOS= GOARCH= $(GO) generate -tags '$(TAGS)' ./... + @CC= GOOS= GOARCH= CGO_ENABLED=0 $(GO) generate -tags '$(TAGS)' ./... .PHONY: security-check security-check: