From eca507332449a2f835f3f03a1fd2637902683e98 Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Sun, 6 Sep 2015 02:08:34 -0400 Subject: [PATCH] Support for platform libraries for Arduino. Went to use the ESP8266 wifi library and couldn't since it's shipped with the esp8266 platform. Also fixed up a platform sdk problem that prevented the project from building even after I added the library. Change-Id: If99596f1f531f62f7553b238aa7cf0d6c426aca8 --- .../core/internal/board/ArduinoLibrary.java | 49 ++++++++++++++----- .../core/internal/board/ArduinoManager.java | 5 +- .../core/internal/board/ArduinoPlatform.java | 29 +++++++++++ .../core/internal/board/LibraryIndex.java | 24 ++++++++- .../build/ArduinoBuildConfiguration.java | 20 +------- .../templates/board.mk | 36 +++++++++----- 6 files changed, 119 insertions(+), 44 deletions(-) diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoLibrary.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoLibrary.java index b993165cdc0..a90875cf917 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoLibrary.java +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoLibrary.java @@ -1,15 +1,18 @@ package org.eclipse.cdt.arduino.core.internal.board; import java.io.File; +import java.io.FileReader; +import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Properties; import org.eclipse.cdt.arduino.core.internal.ArduinoPreferences; import org.eclipse.cdt.arduino.core.internal.build.ArduinoBuildConfiguration; -import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; @@ -31,6 +34,29 @@ public class ArduinoLibrary { private int size; private String checksum; + private Path installPath; + + public ArduinoLibrary() { + } + + public ArduinoLibrary(Path propertiesFile) throws IOException { + installPath = propertiesFile.getParent(); + + Properties props = new Properties(); + try (FileReader reader = new FileReader(propertiesFile.toFile())) { + props.load(reader); + } + + name = props.getProperty("name"); //$NON-NLS-1$ + version = props.getProperty("version"); //$NON-NLS-1$ + author = props.getProperty("author"); //$NON-NLS-1$ + maintainer = props.getProperty("maintainer"); //$NON-NLS-1$ + sentence = props.getProperty("sentence"); //$NON-NLS-1$ + paragraph = props.getProperty("paragraph"); //$NON-NLS-1$ + category = props.getProperty("category"); //$NON-NLS-1$ + architectures = Arrays.asList(props.getProperty("architectures").split(",")); //$NON-NLS-1$ //$NON-NLS-2$ + } + public String getName() { return name; } @@ -144,8 +170,9 @@ public class ArduinoLibrary { } public Path getInstallPath() { - return ArduinoPreferences.getArduinoHome().resolve("libraries").resolve(name.replace(' ', '_')) //$NON-NLS-1$ - .resolve(version); + return installPath != null ? installPath + : ArduinoPreferences.getArduinoHome().resolve("libraries").resolve(name.replace(' ', '_')) //$NON-NLS-1$ + .resolve(version); } public boolean isInstalled() { @@ -171,31 +198,31 @@ public class ArduinoLibrary { } } - private void getSources(IProject project, Collection sources, Path dir, boolean recurse) { + private void getSources(Collection sources, Path dir, boolean recurse) { for (File file : dir.toFile().listFiles()) { if (file.isDirectory()) { if (recurse) { - getSources(project, sources, file.toPath(), recurse); + getSources(sources, file.toPath(), recurse); } } else { if (ArduinoBuildConfiguration.isSource(file.getName())) { - sources.add(file.toPath()); + sources.add(file.getAbsolutePath()); } } } } - public Collection getSources(IProject project) { - List sources = new ArrayList<>(); + public Collection getSources() { + List sources = new ArrayList<>(); Path installPath = getInstallPath(); Path srcPath = installPath.resolve("src"); //$NON-NLS-1$ if (srcPath.toFile().isDirectory()) { - getSources(project, sources, srcPath, true); + getSources(sources, srcPath, true); } else { - getSources(project, sources, installPath, false); + getSources(sources, installPath, false); Path utilityPath = installPath.resolve("utility"); //$NON-NLS-1$ if (utilityPath.toFile().isDirectory()) { - getSources(project, sources, utilityPath, false); + getSources(sources, utilityPath, false); } } return sources; diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoManager.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoManager.java index e689505fdc4..2833ac7bd18 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoManager.java +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoManager.java @@ -107,7 +107,7 @@ public class ArduinoManager { } } - public synchronized List getPackageIndices() throws CoreException { + public synchronized List getPackageIndices() { if (packageIndices == null) { String[] boardUrls = ArduinoPreferences.getBoardUrls().split("\n"); //$NON-NLS-1$ packageIndices = new ArrayList<>(boardUrls.length); @@ -118,7 +118,7 @@ public class ArduinoManager { return packageIndices; } - private void loadLibraryIndex(boolean download) { + public void loadLibraryIndex(boolean download) { try { URL librariesUrl = new URL(LIBRARIES_URL); Path librariesPath = ArduinoPreferences.getArduinoHome() @@ -130,6 +130,7 @@ public class ArduinoManager { if (librariesFile.exists()) { try (Reader reader = new FileReader(librariesFile)) { libraryIndex = new Gson().fromJson(reader, LibraryIndex.class); + libraryIndex.resolve(); } } } catch (IOException e) { diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoPlatform.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoPlatform.java index e2bff2453b3..9e3911ed537 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoPlatform.java +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoPlatform.java @@ -8,6 +8,7 @@ package org.eclipse.cdt.arduino.core.internal.board; import java.io.BufferedReader; +import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader; @@ -17,6 +18,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -25,6 +27,7 @@ import java.util.Properties; import org.eclipse.cdt.arduino.core.internal.Activator; import org.eclipse.cdt.arduino.core.internal.ArduinoPreferences; import org.eclipse.cdt.arduino.core.internal.HierarchicalProperties; +import org.eclipse.cdt.arduino.core.internal.build.ArduinoBuildConfiguration; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; @@ -193,6 +196,29 @@ public class ArduinoPlatform { installPath.resolve("variants/{build.variant}")); //$NON-NLS-1$ } + private void getSources(Collection sources, Path dir, boolean recurse) { + for (File file : dir.toFile().listFiles()) { + if (file.isDirectory()) { + if (recurse) { + getSources(sources, file.toPath(), recurse); + } + } else { + if (ArduinoBuildConfiguration.isSource(file.getName())) { + sources.add(file.getAbsolutePath()); + } + } + } + } + + public Collection getSources(String core) { + List sources = new ArrayList<>(); + Path srcPath = getInstallPath().resolve("cores").resolve(core); //$NON-NLS-1$ + if (srcPath.toFile().isDirectory()) { + getSources(sources, srcPath, true); + } + return sources; + } + public IStatus install(IProgressMonitor monitor) { // Check if we're installed already if (isInstalled()) { @@ -234,6 +260,9 @@ public class ArduinoPlatform { } } + // Reload the library index to pick up platform libraries + ArduinoManager.instance.loadLibraryIndex(false); + return mstatus != null ? mstatus : Status.OK_STATUS; } diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/LibraryIndex.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/LibraryIndex.java index a0bbef428ca..4df9626aab7 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/LibraryIndex.java +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/LibraryIndex.java @@ -1,5 +1,8 @@ package org.eclipse.cdt.arduino.core.internal.board; +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -18,7 +21,26 @@ public class LibraryIndex { // library name to latest version of library private Map latestLibs = new HashMap<>(); - public void resolve() { + public void resolve() throws IOException { + // Add in platform libraries + for (PackageIndex index : ArduinoManager.instance.getPackageIndices()) { + for (ArduinoPackage pkg : index.getPackages()) { + for (ArduinoPlatform platform : pkg.getPlatforms()) { + if (platform.isInstalled()) { + File[] libraryDirs = platform.getInstallPath().resolve("libraries").toFile().listFiles(); //$NON-NLS-1$ + if (libraryDirs != null) { + for (File libraryDir : libraryDirs) { + Path propsPath = libraryDir.toPath().resolve("library.properties"); //$NON-NLS-1$ + if (propsPath.toFile().exists()) { + libraries.add(new ArduinoLibrary(propsPath)); + } + } + } + } + } + } + } + for (ArduinoLibrary library : libraries) { String name = library.getName(); diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/build/ArduinoBuildConfiguration.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/build/ArduinoBuildConfiguration.java index 0a1b76ef89f..a1b58a4dfe5 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/build/ArduinoBuildConfiguration.java +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/build/ArduinoBuildConfiguration.java @@ -2,7 +2,6 @@ package org.eclipse.cdt.arduino.core.internal.build; import java.io.BufferedReader; import java.io.File; -import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStreamReader; import java.nio.file.Files; @@ -332,9 +331,7 @@ public class ArduinoBuildConfiguration { // The list of library sources List librarySources = new ArrayList<>(); for (ArduinoLibrary lib : ArduinoManager.instance.getLibraries(project)) { - for (Path path : lib.getSources(project)) { - librarySources.add(pathString(path)); - } + librarySources.addAll(lib.getSources()); } buildModel.put("libraries_srcs", librarySources); //$NON-NLS-1$ buildModel.put("libraries_path", pathString(ArduinoPreferences.getArduinoHome().resolve("libraries"))); //$NON-NLS-1$ //$NON-NLS-2$ @@ -363,20 +360,7 @@ public class ArduinoBuildConfiguration { Path platformPath = platform.getInstallPath(); buildModel.put("platform_path", pathString(platformPath)); //$NON-NLS-1$ - - Path corePath = platformPath.resolve("cores").resolve((String) properties.get("build.core")); //$NON-NLS-1$ //$NON-NLS-2$ - File[] platformFiles = corePath.toFile().listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return isSource(name); - } - }); - - String[] platformSource = new String[platformFiles.length]; - for (int i = 0; i < platformSource.length; ++i) { - platformSource[i] = pathString(platformFiles[i].toPath()); - } - buildModel.put("platform_srcs", platformSource); //$NON-NLS-1$ + buildModel.put("platform_srcs", platform.getSources(properties.getProperty("build.core"))); //$NON-NLS-1$ //$NON-NLS-2$ properties.put("object_file", "$@"); //$NON-NLS-1$ //$NON-NLS-2$ properties.put("source_file", "$<"); //$NON-NLS-1$ //$NON-NLS-2$ diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/templates/board.mk b/toolchains/arduino/org.eclipse.cdt.arduino.core/templates/board.mk index 38aee8c3f7e..0d1b43ef3f6 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/templates/board.mk +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/templates/board.mk @@ -10,7 +10,7 @@ PROJECT_OBJS = \ <#list project_srcs as file> <#assign cpp = file?matches("(.*)\\.cpp")> <#if cpp> - ${build_path}/project/${cpp?groups[1]}.o \ + ${build_path}/project/${cpp?groups[1]}.cpp.o \ @@ -18,11 +18,11 @@ PLATFORM_OBJS = \ <#list platform_srcs as file> <#assign cpp = file?matches("${platform_path}/(.*)\\.cpp")> <#if cpp> - ${build_path}/platform/${cpp?groups[1]}.o \ + ${build_path}/platform/${cpp?groups[1]}.cpp.o \ <#assign c = file?matches("${platform_path}/(.*)\\.c")> <#if c> - ${build_path}/platform/${c?groups[1]}.o \ + ${build_path}/platform/${c?groups[1]}.c.o \ <#assign S = file?matches("${platform_path}/(.*)\\.S")> <#if S> @@ -33,14 +33,20 @@ PLATFORM_OBJS = \ LIBRARIES_OBJS = \ <#list libraries_srcs as file> <#assign cpp = file?matches("${libraries_path}/(.*?)/.*?/(.*)\\.cpp")> +<#if !cpp> +<#assign cpp = file?matches("${platform_path}/libraries/(.*?)/.*?/(.*)\\.cpp")> + <#if cpp> - ${build_path}/libraries/${cpp?groups[1]}/${cpp?groups[2]}.o \ + ${build_path}/libraries/${cpp?groups[1]}/${cpp?groups[2]}.cpp.o \ <#assign c = file?matches("${libraries_path}/(.*?)/.*?/(.*)\\.c")> -<#if c> - ${build_path}/libraries/${c?groups[1]}/${c?groups[2]}.o \ +<#if !c> +<#assign c = file?matches("${platform_path}/libraries/(.*?)/.*?/(.*)\\.c")> - +<#if c> + ${build_path}/libraries/${c?groups[1]}/${c?groups[2]}.c.o \ + + all: ${build_path}/${project_name}.hex ${build_path}/${project_name}.eep @@ -64,7 +70,7 @@ size: <#list project_srcs as file> <#assign cpp = file?matches("(.*)\\.cpp")> <#if cpp> -${build_path}/project/${cpp?groups[1]}.o: ../${file} +${build_path}/project/${cpp?groups[1]}.cpp.o: ../${file} @$(call mymkdir,$(dir $@)) ${recipe_cpp_o_pattern} @@ -74,7 +80,7 @@ ${build_path}/project/${cpp?groups[1]}.o: ../${file} <#list platform_srcs as file> <#assign cpp = file?matches("${platform_path}/(.*)\\.cpp")> <#if cpp> -${build_path}/platform/${cpp?groups[1]}.o: ${file} +${build_path}/platform/${cpp?groups[1]}.cpp.o: ${file} @$(call mymkdir,$(dir $@)) ${recipe_cpp_o_pattern} ${recipe_ar_pattern} @@ -82,7 +88,7 @@ ${build_path}/platform/${cpp?groups[1]}.o: ${file} <#assign c = file?matches("${platform_path}/(.*)\\.c")> <#if c> -${build_path}/platform/${c?groups[1]}.o: ${file} +${build_path}/platform/${c?groups[1]}.c.o: ${file} @$(call mymkdir,$(dir $@)) ${recipe_c_o_pattern} ${recipe_ar_pattern} @@ -100,15 +106,21 @@ ${build_path}/platform/${S?groups[1]}.S.o: ${file} <#list libraries_srcs as file> <#assign cpp = file?matches("${libraries_path}/(.*?)/.*?/(.*)\\.cpp")> +<#if !cpp> +<#assign cpp = file?matches("${platform_path}/libraries/(.*?)/.*?/(.*)\\.cpp")> + <#if cpp> -${build_path}/libraries/${cpp?groups[1]}/${cpp?groups[2]}.o: ${file} +${build_path}/libraries/${cpp?groups[1]}/${cpp?groups[2]}.cpp.o: ${file} @$(call mymkdir,$(dir $@)) ${recipe_cpp_o_pattern} <#assign c = file?matches("${libraries_path}/(.*?)/.*?/(.*)\\.c")> +<#if !c> +<#assign c = file?matches("${platform_path}/libraries/(.*?)/.*?/(.*)\\.c")> + <#if c> -${build_path}/libraries/${c?groups[1]}/${c?groups[2]}.o: ${file} +${build_path}/libraries/${c?groups[1]}/${c?groups[2]}.c.o: ${file} @$(call mymkdir,$(dir $@)) ${recipe_c_o_pattern}