-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
See arduino#1250 See arduino#236
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,16 +32,16 @@ | |
import processing.app.forms.PasswordAuthorizationDialog; | ||
import processing.app.helpers.PreferencesMap; | ||
import processing.app.helpers.FileUtils; | ||
import processing.app.helpers.filefilters.OnlyFilesWithExtension; | ||
import processing.app.packages.Library; | ||
import processing.app.packages.LibraryList; | ||
import processing.app.packages.LibraryResolver; | ||
import processing.app.preproc.*; | ||
import processing.core.*; | ||
import static processing.app.I18n._; | ||
|
||
import java.awt.*; | ||
import java.io.*; | ||
import java.util.*; | ||
import java.util.List; | ||
|
||
import javax.swing.*; | ||
|
||
|
@@ -1116,7 +1116,7 @@ public void importLibrary(File jarPath) throws IOException { | |
// make sure the user didn't hide the sketch folder | ||
ensureExistence(); | ||
|
||
String list[] = Base.headerListFromIncludePath(jarPath); | ||
String list[] = jarPath.list(new OnlyFilesWithExtension(".h")); | ||
|
||
// import statements into the main sketch file (code[0]) | ||
// if the current code is a .java file, insert into current | ||
|
@@ -1389,13 +1389,29 @@ public void preprocess(String buildPath, PdePreprocessor preprocessor) throws Ru | |
// grab the imports from the code just preproc'd | ||
|
||
importedLibraries = new LibraryList(); | ||
LibraryResolver resolver = Base.getLibraryResolver(); | ||
for (String item : preprocessor.getExtraImports()) { | ||
Library lib = Base.importToLibraryTable.get(item); | ||
Library lib = resolver.importToLibrary(item); | ||
if (lib != null && !importedLibraries.contains(lib)) { | ||
importedLibraries.add(lib); | ||
} | ||
} | ||
|
||
|
||
// extend the import list with the library dependency tree | ||
while (true) { | ||
LibraryList dependencies = new LibraryList(); | ||
for (Library library : importedLibraries) { | ||
for (Library dependency : library.getResolvedDependencies()) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
cmaglie
Author
Owner
|
||
if (importedLibraries.contains(dependency)) | ||
continue; | ||
dependencies.addOrReplace(dependency); | ||
} | ||
} | ||
if (dependencies.size() == 0) | ||
break; | ||
importedLibraries.addAll(dependencies); | ||
} | ||
|
||
// 3. then loop over the code[] and save each .java file | ||
|
||
for (SketchCode sc : code) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* This file is part of Arduino. | ||
* | ||
* Arduino is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* As a special exception, you may use this file as part of a free software | ||
* library without restriction. Specifically, if other files instantiate | ||
* templates or use macros or inline functions from this file, or you compile | ||
* this file and link it with other files to produce an executable, this | ||
* file does not by itself cause the resulting executable to be covered by | ||
* the GNU General Public License. This exception does not however | ||
* invalidate any other reasons why the executable file might be covered by | ||
* the GNU General Public License. | ||
* | ||
* Copyright 2013 Arduino LLC (http://www.arduino.cc/) | ||
*/ | ||
|
||
package processing.app.packages; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import processing.app.helpers.FileUtils; | ||
import processing.app.helpers.filefilters.OnlyFilesWithExtension; | ||
import processing.app.preproc.PdePreprocessor; | ||
|
||
/** | ||
* This resolver uses an heuristic approach to resolve dependencies between | ||
* libraries without looking into libraries metadata. | ||
* | ||
* All libraries headers are inspected to search for #include lines, afterward | ||
* import dependencies are searched in the same way we do for includes in | ||
* sketches, i.e. looking for a library containing the requested headers. | ||
*/ | ||
public class HeuristicResolver implements LibraryResolver { | ||
|
||
private LibraryList libraries; | ||
private Map<String, Library> importToLibrary; | ||
|
||
public HeuristicResolver(LibraryList _libraries, String arch) { | ||
libraries = _libraries; | ||
importToLibrary = new HashMap<String, Library>(); | ||
|
||
// Populate importToLibrary table | ||
for (Library library : libraries) { | ||
File srcFolder = library.getSrcFolder(); | ||
for (String header : srcFolder.list(new OnlyFilesWithExtension(".h"))) { | ||
importToLibrary.put(header, library); | ||
} | ||
} | ||
|
||
// Resolve all libraries dependencies | ||
for (Library library : libraries) | ||
library.resolvedDependencies = resolve(library, arch); | ||
} | ||
|
||
/** | ||
* Resolve dependencies for a library | ||
* | ||
* @param library | ||
* @param arch | ||
* @return A LibraryList containing the dependencies | ||
*/ | ||
private LibraryList resolve(Library library, String arch) { | ||
This comment has been minimized.
Sorry, something went wrong.
matthijskooijman
|
||
List<File> headers = new ArrayList<File>(); | ||
for (File folder : library.getSrcFolders(arch)) { | ||
List<File> files = FileUtils.listAllFilesWithExtension(folder, ".h", | ||
".c", ".cpp"); | ||
This comment has been minimized.
Sorry, something went wrong.
matthijskooijman
|
||
headers.addAll(files); | ||
} | ||
|
||
LibraryList result = new LibraryList(); | ||
for (File header : headers) | ||
result.addOrReplaceAll(resolveHeader(header, headers, library)); | ||
return result; | ||
} | ||
|
||
/** | ||
* Inspect headerFile and search for dependencies | ||
* | ||
* @param headerFile | ||
* @param exclusionList | ||
* @param library | ||
*/ | ||
private LibraryList resolveHeader(File headerFile, List<File> exclusionList, | ||
Library library) { | ||
LibraryList res = new LibraryList(); | ||
|
||
// Extract #includes from header file | ||
List<String> imports; | ||
try { | ||
PdePreprocessor preprocessor = new PdePreprocessor(); | ||
String header = FileUtils.readFileToString(headerFile); | ||
preprocessor.writePrefix(header); | ||
imports = preprocessor.getExtraImports(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return res; | ||
} | ||
|
||
// For every #include found... | ||
for (String libImport : imports) { | ||
|
||
// ...check if there is a matching library... | ||
Library depLib = importToLibrary.get(libImport); | ||
if (depLib == null || depLib == library) | ||
continue; | ||
|
||
// ...and check if the include is not in the exclusion list | ||
boolean exclude = false; | ||
for (File excluded : exclusionList) { | ||
if (excluded.getName().equals(libImport)) | ||
exclude = true; | ||
} | ||
if (exclude) | ||
continue; | ||
This comment has been minimized.
Sorry, something went wrong.
matthijskooijman
|
||
|
||
// add the dependency | ||
res.addOrReplace(depLib); | ||
|
||
System.out.println("Found dependency for " + library.getName()); | ||
System.out.println(" " + headerFile + " uses " + libImport + " -> " + | ||
depLib.getName()); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
@Override | ||
public Library importToLibrary(String h) { | ||
This comment has been minimized.
Sorry, something went wrong.
matthijskooijman
|
||
return importToLibrary.get(h); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* This file is part of Arduino. | ||
* | ||
* Arduino is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* As a special exception, you may use this file as part of a free software | ||
* library without restriction. Specifically, if other files instantiate | ||
* templates or use macros or inline functions from this file, or you compile | ||
* this file and link it with other files to produce an executable, this | ||
* file does not by itself cause the resulting executable to be covered by | ||
* the GNU General Public License. This exception does not however | ||
* invalidate any other reasons why the executable file might be covered by | ||
* the GNU General Public License. | ||
* | ||
* Copyright 2013 Arduino LLC (http://www.arduino.cc/) | ||
*/ | ||
|
||
package processing.app.packages; | ||
|
||
public interface LibraryResolver { | ||
|
||
/** | ||
* Returns the Library referenced by the include file name | ||
* | ||
* @param header | ||
* The include file name, for example "SPI.h". | ||
* @return The referenced library | ||
*/ | ||
public abstract Library importToLibrary(String header); | ||
|
||
} |
It seems like this scans the dependencies of libraries in importedLibraries over and over again, which shouldn't be needed?
I think you can fix this by making the first forloop loop over "dependencies" instead of "importedLibraries, and initialize "dependencies" to the same contents as "importedLibraries" just above the while loop?