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/link: cannot cross compile a linux plugin on macOS #22462

Open
ash2k opened this Issue Oct 27, 2017 · 15 comments

Comments

Projects
None yet
8 participants
@ash2k

ash2k commented Oct 27, 2017

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

go version go1.9.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

macOS Sierra 10.12.6 with latest Xcode 9.0.1 and Command Line Tools installed.

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/mmazurskiy/gopath"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9.2/libexec/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/fh/7zp6kdcj3rgdd_7_nmw439_st0zyrt/T/go-build968645272=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

package main

func Process() {
}
GOOS=linux go build -buildmode=plugin -i -v -o build/bin/p1 github.com/atlassian/smith/cmd/p1
github.com/atlassian/smith/cmd/p1
# github.com/atlassian/smith/cmd/p1
warning: unable to find runtime/cgo.a
/usr/local/Cellar/go/1.9.2/libexec/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)
GOOS=linux go build -buildmode=plugin -i -v -ldflags="-v" -o build/bin/p1 github.com/atlassian/smith/cmd/p1
github.com/atlassian/smith/cmd/p1
# github.com/atlassian/smith/cmd/p1
HEADER = -H4 -T0x401000 -D0x0 -R0x1000
searching for runtime.a in $WORK/runtime.a
searching for runtime.a in /usr/local/Cellar/go/1.9.2/libexec/pkg/linux_amd64_dynlink/runtime.a
searching for runtime/cgo.a in $WORK/runtime/cgo.a
searching for runtime/cgo.a in /usr/local/Cellar/go/1.9.2/libexec/pkg/linux_amd64_dynlink/runtime/cgo.a
warning: unable to find runtime/cgo.a
 0.00 deadcode
 0.01 pclntab=165547 bytes, funcdata total 31135 bytes
 0.01 dodata
 0.01 dwarf
 0.01 symsize = 0
 0.02 reloc
 0.02 asmb
 0.02 codeblk
 0.02 rodatblk
 0.02 datblk
 0.02 sym
 0.02 symsize = 22368
 0.02 symsize = 102000
 0.02 dwarf
 0.03 headr
 0.03 host link: "clang" "-m64" "-gdwarf-2" "-Wl,-z,relro" "-shared" "-Wl,-znow" "-Wl,-znocopyreloc" "-o" "/var/folders/fh/7zp6kdcj3rgdd_7_nmw439_st0zyrt/T/go-build232503875/github.com/atlassian/smith/cmd/p1/_obj/exe/a.out.so" "-rdynamic" "-Qunused-arguments" "/var/folders/fh/7zp6kdcj3rgdd_7_nmw439_st0zyrt/T/go-link-110686925/go.o"
/usr/local/Cellar/go/1.9.2/libexec/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)

With explicit CGO_ENABLED=1 I get:

CGO_ENABLED=1 GOOS=linux go build -buildmode=plugin -i -v -ldflags="-v" -o build/bin/p1 github.com/atlassian/smith/cmd/p1
runtime/cgo
# runtime/cgo
ld: unknown option: --build-id=none
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What did you expect to see?

Successfully compiled plugin.

What did you see instead?

Failures.

@forskning

This comment has been minimized.

forskning commented Oct 27, 2017

@bronze1man

This comment has been minimized.

bronze1man commented Oct 27, 2017

@forskning

I can cross compile a linux exe with cgo on macOS with go1.9 and gcc 7.2.0.

go version:

go version go1.9 darwin/amd64

gcc version:

> /usr/local/x86_64-ubuntu14.04-linux-gnu/bin/x86_64-ubuntu14.04-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=/usr/local/x86_64-ubuntu14.04-linux-gnu/bin/x86_64-ubuntu14.04-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/usr/local/x86_64-ubuntu14.04-linux-gnu/bin/../libexec/gcc/x86_64-ubuntu14.04-linux-gnu/7.2.0/lto-wrapper
Target: x86_64-ubuntu14.04-linux-gnu
Configured with: /Volumes/ct-ng/build_dir/crosstool-ng/.build/x86_64-ubuntu14.04-linux-gnu/src/gcc/configure --build=x86_64-build_apple-darwin16.7.0 --host=x86_64-build_apple-darwin16.7.0 --target=x86_64-ubuntu14.04-linux-gnu --prefix=/Volumes/ct-ng/ct-ng/x86_64-ubuntu14.04-linux-gnu --with-sysroot=/Volumes/ct-ng/ct-ng/x86_64-ubuntu14.04-linux-gnu/x86_64-ubuntu14.04-linux-gnu/sysroot --enable-languages=c,c++ --with-pkgversion='crosstool-NG crosstool-ng-1.23.0-220-g2ebbd3e3' --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --disable-libsanitizer --enable-libmpx --with-gmp=/Volumes/ct-ng/build_dir/crosstool-ng/.build/x86_64-ubuntu14.04-linux-gnu/buildtools --with-mpfr=/Volumes/ct-ng/build_dir/crosstool-ng/.build/x86_64-ubuntu14.04-linux-gnu/buildtools --with-mpc=/Volumes/ct-ng/build_dir/crosstool-ng/.build/x86_64-ubuntu14.04-linux-gnu/buildtools --with-isl=/Volumes/ct-ng/build_dir/crosstool-ng/.build/x86_64-ubuntu14.04-linux-gnu/buildtools --enable-lto --enable-threads=posix --enable-target-optspace --disable-plugin --disable-nls --disable-multilib --with-local-prefix=/Volumes/ct-ng/ct-ng/x86_64-ubuntu14.04-linux-gnu/x86_64-ubuntu14.04-linux-gnu/sysroot --enable-long-long
Thread model: posix
gcc version 7.2.0 (crosstool-NG crosstool-ng-1.23.0-220-g2ebbd3e3)
@forskning

