Skip to content

Commit

Permalink
Replace arm64 minor variant logic with lookup table
Browse files Browse the repository at this point in the history
Signed-off-by: Chongyi Zheng <git@zcy.dev>
  • Loading branch information
harryzcy committed Nov 5, 2024
1 parent 9b464c3 commit 5cffce7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 68 deletions.
120 changes: 67 additions & 53 deletions compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ type MatchComparer interface {
Less(specs.Platform, specs.Platform) bool
}

// arm64PlatformVector returns an (order) vector of platforms
// for v[major].[minor] down to v[major].0.
func arm64PlatformVector(platform specs.Platform, major, minor int) []specs.Platform {
vector := make([]specs.Platform, 0)
for ; minor >= 0; minor-- {
arm64Variant := "v" + strconv.Itoa(major) + "." + strconv.Itoa(minor)
if minor == 0 {
arm64Variant = "v" + strconv.Itoa(major)
}
vector = append(vector, specs.Platform{
Architecture: "arm64",
OS: platform.OS,
OSVersion: platform.OSVersion,
OSFeatures: platform.OSFeatures,
Variant: arm64Variant,
})
}
return vector
}

// platformVector returns an (ordered) vector of appropriate specs.Platform
// objects to try matching for the given platform object (see platforms.Only).
func platformVector(platform specs.Platform) []specs.Platform {
Expand Down Expand Up @@ -73,63 +93,57 @@ func platformVector(platform specs.Platform) []specs.Platform {
variant = "v8"
}

majorVariant, minorVariant, hasMinor := strings.Cut(variant, ".")
if armMajor, err := strconv.Atoi(strings.TrimPrefix(majorVariant, "v")); err == nil && armMajor >= 8 {
armMinor := 0
if len(variant) == 4 {
if minor, err := strconv.Atoi(minorVariant); err == nil && hasMinor {
armMinor = minor
}
}

if armMajor == 9 {
for minor := armMinor - 1; minor >= 0; minor-- {
arm64Variant := "v" + strconv.Itoa(armMajor) + "." + strconv.Itoa(minor)
if minor == 0 {
arm64Variant = "v" + strconv.Itoa(armMajor)
}
vector = append(vector, specs.Platform{
Architecture: platform.Architecture,
OS: platform.OS,
OSVersion: platform.OSVersion,
OSFeatures: platform.OSFeatures,
Variant: arm64Variant,
})
}

// v9.0 diverged from v8.5, meaning that v9.x is compatible with v8.{x+5} until v9.4/v8.9
armMinor = armMinor + 5
if armMinor > 9 {
armMinor = 9
}
armMajor = 8
vector = append(vector, specs.Platform{
Architecture: platform.Architecture,
OS: platform.OS,
OSVersion: platform.OSVersion,
OSFeatures: platform.OSFeatures,
Variant: "v8." + strconv.Itoa(armMinor),
})
}

for minor := armMinor - 1; minor >= 0; minor-- {
arm64Variant := "v" + strconv.Itoa(armMajor) + "." + strconv.Itoa(minor)
if minor == 0 {
arm64Variant = "v" + strconv.Itoa(armMajor)
}
vector = append(vector, specs.Platform{
Architecture: platform.Architecture,
OS: platform.OS,
OSVersion: platform.OSVersion,
OSFeatures: platform.OSFeatures,
Variant: arm64Variant,
})
}
vector = []specs.Platform{} // Reset vector, `arm64PlatformVector` will add the first variant
switch variant {
case "v8", "v8.0":
vector = append(vector, arm64PlatformVector(platform, 8, 0)...)
case "v8.1":
vector = append(vector, arm64PlatformVector(platform, 8, 1)...)
case "v8.2":
vector = append(vector, arm64PlatformVector(platform, 8, 2)...)
case "v8.3":
vector = append(vector, arm64PlatformVector(platform, 8, 3)...)
case "v8.4":
vector = append(vector, arm64PlatformVector(platform, 8, 4)...)
case "v8.5":
vector = append(vector, arm64PlatformVector(platform, 8, 5)...)
case "v8.6":
vector = append(vector, arm64PlatformVector(platform, 8, 6)...)
case "v8.7":
vector = append(vector, arm64PlatformVector(platform, 8, 7)...)
case "v8.8":
vector = append(vector, arm64PlatformVector(platform, 8, 8)...)
case "v8.9":
vector = append(vector, arm64PlatformVector(platform, 8, 9)...)
case "v9", "v9.0":
vector = append(vector, arm64PlatformVector(platform, 9, 0)...)
vector = append(vector, arm64PlatformVector(platform, 8, 5)...)
case "v9.1":
vector = append(vector, arm64PlatformVector(platform, 9, 1)...)
vector = append(vector, arm64PlatformVector(platform, 8, 6)...)
case "v9.2":
vector = append(vector, arm64PlatformVector(platform, 9, 2)...)
vector = append(vector, arm64PlatformVector(platform, 8, 7)...)
case "v9.3":
vector = append(vector, arm64PlatformVector(platform, 9, 3)...)
vector = append(vector, arm64PlatformVector(platform, 8, 8)...)
case "v9.4":
vector = append(vector, arm64PlatformVector(platform, 9, 4)...)
vector = append(vector, arm64PlatformVector(platform, 8, 9)...)
case "v9.5":
vector = append(vector, arm64PlatformVector(platform, 9, 5)...)
vector = append(vector, arm64PlatformVector(platform, 8, 9)...)
case "v9.6":
vector = append(vector, arm64PlatformVector(platform, 9, 6)...)
vector = append(vector, arm64PlatformVector(platform, 8, 9)...)
case "v9.7":
vector = append(vector, arm64PlatformVector(platform, 9, 7)...)
vector = append(vector, arm64PlatformVector(platform, 8, 9)...)
}

// All arm64/v8.x and arm64/v9.x are compatible with arm/v8 (32-bits) and below.
// There's no arm64 v9 variant, so it's normalized to v8.
if strings.HasPrefix(variant, "v8.") || strings.HasPrefix(variant, "v9.") {
if strings.HasPrefix(variant, "v8") || strings.HasPrefix(variant, "v9") {
variant = "v8"
}
vector = append(vector, platformVector(specs.Platform{
Expand Down
19 changes: 4 additions & 15 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,11 @@ func normalizeArch(arch, variant string) (string, string) {
}
case "aarch64", "arm64":
arch = "arm64"
majorVariant, minorVariant, hasMinor := strings.Cut(variant, ".")
majorVariant = strings.TrimPrefix(majorVariant, "v")
if minorVariant == "0" {
minorVariant = ""
hasMinor = false
}

if (majorVariant == "" || majorVariant == "8") && !hasMinor {
// normalize v8 to empty string
switch variant {
case "8", "v8", "v8.0":
variant = ""
} else {
// otherwise to v8.x or v9 or v9.x
variant = "v" + majorVariant
if hasMinor {
variant = variant + "." + minorVariant
}
case "9", "9.0", "v9.0":
variant = "v9"
}
case "armhf":
arch = "arm"
Expand Down

0 comments on commit 5cffce7

Please sign in to comment.