Skip to content

Commit 371935f

Browse files
Advise of duplicate libraries after compiling
backport from arduino/Arduino#2850
1 parent 1340c35 commit 371935f

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

app/src/processing/app/Base.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public class Base {
8888
static HashSet<File> libraries;
8989

9090
// maps imported packages to their library folder
91-
static HashMap<String, File> importToLibraryTable;
91+
static HashMap<String, LinkedList<File>> importToLibraryTable;
9292

9393
// classpath for all known libraries for p5
9494
// (both those in the p5/libs folder and those with lib subfolders
@@ -1117,7 +1117,7 @@ public void actionPerformed(ActionEvent e) {
11171117
libraries = new HashSet<File>();
11181118

11191119
// reset the table mapping imports to libraries
1120-
importToLibraryTable = new HashMap<String, File>();
1120+
importToLibraryTable = new HashMap<String, LinkedList<File>>();
11211121

11221122
// Add from the "libraries" subfolder in the Processing directory
11231123
try {
@@ -1377,12 +1377,16 @@ public void actionPerformed(ActionEvent event) {
13771377
String headers[] = Compiler.headerListFromIncludePath(libFolderPath);
13781378
for (String header : headers) {
13791379

1380-
File old = importToLibraryTable.get(header);
1381-
if (old == null) {
1380+
LinkedList<File> liblist = importToLibraryTable.get(header);
1381+
if (liblist == null) {
13821382
// found a header file not on the list
13831383
//System.out.println("library " + libFolder + " for header " + header);
1384-
importToLibraryTable.put(header, libFolder);
1384+
liblist = new LinkedList<File>();
1385+
liblist.addFirst(libFolder);
1386+
importToLibraryTable.put(header, liblist);
13851387
} else {
1388+
File old = liblist.peekFirst();
1389+
boolean useThisLib = false;
13861390
// found a header file already on the list,
13871391
// so we must decide whether to use the library already
13881392
// found, or use this library when this header is #include
@@ -1393,15 +1397,20 @@ public void actionPerformed(ActionEvent event) {
13931397
if (name.equalsIgnoreCase(libFolder.getName()) || name.startsWith(libFolder.getName())) {
13941398
// header name clearly matches this new library, so use it
13951399
//System.out.println(" use: " + libFolder.getPath());
1396-
importToLibraryTable.put(header, libFolder);
1400+
useThisLib = true;
13971401
} else if (name.equalsIgnoreCase(old.getName()) || name.startsWith(old.getName())) {
13981402
// header name clearly matches the previously found library, so keep it
13991403
//System.out.println(" use: " + old.getPath());
14001404
} else {
14011405
// neither is a good match, so go with the last one found
14021406
//System.out.println(" use: " + libFolder.getPath());
1403-
importToLibraryTable.put(header, libFolder);
1407+
useThisLib = true;
14041408
}
1409+
if (useThisLib) {
1410+
liblist.addFirst(libFolder);
1411+
} else {
1412+
liblist.addLast(libFolder);
1413+
}
14051414
}
14061415
}
14071416
} catch (IOException e) {

app/src/processing/app/Sketch.java

+41-9
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public class Sketch {
9696
* List of library folders.
9797
*/
9898
private ArrayList<File> importedLibraries;
99+
private ArrayList<String> importedDuplicateHeaders;
100+
private ArrayList<LinkedList<File>> importedDuplicateLibraries;
99101

100102
/**
101103
* path is location of the main .pde file, because this is also
@@ -1436,14 +1438,20 @@ public String preprocess(String buildPath, PdePreprocessor preprocessor) throws
14361438
// grab the imports from the code just preproc'd
14371439

14381440
importedLibraries = new ArrayList<File>();
1441+
importedDuplicateHeaders = new ArrayList<String>();
1442+
importedDuplicateLibraries = new ArrayList<LinkedList<File>>();
14391443

14401444
for (String item : preprocessor.getExtraImports()) {
1441-
File libFolder = (File) Base.importToLibraryTable.get(item);
1442-
1443-
if (libFolder != null && !importedLibraries.contains(libFolder)) {
1444-
importedLibraries.add(libFolder);
1445-
//classPath += Compiler.contentsToClassPath(libFolder);
1446-
libraryPath += File.pathSeparator + libFolder.getAbsolutePath();
1445+
LinkedList<File> liblist = Base.importToLibraryTable.get(item);
1446+
if (liblist != null) {
1447+
File libFolder = liblist.peekFirst();
1448+
if (libFolder != null && !importedLibraries.contains(libFolder)) {
1449+
importedLibraries.add(libFolder);
1450+
if (liblist.size() > 1) {
1451+
importedDuplicateHeaders.add(item);
1452+
importedDuplicateLibraries.add(liblist);
1453+
}
1454+
}
14471455
}
14481456
}
14491457

@@ -1582,14 +1590,38 @@ public String build(String buildPath, boolean verbose)
15821590
// compile the program. errors will happen as a RunnerException
15831591
// that will bubble up to whomever called build().
15841592
Compiler compiler = new Compiler();
1585-
if (compiler.compile(this, buildPath, primaryClassName, verbose)) {
1586-
size(buildPath, primaryClassName);
1587-
return primaryClassName;
1593+
try {
1594+
if (compiler.compile(this, buildPath, primaryClassName, verbose)) {
1595+
size(buildPath, primaryClassName);
1596+
return primaryClassName;
1597+
}
1598+
} catch (RunnerException e) {
1599+
// when the compile fails, take this opportunity to show
1600+
// any helpful info possible before throwing the exception
1601+
adviseDuplicateLibraries();
1602+
throw e;
15881603
}
15891604
return null;
15901605
}
15911606

15921607

1608+
public void adviseDuplicateLibraries() {
1609+
for (int i=0; i < importedDuplicateHeaders.size(); i++) {
1610+
System.out.println(I18n.format(_("Multiple libraries were found for \"{0}\""),
1611+
importedDuplicateHeaders.get(i)));
1612+
boolean first = true;
1613+
for (File libFolder : importedDuplicateLibraries.get(i)) {
1614+
if (first) {
1615+
System.out.println(I18n.format(_(" Used: {0}"), libFolder.getPath()));
1616+
first = false;
1617+
} else {
1618+
System.out.println(I18n.format(_(" Not Used: {0}"), libFolder.getPath()));
1619+
}
1620+
}
1621+
}
1622+
}
1623+
1624+
15931625
protected boolean exportApplet(boolean usingProgrammer) throws Exception {
15941626
return exportApplet(tempBuildFolder.getAbsolutePath(), usingProgrammer);
15951627
}

app/src/processing/app/debug/Compiler.java

+2
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,11 @@ public boolean compile(Sketch sketch,
343343
commandScript.add("-file=" + primaryClassName);
344344
execTeensySimple(commandScript);
345345
}
346+
sketch.adviseDuplicateLibraries();
346347
return true;
347348
}
348349

350+
349351
private List<File> recursiveCompile(String avrBasePath, File srcFolder,
350352
File outputFolder, List<File> includePaths,
351353
Map<String, String> boardPreferences) throws RunnerException {

0 commit comments

Comments
 (0)