1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-26 20:54:22 +01:00

IDE: various refactoring to make sam hardware compiling.

This commit is contained in:
Cristian Maglie 2011-09-23 04:47:41 +02:00
parent 02c76ff87b
commit 3681035869
5 changed files with 214 additions and 254 deletions

View File

@ -1841,7 +1841,7 @@ public class Editor extends JFrame implements RunnerListener {
public void run() { public void run() {
try { try {
sketch.prepare(); sketch.prepare();
String appletClassName = sketch.build(false); sketch.build(false);
statusNotice("Done compiling."); statusNotice("Done compiling.");
} catch (Exception e) { } catch (Exception e) {
statusError(e); statusError(e);
@ -1856,7 +1856,7 @@ public class Editor extends JFrame implements RunnerListener {
public void run() { public void run() {
try { try {
sketch.prepare(); sketch.prepare();
String appletClassName = sketch.build(true); sketch.build(true);
statusNotice("Done compiling."); statusNotice("Done compiling.");
} catch (Exception e) { } catch (Exception e) {
statusError(e); statusError(e);

View File

@ -32,15 +32,11 @@ import processing.app.preproc.*;
import processing.core.*; import processing.core.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*; import java.io.*;
import java.text.MessageFormat;
import java.util.*; import java.util.*;
import java.util.zip.*;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
/** /**
@ -1512,17 +1508,50 @@ public class Sketch {
// run the preprocessor // run the preprocessor
String primaryClassName = preprocess(buildPath); String primaryClassName = preprocess(buildPath);
Map<String, String> config = new HashMap<String, String>();
mergeMapsAndRemoveNulls(Preferences.getMap(), config);
Map<String, String> boardPrefs = Base.getBoardPreferences();
// Check for null platform, and use system default if not found
String platform = boardPrefs.get("platform");
if (platform == null)
mergeMapsAndRemoveNulls(Base.getPlatformPreferences(), config);
else
mergeMapsAndRemoveNulls(Base.getPlatformPreferences(platform), config);
mergeMapsAndRemoveNulls(boardPrefs, config);
String toolchainPath = config.get("compiler.path");
if (toolchainPath == null) {
toolchainPath = Base.getAvrBasePath();
} else {
// Put in the system path in the compiler path if available
String basePath = System.getProperty("user.dir");
if (Base.isMacOS())
basePath += "/Arduino.app/Contents/Resources/Java";
Object[] args = { basePath };
toolchainPath = new MessageFormat(toolchainPath).format(args);
}
config.put("compiler.path", toolchainPath);
// compile the program. errors will happen as a RunnerException // compile the program. errors will happen as a RunnerException
// that will bubble up to whomever called build(). // that will bubble up to whomever called build().
Compiler compiler = new Compiler(); Compiler compiler = new Compiler(config);
if (compiler.compile(this, buildPath, primaryClassName, verbose)) { if (compiler.compile(this, buildPath, primaryClassName, verbose)) {
size(buildPath, primaryClassName); size(buildPath, primaryClassName, config);
return primaryClassName; return primaryClassName;
} }
return null; return null;
}
private void mergeMapsAndRemoveNulls(Map<String, String> src,
Map<String, String> dst) {
for (String k : src.keySet()) {
String v = src.get(k);
if (v == null)
v = "";
dst.put(k, v);
}
} }
protected boolean exportApplet(boolean usingProgrammer) throws Exception { protected boolean exportApplet(boolean usingProgrammer) throws Exception {
return exportApplet(tempBuildFolder.getAbsolutePath(), usingProgrammer); return exportApplet(tempBuildFolder.getAbsolutePath(), usingProgrammer);
} }
@ -1574,13 +1603,14 @@ public class Sketch {
} }
protected void size(String buildPath, String suggestedClassName) protected void size(String buildPath, String suggestedClassName,
throws RunnerException { Map<String, String> prefs) throws RunnerException {
long size = 0; long size = 0;
String maxsizeString = Base.getBoardPreferences().get("upload.maximum_size"); String maxsizeString = prefs.get("upload.maximum_size");
if (maxsizeString == null) return; if (maxsizeString == null)
return;
long maxsize = Integer.parseInt(maxsizeString); long maxsize = Integer.parseInt(maxsizeString);
Sizer sizer = new Sizer(buildPath, suggestedClassName); Sizer sizer = new Sizer(buildPath, suggestedClassName, prefs);
try { try {
size = sizer.computeSize(); size = sizer.computeSize();
System.out.println("Binary sketch size: " + size + " bytes (of a " + System.out.println("Binary sketch size: " + size + " bytes (of a " +

View File

@ -28,14 +28,10 @@ import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import processing.app.Base; import processing.app.Base;
import processing.app.Preferences;
import processing.app.Sketch; import processing.app.Sketch;
import processing.app.SketchCode; import processing.app.SketchCode;
import processing.core.PApplet; import processing.core.PApplet;
@ -62,15 +58,15 @@ public class Compiler implements MessageConsumer {
Map<String, String> configPreferences; Map<String, String> configPreferences;
Map<String, String> boardPreferences;
Map<String, String> platformPreferences;
String avrBasePath; String avrBasePath;
List<File> objectFiles; List<File> objectFiles;
public Compiler() { public Compiler(Map<String, String> preferences) {
// Merge all the preferences file in the correct order of precedence
// into a new map.
configPreferences = preferences;
avrBasePath = configPreferences.get("compiler.path");
} }
/** /**
@ -95,40 +91,8 @@ public class Compiler implements MessageConsumer {
this.verbose = verbose; this.verbose = verbose;
objectFiles = new ArrayList<File>(); objectFiles = new ArrayList<File>();
// the pms object isn't used for anything but storage // System.out.println("-> compiler.java is doing stuff");
// MessageStream pms = new MessageStream(this);
Map<String, String> boardPreferences = Base.getBoardPreferences();
// Check for null platform, and use system default if not found
platform = boardPreferences.get("platform");
if (platform == null)
platformPreferences = new HashMap(Base.getPlatformPreferences());
else
platformPreferences = new HashMap(Base.getPlatformPreferences(platform));
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");
if (avrBasePath == null) {
avrBasePath = Base.getAvrBasePath();
// System.out.println("avrBasePath: " + avrBasePath);
} else {
// System.out.println("avrBasePath:exists: " + avrBasePath);
// Put in the system path in the compiler path if available
String basePath = System.getProperty("user.dir");
if (Base.isMacOS()) {
// logger.debug("basePath: " + basePath);
basePath += "/Arduino.app/Contents/Resources/Java";
}
Object[] Args = { basePath };
avrBasePath = new MessageFormat(avrBasePath).format(Args);
// System.out.println("avrBasePath:new: " + avrBasePath);
}
board = configPreferences.get("board"); board = configPreferences.get("board");
if (board == "") if (board == "")
board = "_UNKNOWN"; board = "_UNKNOWN";
@ -148,7 +112,6 @@ public class Compiler implements MessageConsumer {
target = Base.getTarget(); target = Base.getTarget();
coreFolder = new File(new File(target.getFolder(), "cores"), core); coreFolder = new File(new File(target.getFolder(), "cores"), core);
corePath = coreFolder.getAbsolutePath(); corePath = coreFolder.getAbsolutePath();
systemPath = new File(target.getFolder(), "system").getAbsolutePath(); systemPath = new File(target.getFolder(), "system").getAbsolutePath();
} else { } else {
target = Base.targetsTable.get(core.substring(0, core.indexOf(':'))); target = Base.targetsTable.get(core.substring(0, core.indexOf(':')));
@ -501,20 +464,24 @@ public class Compiler implements MessageConsumer {
String objectName, String objectName,
Map<String, String> configPreferences) { Map<String, String> configPreferences) {
System.out.println("getCommandCompilerS: start"); System.out.println("getCommandCompilerS: start");
String baseCommandString = configPreferences.get("recipe.cpp.o.pattern"); String recipe = configPreferences.get("recipe.cpp.o.pattern");
MessageFormat compileFormat = new MessageFormat(baseCommandString); MessageFormat compileFormat = new MessageFormat(recipe);
// getIncludes to String // getIncludes to String
String includes = preparePaths(includePaths); String args[] = new String[11];
Object[] Args = { avrBasePath, configPreferences.get("compiler.cpp.cmd"), args[0] = avrBasePath;
configPreferences.get("compiler.S.flags"), args[1] = configPreferences.get("compiler.cpp.cmd");
configPreferences.get("compiler.cpudef"), args[2] = configPreferences.get("compiler.S.flags");
configPreferences.get("build.mcu"), args[3] = configPreferences.get("compiler.cpudef");
configPreferences.get("build.f_cpu"), args[4] = configPreferences.get("build.mcu");
configPreferences.get("software"), Base.REVISION, includes, sourceName, args[5] = configPreferences.get("build.f_cpu");
objectName }; args[6] = configPreferences.get("software");
args[7] = "" + Base.REVISION;
args[8] = preparePaths(includePaths);
args[9] = sourceName;
args[10] = objectName;
String command = compileFormat.format(Args); String command = compileFormat.format(args);
String[] commandArray = command.split("\\|"); String[] commandArray = command.split("\\|");
return commandArray; return commandArray;
} }
@ -553,21 +520,21 @@ public class Compiler implements MessageConsumer {
// getIncludes to String // getIncludes to String
String includes = preparePaths(includePaths); String includes = preparePaths(includePaths);
Object[] Args = { avrBasePath, // 0 String[] args = new String[12];
configPreferences.get("compiler.c.cmd"), // 1 args[0] = avrBasePath;
configPreferences.get("compiler.c.flags"), // 2 args[1] = configPreferences.get("compiler.c.cmd");
configPreferences.get("compiler.cpudef"), // 3 args[2] = configPreferences.get("compiler.c.flags");
configPreferences.get("build.mcu"), // 4 args[3] = configPreferences.get("compiler.cpudef");
configPreferences.get("build.f_cpu"), // 5 args[4] = configPreferences.get("build.mcu");
configPreferences.get("software"), // 6 args[5] = configPreferences.get("build.f_cpu");
Base.REVISION, // 7 args[6] = configPreferences.get("software");
includes, // 8 args[7] = "" + Base.REVISION;
sourceName, // 9 args[8] = includes;
objectName, // 10 args[9] = sourceName;
configPreferences.get("build.extra_flags") // 11 args[10] = objectName;
}; args[11] = configPreferences.get("build.extra_flags");
String command = compileFormat.format(Args); String command = compileFormat.format(args);
String[] commandArray = command.split("\\|"); String[] commandArray = command.split("\\|");
return commandArray; return commandArray;
} }
@ -607,21 +574,21 @@ public class Compiler implements MessageConsumer {
// getIncludes to String // getIncludes to String
String includes = preparePaths(includePaths); String includes = preparePaths(includePaths);
Object[] Args = { avrBasePath, // 0 String[] args = new String[12];
configPreferences.get("compiler.cpp.cmd"), // 1 args[0] = avrBasePath;
configPreferences.get("compiler.cpp.flags"), // 2 args[1] = configPreferences.get("compiler.cpp.cmd");
configPreferences.get("compiler.cpudef"), // 3 args[2] = configPreferences.get("compiler.cpp.flags");
configPreferences.get("build.mcu"), // 4 args[3] = configPreferences.get("compiler.cpudef");
configPreferences.get("build.f_cpu"), // 5 args[4] = configPreferences.get("build.mcu");
configPreferences.get("software"), // 6 args[5] = configPreferences.get("build.f_cpu");
Base.REVISION, // 7 args[6] = configPreferences.get("software");
includes, // 8 args[7] = "" + Base.REVISION;
sourceName, // 9 args[8] = includes;
objectName, // 10 args[9] = sourceName;
configPreferences.get("build.extra_flags") // 11 args[10] = objectName;
}; args[11] = configPreferences.get("build.extra_flags");
String command = compileFormat.format(Args); String command = compileFormat.format(args);
String[] commandArray = command.split("\\|"); String[] commandArray = command.split("\\|");
// System.out.println("command:" + command); // System.out.println("command:" + command);
@ -658,7 +625,7 @@ public class Compiler implements MessageConsumer {
static public ArrayList<File> findFilesInPath(String path, String extension, static public ArrayList<File> findFilesInPath(String path, String extension,
boolean recurse) { boolean recurse) {
System.out.println("findFilesInPath: " + path); // System.out.println("findFilesInPath: " + path);
return findFilesInFolder(new File(path), extension, recurse); return findFilesInFolder(new File(path), extension, recurse);
} }
@ -690,11 +657,10 @@ public class Compiler implements MessageConsumer {
List<String> includePaths, List<String> includePaths,
Map<String, String> configPreferences) Map<String, String> configPreferences)
throws RunnerException { throws RunnerException {
System.out.println("compileSketch: start"); // System.out.println("compileSketch: start");
System.out.println("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)); // System.out.println("-I" + (String) includePaths.get(i));
}
// logger.debug("compileSketch: start"); // logger.debug("compileSketch: start");
objectFiles.addAll(compileFiles(avrBasePath, buildPath, includePaths, objectFiles.addAll(compileFiles(avrBasePath, buildPath, includePaths,
@ -732,7 +698,7 @@ public class Compiler implements MessageConsumer {
false), false),
findFilesInFolder(libraryFolder, "cpp", findFilesInFolder(libraryFolder, "cpp",
false), false),
boardPreferences)); configPreferences));
outputFolder = new File(outputFolder, "utility"); outputFolder = new File(outputFolder, "utility");
createFolder(outputFolder); createFolder(outputFolder);
objectFiles.addAll(compileFiles(avrBasePath, outputFolder objectFiles.addAll(compileFiles(avrBasePath, outputFolder
@ -742,7 +708,7 @@ public class Compiler implements MessageConsumer {
false), false),
findFilesInFolder(utilityFolder, "cpp", findFilesInFolder(utilityFolder, "cpp",
false), false),
boardPreferences)); configPreferences));
// other libraries should not see this library's utility/ folder // other libraries should not see this library's utility/ folder
includePaths.remove(includePaths.size() - 1); includePaths.remove(includePaths.size() - 1);
} }
@ -762,7 +728,6 @@ public class Compiler implements MessageConsumer {
// for (int i = 0; i < includePaths.size(); i++) // for (int i = 0; i < includePaths.size(); i++)
// System.out.println("-I" + includePaths.get(i)); // System.out.println("-I" + includePaths.get(i));
String commandString = "";
// System.out.println("corePath: " + corePath); // System.out.println("corePath: " + corePath);
List<String> srcDirs = new ArrayList<String>(); List<String> srcDirs = new ArrayList<String>();
srcDirs.add(corePath); srcDirs.add(corePath);
@ -771,32 +736,29 @@ public class Compiler implements MessageConsumer {
srcDirs.addAll(variantExtraSrc); srcDirs.addAll(variantExtraSrc);
List<File> objects = new ArrayList<File>(); List<File> objects = new ArrayList<File>();
for (String dir : srcDirs)
for (String dir : srcDirs) {
objects.addAll(compileFiles(avrBasePath, buildPath, includePaths, objects.addAll(compileFiles(avrBasePath, buildPath, includePaths,
findFilesInPath(dir, "S", false), findFilesInPath(dir, "S", false),
findFilesInPath(dir, "c", false), findFilesInPath(dir, "c", false),
findFilesInPath(dir, "cpp", false), findFilesInPath(dir, "cpp", false),
configPreferences)); configPreferences));
}
for (File file : objects) { for (File file : objects) {
// List commandAR = new ArrayList(baseCommandAR); // List commandAR = new ArrayList(baseCommandAR);
// commandAR = commandAR + file.getAbsolutePath(); // commandAR = commandAR + file.getAbsolutePath();
Object[] Args = { avrBasePath, // 0 String[] args = new String[6];
configPreferences.get("compiler.ar.cmd"), // 1 args[0] = avrBasePath;
configPreferences.get("compiler.ar.flags"), // 2 args[1] = configPreferences.get("compiler.ar.cmd");
// corePath, args[2] = configPreferences.get("compiler.ar.flags");
buildPath + File.separator, "core.a", // 3 args[3] = buildPath + File.separator;
// objectName args[4] = "core.a";
file.getAbsolutePath() // 4 args[5] = file.getAbsolutePath();
};
// System.out.println("compileCore(...) substitute"); // System.out.println("compileCore(...) substitute");
String baseCommandString = configPreferences.get("recipe.ar.pattern"); String baseCommandString = configPreferences.get("recipe.ar.pattern");
MessageFormat compileFormat = new MessageFormat(baseCommandString); MessageFormat compileFormat = new MessageFormat(baseCommandString);
commandString = compileFormat.format(Args); String commandString = compileFormat.format(args);
String[] commandArray = commandString.split("\\|"); String[] commandArray = commandString.split("\\|");
execAsynchronously(commandArray); execAsynchronously(commandArray);
@ -808,32 +770,31 @@ public class Compiler implements MessageConsumer {
List<String> includePaths, String pinsPath, List<String> includePaths, String pinsPath,
Map<String, String> configPreferences) Map<String, String> configPreferences)
throws RunnerException { throws RunnerException {
System.out.println("compileLink: start"); // System.out.println("compileLink: start");
String baseCommandString = configPreferences String recipe = configPreferences.get("recipe.c.combine.pattern");
.get("recipe.c.combine.pattern"); MessageFormat compileFormat = new MessageFormat(recipe);
String commandString = "";
MessageFormat compileFormat = new MessageFormat(baseCommandString);
String objectFileList = "";
for (File file : objectFiles) { String objectFileList = "";
for (File file : objectFiles)
objectFileList = objectFileList + file.getAbsolutePath() + "|"; objectFileList = objectFileList + file.getAbsolutePath() + "|";
}
System.out.println("objectFileList: " + objectFileList); System.out.println("objectFileList: " + objectFileList);
Object[] Args = { avrBasePath, // 0 String args[] = new String[12];
configPreferences.get("compiler.c.elf.cmd"), // 1 args[0] = avrBasePath;
configPreferences.get("compiler.c.elf.flags"), // 2 args[1] = configPreferences.get("compiler.c.elf.cmd");
configPreferences.get("compiler.cpudef"), // 3 args[2] = configPreferences.get("compiler.c.elf.flags");
configPreferences.get("build.mcu"), // 4 args[3] = configPreferences.get("compiler.cpudef");
buildPath + File.separator, // 5 args[4] = configPreferences.get("build.mcu");
primaryClassName, // 6 args[5] = buildPath + File.separator;
objectFileList, // 7 args[6] = primaryClassName;
buildPath + File.separator + "core.a", // 8 args[7] = objectFileList;
buildPath, // 9 args[8] = buildPath + File.separator + "core.a";
corePath, // 10 args[9] = buildPath;
pinsPath + File.separator + configPreferences.get("build.ldscript") // 11 args[10] = corePath;
}; args[11] = pinsPath + File.separator
commandString = compileFormat.format(Args); + configPreferences.get("build.ldscript");
String commandString = compileFormat.format(args);
String[] commandArray = commandString.split("\\|"); String[] commandArray = commandString.split("\\|");
execAsynchronously(commandArray); execAsynchronously(commandArray);
} }
@ -843,18 +804,19 @@ public class Compiler implements MessageConsumer {
List<String> includePaths, List<String> includePaths,
Map<String, String> configPreferences) throws RunnerException { Map<String, String> configPreferences) throws RunnerException {
// logger.debug("compileEep: start"); // logger.debug("compileEep: start");
String baseCommandString = configPreferences String recipe = configPreferences.get("recipe.objcopy.eep.pattern");
.get("recipe.objcopy.eep.pattern"); if (recipe.trim().isEmpty())
String commandString = ""; return;
MessageFormat compileFormat = new MessageFormat(baseCommandString); MessageFormat compileFormat = new MessageFormat(recipe);
String objectFileList = "";
Object[] Args = { avrBasePath, String[] args = new String[5];
configPreferences.get("compiler.objcopy.cmd"), args[0] = avrBasePath;
configPreferences.get("compiler.objcopy.eep.flags"), args[1] = configPreferences.get("compiler.objcopy.cmd");
buildPath + File.separator + primaryClassName, args[2] = configPreferences.get("compiler.objcopy.eep.flags");
buildPath + File.separator + primaryClassName }; args[3] = buildPath + File.separator + primaryClassName;
commandString = compileFormat.format(Args); args[4] = buildPath + File.separator + primaryClassName;
String commandString = compileFormat.format(args);
String[] commandArray = commandString.split("\\|"); String[] commandArray = commandString.split("\\|");
execAsynchronously(commandArray); execAsynchronously(commandArray);
} }
@ -864,71 +826,21 @@ public class Compiler implements MessageConsumer {
List<String> includePaths, List<String> includePaths,
Map<String, String> configPreferences) throws RunnerException { Map<String, String> configPreferences) throws RunnerException {
// logger.debug("compileHex: start"); // logger.debug("compileHex: start");
String baseCommandString = configPreferences String recipe = configPreferences.get("recipe.objcopy.hex.pattern");
.get("recipe.objcopy.hex.pattern"); MessageFormat compileFormat = new MessageFormat(recipe);
String commandString = "";
MessageFormat compileFormat = new MessageFormat(baseCommandString);
String objectFileList = "";
Object[] Args = { avrBasePath, String[] args = new String[5];
configPreferences.get("compiler.elf2hex.cmd"), args[0] = avrBasePath;
configPreferences.get("compiler.elf2hex.flags"), args[1] = configPreferences.get("compiler.elf2hex.cmd");
buildPath + File.separator + primaryClassName, args[2] = configPreferences.get("compiler.elf2hex.flags");
buildPath + File.separator + primaryClassName }; args[3] = buildPath + File.separator + primaryClassName;
commandString = compileFormat.format(Args); args[4] = buildPath + File.separator + primaryClassName;
String commandString = compileFormat.format(args);
String[] commandArray = commandString.split("\\|"); String[] commandArray = commandString.split("\\|");
execAsynchronously(commandArray); 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;
}
// getIncludes to String // getIncludes to String
private static String preparePaths(List<String> includePaths) { private static String preparePaths(List<String> includePaths) {
String includes = ""; String includes = "";

View File

@ -1,67 +1,77 @@
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/* /*
Sizer - computes the size of a .hex file Sizer - computes the size of a .hex file
Part of the Arduino project - http://www.arduino.cc/ Part of the Arduino project - http://www.arduino.cc/
Copyright (c) 2006 David A. Mellis Copyright (c) 2006 David A. Mellis
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or the Free Software Foundation; either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation, along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id$ $Id$
*/ */
package processing.app.debug; package processing.app.debug;
import processing.app.Base; import java.io.File;
import java.text.MessageFormat;
import java.io.*; import java.util.Map;
import java.util.*; import java.util.NoSuchElementException;
import java.util.StringTokenizer;
public class Sizer implements MessageConsumer { public class Sizer implements MessageConsumer {
private String buildPath, sketchName; private String buildPath, sketchName;
private String firstLine; private String firstLine;
private long size; private long size;
private RunnerException exception; private RunnerException exception;
public Sizer(String buildPath, String sketchName) { private Map<String, String> prefs;
public Sizer(String buildPath, String sketchName,
Map<String, String> prefs) {
this.buildPath = buildPath; this.buildPath = buildPath;
this.sketchName = sketchName; this.sketchName = sketchName;
this.prefs = prefs;
} }
public long computeSize() throws RunnerException { public long computeSize() throws RunnerException {
String avrBasePath = Base.getAvrBasePath(); String args[] = new String[3];
String commandSize[] = new String[] { args[0] = prefs.get("compiler.path");
avrBasePath + "avr-size", args[1] = prefs.get("compiler.size.cmd");
" " args[2] = buildPath + File.separator + sketchName;
};
String recipe = prefs.get("recipe.size.pattern");
commandSize[1] = buildPath + File.separator + sketchName + ".hex"; MessageFormat compileFormat = new MessageFormat(recipe);
String command = compileFormat.format(args);
String[] commandArray = command.split("\\|");
int r = 0; int r = 0;
try { try {
exception = null; exception = null;
size = -1; size = -1;
firstLine = null; firstLine = null;
Process process = Runtime.getRuntime().exec(commandSize); Process process = Runtime.getRuntime().exec(commandArray);
MessageSiphon in = new MessageSiphon(process.getInputStream(), this); MessageSiphon in = new MessageSiphon(process.getInputStream(), this);
MessageSiphon err = new MessageSiphon(process.getErrorStream(), this); MessageSiphon err = new MessageSiphon(process.getErrorStream(), this);
boolean running = true; boolean running = true;
while(running) { while (running) {
try { try {
if (in.thread != null) if (in.thread != null)
in.thread.join(); in.thread.join();
@ -69,25 +79,28 @@ public class Sizer implements MessageConsumer {
err.thread.join(); err.thread.join();
r = process.waitFor(); r = process.waitFor();
running = false; running = false;
} catch (InterruptedException intExc) { } } catch (InterruptedException intExc) {
}
} }
} catch (Exception e) { } catch (Exception e) {
// The default Throwable.toString() never returns null, but apparently // The default Throwable.toString() never returns null, but apparently
// some sub-class has overridden it to do so, thus we need to check for // some sub-class has overridden it to do so, thus we need to check for
// it. See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1166589459 // it. See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1166589459
exception = new RunnerException( if (e.toString() == null)
(e.toString() == null) ? e.getClass().getName() + r : e.toString() + r); exception = new RunnerException(e.getClass().getName() + r);
else
exception = new RunnerException(e.toString() + r);
} }
if (exception != null) if (exception != null)
throw exception; throw exception;
if (size == -1) if (size == -1)
throw new RunnerException(firstLine); throw new RunnerException(firstLine);
return size; return size;
} }
public void message(String s) { public void message(String s) {
if (firstLine == null) if (firstLine == null)
firstLine = s; firstLine = s;

View File

@ -29,13 +29,17 @@ sam.recipe.c.combine.pattern={0}{1}|{2}|{3}{4}|-T{11}|-Wl,-Map,{5}{6}.map|-o|{5}
##create eeprom ##create eeprom
#sam.recipe.objcopy.eep.pattern={0=compiler.path}{1=compiler.objcopy.cmd}{2=compiler.objcopy.eep.flags} {3=BUILD_PATH}{4=SOURCE_NAME}.elf {5=BUILD_PATH}{6=SOURCE_NAME}.eep #sam.recipe.objcopy.eep.pattern={0=compiler.path}{1=compiler.objcopy.cmd}{2=compiler.objcopy.eep.flags} {3=BUILD_PATH}{4=SOURCE_NAME}.elf {5=BUILD_PATH}{6=SOURCE_NAME}.eep
sam.recipe.objcopy.eep.pattern={0}{1}|{2}|{3}.elf|{4}.eep #sam.recipe.objcopy.eep.pattern={0}{1}|{2}|{3}.elf|{4}.eep
sam.recipe.objcopy.eep.pattern=
##create hex ##create hex
#sam.recipe.objcopy.hex.pattern={0=compiler.path}{1=compiler.objcopy.cmd}{2=compiler.objcopy.elf.flags} {3=BUILD_PATH}{4=SOURCE_NAME}.elf {5=BUILD_PATH}{6=SOURCE_NAME}.hex #sam.recipe.objcopy.hex.pattern={0=compiler.path}{1=compiler.objcopy.cmd}{2=compiler.objcopy.elf.flags} {3=BUILD_PATH}{4=SOURCE_NAME}.elf {5=BUILD_PATH}{6=SOURCE_NAME}.hex
sam.recipe.objcopy.hex.pattern={0}{1}|{2}|{3}.elf|{4}.hex sam.recipe.objcopy.hex.pattern={0}{1}|{2}|{3}.elf|{4}.bin
##compute size
sam.recipe.size.pattern={0}{1}|{2}.elf
######################################################## ########################################################
sam.name=Atmel SAM sam.name=Atmel SAM
@ -56,6 +60,7 @@ sam.compiler.elf2hex.flags=|-O|binary
sam.compiler.elf2hex.cmd=arm-none-eabi-objcopy sam.compiler.elf2hex.cmd=arm-none-eabi-objcopy
sam.compiler.ldflags= sam.compiler.ldflags=
sam.compiler.cpudef=-mcpu= sam.compiler.cpudef=-mcpu=
sam.compiler.size.cmd=arm-none-eabi-size
sam.compiler.upload.cmd= sam.compiler.upload.cmd=
sam.compiler.upload.flags= sam.compiler.upload.flags=
sam.compiler.define=-DARDUINO= sam.compiler.define=-DARDUINO=