Skip to content

Commit 5e76635

Browse files
committed
Use Files instead of String to handle paths in Compiler class.
1 parent e045cd2 commit 5e76635

File tree

2 files changed

+105
-80
lines changed

2 files changed

+105
-80
lines changed

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

Lines changed: 72 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,29 @@ public boolean compile(boolean _verbose) throws RunnerException {
8888

8989
// 0. include paths for core + all libraries
9090
sketch.setCompilingProgress(20);
91-
List<String> includePaths = new ArrayList<String>();
92-
includePaths.add(prefs.get("build.core.path"));
93-
if (prefs.get("build.variant.path").length() != 0)
94-
includePaths.add(prefs.get("build.variant.path"));
91+
List<File> includeFolders = new ArrayList<File>();
92+
includeFolders.add(prefs.getFile("build.core.path"));
93+
if (prefs.getFile("build.variant.path") != null)
94+
includeFolders.add(prefs.getFile("build.variant.path"));
9595
for (Library lib : sketch.getImportedLibraries()) {
9696
if (verbose)
9797
System.out.println(I18n
9898
.format(_("Using library {0} in folder: {1} {2}"), lib.getName(),
9999
lib.getFolder(), lib.isLegacy() ? "(legacy)" : ""));
100-
includePaths.add(lib.getSrcFolder().getPath());
100+
includeFolders.add(lib.getSrcFolder());
101101
}
102102
if (verbose)
103103
System.out.println();
104104

105105
// 1. compile the sketch (already in the buildPath)
106106
sketch.setCompilingProgress(30);
107-
compileSketch(includePaths);
107+
compileSketch(includeFolders);
108108
sketchIsCompiled = true;
109109

110110
// 2. compile the libraries, outputting .o files to: <buildPath>/<library>/
111111
// Doesn't really use configPreferences
112112
sketch.setCompilingProgress(40);
113-
compileLibraries(includePaths);
113+
compileLibraries(includeFolders);
114114

115115
// 3. compile the core, outputting .o files to <buildPath> and then
116116
// collecting them into the core.a library file.
@@ -119,15 +119,15 @@ public boolean compile(boolean _verbose) throws RunnerException {
119119

120120
// 4. link it all together into the .elf file
121121
sketch.setCompilingProgress(60);
122-
compileLink(includePaths);
122+
compileLink();
123123

124124
// 5. extract EEPROM data (from EEMEM directive) to .eep file.
125125
sketch.setCompilingProgress(70);
126-
compileEep(includePaths);
126+
compileEep();
127127

128128
// 6. build the .hex file
129129
sketch.setCompilingProgress(80);
130-
compileHex(includePaths);
130+
compileHex();
131131

132132
sketch.setCompilingProgress(90);
133133
return true;
@@ -217,45 +217,38 @@ private PreferencesMap createBuildPreferences(String _buildPath,
217217
return p;
218218
}
219219

220-
private List<File> compileFiles(String outputPath, File sourcePath,
221-
boolean recurse, List<String> includePaths)
220+
private List<File> compileFiles(File outputPath, File sourcePath,
221+
boolean recurse, List<File> includeFolders)
222222
throws RunnerException {
223223
List<File> sSources = findFilesInFolder(sourcePath, "S", recurse);
224224
List<File> cSources = findFilesInFolder(sourcePath, "c", recurse);
225225
List<File> cppSources = findFilesInFolder(sourcePath, "cpp", recurse);
226226
List<File> objectPaths = new ArrayList<File>();
227227

228228
for (File file : sSources) {
229-
String objectPath = outputPath + File.separator + file.getName() + ".o";
230-
objectPaths.add(new File(objectPath));
231-
String[] cmd = getCommandCompilerS(includePaths, file.getAbsolutePath(),
232-
objectPath);
229+
File objectFile = new File(outputPath, file.getName() + ".o");
230+
objectPaths.add(objectFile);
231+
String[] cmd = getCommandCompilerS(includeFolders, file, objectFile);
233232
execAsynchronously(cmd);
234233
}
235234

236235
for (File file : cSources) {
237-
String objectPath = outputPath + File.separator + file.getName() + ".o";
238-
String dependPath = outputPath + File.separator + file.getName() + ".d";
239-
File objectFile = new File(objectPath);
240-
File dependFile = new File(dependPath);
236+
File objectFile = new File(outputPath, file.getName() + ".o");
237+
File dependFile = new File(outputPath, file.getName() + ".d");
241238
objectPaths.add(objectFile);
242239
if (isAlreadyCompiled(file, objectFile, dependFile, prefs))
243240
continue;
244-
String[] cmd = getCommandCompilerC(includePaths, file.getAbsolutePath(),
245-
objectPath);
241+
String[] cmd = getCommandCompilerC(includeFolders, file, objectFile);
246242
execAsynchronously(cmd);
247243
}
248244

249245
for (File file : cppSources) {
250-
String objectPath = outputPath + File.separator + file.getName() + ".o";
251-
String dependPath = outputPath + File.separator + file.getName() + ".d";
252-
File objectFile = new File(objectPath);
253-
File dependFile = new File(dependPath);
246+
File objectFile = new File(outputPath, file.getName() + ".o");
247+
File dependFile = new File(outputPath, file.getName() + ".d");
254248
objectPaths.add(objectFile);
255249
if (isAlreadyCompiled(file, objectFile, dependFile, prefs))
256250
continue;
257-
String[] cmd = getCommandCompilerCPP(includePaths,
258-
file.getAbsolutePath(), objectPath);
251+
String[] cmd = getCommandCompilerCPP(includeFolders, file, objectFile);
259252
execAsynchronously(cmd);
260253
}
261254

@@ -510,15 +503,15 @@ public void message(String s) {
510503
System.err.print(s);
511504
}
512505

513-
private String[] getCommandCompilerS(List<String> includePaths,
514-
String sourceName, String objectName)
506+
private String[] getCommandCompilerS(List<File> includeFolders,
507+
File sourceFile, File objectFile)
515508
throws RunnerException {
516-
String includes = preparePaths(includePaths);
509+
String includes = prepareIncludes(includeFolders);
517510
PreferencesMap dict = new PreferencesMap(prefs);
518511
dict.put("ide_version", "" + Base.REVISION);
519512
dict.put("includes", includes);
520-
dict.put("source_file", sourceName);
521-
dict.put("object_file", objectName);
513+
dict.put("source_file", sourceFile.getAbsolutePath());
514+
dict.put("object_file", objectFile.getAbsolutePath());
522515

523516
try {
524517
String cmd = prefs.get("recipe.S.o.pattern");
@@ -528,16 +521,16 @@ private String[] getCommandCompilerS(List<String> includePaths,
528521
}
529522
}
530523

531-
private String[] getCommandCompilerC(List<String> includePaths,
532-
String sourceName, String objectName)
524+
private String[] getCommandCompilerC(List<File> includeFolders,
525+
File sourceFile, File objectFile)
533526
throws RunnerException {
534-
String includes = preparePaths(includePaths);
527+
String includes = prepareIncludes(includeFolders);
535528

536529
PreferencesMap dict = new PreferencesMap(prefs);
537530
dict.put("ide_version", "" + Base.REVISION);
538531
dict.put("includes", includes);
539-
dict.put("source_file", sourceName);
540-
dict.put("object_file", objectName);
532+
dict.put("source_file", sourceFile.getAbsolutePath());
533+
dict.put("object_file", objectFile.getAbsolutePath());
541534

542535
String cmd = prefs.get("recipe.c.o.pattern");
543536
try {
@@ -547,16 +540,16 @@ private String[] getCommandCompilerC(List<String> includePaths,
547540
}
548541
}
549542

550-
private String[] getCommandCompilerCPP(List<String> includePaths,
551-
String sourceName, String objectName)
543+
private String[] getCommandCompilerCPP(List<File> includeFolders,
544+
File sourceFile, File objectFile)
552545
throws RunnerException {
553-
String includes = preparePaths(includePaths);
546+
String includes = prepareIncludes(includeFolders);
554547

555548
PreferencesMap dict = new PreferencesMap(prefs);
556549
dict.put("ide_version", "" + Base.REVISION);
557550
dict.put("includes", includes);
558-
dict.put("source_file", sourceName);
559-
dict.put("object_file", objectName);
551+
dict.put("source_file", sourceFile.getAbsolutePath());
552+
dict.put("object_file", objectFile.getAbsolutePath());
560553

561554
String cmd = prefs.get("recipe.cpp.o.pattern");
562555
try {
@@ -605,29 +598,28 @@ static public List<File> findFilesInFolder(File folder, String extension,
605598
}
606599

607600
// 1. compile the sketch (already in the buildPath)
608-
void compileSketch(List<String> includePaths) throws RunnerException {
609-
String buildPath = prefs.get("build.path");
610-
objectFiles.addAll(compileFiles(buildPath, new File(buildPath), false,
611-
includePaths));
601+
void compileSketch(List<File> includeFolders) throws RunnerException {
602+
File buildPath = prefs.getFile("build.path");
603+
objectFiles.addAll(compileFiles(buildPath, buildPath, false, includeFolders));
612604
}
613605

614606
// 2. compile the libraries, outputting .o files to:
615607
// <buildPath>/<library>/
616-
void compileLibraries(List<String> includePaths) throws RunnerException {
608+
void compileLibraries(List<File> includeFolders) throws RunnerException {
617609
for (Library lib : sketch.getImportedLibraries()) {
618-
compileLibrary(lib, includePaths);
610+
compileLibrary(lib, includeFolders);
619611
}
620612
}
621613

622-
private void compileLibrary(Library lib, List<String> includePaths)
614+
private void compileLibrary(Library lib, List<File> includeFolders)
623615
throws RunnerException {
624616
File libFolder = lib.getSrcFolder();
625-
File libBuildFolder = new File(prefs.get("build.path"), lib.getName());
617+
File libBuildFolder = prefs.getFile(("build.path"), lib.getName());
626618

627619
if (lib.useRecursion()) {
628620
// libBuildFolder == {build.path}/LibName
629621
// libFolder == {lib.path}/src
630-
recursiveCompileFilesInFolder(libBuildFolder, libFolder, includePaths);
622+
recursiveCompileFilesInFolder(libBuildFolder, libFolder, includeFolders);
631623

632624
} else {
633625
// libFolder == {lib.path}/
@@ -637,26 +629,26 @@ private void compileLibrary(Library lib, List<String> includePaths)
637629
File utilityFolder = new File(libFolder, "utility");
638630
File utilityBuildFolder = new File(libBuildFolder, "utility");
639631

640-
includePaths.add(utilityFolder.getAbsolutePath());
641-
compileFilesInFolder(libBuildFolder, libFolder, includePaths);
642-
compileFilesInFolder(utilityBuildFolder, utilityFolder, includePaths);
632+
includeFolders.add(utilityFolder);
633+
compileFilesInFolder(libBuildFolder, libFolder, includeFolders);
634+
compileFilesInFolder(utilityBuildFolder, utilityFolder, includeFolders);
643635

644636
// other libraries should not see this library's utility/ folder
645-
includePaths.remove(utilityFolder.getAbsolutePath());
637+
includeFolders.remove(utilityFolder);
646638
}
647639
}
648640

649-
private void recursiveCompileFilesInFolder(File srcBuildFolder, File srcFolder, List<String> includePaths) throws RunnerException {
650-
compileFilesInFolder(srcBuildFolder, srcFolder, includePaths);
641+
private void recursiveCompileFilesInFolder(File srcBuildFolder, File srcFolder, List<File> includeFolders) throws RunnerException {
642+
compileFilesInFolder(srcBuildFolder, srcFolder, includeFolders);
651643
for (File subFolder : srcFolder.listFiles(new OnlyDirs())) {
652644
File subBuildFolder = new File(srcBuildFolder, subFolder.getName());
653-
recursiveCompileFilesInFolder(subBuildFolder, subFolder, includePaths);
645+
recursiveCompileFilesInFolder(subBuildFolder, subFolder, includeFolders);
654646
}
655647
}
656648

657-
private void compileFilesInFolder(File buildFolder, File srcFolder, List<String> includePaths) throws RunnerException {
649+
private void compileFilesInFolder(File buildFolder, File srcFolder, List<File> includeFolders) throws RunnerException {
658650
createFolder(buildFolder);
659-
List<File> objects = compileFiles(buildFolder.getAbsolutePath(), srcFolder, false, includePaths);
651+
List<File> objects = compileFiles(buildFolder, srcFolder, false, includeFolders);
660652
objectFiles.addAll(objects);
661653
}
662654

@@ -665,22 +657,22 @@ private void compileFilesInFolder(File buildFolder, File srcFolder, List<String>
665657
void compileCore()
666658
throws RunnerException {
667659

668-
String corePath = prefs.get("build.core.path");
669-
String variantPath = prefs.get("build.variant.path");
670-
String buildPath = prefs.get("build.path");
660+
File coreFolder = prefs.getFile("build.core.path");
661+
File variantFolder = prefs.getFile("build.variant.path");
662+
File buildFolder = prefs.getFile("build.path");
671663

672-
List<String> includePaths = new ArrayList<String>();
673-
includePaths.add(corePath); // include core path only
674-
if (variantPath.length() != 0)
675-
includePaths.add(variantPath);
664+
List<File> includeFolders = new ArrayList<File>();
665+
includeFolders.add(coreFolder); // include core path only
666+
if (variantFolder != null)
667+
includeFolders.add(variantFolder);
676668

677-
List<File> coreObjectFiles = compileFiles(buildPath, new File(corePath),
678-
true, includePaths);
679-
if (variantPath.length() != 0)
680-
coreObjectFiles.addAll(compileFiles(buildPath, new File(variantPath),
681-
true, includePaths));
669+
List<File> objectFiles = compileFiles(buildFolder, coreFolder, true,
670+
includeFolders);
671+
if (variantFolder != null)
672+
objectFiles.addAll(compileFiles(buildFolder, variantFolder, true,
673+
includeFolders));
682674

683-
for (File file : coreObjectFiles) {
675+
for (File file : objectFiles) {
684676

685677
PreferencesMap dict = new PreferencesMap(prefs);
686678
dict.put("ide_version", "" + Base.REVISION);
@@ -699,7 +691,7 @@ void compileCore()
699691
}
700692

701693
// 4. link it all together into the .elf file
702-
void compileLink(List<String> includePaths)
694+
void compileLink()
703695
throws RunnerException {
704696

705697
// TODO: Make the --relax thing in configuration files.
@@ -733,7 +725,7 @@ void compileLink(List<String> includePaths)
733725
}
734726

735727
// 5. extract EEPROM data (from EEMEM directive) to .eep file.
736-
void compileEep(List<String> includePaths) throws RunnerException {
728+
void compileEep() throws RunnerException {
737729
PreferencesMap dict = new PreferencesMap(prefs);
738730
dict.put("ide_version", "" + Base.REVISION);
739731

@@ -748,7 +740,7 @@ void compileEep(List<String> includePaths) throws RunnerException {
748740
}
749741

750742
// 6. build the .hex file
751-
void compileHex(List<String> includePaths) throws RunnerException {
743+
void compileHex() throws RunnerException {
752744
PreferencesMap dict = new PreferencesMap(prefs);
753745
dict.put("ide_version", "" + Base.REVISION);
754746

@@ -762,10 +754,10 @@ void compileHex(List<String> includePaths) throws RunnerException {
762754
execAsynchronously(cmdArray);
763755
}
764756

765-
private static String preparePaths(List<String> includePaths) {
757+
private static String prepareIncludes(List<File> includeFolders) {
766758
String res = "";
767-
for (String p : includePaths)
768-
res += " \"-I" + p + '"';
759+
for (File p : includeFolders)
760+
res += " \"-I" + p.getAbsolutePath() + '"';
769761

770762
// Remove first space
771763
return res.substring(1);

app/src/processing/app/helpers/PreferencesMap.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,37 @@ public String getOrExcept(String k) throws PreferencesMapException {
265265
public String toString() {
266266
return toString("");
267267
}
268+
269+
/**
270+
* Creates a new File instance by converting the value of the key into an
271+
* abstract pathname. If the the given key doesn't exists or his value is the
272+
* empty string, the result is <b>null</b>.
273+
*
274+
* @param key
275+
* @return
276+
*/
277+
public File getFile(String key) {
278+
if (!containsKey(key))
279+
return null;
280+
String path = get(key).trim();
281+
if (path.length() == 0)
282+
return null;
283+
return new File(path);
284+
}
285+
286+
/**
287+
* Creates a new File instance by converting the value of the key into an
288+
* abstract pathname with the specified sub folder. If the the given key
289+
* doesn't exists or his value is the empty string, the result is <b>null</b>.
290+
*
291+
* @param key
292+
* @param subFolder
293+
* @return
294+
*/
295+
public File getFile(String key, String subFolder) {
296+
File file = getFile(key);
297+
if (file == null)
298+
return null;
299+
return new File(file, subFolder);
300+
}
268301
}

0 commit comments

Comments
 (0)