-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathplatforms.go
74 lines (62 loc) · 2.16 KB
/
platforms.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Package platforms determins file names and extensions based on the current operating system.
package platforms
import (
"fmt"
semver "github.com/hashicorp/go-version"
"log"
"runtime"
)
var platforms = map[string]string{"darwin": "macos", "linux": "ubuntu1404", "windows": "windows"}
// GetPlatform returns a Bazel CI-compatible platform identifier for the current operating system.
// TODO(fweikert): raise an error for unsupported platforms
func GetPlatform() string {
return platforms[runtime.GOOS]
}
// DetermineExecutableFilenameSuffix returns the extension for binaries on the current operating system.
func DetermineExecutableFilenameSuffix() string {
filenameSuffix := ""
if runtime.GOOS == "windows" {
filenameSuffix = ".exe"
}
return filenameSuffix
}
// DetermineBazelFilename returns the correct file name of a local Bazel binary.
func DetermineBazelFilename(version string, includeSuffix bool) (string, error) {
var machineName string
switch runtime.GOARCH {
case "amd64":
machineName = "x86_64"
case "arm64":
machineName = "arm64"
default:
return "", fmt.Errorf("unsupported machine architecture \"%s\", must be arm64 or x86_64", runtime.GOARCH)
}
var osName string
switch runtime.GOOS {
case "darwin", "linux", "windows":
osName = runtime.GOOS
default:
return "", fmt.Errorf("unsupported operating system \"%s\", must be Linux, macOS or Windows", runtime.GOOS)
}
if osName == "darwin" {
machineName = DarwinFallback(machineName, version)
}
var filenameSuffix string
if includeSuffix {
filenameSuffix = DetermineExecutableFilenameSuffix()
}
return fmt.Sprintf("bazel-%s-%s-%s%s", version, osName, machineName, filenameSuffix), nil
}
// DarwinFallback Darwin arm64 was supported since 4.1.0, before 4.1.0, fall back to x86_64
func DarwinFallback(machineName string, version string) (alterMachineName string) {
v, err := semver.NewVersion(version)
if err != nil {
return machineName
}
armSupportVer, _ := semver.NewVersion("4.1.0")
if machineName == "arm64" && v.LessThan(armSupportVer) {
log.Printf("WARN: Fallback to x86_64 because arm64 is not supported on Apple Silicon until 4.1.0")
return "x86_64"
}
return machineName
}