Repository: go
Issue: cmd/go: pass CGO_FLAGS to C compiler when compiling _cgo_main.c or gccgo
Labels: NeedsFix

I'm unable to compile `go-sqlite3` with `gccgo` for the PowerPC plataform.

### What version of Go are you using (`go version`)?

`go version go1.8.3 gccgo (Ubuntu 7.2.0-8ubuntu3) 7.2.0 linux/amd64`

### Does this issue reproduce with the latest release?

I have not tested with the latest since the above is the highest version available as pre-compiled package in Ubuntu

### What operating system and processor architecture are you using (`go env`)?

Cross-compiling to ppc with gccgo

```
GOARCH="ppc"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/go"
GORACE=""
GOROOT="/usr"
GOTOOLDIR="/usr/lib/gcc/x86_64-linux-gnu/7"
GCCGO="/usr/bin/powerpc-linux-gnuspe-gccgo"
CC="powerpc-fsl-linux-gnuspe-gcc"
GOGCCFLAGS="-m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build540675113=/tmp/go-build -gno-record-gcc-switches"
CXX="powerpc-fsl-linux-gnuspe-g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -O2 -pipe -g -feliminate-unused-debug-types"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="--sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed"
```

C compiler:

```
powerpc-fsl-linux-gnuspe-gcc --version
powerpc-fsl-linux-gnuspe-gcc (GCC) 4.9.2
```


### What did you do?

Attempt to install go-sqlite3:

`GCCGO=powerpc-linux-gnuspe-gccgo CGO_ENABLED=1 GOARCH=ppc CGO_ENABLED=1 go-7 get -x github.com/mattn/go-sqlite3`


### What did you expect to see?

Package compiling

### What did you see instead?

Compilation fails with

```
In file included from cgo-c-prolog-gccgo:1:0:
/opt/fsl/2.0/sysroots/x86_64-fslsdk-linux/usr/lib/powerpc-fsl-linux/gcc/powerpc-fsl-linux/4.9.2/include/stdint.h:9:26: fatal error: stdint.h: No such file or directory
 # include_next <stdint.h>
                          ^
compilation terminated.
```


The compiler I'm using requires some flags to work, in particular the `--sysroot` flag. This is specified in the CC variable which currently is:

```
powerpc-fsl-linux-gnuspe-gcc -m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe
```

The above works with the older `go version go1.4.2 gccgo (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609 linux/amd64`. However, with the version indicated above, the compilation fails because go does not include the flags specified in the CC variable. I managed to work around this by setting CGO_CFLAGS and CGO_LDFLAGS, as can be viewed above. However, I **still** get a compilation error, because apparently gccgo (or cgo?) is not including the CGO_CFLAGS when compiling `_cgo_defun.c` as can be viewed in the verbose output below:

