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