Skip to content

Commit 8cd7297

Browse files
authored
[skip-changelog] legacy: some simple refactorings (#2206)
* Factored a method in library.List * Fixed lint check * Moved ResolveLibrary function in the correct source file * Rename variable 'includes' -> 'includeFolders' * Rename variables to use snakeCase (golang idiomatic) * Rename variable 'include' -> 'missingIncludeH' * Use pointers for SourceFile queues * Clean up implementation of UniqueSourceFileQueue
1 parent 8310d01 commit 8cd7297

File tree

5 files changed

+93
-123
lines changed

5 files changed

+93
-123
lines changed

Diff for: arduino/libraries/librarieslist.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ func (list *List) Add(libs ...*Library) {
4141
}
4242
}
4343

44-
// Remove removes the library from the list
45-
func (list *List) Remove(library *Library) {
44+
// Remove removes the given library from the list
45+
func (list *List) Remove(libraryToRemove *Library) {
4646
for i, lib := range *list {
47-
if lib == library {
47+
if lib == libraryToRemove {
4848
*list = append((*list)[:i], (*list)[i+1:]...)
4949
return
5050
}

Diff for: legacy/builder/container_find_includes.go

+72-27
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,12 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFileQu
342342

343343
first := true
344344
for {
345-
var include string
345+
var missingIncludeH string
346346
cache.ExpectFile(sourcePath)
347347

348-
includes := ctx.IncludeFolders
348+
includeFolders := ctx.IncludeFolders
349349
if library, ok := sourceFile.Origin.(*libraries.Library); ok && library.UtilityDir != nil {
350-
includes = append(includes, library.UtilityDir)
350+
includeFolders = append(includeFolders, library.UtilityDir)
351351
}
352352

353353
if library, ok := sourceFile.Origin.(*libraries.Library); ok {
@@ -361,72 +361,72 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFileQu
361361
}
362362
}
363363

364-
var preproc_err error
365-
var preproc_stderr []byte
364+
var preprocErr error
365+
var preprocStderr []byte
366366

367367
if unchanged && cache.valid {
368-
include = cache.Next().Include
368+
missingIncludeH = cache.Next().Include
369369
if first && ctx.Verbose {
370370
ctx.Info(tr("Using cached library dependencies for file: %[1]s", sourcePath))
371371
}
372372
} else {
373-
var preproc_stdout []byte
374-
preproc_stdout, preproc_stderr, preproc_err = preprocessor.GCC(sourcePath, targetFilePath, includes, ctx.BuildProperties)
373+
var preprocStdout []byte
374+
preprocStdout, preprocStderr, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, ctx.BuildProperties)
375375
if ctx.Verbose {
376-
ctx.WriteStdout(preproc_stdout)
377-
ctx.WriteStdout(preproc_stderr)
376+
ctx.WriteStdout(preprocStdout)
377+
ctx.WriteStdout(preprocStderr)
378378
}
379379
// Unwrap error and see if it is an ExitError.
380-
_, is_exit_error := errors.Cause(preproc_err).(*exec.ExitError)
381-
if preproc_err == nil {
380+
_, isExitErr := errors.Cause(preprocErr).(*exec.ExitError)
381+
if preprocErr == nil {
382382
// Preprocessor successful, done
383-
include = ""
384-
} else if !is_exit_error || preproc_stderr == nil {
383+
missingIncludeH = ""
384+
} else if !isExitErr || preprocStderr == nil {
385385
// Ignore ExitErrors (e.g. gcc returning
386386
// non-zero status), but bail out on
387387
// other errors
388-
return errors.WithStack(preproc_err)
388+
return errors.WithStack(preprocErr)
389389
} else {
390-
include = IncludesFinderWithRegExp(string(preproc_stderr))
391-
if include == "" && ctx.Verbose {
390+
missingIncludeH = IncludesFinderWithRegExp(string(preprocStderr))
391+
if missingIncludeH == "" && ctx.Verbose {
392392
ctx.Info(tr("Error while detecting libraries included by %[1]s", sourcePath))
393393
}
394394
}
395395
}
396396

397-
if include == "" {
397+
if missingIncludeH == "" {
398398
// No missing includes found, we're done
399399
cache.ExpectEntry(sourcePath, "", nil)
400400
return nil
401401
}
402402

403-
library := ResolveLibrary(ctx, include)
403+
library := ResolveLibrary(ctx, missingIncludeH)
404404
if library == nil {
405405
// Library could not be resolved, show error
406-
if preproc_err == nil || preproc_stderr == nil {
406+
if preprocErr == nil || preprocStderr == nil {
407407
// Filename came from cache, so run preprocessor to obtain error to show
408-
var preproc_stdout []byte
409-
preproc_stdout, preproc_stderr, preproc_err = preprocessor.GCC(sourcePath, targetFilePath, includes, ctx.BuildProperties)
408+
var preprocStdout []byte
409+
preprocStdout, preprocStderr, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, ctx.BuildProperties)
410410
if ctx.Verbose {
411-
ctx.WriteStdout(preproc_stdout)
411+
ctx.WriteStdout(preprocStdout)
412412
}
413-
if preproc_err == nil {
413+
if preprocErr == nil {
414414
// If there is a missing #include in the cache, but running
415415
// gcc does not reproduce that, there is something wrong.
416416
// Returning an error here will cause the cache to be
417417
// deleted, so hopefully the next compilation will succeed.
418418
return errors.New(tr("Internal error in cache"))
419419
}
420420
}
421-
ctx.WriteStderr(preproc_stderr)
422-
return errors.WithStack(preproc_err)
421+
ctx.WriteStderr(preprocStderr)
422+
return errors.WithStack(preprocErr)
423423
}
424424

425425
// Add this library to the list of libraries, the
426426
// include path and queue its source files for further
427427
// include scanning
428428
ctx.ImportedLibraries = append(ctx.ImportedLibraries, library)
429-
appendIncludeFolder(ctx, cache, sourcePath, include, library.SourceDir)
429+
appendIncludeFolder(ctx, cache, sourcePath, missingIncludeH, library.SourceDir)
430430
sourceDirs := library.SourceDirs()
431431
for _, sourceDir := range sourceDirs {
432432
queueSourceFilesFromFolder(ctx, sourceFileQueue, library, sourceDir.Dir, sourceDir.Recurse)
@@ -455,3 +455,48 @@ func queueSourceFilesFromFolder(ctx *types.Context, sourceFileQueue *types.Uniqu
455455

456456
return nil
457457
}
458+
459+
func ResolveLibrary(ctx *types.Context, header string) *libraries.Library {
460+
resolver := ctx.LibrariesResolver
461+
importedLibraries := ctx.ImportedLibraries
462+
463+
candidates := resolver.AlternativesFor(header)
464+
465+
if ctx.Verbose {
466+
ctx.Info(tr("Alternatives for %[1]s: %[2]s", header, candidates))
467+
ctx.Info(fmt.Sprintf("ResolveLibrary(%s)", header))
468+
ctx.Info(fmt.Sprintf(" -> %s: %s", tr("candidates"), candidates))
469+
}
470+
471+
if len(candidates) == 0 {
472+
return nil
473+
}
474+
475+
for _, candidate := range candidates {
476+
if importedLibraries.Contains(candidate) {
477+
return nil
478+
}
479+
}
480+
481+
selected := resolver.ResolveFor(header, ctx.TargetPlatform.Platform.Architecture)
482+
if alreadyImported := importedLibraries.FindByName(selected.Name); alreadyImported != nil {
483+
// Certain libraries might have the same name but be different.
484+
// This usually happens when the user includes two or more custom libraries that have
485+
// different header name but are stored in a parent folder with identical name, like
486+
// ./libraries1/Lib/lib1.h and ./libraries2/Lib/lib2.h
487+
// Without this check the library resolution would be stuck in a loop.
488+
// This behaviour has been reported in this issue:
489+
// https://github.com/arduino/arduino-cli/issues/973
490+
if selected == alreadyImported {
491+
selected = alreadyImported
492+
}
493+
}
494+
495+
candidates.Remove(selected)
496+
ctx.LibrariesResolutionResults[header] = types.LibraryResolutionResult{
497+
Library: selected,
498+
NotUsedLibraries: candidates,
499+
}
500+
501+
return selected
502+
}

Diff for: legacy/builder/resolve_library.go

-77
This file was deleted.

Diff for: legacy/builder/types/accessories.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,25 @@ package types
1717

1818
import "golang.org/x/exp/slices"
1919

20-
type UniqueSourceFileQueue []SourceFile
20+
type UniqueSourceFileQueue []*SourceFile
2121

22-
func (queue UniqueSourceFileQueue) Len() int { return len(queue) }
23-
func (queue UniqueSourceFileQueue) Less(i, j int) bool { return false }
24-
func (queue UniqueSourceFileQueue) Swap(i, j int) { panic("Who called me?!?") }
25-
26-
func (queue *UniqueSourceFileQueue) Push(value SourceFile) {
27-
equals := func(elem SourceFile) bool {
28-
return elem.Origin == value.Origin && elem.RelativePath.EqualsTo(value.RelativePath)
29-
}
30-
if !slices.ContainsFunc(*queue, equals) {
22+
func (queue *UniqueSourceFileQueue) Push(value *SourceFile) {
23+
if !queue.Contains(value) {
3124
*queue = append(*queue, value)
3225
}
3326
}
3427

35-
func (queue *UniqueSourceFileQueue) Pop() SourceFile {
28+
func (queue UniqueSourceFileQueue) Contains(target *SourceFile) bool {
29+
return slices.ContainsFunc(queue, target.Equals)
30+
}
31+
32+
func (queue *UniqueSourceFileQueue) Pop() *SourceFile {
3633
old := *queue
3734
x := old[0]
3835
*queue = old[1:]
3936
return x
4037
}
4138

42-
func (queue *UniqueSourceFileQueue) Empty() bool {
43-
return queue.Len() == 0
39+
func (queue UniqueSourceFileQueue) Empty() bool {
40+
return len(queue) == 0
4441
}

Diff for: legacy/builder/types/types.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,23 @@ type SourceFile struct {
3030
RelativePath *paths.Path
3131
}
3232

33+
func (f *SourceFile) Equals(g *SourceFile) bool {
34+
return f.Origin == g.Origin &&
35+
f.RelativePath.EqualsTo(g.RelativePath)
36+
}
37+
3338
// Create a SourceFile containing the given source file path within the
3439
// given origin. The given path can be absolute, or relative within the
3540
// origin's root source folder
36-
func MakeSourceFile(ctx *Context, origin interface{}, path *paths.Path) (SourceFile, error) {
41+
func MakeSourceFile(ctx *Context, origin interface{}, path *paths.Path) (*SourceFile, error) {
3742
if path.IsAbs() {
3843
var err error
3944
path, err = sourceRoot(ctx, origin).RelTo(path)
4045
if err != nil {
41-
return SourceFile{}, err
46+
return nil, err
4247
}
4348
}
44-
return SourceFile{Origin: origin, RelativePath: path}, nil
49+
return &SourceFile{Origin: origin, RelativePath: path}, nil
4550
}
4651

4752
// Return the build root for the given origin, where build products will

0 commit comments

Comments
 (0)