|
4 | 4 |
|
5 | 5 | // +build ignore
|
6 | 6 |
|
| 7 | +// mksyscall_windows wraps golang.org/x/sys/windows/mkwinsyscall. |
7 | 8 | package main
|
8 | 9 |
|
9 | 10 | import (
|
| 11 | + "bytes" |
10 | 12 | "os"
|
11 | 13 | "os/exec"
|
12 | 14 | "path/filepath"
|
13 | 15 | "runtime"
|
14 | 16 | )
|
15 | 17 |
|
16 | 18 | func main() {
|
17 |
| - os.Stderr.WriteString("WARNING: Please switch from using:\n go run $GOROOT/src/syscall/mksyscall_windows.go\nto using:\n go run golang.org/x/sys/windows/mkwinsyscall\n") |
18 |
| - args := append([]string{"run", "golang.org/x/sys/windows/mkwinsyscall"}, os.Args[1:]...) |
19 |
| - cmd := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), args...) |
| 19 | + goTool := filepath.Join(runtime.GOROOT(), "bin", "go") |
| 20 | + |
| 21 | + listCmd := exec.Command(goTool, "list", "-m") |
| 22 | + listCmd.Env = append(os.Environ(), "GO111MODULE=on") |
| 23 | + |
| 24 | + var ( |
| 25 | + cmdEnv []string |
| 26 | + modArgs []string |
| 27 | + ) |
| 28 | + if out, err := listCmd.Output(); err == nil && string(bytes.TrimSpace(out)) == "std" { |
| 29 | + // Force module mode to use mkwinsyscall at the same version as the x/sys |
| 30 | + // module vendored into the standard library. |
| 31 | + cmdEnv = append(os.Environ(), "GO111MODULE=on") |
| 32 | + |
| 33 | + // Force -mod=readonly instead of the default -mod=vendor. |
| 34 | + // |
| 35 | + // mkwinsyscall is not itself vendored into the standard library, and it is |
| 36 | + // not feasible to do so at the moment: std-vendored libraries are included |
| 37 | + // in the "std" meta-pattern (because in general they *are* linked into |
| 38 | + // users binaries separately from the original import paths), and we can't |
| 39 | + // allow a binary in the "std" meta-pattern. |
| 40 | + modArgs = []string{"-mod=readonly"} |
| 41 | + } else { |
| 42 | + // Nobody outside the standard library should be using this wrapper: other |
| 43 | + // modules can vendor in the mkwinsyscall tool directly (as described in |
| 44 | + // https://golang.org/issue/25922), so they don't need this wrapper to |
| 45 | + // set module mode and -mod=readonly explicitly. |
| 46 | + os.Stderr.WriteString("WARNING: Please switch from using:\n go run $GOROOT/src/syscall/mksyscall_windows.go\nto using:\n go run golang.org/x/sys/windows/mkwinsyscall\n") |
| 47 | + } |
| 48 | + |
| 49 | + args := append([]string{"run"}, modArgs...) |
| 50 | + args = append(args, "golang.org/x/sys/windows/mkwinsyscall") |
| 51 | + args = append(args, os.Args[1:]...) |
| 52 | + cmd := exec.Command(goTool, args...) |
20 | 53 | cmd.Stdout = os.Stdout
|
21 | 54 | cmd.Stderr = os.Stderr
|
| 55 | + cmd.Env = cmdEnv |
22 | 56 | err := cmd.Run()
|
23 | 57 | if err != nil {
|
24 | 58 | os.Exit(1)
|
|
0 commit comments