Skip to content

Commit 411cc3b

Browse files
Simplify Compiler.compileFiles
Instead of doing three passes over the filesystem to collect source files, it now does a single pass (using the newly introduced FileUtils.listFiles) and later splits between .S, .c and .cpp files. This allows sharing more code between the three file types and allows removing Compiler.findFilesInFolder. Additionally, this splits compileFiles into a version that compiles all files in a folder and one that only compiles selected files. This prepares for later refactoring of the Library class. This has the side-effect of calling isAlreadyCompiled for .S files as well (which didn't happen previously). However, since the assembler command does not produce any .d files, .S files are still always recompiled, just like before. Finally, this has the side effect of handling file extensions in a case insensitive manner during compilation (instead of just during load), which means that e.g. .CPP files are not just loaded, but also compiled.
1 parent beb3981 commit 411cc3b

File tree

1 file changed

+24
-52
lines changed

1 file changed

+24
-52
lines changed

app/src/processing/app/debug/Compiler.java

+24-52
Original file line numberDiff line numberDiff line change
@@ -240,35 +240,35 @@ private PreferencesMap createBuildPreferences(String _buildPath,
240240
private List<File> compileFiles(File outputPath, File sourcePath,
241241
boolean recurse, List<File> includeFolders)
242242
throws RunnerException {
243-
List<File> sSources = findFilesInFolder(sourcePath, "S", recurse);
244-
List<File> cSources = findFilesInFolder(sourcePath, "c", recurse);
245-
List<File> cppSources = findFilesInFolder(sourcePath, "cpp", recurse);
246-
List<File> objectPaths = new ArrayList<File>();
243+
List<File> sourceFiles = FileUtils.listFiles(sourcePath, recurse, "s", "c", "cpp");
244+
return compileFiles(outputPath, sourcePath, sourceFiles, includeFolders);
247245

248-
for (File file : sSources) {
249-
File objectFile = new File(outputPath, file.getName() + ".o");
250-
objectPaths.add(objectFile);
251-
String[] cmd = getCommandCompilerS(includeFolders, file, objectFile);
252-
execAsynchronously(cmd);
253-
}
254-
255-
for (File file : cSources) {
256-
File objectFile = new File(outputPath, file.getName() + ".o");
257-
File dependFile = new File(outputPath, file.getName() + ".d");
258-
objectPaths.add(objectFile);
259-
if (isAlreadyCompiled(file, objectFile, dependFile, prefs))
260-
continue;
261-
String[] cmd = getCommandCompilerC(includeFolders, file, objectFile);
262-
execAsynchronously(cmd);
263-
}
246+
}
264247

265-
for (File file : cppSources) {
248+
private List<File> compileFiles(File outputPath, File sourcePath,
249+
List<File> sourceFiles, List<File> includeFolders)
250+
throws RunnerException {
251+
List<File> objectPaths = new ArrayList<File>();
252+
for (File file : sourceFiles) {
266253
File objectFile = new File(outputPath, file.getName() + ".o");
267254
File dependFile = new File(outputPath, file.getName() + ".d");
268255
objectPaths.add(objectFile);
256+
269257
if (isAlreadyCompiled(file, objectFile, dependFile, prefs))
270258
continue;
271-
String[] cmd = getCommandCompilerCPP(includeFolders, file, objectFile);
259+
260+
String[] cmd;
261+
if (FileUtils.hasExtension(file, "s")) {
262+
cmd = getCommandCompilerS(includeFolders, file, objectFile);
263+
} else if (FileUtils.hasExtension(file, "c")) {
264+
cmd = getCommandCompilerC(includeFolders, file, objectFile);
265+
} else if (FileUtils.hasExtension(file, "cpp")) {
266+
cmd = getCommandCompilerCPP(includeFolders, file, objectFile);
267+
} else {
268+
// This should not be possible...
269+
throw new RunnerException("Unknown source file extension: " + file.getName());
270+
}
271+
272272
execAsynchronously(cmd);
273273
}
274274

@@ -603,35 +603,6 @@ private void createFolder(File folder) throws RunnerException {
603603
throw new RunnerException("Couldn't create: " + folder);
604604
}
605605

606-
static public List<File> findFilesInFolder(File folder, String extension,
607-
boolean recurse) {
608-
List<File> files = new ArrayList<File>();
609-
610-
if (FileUtils.isSCCSOrHiddenFile(folder)) {
611-
return files;
612-
}
613-
614-
File[] listFiles = folder.listFiles();
615-
if (listFiles == null) {
616-
return files;
617-
}
618-
619-
for (File file : listFiles) {
620-
if (FileUtils.isSCCSOrHiddenFile(file)) {
621-
continue; // skip hidden files
622-
}
623-
624-
if (file.getName().endsWith("." + extension))
625-
files.add(file);
626-
627-
if (recurse && file.isDirectory()) {
628-
files.addAll(findFilesInFolder(file, extension, true));
629-
}
630-
}
631-
632-
return files;
633-
}
634-
635606
// 1. compile the sketch (already in the buildPath)
636607
void compileSketch(List<File> includeFolders) throws RunnerException {
637608
File buildPath = prefs.getFile("build.path");
@@ -666,7 +637,8 @@ private void compileLibrary(Library lib, List<File> includeFolders)
666637

667638
includeFolders.add(utilityFolder);
668639
compileFilesInFolder(libBuildFolder, libFolder, includeFolders);
669-
compileFilesInFolder(utilityBuildFolder, utilityFolder, includeFolders);
640+
if (utilityFolder.isDirectory())
641+
compileFilesInFolder(utilityBuildFolder, utilityFolder, includeFolders);
670642

671643
// other libraries should not see this library's utility/ folder
672644
includeFolders.remove(utilityFolder);

0 commit comments

Comments
 (0)