]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: use temporary file for output of gcc command on Windows
authorYasuhiro Matsumoto <mattn.jp@gmail.com>
Thu, 14 May 2020 09:17:49 +0000 (18:17 +0900)
committerIan Lance Taylor <iant@golang.org>
Wed, 20 May 2020 21:52:17 +0000 (21:52 +0000)
On Windows, some of gcc command (like msys2 native) output NUL as a file.

Fixes #36000

Change-Id: I02c632fa2d710a011d79f24d5beee4bc57ad994e
Reviewed-on: https://go-review.googlesource.com/c/go/+/233977
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/internal/work/exec.go
src/cmd/go/testdata/script/issue36000.txt [new file with mode: 0644]

index 6ab3498c3eb6809ca9f5a2d5f3716be7198bd6de..071c9d2db98ff8c32771d3e82aee7110368bb8e0 100644 (file)
@@ -2434,13 +2434,25 @@ func (b *Builder) gccSupportsFlag(compiler []string, flag string) bool {
        if b.flagCache == nil {
                b.flagCache = make(map[[2]string]bool)
        }
+
+       tmp := os.DevNull
+       if runtime.GOOS == "windows" {
+               f, err := ioutil.TempFile(b.WorkDir, "")
+               if err != nil {
+                       return false
+               }
+               f.Close()
+               tmp = f.Name()
+               defer os.Remove(tmp)
+       }
+
        // We used to write an empty C file, but that gets complicated with
        // go build -n. We tried using a file that does not exist, but that
        // fails on systems with GCC version 4.2.1; that is the last GPLv2
        // version of GCC, so some systems have frozen on it.
        // Now we pass an empty file on stdin, which should work at least for
        // GCC and clang.
-       cmdArgs := str.StringList(compiler, flag, "-c", "-x", "c", "-", "-o", os.DevNull)
+       cmdArgs := str.StringList(compiler, flag, "-c", "-x", "c", "-", "-o", tmp)
        if cfg.BuildN || cfg.BuildX {
                b.Showcmd(b.WorkDir, "%s || true", joinUnambiguously(cmdArgs))
                if cfg.BuildN {
diff --git a/src/cmd/go/testdata/script/issue36000.txt b/src/cmd/go/testdata/script/issue36000.txt
new file mode 100644 (file)
index 0000000..4173275
--- /dev/null
@@ -0,0 +1,6 @@
+# Tests golang.org/issue/36000
+
+[!cgo] skip
+
+# go env with CGO flags should not make NUL file
+go env CGO_CFLAGS