Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better toolchain version select #58

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cmd/zigbee/firmware/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ func clearWorkDir(workDir string) error {
return nil
}

log.Printf("deleting %s\n", path)

return os.RemoveAll(path)
})
}
75 changes: 66 additions & 9 deletions config/ncs_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package config

import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path"
"slices"
"regexp"
"strconv"

"golang.org/x/exp/maps"
)
Expand Down Expand Up @@ -70,22 +72,24 @@ func providePaths(ncsBase, version string, toolchainItem toolchainTopLevelItem)
return NCSLocation{}, fmt.Errorf("no toolchain versions found in toolchain configuration path %q", toolchainConfigPath(ncsBase))
}

log.Printf("available toolchain versions: %v", availableVersions)
log.Printf("requested toolchain version: %q, available versions: %v", version, availableVersions)

// If version is not present - use default from the toolchain.
// But if it is present - treat it as required.
if version == "" {
version = toolchainItem.DefaultToolchain["ncs_version"]
}
// Get directly requested or default version.
bundleID := versionToIdentifier[version]

// If directly requested version is not present - try to use latest from the same minor version.
if bundleID == "" {
version = toolchainItem.DefaultToolchain["ncs_version"]
version = selectVersion(version, availableVersions)
bundleID = versionToIdentifier[version]
}

if bundleID == "" {
log.Println("toolchain config does not provide default version, and required version was not found, falling back to latest version in config")

slices.Sort(availableVersions)
version = availableVersions[len(availableVersions)-1]

bundleID = versionToIdentifier[version]
return NCSLocation{}, errors.New("required version was not found and no other suitable version is present")
}

return constructPaths(ncsBase, version, bundleID), nil
Expand All @@ -101,6 +105,59 @@ func mapVersions(toolchainItem toolchainTopLevelItem) map[string]string {
return mapped
}

type version [3]uint8

var versionRegx = regexp.MustCompile(`^v(\d+)\.(\d+)(?:\.(\d+))?$`)

func parseVersion(ver string) version {
match := versionRegx.FindStringSubmatch(ver)
if match == nil {
log.Fatalf("incorrect version %q", ver)
}

var result version
for i, part := range match[1:] {
if i == 2 && part == "" {
part = "0"
}

parsed, err := strconv.ParseUint(part, 10, 8)
if err != nil {
log.Fatalf("should not happen: bad part of the version: %q", part)
}

result[i] = uint8(parsed)
}

return result
}

func (v version) greaterOrEqual(other version) bool {
return v[0] == other[0] &&
v[1] == other[1] &&
v[2] >= other[2]
}

func selectVersion(requested string, available []string) string {
requestedVer := parseVersion(requested)

foundIdx := -1
for i, availableVer := range available {
parsedAvailable := parseVersion(availableVer)

if parsedAvailable.greaterOrEqual(requestedVer) {
foundIdx = i
requestedVer = parsedAvailable
}
}

if foundIdx == -1 {
return ""
}

return available[foundIdx]
}

func constructPaths(ncsBase, version, bundleID string) NCSLocation {
return NCSLocation{
Version: version,
Expand Down