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: CGO_CFLAGS variable with spaces is parsed incorrectly on windows #45637

Open
justinfx opened this issue Apr 19, 2021 · 2 comments
Open
Labels
help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@justinfx
Copy link

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

$ go version
go version go1.16.2 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using?

windows10, amd64, intel

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\justin\AppData\Local\go-build
set GOENV=C:\Users\justin\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\justin\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\justin\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.2
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\justin\AppData\Local\Temp\go-build995163971=/tmp/go-build -gno-record-gcc-switches

What did you do?

While attempting to build a cgo project on windows, I am finding that the value CGO_CFLAGS is parsed incorrectly if an include path contains spaces.

.
├── go.mod
├── main.c
└── main.go
// main.go
package main

import "C"

func main() {}
// main.c
// <empty>
> set CGO_CFLAGS='-IC:/Program Files/Foo'
> go env CGO_CFLAGS
'-IC:/Program Files/Foo'

> go build -x
WORK=C:\Users\justin\AppData\Local\Temp\go-build1942282733
mkdir -p $WORK\b003\
cd C:\Program Files\Go\src\runtime\cgo
TERM='dumb' CGO_LDFLAGS='"-g" "-O2"' "C:\\Program Files\\Go\\pkg\\tool\\windows_amd64\\cgo.exe" -objdir "$WORK\\b003\\" -importpath runtime/cgo -import_runtime_cgo=false -import_syscall=false -- -I "$WORK\\b003\\" '-IC:/Program Files/Foo' -Wall -Werror "C:\\Program Files\\Go\\src\\runtime\\cgo\\cgo.go"
# runtime/cgo
gcc: error: '-IC:/Program: Invalid argument
gcc: error: Files/Foo': No such file or directory

I've also tried various combinations of quoting and escaping with no change in result:

set CGO_CFLAGS=-I'C:/Program Files/Foo'
set CGO_CFLAGS='-IC:/Program\ Files/Foo'
set CGO_CFLAGS='-IC:/Program\\ Files/Foo'

What did you expect to see?

I would expect the full single string value of "C:/Program Files/Foo" to be passed to the "-I" include flag when compiling.

What did you see instead?

The path is split on spaces, acting like multiple arguments to gcc

# runtime/cgo
gcc: error: '-IC:/Program: Invalid argument
gcc: error: Files/Foo': No such file or directory
@justinfx
Copy link
Author

Similar to #41400

@ianlancetaylor ianlancetaylor changed the title runtime/cgo: CGO_CFLAGS variable with spaces is parsed incorrectly on windows cmd/go: CGO_CFLAGS variable with spaces is parsed incorrectly on windows Apr 19, 2021
@ianlancetaylor ianlancetaylor added help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Apr 19, 2021
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Apr 19, 2021
@networkimprov
Copy link

cc @alexbrainman @mattn @zx2c4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants