Skip to content

Commit

Permalink
Moved .a compiling to a function
Browse files Browse the repository at this point in the history
Less code duplication
  • Loading branch information
NicoHood committed Aug 23, 2015
1 parent 3bb7697 commit 6cfd871
Showing 1 changed file with 41 additions and 87 deletions.
128 changes: 41 additions & 87 deletions arduino-core/src/processing/app/debug/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1074,65 +1074,12 @@ private void compileLibrary(UserLibrary lib, List<File> includeFolders)
// Compile the library with .a linkage if a flag was set in library.properties
if(lib.alinkage()){

File afile = new File(libBuildFolder, lib.getName() + ".a");

createFolder(libBuildFolder);
List<File> libraryObjectFiles = compileFiles(libBuildFolder, libFolder, true, includeFolders);

// See if the .a file is already uptodate
if (afile.exists()) {
boolean changed = false;
for (File file : libraryObjectFiles) {
if (file.lastModified() > afile.lastModified()) {
changed = true;
break;
}
}

// If none of the object files is newer than the .a file, don't
// bother rebuilding the .a file. There is a small corner case
// here: If a source file was removed, but no other source file
// was modified, this will not rebuild core.a even when it
// should. It's hard to fix and not a realistic case, so it
// shouldn't be a problem.
if (!changed) {
if (verbose)
System.out.println(I18n.format(tr("Using previously compiled file: {0}"), afile.getPath()));

// this is no longer an object file, but will be added anyways.
objectFiles.add(afile);
return;
}
}

// Delete the .a file, to prevent any previous code from lingering
afile.delete();

try {
for (File file : libraryObjectFiles) {
PreferencesMap dict = new PreferencesMap(prefs);
dict.put("ide_version", "" + BaseNoGui.REVISION);
dict.put("archive_file", afile.getName());
dict.put("object_file", file.getAbsolutePath());
dict.put("build.path", libBuildFolder.getAbsolutePath());

String[] cmdArray;
String cmd = prefs.getOrExcept("recipe.ar.pattern");
try {
cmdArray = StringReplacer.formatAndSplit(cmd, dict, true);
} catch (Exception e) {
throw new RunnerException(e);
}
execAsynchronously(cmdArray);
}
createFolder(libBuildFolder);
File afile = compileThroughAFile(libBuildFolder, libFolder, lib.getName(), includeFolders);

} catch (RunnerException e) {
afile.delete();
throw e;
}

// this is no longer an object file, but will be added anyways.
objectFiles.add(afile);
// This is not a .o object file, but a .a file with all .o files inside.
// This way libraries can be optimized better, similar as the core files.
objectFiles.add(afile);
}

// no alinkage, old, default .o file linkage
Expand Down Expand Up @@ -1171,39 +1118,17 @@ private void compileFilesInFolder(File buildFolder, File srcFolder, List<File> i
objectFiles.addAll(objects);
}

// 3. compile the core, outputting .o files to <buildPath> and then
// collecting them into the core.a library file.
// Also compiles the variant (if it supplies actual source files),
// which are included in the link directly (not through core.a)
void compileCore()
throws RunnerException, PreferencesMapException {
private File compileThroughAFile(File buildFolder, File srcFolder, String name, List<File> includeFolders)
throws RunnerException, PreferencesMapException {
File afile = new File(buildFolder, name + ".a");

File coreFolder = prefs.getFile("build.core.path");
File variantFolder = prefs.getFile("build.variant.path");
File buildFolder = new File(prefs.getFile("build.path"), "core");
if (!buildFolder.exists() && !buildFolder.mkdirs()) {
throw new RunnerException("Unable to create folder " + buildFolder);
}

List<File> includeFolders = new ArrayList<File>();
includeFolders.add(coreFolder); // include core path only
if (variantFolder != null)
includeFolders.add(variantFolder);


if (variantFolder != null)
objectFiles.addAll(compileFiles(buildFolder, variantFolder, true,
includeFolders));

File afile = new File(buildFolder, "core.a");

List<File> coreObjectFiles = compileFiles(buildFolder, coreFolder, true,
List<File> aObjectFiles = compileFiles(buildFolder, coreFolder, true,
includeFolders);

// See if the .a file is already uptodate
if (afile.exists()) {
boolean changed = false;
for (File file : coreObjectFiles) {
for (File file : aObjectFiles) {
if (file.lastModified() > afile.lastModified()) {
changed = true;
break;
Expand All @@ -1219,15 +1144,15 @@ void compileCore()
if (!changed) {
if (verbose)
System.out.println(I18n.format(tr("Using previously compiled file: {0}"), afile.getPath()));
return;
return afile;
}
}

// Delete the .a file, to prevent any previous code from lingering
afile.delete();

try {
for (File file : coreObjectFiles) {
for (File file : aObjectFiles) {

PreferencesMap dict = new PreferencesMap(prefs);
dict.put("ide_version", "" + BaseNoGui.REVISION);
Expand All @@ -1248,6 +1173,35 @@ void compileCore()
afile.delete();
throw e;
}

return afile;
}

// 3. compile the core, outputting .o files to <buildPath> and then
// collecting them into the core.a library file.
// Also compiles the variant (if it supplies actual source files),
// which are included in the link directly (not through core.a)
void compileCore()
throws RunnerException, PreferencesMapException {

File coreFolder = prefs.getFile("build.core.path");
File variantFolder = prefs.getFile("build.variant.path");
File buildFolder = new File(prefs.getFile("build.path"), "core");
if (!buildFolder.exists() && !buildFolder.mkdirs()) {
throw new RunnerException("Unable to create folder " + buildFolder);
}

List<File> includeFolders = new ArrayList<File>();
includeFolders.add(coreFolder); // include core path only
if (variantFolder != null)
includeFolders.add(variantFolder);


if (variantFolder != null)
objectFiles.addAll(compileFiles(buildFolder, variantFolder, true,
includeFolders));

compileThroughAFile(buildFolder, coreFolder, "core", includeFolders);
}

// 4. link it all together into the .elf file
Expand Down

0 comments on commit 6cfd871

Please sign in to comment.