Skip to content

Commit afbbe77

Browse files
bitroncmaglie
authored andcommitted
Moved the sketch building code from Sketch to Compiler.
1 parent b7d1846 commit afbbe77

File tree

2 files changed

+181
-174
lines changed

2 files changed

+181
-174
lines changed

app/src/processing/app/Sketch.java

Lines changed: 5 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@
2626
import cc.arduino.packages.BoardPort;
2727
import cc.arduino.packages.UploaderAndMonitorFactory;
2828
import cc.arduino.packages.Uploader;
29-
import processing.app.debug.*;
3029
import processing.app.debug.Compiler;
3130
import processing.app.debug.Compiler.ProgressListener;
31+
import processing.app.debug.RunnerException;
32+
import processing.app.debug.TargetPlatform;
3233
import processing.app.forms.PasswordAuthorizationDialog;
3334
import processing.app.helpers.OSUtils;
34-
import processing.app.helpers.PreferencesMap;
35-
import processing.app.helpers.FileUtils;
3635
import processing.app.packages.Library;
3736
import static processing.app.I18n._;
3837

@@ -61,12 +60,6 @@ public class Sketch {
6160
/** Class name for the PApplet, as determined by the preprocessor. */
6261
private String appletClassName;
6362

64-
/**
65-
* File inside the build directory that contains the build options
66-
* used for the last build.
67-
*/
68-
static final String BUILD_PREFS_FILE = "buildprefs.txt";
69-
7063
/**
7164
* path is location of the main .pde file, because this is also
7265
* simplest to use when opening the file from the finder/explorer.
@@ -1017,48 +1010,6 @@ protected void setCurrentCode(String findName) {
10171010
}
10181011

10191012

1020-
/**
1021-
* Cleanup temporary files used during a build/run.
1022-
*/
1023-
protected void cleanup(boolean force) {
1024-
// if the java runtime is holding onto any files in the build dir, we
1025-
// won't be able to delete them, so we need to force a gc here
1026-
System.gc();
1027-
1028-
if (force) {
1029-
// delete the entire directory and all contents
1030-
// when we know something changed and all objects
1031-
// need to be recompiled, or if the board does not
1032-
// use setting build.dependency
1033-
//Base.removeDir(tempBuildFolder);
1034-
1035-
// note that we can't remove the builddir itself, otherwise
1036-
// the next time we start up, internal runs using Runner won't
1037-
// work because the build dir won't exist at startup, so the classloader
1038-
// will ignore the fact that that dir is in the CLASSPATH in run.sh
1039-
Base.removeDescendants(tempBuildFolder);
1040-
} else {
1041-
// delete only stale source files, from the previously
1042-
// compiled sketch. This allows multiple windows to be
1043-
// used. Keep everything else, which might be reusable
1044-
if (tempBuildFolder.exists()) {
1045-
String files[] = tempBuildFolder.list();
1046-
for (String file : files) {
1047-
if (file.endsWith(".c") || file.endsWith(".cpp") || file.endsWith(".s")) {
1048-
File deleteMe = new File(tempBuildFolder, file);
1049-
if (!deleteMe.delete()) {
1050-
System.err.println("Could not delete " + deleteMe);
1051-
}
1052-
}
1053-
}
1054-
}
1055-
}
1056-
1057-
// Create a fresh applet folder (needed before preproc is run below)
1058-
//tempBuildFolder.mkdirs();
1059-
}
1060-
1061-
10621013
/**
10631014
* Preprocess, Compile, and Run the current code.
10641015
* <P>
@@ -1180,47 +1131,6 @@ public String build(boolean verbose) throws RunnerException {
11801131
return build(tempBuildFolder.getAbsolutePath(), verbose);
11811132
}
11821133

1183-
/**
1184-
* Check if the build preferences used on the previous build in
1185-
* buildPath match the ones given.
1186-
*/
1187-
protected boolean buildPreferencesChanged(File buildPrefsFile, String newBuildPrefs) {
1188-
// No previous build, so no match
1189-
if (!buildPrefsFile.exists())
1190-
return true;
1191-
1192-
String previousPrefs;
1193-
try {
1194-
previousPrefs = FileUtils.readFileToString(buildPrefsFile);
1195-
} catch (IOException e) {
1196-
System.err.println(_("Could not read prevous build preferences file, rebuilding all"));
1197-
return true;
1198-
}
1199-
1200-
if (!previousPrefs.equals(newBuildPrefs)) {
1201-
System.out.println(_("Build options changed, rebuilding all"));
1202-
return true;
1203-
} else {
1204-
return false;
1205-
}
1206-
}
1207-
1208-
/**
1209-
* Returns the build preferences of the given compiler as a string.
1210-
* Only includes build-specific preferences, to make sure unrelated
1211-
* preferences don't cause a rebuild (in particular preferences that
1212-
* change on every start, like last.ide.xxx.daterun). */
1213-
protected String buildPrefsString(Compiler compiler) {
1214-
PreferencesMap buildPrefs = compiler.getBuildPreferences();
1215-
String res = "";
1216-
SortedSet<String> treeSet = new TreeSet<String>(buildPrefs.keySet());
1217-
for (String k : treeSet) {
1218-
if (k.startsWith("build.") || k.startsWith("compiler.") || k.startsWith("recipes."))
1219-
res += k + " = " + buildPrefs.get(k) + "\n";
1220-
}
1221-
return res;
1222-
}
1223-
12241134
/**
12251135
* Preprocess and compile all the code for this sketch.
12261136
*
@@ -1231,45 +1141,19 @@ protected String buildPrefsString(Compiler compiler) {
12311141
* @return null if compilation failed, main class name if not
12321142
*/
12331143
public String build(String buildPath, boolean verbose) throws RunnerException {
1234-
String primaryClassName = data.getName() + ".cpp";
1235-
Compiler compiler = new Compiler(data, buildPath, primaryClassName);
1236-
File buildPrefsFile = new File(buildPath, BUILD_PREFS_FILE);
1237-
String newBuildPrefs = buildPrefsString(compiler);
1238-
1239-
// Do a forced cleanup (throw everything away) if the previous
1240-
// build settings do not match the previous ones
1241-
boolean prefsChanged = buildPreferencesChanged(buildPrefsFile, newBuildPrefs);
1242-
cleanup(prefsChanged);
1243-
1244-
if (prefsChanged) {
1245-
try {
1246-
PrintWriter out = new PrintWriter(buildPrefsFile);
1247-
out.print(newBuildPrefs);
1248-
out.close();
1249-
} catch (IOException e) {
1250-
System.err.println(_("Could not write build preferences file"));
1251-
}
1252-
}
1253-
12541144
// run the preprocessor
12551145
editor.status.progressUpdate(20);
12561146

12571147
ensureExistence();
12581148

1259-
compiler.setProgressListener(new ProgressListener() {
1149+
ProgressListener pl = new ProgressListener() {
12601150
@Override
12611151
public void progress(int percent) {
12621152
editor.status.progressUpdate(percent);
12631153
}
1264-
});
1154+
};
12651155

1266-
// compile the program. errors will happen as a RunnerException
1267-
// that will bubble up to whomever called build().
1268-
if (compiler.compile(verbose)) {
1269-
size(compiler.getBuildPreferences());
1270-
return primaryClassName;
1271-
}
1272-
return null;
1156+
return Compiler.build(data, buildPath, tempBuildFolder, pl, verbose);
12731157
}
12741158

12751159
protected boolean exportApplet(boolean usingProgrammer) throws Exception {
@@ -1307,58 +1191,6 @@ public boolean exportApplet(String appletPath, boolean usingProgrammer)
13071191
}
13081192

13091193

1310-
protected void size(PreferencesMap prefs) throws RunnerException {
1311-
String maxTextSizeString = prefs.get("upload.maximum_size");
1312-
String maxDataSizeString = prefs.get("upload.maximum_data_size");
1313-
if (maxTextSizeString == null)
1314-
return;
1315-
long maxTextSize = Integer.parseInt(maxTextSizeString);
1316-
long maxDataSize = -1;
1317-
if (maxDataSizeString != null)
1318-
maxDataSize = Integer.parseInt(maxDataSizeString);
1319-
Sizer sizer = new Sizer(prefs);
1320-
long[] sizes;
1321-
try {
1322-
sizes = sizer.computeSize();
1323-
} catch (RunnerException e) {
1324-
System.err.println(I18n.format(_("Couldn't determine program size: {0}"),
1325-
e.getMessage()));
1326-
return;
1327-
}
1328-
1329-
long textSize = sizes[0];
1330-
long dataSize = sizes[1];
1331-
System.out.println();
1332-
System.out.println(I18n
1333-
.format(_("Sketch uses {0} bytes ({2}%%) of program storage space. Maximum is {1} bytes."),
1334-
textSize, maxTextSize, textSize * 100 / maxTextSize));
1335-
if (dataSize >= 0) {
1336-
if (maxDataSize > 0) {
1337-
System.out
1338-
.println(I18n
1339-
.format(
1340-
_("Global variables use {0} bytes ({2}%%) of dynamic memory, leaving {3} bytes for local variables. Maximum is {1} bytes."),
1341-
dataSize, maxDataSize, dataSize * 100 / maxDataSize,
1342-
maxDataSize - dataSize));
1343-
} else {
1344-
System.out.println(I18n
1345-
.format(_("Global variables use {0} bytes of dynamic memory."), dataSize));
1346-
}
1347-
}
1348-
1349-
if (textSize > maxTextSize)
1350-
throw new RunnerException(
1351-
_("Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it."));
1352-
1353-
if (maxDataSize > 0 && dataSize > maxDataSize)
1354-
throw new RunnerException(
1355-
_("Not enough memory; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing your footprint."));
1356-
1357-
int warnDataPercentage = Integer.parseInt(prefs.get("build.warn_data_percentage"));
1358-
if (maxDataSize > 0 && dataSize > maxDataSize*warnDataPercentage/100)
1359-
System.err.println(_("Low memory available, stability problems may occur."));
1360-
}
1361-
13621194
protected boolean upload(String buildPath, String suggestedClassName, boolean usingProgrammer) throws Exception {
13631195

13641196
TargetPlatform target = Base.getTargetPlatform();

0 commit comments

Comments
 (0)