This comment has been minimized.

forskning commented Oct 27, 2017

The 10-10-15 dated comment by T. de Zeeuw in the link I provided seems to indicate a failed attempt to utilise a gcc compiled for darwin, rather than following I.L. Taylor's suggestion in an earlier comment to that issue, to utilise a "Darwin -> GNU/Linux cross-compiler".

@forskning

This comment has been minimized.

forskning commented Oct 27, 2017

https://github.com/golang/go/wiki/WindowsCrossCompiling

Perhaps similar as A. Donizetti's above wiki page it would be of interest to the community to have a "LinuxCrossCompiling" wiki page.

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Oct 27, 2017

You need to have a cross-compiler.

I guess I'll leave this issue open for documentation.

@ianlancetaylor ianlancetaylor added this to the Go1.10 milestone Oct 27, 2017

@forskning

This comment has been minimized.

forskning commented Oct 27, 2017

CC @ALTree

@ALTree

This comment has been minimized.

Member

ALTree commented Oct 27, 2017

@forskning I'm not really the author of that wikipage, I just happen to be the last person who edited it : )

That said, I agree we could use a {Linux or OsX}CrossCompiling wiki page, since it's something that apparently trips a lot of people and issues about this always end up with someone writing the usual "you need a cross-compiling toolchain to do this." We could write down the procedure nicely once, and then just link the wiki page.

@ALTree ALTree added the help wanted label Oct 27, 2017

@forskning

This comment has been minimized.

forskning commented Oct 27, 2017

FWIW, possibly #22041 could also use a HelpWanted label.

@bronze1man

This comment has been minimized.

bronze1man commented Oct 28, 2017

Is it possible to just create a golang zip for macos/linux/windows that include a cross compile gcc for macos/linux/windows ?
So that people will find that cross compile cgo just work,I do not want to install a lot of stuff to just make cross compile works.Just give me the working binary.
I think the zip size is not a problem.The package for unity 3d is more than 5GB.

@davecheney

This comment has been minimized.

Contributor

davecheney commented Oct 28, 2017

@forskning

This comment has been minimized.

forskning commented Oct 28, 2017

https://forum.golangbridge.org/t/go-mobile-commands/7002

There's a recent somewhat lengthy series of topics on golangbridge.org.

Specifically pertaining to the subrepo mobile and external software; an install of Java8, Android Studio, and NDK, where just for the Android Studio/Android SDK there is the minimum requirement of an allocation of 2GB of free harddrive space.

I think its worth noting here the comment of I.L.T. to (the now Abandoned) https://golang.org/cl/69430, inferring that in regards to that proposed change in the golang.org introductory documentation, that installation of a cross-compiler was more intermediate or advanced level coding.

With host cc options not being set and a message output to that effect this might likewise infer that the coder was at an intermediate or advanced level, in order for that coder to interpret the shell message.

https://forum.golangbridge.org/t/cannot-find-package-golang-org-x-tools-cmd-goimports/7000

With all due respect I like S. Binet's comment near the closing of a separate golangbridge.org
topic.

"it’s left as an exercize for the reader"

That is, with the documentation available, it's left to the reader to be able to locate that information.

But, I note the drawing of R. French on golang.org/help.

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Oct 28, 2017

Why don’t we just make the Go tool refuse to operate if it is in cross compilation mode and cgo is enabled and the host cc options are not set?

@davecheney works for me. Thanks.

@forskning

This comment has been minimized.

forskning commented Oct 29, 2017

@ash2k Do you ever have a need to build windows go programs on macOS?

The FrozenDueToAge #12888 having mentioned cross-compiling to Windows.

@mrhoric

This comment has been minimized.

mrhoric commented Nov 11, 2017

@forskning As the issue #12888 saying:

In order to use cgo when cross-compiling, you need to set the CC environment variable to a Darwin -> GNU/Linux cross-compiler. You are using a Darwin native compiler, which can not work. Even if the build succeeded, the resulting code would not run on GNU/Lin

How do i get the Darwin -> GNU/Linux cross-compiler ? thx!

@forskning

This comment has been minimized.

forskning commented Nov 11, 2017

@mrhoric the last line of the shell output from #22462 (comment) indicates that crosstool-NG was utilised

@rsc rsc modified the milestones: Go1.10, Go1.11 Nov 22, 2017

@ianlancetaylor ianlancetaylor modified the milestones: Go1.11, Unplanned Jun 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment