|
| 1 | +/* |
| 2 | + * This file is part of Arduino. |
| 3 | + * |
| 4 | + * Arduino is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + * As a special exception, you may use this file as part of a free software |
| 19 | + * library without restriction. Specifically, if other files instantiate |
| 20 | + * templates or use macros or inline functions from this file, or you compile |
| 21 | + * this file and link it with other files to produce an executable, this |
| 22 | + * file does not by itself cause the resulting executable to be covered by |
| 23 | + * the GNU General Public License. This exception does not however |
| 24 | + * invalidate any other reasons why the executable file might be covered by |
| 25 | + * the GNU General Public License. |
| 26 | + * |
| 27 | + * Copyright 2013 Arduino LLC (http://www.arduino.cc/) |
| 28 | + */ |
| 29 | + |
| 30 | +package processing.app.packages; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +import processing.app.helpers.FileUtils; |
| 40 | +import processing.app.helpers.filefilters.OnlyFilesWithExtension; |
| 41 | +import processing.app.preproc.PdePreprocessor; |
| 42 | + |
| 43 | +/** |
| 44 | + * This resolver uses an heuristic approach to resolve dependencies between |
| 45 | + * libraries without looking into libraries metadata. |
| 46 | + * |
| 47 | + * All libraries headers are inspected to search for #include lines, afterward |
| 48 | + * import dependencies are searched in the same way we do for includes in |
| 49 | + * sketches, i.e. looking for a library containing the requested headers. |
| 50 | + */ |
| 51 | +public class HeuristicResolver implements LibraryResolver { |
| 52 | + |
| 53 | + private LibraryList libraries; |
| 54 | + private Map<String, Library> importToLibrary; |
| 55 | + |
| 56 | + public HeuristicResolver(LibraryList _libraries, String arch) { |
| 57 | + libraries = _libraries; |
| 58 | + importToLibrary = new HashMap<String, Library>(); |
| 59 | + |
| 60 | + // Populate importToLibrary table |
| 61 | + for (Library library : libraries) { |
| 62 | + File srcFolder = library.getSrcFolder(); |
| 63 | + for (String header : srcFolder.list(new OnlyFilesWithExtension(".h"))) { |
| 64 | + importToLibrary.put(header, library); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Resolve all libraries dependencies |
| 69 | + for (Library library : libraries) |
| 70 | + library.resolvedDependencies = resolve(library, arch); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Resolve dependencies for a library |
| 75 | + * |
| 76 | + * @param library |
| 77 | + * @param arch |
| 78 | + * @return A LibraryList containing the dependencies |
| 79 | + */ |
| 80 | + private LibraryList resolve(Library library, String arch) { |
| 81 | + List<File> headers = new ArrayList<File>(); |
| 82 | + for (File folder : library.getSrcFolders(arch)) { |
| 83 | + List<File> files = FileUtils.listAllFilesWithExtension(folder, ".h", |
| 84 | + ".c", ".cpp"); |
| 85 | + headers.addAll(files); |
| 86 | + } |
| 87 | + |
| 88 | + LibraryList result = new LibraryList(); |
| 89 | + for (File header : headers) |
| 90 | + result.addOrReplaceAll(resolveHeader(header, headers, library)); |
| 91 | + return result; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Inspect headerFile and search for dependencies |
| 96 | + * |
| 97 | + * @param headerFile |
| 98 | + * @param exclusionList |
| 99 | + * @param library |
| 100 | + */ |
| 101 | + private LibraryList resolveHeader(File headerFile, List<File> exclusionList, |
| 102 | + Library library) { |
| 103 | + LibraryList res = new LibraryList(); |
| 104 | + |
| 105 | + // Extract #includes from header file |
| 106 | + List<String> imports; |
| 107 | + try { |
| 108 | + PdePreprocessor preprocessor = new PdePreprocessor(); |
| 109 | + String header = FileUtils.readFileToString(headerFile); |
| 110 | + preprocessor.writePrefix(header); |
| 111 | + imports = preprocessor.getExtraImports(); |
| 112 | + } catch (IOException e) { |
| 113 | + e.printStackTrace(); |
| 114 | + return res; |
| 115 | + } |
| 116 | + |
| 117 | + // For every #include found... |
| 118 | + for (String libImport : imports) { |
| 119 | + |
| 120 | + // ...check if there is a matching library... |
| 121 | + Library depLib = importToLibrary.get(libImport); |
| 122 | + if (depLib == null || depLib == library) |
| 123 | + continue; |
| 124 | + |
| 125 | + // ...and check if the include is not in the exclusion list |
| 126 | + boolean exclude = false; |
| 127 | + for (File excluded : exclusionList) { |
| 128 | + if (excluded.getName().equals(libImport)) |
| 129 | + exclude = true; |
| 130 | + } |
| 131 | + if (exclude) |
| 132 | + continue; |
| 133 | + |
| 134 | + // add the dependency |
| 135 | + res.addOrReplace(depLib); |
| 136 | + |
| 137 | + System.out.println("Found dependency for " + library.getName()); |
| 138 | + System.out.println(" " + headerFile + " uses " + libImport + " -> " + |
| 139 | + depLib.getName()); |
| 140 | + } |
| 141 | + |
| 142 | + return res; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public Library importToLibrary(String h) { |
| 147 | + return importToLibrary.get(h); |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments