]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: support shared libraries in 'go version' on Windows
authorqmuntal <quimmuntal@gmail.com>
Fri, 11 Mar 2022 15:55:19 +0000 (16:55 +0100)
committerGopher Robot <gobot@golang.org>
Mon, 10 Oct 2022 18:54:31 +0000 (18:54 +0000)
This change modifies 'go version' to support shared windows libraries.

Updates #48187

Change-Id: I2e8436b8df84fe76677106fa9ca02dcd1fb90e77
Reviewed-on: https://go-review.googlesource.com/c/go/+/391855
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Quim Muntal <quimmuntal@gmail.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Joedian Reid <joedian@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>

src/cmd/go/alldocs.go
src/cmd/go/internal/version/version.go
src/cmd/go/testdata/script/version.txt
src/cmd/go/testdata/script/version_cshared.txt [new file with mode: 0644]

index 57d7c4ebf780ff8730c14224500c70b059b95b4d..6a348dbb75df01b9f1bb4b135f193858741df182 100644 (file)
 //
 //     go version [-m] [-v] [file ...]
 //
-// Version prints the build information for Go executables.
+// Version prints the build information for Go binary files.
 //
-// Go version reports the Go version used to build each of the named
-// executable files.
+// Go version reports the Go version used to build each of the named files.
 //
 // If no files are named on the command line, go version prints its own
 // version information.
 // By default, go version does not report unrecognized files found
 // during a directory scan. The -v flag causes it to report unrecognized files.
 //
-// The -m flag causes go version to print each executable's embedded
+// The -m flag causes go version to print each file's embedded
 // module version information, when available. In the output, the module
 // information consists of multiple lines following the version line, each
 // indented by a leading tab character.
index 5de7b83efaea8308a274133c6eaa4588f7367f3a..6bbd48c6e673821fe02e9c2a42e506dddc6f8193 100644 (file)
@@ -22,10 +22,9 @@ import (
 var CmdVersion = &base.Command{
        UsageLine: "go version [-m] [-v] [file ...]",
        Short:     "print Go version",
-       Long: `Version prints the build information for Go executables.
+       Long: `Version prints the build information for Go binary files.
 
-Go version reports the Go version used to build each of the named
-executable files.
+Go version reports the Go version used to build each of the named files.
 
 If no files are named on the command line, go version prints its own
 version information.
@@ -35,7 +34,7 @@ looking for recognized Go binaries and reporting their versions.
 By default, go version does not report unrecognized files found
 during a directory scan. The -v flag causes it to report unrecognized files.
 
-The -m flag causes go version to print each executable's embedded
+The -m flag causes go version to print each file's embedded
 module version information, when available. In the output, the module
 information consists of multiple lines following the version line, each
 indented by a leading tab character.
@@ -92,7 +91,7 @@ func runVersion(ctx context.Context, cmd *base.Command, args []string) {
        }
 }
 
-// scanDir scans a directory for executables to run scanFile on.
+// scanDir scans a directory for binary to run scanFile on.
 func scanDir(dir string) {
        filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
                if d.Type().IsRegular() || d.Type()&fs.ModeSymlink != 0 {
@@ -109,18 +108,24 @@ func scanDir(dir string) {
        })
 }
 
-// isExe reports whether the file should be considered executable.
-func isExe(file string, info fs.FileInfo) bool {
-       if runtime.GOOS == "windows" {
-               return strings.HasSuffix(strings.ToLower(file), ".exe")
+// isGoBinaryCandidate reports whether the file is a candidate to be a Go binary.
+func isGoBinaryCandidate(file string, info fs.FileInfo) bool {
+       if info.Mode().IsRegular() && info.Mode()&0111 != 0 {
+               return true
+       }
+       name := strings.ToLower(file)
+       switch filepath.Ext(name) {
+       case ".so", ".exe", ".dll":
+               return true
+       default:
+               return strings.Contains(name, ".so.")
        }
-       return info.Mode().IsRegular() && info.Mode()&0111 != 0
 }
 
 // scanFile scans file to try to report the Go and module versions.
 // If mustPrint is true, scanFile will report any error reading file.
 // Otherwise (mustPrint is false, because scanFile is being called
-// by scanDir) scanFile prints nothing for non-Go executables.
+// by scanDir) scanFile prints nothing for non-Go binaries.
 func scanFile(file string, info fs.FileInfo, mustPrint bool) {
        if info.Mode()&fs.ModeSymlink != 0 {
                // Accept file symlinks only.
@@ -134,20 +139,20 @@ func scanFile(file string, info fs.FileInfo, mustPrint bool) {
                info = i
        }
 
-       if !isExe(file, info) {
-               if mustPrint {
-                       fmt.Fprintf(os.Stderr, "%s: not executable file\n", file)
-               }
-               return
-       }
-
        bi, err := buildinfo.ReadFile(file)
        if err != nil {
                if mustPrint {
                        if pathErr := (*os.PathError)(nil); errors.As(err, &pathErr) && filepath.Clean(pathErr.Path) == filepath.Clean(file) {
                                fmt.Fprintf(os.Stderr, "%v\n", file)
                        } else {
-                               fmt.Fprintf(os.Stderr, "%s: %v\n", file, err)
+
+                               // Skip errors for non-Go binaries.
+                               // buildinfo.ReadFile errors are not fine-grained enough
+                               // to know if the file is a Go binary or not,
+                               // so try to infer it from the file mode and extension.
+                               if isGoBinaryCandidate(file, info) {
+                                       fmt.Fprintf(os.Stderr, "%s: %v\n", file, err)
+                               }
                        }
                }
                return
index a928067a7ef92d7b2c0d5b772f61fd896fab2dc7..839aa8526c6cfd7829a5f652d6ce809a9b7f33d7 100644 (file)
@@ -9,6 +9,19 @@ stderr 'with arguments'
 ! go version -v
 stderr 'with arguments'
 
+# Check that 'go version' succeed even when it does not contain Go build info.
+# It should print an error if the file has a known Go binary extension.
+#
+go version empty.txt
+! stdout .
+! stderr .
+go version empty.exe
+stderr 'could not read Go build info'
+go version empty.so
+stderr 'could not read Go build info'
+go version empty.dll
+stderr 'could not read Go build info'
+
 # Neither of the two flags above should be an issue via GOFLAGS.
 env GOFLAGS='-m -v'
 go version
@@ -71,4 +84,9 @@ module m
 
 -- empty.go --
 package main
-func main(){}
\ No newline at end of file
+func main(){}
+
+-- empty.txt --
+-- empty.exe --
+-- empty.so --
+-- empty.dll --
diff --git a/src/cmd/go/testdata/script/version_cshared.txt b/src/cmd/go/testdata/script/version_cshared.txt
new file mode 100644 (file)
index 0000000..29e21fc
--- /dev/null
@@ -0,0 +1,19 @@
+[short] skip
+[!buildmode:c-shared] stop
+
+env GO111MODULE=on
+
+go get rsc.io/fortune
+go build -buildmode=c-shared -o external.so rsc.io/fortune
+go version external.so
+stdout '^external.so: .+'
+go version -m external.so
+stdout '^\tpath\trsc.io/fortune'
+stdout '^\tmod\trsc.io/fortune\tv1.0.0'
+
+-- go.mod --
+module m
+
+-- empty.go --
+package main
+func main(){}