Skip to content

Commit

Permalink
feat: add a simple toolchain mapper
Browse files Browse the repository at this point in the history
Change-Id: I4dc426204148634590eebc12b10a42df02ee058c
  • Loading branch information
lukokr-aarch64 committed Oct 10, 2023
1 parent 27be612 commit 64d32f2
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Blueprints
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ bootstrap_go_package {
}


bootstrap_go_package {
name: "bob-toolchain-mapper",
srcs: [
"core/toolchain/mapper/mapper.go",
],
testSrcs: [
"core/toolchain/mapper/mapper_test.go",
],
pkgPath: "github.com/ARM-software/bob-build/core/toolchain/mapper",
}


bootstrap_go_package {
name: "bob-backend",
deps: [
Expand Down
15 changes: 15 additions & 0 deletions core/toolchain/mapper/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "mapper",
srcs = ["mapper.go"],
importpath = "github.com/ARM-software/bob-build/core/toolchain/mapper",
visibility = ["//visibility:public"],
)

go_test(
name = "mapper_test",
srcs = ["mapper_test.go"],
embed = [":mapper"],
deps = ["@com_github_stretchr_testify//assert"],
)
53 changes: 53 additions & 0 deletions core/toolchain/mapper/mapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package mapper

import (
"path/filepath"
"sync"
)

// Map of toolchain targets by module path
type Mapper struct {
modules map[string][]string
lock sync.Mutex
}

func New() *Mapper {
return &Mapper{
lock: sync.Mutex{},
modules: map[string][]string{},
}
}

func (t *Mapper) Add(path string, name string) {
t.lock.Lock()
defer t.lock.Unlock()

if current, ok := t.modules[path]; ok {
t.modules[path] = append(current, name)
} else {
t.modules[path] = []string{name}
}
}

// Returns the toolchain module name based on the path.
// The behavior is as follows:
// - For given path, if a toolchain config exists return the first registered toolchain.
// - If no config exists for current path, walk the directories upwards looking for default.
func (t *Mapper) Get(path string) string {
t.lock.Lock()
defer t.lock.Unlock()

for p := path; p != "."; {
if names, ok := t.modules[p]; ok {
return names[0]
}
p = filepath.Dir(p)
}

// Check for root
if names, ok := t.modules["."]; ok {
return names[0]
}

return ""
}
40 changes: 40 additions & 0 deletions core/toolchain/mapper/mapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package mapper

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestEmptyAdd(t *testing.T) {
tc := New()
tc.Add("foo/bar/baz", "tc1")
tc.Add("foo/bar", "tc2")
}

func Test_UnregisteredDefault(t *testing.T) {
tc := New()
assert.Equal(t, "", tc.Get("."))
}

func TestCommon(t *testing.T) {
tc := New()
tc.Add("foo/bar/baz", "baz")
tc.Add("foo/bar", "bar1")
tc.Add("foo/bar", "bar2")
tc.Add(".", "root")

t.Run("DirectHit", func(t *testing.T) {
assert.Equal(t, "root", tc.Get("."))
})

t.Run("RootDefault", func(t *testing.T) {
assert.Equal(t, "root", tc.Get("does/not/exist"))
})

t.Run("OrderOfRegister", func(t *testing.T) {
assert.Equal(t, "bar1", tc.Get("foo/bar"))
assert.Equal(t, "bar1", tc.Get("foo/bar/child"))
})

}

0 comments on commit 64d32f2

Please sign in to comment.