Skip to content

Commit 22a1e71

Browse files
committed
Use libraryresolver to resolve libraries
1 parent 4722d66 commit 22a1e71

10 files changed

+68
-317
lines changed

libraries_loader.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"os"
3434

3535
"github.com/bcmi-labs/arduino-cli/arduino/libraries/librariesmanager"
36+
"github.com/bcmi-labs/arduino-cli/arduino/libraries/librariesresolver"
3637

3738
"github.com/arduino/arduino-builder/i18n"
3839
"github.com/arduino/arduino-builder/types"
@@ -89,22 +90,11 @@ func (s *LibrariesLoader) Run(ctx *types.Context) error {
8990
}
9091
}
9192

92-
headerToLibraries := make(map[string][]*libraries.Library)
93-
for _, lib := range lm.Libraries {
94-
for _, library := range lib.Alternatives {
95-
headers, err := library.SrcFolder.ReadDir()
96-
if err != nil {
97-
return i18n.WrapError(err)
98-
}
99-
headers.FilterSuffix(".h", ".hpp", ".hh")
100-
for _, header := range headers {
101-
headerFileName := header.Base()
102-
headerToLibraries[headerFileName] = append(headerToLibraries[headerFileName], library)
103-
}
104-
}
93+
resolver := librariesresolver.NewCppResolver()
94+
if err := resolver.ScanFromLibrariesManager(lm); err != nil {
95+
return i18n.WrapError(err)
10596
}
106-
107-
ctx.HeaderToLibraries = headerToLibraries
97+
ctx.LibrariesResolver = resolver
10898

10999
return nil
110100
}

phases/libraries_builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (s *LibrariesBuilder) Run(ctx *types.Context) error {
7171
return nil
7272
}
7373

74-
func fixLDFLAGforPrecompiledLibraries(ctx *types.Context, libs []*libraries.Library) error {
74+
func fixLDFLAGforPrecompiledLibraries(ctx *types.Context, libs libraries.List) error {
7575

7676
for _, library := range libs {
7777
if library.Precompiled {
@@ -96,7 +96,7 @@ func fixLDFLAGforPrecompiledLibraries(ctx *types.Context, libs []*libraries.Libr
9696
return nil
9797
}
9898

99-
func compileLibraries(ctx *types.Context, libraries []*libraries.Library, buildPath *paths.Path, buildProperties properties.Map, includes []string) (paths.PathList, error) {
99+
func compileLibraries(ctx *types.Context, libraries libraries.List, buildPath *paths.Path, buildProperties properties.Map, includes []string) (paths.PathList, error) {
100100
objectFiles := paths.NewPathList()
101101
for _, library := range libraries {
102102
libraryObjectFiles, err := compileLibrary(ctx, library, buildPath, buildProperties, includes)

resolve_library.go

Lines changed: 17 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -30,98 +30,44 @@
3030
package builder
3131

3232
import (
33-
"path/filepath"
34-
"strings"
33+
"fmt"
3534

36-
"github.com/arduino/arduino-builder/constants"
3735
"github.com/arduino/arduino-builder/types"
38-
"github.com/arduino/arduino-builder/utils"
39-
"github.com/bcmi-labs/arduino-cli/arduino/cores"
4036
"github.com/bcmi-labs/arduino-cli/arduino/libraries"
4137
)
4238

4339
func ResolveLibrary(ctx *types.Context, header string) *libraries.Library {
44-
headerToLibraries := ctx.HeaderToLibraries
45-
platforms := []*cores.PlatformRelease{ctx.ActualPlatform, ctx.TargetPlatform}
46-
libraryResolutionResults := ctx.LibrariesResolutionResults
40+
resolver := ctx.LibrariesResolver
4741
importedLibraries := ctx.ImportedLibraries
4842

49-
libs := append([]*libraries.Library{}, headerToLibraries[header]...)
43+
candidates := resolver.AlternativesFor(header)
44+
fmt.Printf("ResolveLibrary(%s)\n", header)
45+
fmt.Printf(" -> candidates: %s\n", candidates)
5046

51-
if libs == nil || len(libs) == 0 {
47+
if candidates == nil || len(candidates) == 0 {
5248
return nil
5349
}
5450

55-
if importedLibraryContainsOneOfCandidates(importedLibraries, libs) {
56-
return nil
57-
}
58-
59-
if len(libs) == 1 {
60-
return libs[0]
61-
}
62-
63-
reverse(libs)
64-
65-
var library *libraries.Library
66-
67-
for _, platform := range platforms {
68-
if platform != nil {
69-
library = findBestLibraryWithHeader(header, librariesCompatibleWithPlatform(libs, platform, true))
51+
for _, candidate := range candidates {
52+
if importedLibraries.Contains(candidate) {
53+
return nil
7054
}
7155
}
7256

73-
if library == nil {
74-
library = findBestLibraryWithHeader(header, libs)
75-
}
76-
77-
if library == nil {
78-
// reorder libraries to promote fully compatible ones
79-
for _, platform := range platforms {
80-
if platform != nil {
81-
libs = append(librariesCompatibleWithPlatform(libs, platform, false), libs...)
82-
}
83-
}
84-
library = libs[0]
57+
selected := resolver.ResolveFor(header, ctx.TargetPlatform.Platform.Architecture)
58+
if alreadyImported := importedLibraries.FindByName(selected.Name); alreadyImported != nil {
59+
selected = alreadyImported
8560
}
8661

87-
library = useAlreadyImportedLibraryWithSameNameIfExists(library, importedLibraries)
88-
89-
libraryResolutionResults[header] = types.LibraryResolutionResult{
90-
Library: library,
91-
NotUsedLibraries: filterOutLibraryFrom(libs, library),
62+
ctx.LibrariesResolutionResults[header] = types.LibraryResolutionResult{
63+
Library: selected,
64+
NotUsedLibraries: filterOutLibraryFrom(candidates, selected),
9265
}
9366

94-
return library
95-
}
96-
97-
//facepalm. sort.Reverse needs an Interface that implements Len/Less/Swap. It's a slice! What else for reversing it?!?
98-
func reverse(data []*libraries.Library) {
99-
for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {
100-
data[i], data[j] = data[j], data[i]
101-
}
67+
return selected
10268
}
10369

104-
func importedLibraryContainsOneOfCandidates(imported []*libraries.Library, candidates []*libraries.Library) bool {
105-
for _, i := range imported {
106-
for _, j := range candidates {
107-
if i == j {
108-
return true
109-
}
110-
}
111-
}
112-
return false
113-
}
114-
115-
func useAlreadyImportedLibraryWithSameNameIfExists(library *libraries.Library, imported []*libraries.Library) *libraries.Library {
116-
for _, lib := range imported {
117-
if lib.Name == library.Name {
118-
return lib
119-
}
120-
}
121-
return library
122-
}
123-
124-
func filterOutLibraryFrom(libs []*libraries.Library, libraryToRemove *libraries.Library) []*libraries.Library {
70+
func filterOutLibraryFrom(libs libraries.List, libraryToRemove *libraries.Library) libraries.List {
12571
filteredOutLibraries := []*libraries.Library{}
12672
for _, lib := range libs {
12773
if lib != libraryToRemove {
@@ -130,107 +76,3 @@ func filterOutLibraryFrom(libs []*libraries.Library, libraryToRemove *libraries.
13076
}
13177
return filteredOutLibraries
13278
}
133-
134-
func libraryCompatibleWithPlatform(library *libraries.Library, platform *cores.PlatformRelease) (bool, bool) {
135-
if len(library.Architectures) == 0 {
136-
return true, true
137-
}
138-
if utils.SliceContains(library.Architectures, constants.LIBRARY_ALL_ARCHS) {
139-
return true, true
140-
}
141-
return utils.SliceContains(library.Architectures, platform.Platform.Architecture), false
142-
}
143-
144-
func libraryCompatibleWithAllPlatforms(library *libraries.Library) bool {
145-
if utils.SliceContains(library.Architectures, constants.LIBRARY_ALL_ARCHS) {
146-
return true
147-
}
148-
return false
149-
}
150-
151-
func librariesCompatibleWithPlatform(libs []*libraries.Library, platform *cores.PlatformRelease, reorder bool) []*libraries.Library {
152-
var compatibleLibraries []*libraries.Library
153-
for _, library := range libs {
154-
compatible, generic := libraryCompatibleWithPlatform(library, platform)
155-
if compatible {
156-
if !generic && len(compatibleLibraries) != 0 && libraryCompatibleWithAllPlatforms(compatibleLibraries[0]) && reorder == true {
157-
//priority inversion
158-
compatibleLibraries = append([]*libraries.Library{library}, compatibleLibraries...)
159-
} else {
160-
compatibleLibraries = append(compatibleLibraries, library)
161-
}
162-
}
163-
}
164-
165-
return compatibleLibraries
166-
}
167-
168-
func findBestLibraryWithHeader(header string, libs []*libraries.Library) *libraries.Library {
169-
headerName := strings.Replace(header, filepath.Ext(header), constants.EMPTY_STRING, -1)
170-
171-
var library *libraries.Library
172-
for _, headerName := range []string{headerName, strings.ToLower(headerName)} {
173-
library = findLibWithName(headerName, libs)
174-
if library != nil {
175-
return library
176-
}
177-
library = findLibWithName(headerName+"-master", libs)
178-
if library != nil {
179-
return library
180-
}
181-
library = findLibWithNameStartingWith(headerName, libs)
182-
if library != nil {
183-
return library
184-
}
185-
library = findLibWithNameEndingWith(headerName, libs)
186-
if library != nil {
187-
return library
188-
}
189-
library = findLibWithNameContaining(headerName, libs)
190-
if library != nil {
191-
return library
192-
}
193-
}
194-
195-
return nil
196-
}
197-
198-
func findLibWithName(name string, libraries []*libraries.Library) *libraries.Library {
199-
for _, library := range libraries {
200-
if simplifyName(library.Name) == simplifyName(name) {
201-
return library
202-
}
203-
}
204-
return nil
205-
}
206-
207-
func findLibWithNameStartingWith(name string, libraries []*libraries.Library) *libraries.Library {
208-
for _, library := range libraries {
209-
if strings.HasPrefix(simplifyName(library.Name), simplifyName(name)) {
210-
return library
211-
}
212-
}
213-
return nil
214-
}
215-
216-
func findLibWithNameEndingWith(name string, libraries []*libraries.Library) *libraries.Library {
217-
for _, library := range libraries {
218-
if strings.HasSuffix(simplifyName(library.Name), simplifyName(name)) {
219-
return library
220-
}
221-
}
222-
return nil
223-
}
224-
225-
func findLibWithNameContaining(name string, libraries []*libraries.Library) *libraries.Library {
226-
for _, library := range libraries {
227-
if strings.Contains(simplifyName(library.Name), simplifyName(name)) {
228-
return library
229-
}
230-
}
231-
return nil
232-
}
233-
234-
func simplifyName(name string) string {
235-
return strings.ToLower(strings.Replace(name, "_", " ", -1))
236-
}

resolve_library_test.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

test/helper_tools_downloader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func DownloadCoresAndToolsAndLibraries(t *testing.T) {
142142
Library{Name: "Bridge", Version: "1.6.1"},
143143
Library{Name: "CapacitiveSensor", Version: "0.5.0", VersionInLibProperties: "0.5"},
144144
Library{Name: "Ethernet", Version: "1.1.1"},
145-
Library{Name: "Robot IR Remote", Version: "1.0.2"},
145+
Library{Name: "Robot IR Remote", Version: "2.0.0"},
146146
Library{Name: "FastLED", Version: "3.1.0"},
147147
}
148148

test/includes_to_include_folders_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,8 @@ func TestIncludesToIncludeFoldersANewLibrary(t *testing.T) {
168168
defer buildPath.RemoveAll()
169169

170170
commands := []types.Command{
171-
172171
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
173-
174172
&builder.ContainerMergeCopySketchFiles{},
175-
176173
&builder.ContainerFindIncludes{},
177174
}
178175

0 commit comments

Comments
 (0)