Skip to content

A new compiler #3779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ hardware/arduino/bootloaders/caterina_LUFA/Caterina.elf
hardware/arduino/bootloaders/caterina_LUFA/Caterina.eep
hardware/arduino/bootloaders/caterina_LUFA/.dep/
build/*.zip
build/*.tar.bz2
build/windows/work/
build/windows/*.zip
build/windows/*.tgz
Expand Down
4 changes: 2 additions & 2 deletions app/src/cc/arduino/ConsoleOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ private void clearBuffer() {
if (document != null) {
SwingUtilities.invokeLater(() -> {
try {
String lineWithoutSlashR = line.replace("\r\n", "\n").replace("\r", "\n");
String lineWithoutCR = line.replace("\r\n", "\n").replace("\r", "\n");
int offset = document.getLength();
document.insertString(offset, lineWithoutSlashR, attributes);
document.insertString(offset, lineWithoutCR, attributes);
} catch (BadLocationException ble) {
//ignore
}
Expand Down
2 changes: 0 additions & 2 deletions app/src/cc/arduino/packages/formatter/AStyle.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
* This file is part of Arduino.
*
Expand Down
4 changes: 4 additions & 0 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@ public void actionPerformed(ActionEvent e) {
item = newJMenuItemAlt(tr("Export compiled Binary"), 'S');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (new ShouldSaveReadOnly().test(sketch) && !handleSave(true)) {
System.out.println(tr("Export canceled, changes must first be saved."));
return;
}
handleRun(false, new ShouldSaveReadOnly(), Editor.this.presentAndSaveHandler, Editor.this.runAndSaveHandler);
}
});
Expand Down
46 changes: 35 additions & 11 deletions app/src/processing/app/Sketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@

package processing.app;

import cc.arduino.Compiler;
import cc.arduino.CompilerProgressListener;
import cc.arduino.UploaderUtils;
import cc.arduino.files.DeleteFilesOnShutdown;
import cc.arduino.packages.Uploader;
import processing.app.debug.Compiler;
import processing.app.debug.Compiler.ProgressListener;
import processing.app.debug.RunnerException;
import processing.app.forms.PasswordAuthorizationDialog;
import processing.app.helpers.FileUtils;
import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesMapException;
import processing.app.packages.LibraryList;
Expand All @@ -37,10 +40,14 @@
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static processing.app.I18n.tr;

Expand All @@ -60,7 +67,7 @@ public class Sketch {
private int currentIndex;

private final SketchData data;

/**
* path is location of the main .pde file, because this is also
* simplest to use when opening the file from the finder/explorer.
Expand Down Expand Up @@ -1093,7 +1100,7 @@ public void prepare() throws IOException {
* @return null if compilation failed, main class name if not
* @throws RunnerException
*/
public String build(boolean verbose, boolean save) throws RunnerException, PreferencesMapException {
public String build(boolean verbose, boolean save) throws RunnerException, PreferencesMapException, IOException {
return build(tempBuildFolder.getAbsolutePath(), verbose, save);
}

Expand All @@ -1106,15 +1113,32 @@ public String build(boolean verbose, boolean save) throws RunnerException, Prefe
*
* @return null if compilation failed, main class name if not
*/
private String build(String buildPath, boolean verbose, boolean save) throws RunnerException, PreferencesMapException {
private String build(String buildPath, boolean verbose, boolean save) throws RunnerException, PreferencesMapException, IOException {
// run the preprocessor
editor.status.progressUpdate(20);

ensureExistence();

ProgressListener pl = editor.status::progressUpdate;

return Compiler.build(data, buildPath, tempBuildFolder, pl, verbose, save);

CompilerProgressListener progressListener = editor.status::progressUpdate;

String pathToSketch = data.getMainFilePath();
if (isModified()) {
pathToSketch = saveSketchInTempFolder();
}

return new Compiler(pathToSketch, data, buildPath).build(progressListener, save);
}

private String saveSketchInTempFolder() throws IOException {
File tempFolder = FileUtils.createTempFolder();
DeleteFilesOnShutdown.add(tempFolder);
FileUtils.copy(getFolder(), tempFolder);

for (SketchCode sc : Stream.of(data.getCodes()).filter(SketchCode::isModified).collect(Collectors.toList())) {
Files.write(Paths.get(tempFolder.getAbsolutePath(), sc.getFileName()), sc.getProgram().getBytes());
}

return Paths.get(tempFolder.getAbsolutePath(), data.getPrimaryFile().getName()).toString();
}

protected boolean exportApplet(boolean usingProgrammer) throws Exception {
Expand Down Expand Up @@ -1153,7 +1177,7 @@ private boolean exportApplet(String appletPath, boolean usingProgrammer)

private boolean upload(String buildPath, String suggestedClassName, boolean usingProgrammer) throws Exception {

Uploader uploader = Compiler.getUploaderByPreferences(false);
Uploader uploader = new UploaderUtils().getUploaderByPreferences(false);

boolean success = false;
do {
Expand All @@ -1172,7 +1196,7 @@ private boolean upload(String buildPath, String suggestedClassName, boolean usin

List<String> warningsAccumulator = new LinkedList<>();
try {
success = Compiler.upload(data, uploader, buildPath, suggestedClassName, usingProgrammer, false, warningsAccumulator);
success = new UploaderUtils().upload(data, uploader, buildPath, suggestedClassName, usingProgrammer, false, warningsAccumulator);
} finally {
if (uploader.requiresAuthorization() && !success) {
PreferencesData.remove(uploader.getAuthorizationKey());
Expand Down
62 changes: 0 additions & 62 deletions app/src/processing/app/debug/MessageStream.java

This file was deleted.

95 changes: 95 additions & 0 deletions app/test/cc/arduino/i18n/ExternalProcessOutputParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* This file is part of Arduino.
*
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
*
* Arduino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/

package cc.arduino.i18n;

import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertEquals;

public class ExternalProcessOutputParserTest {

@Test
public void testParser1() throws Exception {
Map<String, Object> output = new ExternalProcessOutputParser().parse("===WARNING: Category '{0}' in library {1} is not valid. Setting to '{2}' ||| [ Wire Uncategorized]");

assertEquals("WARNING: Category '{0}' in library {1} is not valid. Setting to '{2}'", output.get("msg"));
Object[] args = (Object[]) output.get("args");
assertEquals(3, args.length);
assertEquals("", args[0]);
assertEquals("Wire", args[1]);
assertEquals("Uncategorized", args[2]);
}

@Test
public void testParser2() throws Exception {
Map<String, Object> output = new ExternalProcessOutputParser().parse("===Using previously compiled file: {0} ||| [%2Ftmp%2Farduino-sketch-456612873D8321DA02916066CB8B2FE6%2Flibraries%2FBridge%2FBridge.cpp.o]");

assertEquals("Using previously compiled file: {0}", output.get("msg"));
Object[] args = (Object[]) output.get("args");
assertEquals(1, args.length);
assertEquals("/tmp/arduino-sketch-456612873D8321DA02916066CB8B2FE6/libraries/Bridge/Bridge.cpp.o", args[0]);
}

@Test
public void testParser3() throws Exception {
Map<String, Object> output = new ExternalProcessOutputParser().parse("===Using library {0} at version {1} in folder: {2} {3} {4} ||| [Stepper 1.1.1 %2Fhome%2Ffederico%2Fmateriale%2Fworks_Arduino%2FArduino%2Fbuild%2Flinux%2Fwork%2Flibraries%2FStepper ]");

assertEquals("Using library {0} at version {1} in folder: {2} {3} {4}", output.get("msg"));
Object[] args = (Object[]) output.get("args");
assertEquals(5, args.length);
assertEquals("Stepper", args[0]);
assertEquals("1.1.1", args[1]);
assertEquals("/home/federico/materiale/works_Arduino/Arduino/build/linux/work/libraries/Stepper", args[2]);
assertEquals("", args[3]);
assertEquals("", args[4]);
}

@Test
public void testParser4() throws Exception {
Map<String, Object> output = new ExternalProcessOutputParser().parse("==={0} ||| []");

assertEquals("{0}", output.get("msg"));
Object[] args = (Object[]) output.get("args");
assertEquals(0, args.length);
}

@Test
public void testParser5() throws Exception {
Map<String, Object> output = new ExternalProcessOutputParser().parse("==={0} ||| [ ]");

assertEquals("{0}", output.get("msg"));
Object[] args = (Object[]) output.get("args");
assertEquals(1, args.length);
assertEquals("", args[0]);
}

}
56 changes: 56 additions & 0 deletions app/test/cc/arduino/i18n/I18NTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This file is part of Arduino.
*
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
*
* Arduino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/

package cc.arduino.i18n;

import org.junit.Test;
import processing.app.AbstractWithPreferencesTest;
import processing.app.I18n;

import java.util.Map;

import static org.junit.Assert.assertEquals;

public class I18NTest extends AbstractWithPreferencesTest {

@Test
public void testMessageFormat() throws Exception {
Object[] args = new Object[]{"a", "b", "c"};
String actual = I18n.format("WARNING: Category '{0}' in library {1} is not valid. Setting to '{2}'", args);
assertEquals("WARNING: Category 'a' in library b is not valid. Setting to 'c'", actual);
}

@Test
public void testMessageFormatFromExternalProcess() throws Exception {
Map<String, Object> output = new ExternalProcessOutputParser().parse("===WARNING: Category '{0}' in library {1} is not valid. Setting to '{2}' ||| [ Wire Uncategorized]");

String actual = I18n.format((String) output.get("msg"), (Object[])output.get("args"));
assertEquals("WARNING: Category '' in library Wire is not valid. Setting to 'Uncategorized'", actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
package processing.app.debug;

import static org.junit.Assert.assertEquals;
import static processing.app.debug.Compiler.unescapeDepFile;
import static processing.app.debug.OldCompiler.unescapeDepFile;

import org.junit.Test;

import processing.app.AbstractWithPreferencesTest;

public class CompilerTest extends AbstractWithPreferencesTest {
public class OldCompilerTest extends AbstractWithPreferencesTest {

@Test
public void makeDepUnescapeTest() throws Exception {
Expand Down
Loading