...

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

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

View as plain text