1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-16 11:21:18 +03:00

Compiler class refactoring: toolchain path, build path and preferences are now class members.

This commit is contained in:
Cristian Maglie
2011-12-31 15:07:59 +01:00
parent abe41d805d
commit 770c8dfe35

View File

@ -50,16 +50,18 @@ public class Compiler implements MessageConsumer {
static final String SUPER_BADNESS = static final String SUPER_BADNESS =
I18n.format(_("Compiler error, please submit this code to {0}"), BUGS_URL); I18n.format(_("Compiler error, please submit this code to {0}"), BUGS_URL);
Sketch sketch; private Sketch sketch;
String buildPath;
String primaryClassName;
String board;
boolean verbose; private String buildPath;
private String toolsPath;
private String primaryClassName;
private String board;
private List<File> objectFiles;
RunnerException exception; private PreferencesMap prefs;
private boolean verbose;
List<File> objectFiles; private RunnerException exception;
/** /**
* Compile sketch. * Compile sketch.
@ -90,7 +92,7 @@ public class Compiler implements MessageConsumer {
platformPreferences = Base.getPlatformPreferences(platform); platformPreferences = Base.getPlatformPreferences(platform);
// Merge all the global preference configuration in order of priority // Merge all the global preference configuration in order of priority
PreferencesMap prefs = new PreferencesMap(); prefs = new PreferencesMap();
prefs.putAll(Preferences.getMap()); prefs.putAll(Preferences.getMap());
prefs.putAll(platformPreferences); prefs.putAll(platformPreferences);
prefs.putAll(boardPreferences); prefs.putAll(boardPreferences);
@ -99,21 +101,21 @@ public class Compiler implements MessageConsumer {
prefs.put(k, ""); prefs.put(k, "");
} }
String avrBasePath = prefs.get("compiler.path"); toolsPath = prefs.get("compiler.path");
if (avrBasePath == null) { if (toolsPath == null) {
avrBasePath = Base.getAvrBasePath(); toolsPath = Base.getAvrBasePath();
System.out.println("avrBasePath: " + avrBasePath); System.out.println("avrBasePath: " + toolsPath);
} else { } else {
System.out.println("avrBasePath:exists: " + avrBasePath); System.out.println("avrBasePath:exists: " + toolsPath);
// Put in the system path in the compiler path if available // Put in the system path in the compiler path if available
MessageFormat compileFormat = new MessageFormat(avrBasePath); MessageFormat compileFormat = new MessageFormat(toolsPath);
String basePath = System.getProperty("user.dir"); String basePath = System.getProperty("user.dir");
if (Base.isMacOS()) if (Base.isMacOS())
basePath += "/Arduino.app/Contents/Resources/Java"; basePath += "/Arduino.app/Contents/Resources/Java";
Object[] Args = { basePath }; Object[] Args = { basePath };
avrBasePath = compileFormat.format(Args); toolsPath = compileFormat.format(Args);
System.out.println("avrBasePath:new: " + avrBasePath); System.out.println("avrBasePath:new: " + toolsPath);
} }
board = prefs.get("board"); board = prefs.get("board");
if (board == "") if (board == "")
@ -168,76 +170,74 @@ public class Compiler implements MessageConsumer {
// 1. compile the sketch (already in the buildPath) // 1. compile the sketch (already in the buildPath)
sketch.setCompilingProgress(30); sketch.setCompilingProgress(30);
compileSketch(avrBasePath, _buildPath, includePaths, prefs); compileSketch(includePaths);
// 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 // Doesn't really use configPreferences
sketch.setCompilingProgress(40); sketch.setCompilingProgress(40);
compileLibraries(avrBasePath, _buildPath, includePaths, prefs); compileLibraries(includePaths);
// 3. compile the core, outputting .o files to <buildPath> and then // 3. compile the core, outputting .o files to <buildPath> and then
// collecting them into the core.a library file. // collecting them into the core.a library file.
sketch.setCompilingProgress(50); sketch.setCompilingProgress(50);
compileCore(avrBasePath, _buildPath, corePath, variant, variantPath, prefs); compileCore(corePath, variant, variantPath);
// 4. link it all together into the .elf file // 4. link it all together into the .elf file
sketch.setCompilingProgress(60); sketch.setCompilingProgress(60);
compileLink(avrBasePath, _buildPath, corePath, includePaths, prefs); compileLink(corePath, includePaths);
// 5. extract EEPROM data (from EEMEM directive) to .eep file. // 5. extract EEPROM data (from EEMEM directive) to .eep file.
sketch.setCompilingProgress(70); sketch.setCompilingProgress(70);
compileEep(avrBasePath, _buildPath, includePaths, prefs); compileEep(includePaths);
// 6. build the .hex file // 6. build the .hex file
sketch.setCompilingProgress(80); sketch.setCompilingProgress(80);
compileHex(avrBasePath, _buildPath, includePaths, prefs); compileHex(includePaths);
sketch.setCompilingProgress(90); sketch.setCompilingProgress(90);
return true; return true;
} }
private List<File> compileFiles(String avrBasePath, String buildPath, private List<File> compileFiles(String outputPath, File sourcePath,
File sourcePath, boolean recurse, boolean recurse, List<String> includePaths)
List<String> includePaths, throws RunnerException {
PreferencesMap prefs) throws RunnerException {
List<File> sSources = findFilesInFolder(sourcePath, "S", recurse); List<File> sSources = findFilesInFolder(sourcePath, "S", recurse);
List<File> cSources = findFilesInFolder(sourcePath, "c", recurse); List<File> cSources = findFilesInFolder(sourcePath, "c", recurse);
List<File> cppSources = findFilesInFolder(sourcePath, "cpp", recurse); List<File> cppSources = findFilesInFolder(sourcePath, "cpp", recurse);
List<File> objectPaths = new ArrayList<File>(); List<File> objectPaths = new ArrayList<File>();
for (File file : sSources) { for (File file : sSources) {
String objectPath = buildPath + File.separator + file.getName() + ".o"; String objectPath = outputPath + File.separator + file.getName() + ".o";
objectPaths.add(new File(objectPath)); objectPaths.add(new File(objectPath));
execAsynchronously(getCommandCompilerS(avrBasePath, includePaths, String[] cmd = getCommandCompilerS(includePaths, file.getAbsolutePath(),
file.getAbsolutePath(), objectPath);
objectPath, execAsynchronously(cmd);
prefs));
} }
for (File file : cSources) { for (File file : cSources) {
String objectPath = buildPath + File.separator + file.getName() + ".o"; String objectPath = outputPath + File.separator + file.getName() + ".o";
String dependPath = buildPath + File.separator + file.getName() + ".d"; String dependPath = outputPath + File.separator + file.getName() + ".d";
File objectFile = new File(objectPath); File objectFile = new File(objectPath);
File dependFile = new File(dependPath); File dependFile = new File(dependPath);
objectPaths.add(objectFile); objectPaths.add(objectFile);
if (is_already_compiled(file, objectFile, dependFile, prefs)) continue; if (is_already_compiled(file, objectFile, dependFile, prefs))
execAsynchronously(getCommandCompilerC(avrBasePath, includePaths, continue;
file.getAbsolutePath(), String[] cmd = getCommandCompilerC(includePaths, file.getAbsolutePath(),
objectPath, objectPath);
prefs)); execAsynchronously(cmd);
} }
for (File file : cppSources) { for (File file : cppSources) {
String objectPath = buildPath + File.separator + file.getName() + ".o"; String objectPath = outputPath + File.separator + file.getName() + ".o";
String dependPath = buildPath + File.separator + file.getName() + ".d"; String dependPath = outputPath + File.separator + file.getName() + ".d";
File objectFile = new File(objectPath); File objectFile = new File(objectPath);
File dependFile = new File(dependPath); File dependFile = new File(dependPath);
objectPaths.add(objectFile); objectPaths.add(objectFile);
if (is_already_compiled(file, objectFile, dependFile, prefs)) continue; if (is_already_compiled(file, objectFile, dependFile, prefs))
execAsynchronously(getCommandCompilerCPP(avrBasePath, includePaths, continue;
file.getAbsolutePath(), String[] cmd = getCommandCompilerCPP(includePaths,
objectPath, file.getAbsolutePath(), objectPath);
prefs)); execAsynchronously(cmd);
} }
return objectPaths; return objectPaths;
@ -368,23 +368,22 @@ public class Compiler implements MessageConsumer {
// to digest it, and the fact that they have five stomaches. // to digest it, and the fact that they have five stomaches.
// //
//System.out.println("throwing up " + exception); //System.out.println("throwing up " + exception);
if (exception != null) { throw exception; } if (exception != null)
throw exception;
if (result > 1) { if (result > 1) {
// a failure in the tool (e.g. unable to locate a sub-executable) // a failure in the tool (e.g. unable to locate a sub-executable)
System.err System.err
.println(I18n.format(_("{0} returned {1}"), command[0], result)); .println(I18n.format(_("{0} returned {1}"), command[0], result));
} }
if (result != 0) { if (result != 0) {
RunnerException re = new RunnerException(_("Error compiling.")); RunnerException re = new RunnerException(_("Error compiling."));
re.hideStackTrace(); re.hideStackTrace();
throw re; throw re;
} }
System.out.println("execAsync: Done.");
} }
/** /**
* Part of the MessageConsumer interface, this is called * Part of the MessageConsumer interface, this is called
* whenever a piece (usually a line) of error message is spewed * whenever a piece (usually a line) of error message is spewed
@ -477,10 +476,8 @@ public class Compiler implements MessageConsumer {
System.err.print(s); System.err.print(s);
} }
private String[] getCommandCompilerS(String avrBasePath, private String[] getCommandCompilerS(List<String> includePaths,
List<String> includePaths, String sourceName, String objectName)
String sourceName, String objectName,
PreferencesMap prefs)
throws RunnerException { throws RunnerException {
String includes = preparePaths(includePaths); String includes = preparePaths(includePaths);
PreferencesMap dict = new PreferencesMap(prefs); PreferencesMap dict = new PreferencesMap(prefs);
@ -488,7 +485,7 @@ public class Compiler implements MessageConsumer {
dict.put("includes", includes); dict.put("includes", includes);
dict.put("source_file", sourceName); dict.put("source_file", sourceName);
dict.put("object_file", objectName); dict.put("object_file", objectName);
dict.put("toolchain_path", avrBasePath); dict.put("toolchain_path", toolsPath);
try { try {
String cmd = prefs.get("recipe.S.o.pattern"); String cmd = prefs.get("recipe.S.o.pattern");
@ -498,10 +495,8 @@ public class Compiler implements MessageConsumer {
} }
} }
private String[] getCommandCompilerC(String avrBasePath, private String[] getCommandCompilerC(List<String> includePaths,
List<String> includePaths, String sourceName, String objectName)
String sourceName, String objectName,
PreferencesMap prefs)
throws RunnerException { throws RunnerException {
String includes = preparePaths(includePaths); String includes = preparePaths(includePaths);
@ -510,7 +505,7 @@ public class Compiler implements MessageConsumer {
dict.put("includes", includes); dict.put("includes", includes);
dict.put("source_file", sourceName); dict.put("source_file", sourceName);
dict.put("object_file", objectName); dict.put("object_file", objectName);
dict.put("toolchain_path", avrBasePath); dict.put("toolchain_path", toolsPath);
String cmd = prefs.get("recipe.c.o.pattern"); String cmd = prefs.get("recipe.c.o.pattern");
try { try {
@ -520,10 +515,8 @@ public class Compiler implements MessageConsumer {
} }
} }
private String[] getCommandCompilerCPP(String avrBasePath, private String[] getCommandCompilerCPP(List<String> includePaths,
List<String> includePaths, String sourceName, String objectName)
String sourceName, String objectName,
PreferencesMap prefs)
throws RunnerException { throws RunnerException {
String includes = preparePaths(includePaths); String includes = preparePaths(includePaths);
@ -532,7 +525,7 @@ public class Compiler implements MessageConsumer {
dict.put("includes", includes); dict.put("includes", includes);
dict.put("source_file", sourceName); dict.put("source_file", sourceName);
dict.put("object_file", objectName); dict.put("object_file", objectName);
dict.put("toolchain_path", avrBasePath); dict.put("toolchain_path", toolsPath);
String cmd = prefs.get("recipe.cpp.o.pattern"); String cmd = prefs.get("recipe.cpp.o.pattern");
try { try {
@ -589,21 +582,14 @@ public class Compiler implements MessageConsumer {
} }
// 1. compile the sketch (already in the buildPath) // 1. compile the sketch (already in the buildPath)
void compileSketch(String avrBasePath, String buildPath, void compileSketch(List<String> includePaths) throws RunnerException {
List<String> includePaths, PreferencesMap prefs) objectFiles.addAll(compileFiles(buildPath, new File(buildPath), false,
throws RunnerException { includePaths));
objectFiles.addAll(compileFiles(avrBasePath, buildPath,
new File(buildPath), false, includePaths,
prefs));
} }
// 2. compile the libraries, outputting .o files to: // 2. compile the libraries, outputting .o files to:
// <buildPath>/<library>/ // <buildPath>/<library>/
void compileLibraries(String avrBasePath, String buildPath, void compileLibraries(List<String> includePaths) throws RunnerException {
List<String> includePaths,
PreferencesMap configPreferences)
throws RunnerException {
System.out.println("compileLibraries: start");
for (File libraryFolder : sketch.getImportedLibraries()) { for (File libraryFolder : sketch.getImportedLibraries()) {
File outputFolder = new File(buildPath, libraryFolder.getName()); File outputFolder = new File(buildPath, libraryFolder.getName());
@ -612,14 +598,12 @@ public class Compiler implements MessageConsumer {
// this library can use includes in its utility/ folder // this library can use includes in its utility/ folder
includePaths.add(utilityFolder.getAbsolutePath()); includePaths.add(utilityFolder.getAbsolutePath());
objectFiles.addAll(compileFiles(avrBasePath, outputFolder objectFiles.addAll(compileFiles(outputFolder.getAbsolutePath(),
.getAbsolutePath(), libraryFolder, false, includePaths, libraryFolder, false, includePaths));
configPreferences));
outputFolder = new File(outputFolder, "utility"); outputFolder = new File(outputFolder, "utility");
createFolder(outputFolder); createFolder(outputFolder);
objectFiles.addAll(compileFiles(avrBasePath, outputFolder objectFiles.addAll(compileFiles(outputFolder.getAbsolutePath(),
.getAbsolutePath(), utilityFolder, false, includePaths, utilityFolder, false, includePaths));
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);
} }
@ -627,8 +611,7 @@ public class Compiler implements MessageConsumer {
// 3. compile the core, outputting .o files to <buildPath> and then // 3. compile the core, outputting .o files to <buildPath> and then
// collecting them into the core.a library file. // collecting them into the core.a library file.
void compileCore(String avrBasePath, String buildPath, String corePath, void compileCore(String corePath, String variant, String variantPath)
String variant, String variantPath, PreferencesMap prefs)
throws RunnerException { throws RunnerException {
List<String> includePaths = new ArrayList<String>(); List<String> includePaths = new ArrayList<String>();
@ -636,13 +619,13 @@ public class Compiler implements MessageConsumer {
if (variantPath != null) if (variantPath != null)
includePaths.add(variantPath); includePaths.add(variantPath);
List<File> coreObjectFiles = compileFiles(avrBasePath, buildPath, new File( List<File> coreObjectFiles = compileFiles(buildPath, new File(corePath),
corePath), true, includePaths, prefs); true, includePaths);
for (File file : coreObjectFiles) { for (File file : coreObjectFiles) {
PreferencesMap dict = new PreferencesMap(prefs); PreferencesMap dict = new PreferencesMap(prefs);
dict.put("toolchain_path", avrBasePath); dict.put("toolchain_path", toolsPath);
dict.put("ide_version", "" + Base.REVISION); dict.put("ide_version", "" + Base.REVISION);
dict.put("build_path", buildPath + File.separator); dict.put("build_path", buildPath + File.separator);
dict.put("archive_file", "core.a"); dict.put("archive_file", "core.a");
@ -660,8 +643,7 @@ public class Compiler implements MessageConsumer {
} }
// 4. link it all together into the .elf file // 4. link it all together into the .elf file
void compileLink(String avrBasePath, String buildPath, String corePath, void compileLink(String corePath, List<String> includePaths)
List<String> includePaths, PreferencesMap prefs)
throws RunnerException { throws RunnerException {
// TODO: Make the --relax thing in configuration files. // TODO: Make the --relax thing in configuration files.
@ -680,7 +662,7 @@ public class Compiler implements MessageConsumer {
PreferencesMap dict = new PreferencesMap(prefs); PreferencesMap dict = new PreferencesMap(prefs);
dict.put("compiler.c.elf.flags", dict dict.put("compiler.c.elf.flags", dict
.get("compiler.c.elf.flags" + optRelax)); .get("compiler.c.elf.flags" + optRelax));
dict.put("toolchain_path", avrBasePath); dict.put("toolchain_path", toolsPath);
dict.put("build_path", buildPath + File.separator); dict.put("build_path", buildPath + File.separator);
dict.put("archive_file", "core.a"); dict.put("archive_file", "core.a");
dict.put("project_name", primaryClassName); dict.put("project_name", primaryClassName);
@ -699,11 +681,9 @@ public class Compiler implements MessageConsumer {
} }
// 5. extract EEPROM data (from EEMEM directive) to .eep file. // 5. extract EEPROM data (from EEMEM directive) to .eep file.
void compileEep(String avrBasePath, String buildPath, void compileEep(List<String> includePaths) throws RunnerException {
List<String> includePaths, PreferencesMap prefs)
throws RunnerException {
PreferencesMap dict = new PreferencesMap(prefs); PreferencesMap dict = new PreferencesMap(prefs);
dict.put("toolchain_path", avrBasePath); dict.put("toolchain_path", toolsPath);
dict.put("build_path", buildPath + File.separator); dict.put("build_path", buildPath + File.separator);
dict.put("project_name", primaryClassName); dict.put("project_name", primaryClassName);
dict.put("ide_version", "" + Base.REVISION); dict.put("ide_version", "" + Base.REVISION);
@ -719,11 +699,9 @@ public class Compiler implements MessageConsumer {
} }
// 6. build the .hex file // 6. build the .hex file
void compileHex(String avrBasePath, String buildPath, void compileHex(List<String> includePaths) throws RunnerException {
List<String> includePaths, PreferencesMap prefs)
throws RunnerException {
PreferencesMap dict = new PreferencesMap(prefs); PreferencesMap dict = new PreferencesMap(prefs);
dict.put("toolchain_path", avrBasePath); dict.put("toolchain_path", toolsPath);
dict.put("build_path", buildPath + File.separator); dict.put("build_path", buildPath + File.separator);
dict.put("project_name", primaryClassName); dict.put("project_name", primaryClassName);
dict.put("ide_version", "" + Base.REVISION); dict.put("ide_version", "" + Base.REVISION);