@@ -308,25 +308,8 @@ func (spec *TargetSpec) OpenOCDConfiguration() (args []string, err error) {
308
308
// getGorootVersion returns the major and minor version for a given GOROOT path.
309
309
// If the goroot cannot be determined, (0, 0) is returned.
310
310
func getGorootVersion (goroot string ) (major , minor int , err error ) {
311
- var s string
312
- var n int
313
- var trailing string
314
-
315
- if data , err := ioutil .ReadFile (filepath .Join (
316
- goroot , "src" , "runtime" , "internal" , "sys" , "zversion.go" )); err == nil {
317
-
318
- r := regexp .MustCompile ("const TheVersion = `(.*)`" )
319
- matches := r .FindSubmatch (data )
320
- if len (matches ) != 2 {
321
- return 0 , 0 , errors .New ("Invalid go version output:\n " + string (data ))
322
- }
323
-
324
- s = string (matches [1 ])
325
-
326
- } else if data , err := ioutil .ReadFile (filepath .Join (goroot , "VERSION" )); err == nil {
327
- s = string (data )
328
-
329
- } else {
311
+ s , err := getGorootVersionString (goroot )
312
+ if err != nil {
330
313
return 0 , 0 , err
331
314
}
332
315
@@ -340,7 +323,8 @@ func getGorootVersion(goroot string) (major, minor int, err error) {
340
323
}
341
324
342
325
// Ignore the errors, we don't really handle errors here anyway.
343
- n , err = fmt .Sscanf (s , "go%d.%d%s" , & major , & minor , & trailing )
326
+ var trailing string
327
+ n , err := fmt .Sscanf (s , "go%d.%d%s" , & major , & minor , & trailing )
344
328
if n == 2 && err == io .EOF {
345
329
// Means there were no trailing characters (i.e., not an alpha/beta)
346
330
err = nil
@@ -351,6 +335,29 @@ func getGorootVersion(goroot string) (major, minor int, err error) {
351
335
return
352
336
}
353
337
338
+ // getGorootVersionString returns the version string as reported by the Go
339
+ // toolchain for the given GOROOT path. It is usually of the form `go1.x.y` but
340
+ // can have some variations (for beta releases, for example).
341
+ func getGorootVersionString (goroot string ) (string , error ) {
342
+ if data , err := ioutil .ReadFile (filepath .Join (
343
+ goroot , "src" , "runtime" , "internal" , "sys" , "zversion.go" )); err == nil {
344
+
345
+ r := regexp .MustCompile ("const TheVersion = `(.*)`" )
346
+ matches := r .FindSubmatch (data )
347
+ if len (matches ) != 2 {
348
+ return "" , errors .New ("Invalid go version output:\n " + string (data ))
349
+ }
350
+
351
+ return string (matches [1 ]), nil
352
+
353
+ } else if data , err := ioutil .ReadFile (filepath .Join (goroot , "VERSION" )); err == nil {
354
+ return string (data ), nil
355
+
356
+ } else {
357
+ return "" , err
358
+ }
359
+ }
360
+
354
361
// getClangHeaderPath returns the path to the built-in Clang headers. It tries
355
362
// multiple locations, which should make it find the directory when installed in
356
363
// various ways.
0 commit comments