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: go env discards all but the first word of $CC #15457

Closed
AxbB36 opened this issue Apr 26, 2016 · 11 comments
Closed

cmd/go: go env discards all but the first word of $CC #15457

AxbB36 opened this issue Apr 26, 2016 · 11 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@AxbB36
Copy link

AxbB36 commented Apr 26, 2016

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

go1.6.2

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

linux/386

If you try to stuff extra arguments into the CC environment variable, go env will throw them away (here, bar is discarded):

go1.6.2$ CC="foo bar" ./bin/go env
...
CC="foo"
GOGCCFLAGS="-fPIC -m32 -pthread -fmessage-length=0"

Earlier releases, for example 1.4.3, also remove arguments from CC, but keep them in a separate variable, GOGCCFLAGS:

go1.4.3$ $ CC="foo bar" ./bin/go env
...
CC="foo"
GOGCCFLAGS="bar -fPIC -m32 -pthread -fmessage-length=0"

The code responsible for this shuffling lies in the mkEnv function in env.go:
https://github.com/golang/go/blob/go1.4.3/src/cmd/go/env.go#L55-L56

        env = append(env, envVar{"CC", cmd[0]})
        env = append(env, envVar{"GOGCCFLAGS", strings.Join(cmd[3:], " ")})

(Indices 1 and 2 are skipped because they are inserted by ccompilerCmd in build.go.) Note that mkEnv ignores any previous value of GOGCCFLAGS.

b38fa89 is the commit that caused the behvior to change, by calling mkEnv an extra time and overwriting environment variables with its output. Since one application of mkEnv does this:

CC ← CC[0]
GOGCCFLAGS ← CC[1:] -fPIC -m32 -pthread -fmessage-length=0

two applications have the effect of discarding CC[1:]:

CC ← CC[0]
GOGCCFLAGS ← -fPIC -m32 -pthread -fmessage-length=0

Why this matters to me: I'm cross-compiling using a specially installed GCC that needs extra -I and -L flags to find its files. (Specifically, I'm doing a cross build from linux to darwin for Tor Browser; see here for details.) I'm running this command, which worked with 1.4.3:

CGO_ENABLED=1 CC_FOR_TARGET="$CC $CFLAGS $LDFLAGS" CC= CFLAGS= LDFLAGS= ./make.bash

($CC is the cross compiler.) But because make.bash calls dist env, the necessary $CFLAGS and $LDFLAGS disappear and the build fails somewhere in cgo because the compiler cannot find errno.h.
#13377 is a related issue, particularly this comment that talks about "only using the first word of $CC." As a workaround I'm using a suggestion from that ticket: I'm making a shell script called cc-for-target that contains exec $CC $CFLAGS $LDFLAGS "$@" and invoking make.bash with CC_FOR_TARGET=cc-for-target.

@AxbB36
Copy link
Author

AxbB36 commented Apr 26, 2016

This is what I did to find that b38fa89 was the commit that changed behavior.

git bisect start
git bisect new go1.6.2
git bisect old go1.4.3
git bisect run ../check.sh

where check.sh contains:

#!/bin/sh
git clean -df
(cd src && CGO_ENABLED=0 ./make.bash) || exit 125
CC='gcc XYZ' ./bin/go env | grep XYZ
exit $?

@dominikh
Copy link
Member

See also #11685

@bradfitz bradfitz changed the title go env discards all but the first word of $CC cmd/go: go env discards all but the first word of $CC May 4, 2016
@bradfitz bradfitz added this to the Unplanned milestone May 4, 2016
@davmaz
Copy link

davmaz commented Aug 4, 2016

Ian -
Have you looked into this. It's killing my cross compile now because of this problem. Is there any work-around? I don't quite understand the one mentioned above.

Thanks -
Dave

@ianlancetaylor
Copy link
Contributor

@davmaz I have not looked into this at all. As far as I know it only affects go env output.

In any case it appears that a simple workaround for now is to store your command line in an executable shell script, and set CC to that shell script.

