Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/go/internal/work/init.go

Documentation: cmd/go/internal/work

     1  // Copyright 2017 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  // Build initialization (after flag parsing).
     6  
     7  package work
     8  
     9  import (
    10  	"cmd/go/internal/base"
    11  	"cmd/go/internal/cfg"
    12  	"cmd/go/internal/fsys"
    13  	"cmd/go/internal/modload"
    14  	"cmd/internal/sys"
    15  	"flag"
    16  	"fmt"
    17  	"os"
    18  	"path/filepath"
    19  	"runtime"
    20  )
    21  
    22  func BuildInit() {
    23  	modload.Init()
    24  	instrumentInit()
    25  	buildModeInit()
    26  	if err := fsys.Init(base.Cwd()); err != nil {
    27  		base.Fatalf("go: %v", err)
    28  	}
    29  
    30  	// Make sure -pkgdir is absolute, because we run commands
    31  	// in different directories.
    32  	if cfg.BuildPkgdir != "" && !filepath.IsAbs(cfg.BuildPkgdir) {
    33  		p, err := filepath.Abs(cfg.BuildPkgdir)
    34  		if err != nil {
    35  			fmt.Fprintf(os.Stderr, "go %s: evaluating -pkgdir: %v\n", flag.Args()[0], err)
    36  			base.SetExitStatus(2)
    37  			base.Exit()
    38  		}
    39  		cfg.BuildPkgdir = p
    40  	}
    41  
    42  	// Make sure CC and CXX are absolute paths
    43  	for _, key := range []string{"CC", "CXX"} {
    44  		if path := cfg.Getenv(key); !filepath.IsAbs(path) && path != "" && path != filepath.Base(path) {
    45  			base.Fatalf("go %s: %s environment variable is relative; must be absolute path: %s\n", flag.Args()[0], key, path)
    46  		}
    47  	}
    48  }
    49  
    50  func instrumentInit() {
    51  	if !cfg.BuildRace && !cfg.BuildMSan {
    52  		return
    53  	}
    54  	if cfg.BuildRace && cfg.BuildMSan {
    55  		fmt.Fprintf(os.Stderr, "go %s: may not use -race and -msan simultaneously\n", flag.Args()[0])
    56  		base.SetExitStatus(2)
    57  		base.Exit()
    58  	}
    59  	if cfg.BuildMSan && !sys.MSanSupported(cfg.Goos, cfg.Goarch) {
    60  		fmt.Fprintf(os.Stderr, "-msan is not supported on %s/%s\n", cfg.Goos, cfg.Goarch)
    61  		base.SetExitStatus(2)
    62  		base.Exit()
    63  	}
    64  	if cfg.BuildRace {
    65  		if !sys.RaceDetectorSupported(cfg.Goos, cfg.Goarch) {
    66  			fmt.Fprintf(os.Stderr, "go %s: -race is only supported on linux/amd64, linux/ppc64le, linux/arm64, freebsd/amd64, netbsd/amd64, darwin/amd64, darwin/arm64, and windows/amd64\n", flag.Args()[0])
    67  			base.SetExitStatus(2)
    68  			base.Exit()
    69  		}
    70  	}
    71  	mode := "race"
    72  	if cfg.BuildMSan {
    73  		mode = "msan"
    74  		// MSAN does not support non-PIE binaries on ARM64.
    75  		// See issue #33712 for details.
    76  		if cfg.Goos == "linux" && cfg.Goarch == "arm64" && cfg.BuildBuildmode == "default" {
    77  			cfg.BuildBuildmode = "pie"
    78  		}
    79  	}
    80  	modeFlag := "-" + mode
    81  
    82  	if !cfg.BuildContext.CgoEnabled {
    83  		if runtime.GOOS != cfg.Goos || runtime.GOARCH != cfg.Goarch {
    84  			fmt.Fprintf(os.Stderr, "go %s: %s requires cgo\n", flag.Args()[0], modeFlag)
    85  		} else {
    86  			fmt.Fprintf(os.Stderr, "go %s: %s requires cgo; enable cgo by setting CGO_ENABLED=1\n", flag.Args()[0], modeFlag)
    87  		}
    88  
    89  		base.SetExitStatus(2)
    90  		base.Exit()
    91  	}
    92  	forcedGcflags = append(forcedGcflags, modeFlag)
    93  	forcedLdflags = append(forcedLdflags, modeFlag)
    94  
    95  	if cfg.BuildContext.InstallSuffix != "" {
    96  		cfg.BuildContext.InstallSuffix += "_"
    97  	}
    98  	cfg.BuildContext.InstallSuffix += mode
    99  	cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, mode)
   100  }
   101  
   102  func buildModeInit() {
   103  	gccgo := cfg.BuildToolchainName == "gccgo"
   104  	var codegenArg string
   105  
   106  	// Configure the build mode first, then verify that it is supported.
   107  	// That way, if the flag is completely bogus we will prefer to error out with
   108  	// "-buildmode=%s not supported" instead of naming the specific platform.
   109  
   110  	switch cfg.BuildBuildmode {
   111  	case "archive":
   112  		pkgsFilter = pkgsNotMain
   113  	case "c-archive":
   114  		pkgsFilter = oneMainPkg
   115  		if gccgo {
   116  			codegenArg = "-fPIC"
   117  		} else {
   118  			switch cfg.Goos {
   119  			case "darwin", "ios":
   120  				switch cfg.Goarch {
   121  				case "arm64":
   122  					codegenArg = "-shared"
   123  				}
   124  
   125  			case "dragonfly", "freebsd", "illumos", "linux", "netbsd", "openbsd", "solaris":
   126  				// Use -shared so that the result is
   127  				// suitable for inclusion in a PIE or
   128  				// shared library.
   129  				codegenArg = "-shared"
   130  			}
   131  		}
   132  		cfg.ExeSuffix = ".a"
   133  		ldBuildmode = "c-archive"
   134  	case "c-shared":
   135  		pkgsFilter = oneMainPkg
   136  		if gccgo {
   137  			codegenArg = "-fPIC"
   138  		} else {
   139  			switch cfg.Goos {
   140  			case "linux", "android", "freebsd":
   141  				codegenArg = "-shared"
   142  			case "windows":
   143  				// Do not add usual .exe suffix to the .dll file.
   144  				cfg.ExeSuffix = ""
   145  			}
   146  		}
   147  		ldBuildmode = "c-shared"
   148  	case "default":
   149  		switch cfg.Goos {
   150  		case "android":
   151  			codegenArg = "-shared"
   152  			ldBuildmode = "pie"
   153  		case "windows":
   154  			ldBuildmode = "pie"
   155  		case "ios":
   156  			codegenArg = "-shared"
   157  			ldBuildmode = "pie"
   158  		case "darwin":
   159  			switch cfg.Goarch {
   160  			case "arm64":
   161  				codegenArg = "-shared"
   162  			}
   163  			fallthrough
   164  		default:
   165  			ldBuildmode = "exe"
   166  		}
   167  		if gccgo {
   168  			codegenArg = ""
   169  		}
   170  	case "exe":
   171  		pkgsFilter = pkgsMain
   172  		ldBuildmode = "exe"
   173  		// Set the pkgsFilter to oneMainPkg if the user passed a specific binary output
   174  		// and is using buildmode=exe for a better error message.
   175  		// See issue #20017.
   176  		if cfg.BuildO != "" {
   177  			pkgsFilter = oneMainPkg
   178  		}
   179  	case "pie":
   180  		if cfg.BuildRace {
   181  			base.Fatalf("-buildmode=pie not supported when -race is enabled")
   182  		}
   183  		if gccgo {
   184  			codegenArg = "-fPIE"
   185  		} else {
   186  			switch cfg.Goos {
   187  			case "aix", "windows":
   188  			default:
   189  				codegenArg = "-shared"
   190  			}
   191  		}
   192  		ldBuildmode = "pie"
   193  	case "shared":
   194  		pkgsFilter = pkgsNotMain
   195  		if gccgo {
   196  			codegenArg = "-fPIC"
   197  		} else {
   198  			codegenArg = "-dynlink"
   199  		}
   200  		if cfg.BuildO != "" {
   201  			base.Fatalf("-buildmode=shared and -o not supported together")
   202  		}
   203  		ldBuildmode = "shared"
   204  	case "plugin":
   205  		pkgsFilter = oneMainPkg
   206  		if gccgo {
   207  			codegenArg = "-fPIC"
   208  		} else {
   209  			codegenArg = "-dynlink"
   210  		}
   211  		cfg.ExeSuffix = ".so"
   212  		ldBuildmode = "plugin"
   213  	default:
   214  		base.Fatalf("buildmode=%s not supported", cfg.BuildBuildmode)
   215  	}
   216  
   217  	if !sys.BuildModeSupported(cfg.BuildToolchainName, cfg.BuildBuildmode, cfg.Goos, cfg.Goarch) {
   218  		base.Fatalf("-buildmode=%s not supported on %s/%s\n", cfg.BuildBuildmode, cfg.Goos, cfg.Goarch)
   219  	}
   220  
   221  	if cfg.BuildLinkshared {
   222  		if !sys.BuildModeSupported(cfg.BuildToolchainName, "shared", cfg.Goos, cfg.Goarch) {
   223  			base.Fatalf("-linkshared not supported on %s/%s\n", cfg.Goos, cfg.Goarch)
   224  		}
   225  		if gccgo {
   226  			codegenArg = "-fPIC"
   227  		} else {
   228  			forcedAsmflags = append(forcedAsmflags, "-D=GOBUILDMODE_shared=1",
   229  				"-linkshared")
   230  			codegenArg = "-dynlink"
   231  			forcedGcflags = append(forcedGcflags, "-linkshared")
   232  			// TODO(mwhudson): remove -w when that gets fixed in linker.
   233  			forcedLdflags = append(forcedLdflags, "-linkshared", "-w")
   234  		}
   235  	}
   236  	if codegenArg != "" {
   237  		if gccgo {
   238  			forcedGccgoflags = append([]string{codegenArg}, forcedGccgoflags...)
   239  		} else {
   240  			forcedAsmflags = append([]string{codegenArg}, forcedAsmflags...)
   241  			forcedGcflags = append([]string{codegenArg}, forcedGcflags...)
   242  		}
   243  		// Don't alter InstallSuffix when modifying default codegen args.
   244  		if cfg.BuildBuildmode != "default" || cfg.BuildLinkshared {
   245  			if cfg.BuildContext.InstallSuffix != "" {
   246  				cfg.BuildContext.InstallSuffix += "_"
   247  			}
   248  			cfg.BuildContext.InstallSuffix += codegenArg[1:]
   249  		}
   250  	}
   251  
   252  	switch cfg.BuildMod {
   253  	case "":
   254  		// Behavior will be determined automatically, as if no flag were passed.
   255  	case "readonly", "vendor", "mod":
   256  		if !cfg.ModulesEnabled && !base.InGOFLAGS("-mod") {
   257  			base.Fatalf("build flag -mod=%s only valid when using modules", cfg.BuildMod)
   258  		}
   259  	default:
   260  		base.Fatalf("-mod=%s not supported (can be '', 'mod', 'readonly', or 'vendor')", cfg.BuildMod)
   261  	}
   262  	if !cfg.ModulesEnabled {
   263  		if cfg.ModCacheRW && !base.InGOFLAGS("-modcacherw") {
   264  			base.Fatalf("build flag -modcacherw only valid when using modules")
   265  		}
   266  		if cfg.ModFile != "" && !base.InGOFLAGS("-mod") {
   267  			base.Fatalf("build flag -modfile only valid when using modules")
   268  		}
   269  	}
   270  }
   271  

View as plain text