@@ -45,7 +45,7 @@ public class BaseNoGui {
45
45
static private File toolsFolder ;
46
46
47
47
// maps #included files to their library folder
48
- public static Map <String , UserLibrary > importToLibraryTable ;
48
+ public static Map <String , LibraryList > importToLibraryTable ;
49
49
50
50
// maps library name to their library folder
51
51
static private LibraryList libraries ;
@@ -771,14 +771,24 @@ static private void createToolPreferences(ContributionsIndexer indexer) {
771
771
}
772
772
773
773
static public void populateImportToLibraryTable () {
774
- // Populate importToLibraryTable
775
- importToLibraryTable = new HashMap <String , UserLibrary >();
774
+ // Populate importToLibraryTable. Each header filename maps to
775
+ // a list of libraries. Compiler.java will use only the first
776
+ // library on each list. The others are used only to advise
777
+ // user of ambiguously matched and duplicate libraries.
778
+ importToLibraryTable = new HashMap <String , LibraryList >();
776
779
for (UserLibrary lib : librariesIndexer .getInstalledLibraries ()) {
777
780
try {
778
781
String headers [] = headerListFromIncludePath (lib .getSrcFolder ());
779
782
for (String header : headers ) {
780
- UserLibrary old = importToLibraryTable .get (header );
781
- if (old != null ) {
783
+ LibraryList list = importToLibraryTable .get (header );
784
+ if (list == null ) {
785
+ // This is the first library found with this header
786
+ list = new LibraryList ();
787
+ list .addFirst (lib );
788
+ importToLibraryTable .put (header , list );
789
+ } else {
790
+ UserLibrary old = list .peekFirst ();
791
+ boolean useThisLib = true ;
782
792
// This is the case where 2 libraries have a .h header
783
793
// with the same name. We must decide which library to
784
794
// use when a sketch has #include "name.h"
@@ -796,58 +806,81 @@ static public void populateImportToLibraryTable() {
796
806
String oldName = old .getInstalledFolder ().getName (); // just the library folder name
797
807
String libName = lib .getInstalledFolder ().getName (); // just the library folder name
798
808
//System.out.println("name conflict: " + name);
799
- //System.out.println(" old = " + oldName + " -> " + old.getFolder ().getPath());
800
- //System.out.println(" new = " + libName + " -> " + lib.getFolder ().getPath());
809
+ //System.out.println(" old = " + oldName + " -> " + old.getInstalledFolder ().getPath());
810
+ //System.out.println(" new = " + libName + " -> " + lib.getInstalledFolder ().getPath());
801
811
String name_lc = name .toLowerCase ();
802
812
String oldName_lc = oldName .toLowerCase ();
803
813
String libName_lc = libName .toLowerCase ();
804
814
// always favor a perfect name match
805
815
if (libName .equals (name )) {
806
816
} else if (oldName .equals (name )) {
807
- continue ;
817
+ useThisLib = false ;
808
818
// check for "-master" appended (zip file from github)
809
819
} else if (libName .equals (name +"-master" )) {
810
820
} else if (oldName .equals (name +"-master" )) {
811
- continue ;
821
+ useThisLib = false ;
812
822
// next, favor a match with other stuff appended
813
823
} else if (libName .startsWith (name )) {
814
824
} else if (oldName .startsWith (name )) {
815
- continue ;
825
+ useThisLib = false ;
816
826
// otherwise, favor a match with stuff prepended
817
827
} else if (libName .endsWith (name )) {
818
828
} else if (oldName .endsWith (name )) {
819
- continue ;
829
+ useThisLib = false ;
820
830
// as a last resort, match if stuff prepended and appended
821
831
} else if (libName .contains (name )) {
822
832
} else if (oldName .contains (name )) {
823
- continue ;
833
+ useThisLib = false ;
824
834
// repeat all the above tests, with case insensitive matching
825
835
} else if (libName_lc .equals (name_lc )) {
826
836
} else if (oldName_lc .equals (name_lc )) {
827
- continue ;
837
+ useThisLib = false ;
828
838
} else if (libName_lc .equals (name_lc +"-master" )) {
829
839
} else if (oldName_lc .equals (name_lc +"-master" )) {
830
- continue ;
840
+ useThisLib = false ;
831
841
} else if (libName_lc .startsWith (name_lc )) {
832
842
} else if (oldName_lc .startsWith (name_lc )) {
833
- continue ;
843
+ useThisLib = false ;
834
844
} else if (libName_lc .endsWith (name_lc )) {
835
845
} else if (oldName_lc .endsWith (name_lc )) {
836
- continue ;
846
+ useThisLib = false ;
837
847
} else if (libName_lc .contains (name_lc )) {
838
848
} else if (oldName_lc .contains (name_lc )) {
839
- continue ;
849
+ useThisLib = false ;
840
850
} else {
841
851
// none of these tests matched, so just default to "libName".
842
852
}
853
+ if (useThisLib ) {
854
+ list .addFirst (lib );
855
+ } else {
856
+ list .addLast (lib );
857
+ }
843
858
}
844
- importToLibraryTable .put (header , lib );
845
859
}
846
860
} catch (IOException e ) {
847
861
showWarning (_ ("Error" ), I18n
848
862
.format ("Unable to list header files in {0}" , lib .getSrcFolder ()), e );
849
863
}
850
864
}
865
+ // repeat for ALL libraries, to pick up duplicates not visible normally.
866
+ // any new libraries found here are NEVER used, but they are added to the
867
+ // end of already-found headers, to allow Compiler to report them if
868
+ // the sketch tries to use them.
869
+ for (UserLibrary lib : librariesIndexer .getInstalledLibrariesWithDuplicates ()) {
870
+ try {
871
+ String headers [] = headerListFromIncludePath (lib .getSrcFolder ());
872
+ for (String header : headers ) {
873
+ LibraryList list = importToLibraryTable .get (header );
874
+ if (list != null ) {
875
+ if (!(list .hasLibrary (lib ))) {
876
+ list .addLast (lib );
877
+ //System.out.println(" duplicate lib: " + lib.getInstalledFolder().getPath());
878
+ }
879
+ }
880
+ }
881
+ } catch (IOException e ) {
882
+ }
883
+ }
851
884
}
852
885
853
886
static public void initParameters (String args []) {
0 commit comments