1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-08-01 03:47:23 +03:00

Various cleanups. Introduced class PreferencesMap to replace/simplify Map<String, String>.

This commit is contained in:
Cristian Maglie
2011-12-28 20:00:07 +01:00
parent e63c2d1429
commit dc616601cd
5 changed files with 320 additions and 389 deletions

View File

@ -23,18 +23,25 @@
package processing.app.debug;
import static processing.app.I18n._;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import processing.app.Base;
import processing.app.PreferencesMap;
import processing.app.I18n;
import processing.app.Preferences;
import processing.app.Sketch;
import processing.app.SketchCode;
import processing.core.*;
import processing.app.I18n;
import static processing.app.I18n._;
import java.io.*;
import java.util.*;
import java.text.MessageFormat;
import processing.core.PApplet;
public class Compiler implements MessageConsumer {
static final String BUGS_URL =
@ -45,60 +52,54 @@ public class Compiler implements MessageConsumer {
Sketch sketch;
String buildPath;
String primaryClassName;
String platform;
String board;
boolean verbose;
RunnerException exception;
HashMap<String, String> configPreferences;
HashMap<String, String> platformPreferences;
String avrBasePath;
String corePath;
List<File> objectFiles;
ArrayList<String> includePaths;
public Compiler() { }
/**
* Compile with avr-gcc.
*
* @param sketch Sketch object to be compiled.
* @param buildPath Where the temporary files live and will be built from.
* @param primaryClassName the name of the combined sketch file w/ extension
* @param _sketch Sketch object to be compiled.
* @param _buildPath Where the temporary files live and will be built from.
* @param _primaryClassName the name of the combined sketch file w/ extension
* @return true if successful.
* @throws RunnerException Only if there's a problem. Only then.
*/
public boolean compile(Sketch sketch,
String buildPath,
String primaryClassName,
boolean verbose) throws RunnerException {
this.sketch = sketch;
this.buildPath = buildPath;
this.primaryClassName = primaryClassName;
this.verbose = verbose;
public boolean compile(Sketch _sketch,
String _buildPath,
String _primaryClassName,
boolean _verbose) throws RunnerException {
sketch = _sketch;
buildPath = _buildPath;
primaryClassName = _primaryClassName;
verbose = _verbose;
objectFiles = new ArrayList<File>();
Map<String, String> boardPreferences = Base.getBoardPreferences();
PreferencesMap boardPreferences = Base.getBoardPreferences();
//Check for null platform, and use system default if not found
platform = boardPreferences.get("platform");
// Check for null platform, and use system default if not found
PreferencesMap platformPreferences;
String platform = boardPreferences.get("platform");
if (platform == null)
{
platformPreferences = new HashMap(Base.getPlatformPreferences());
}
platformPreferences = Base.getPlatformPreferences();
else
{
platformPreferences = new HashMap(Base.getPlatformPreferences(platform));
platformPreferences = Base.getPlatformPreferences(platform);
// Merge all the global preference configuration
PreferencesMap configPreferences = new PreferencesMap();
configPreferences.putAll(Preferences.getMap());
configPreferences.putAll(platformPreferences);
configPreferences.putAll(boardPreferences);
for (String k : configPreferences.keySet()) {
if (configPreferences.get(k)==null)
configPreferences.put(k, "");
}
System.out.println("////////////////////////////compiler.java is doing stuff/////////////////");
//Put all the global preference configuration into one Master configpreferences
configPreferences = mergePreferences( Preferences.getMap(), platformPreferences, boardPreferences);
avrBasePath = configPreferences.get("compiler.path");
String avrBasePath = configPreferences.get("compiler.path");
if (avrBasePath == null)
{
avrBasePath = Base.getAvrBasePath();
@ -118,23 +119,19 @@ public class Compiler implements MessageConsumer {
Object[] Args = {basePath};
avrBasePath = compileFormat.format( Args );
System.out.println("avrBasePath:new: " + avrBasePath);
}
this.board = configPreferences.get("board");
if (this.board == "")
{
this.board = "_UNKNOWN";
}
board = configPreferences.get("board");
if (board == "")
board = "_UNKNOWN";
String core = configPreferences.get("build.core");
if (core == null) {
RunnerException re = new RunnerException(_("No board selected; please choose a board from the Tools > Board menu."));
RunnerException re = new RunnerException(_("No board selected; please choose a board from the Tools > Board menu."));
re.hideStackTrace();
throw re;
}
String corePath;
if (core.indexOf(':') == -1) {
Target t = Base.getTarget();
File coreFolder = new File(new File(t.getFolder(), "cores"), core);
@ -148,7 +145,6 @@ public class Compiler implements MessageConsumer {
String variant = boardPreferences.get("build.variant");
String variantPath = null;
if (variant != null) {
if (variant.indexOf(':') == -1) {
Target t = Base.getTarget();
@ -164,25 +160,25 @@ public class Compiler implements MessageConsumer {
// 0. include paths for core + all libraries
sketch.setCompilingProgress(20);
ArrayList<String> includePaths = new ArrayList<String>();
List<String> includePaths = new ArrayList<String>();
includePaths.add(corePath);
if (variantPath != null) includePaths.add(variantPath);
for (File file : sketch.getImportedLibraries()) {
if (variantPath != null)
includePaths.add(variantPath);
for (File file : sketch.getImportedLibraries())
includePaths.add(file.getPath());
}
// 1. compile the sketch (already in the buildPath)
System.out.println("1. compileSketch");
sketch.setCompilingProgress(30);
compileSketch(avrBasePath, buildPath, includePaths, configPreferences);
compileSketch(avrBasePath, _buildPath, includePaths, configPreferences);
// 2. compile the libraries, outputting .o files to: <buildPath>/<library>/
// 2. compile the libraries, outputting .o files to:
// <buildPath>/<library>/
//Doesn't really use configPreferences
System.out.println("2. compileLibraries");
sketch.setCompilingProgress(40);
compileLibraries(avrBasePath, buildPath, includePaths, configPreferences);
System.out.println("2. compileLibraries");
sketch.setCompilingProgress(40);
compileLibraries(avrBasePath, _buildPath, includePaths, configPreferences);
/*
for (File libraryFolder : sketch.getImportedLibraries()) {
@ -215,7 +211,7 @@ public class Compiler implements MessageConsumer {
System.out.println("3. compileCore");
System.out.println("corePath: " + corePath);
sketch.setCompilingProgress(50);
compileCore(avrBasePath, buildPath, corePath, variant, variantPath, configPreferences);
compileCore(avrBasePath, _buildPath, corePath, variant, variantPath, configPreferences);
/*
@ -244,7 +240,7 @@ public class Compiler implements MessageConsumer {
// 4. link it all together into the .elf file
sketch.setCompilingProgress(60);
System.out.println("4. compileLink");
compileLink(avrBasePath, buildPath, corePath, includePaths, configPreferences);
compileLink(avrBasePath, _buildPath, corePath, includePaths, configPreferences);
/*
List baseCommandLinker = new ArrayList(Arrays.asList(new String[] {
@ -291,7 +287,7 @@ public class Compiler implements MessageConsumer {
execAsynchronously(commandObjcopy);
*/
System.out.println("5. compileEep");
compileEep(avrBasePath, buildPath, includePaths, configPreferences);
compileEep(avrBasePath, _buildPath, includePaths, configPreferences);
// 6. build the .hex file
sketch.setCompilingProgress(80);
@ -304,7 +300,7 @@ public class Compiler implements MessageConsumer {
execAsynchronously(commandObjcopy);
*/
System.out.println("6. compileHex");
compileHex(avrBasePath, buildPath, includePaths, configPreferences);
compileHex(avrBasePath, _buildPath, includePaths, configPreferences);
sketch.setCompilingProgress(90);
return true;
@ -312,10 +308,10 @@ public class Compiler implements MessageConsumer {
private List<File> compileFiles(String avrBasePath,
String buildPath, ArrayList<String> includePaths,
ArrayList<File> sSources,
ArrayList<File> cSources, ArrayList<File> cppSources,
Map<String, String> boardPreferences)
String buildPath, List<String> includePaths,
List<File> sSources,
List<File> cSources, List<File> cppSources,
PreferencesMap prefs)
throws RunnerException {
List<File> objectPaths = new ArrayList<File>();
@ -326,7 +322,7 @@ public class Compiler implements MessageConsumer {
execAsynchronously(getCommandCompilerS(avrBasePath, includePaths,
file.getAbsolutePath(),
objectPath,
configPreferences));
prefs));
}
for (File file : cSources) {
@ -335,11 +331,11 @@ public class Compiler implements MessageConsumer {
File objectFile = new File(objectPath);
File dependFile = new File(dependPath);
objectPaths.add(objectFile);
if (is_already_compiled(file, objectFile, dependFile, boardPreferences)) continue;
if (is_already_compiled(file, objectFile, dependFile, prefs)) continue;
execAsynchronously(getCommandCompilerC(avrBasePath, includePaths,
file.getAbsolutePath(),
objectPath,
configPreferences));
prefs));
}
for (File file : cppSources) {
@ -348,11 +344,11 @@ public class Compiler implements MessageConsumer {
File objectFile = new File(objectPath);
File dependFile = new File(dependPath);
objectPaths.add(objectFile);
if (is_already_compiled(file, objectFile, dependFile, boardPreferences)) continue;
if (is_already_compiled(file, objectFile, dependFile, prefs)) continue;
execAsynchronously(getCommandCompilerCPP(avrBasePath, includePaths,
file.getAbsolutePath(),
objectPath,
configPreferences));
prefs));
}
return objectPaths;
@ -621,9 +617,9 @@ public class Compiler implements MessageConsumer {
// ///////////////////////////////////////////////////////////////////////////
static private String[] getCommandCompilerS(String avrBasePath,
ArrayList<String> includePaths, String sourceName, String objectName,
HashMap<String, String> configPreferences)
{
List<String> includePaths, String sourceName, String objectName,
PreferencesMap configPreferences)
{
System.out.println("getCommandCompilerS: start");
String baseCommandString = configPreferences.get("recipe.cpp.o.pattern");
MessageFormat compileFormat = new MessageFormat(baseCommandString);
@ -682,8 +678,8 @@ public class Compiler implements MessageConsumer {
//removed static
private String[] getCommandCompilerC(String avrBasePath,
ArrayList<String> includePaths, String sourceName, String objectName,
HashMap<String, String> configPreferences)
List<String> includePaths, String sourceName, String objectName,
PreferencesMap configPreferences)
{
System.out.println("getCommandCompilerC: start");
String baseCommandString = configPreferences.get("recipe.c.o.pattern");
@ -691,7 +687,7 @@ public class Compiler implements MessageConsumer {
//getIncludes to String
String includes = preparePaths(includePaths);
Object[] Args = {
String[] args = {
avrBasePath,
configPreferences.get("compiler.c.cmd"),
configPreferences.get("compiler.c.flags"),
@ -699,13 +695,13 @@ public class Compiler implements MessageConsumer {
configPreferences.get("build.mcu"),
configPreferences.get("build.f_cpu"),
configPreferences.get("software"),
Base.REVISION,
"" + Base.REVISION,
includes,
sourceName,
objectName
};
String command = compileFormat.format( Args );
String command = compileFormat.format(args);
String[] commandArray = command.split("\\|");
return commandArray;
}
@ -743,8 +739,8 @@ public class Compiler implements MessageConsumer {
*/
static private String[] getCommandCompilerCPP(String avrBasePath,
ArrayList<String> includePaths, String sourceName, String objectName,
HashMap<String, String> configPreferences)
List<String> includePaths, String sourceName, String objectName,
PreferencesMap configPreferences)
{
System.out.println("getCommandCompilerCPP: start");
String baseCommandString = configPreferences.get("recipe.cpp.o.pattern");
@ -752,7 +748,7 @@ public class Compiler implements MessageConsumer {
//getIncludes to String
String includes = preparePaths(includePaths);
Object[] Args = {
String[] args = {
avrBasePath,
configPreferences.get("compiler.cpp.cmd"),
configPreferences.get("compiler.cpp.flags"),
@ -760,13 +756,13 @@ public class Compiler implements MessageConsumer {
configPreferences.get("build.mcu"),
configPreferences.get("build.f_cpu"),
configPreferences.get("software"),
Base.REVISION,
"" + Base.REVISION,
includes,
sourceName,
objectName
};
String command = compileFormat.format( Args );
String command = compileFormat.format(args);
String[] commandArray = command.split("\\|");
/*
@ -803,15 +799,15 @@ public class Compiler implements MessageConsumer {
return (new File(path)).list(onlyHFiles);
}
static public ArrayList<File> findFilesInPath(String path, String extension,
boolean recurse) {
System.out.println("findFilesInPath: " + path);
static public List<File> findFilesInPath(String path, String extension,
boolean recurse) {
System.out.println("findFilesInPath: " + path);
return findFilesInFolder(new File(path), extension, recurse);
}
static public ArrayList<File> findFilesInFolder(File folder, String extension,
boolean recurse) {
ArrayList<File> files = new ArrayList<File>();
static public List<File> findFilesInFolder(File folder, String extension,
boolean recurse) {
List<File> files = new ArrayList<File>();
if (folder.listFiles() == null) return files;
@ -831,17 +827,18 @@ public class Compiler implements MessageConsumer {
// 1. compile the sketch (already in the buildPath)
void compileSketch(String avrBasePath, String buildPath, ArrayList<String> includePaths, HashMap<String, String> configPreferences)
void compileSketch(String avrBasePath, String buildPath,
List<String> includePaths, PreferencesMap configPreferences)
throws RunnerException
{
System.out.println("compileSketch: start");
System.out.println("includePaths: ");
for (int i = 0; i < includePaths.size(); i++) {
System.out.println("-I" + (String) includePaths.get(i));
}
for (int i = 0; i < includePaths.size(); i++) {
System.out.println("-I" + (String) includePaths.get(i));
}
//logger.debug("compileSketch: start");
this.objectFiles.addAll(compileFiles(avrBasePath, buildPath, includePaths,
objectFiles.addAll(compileFiles(avrBasePath, buildPath, includePaths,
findFilesInPath(buildPath, "S", false),
findFilesInPath(buildPath, "c", false),
findFilesInPath(buildPath, "cpp", false),
@ -851,11 +848,11 @@ public class Compiler implements MessageConsumer {
// 2. compile the libraries, outputting .o files to:
// <buildPath>/<library>/
void compileLibraries (String avrBasePath, String buildPath,
ArrayList<String> includePaths,
HashMap<String, String> configPreferences)
List<String> includePaths,
PreferencesMap configPreferences)
throws RunnerException
{
System.out.println("compileLibraries: start");
System.out.println("compileLibraries: start");
for (File libraryFolder : sketch.getImportedLibraries()) {
System.out.println("libraryFolder: " + libraryFolder);
@ -894,20 +891,19 @@ public class Compiler implements MessageConsumer {
// collecting them into the core.a library file.
void compileCore (String avrBasePath, String buildPath,
String corePath, String variant, String variantPath,
HashMap<String, String> configPreferences)
PreferencesMap configPreferences)
throws RunnerException
{
System.out.println("compileCore(...) start");
ArrayList<String> includePaths = new ArrayList();
List<String> includePaths = new ArrayList<String>();
includePaths.add(corePath); //include core path only
if (variantPath != null) includePaths.add(variantPath);
//debug includePaths
System.out.println("includePaths: ");
for (int i = 0; i < includePaths.size(); i++) {
for (int i = 0; i < includePaths.size(); i++)
System.out.println("-I" + (String) includePaths.get(i));
}
String baseCommandString = configPreferences.get("recipe.ar.pattern");
String commandString = "";
@ -926,7 +922,7 @@ public class Compiler implements MessageConsumer {
//List commandAR = new ArrayList(baseCommandAR);
//commandAR = commandAR + file.getAbsolutePath();
Object[] Args = {
String[] args = {
avrBasePath,
configPreferences.get("compiler.ar.cmd"),
configPreferences.get("compiler.ar.flags"),
@ -936,20 +932,18 @@ public class Compiler implements MessageConsumer {
//objectName
file.getAbsolutePath()
};
System.out.println("compileCore(...) substitute");
commandString = compileFormat.format( Args );
System.out.println("compileCore(...) substitute");
commandString = compileFormat.format(args);
String[] commandArray = commandString.split("\\|");
execAsynchronously(commandArray);
}
}
// 4. link it all together into the .elf file
void compileLink(String avrBasePath, String buildPath,
String corePath, ArrayList<String> includePaths,
HashMap<String, String> configPreferences)
String corePath, List<String> includePaths,
PreferencesMap configPreferences)
throws RunnerException
{
// For atmega2560, need --relax linker option to link larger
@ -969,7 +963,7 @@ public class Compiler implements MessageConsumer {
}
System.out.println("objectFileList: " + objectFileList);
Object[] Args = {
String[] args = {
avrBasePath,
configPreferences.get("compiler.c.elf.cmd"),
configPreferences.get("compiler.c.elf.flags")+optRelax,
@ -983,128 +977,59 @@ public class Compiler implements MessageConsumer {
corePath,
configPreferences.get("ldscript"),
};
commandString = compileFormat.format( Args );
commandString = compileFormat.format(args);
String[] commandArray = commandString.split("\\|");
execAsynchronously(commandArray);
}
// 5. extract EEPROM data (from EEMEM directive) to .eep file.
void compileEep (String avrBasePath, String buildPath, ArrayList<String> includePaths, HashMap<String, String> configPreferences)
void compileEep (String avrBasePath, String buildPath,
List<String> includePaths, PreferencesMap configPreferences)
throws RunnerException
{
//logger.debug("compileEep: start");
String baseCommandString = configPreferences.get("recipe.objcopy.eep.pattern");
String commandString = "";
MessageFormat compileFormat = new MessageFormat(baseCommandString);
String objectFileList = "";
Object[] Args = {
String[] args = {
avrBasePath,
configPreferences.get("compiler.objcopy.cmd"),
configPreferences.get("compiler.objcopy.eep.flags"),
buildPath + File.separator + primaryClassName,
buildPath + File.separator + primaryClassName
};
commandString = compileFormat.format( Args );
commandString = compileFormat.format(args);
String[] commandArray = commandString.split("\\|");
execAsynchronously(commandArray);
}
// 6. build the .hex file
void compileHex (String avrBasePath, String buildPath, ArrayList<String> includePaths, HashMap<String, String> configPreferences)
void compileHex (String avrBasePath, String buildPath,
List<String> includePaths, PreferencesMap configPreferences)
throws RunnerException
{
//logger.debug("compileHex: start");
String baseCommandString = configPreferences.get("recipe.objcopy.hex.pattern");
String commandString = "";
MessageFormat compileFormat = new MessageFormat(baseCommandString);
String objectFileList = "";
Object[] Args = {
String[] args = {
avrBasePath,
configPreferences.get("compiler.elf2hex.cmd"),
configPreferences.get("compiler.elf2hex.flags"),
buildPath + File.separator + primaryClassName,
buildPath + File.separator + primaryClassName
};
commandString = compileFormat.format( Args );
commandString = compileFormat.format(args);
String[] commandArray = commandString.split("\\|");
execAsynchronously(commandArray);
}
//merge all the preferences file in the correct order of precedence
HashMap mergePreferences(Map Preferences, Map platformPreferences, Map boardPreferences)
{
HashMap _map = new HashMap();
Iterator iterator = Preferences.entrySet().iterator();
while(iterator.hasNext())
{
Map.Entry pair = (Map.Entry)iterator.next();
if (pair.getValue() == null)
{
_map.put(pair.getKey(), "");
}
else
{
_map.put(pair.getKey(), pair.getValue());
}
}
//logger.debug("Done: Preferences");
iterator = platformPreferences.entrySet().iterator();
while(iterator.hasNext())
{
Map.Entry pair = (Map.Entry)iterator.next();
if (pair.getValue() == null)
{
_map.put(pair.getKey(), "");
}
else
{
_map.put(pair.getKey(), pair.getValue());
}
//System.out.println(pair.getKey() + " = " + pair.getValue());
}
//System.out.println("Done: platformPreferences");
iterator = boardPreferences.entrySet().iterator();
while(iterator.hasNext())
{
Map.Entry pair = (Map.Entry)iterator.next();
if (pair.getValue() == null)
{
_map.put(pair.getKey(), "");
}
else
{
_map.put(pair.getKey(), pair.getValue());
}
//System.out.println(pair.getKey() + " = " + pair.getValue());
}
//System.out.println("Done: boardPreferences");
return _map;
}
private static String preparePaths(ArrayList<String> includePaths) {
//getIncludes to String
//logger.debug("Start: Prepare paths");
private static String preparePaths(List<String> includePaths) {
String includes = "";
for (int i = 0; i < includePaths.size(); i++)
{
includes = includes + (" -I" + (String) includePaths.get(i)) + "|";
}
//logger.debug("Paths prepared: " + includes);
for (String p : includePaths)
includes += " -I" + p + "|";
return includes;
}
}