@@ -455,3 +455,48 @@ func queueSourceFilesFromFolder(ctx *types.Context, sourceFileQueue *types.Uniqu
455
455
456
456
return nil
457
457
}
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
+ }
0 commit comments