github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/internal/platform/supported.go (about)

     1  // Copyright 2018 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 platform
     6  
     7  // RaceDetectorSupported reports whether goos/goarch supports the race
     8  // detector. There is a copy of this function in cmd/dist/test.go.
     9  // Race detector only supports 48-bit VMA on arm64. But it will always
    10  // return true for arm64, because we don't have VMA size information during
    11  // the compile time.
    12  func RaceDetectorSupported(goos, goarch string) bool {
    13  	switch goos {
    14  	case "linux":
    15  		return goarch == "amd64" || goarch == "ppc64le" || goarch == "arm64" || goarch == "s390x"
    16  	case "darwin":
    17  		return goarch == "amd64" || goarch == "arm64"
    18  	case "freebsd", "netbsd", "openbsd", "windows":
    19  		return goarch == "amd64"
    20  	default:
    21  		return false
    22  	}
    23  }
    24  
    25  // MSanSupported reports whether goos/goarch supports the memory
    26  // sanitizer option.
    27  // There is a copy of this function in misc/cgo/testsanitizers/cc_test.go.
    28  func MSanSupported(goos, goarch string) bool {
    29  	switch goos {
    30  	case "linux":
    31  		return goarch == "amd64" || goarch == "arm64"
    32  	case "freebsd":
    33  		return goarch == "amd64"
    34  	default:
    35  		return false
    36  	}
    37  }
    38  
    39  // ASanSupported reports whether goos/goarch supports the address
    40  // sanitizer option.
    41  // There is a copy of this function in misc/cgo/testsanitizers/cc_test.go.
    42  func ASanSupported(goos, goarch string) bool {
    43  	switch goos {
    44  	case "linux":
    45  		return goarch == "arm64" || goarch == "amd64" || goarch == "riscv64" || goarch == "ppc64le"
    46  	default:
    47  		return false
    48  	}
    49  }
    50  
    51  // FuzzSupported reports whether goos/goarch supports fuzzing
    52  // ('go test -fuzz=.').
    53  func FuzzSupported(goos, goarch string) bool {
    54  	switch goos {
    55  	case "darwin", "freebsd", "linux", "windows":
    56  		return true
    57  	default:
    58  		return false
    59  	}
    60  }
    61  
    62  // FuzzInstrumented reports whether fuzzing on goos/goarch uses coverage
    63  // instrumentation. (FuzzInstrumented implies FuzzSupported.)
    64  func FuzzInstrumented(goos, goarch string) bool {
    65  	switch goarch {
    66  	case "amd64", "arm64":
    67  		// TODO(#14565): support more architectures.
    68  		return FuzzSupported(goos, goarch)
    69  	default:
    70  		return false
    71  	}
    72  }
    73  
    74  // MustLinkExternal reports whether goos/goarch requires external linking
    75  // with or without cgo dependencies.
    76  func MustLinkExternal(goos, goarch string, withCgo bool) bool {
    77  	if withCgo {
    78  		switch goarch {
    79  		case "loong64",
    80  			"mips", "mipsle", "mips64", "mips64le",
    81  			"riscv64":
    82  			// Internally linking cgo is incomplete on some architectures.
    83  			// https://go.dev/issue/14449
    84  			return true
    85  		case "arm64":
    86  			if goos == "windows" {
    87  				// windows/arm64 internal linking is not implemented.
    88  				return true
    89  			}
    90  		case "ppc64":
    91  			// Big Endian PPC64 cgo internal linking is not implemented for aix or linux.
    92  			// https://go.dev/issue/8912
    93  			return true
    94  		}
    95  
    96  		switch goos {
    97  		case "android":
    98  			return true
    99  		case "dragonfly":
   100  			// It seems that on Dragonfly thread local storage is
   101  			// set up by the dynamic linker, so internal cgo linking
   102  			// doesn't work. Test case is "go test runtime/cgo".
   103  			return true
   104  		}
   105  	}
   106  
   107  	switch goos {
   108  	case "android":
   109  		if goarch != "arm64" {
   110  			return true
   111  		}
   112  	case "ios":
   113  		if goarch == "arm64" {
   114  			return true
   115  		}
   116  	}
   117  	return false
   118  }
   119  
   120  // BuildModeSupported reports whether goos/goarch supports the given build mode
   121  // using the given compiler.
   122  // There is a copy of this function in cmd/dist/test.go.
   123  func BuildModeSupported(compiler, buildmode, goos, goarch string) bool {
   124  	if compiler == "gccgo" {
   125  		return true
   126  	}
   127  
   128  	platform := goos + "/" + goarch
   129  	if _, ok := osArchSupportsCgo[platform]; !ok {
   130  		return false // platform unrecognized
   131  	}
   132  
   133  	switch buildmode {
   134  	case "archive":
   135  		return true
   136  
   137  	case "c-archive":
   138  		switch goos {
   139  		case "aix", "darwin", "ios", "windows":
   140  			return true
   141  		case "linux":
   142  			switch goarch {
   143  			case "386", "amd64", "arm", "armbe", "arm64", "arm64be", "loong64", "ppc64le", "riscv64", "s390x":
   144  				// linux/ppc64 not supported because it does
   145  				// not support external linking mode yet.
   146  				return true
   147  			default:
   148  				// Other targets do not support -shared,
   149  				// per ParseFlags in
   150  				// cmd/compile/internal/base/flag.go.
   151  				// For c-archive the Go tool passes -shared,
   152  				// so that the result is suitable for inclusion
   153  				// in a PIE or shared library.
   154  				return false
   155  			}
   156  		case "freebsd":
   157  			return goarch == "amd64"
   158  		}
   159  		return false
   160  
   161  	case "c-shared":
   162  		switch platform {
   163  		case "linux/amd64", "linux/arm", "linux/arm64", "linux/loong64", "linux/386", "linux/ppc64le", "linux/riscv64", "linux/s390x",
   164  			"android/amd64", "android/arm", "android/arm64", "android/386",
   165  			"freebsd/amd64",
   166  			"darwin/amd64", "darwin/arm64",
   167  			"windows/amd64", "windows/386", "windows/arm64":
   168  			return true
   169  		}
   170  		return false
   171  
   172  	case "default":
   173  		return true
   174  
   175  	case "exe":
   176  		return true
   177  
   178  	case "pie":
   179  		switch platform {
   180  		case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/loong64", "linux/ppc64le", "linux/riscv64", "linux/s390x",
   181  			"android/amd64", "android/arm", "android/arm64", "android/386",
   182  			"freebsd/amd64",
   183  			"darwin/amd64", "darwin/arm64",
   184  			"ios/amd64", "ios/arm64",
   185  			"aix/ppc64",
   186  			"windows/386", "windows/amd64", "windows/arm", "windows/arm64":
   187  			return true
   188  		}
   189  		return false
   190  
   191  	case "shared":
   192  		switch platform {
   193  		case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
   194  			return true
   195  		}
   196  		return false
   197  
   198  	case "plugin":
   199  		switch platform {
   200  		case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le",
   201  			"android/amd64", "android/386",
   202  			"darwin/amd64", "darwin/arm64",
   203  			"freebsd/amd64":
   204  			return true
   205  		}
   206  		return false
   207  
   208  	default:
   209  		return false
   210  	}
   211  }
   212  
   213  func InternalLinkPIESupported(goos, goarch string) bool {
   214  	switch goos + "/" + goarch {
   215  	case "android/arm64",
   216  		"darwin/amd64", "darwin/arm64",
   217  		"linux/amd64", "linux/arm64", "linux/ppc64le",
   218  		"windows/386", "windows/amd64", "windows/arm", "windows/arm64":
   219  		return true
   220  	}
   221  	return false
   222  }
   223  
   224  // DefaultPIE reports whether goos/goarch produces a PIE binary when using the
   225  // "default" buildmode. On Windows this is affected by -race,
   226  // so force the caller to pass that in to centralize that choice.
   227  func DefaultPIE(goos, goarch string, isRace bool) bool {
   228  	switch goos {
   229  	case "android", "ios":
   230  		return true
   231  	case "windows":
   232  		if isRace {
   233  			// PIE is not supported with -race on windows;
   234  			// see https://go.dev/cl/416174.
   235  			return false
   236  		}
   237  		return true
   238  	case "darwin":
   239  		return goarch == "arm64"
   240  	}
   241  	return false
   242  }
   243  
   244  // CgoSupported reports whether goos/goarch supports cgo.
   245  func CgoSupported(goos, goarch string) bool {
   246  	return osArchSupportsCgo[goos+"/"+goarch]
   247  }
   248  
   249  // ExecutableHasDWARF reports whether the linked executable includes DWARF
   250  // symbols on goos/goarch.
   251  func ExecutableHasDWARF(goos, goarch string) bool {
   252  	switch goos {
   253  	case "plan9", "ios":
   254  		return false
   255  	}
   256  	return true
   257  }