github.com/flyinox/gosm@v0.0.0-20171117061539-16768cb62077/src/go/build/build.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package build 6 7 import ( 8 "bytes" 9 "errors" 10 "fmt" 11 "go/ast" 12 "go/doc" 13 "go/parser" 14 "go/token" 15 "io" 16 "io/ioutil" 17 "log" 18 "os" 19 pathpkg "path" 20 "path/filepath" 21 "runtime" 22 "sort" 23 "strconv" 24 "strings" 25 "unicode" 26 "unicode/utf8" 27 ) 28 29 // A Context specifies the supporting context for a build. 30 type Context struct { 31 GOARCH string // target architecture 32 GOOS string // target operating system 33 GOROOT string // Go root 34 GOPATH string // Go path 35 CgoEnabled bool // whether cgo can be used 36 UseAllFiles bool // use files regardless of +build lines, file names 37 Compiler string // compiler to assume when computing target paths 38 39 // The build and release tags specify build constraints 40 // that should be considered satisfied when processing +build lines. 41 // Clients creating a new context may customize BuildTags, which 42 // defaults to empty, but it is usually an error to customize ReleaseTags, 43 // which defaults to the list of Go releases the current release is compatible with. 44 // In addition to the BuildTags and ReleaseTags, build constraints 45 // consider the values of GOARCH and GOOS as satisfied tags. 46 BuildTags []string 47 ReleaseTags []string 48 49 // The install suffix specifies a suffix to use in the name of the installation 50 // directory. By default it is empty, but custom builds that need to keep 51 // their outputs separate can set InstallSuffix to do so. For example, when 52 // using the race detector, the go command uses InstallSuffix = "race", so 53 // that on a Linux/386 system, packages are written to a directory named 54 // "linux_386_race" instead of the usual "linux_386". 55 InstallSuffix string 56 57 // By default, Import uses the operating system's file system calls 58 // to read directories and files. To read from other sources, 59 // callers can set the following functions. They all have default 60 // behaviors that use the local file system, so clients need only set 61 // the functions whose behaviors they wish to change. 62 63 // JoinPath joins the sequence of path fragments into a single path. 64 // If JoinPath is nil, Import uses filepath.Join. 65 JoinPath func(elem ...string) string 66 67 // SplitPathList splits the path list into a slice of individual paths. 68 // If SplitPathList is nil, Import uses filepath.SplitList. 69 SplitPathList func(list string) []string 70 71 // IsAbsPath reports whether path is an absolute path. 72 // If IsAbsPath is nil, Import uses filepath.IsAbs. 73 IsAbsPath func(path string) bool 74 75 // IsDir reports whether the path names a directory. 76 // If IsDir is nil, Import calls os.Stat and uses the result's IsDir method. 77 IsDir func(path string) bool 78 79 // HasSubdir reports whether dir is lexically a subdirectory of 80 // root, perhaps multiple levels below. It does not try to check 81 // whether dir exists. 82 // If so, HasSubdir sets rel to a slash-separated path that 83 // can be joined to root to produce a path equivalent to dir. 84 // If HasSubdir is nil, Import uses an implementation built on 85 // filepath.EvalSymlinks. 86 HasSubdir func(root, dir string) (rel string, ok bool) 87 88 // ReadDir returns a slice of os.FileInfo, sorted by Name, 89 // describing the content of the named directory. 90 // If ReadDir is nil, Import uses ioutil.ReadDir. 91 ReadDir func(dir string) ([]os.FileInfo, error) 92 93 // OpenFile opens a file (not a directory) for reading. 94 // If OpenFile is nil, Import uses os.Open. 95 OpenFile func(path string) (io.ReadCloser, error) 96 } 97 98 // joinPath calls ctxt.JoinPath (if not nil) or else filepath.Join. 99 func (ctxt *Context) joinPath(elem ...string) string { 100 if f := ctxt.JoinPath; f != nil { 101 return f(elem...) 102 } 103 return filepath.Join(elem...) 104 } 105 106 // splitPathList calls ctxt.SplitPathList (if not nil) or else filepath.SplitList. 107 func (ctxt *Context) splitPathList(s string) []string { 108 if f := ctxt.SplitPathList; f != nil { 109 return f(s) 110 } 111 return filepath.SplitList(s) 112 } 113 114 // isAbsPath calls ctxt.IsAbsPath (if not nil) or else filepath.IsAbs. 115 func (ctxt *Context) isAbsPath(path string) bool { 116 if f := ctxt.IsAbsPath; f != nil { 117 return f(path) 118 } 119 return filepath.IsAbs(path) 120 } 121 122 // isDir calls ctxt.IsDir (if not nil) or else uses os.Stat. 123 func (ctxt *Context) isDir(path string) bool { 124 if f := ctxt.IsDir; f != nil { 125 return f(path) 126 } 127 fi, err := os.Stat(path) 128 return err == nil && fi.IsDir() 129 } 130 131 // hasSubdir calls ctxt.HasSubdir (if not nil) or else uses 132 // the local file system to answer the question. 133 func (ctxt *Context) hasSubdir(root, dir string) (rel string, ok bool) { 134 if f := ctxt.HasSubdir; f != nil { 135 return f(root, dir) 136 } 137 138 // Try using paths we received. 139 if rel, ok = hasSubdir(root, dir); ok { 140 return 141 } 142 143 // Try expanding symlinks and comparing 144 // expanded against unexpanded and 145 // expanded against expanded. 146 rootSym, _ := filepath.EvalSymlinks(root) 147 dirSym, _ := filepath.EvalSymlinks(dir) 148 149 if rel, ok = hasSubdir(rootSym, dir); ok { 150 return 151 } 152 if rel, ok = hasSubdir(root, dirSym); ok { 153 return 154 } 155 return hasSubdir(rootSym, dirSym) 156 } 157 158 // hasSubdir reports if dir is within root by performing lexical analysis only. 159 func hasSubdir(root, dir string) (rel string, ok bool) { 160 const sep = string(filepath.Separator) 161 root = filepath.Clean(root) 162 if !strings.HasSuffix(root, sep) { 163 root += sep 164 } 165 dir = filepath.Clean(dir) 166 if !strings.HasPrefix(dir, root) { 167 return "", false 168 } 169 return filepath.ToSlash(dir[len(root):]), true 170 } 171 172 // readDir calls ctxt.ReadDir (if not nil) or else ioutil.ReadDir. 173 func (ctxt *Context) readDir(path string) ([]os.FileInfo, error) { 174 if f := ctxt.ReadDir; f != nil { 175 return f(path) 176 } 177 return ioutil.ReadDir(path) 178 } 179 180 // openFile calls ctxt.OpenFile (if not nil) or else os.Open. 181 func (ctxt *Context) openFile(path string) (io.ReadCloser, error) { 182 if fn := ctxt.OpenFile; fn != nil { 183 return fn(path) 184 } 185 186 f, err := os.Open(path) 187 if err != nil { 188 return nil, err // nil interface 189 } 190 return f, nil 191 } 192 193 // isFile determines whether path is a file by trying to open it. 194 // It reuses openFile instead of adding another function to the 195 // list in Context. 196 func (ctxt *Context) isFile(path string) bool { 197 f, err := ctxt.openFile(path) 198 if err != nil { 199 return false 200 } 201 f.Close() 202 return true 203 } 204 205 // gopath returns the list of Go path directories. 206 func (ctxt *Context) gopath() []string { 207 var all []string 208 for _, p := range ctxt.splitPathList(ctxt.GOPATH) { 209 if p == "" || p == ctxt.GOROOT { 210 // Empty paths are uninteresting. 211 // If the path is the GOROOT, ignore it. 212 // People sometimes set GOPATH=$GOROOT. 213 // Do not get confused by this common mistake. 214 continue 215 } 216 if strings.HasPrefix(p, "~") { 217 // Path segments starting with ~ on Unix are almost always 218 // users who have incorrectly quoted ~ while setting GOPATH, 219 // preventing it from expanding to $HOME. 220 // The situation is made more confusing by the fact that 221 // bash allows quoted ~ in $PATH (most shells do not). 222 // Do not get confused by this, and do not try to use the path. 223 // It does not exist, and printing errors about it confuses 224 // those users even more, because they think "sure ~ exists!". 225 // The go command diagnoses this situation and prints a 226 // useful error. 227 // On Windows, ~ is used in short names, such as c:\progra~1 228 // for c:\program files. 229 continue 230 } 231 all = append(all, p) 232 } 233 return all 234 } 235 236 // SrcDirs returns a list of package source root directories. 237 // It draws from the current Go root and Go path but omits directories 238 // that do not exist. 239 func (ctxt *Context) SrcDirs() []string { 240 var all []string 241 if ctxt.GOROOT != "" { 242 dir := ctxt.joinPath(ctxt.GOROOT, "src") 243 if ctxt.isDir(dir) { 244 all = append(all, dir) 245 } 246 } 247 for _, p := range ctxt.gopath() { 248 dir := ctxt.joinPath(p, "src") 249 if ctxt.isDir(dir) { 250 all = append(all, dir) 251 } 252 } 253 return all 254 } 255 256 // Default is the default Context for builds. 257 // It uses the GOARCH, GOOS, GOROOT, and GOPATH environment variables 258 // if set, or else the compiled code's GOARCH, GOOS, and GOROOT. 259 var Default Context = defaultContext() 260 261 func defaultGOPATH() string { 262 env := "HOME" 263 if runtime.GOOS == "windows" { 264 env = "USERPROFILE" 265 } else if runtime.GOOS == "plan9" { 266 env = "home" 267 } 268 if home := os.Getenv(env); home != "" { 269 def := filepath.Join(home, "go") 270 if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) { 271 // Don't set the default GOPATH to GOROOT, 272 // as that will trigger warnings from the go tool. 273 return "" 274 } 275 return def 276 } 277 return "" 278 } 279 280 func defaultContext() Context { 281 var c Context 282 283 c.GOARCH = envOr("GOARCH", runtime.GOARCH) 284 c.GOOS = envOr("GOOS", runtime.GOOS) 285 c.GOROOT = pathpkg.Clean(runtime.GOROOT()) 286 c.GOPATH = envOr("GOPATH", defaultGOPATH()) 287 c.Compiler = runtime.Compiler 288 289 // Each major Go release in the Go 1.x series should add a tag here. 290 // Old tags should not be removed. That is, the go1.x tag is present 291 // in all releases >= Go 1.x. Code that requires Go 1.x or later should 292 // say "+build go1.x", and code that should only be built before Go 1.x 293 // (perhaps it is the stub to use in that case) should say "+build !go1.x". 294 // NOTE: If you add to this list, also update the doc comment in doc.go. 295 c.ReleaseTags = []string{"go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6", "go1.7", "go1.8", "go1.9"} 296 297 env := os.Getenv("CGO_ENABLED") 298 if env == "" { 299 env = defaultCGO_ENABLED 300 } 301 switch env { 302 case "1": 303 c.CgoEnabled = true 304 case "0": 305 c.CgoEnabled = false 306 default: 307 // cgo must be explicitly enabled for cross compilation builds 308 if runtime.GOARCH == c.GOARCH && runtime.GOOS == c.GOOS { 309 c.CgoEnabled = cgoEnabled[c.GOOS+"/"+c.GOARCH] 310 break 311 } 312 c.CgoEnabled = false 313 } 314 315 return c 316 } 317 318 func envOr(name, def string) string { 319 s := os.Getenv(name) 320 if s == "" { 321 return def 322 } 323 return s 324 } 325 326 // An ImportMode controls the behavior of the Import method. 327 type ImportMode uint 328 329 const ( 330 // If FindOnly is set, Import stops after locating the directory 331 // that should contain the sources for a package. It does not 332 // read any files in the directory. 333 FindOnly ImportMode = 1 << iota 334 335 // If AllowBinary is set, Import can be satisfied by a compiled 336 // package object without corresponding sources. 337 // 338 // Deprecated: 339 // The supported way to create a compiled-only package is to 340 // write source code containing a //go:binary-only-package comment at 341 // the top of the file. Such a package will be recognized 342 // regardless of this flag setting (because it has source code) 343 // and will have BinaryOnly set to true in the returned Package. 344 AllowBinary 345 346 // If ImportComment is set, parse import comments on package statements. 347 // Import returns an error if it finds a comment it cannot understand 348 // or finds conflicting comments in multiple source files. 349 // See golang.org/s/go14customimport for more information. 350 ImportComment 351 352 // By default, Import searches vendor directories 353 // that apply in the given source directory before searching 354 // the GOROOT and GOPATH roots. 355 // If an Import finds and returns a package using a vendor 356 // directory, the resulting ImportPath is the complete path 357 // to the package, including the path elements leading up 358 // to and including "vendor". 359 // For example, if Import("y", "x/subdir", 0) finds 360 // "x/vendor/y", the returned package's ImportPath is "x/vendor/y", 361 // not plain "y". 362 // See golang.org/s/go15vendor for more information. 363 // 364 // Setting IgnoreVendor ignores vendor directories. 365 // 366 // In contrast to the package's ImportPath, 367 // the returned package's Imports, TestImports, and XTestImports 368 // are always the exact import paths from the source files: 369 // Import makes no attempt to resolve or check those paths. 370 IgnoreVendor 371 ) 372 373 // A Package describes the Go package found in a directory. 374 type Package struct { 375 Dir string // directory containing package sources 376 Name string // package name 377 ImportComment string // path in import comment on package statement 378 Doc string // documentation synopsis 379 ImportPath string // import path of package ("" if unknown) 380 Root string // root of Go tree where this package lives 381 SrcRoot string // package source root directory ("" if unknown) 382 PkgRoot string // package install root directory ("" if unknown) 383 PkgTargetRoot string // architecture dependent install root directory ("" if unknown) 384 BinDir string // command install directory ("" if unknown) 385 Goroot bool // package found in Go root 386 PkgObj string // installed .a file 387 AllTags []string // tags that can influence file selection in this directory 388 ConflictDir string // this directory shadows Dir in $GOPATH 389 BinaryOnly bool // cannot be rebuilt from source (has //go:binary-only-package comment) 390 391 // Source files 392 GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles) 393 CgoFiles []string // .go source files that import "C" 394 IgnoredGoFiles []string // .go source files ignored for this build 395 InvalidGoFiles []string // .go source files with detected problems (parse error, wrong package name, and so on) 396 CFiles []string // .c source files 397 CXXFiles []string // .cc, .cpp and .cxx source files 398 MFiles []string // .m (Objective-C) source files 399 HFiles []string // .h, .hh, .hpp and .hxx source files 400 FFiles []string // .f, .F, .for and .f90 Fortran source files 401 SFiles []string // .s source files 402 SwigFiles []string // .swig files 403 SwigCXXFiles []string // .swigcxx files 404 SysoFiles []string // .syso system object files to add to archive 405 406 // Cgo directives 407 CgoCFLAGS []string // Cgo CFLAGS directives 408 CgoCPPFLAGS []string // Cgo CPPFLAGS directives 409 CgoCXXFLAGS []string // Cgo CXXFLAGS directives 410 CgoFFLAGS []string // Cgo FFLAGS directives 411 CgoLDFLAGS []string // Cgo LDFLAGS directives 412 CgoPkgConfig []string // Cgo pkg-config directives 413 414 // Dependency information 415 Imports []string // import paths from GoFiles, CgoFiles 416 ImportPos map[string][]token.Position // line information for Imports 417 418 // Test information 419 TestGoFiles []string // _test.go files in package 420 TestImports []string // import paths from TestGoFiles 421 TestImportPos map[string][]token.Position // line information for TestImports 422 XTestGoFiles []string // _test.go files outside package 423 XTestImports []string // import paths from XTestGoFiles 424 XTestImportPos map[string][]token.Position // line information for XTestImports 425 } 426 427 // IsCommand reports whether the package is considered a 428 // command to be installed (not just a library). 429 // Packages named "main" are treated as commands. 430 func (p *Package) IsCommand() bool { 431 return p.Name == "main" 432 } 433 434 // ImportDir is like Import but processes the Go package found in 435 // the named directory. 436 func (ctxt *Context) ImportDir(dir string, mode ImportMode) (*Package, error) { 437 return ctxt.Import(".", dir, mode) 438 } 439 440 // NoGoError is the error used by Import to describe a directory 441 // containing no buildable Go source files. (It may still contain 442 // test files, files hidden by build tags, and so on.) 443 type NoGoError struct { 444 Dir string 445 } 446 447 func (e *NoGoError) Error() string { 448 return "no buildable Go source files in " + e.Dir 449 } 450 451 // MultiplePackageError describes a directory containing 452 // multiple buildable Go source files for multiple packages. 453 type MultiplePackageError struct { 454 Dir string // directory containing files 455 Packages []string // package names found 456 Files []string // corresponding files: Files[i] declares package Packages[i] 457 } 458 459 func (e *MultiplePackageError) Error() string { 460 // Error string limited to two entries for compatibility. 461 return fmt.Sprintf("found packages %s (%s) and %s (%s) in %s", e.Packages[0], e.Files[0], e.Packages[1], e.Files[1], e.Dir) 462 } 463 464 func nameExt(name string) string { 465 i := strings.LastIndex(name, ".") 466 if i < 0 { 467 return "" 468 } 469 return name[i:] 470 } 471 472 // Import returns details about the Go package named by the import path, 473 // interpreting local import paths relative to the srcDir directory. 474 // If the path is a local import path naming a package that can be imported 475 // using a standard import path, the returned package will set p.ImportPath 476 // to that path. 477 // 478 // In the directory containing the package, .go, .c, .h, and .s files are 479 // considered part of the package except for: 480 // 481 // - .go files in package documentation 482 // - files starting with _ or . (likely editor temporary files) 483 // - files with build constraints not satisfied by the context 484 // 485 // If an error occurs, Import returns a non-nil error and a non-nil 486 // *Package containing partial information. 487 // 488 func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Package, error) { 489 p := &Package{ 490 ImportPath: path, 491 } 492 if path == "" { 493 return p, fmt.Errorf("import %q: invalid import path", path) 494 } 495 496 var pkgtargetroot string 497 var pkga string 498 var pkgerr error 499 suffix := "" 500 if ctxt.InstallSuffix != "" { 501 suffix = "_" + ctxt.InstallSuffix 502 } 503 switch ctxt.Compiler { 504 case "gccgo": 505 pkgtargetroot = "pkg/gccgo_" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix 506 case "gc": 507 pkgtargetroot = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix 508 default: 509 // Save error for end of function. 510 pkgerr = fmt.Errorf("import %q: unknown compiler %q", path, ctxt.Compiler) 511 } 512 setPkga := func() { 513 switch ctxt.Compiler { 514 case "gccgo": 515 dir, elem := pathpkg.Split(p.ImportPath) 516 pkga = pkgtargetroot + "/" + dir + "lib" + elem + ".a" 517 case "gc": 518 pkga = pkgtargetroot + "/" + p.ImportPath + ".a" 519 } 520 } 521 setPkga() 522 523 binaryOnly := false 524 if IsLocalImport(path) { 525 pkga = "" // local imports have no installed path 526 if srcDir == "" { 527 return p, fmt.Errorf("import %q: import relative to unknown directory", path) 528 } 529 if !ctxt.isAbsPath(path) { 530 p.Dir = ctxt.joinPath(srcDir, path) 531 } 532 // p.Dir directory may or may not exist. Gather partial information first, check if it exists later. 533 // Determine canonical import path, if any. 534 // Exclude results where the import path would include /testdata/. 535 inTestdata := func(sub string) bool { 536 return strings.Contains(sub, "/testdata/") || strings.HasSuffix(sub, "/testdata") || strings.HasPrefix(sub, "testdata/") || sub == "testdata" 537 } 538 if ctxt.GOROOT != "" { 539 root := ctxt.joinPath(ctxt.GOROOT, "src") 540 if sub, ok := ctxt.hasSubdir(root, p.Dir); ok && !inTestdata(sub) { 541 p.Goroot = true 542 p.ImportPath = sub 543 p.Root = ctxt.GOROOT 544 goto Found 545 } 546 } 547 all := ctxt.gopath() 548 for i, root := range all { 549 rootsrc := ctxt.joinPath(root, "src") 550 if sub, ok := ctxt.hasSubdir(rootsrc, p.Dir); ok && !inTestdata(sub) { 551 // We found a potential import path for dir, 552 // but check that using it wouldn't find something 553 // else first. 554 if ctxt.GOROOT != "" { 555 if dir := ctxt.joinPath(ctxt.GOROOT, "src", sub); ctxt.isDir(dir) { 556 p.ConflictDir = dir 557 goto Found 558 } 559 } 560 for _, earlyRoot := range all[:i] { 561 if dir := ctxt.joinPath(earlyRoot, "src", sub); ctxt.isDir(dir) { 562 p.ConflictDir = dir 563 goto Found 564 } 565 } 566 567 // sub would not name some other directory instead of this one. 568 // Record it. 569 p.ImportPath = sub 570 p.Root = root 571 goto Found 572 } 573 } 574 // It's okay that we didn't find a root containing dir. 575 // Keep going with the information we have. 576 } else { 577 if strings.HasPrefix(path, "/") { 578 return p, fmt.Errorf("import %q: cannot import absolute path", path) 579 } 580 581 // tried records the location of unsuccessful package lookups 582 var tried struct { 583 vendor []string 584 goroot string 585 gopath []string 586 } 587 gopath := ctxt.gopath() 588 589 // Vendor directories get first chance to satisfy import. 590 if mode&IgnoreVendor == 0 && srcDir != "" { 591 searchVendor := func(root string, isGoroot bool) bool { 592 sub, ok := ctxt.hasSubdir(root, srcDir) 593 if !ok || !strings.HasPrefix(sub, "src/") || strings.Contains(sub, "/testdata/") { 594 return false 595 } 596 for { 597 vendor := ctxt.joinPath(root, sub, "vendor") 598 if ctxt.isDir(vendor) { 599 dir := ctxt.joinPath(vendor, path) 600 if ctxt.isDir(dir) && hasGoFiles(ctxt, dir) { 601 p.Dir = dir 602 p.ImportPath = strings.TrimPrefix(pathpkg.Join(sub, "vendor", path), "src/") 603 p.Goroot = isGoroot 604 p.Root = root 605 setPkga() // p.ImportPath changed 606 return true 607 } 608 tried.vendor = append(tried.vendor, dir) 609 } 610 i := strings.LastIndex(sub, "/") 611 if i < 0 { 612 break 613 } 614 sub = sub[:i] 615 } 616 return false 617 } 618 if searchVendor(ctxt.GOROOT, true) { 619 goto Found 620 } 621 for _, root := range gopath { 622 if searchVendor(root, false) { 623 goto Found 624 } 625 } 626 } 627 628 // Determine directory from import path. 629 if ctxt.GOROOT != "" { 630 dir := ctxt.joinPath(ctxt.GOROOT, "src", path) 631 isDir := ctxt.isDir(dir) 632 binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(ctxt.GOROOT, pkga)) 633 if isDir || binaryOnly { 634 p.Dir = dir 635 p.Goroot = true 636 p.Root = ctxt.GOROOT 637 goto Found 638 } 639 tried.goroot = dir 640 } 641 for _, root := range gopath { 642 dir := ctxt.joinPath(root, "src", path) 643 isDir := ctxt.isDir(dir) 644 binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(root, pkga)) 645 if isDir || binaryOnly { 646 p.Dir = dir 647 p.Root = root 648 goto Found 649 } 650 tried.gopath = append(tried.gopath, dir) 651 } 652 653 // package was not found 654 var paths []string 655 format := "\t%s (vendor tree)" 656 for _, dir := range tried.vendor { 657 paths = append(paths, fmt.Sprintf(format, dir)) 658 format = "\t%s" 659 } 660 if tried.goroot != "" { 661 paths = append(paths, fmt.Sprintf("\t%s (from $GOROOT)", tried.goroot)) 662 } else { 663 paths = append(paths, "\t($GOROOT not set)") 664 } 665 format = "\t%s (from $GOPATH)" 666 for _, dir := range tried.gopath { 667 paths = append(paths, fmt.Sprintf(format, dir)) 668 format = "\t%s" 669 } 670 if len(tried.gopath) == 0 { 671 paths = append(paths, "\t($GOPATH not set. For more details see: 'go help gopath')") 672 } 673 return p, fmt.Errorf("cannot find package %q in any of:\n%s", path, strings.Join(paths, "\n")) 674 } 675 676 Found: 677 if p.Root != "" { 678 p.SrcRoot = ctxt.joinPath(p.Root, "src") 679 p.PkgRoot = ctxt.joinPath(p.Root, "pkg") 680 p.BinDir = ctxt.joinPath(p.Root, "bin") 681 if pkga != "" { 682 p.PkgTargetRoot = ctxt.joinPath(p.Root, pkgtargetroot) 683 p.PkgObj = ctxt.joinPath(p.Root, pkga) 684 } 685 } 686 687 // If it's a local import path, by the time we get here, we still haven't checked 688 // that p.Dir directory exists. This is the right time to do that check. 689 // We can't do it earlier, because we want to gather partial information for the 690 // non-nil *Package returned when an error occurs. 691 // We need to do this before we return early on FindOnly flag. 692 if IsLocalImport(path) && !ctxt.isDir(p.Dir) { 693 // package was not found 694 return p, fmt.Errorf("cannot find package %q in:\n\t%s", path, p.Dir) 695 } 696 697 if mode&FindOnly != 0 { 698 return p, pkgerr 699 } 700 if binaryOnly && (mode&AllowBinary) != 0 { 701 return p, pkgerr 702 } 703 704 dirs, err := ctxt.readDir(p.Dir) 705 if err != nil { 706 return p, err 707 } 708 709 var badGoError error 710 var Sfiles []string // files with ".S" (capital S) 711 var firstFile, firstCommentFile string 712 imported := make(map[string][]token.Position) 713 testImported := make(map[string][]token.Position) 714 xTestImported := make(map[string][]token.Position) 715 allTags := make(map[string]bool) 716 fset := token.NewFileSet() 717 for _, d := range dirs { 718 if d.IsDir() { 719 continue 720 } 721 722 name := d.Name() 723 ext := nameExt(name) 724 725 badFile := func(err error) { 726 if badGoError == nil { 727 badGoError = err 728 } 729 p.InvalidGoFiles = append(p.InvalidGoFiles, name) 730 } 731 732 match, data, filename, err := ctxt.matchFile(p.Dir, name, allTags, &p.BinaryOnly) 733 if err != nil { 734 badFile(err) 735 continue 736 } 737 if !match { 738 if ext == ".go" { 739 p.IgnoredGoFiles = append(p.IgnoredGoFiles, name) 740 } 741 continue 742 } 743 744 // Going to save the file. For non-Go files, can stop here. 745 switch ext { 746 case ".c": 747 p.CFiles = append(p.CFiles, name) 748 continue 749 case ".cc", ".cpp", ".cxx": 750 p.CXXFiles = append(p.CXXFiles, name) 751 continue 752 case ".m": 753 p.MFiles = append(p.MFiles, name) 754 continue 755 case ".h", ".hh", ".hpp", ".hxx": 756 p.HFiles = append(p.HFiles, name) 757 continue 758 case ".f", ".F", ".for", ".f90": 759 p.FFiles = append(p.FFiles, name) 760 continue 761 case ".s": 762 p.SFiles = append(p.SFiles, name) 763 continue 764 case ".S": 765 Sfiles = append(Sfiles, name) 766 continue 767 case ".swig": 768 p.SwigFiles = append(p.SwigFiles, name) 769 continue 770 case ".swigcxx": 771 p.SwigCXXFiles = append(p.SwigCXXFiles, name) 772 continue 773 case ".syso": 774 // binary objects to add to package archive 775 // Likely of the form foo_windows.syso, but 776 // the name was vetted above with goodOSArchFile. 777 p.SysoFiles = append(p.SysoFiles, name) 778 continue 779 } 780 781 pf, err := parser.ParseFile(fset, filename, data, parser.ImportsOnly|parser.ParseComments) 782 if err != nil { 783 badFile(err) 784 continue 785 } 786 787 pkg := pf.Name.Name 788 if pkg == "documentation" { 789 p.IgnoredGoFiles = append(p.IgnoredGoFiles, name) 790 continue 791 } 792 793 isTest := strings.HasSuffix(name, "_test.go") 794 isXTest := false 795 if isTest && strings.HasSuffix(pkg, "_test") { 796 isXTest = true 797 pkg = pkg[:len(pkg)-len("_test")] 798 } 799 800 if p.Name == "" { 801 p.Name = pkg 802 firstFile = name 803 } else if pkg != p.Name { 804 badFile(&MultiplePackageError{ 805 Dir: p.Dir, 806 Packages: []string{p.Name, pkg}, 807 Files: []string{firstFile, name}, 808 }) 809 p.InvalidGoFiles = append(p.InvalidGoFiles, name) 810 } 811 if pf.Doc != nil && p.Doc == "" { 812 p.Doc = doc.Synopsis(pf.Doc.Text()) 813 } 814 815 if mode&ImportComment != 0 { 816 qcom, line := findImportComment(data) 817 if line != 0 { 818 com, err := strconv.Unquote(qcom) 819 if err != nil { 820 badFile(fmt.Errorf("%s:%d: cannot parse import comment", filename, line)) 821 } else if p.ImportComment == "" { 822 p.ImportComment = com 823 firstCommentFile = name 824 } else if p.ImportComment != com { 825 badFile(fmt.Errorf("found import comments %q (%s) and %q (%s) in %s", p.ImportComment, firstCommentFile, com, name, p.Dir)) 826 } 827 } 828 } 829 830 // Record imports and information about cgo. 831 isCgo := false 832 for _, decl := range pf.Decls { 833 d, ok := decl.(*ast.GenDecl) 834 if !ok { 835 continue 836 } 837 for _, dspec := range d.Specs { 838 spec, ok := dspec.(*ast.ImportSpec) 839 if !ok { 840 continue 841 } 842 quoted := spec.Path.Value 843 path, err := strconv.Unquote(quoted) 844 if err != nil { 845 log.Panicf("%s: parser returned invalid quoted string: <%s>", filename, quoted) 846 } 847 if isXTest { 848 xTestImported[path] = append(xTestImported[path], fset.Position(spec.Pos())) 849 } else if isTest { 850 testImported[path] = append(testImported[path], fset.Position(spec.Pos())) 851 } else { 852 imported[path] = append(imported[path], fset.Position(spec.Pos())) 853 } 854 if path == "C" { 855 if isTest { 856 badFile(fmt.Errorf("use of cgo in test %s not supported", filename)) 857 } else { 858 cg := spec.Doc 859 if cg == nil && len(d.Specs) == 1 { 860 cg = d.Doc 861 } 862 if cg != nil { 863 if err := ctxt.saveCgo(filename, p, cg); err != nil { 864 badFile(err) 865 } 866 } 867 isCgo = true 868 } 869 } 870 } 871 } 872 if isCgo { 873 allTags["cgo"] = true 874 if ctxt.CgoEnabled { 875 p.CgoFiles = append(p.CgoFiles, name) 876 } else { 877 p.IgnoredGoFiles = append(p.IgnoredGoFiles, name) 878 } 879 } else if isXTest { 880 p.XTestGoFiles = append(p.XTestGoFiles, name) 881 } else if isTest { 882 p.TestGoFiles = append(p.TestGoFiles, name) 883 } else { 884 p.GoFiles = append(p.GoFiles, name) 885 } 886 } 887 if badGoError != nil { 888 return p, badGoError 889 } 890 if len(p.GoFiles)+len(p.CgoFiles)+len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 { 891 return p, &NoGoError{p.Dir} 892 } 893 894 for tag := range allTags { 895 p.AllTags = append(p.AllTags, tag) 896 } 897 sort.Strings(p.AllTags) 898 899 p.Imports, p.ImportPos = cleanImports(imported) 900 p.TestImports, p.TestImportPos = cleanImports(testImported) 901 p.XTestImports, p.XTestImportPos = cleanImports(xTestImported) 902 903 // add the .S files only if we are using cgo 904 // (which means gcc will compile them). 905 // The standard assemblers expect .s files. 906 if len(p.CgoFiles) > 0 { 907 p.SFiles = append(p.SFiles, Sfiles...) 908 sort.Strings(p.SFiles) 909 } 910 911 return p, pkgerr 912 } 913 914 // hasGoFiles reports whether dir contains any files with names ending in .go. 915 // For a vendor check we must exclude directories that contain no .go files. 916 // Otherwise it is not possible to vendor just a/b/c and still import the 917 // non-vendored a/b. See golang.org/issue/13832. 918 func hasGoFiles(ctxt *Context, dir string) bool { 919 ents, _ := ctxt.readDir(dir) 920 for _, ent := range ents { 921 if !ent.IsDir() && strings.HasSuffix(ent.Name(), ".go") { 922 return true 923 } 924 } 925 return false 926 } 927 928 func findImportComment(data []byte) (s string, line int) { 929 // expect keyword package 930 word, data := parseWord(data) 931 if string(word) != "package" { 932 return "", 0 933 } 934 935 // expect package name 936 _, data = parseWord(data) 937 938 // now ready for import comment, a // or /* */ comment 939 // beginning and ending on the current line. 940 for len(data) > 0 && (data[0] == ' ' || data[0] == '\t' || data[0] == '\r') { 941 data = data[1:] 942 } 943 944 var comment []byte 945 switch { 946 case bytes.HasPrefix(data, slashSlash): 947 i := bytes.Index(data, newline) 948 if i < 0 { 949 i = len(data) 950 } 951 comment = data[2:i] 952 case bytes.HasPrefix(data, slashStar): 953 data = data[2:] 954 i := bytes.Index(data, starSlash) 955 if i < 0 { 956 // malformed comment 957 return "", 0 958 } 959 comment = data[:i] 960 if bytes.Contains(comment, newline) { 961 return "", 0 962 } 963 } 964 comment = bytes.TrimSpace(comment) 965 966 // split comment into `import`, `"pkg"` 967 word, arg := parseWord(comment) 968 if string(word) != "import" { 969 return "", 0 970 } 971 972 line = 1 + bytes.Count(data[:cap(data)-cap(arg)], newline) 973 return strings.TrimSpace(string(arg)), line 974 } 975 976 var ( 977 slashSlash = []byte("//") 978 slashStar = []byte("/*") 979 starSlash = []byte("*/") 980 newline = []byte("\n") 981 ) 982 983 // skipSpaceOrComment returns data with any leading spaces or comments removed. 984 func skipSpaceOrComment(data []byte) []byte { 985 for len(data) > 0 { 986 switch data[0] { 987 case ' ', '\t', '\r', '\n': 988 data = data[1:] 989 continue 990 case '/': 991 if bytes.HasPrefix(data, slashSlash) { 992 i := bytes.Index(data, newline) 993 if i < 0 { 994 return nil 995 } 996 data = data[i+1:] 997 continue 998 } 999 if bytes.HasPrefix(data, slashStar) { 1000 data = data[2:] 1001 i := bytes.Index(data, starSlash) 1002 if i < 0 { 1003 return nil 1004 } 1005 data = data[i+2:] 1006 continue 1007 } 1008 } 1009 break 1010 } 1011 return data 1012 } 1013 1014 // parseWord skips any leading spaces or comments in data 1015 // and then parses the beginning of data as an identifier or keyword, 1016 // returning that word and what remains after the word. 1017 func parseWord(data []byte) (word, rest []byte) { 1018 data = skipSpaceOrComment(data) 1019 1020 // Parse past leading word characters. 1021 rest = data 1022 for { 1023 r, size := utf8.DecodeRune(rest) 1024 if unicode.IsLetter(r) || '0' <= r && r <= '9' || r == '_' { 1025 rest = rest[size:] 1026 continue 1027 } 1028 break 1029 } 1030 1031 word = data[:len(data)-len(rest)] 1032 if len(word) == 0 { 1033 return nil, nil 1034 } 1035 1036 return word, rest 1037 } 1038 1039 // MatchFile reports whether the file with the given name in the given directory 1040 // matches the context and would be included in a Package created by ImportDir 1041 // of that directory. 1042 // 1043 // MatchFile considers the name of the file and may use ctxt.OpenFile to 1044 // read some or all of the file's content. 1045 func (ctxt *Context) MatchFile(dir, name string) (match bool, err error) { 1046 match, _, _, err = ctxt.matchFile(dir, name, nil, nil) 1047 return 1048 } 1049 1050 // matchFile determines whether the file with the given name in the given directory 1051 // should be included in the package being constructed. 1052 // It returns the data read from the file. 1053 // If name denotes a Go program, matchFile reads until the end of the 1054 // imports (and returns that data) even though it only considers text 1055 // until the first non-comment. 1056 // If allTags is non-nil, matchFile records any encountered build tag 1057 // by setting allTags[tag] = true. 1058 func (ctxt *Context) matchFile(dir, name string, allTags map[string]bool, binaryOnly *bool) (match bool, data []byte, filename string, err error) { 1059 if strings.HasPrefix(name, "_") || 1060 strings.HasPrefix(name, ".") { 1061 return 1062 } 1063 1064 i := strings.LastIndex(name, ".") 1065 if i < 0 { 1066 i = len(name) 1067 } 1068 ext := name[i:] 1069 1070 if !ctxt.goodOSArchFile(name, allTags) && !ctxt.UseAllFiles { 1071 return 1072 } 1073 1074 switch ext { 1075 case ".go", ".c", ".cc", ".cxx", ".cpp", ".m", ".s", ".h", ".hh", ".hpp", ".hxx", ".f", ".F", ".f90", ".S", ".swig", ".swigcxx": 1076 // tentatively okay - read to make sure 1077 case ".syso": 1078 // binary, no reading 1079 match = true 1080 return 1081 default: 1082 // skip 1083 return 1084 } 1085 1086 filename = ctxt.joinPath(dir, name) 1087 f, err := ctxt.openFile(filename) 1088 if err != nil { 1089 return 1090 } 1091 1092 if strings.HasSuffix(filename, ".go") { 1093 data, err = readImports(f, false, nil) 1094 if strings.HasSuffix(filename, "_test.go") { 1095 binaryOnly = nil // ignore //go:binary-only-package comments in _test.go files 1096 } 1097 } else { 1098 binaryOnly = nil // ignore //go:binary-only-package comments in non-Go sources 1099 data, err = readComments(f) 1100 } 1101 f.Close() 1102 if err != nil { 1103 err = fmt.Errorf("read %s: %v", filename, err) 1104 return 1105 } 1106 1107 // Look for +build comments to accept or reject the file. 1108 var sawBinaryOnly bool 1109 if !ctxt.shouldBuild(data, allTags, &sawBinaryOnly) && !ctxt.UseAllFiles { 1110 return 1111 } 1112 1113 if binaryOnly != nil && sawBinaryOnly { 1114 *binaryOnly = true 1115 } 1116 match = true 1117 return 1118 } 1119 1120 func cleanImports(m map[string][]token.Position) ([]string, map[string][]token.Position) { 1121 all := make([]string, 0, len(m)) 1122 for path := range m { 1123 all = append(all, path) 1124 } 1125 sort.Strings(all) 1126 return all, m 1127 } 1128 1129 // Import is shorthand for Default.Import. 1130 func Import(path, srcDir string, mode ImportMode) (*Package, error) { 1131 return Default.Import(path, srcDir, mode) 1132 } 1133 1134 // ImportDir is shorthand for Default.ImportDir. 1135 func ImportDir(dir string, mode ImportMode) (*Package, error) { 1136 return Default.ImportDir(dir, mode) 1137 } 1138 1139 var slashslash = []byte("//") 1140 1141 // Special comment denoting a binary-only package. 1142 // See https://golang.org/design/2775-binary-only-packages 1143 // for more about the design of binary-only packages. 1144 var binaryOnlyComment = []byte("//go:binary-only-package") 1145 1146 // shouldBuild reports whether it is okay to use this file, 1147 // The rule is that in the file's leading run of // comments 1148 // and blank lines, which must be followed by a blank line 1149 // (to avoid including a Go package clause doc comment), 1150 // lines beginning with '// +build' are taken as build directives. 1151 // 1152 // The file is accepted only if each such line lists something 1153 // matching the file. For example: 1154 // 1155 // // +build windows linux 1156 // 1157 // marks the file as applicable only on Windows and Linux. 1158 // 1159 // If shouldBuild finds a //go:binary-only-package comment in the file, 1160 // it sets *binaryOnly to true. Otherwise it does not change *binaryOnly. 1161 // 1162 func (ctxt *Context) shouldBuild(content []byte, allTags map[string]bool, binaryOnly *bool) bool { 1163 sawBinaryOnly := false 1164 1165 // Pass 1. Identify leading run of // comments and blank lines, 1166 // which must be followed by a blank line. 1167 end := 0 1168 p := content 1169 for len(p) > 0 { 1170 line := p 1171 if i := bytes.IndexByte(line, '\n'); i >= 0 { 1172 line, p = line[:i], p[i+1:] 1173 } else { 1174 p = p[len(p):] 1175 } 1176 line = bytes.TrimSpace(line) 1177 if len(line) == 0 { // Blank line 1178 end = len(content) - len(p) 1179 continue 1180 } 1181 if !bytes.HasPrefix(line, slashslash) { // Not comment line 1182 break 1183 } 1184 } 1185 content = content[:end] 1186 1187 // Pass 2. Process each line in the run. 1188 p = content 1189 allok := true 1190 for len(p) > 0 { 1191 line := p 1192 if i := bytes.IndexByte(line, '\n'); i >= 0 { 1193 line, p = line[:i], p[i+1:] 1194 } else { 1195 p = p[len(p):] 1196 } 1197 line = bytes.TrimSpace(line) 1198 if bytes.HasPrefix(line, slashslash) { 1199 if bytes.Equal(line, binaryOnlyComment) { 1200 sawBinaryOnly = true 1201 } 1202 line = bytes.TrimSpace(line[len(slashslash):]) 1203 if len(line) > 0 && line[0] == '+' { 1204 // Looks like a comment +line. 1205 f := strings.Fields(string(line)) 1206 if f[0] == "+build" { 1207 ok := false 1208 for _, tok := range f[1:] { 1209 if ctxt.match(tok, allTags) { 1210 ok = true 1211 } 1212 } 1213 if !ok { 1214 allok = false 1215 } 1216 } 1217 } 1218 } 1219 } 1220 1221 if binaryOnly != nil && sawBinaryOnly { 1222 *binaryOnly = true 1223 } 1224 1225 return allok 1226 } 1227 1228 // saveCgo saves the information from the #cgo lines in the import "C" comment. 1229 // These lines set CFLAGS, CPPFLAGS, CXXFLAGS and LDFLAGS and pkg-config directives 1230 // that affect the way cgo's C code is built. 1231 func (ctxt *Context) saveCgo(filename string, di *Package, cg *ast.CommentGroup) error { 1232 text := cg.Text() 1233 for _, line := range strings.Split(text, "\n") { 1234 orig := line 1235 1236 // Line is 1237 // #cgo [GOOS/GOARCH...] LDFLAGS: stuff 1238 // 1239 line = strings.TrimSpace(line) 1240 if len(line) < 5 || line[:4] != "#cgo" || (line[4] != ' ' && line[4] != '\t') { 1241 continue 1242 } 1243 1244 // Split at colon. 1245 line = strings.TrimSpace(line[4:]) 1246 i := strings.Index(line, ":") 1247 if i < 0 { 1248 return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig) 1249 } 1250 line, argstr := line[:i], line[i+1:] 1251 1252 // Parse GOOS/GOARCH stuff. 1253 f := strings.Fields(line) 1254 if len(f) < 1 { 1255 return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig) 1256 } 1257 1258 cond, verb := f[:len(f)-1], f[len(f)-1] 1259 if len(cond) > 0 { 1260 ok := false 1261 for _, c := range cond { 1262 if ctxt.match(c, nil) { 1263 ok = true 1264 break 1265 } 1266 } 1267 if !ok { 1268 continue 1269 } 1270 } 1271 1272 args, err := splitQuoted(argstr) 1273 if err != nil { 1274 return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig) 1275 } 1276 var ok bool 1277 for i, arg := range args { 1278 if arg, ok = expandSrcDir(arg, di.Dir); !ok { 1279 return fmt.Errorf("%s: malformed #cgo argument: %s", filename, arg) 1280 } 1281 args[i] = arg 1282 } 1283 1284 switch verb { 1285 case "CFLAGS", "CPPFLAGS", "CXXFLAGS", "FFLAGS", "LDFLAGS": 1286 // Change relative paths to absolute. 1287 ctxt.makePathsAbsolute(args, di.Dir) 1288 } 1289 1290 switch verb { 1291 case "CFLAGS": 1292 di.CgoCFLAGS = append(di.CgoCFLAGS, args...) 1293 case "CPPFLAGS": 1294 di.CgoCPPFLAGS = append(di.CgoCPPFLAGS, args...) 1295 case "CXXFLAGS": 1296 di.CgoCXXFLAGS = append(di.CgoCXXFLAGS, args...) 1297 case "FFLAGS": 1298 di.CgoFFLAGS = append(di.CgoFFLAGS, args...) 1299 case "LDFLAGS": 1300 di.CgoLDFLAGS = append(di.CgoLDFLAGS, args...) 1301 case "pkg-config": 1302 di.CgoPkgConfig = append(di.CgoPkgConfig, args...) 1303 default: 1304 return fmt.Errorf("%s: invalid #cgo verb: %s", filename, orig) 1305 } 1306 } 1307 return nil 1308 } 1309 1310 // expandSrcDir expands any occurrence of ${SRCDIR}, making sure 1311 // the result is safe for the shell. 1312 func expandSrcDir(str string, srcdir string) (string, bool) { 1313 // "\" delimited paths cause safeCgoName to fail 1314 // so convert native paths with a different delimiter 1315 // to "/" before starting (eg: on windows). 1316 srcdir = filepath.ToSlash(srcdir) 1317 1318 chunks := strings.Split(str, "${SRCDIR}") 1319 if len(chunks) < 2 { 1320 return str, safeCgoName(str) 1321 } 1322 ok := true 1323 for _, chunk := range chunks { 1324 ok = ok && (chunk == "" || safeCgoName(chunk)) 1325 } 1326 ok = ok && (srcdir == "" || safeCgoName(srcdir)) 1327 res := strings.Join(chunks, srcdir) 1328 return res, ok && res != "" 1329 } 1330 1331 // makePathsAbsolute looks for compiler options that take paths and 1332 // makes them absolute. We do this because through the 1.8 release we 1333 // ran the compiler in the package directory, so any relative -I or -L 1334 // options would be relative to that directory. In 1.9 we changed to 1335 // running the compiler in the build directory, to get consistent 1336 // build results (issue #19964). To keep builds working, we change any 1337 // relative -I or -L options to be absolute. 1338 // 1339 // Using filepath.IsAbs and filepath.Join here means the results will be 1340 // different on different systems, but that's OK: -I and -L options are 1341 // inherently system-dependent. 1342 func (ctxt *Context) makePathsAbsolute(args []string, srcDir string) { 1343 nextPath := false 1344 for i, arg := range args { 1345 if nextPath { 1346 if !filepath.IsAbs(arg) { 1347 args[i] = filepath.Join(srcDir, arg) 1348 } 1349 nextPath = false 1350 } else if strings.HasPrefix(arg, "-I") || strings.HasPrefix(arg, "-L") { 1351 if len(arg) == 2 { 1352 nextPath = true 1353 } else { 1354 if !filepath.IsAbs(arg[2:]) { 1355 args[i] = arg[:2] + filepath.Join(srcDir, arg[2:]) 1356 } 1357 } 1358 } 1359 } 1360 } 1361 1362 // NOTE: $ is not safe for the shell, but it is allowed here because of linker options like -Wl,$ORIGIN. 1363 // We never pass these arguments to a shell (just to programs we construct argv for), so this should be okay. 1364 // See golang.org/issue/6038. 1365 // The @ is for OS X. See golang.org/issue/13720. 1366 // The % is for Jenkins. See golang.org/issue/16959. 1367 const safeString = "+-.,/0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz:$@% " 1368 1369 func safeCgoName(s string) bool { 1370 if s == "" { 1371 return false 1372 } 1373 for i := 0; i < len(s); i++ { 1374 if c := s[i]; c < utf8.RuneSelf && strings.IndexByte(safeString, c) < 0 { 1375 return false 1376 } 1377 } 1378 return true 1379 } 1380 1381 // splitQuoted splits the string s around each instance of one or more consecutive 1382 // white space characters while taking into account quotes and escaping, and 1383 // returns an array of substrings of s or an empty list if s contains only white space. 1384 // Single quotes and double quotes are recognized to prevent splitting within the 1385 // quoted region, and are removed from the resulting substrings. If a quote in s 1386 // isn't closed err will be set and r will have the unclosed argument as the 1387 // last element. The backslash is used for escaping. 1388 // 1389 // For example, the following string: 1390 // 1391 // a b:"c d" 'e''f' "g\"" 1392 // 1393 // Would be parsed as: 1394 // 1395 // []string{"a", "b:c d", "ef", `g"`} 1396 // 1397 func splitQuoted(s string) (r []string, err error) { 1398 var args []string 1399 arg := make([]rune, len(s)) 1400 escaped := false 1401 quoted := false 1402 quote := '\x00' 1403 i := 0 1404 for _, rune := range s { 1405 switch { 1406 case escaped: 1407 escaped = false 1408 case rune == '\\': 1409 escaped = true 1410 continue 1411 case quote != '\x00': 1412 if rune == quote { 1413 quote = '\x00' 1414 continue 1415 } 1416 case rune == '"' || rune == '\'': 1417 quoted = true 1418 quote = rune 1419 continue 1420 case unicode.IsSpace(rune): 1421 if quoted || i > 0 { 1422 quoted = false 1423 args = append(args, string(arg[:i])) 1424 i = 0 1425 } 1426 continue 1427 } 1428 arg[i] = rune 1429 i++ 1430 } 1431 if quoted || i > 0 { 1432 args = append(args, string(arg[:i])) 1433 } 1434 if quote != 0 { 1435 err = errors.New("unclosed quote") 1436 } else if escaped { 1437 err = errors.New("unfinished escaping") 1438 } 1439 return args, err 1440 } 1441 1442 // match reports whether the name is one of: 1443 // 1444 // $GOOS 1445 // $GOARCH 1446 // cgo (if cgo is enabled) 1447 // !cgo (if cgo is disabled) 1448 // ctxt.Compiler 1449 // !ctxt.Compiler 1450 // tag (if tag is listed in ctxt.BuildTags or ctxt.ReleaseTags) 1451 // !tag (if tag is not listed in ctxt.BuildTags or ctxt.ReleaseTags) 1452 // a comma-separated list of any of these 1453 // 1454 func (ctxt *Context) match(name string, allTags map[string]bool) bool { 1455 if name == "" { 1456 if allTags != nil { 1457 allTags[name] = true 1458 } 1459 return false 1460 } 1461 if i := strings.Index(name, ","); i >= 0 { 1462 // comma-separated list 1463 ok1 := ctxt.match(name[:i], allTags) 1464 ok2 := ctxt.match(name[i+1:], allTags) 1465 return ok1 && ok2 1466 } 1467 if strings.HasPrefix(name, "!!") { // bad syntax, reject always 1468 return false 1469 } 1470 if strings.HasPrefix(name, "!") { // negation 1471 return len(name) > 1 && !ctxt.match(name[1:], allTags) 1472 } 1473 1474 if allTags != nil { 1475 allTags[name] = true 1476 } 1477 1478 // Tags must be letters, digits, underscores or dots. 1479 // Unlike in Go identifiers, all digits are fine (e.g., "386"). 1480 for _, c := range name { 1481 if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' { 1482 return false 1483 } 1484 } 1485 1486 // special tags 1487 if ctxt.CgoEnabled && name == "cgo" { 1488 return true 1489 } 1490 if name == ctxt.GOOS || name == ctxt.GOARCH || name == ctxt.Compiler { 1491 return true 1492 } 1493 if ctxt.GOOS == "android" && name == "linux" { 1494 return true 1495 } 1496 1497 // other tags 1498 for _, tag := range ctxt.BuildTags { 1499 if tag == name { 1500 return true 1501 } 1502 } 1503 for _, tag := range ctxt.ReleaseTags { 1504 if tag == name { 1505 return true 1506 } 1507 } 1508 1509 return false 1510 } 1511 1512 // goodOSArchFile returns false if the name contains a $GOOS or $GOARCH 1513 // suffix which does not match the current system. 1514 // The recognized name formats are: 1515 // 1516 // name_$(GOOS).* 1517 // name_$(GOARCH).* 1518 // name_$(GOOS)_$(GOARCH).* 1519 // name_$(GOOS)_test.* 1520 // name_$(GOARCH)_test.* 1521 // name_$(GOOS)_$(GOARCH)_test.* 1522 // 1523 // An exception: if GOOS=android, then files with GOOS=linux are also matched. 1524 func (ctxt *Context) goodOSArchFile(name string, allTags map[string]bool) bool { 1525 if dot := strings.Index(name, "."); dot != -1 { 1526 name = name[:dot] 1527 } 1528 1529 // Before Go 1.4, a file called "linux.go" would be equivalent to having a 1530 // build tag "linux" in that file. For Go 1.4 and beyond, we require this 1531 // auto-tagging to apply only to files with a non-empty prefix, so 1532 // "foo_linux.go" is tagged but "linux.go" is not. This allows new operating 1533 // systems, such as android, to arrive without breaking existing code with 1534 // innocuous source code in "android.go". The easiest fix: cut everything 1535 // in the name before the initial _. 1536 i := strings.Index(name, "_") 1537 if i < 0 { 1538 return true 1539 } 1540 name = name[i:] // ignore everything before first _ 1541 1542 l := strings.Split(name, "_") 1543 if n := len(l); n > 0 && l[n-1] == "test" { 1544 l = l[:n-1] 1545 } 1546 n := len(l) 1547 if n >= 2 && knownOS[l[n-2]] && knownArch[l[n-1]] { 1548 if allTags != nil { 1549 allTags[l[n-2]] = true 1550 allTags[l[n-1]] = true 1551 } 1552 if l[n-1] != ctxt.GOARCH { 1553 return false 1554 } 1555 if ctxt.GOOS == "android" && l[n-2] == "linux" { 1556 return true 1557 } 1558 return l[n-2] == ctxt.GOOS 1559 } 1560 if n >= 1 && knownOS[l[n-1]] { 1561 if allTags != nil { 1562 allTags[l[n-1]] = true 1563 } 1564 if ctxt.GOOS == "android" && l[n-1] == "linux" { 1565 return true 1566 } 1567 return l[n-1] == ctxt.GOOS 1568 } 1569 if n >= 1 && knownArch[l[n-1]] { 1570 if allTags != nil { 1571 allTags[l[n-1]] = true 1572 } 1573 return l[n-1] == ctxt.GOARCH 1574 } 1575 return true 1576 } 1577 1578 var knownOS = make(map[string]bool) 1579 var knownArch = make(map[string]bool) 1580 1581 func init() { 1582 for _, v := range strings.Fields(goosList) { 1583 knownOS[v] = true 1584 } 1585 for _, v := range strings.Fields(goarchList) { 1586 knownArch[v] = true 1587 } 1588 } 1589 1590 // ToolDir is the directory containing build tools. 1591 var ToolDir = filepath.Join(runtime.GOROOT(), "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH) 1592 1593 // IsLocalImport reports whether the import path is 1594 // a local import path, like ".", "..", "./foo", or "../foo". 1595 func IsLocalImport(path string) bool { 1596 return path == "." || path == ".." || 1597 strings.HasPrefix(path, "./") || strings.HasPrefix(path, "../") 1598 } 1599 1600 // ArchChar returns "?" and an error. 1601 // In earlier versions of Go, the returned string was used to derive 1602 // the compiler and linker tool names, the default object file suffix, 1603 // and the default linker output name. As of Go 1.5, those strings 1604 // no longer vary by architecture; they are compile, link, .o, and a.out, respectively. 1605 func ArchChar(goarch string) (string, error) { 1606 return "?", errors.New("architecture letter no longer used") 1607 }