Skip to content

Commit

Permalink
feat(platforms): fallback to x86_64 before bazel version 4.1.0 on App… (
Browse files Browse the repository at this point in the history
#299)

* feat(platforms): fallback to x86_64 before bazel version 4.1.0 on Apple Silicon

* add test target in BUILD

* fix build

* rm return value err

* refactor: add log, rename sem_ver to semver

* refactor: add log
  • Loading branch information
xinnjie authored Mar 9, 2022
1 parent 81f38ed commit 15abf0e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
11 changes: 10 additions & 1 deletion platforms/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["platforms.go"],
importpath = "github.com/bazelbuild/bazelisk/platforms",
visibility = ["//visibility:public"],
deps = [
"@com_github_hashicorp_go_version//:go_default_library",
],
)

go_test(
name = "platforms_test",
srcs = ["platforms_test.go"],
embed = [":go_default_library"],
)
22 changes: 22 additions & 0 deletions platforms/platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package platforms

import (
"fmt"
semver "github.com/hashicorp/go-version"
"log"
"runtime"
)

Expand Down Expand Up @@ -43,10 +45,30 @@ func DetermineBazelFilename(version string, includeSuffix bool) (string, error)
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
}
56 changes: 56 additions & 0 deletions platforms/platforms_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package platforms

import "testing"

func TestDarwinFallback(t *testing.T) {
type args struct {
machineName string
version string
}
tests := []struct {
name string
args args
wantAlterMachineName string
}{
{
name: "before 4.1.0, x86_64 do not fallback",
args: args{
machineName: "x86_64",
version: "4.0.1",
},
wantAlterMachineName: "x86_64",
},
{
name: "since 4.1.0, x86_64 do not fallback either",
args: args{
machineName: "x86_64",
version: "4.1.0",
},
wantAlterMachineName: "x86_64",
},
{
name: "before 4.1.0, arm64 not supported, fallback to x86_64 on arm64",
args: args{
machineName: "arm64",
version: "4.0.1",
},
wantAlterMachineName: "x86_64",
},
{
name: "since 4.1.0, arm64 supported, do not fallback",
args: args{
machineName: "arm64",
version: "4.1.0",
},
wantAlterMachineName: "arm64",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotAlterMachineName := DarwinFallback(tt.args.machineName, tt.args.version)
if gotAlterMachineName != tt.wantAlterMachineName {
t.Errorf("DarwinFallback() gotAlterMachineName = %v, want %v", gotAlterMachineName, tt.wantAlterMachineName)
}
})
}
}

0 comments on commit 15abf0e

Please sign in to comment.