```
GCCGO=powerpc-linux-gnuspe-gccgo CGO_ENABLED=1 GOARCH=ppc CGO_ENABLED=1 go-7 get -x github.com/mattn/go-sqlite3
cd .
git clone https://github.com/mattn/go-sqlite3 /go/src/github.com/mattn/go-sqlite3
cd /go/src/github.com/mattn/go-sqlite3
git submodule update --init --recursive
cd /go/src/github.com/mattn/go-sqlite3
git show-ref
cd /go/src/github.com/mattn/go-sqlite3
git submodule update --init --recursive
cd .
git clone https://go.googlesource.com/net /go/src/golang.org/x/net
cd /go/src/golang.org/x/net
git submodule update --init --recursive
cd /go/src/golang.org/x/net
git show-ref
cd /go/src/golang.org/x/net
git submodule update --init --recursive
WORK=/tmp/go-build601413480
mkdir -p $WORK/golang.org/x/net/context/_obj/
mkdir -p $WORK/golang.org/x/net/
cd /go/src/golang.org/x/net/context
/usr/bin/powerpc-linux-gnuspe-gccgo -I $WORK -c -g -fgo-pkgpath=golang.org/x/net/context -fgo-relative-import-path=_/go/src/golang.org/x/net/context -o $WORK/golang.org/x/net/context/_obj/_go_.o ./context.go ./go17.go ./pre_go19.go
ar rc $WORK/golang.org/x/net/libcontext.a $WORK/golang.org/x/net/context/_obj/_go_.o
mkdir -p /go/pkg/gccgo_linux_ppc/golang.org/x/net/
cp $WORK/golang.org/x/net/libcontext.a /go/pkg/gccgo_linux_ppc/golang.org/x/net/libcontext.a
mkdir -p $WORK/github.com/mattn/go-sqlite3/_obj/
mkdir -p $WORK/github.com/mattn/
cd /go/src/github.com/mattn/go-sqlite3
CGO_LDFLAGS="--sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe" "-Wl,-O1" "-Wl,--hash-style=gnu" "-Wl,--as-needed" "-ldl" /usr/lib/gcc/x86_64-linux-gnu/7/cgo -objdir $WORK/github.com/mattn/go-sqlite3/_obj/ -importpath github.com/mattn/go-sqlite3 -gccgo -gccgopkgpath=github.com/mattn/go-sqlite3 -- -I $WORK/github.com/mattn/go-sqlite3/_obj/ -m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -O2 -pipe -g -feliminate-unused-debug-types -std=gnu99 -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_TRACE_SIZE_LIMIT=15 -DSQLITE_DISABLE_INTRINSIC -Wno-deprecated-declarations -I. backup.go callback.go error.go sqlite3.go sqlite3_context.go sqlite3_load_extension.go sqlite3_other.go sqlite3_type.go
cd $WORK
powerpc-fsl-linux-gnuspe-gcc -fdebug-prefix-map=a=b -c trivial.c
powerpc-fsl-linux-gnuspe-gcc -gno-record-gcc-switches -c trivial.c
cd /go/src/github.com/mattn/go-sqlite3
powerpc-fsl-linux-gnuspe-gcc -I . -fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/github.com/mattn/go-sqlite3/_obj/ -m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -O2 -pipe -g -feliminate-unused-debug-types -std=gnu99 -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_TRACE_SIZE_LIMIT=15 -DSQLITE_DISABLE_INTRINSIC -Wno-deprecated-declarations -I. -o $WORK/github.com/mattn/go-sqlite3/_obj/_cgo_export.o -c $WORK/github.com/mattn/go-sqlite3/_obj/_cgo_export.c
powerpc-fsl-linux-gnuspe-gcc -I . -fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/github.com/mattn/go-sqlite3/_obj/ -m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -O2 -pipe -g -feliminate-unused-debug-types -std=gnu99 -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_TRACE_SIZE_LIMIT=15 -DSQLITE_DISABLE_INTRINSIC -Wno-deprecated-declarations -I. -o $WORK/github.com/mattn/go-sqlite3/_obj/backup.cgo2.o -c $WORK/github.com/mattn/go-sqlite3/_obj/backup.cgo2.c
powerpc-fsl-linux-gnuspe-gcc -I . -fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/github.com/mattn/go-sqlite3/_obj/ -m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -O2 -pipe -g -feliminate-unused-debug-types -std=gnu99 -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_TRACE_SIZE_LIMIT=15 -DSQLITE_DISABLE_INTRINSIC -Wno-deprecated-declarations -I. -o $WORK/github.com/mattn/go-sqlite3/_obj/callback.cgo2.o -c $WORK/github.com/mattn/go-sqlite3/_obj/callback.cgo2.c
powerpc-fsl-linux-gnuspe-gcc -I . -fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/github.com/mattn/go-sqlite3/_obj/ -m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -O2 -pipe -g -feliminate-unused-debug-types -std=gnu99 -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_TRACE_SIZE_LIMIT=15 -DSQLITE_DISABLE_INTRINSIC -Wno-deprecated-declarations -I. -o $WORK/github.com/mattn/go-sqlite3/_obj/error.cgo2.o -c $WORK/github.com/mattn/go-sqlite3/_obj/error.cgo2.c
powerpc-fsl-linux-gnuspe-gcc -I . -fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/github.com/mattn/go-sqlite3/_obj/ -m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -O2 -pipe -g -feliminate-unused-debug-types -std=gnu99 -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_TRACE_SIZE_LIMIT=15 -DSQLITE_DISABLE_INTRINSIC -Wno-deprecated-declarations -I. -o $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3.cgo2.o -c $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3.cgo2.c
powerpc-fsl-linux-gnuspe-gcc -I . -fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/github.com/mattn/go-sqlite3/_obj/ -m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -O2 -pipe -g -feliminate-unused-debug-types -std=gnu99 -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_TRACE_SIZE_LIMIT=15 -DSQLITE_DISABLE_INTRINSIC -Wno-deprecated-declarations -I. -o $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3_context.cgo2.o -c $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3_context.cgo2.c
powerpc-fsl-linux-gnuspe-gcc -I . -fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/github.com/mattn/go-sqlite3/_obj/ -m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -O2 -pipe -g -feliminate-unused-debug-types -std=gnu99 -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_TRACE_SIZE_LIMIT=15 -DSQLITE_DISABLE_INTRINSIC -Wno-deprecated-declarations -I. -o $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3_load_extension.cgo2.o -c $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3_load_extension.cgo2.c
powerpc-fsl-linux-gnuspe-gcc -I . -fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/github.com/mattn/go-sqlite3/_obj/ -m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -O2 -pipe -g -feliminate-unused-debug-types -std=gnu99 -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_TRACE_SIZE_LIMIT=15 -DSQLITE_DISABLE_INTRINSIC -Wno-deprecated-declarations -I. -o $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3_other.cgo2.o -c $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3_other.cgo2.c
powerpc-fsl-linux-gnuspe-gcc -I . -fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/github.com/mattn/go-sqlite3/_obj/ -m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -O2 -pipe -g -feliminate-unused-debug-types -std=gnu99 -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_TRACE_SIZE_LIMIT=15 -DSQLITE_DISABLE_INTRINSIC -Wno-deprecated-declarations -I. -o $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3_type.cgo2.o -c $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3_type.cgo2.c
powerpc-fsl-linux-gnuspe-gcc -I . -fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/github.com/mattn/go-sqlite3/_obj/ -m32 -mcpu=8548 -mabi=spe -mspe -mfloat-gprs=double --sysroot=/opt/fsl/2.0/sysroots/ppce500v2-fsl-linux-gnuspe -O2 -pipe -g -feliminate-unused-debug-types -std=gnu99 -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_TRACE_SIZE_LIMIT=15 -DSQLITE_DISABLE_INTRINSIC -Wno-deprecated-declarations -I. -o $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3-binding.o -c ./sqlite3-binding.c
powerpc-fsl-linux-gnuspe-gcc -Wall -g -I $WORK/github.com/mattn/go-sqlite3/_obj/ -I /usr/pkg/include -o $WORK/github.com/mattn/go-sqlite3/_obj/_cgo_defun.o -D GOOS_linux -D GOARCH_ppc -D "GOPKGPATH=\"github_com_mattn_go_sqlite3\"" -c $WORK/github.com/mattn/go-sqlite3/_obj/_cgo_defun.c
# github.com/mattn/go-sqlite3
In file included from cgo-c-prolog-gccgo:1:0:
/opt/fsl/2.0/sysroots/x86_64-fslsdk-linux/usr/lib/powerpc-fsl-linux/gcc/powerpc-fsl-linux/4.9.2/include/stdint.h:9:26: fatal error: stdint.h: No such file or directory
 # include_next <stdint.h>
                          ^
compilation terminated.
```

Observe that the last compiler invocation did not include the contents of CGO_CFLAGS, preventing `--sysroot` being passed which messes the include path and makes compilation fail.