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