@davmaz
Copy link

davmaz commented Aug 4, 2016

I guess I'll have to find a way. But it does not just effect "go env", it changes the compiler calls (ignores the exported values after the first) and puts in new ones that wreck the cross-compile. I'm surprised more user haven't complained, but I understand. I'll just have to find a way around it if I want to stay current with the latest go version.

@ianlancetaylor ianlancetaylor modified the milestones: Go1.8, Unplanned Aug 4, 2016
@ianlancetaylor
Copy link
Contributor

@davmaz This issue just talks about go env. If this affects more than that, can you show the problem in detail, or open a new issue for the problem you are having? Thanks.

@davmaz
Copy link

davmaz commented Aug 8, 2016

Ian,
I think it may involve more than go env. My problem is with cross-compiling a popular C/Fortran library called OpenBLAS. Here's what I did:

I've got the OpenBLAS make to cross-compile the library by doing this:

  • Find all installed packages for the ARM compiler: sudo aptitude search arm-linux-gnueabi
  • Purge ALL gnueabi versions: sudo apt-get purge --auto-remove gfortran-arm-linux-gnueabi, etc for any "installed"
  • Install only gnueabihf versions: sudo aptitude install gfortran-arm-linux-gnueabihf (this also installs gnueabihf gcc)
  • From the OpenBLAS directory: make CC=arm-linux-gnueabihf-gcc FC=arm-linux-gnueabihf-gfortran HOSTCC=gcc TARGET=CORTEXA9
  • Install the ARM libs: sudo make PREFIX=/usr/local install

The make runs to completion and I can install the ARM OpenBLAS library. I followed similar steps to get FFTW to cross-compile if anyone is interested.

Next, installing go for cross-compiling I did this:

  • export CGO_ENABLED=1
  • export CC_FOR_TARGET=arm-linux-gnueabihf-gcc
  • export GOGCCFLAGS="-fPIC -pthread -mfpu=vfp -mfloat-abi=hard --static"
  • export GOOS=linux
  • export GOARCH=arm
  • export GOARM=7
  • bash ./all.bash
  • go env shows:
  • GOARCH="arm"
  • GOBIN=""
  • GOEXE=""
  • GOHOSTARCH="amd64"
  • GOHOSTOS="linux"
  • GOOS="linux"
  • GOPATH="/opt/Projects/Golang"
  • GORACE=""
  • GOROOT="/usr/local/go"
  • GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
  • CC="arm-linux-gnueabihf-gcc"
  • GOGCCFLAGS="-fPIC -marm -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build149148529=/tmp/go-build -gno-record-gcc-switches"
  • CXX="g++"
  • CGO_ENABLED="1"

Notice the GOGCCFLAGS get mangled (see golang go issues 8161). I can work around that by specifying GOGCCFLAGS on the command line:
GOOS=linux GOARCH=arm GOARM=7 GOGCCFLAGS="-fPIC -pthread -mfpu=vfp -mfloat-abi=hard --static" CC=arm-linux-gnueabihf-gcc CGO_LDFLAGS="-L/usr/local/lib -lopenblas " go build -x rand_zgeev.go

However, when I copy this to the arm system, it does not run:
./rand_zgeev
-sh: ./rand_zgeev: not found

Any ideas why this won't run?


