github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/cmd/go/internal/work/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 work
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  	"go/build"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"runtime"
    15  	"strings"
    16  
    17  	"cmd/go/internal/base"
    18  	"cmd/go/internal/cfg"
    19  	"cmd/go/internal/load"
    20  	"cmd/go/internal/search"
    21  )
    22  
    23  var CmdBuild = &base.Command{
    24  	UsageLine: "go build [-o output] [-i] [build flags] [packages]",
    25  	Short:     "compile packages and dependencies",
    26  	Long: `
    27  Build compiles the packages named by the import paths,
    28  along with their dependencies, but it does not install the results.
    29  
    30  If the arguments to build are a list of .go files, build treats
    31  them as a list of source files specifying a single package.
    32  
    33  When compiling a single main package, build writes
    34  the resulting executable to an output file named after
    35  the first source file ('go build ed.go rx.go' writes 'ed' or 'ed.exe')
    36  or the source code directory ('go build unix/sam' writes 'sam' or 'sam.exe').
    37  The '.exe' suffix is added when writing a Windows executable.
    38  
    39  When compiling multiple packages or a single non-main package,
    40  build compiles the packages but discards the resulting object,
    41  serving only as a check that the packages can be built.
    42  
    43  When compiling packages, build ignores files that end in '_test.go'.
    44  
    45  The -o flag, only allowed when compiling a single package,
    46  forces build to write the resulting executable or object
    47  to the named output file, instead of the default behavior described
    48  in the last two paragraphs.
    49  
    50  The -i flag installs the packages that are dependencies of the target.
    51  
    52  The build flags are shared by the build, clean, get, install, list, run,
    53  and test commands:
    54  
    55  	-a
    56  		force rebuilding of packages that are already up-to-date.
    57  	-n
    58  		print the commands but do not run them.
    59  	-p n
    60  		the number of programs, such as build commands or
    61  		test binaries, that can be run in parallel.
    62  		The default is the number of CPUs available.
    63  	-race
    64  		enable data race detection.
    65  		Supported only on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64.
    66  	-msan
    67  		enable interoperation with memory sanitizer.
    68  		Supported only on linux/amd64, linux/arm64
    69  		and only with Clang/LLVM as the host C compiler.
    70  	-v
    71  		print the names of packages as they are compiled.
    72  	-work
    73  		print the name of the temporary work directory and
    74  		do not delete it when exiting.
    75  	-x
    76  		print the commands.
    77  
    78  	-asmflags '[pattern=]arg list'
    79  		arguments to pass on each go tool asm invocation.
    80  	-buildmode mode
    81  		build mode to use. See 'go help buildmode' for more.
    82  	-compiler name
    83  		name of compiler to use, as in runtime.Compiler (gccgo or gc).
    84  	-gccgoflags '[pattern=]arg list'
    85  		arguments to pass on each gccgo compiler/linker invocation.
    86  	-gcflags '[pattern=]arg list'
    87  		arguments to pass on each go tool compile invocation.
    88  	-installsuffix suffix
    89  		a suffix to use in the name of the package installation directory,
    90  		in order to keep output separate from default builds.
    91  		If using the -race flag, the install suffix is automatically set to race
    92  		or, if set explicitly, has _race appended to it. Likewise for the -msan
    93  		flag. Using a -buildmode option that requires non-default compile flags
    94  		has a similar effect.
    95  	-ldflags '[pattern=]arg list'
    96  		arguments to pass on each go tool link invocation.
    97  	-linkshared
    98  		link against shared libraries previously created with
    99  		-buildmode=shared.
   100  	-mod mode
   101  		module download mode to use: readonly or vendor.
   102  		See 'go help modules' for more.
   103  	-pkgdir dir
   104  		install and load all packages from dir instead of the usual locations.
   105  		For example, when building with a non-standard configuration,
   106  		use -pkgdir to keep generated packages in a separate location.
   107  	-tags 'tag list'
   108  		a space-separated list of build tags to consider satisfied during the
   109  		build. For more information about build tags, see the description of
   110  		build constraints in the documentation for the go/build package.
   111  	-toolexec 'cmd args'
   112  		a program to use to invoke toolchain programs like vet and asm.
   113  		For example, instead of running asm, the go command will run
   114  		'cmd args /path/to/asm <arguments for asm>'.
   115  
   116  The -asmflags, -gccgoflags, -gcflags, and -ldflags flags accept a
   117  space-separated list of arguments to pass to an underlying tool
   118  during the build. To embed spaces in an element in the list, surround
   119  it with either single or double quotes. The argument list may be
   120  preceded by a package pattern and an equal sign, which restricts
   121  the use of that argument list to the building of packages matching
   122  that pattern (see 'go help packages' for a description of package
   123  patterns). Without a pattern, the argument list applies only to the
   124  packages named on the command line. The flags may be repeated
   125  with different patterns in order to specify different arguments for
   126  different sets of packages. If a package matches patterns given in
   127  multiple flags, the latest match on the command line wins.
   128  For example, 'go build -gcflags=-S fmt' prints the disassembly
   129  only for package fmt, while 'go build -gcflags=all=-S fmt'
   130  prints the disassembly for fmt and all its dependencies.
   131  
   132  For more about specifying packages, see 'go help packages'.
   133  For more about where packages and binaries are installed,
   134  run 'go help gopath'.
   135  For more about calling between Go and C/C++, run 'go help c'.
   136  
   137  Note: Build adheres to certain conventions such as those described
   138  by 'go help gopath'. Not all projects can follow these conventions,
   139  however. Installations that have their own conventions or that use
   140  a separate software build system may choose to use lower-level
   141  invocations such as 'go tool compile' and 'go tool link' to avoid
   142  some of the overheads and design decisions of the build tool.
   143  
   144  See also: go install, go get, go clean.
   145  	`,
   146  }
   147  
   148  const concurrentGCBackendCompilationEnabledByDefault = true
   149  
   150  func init() {
   151  	// break init cycle
   152  	CmdBuild.Run = runBuild
   153  	CmdInstall.Run = runInstall
   154  
   155  	CmdBuild.Flag.BoolVar(&cfg.BuildI, "i", false, "")
   156  	CmdBuild.Flag.StringVar(&cfg.BuildO, "o", "", "output file")
   157  
   158  	CmdInstall.Flag.BoolVar(&cfg.BuildI, "i", false, "")
   159  
   160  	AddBuildFlags(CmdBuild)
   161  	AddBuildFlags(CmdInstall)
   162  }
   163  
   164  // Note that flags consulted by other parts of the code
   165  // (for example, buildV) are in cmd/go/internal/cfg.
   166  
   167  var (
   168  	forcedAsmflags   []string // internally-forced flags for cmd/asm
   169  	forcedGcflags    []string // internally-forced flags for cmd/compile
   170  	forcedLdflags    []string // internally-forced flags for cmd/link
   171  	forcedGccgoflags []string // internally-forced flags for gccgo
   172  )
   173  
   174  var BuildToolchain toolchain = noToolchain{}
   175  var ldBuildmode string
   176  
   177  // buildCompiler implements flag.Var.
   178  // It implements Set by updating both
   179  // BuildToolchain and buildContext.Compiler.
   180  type buildCompiler struct{}
   181  
   182  func (c buildCompiler) Set(value string) error {
   183  	switch value {
   184  	case "gc":
   185  		BuildToolchain = gcToolchain{}
   186  	case "gccgo":
   187  		BuildToolchain = gccgoToolchain{}
   188  	default:
   189  		return fmt.Errorf("unknown compiler %q", value)
   190  	}
   191  	cfg.BuildToolchainName = value
   192  	cfg.BuildToolchainCompiler = BuildToolchain.compiler
   193  	cfg.BuildToolchainLinker = BuildToolchain.linker
   194  	cfg.BuildContext.Compiler = value
   195  	return nil
   196  }
   197  
   198  func (c buildCompiler) String() string {
   199  	return cfg.BuildContext.Compiler
   200  }
   201  
   202  func init() {
   203  	switch build.Default.Compiler {
   204  	case "gc", "gccgo":
   205  		buildCompiler{}.Set(build.Default.Compiler)
   206  	}
   207  }
   208  
   209  // addBuildFlags adds the flags common to the build, clean, get,
   210  // install, list, run, and test commands.
   211  func AddBuildFlags(cmd *base.Command) {
   212  	cmd.Flag.BoolVar(&cfg.BuildA, "a", false, "")
   213  	cmd.Flag.BoolVar(&cfg.BuildN, "n", false, "")
   214  	cmd.Flag.IntVar(&cfg.BuildP, "p", cfg.BuildP, "")
   215  	cmd.Flag.BoolVar(&cfg.BuildV, "v", false, "")
   216  	cmd.Flag.BoolVar(&cfg.BuildX, "x", false, "")
   217  
   218  	cmd.Flag.Var(&load.BuildAsmflags, "asmflags", "")
   219  	cmd.Flag.Var(buildCompiler{}, "compiler", "")
   220  	cmd.Flag.StringVar(&cfg.BuildBuildmode, "buildmode", "default", "")
   221  	cmd.Flag.Var(&load.BuildGcflags, "gcflags", "")
   222  	cmd.Flag.Var(&load.BuildGccgoflags, "gccgoflags", "")
   223  	cmd.Flag.StringVar(&cfg.BuildMod, "mod", "", "")
   224  	cmd.Flag.StringVar(&cfg.BuildContext.InstallSuffix, "installsuffix", "", "")
   225  	cmd.Flag.Var(&load.BuildLdflags, "ldflags", "")
   226  	cmd.Flag.BoolVar(&cfg.BuildLinkshared, "linkshared", false, "")
   227  	cmd.Flag.StringVar(&cfg.BuildPkgdir, "pkgdir", "", "")
   228  	cmd.Flag.BoolVar(&cfg.BuildRace, "race", false, "")
   229  	cmd.Flag.BoolVar(&cfg.BuildMSan, "msan", false, "")
   230  	cmd.Flag.Var((*base.StringsFlag)(&cfg.BuildContext.BuildTags), "tags", "")
   231  	cmd.Flag.Var((*base.StringsFlag)(&cfg.BuildToolexec), "toolexec", "")
   232  	cmd.Flag.BoolVar(&cfg.BuildWork, "work", false, "")
   233  
   234  	// Undocumented, unstable debugging flags.
   235  	cmd.Flag.StringVar(&cfg.DebugActiongraph, "debug-actiongraph", "", "")
   236  }
   237  
   238  // fileExtSplit expects a filename and returns the name
   239  // and ext (without the dot). If the file has no
   240  // extension, ext will be empty.
   241  func fileExtSplit(file string) (name, ext string) {
   242  	dotExt := filepath.Ext(file)
   243  	name = file[:len(file)-len(dotExt)]
   244  	if dotExt != "" {
   245  		ext = dotExt[1:]
   246  	}
   247  	return
   248  }
   249  
   250  func pkgsMain(pkgs []*load.Package) (res []*load.Package) {
   251  	for _, p := range pkgs {
   252  		if p.Name == "main" {
   253  			res = append(res, p)
   254  		}
   255  	}
   256  	return res
   257  }
   258  
   259  func pkgsNotMain(pkgs []*load.Package) (res []*load.Package) {
   260  	for _, p := range pkgs {
   261  		if p.Name != "main" {
   262  			res = append(res, p)
   263  		}
   264  	}
   265  	return res
   266  }
   267  
   268  func oneMainPkg(pkgs []*load.Package) []*load.Package {
   269  	if len(pkgs) != 1 || pkgs[0].Name != "main" {
   270  		base.Fatalf("-buildmode=%s requires exactly one main package", cfg.BuildBuildmode)
   271  	}
   272  	return pkgs
   273  }
   274  
   275  var pkgsFilter = func(pkgs []*load.Package) []*load.Package { return pkgs }
   276  
   277  var runtimeVersion = runtime.Version()
   278  
   279  func runBuild(cmd *base.Command, args []string) {
   280  	BuildInit()
   281  	var b Builder
   282  	b.Init()
   283  
   284  	pkgs := load.PackagesForBuild(args)
   285  
   286  	if len(pkgs) == 1 && pkgs[0].Name == "main" && cfg.BuildO == "" {
   287  		cfg.BuildO = load.DefaultExecName(pkgs[0].ImportPath)
   288  		cfg.BuildO += cfg.ExeSuffix
   289  	}
   290  
   291  	// sanity check some often mis-used options
   292  	switch cfg.BuildContext.Compiler {
   293  	case "gccgo":
   294  		if load.BuildGcflags.Present() {
   295  			fmt.Println("go build: when using gccgo toolchain, please pass compiler flags using -gccgoflags, not -gcflags")
   296  		}
   297  		if load.BuildLdflags.Present() {
   298  			fmt.Println("go build: when using gccgo toolchain, please pass linker flags using -gccgoflags, not -ldflags")
   299  		}
   300  	case "gc":
   301  		if load.BuildGccgoflags.Present() {
   302  			fmt.Println("go build: when using gc toolchain, please pass compile flags using -gcflags, and linker flags using -ldflags")
   303  		}
   304  	}
   305  
   306  	depMode := ModeBuild
   307  	if cfg.BuildI {
   308  		depMode = ModeInstall
   309  	}
   310  
   311  	pkgs = omitTestOnly(pkgsFilter(load.Packages(args)))
   312  
   313  	// Special case -o /dev/null by not writing at all.
   314  	if cfg.BuildO == os.DevNull {
   315  		cfg.BuildO = ""
   316  	}
   317  
   318  	if cfg.BuildO != "" {
   319  		if len(pkgs) > 1 {
   320  			base.Fatalf("go build: cannot use -o with multiple packages")
   321  		} else if len(pkgs) == 0 {
   322  			base.Fatalf("no packages to build")
   323  		}
   324  		p := pkgs[0]
   325  		p.Target = cfg.BuildO
   326  		p.Stale = true // must build - not up to date
   327  		p.StaleReason = "build -o flag in use"
   328  		a := b.AutoAction(ModeInstall, depMode, p)
   329  		b.Do(a)
   330  		return
   331  	}
   332  
   333  	a := &Action{Mode: "go build"}
   334  	for _, p := range pkgs {
   335  		a.Deps = append(a.Deps, b.AutoAction(ModeBuild, depMode, p))
   336  	}
   337  	if cfg.BuildBuildmode == "shared" {
   338  		a = b.buildmodeShared(ModeBuild, depMode, args, pkgs, a)
   339  	}
   340  	b.Do(a)
   341  }
   342  
   343  var CmdInstall = &base.Command{
   344  	UsageLine: "go install [-i] [build flags] [packages]",
   345  	Short:     "compile and install packages and dependencies",
   346  	Long: `
   347  Install compiles and installs the packages named by the import paths.
   348  
   349  The -i flag installs the dependencies of the named packages as well.
   350  
   351  For more about the build flags, see 'go help build'.
   352  For more about specifying packages, see 'go help packages'.
   353  
   354  See also: go build, go get, go clean.
   355  	`,
   356  }
   357  
   358  // libname returns the filename to use for the shared library when using
   359  // -buildmode=shared. The rules we use are:
   360  // Use arguments for special 'meta' packages:
   361  //	std --> libstd.so
   362  //	std cmd --> libstd,cmd.so
   363  // A single non-meta argument with trailing "/..." is special cased:
   364  //	foo/... --> libfoo.so
   365  //	(A relative path like "./..."  expands the "." first)
   366  // Use import paths for other cases, changing '/' to '-':
   367  //	somelib --> libsubdir-somelib.so
   368  //	./ or ../ --> libsubdir-somelib.so
   369  //	gopkg.in/tomb.v2 -> libgopkg.in-tomb.v2.so
   370  //	a/... b/... ---> liba/c,b/d.so - all matching import paths
   371  // Name parts are joined with ','.
   372  func libname(args []string, pkgs []*load.Package) (string, error) {
   373  	var libname string
   374  	appendName := func(arg string) {
   375  		if libname == "" {
   376  			libname = arg
   377  		} else {
   378  			libname += "," + arg
   379  		}
   380  	}
   381  	var haveNonMeta bool
   382  	for _, arg := range args {
   383  		if search.IsMetaPackage(arg) {
   384  			appendName(arg)
   385  		} else {
   386  			haveNonMeta = true
   387  		}
   388  	}
   389  	if len(libname) == 0 { // non-meta packages only. use import paths
   390  		if len(args) == 1 && strings.HasSuffix(args[0], "/...") {
   391  			// Special case of "foo/..." as mentioned above.
   392  			arg := strings.TrimSuffix(args[0], "/...")
   393  			if build.IsLocalImport(arg) {
   394  				cwd, _ := os.Getwd()
   395  				bp, _ := cfg.BuildContext.ImportDir(filepath.Join(cwd, arg), build.FindOnly)
   396  				if bp.ImportPath != "" && bp.ImportPath != "." {
   397  					arg = bp.ImportPath
   398  				}
   399  			}
   400  			appendName(strings.ReplaceAll(arg, "/", "-"))
   401  		} else {
   402  			for _, pkg := range pkgs {
   403  				appendName(strings.ReplaceAll(pkg.ImportPath, "/", "-"))
   404  			}
   405  		}
   406  	} else if haveNonMeta { // have both meta package and a non-meta one
   407  		return "", errors.New("mixing of meta and non-meta packages is not allowed")
   408  	}
   409  	// TODO(mwhudson): Needs to change for platforms that use different naming
   410  	// conventions...
   411  	return "lib" + libname + ".so", nil
   412  }
   413  
   414  func runInstall(cmd *base.Command, args []string) {
   415  	BuildInit()
   416  	InstallPackages(args, load.PackagesForBuild(args))
   417  }
   418  
   419  // omitTestOnly returns pkgs with test-only packages removed.
   420  func omitTestOnly(pkgs []*load.Package) []*load.Package {
   421  	var list []*load.Package
   422  	for _, p := range pkgs {
   423  		if len(p.GoFiles)+len(p.CgoFiles) == 0 && !p.Internal.CmdlinePkgLiteral {
   424  			// Package has no source files,
   425  			// perhaps due to build tags or perhaps due to only having *_test.go files.
   426  			// Also, it is only being processed as the result of a wildcard match
   427  			// like ./..., not because it was listed as a literal path on the command line.
   428  			// Ignore it.
   429  			continue
   430  		}
   431  		list = append(list, p)
   432  	}
   433  	return list
   434  }
   435  
   436  func InstallPackages(patterns []string, pkgs []*load.Package) {
   437  	if cfg.GOBIN != "" && !filepath.IsAbs(cfg.GOBIN) {
   438  		base.Fatalf("cannot install, GOBIN must be an absolute path")
   439  	}
   440  
   441  	pkgs = omitTestOnly(pkgsFilter(pkgs))
   442  	for _, p := range pkgs {
   443  		if p.Target == "" {
   444  			switch {
   445  			case p.Standard && p.ImportPath == "unsafe":
   446  				// unsafe is a built-in package, has no target
   447  			case p.Name != "main" && p.Internal.Local && p.ConflictDir == "":
   448  				// Non-executables outside GOPATH need not have a target:
   449  				// we can use the cache to hold the built package archive for use in future builds.
   450  				// The ones inside GOPATH should have a target (in GOPATH/pkg)
   451  				// or else something is wrong and worth reporting (like a ConflictDir).
   452  			case p.Name != "main" && p.Module != nil:
   453  				// Non-executables have no target (except the cache) when building with modules.
   454  			case p.Internal.GobinSubdir:
   455  				base.Errorf("go %s: cannot install cross-compiled binaries when GOBIN is set", cfg.CmdName)
   456  			case p.Internal.CmdlineFiles:
   457  				base.Errorf("go %s: no install location for .go files listed on command line (GOBIN not set)", cfg.CmdName)
   458  			case p.ConflictDir != "":
   459  				base.Errorf("go %s: no install location for %s: hidden by %s", cfg.CmdName, p.Dir, p.ConflictDir)
   460  			default:
   461  				base.Errorf("go %s: no install location for directory %s outside GOPATH\n"+
   462  					"\tFor more details see: 'go help gopath'", cfg.CmdName, p.Dir)
   463  			}
   464  		}
   465  	}
   466  	base.ExitIfErrors()
   467  
   468  	var b Builder
   469  	b.Init()
   470  	depMode := ModeBuild
   471  	if cfg.BuildI {
   472  		depMode = ModeInstall
   473  	}
   474  	a := &Action{Mode: "go install"}
   475  	var tools []*Action
   476  	for _, p := range pkgs {
   477  		// If p is a tool, delay the installation until the end of the build.
   478  		// This avoids installing assemblers/compilers that are being executed
   479  		// by other steps in the build.
   480  		a1 := b.AutoAction(ModeInstall, depMode, p)
   481  		if load.InstallTargetDir(p) == load.ToTool {
   482  			a.Deps = append(a.Deps, a1.Deps...)
   483  			a1.Deps = append(a1.Deps, a)
   484  			tools = append(tools, a1)
   485  			continue
   486  		}
   487  		a.Deps = append(a.Deps, a1)
   488  	}
   489  	if len(tools) > 0 {
   490  		a = &Action{
   491  			Mode: "go install (tools)",
   492  			Deps: tools,
   493  		}
   494  	}
   495  
   496  	if cfg.BuildBuildmode == "shared" {
   497  		// Note: If buildmode=shared then only non-main packages
   498  		// are present in the pkgs list, so all the special case code about
   499  		// tools above did not apply, and a is just a simple Action
   500  		// with a list of Deps, one per package named in pkgs,
   501  		// the same as in runBuild.
   502  		a = b.buildmodeShared(ModeInstall, ModeInstall, patterns, pkgs, a)
   503  	}
   504  
   505  	b.Do(a)
   506  	base.ExitIfErrors()
   507  
   508  	// Success. If this command is 'go install' with no arguments
   509  	// and the current directory (the implicit argument) is a command,
   510  	// remove any leftover command binary from a previous 'go build'.
   511  	// The binary is installed; it's not needed here anymore.
   512  	// And worse it might be a stale copy, which you don't want to find
   513  	// instead of the installed one if $PATH contains dot.
   514  	// One way to view this behavior is that it is as if 'go install' first
   515  	// runs 'go build' and the moves the generated file to the install dir.
   516  	// See issue 9645.
   517  	if len(patterns) == 0 && len(pkgs) == 1 && pkgs[0].Name == "main" {
   518  		// Compute file 'go build' would have created.
   519  		// If it exists and is an executable file, remove it.
   520  		targ := load.DefaultExecName(pkgs[0].ImportPath)
   521  		targ += cfg.ExeSuffix
   522  		if filepath.Join(pkgs[0].Dir, targ) != pkgs[0].Target { // maybe $GOBIN is the current directory
   523  			fi, err := os.Stat(targ)
   524  			if err == nil {
   525  				m := fi.Mode()
   526  				if m.IsRegular() {
   527  					if m&0111 != 0 || cfg.Goos == "windows" { // windows never sets executable bit
   528  						os.Remove(targ)
   529  					}
   530  				}
   531  			}
   532  		}
   533  	}
   534  }
   535  
   536  // ExecCmd is the command to use to run user binaries.
   537  // Normally it is empty, meaning run the binaries directly.
   538  // If cross-compiling and running on a remote system or
   539  // simulator, it is typically go_GOOS_GOARCH_exec, with
   540  // the target GOOS and GOARCH substituted.
   541  // The -exec flag overrides these defaults.
   542  var ExecCmd []string
   543  
   544  // FindExecCmd derives the value of ExecCmd to use.
   545  // It returns that value and leaves ExecCmd set for direct use.
   546  func FindExecCmd() []string {
   547  	if ExecCmd != nil {
   548  		return ExecCmd
   549  	}
   550  	ExecCmd = []string{} // avoid work the second time
   551  	if cfg.Goos == runtime.GOOS && cfg.Goarch == runtime.GOARCH {
   552  		return ExecCmd
   553  	}
   554  	path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", cfg.Goos, cfg.Goarch))
   555  	if err == nil {
   556  		ExecCmd = []string{path}
   557  	}
   558  	return ExecCmd
   559  }