golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.97k stars 17.53k forks source link

cmd/link: internal linking for GOOS=linux, -buildmode=plugin #18157

Open urso opened 7 years ago

urso commented 7 years ago

Please answer these questions before submitting your issue. Thanks!

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

(1.8beta1) go version devel +41908a5 Thu Dec 1 02:54:21 2016 +0000 darwin/amd64

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

Darwin:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/urso/.gvm/pkgsets/go1.8beta1/global"
GORACE=""
GOROOT="/Users/urso/.gvm/gos/go1.8beta1"
GOTOOLDIR="/Users/urso/.gvm/gos/go1.8beta1/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/2s/38f_b6sj0mn6_14km12vyq0r0000gn/T/go-build255645567=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

Linux:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/urso/.gvm/pkgsets/go1.8beta1/global"
GORACE=""
GOROOT="/home/urso/.gvm/gos/go1.8beta1"
GOTOOLDIR="/home/urso/.gvm/gos/go1.8beta1/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build240993252=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

Try to cross compile the plugin by setting GOOS=linux (darwin)

package main

import "fmt"

func F() { fmt.Println("Hello World!") }

func main() {}

What did you expect to see?

plugin being build

What did you see instead?

Darwin:

$ GOOS=linux go build -buildmode=plugin plugin.go
# command-line-arguments
warning: unable to find runtime/cgo.a
/Users/urso/.gvm/gos/go1.8beta1/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: unknown option: -z
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Linux:

$ GOOS=darwin go build -buildmode=plugin plugin.go
# command-line-arguments
warning: unable to find runtime/cgo.a
/home/urso/.gvm/gos/go1.8beta1/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/bin/ld: -f may not be used without -shared
collect2: error: ld returned 1 exit status
minux commented 7 years ago

Cross compiling plugins needs to use external linking, and therefore you need to have cross compiling toolchain for the target platform installed.

i.e. for the first case, you need linux toolchain installed on Darwin and for the 2nd case, you need darwin toolchain install on Linux.

bradfitz commented 7 years ago

Should the go tool emit a more helpful error message after the existing failure messages?

Or is there missing documentation?

/cc @ianlancetaylor @crawshaw

minux commented 7 years ago

Related issues:

17620: cmd/go: docs need section on cross-compiling

16395: cmd/go: provide better error message when cross-compiling toolchain

is required

maybe we can merge this one into #16395?

crawshaw commented 7 years ago

Better error handling is covered by #16395, any solution to that will work here.

There are two potential feature requests here, the first is internal linking of plugins which don't use cgo when GOOS=linux, the second is the same for GOOS=darwin.

Let's turn this issue into a feature request for GOOS=linux, -buildmode=plugin internal linking. It's not a priority for me, but it's an entirely reasonable feature request.

@urso if you want, you can file a separate bug for GOOS=darwin, -buildmode=plugin internal linking.