From: Ian Lance Taylor [notifications@github.com]
Sent: Thursday, August 04, 2016 18:51
To: golang/go
Cc: davem@ltsnet.net; Mention
Subject: Re: [golang/go] cmd/go: go env discards all but the first word of $CC (#15457)

@davmazhttps://github.com/davmaz This issue just talks about go env. If this affects more than that, can you show the problem in detail, or open a new issue for the problem you are having? Thanks.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com//issues/15457#issuecomment-237707818, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAgSBmJOyHpR6JZxYalVP8M2gg97nHi4ks5qcmzxgaJpZM4IQdm1.

@ianlancetaylor
Copy link
Contributor

@davmaz Let's please take that part of the conversation to a forum (see https://golang.org/wiki/Questions) or a separate issue, rather than complicating this issue. Thanks.

@davmaz
Copy link

davmaz commented Aug 8, 2016

Sorry Ian. I didn't know how to get back there. Thanks for the link.


From: Ian Lance Taylor [notifications@github.com]
Sent: Monday, August 08, 2016 16:20
To: golang/go
Cc: davem@ltsnet.net; Mention
Subject: Re: [golang/go] cmd/go: go env discards all but the first word of $CC (#15457)

@davmazhttps://github.com/davmaz Let's please take that part of the conversation to a forum (see https://golang.org/wiki/Questions) or a separate issue, rather than complicating this issue. Thanks.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com//issues/15457#issuecomment-238363658, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAgSBpYXKQHp9ISi2khqg2Q9PSqJZSa3ks5qd4-VgaJpZM4IQdm1.

arlolra pushed a commit to arlolra/tor-browser-bundle that referenced this issue Aug 13, 2016
It now requires an installation of go 1.4.x to bootstrap the build.

We set CGO_ENABLED=0 when building the bootstrap go compiler because go
1.4.3 won't build with a newer GNU toolchain:
golang/go#13114. It didn't cause a problem for
me on Ubuntu 14.04, but this will prevent it from breaking in the
future. We don't need cgo in the bootstrap compiler.

The other change is that the go build system no longer allows us to pass
a command with arguments for CC_FOR_TARGET. I opened a ticket for it:
golang/go#15457. We need -I and -L arguments
in the mac build so that gcc gan find its headers and libraries. So we
wrap up the arguments in a shell script and use the shell script as
CC_FOR_TARGET.
arlolra pushed a commit to arlolra/tor-browser-bundle that referenced this issue Aug 13, 2016
It now requires an installation of go 1.4.x to bootstrap the build.

We set CGO_ENABLED=0 when building the bootstrap go compiler because go
1.4.3 won't build with a newer GNU toolchain:
golang/go#13114. It didn't cause a problem for
me on Ubuntu 14.04, but this will prevent it from breaking in the
future. We don't need cgo in the bootstrap compiler.

The other change is that the go build system no longer allows us to pass
a command with arguments for CC_FOR_TARGET. I opened a ticket for it:
golang/go#15457. We need -I and -L arguments
in the mac build so that gcc gan find its headers and libraries. So we
wrap up the arguments in a shell script and use the shell script as
CC_FOR_TARGET.
@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 6, 2016
@rsc
Copy link
Contributor

rsc commented Oct 24, 2016

/cc @quentinmit

@gopherbot
Copy link

CL https://golang.org/cl/33293 mentions this issue.

boklm pushed a commit to boklm/tor-browser-bundle that referenced this issue Apr 22, 2017
Remove workaround for fixed upstream bug, GitHub #17732
(__MAC_OS_X_VERSION_MAX_ALLOWED).

I wasn't able to remove the cc-for-target workaround for GitHub #15457
(CC_FOR_TARGET). Even though according to
golang/go#15457 it has been
been fixed, make.bash still seems to lose all but the first argument of
CC_FOR_TARGET somewhere.
boklm added a commit to boklm/tor-browser-build that referenced this issue Jun 2, 2017
Remove workaround for fixed upstream bug, GitHub #17732
(__MAC_OS_X_VERSION_MAX_ALLOWED).

I wasn't able to remove the cc-for-target workaround for GitHub #15457
(CC_FOR_TARGET). Even though according to
golang/go#15457 it has been
been fixed, make.bash still seems to lose all but the first argument of
CC_FOR_TARGET somewhere.

tor-browser-bundle.git author: David Fifield <david@bamsoftware.com>
tor-browser-bundle.git commit: 9d546f20ae711e7df9c574d9bdfccf34ddf1b650
@golang golang locked and limited conversation to collaborators Nov 22, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

8 participants