Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: document system setup for cross compiling between linux architectures #47175

Open
Link512 opened this issue Jul 13, 2021 · 5 comments
Open
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@Link512
Copy link

Link512 commented Jul 13, 2021

What version of Go are you using (go version)?

$ go version
1.16.6

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_arm64"
GOVCS=""
GOVERSION="go1.16.6"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1439812787=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Trying to cross compile a simple program that uses github.com/mattn/go-sqlite3 (which requires CGO) from linux/arm64 to linux/amd64 using docker on an M1 macbook, fails due to an incorrect flag passed to gcc

Some extra info:

Host OS: macOS 11.4
Host processor: Apple M1
Docker desktop version: 3.5.2 (66501)
Docker engine version: 20.10.7
gcc version: gcc (Alpine 10.3.1_git20210424) 10.3.1 20210424

The program

package main

import (
	"database/sql"

	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db, err := sql.Open("sqlite3", "tmp.sqlite3")
	if err != nil {
		panic(err)
	}
	_, err = db.Exec("SELECT 1")
	if err != nil {
		panic(err)
	}
}

The dockerfile

FROM golang:1.16.6-alpine3.14

WORKDIR /app

RUN apk --no-cache add gcc libc-dev

COPY . .
RUN go mod download

RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -o main .

What did you expect to see?

The compilation succeed

What did you see instead?

[6/6] RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -o main .:
#10 0.294 # runtime/cgo
#10 0.294 gcc: error: unrecognized command-line option '-m64'

@bcmills
Copy link
Contributor

bcmills commented Jul 13, 2021

@cherrymui, do you have any insight on building amd64 code from an M1?

@golang/release, my understanding is that we have a native arm64 builder for macOS — should we add a builder (perhaps just a misc-compile builder?) to test building for amd64 on arm64 hardware?

@bcmills bcmills changed the title Invalid gcc flag when cross compiling from linux/arm64 for linux/amd64 cmd/go: Invalid gcc flag when cross compiling from linux/arm64 for linux/amd64 Jul 13, 2021
@cherrymui
Copy link
Member

@cherrymui, do you have any insight on building amd64 code from an M1?

I don't think this is related to M1. The OP is using a linux-arm64 container.

@Link512 for cross-compiling, I think you need to use a cross C compiler that targets linux/amd64. Is your gcc a cross compiler? Thanks.

@bcmills bcmills added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Jul 13, 2021
@Link512
Copy link
Author

Link512 commented Jul 13, 2021

@Link512 for cross-compiling, I think you need to use a cross C compiler that targets linux/amd64. Is your gcc a cross compiler? Thanks.

Good question, I believe that is the underlying issue. The default arm64 gcc installed by alpine linux seems to not support amd64 cross compilation.

@bcmills bcmills removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 13, 2021
@bcmills
Copy link
Contributor

bcmills commented Jul 13, 2021

I'm inclined to say that this is a documentation issue: we probably ought to have instructions for setting up a cross-compiling environment on Linux. (I see https://golang.org/wiki/GccgoCrossCompilation and https://golang.org/wiki/WindowsCrossCompiling, but only third-party resources for cross-compiling from one Linux arch to another.)

@bcmills bcmills changed the title cmd/go: Invalid gcc flag when cross compiling from linux/arm64 for linux/amd64 cmd/go: document system setup for cross compiling between linux architectures Jul 13, 2021
@bcmills bcmills added Documentation NeedsFix The path to resolution is known, but the work has not been done. labels Jul 13, 2021
@bcmills bcmills added this to the Backlog milestone Jul 13, 2021
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 13, 2021
@ALTree
Copy link
Member

ALTree commented Jul 13, 2021

This is mentioned in the cgo documentation:

When cross-compiling, you must specify a C cross-compiler for cgo to use. You can do this by setting the generic CC_FOR_TARGET or the more specific CC_FOR_${GOOS}_${GOARCH} (for example, CC_FOR_linux_arm) environment variable when building the toolchain using make.bash, or you can set the CC environment variable any time you run the go tool.

But I guess a general wiki page on cross-compilation wouldn't hurt, since many people probably wouldn't think to read the cgo doc page before trying to cross-compile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants