@@ -12,6 +12,7 @@ import (
1212 "os/exec"
1313 "path/filepath"
1414 "runtime"
15+ "strings"
1516)
1617
1718const mkwinsyscallVersion = "v0.37.0"
@@ -36,7 +37,10 @@ func main() {
3637 fmt .Fprintf (flag .CommandLine .Output (), "%s\n \n " , description )
3738 }
3839 flag .Parse ()
39- goTool := filepath .Join (runtime .GOROOT (), "bin" , "go" )
40+ goTool , err := exec .LookPath ("go" )
41+ if err != nil {
42+ log .Fatal (err )
43+ }
4044
4145 listCmd := exec .Command (goTool , "list" , "-m" )
4246 listCmd .Env = append (os .Environ (), "GO111MODULE=on" )
@@ -47,8 +51,8 @@ func main() {
4751 log .Fatal ("WARNING: Please switch from using:\n go run ./cmd/mksyscall\n to using:\n go run golang.org/x/sys/windows/mkwinsyscall\n " )
4852 }
4953
50- install (goTool )
51- zsys := generateSyscalls ()
54+ mkwinsyscall := install (goTool )
55+ zsys := generateSyscalls (mkwinsyscall )
5256
5357 if * output == "" {
5458 os .Stdout .Write (zsys )
@@ -62,7 +66,7 @@ func main() {
6266
6367// install makes sure mkwinsyscall can be called by
6468// running go install golang.org/x/sys/windows/mkwinsyscall.
65- func install (goTool string ) {
69+ func install (goTool string ) string {
6670 // mkwinsyscall is hardcoded here instead of adding it to go.mod so
6771 // it doesn't appear in go.sum, which will reduce the likelihood
6872 // of having patch conflicts when vendoring go-crypto-winnative.
@@ -75,6 +79,29 @@ func install(goTool string) {
7579 if err != nil {
7680 log .Fatal (err )
7781 }
82+
83+ binDir := goEnv (goTool , "GOBIN" )
84+ if binDir == "" {
85+ goPaths := filepath .SplitList (goEnv (goTool , "GOPATH" ))
86+ if len (goPaths ) == 0 {
87+ log .Fatal ("GOPATH is empty" )
88+ }
89+ binDir = filepath .Join (goPaths [0 ], "bin" )
90+ }
91+ binary := "mkwinsyscall"
92+ if runtime .GOOS == "windows" {
93+ binary += ".exe"
94+ }
95+ return filepath .Join (binDir , binary )
96+ }
97+
98+ func goEnv (goTool , name string ) string {
99+ cmd := exec .Command (goTool , "env" , name )
100+ output , err := cmd .Output ()
101+ if err != nil {
102+ log .Fatal (err )
103+ }
104+ return strings .TrimSpace (string (output ))
78105}
79106
80107// generateSyscalls runs mkwinsyscall with GOROOT set to the current working directory.
@@ -84,15 +111,15 @@ func install(goTool string) {
84111// to avoid DLL preloading attacks. As sysdll is a std internal package, this function
85112// replaces the generated code's sysdll import with our own version located at
86113// "./internal/sysdll".
87- func generateSyscalls () []byte {
114+ func generateSyscalls (mkwinsyscall string ) []byte {
88115 wd , err := os .Getwd ()
89116 if err != nil {
90117 log .Fatal (err )
91118 }
92119 args := flag .Args ()
93120 // We have intercepted the output argument, so we can be sure
94121 // that mkwinsyscall will emit the generated file to the standard output.
95- cmd := exec .Command (" mkwinsyscall" , args ... )
122+ cmd := exec .Command (mkwinsyscall , args ... )
96123 var bout bytes.Buffer
97124 cmd .Stdout = & bout
98125 cmd .Stderr = os .Stderr
@@ -104,6 +131,7 @@ func generateSyscalls() []byte {
104131 zsys := bout .Bytes ()
105132 zsys = bytes .ReplaceAll (zsys , []byte ("\" internal/syscall/windows/sysdll\" " ), []byte ("\" github.com/microsoft/go-crypto-winnative/internal/sysdll\" " ))
106133 zsys = bytes .ReplaceAll (zsys , []byte ("windows.NTStatus" ), []byte ("NTStatus" ))
134+ zsys = bytes .ReplaceAll (zsys , []byte (".dll.dll\" " ), []byte (".dll\" " ))
107135
108136 return zsys
109137}
0 commit comments