github.com/corona10/go@v0.0.0-20180224231303-7a218942be57/src/cmd/go/internal/work/init.go (about)

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