Adds support of incremental build into tools

This commit is contained in:
konsoletyper 2014-09-10 21:12:11 +04:00
parent e28771ad48
commit df49ead369
5 changed files with 134 additions and 78 deletions

View File

@ -65,6 +65,19 @@ public final class TeaVMRunner {
.withDescription("Generate debug information") .withDescription("Generate debug information")
.withLongOpt("debug") .withLongOpt("debug")
.create('D')); .create('D'));
options.addOption(OptionBuilder
.withDescription("Generate source maps")
.withLongOpt("sourcemaps")
.create('S'));
options.addOption(OptionBuilder
.withDescription("Incremental build")
.withLongOpt("incremental")
.create('i'));
options.addOption(OptionBuilder
.withArgName("directory")
.withDescription("Incremental build cache directory")
.withLongOpt("cachedir")
.create('c'));
options.addOption(OptionBuilder options.addOption(OptionBuilder
.withDescription("Generate source maps") .withDescription("Generate source maps")
.withLongOpt("sourcemaps") .withLongOpt("sourcemaps")
@ -117,10 +130,16 @@ public final class TeaVMRunner {
tool.setMainPageIncluded(true); tool.setMainPageIncluded(true);
} }
if (commandLine.hasOption('D')) { if (commandLine.hasOption('D')) {
tool.setDebugInformation(new File(tool.getTargetDirectory(), tool.getTargetFileName() + ".teavmdbg")); tool.setDebugInformationGenerated(true);
if (commandLine.hasOption("sourcemaps")) { }
tool.setSourceMapsFileGenerated(true); if (commandLine.hasOption('S')) {
} tool.setSourceMapsFileGenerated(true);
}
if (commandLine.hasOption('i')) {
tool.setIncremental(true);
}
if (commandLine.hasOption('c')) {
tool.setCacheDirectory(new File(commandLine.getOptionValue('c')));
} }
args = commandLine.getArgs(); args = commandLine.getArgs();
if (args.length > 1) { if (args.length > 1) {

View File

@ -1,36 +0,0 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.cache;
import org.teavm.model.MethodReference;
import org.teavm.model.Program;
import org.teavm.model.ProgramCache;
/**
*
* @author Alexey Andreev
*/
public class FileProgramCache implements ProgramCache {
@Override
public Program get(MethodReference method) {
return null;
}
@Override
public void store(MethodReference method, Program program) {
}
}

View File

@ -20,13 +20,14 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.teavm.cache.DiskCachedClassHolderSource;
import org.teavm.cache.DiskProgramCache;
import org.teavm.cache.DiskRegularMethodNodeCache;
import org.teavm.cache.FileSymbolTable;
import org.teavm.debugging.information.DebugInformation; import org.teavm.debugging.information.DebugInformation;
import org.teavm.debugging.information.DebugInformationBuilder; import org.teavm.debugging.information.DebugInformationBuilder;
import org.teavm.javascript.RenderingContext; import org.teavm.javascript.RenderingContext;
import org.teavm.model.ClassHolderTransformer; import org.teavm.model.*;
import org.teavm.model.MethodDescriptor;
import org.teavm.model.MethodReference;
import org.teavm.model.ValueType;
import org.teavm.parsing.ClasspathClassHolderSource; import org.teavm.parsing.ClasspathClassHolderSource;
import org.teavm.vm.*; import org.teavm.vm.*;
import org.teavm.vm.spi.AbstractRendererListener; import org.teavm.vm.spi.AbstractRendererListener;
@ -44,14 +45,20 @@ public class TeaVMTool {
private Properties properties = new Properties(); private Properties properties = new Properties();
private boolean mainPageIncluded; private boolean mainPageIncluded;
private boolean bytecodeLogging; private boolean bytecodeLogging;
private File debugInformation; private boolean debugInformationGenerated;
private String sourceMapsFileName;
private boolean sourceMapsFileGenerated; private boolean sourceMapsFileGenerated;
private boolean incremental;
private File cacheDirectory = new File("./teavm-cache");
private List<ClassHolderTransformer> transformers = new ArrayList<>(); private List<ClassHolderTransformer> transformers = new ArrayList<>();
private List<ClassAlias> classAliases = new ArrayList<>(); private List<ClassAlias> classAliases = new ArrayList<>();
private List<MethodAlias> methodAliases = new ArrayList<>(); private List<MethodAlias> methodAliases = new ArrayList<>();
private TeaVMToolLog log = new EmptyTeaVMToolLog(); private TeaVMToolLog log = new EmptyTeaVMToolLog();
private ClassLoader classLoader = TeaVMTool.class.getClassLoader(); private ClassLoader classLoader = TeaVMTool.class.getClassLoader();
private DiskCachedClassHolderSource cachedClassSource;
private DiskProgramCache programCache;
private DiskRegularMethodNodeCache astCache;
private FileSymbolTable symbolTable;
private FileSymbolTable fileTable;
public File getTargetDirectory() { public File getTargetDirectory() {
return targetDirectory; return targetDirectory;
@ -77,6 +84,14 @@ public class TeaVMTool {
this.minifying = minifying; this.minifying = minifying;
} }
public boolean isIncremental() {
return incremental;
}
public void setIncremental(boolean incremental) {
this.incremental = incremental;
}
public String getMainClass() { public String getMainClass() {
return mainClass; return mainClass;
} }
@ -109,20 +124,20 @@ public class TeaVMTool {
this.bytecodeLogging = bytecodeLogging; this.bytecodeLogging = bytecodeLogging;
} }
public File getDebugInformation() { public boolean isDebugInformationGenerated() {
return debugInformation; return debugInformationGenerated;
} }
public void setDebugInformation(File debugInformation) { public void setDebugInformationGenerated(boolean debugInformationGenerated) {
this.debugInformation = debugInformation; this.debugInformationGenerated = debugInformationGenerated;
} }
public String getSourceMapsFileName() { public File getCacheDirectory() {
return sourceMapsFileName; return cacheDirectory;
} }
public void setSourceMapsFileName(String sourceMapsFileName) { public void setCacheDirectory(File cacheDirectory) {
this.sourceMapsFileName = sourceMapsFileName; this.cacheDirectory = cacheDirectory;
} }
public boolean isSourceMapsFileGenerated() { public boolean isSourceMapsFileGenerated() {
@ -169,13 +184,38 @@ public class TeaVMTool {
try { try {
log.info("Building JavaScript file"); log.info("Building JavaScript file");
TeaVMBuilder vmBuilder = new TeaVMBuilder(); TeaVMBuilder vmBuilder = new TeaVMBuilder();
vmBuilder.setClassLoader(classLoader).setClassSource(new ClasspathClassHolderSource(classLoader)); if (incremental) {
cacheDirectory.mkdirs();
symbolTable = new FileSymbolTable(new File(cacheDirectory, "symbols"));
fileTable = new FileSymbolTable(new File(cacheDirectory, "files"));
ClasspathClassHolderSource innerClassSource = new ClasspathClassHolderSource(classLoader);
ClassHolderSource classSource = new PreOptimizingClassHolderSource(innerClassSource);
cachedClassSource = new DiskCachedClassHolderSource(cacheDirectory, symbolTable, fileTable,
classSource, innerClassSource);
programCache = new DiskProgramCache(cacheDirectory, symbolTable, fileTable, innerClassSource);
astCache = new DiskRegularMethodNodeCache(cacheDirectory, symbolTable, fileTable, innerClassSource);
try {
symbolTable.update();
fileTable.update();
} catch (IOException e) {
log.info("Cache was not read");
}
vmBuilder.setClassLoader(classLoader).setClassSource(cachedClassSource);
} else {
vmBuilder.setClassLoader(classLoader).setClassSource(new ClasspathClassHolderSource(classLoader));
}
TeaVM vm = vmBuilder.build(); TeaVM vm = vmBuilder.build();
vm.setMinifying(minifying); vm.setMinifying(minifying);
vm.setBytecodeLogging(bytecodeLogging); vm.setBytecodeLogging(bytecodeLogging);
vm.setProperties(properties); vm.setProperties(properties);
DebugInformationBuilder debugEmitter = debugInformation != null ? new DebugInformationBuilder() : null; DebugInformationBuilder debugEmitter = debugInformationGenerated || sourceMapsFileGenerated ?
new DebugInformationBuilder() : null;
vm.setDebugEmitter(debugEmitter); vm.setDebugEmitter(debugEmitter);
vm.setIncremental(incremental);
if (incremental) {
vm.setAstCache(astCache);
vm.setProgramCache(programCache);
}
vm.installPlugins(); vm.installPlugins();
for (ClassHolderTransformer transformer : transformers) { for (ClassHolderTransformer transformer : transformers) {
vm.add(transformer); vm.add(transformer);
@ -215,24 +255,32 @@ public class TeaVMTool {
vm.build(writer, new DirectoryBuildTarget(targetDirectory)); vm.build(writer, new DirectoryBuildTarget(targetDirectory));
vm.checkForMissingItems(); vm.checkForMissingItems();
log.info("JavaScript file successfully built"); log.info("JavaScript file successfully built");
if (debugInformation != null) { if (debugInformationGenerated) {
DebugInformation debugInfo = debugEmitter.getDebugInformation(); DebugInformation debugInfo = debugEmitter.getDebugInformation();
try (OutputStream debugInfoOut = new FileOutputStream(debugInformation)) { try (OutputStream debugInfoOut = new FileOutputStream(new File(targetDirectory,
targetFileName + ".teavmdbg"))) {
debugInfo.write(debugInfoOut); debugInfo.write(debugInfoOut);
} }
log.info("Debug information successfully written"); log.info("Debug information successfully written");
if (sourceMapsFileGenerated) { }
String sourceMapsFileName = this.sourceMapsFileName; if (sourceMapsFileGenerated) {
if (sourceMapsFileName == null) { DebugInformation debugInfo = debugEmitter.getDebugInformation();
sourceMapsFileName = targetFileName + ".map"; String sourceMapsFileName = targetFileName + ".map";
} writer.append("\n//# sourceMappingURL=").append(sourceMapsFileName);
writer.append("\n//# sourceMappingURL=").append(sourceMapsFileName); try (Writer sourceMapsOut = new OutputStreamWriter(new FileOutputStream(
try (Writer sourceMapsOut = new OutputStreamWriter(new FileOutputStream( new File(targetDirectory, sourceMapsFileName)), "UTF-8")) {
new File(targetDirectory, sourceMapsFileName)), "UTF-8")) { debugInfo.writeAsSourceMaps(sourceMapsOut, targetFileName);
debugInfo.writeAsSourceMaps(sourceMapsOut, targetFileName);
}
log.info("Source maps successfully written");
} }
log.info("Source maps successfully written");
}
if (incremental) {
programCache.flush();
astCache.flush();
cachedClassSource.flush();
symbolTable.flush();
fileTable.flush();
log.info(mainClass);
log.info("Cache updated");
} }
} }
if (runtime == RuntimeCopyOperation.SEPARATE) { if (runtime == RuntimeCopyOperation.SEPARATE) {

View File

@ -78,7 +78,13 @@ public class BuildJavascriptMojo extends AbstractMojo {
private boolean debugInformationGenerated; private boolean debugInformationGenerated;
@Parameter @Parameter
private File debugInformationFile; private boolean sourceMapsGenerated;
@Parameter
private boolean incremental;
@Parameter(defaultValue = "${project.build.directory}/teavm-cache")
private File cacheDirectory;
@Parameter @Parameter
private String[] transformers; private String[] transformers;
@ -151,12 +157,28 @@ public class BuildJavascriptMojo extends AbstractMojo {
this.debugInformationGenerated = debugInformationGenerated; this.debugInformationGenerated = debugInformationGenerated;
} }
public File getDebugInformationFile() { public boolean isSourceMapsGenerated() {
return debugInformationFile; return sourceMapsGenerated;
} }
public void setDebugInformationFile(File debugInformationFile) { public void setSourceMapsGenerated(boolean sourceMapsGenerated) {
this.debugInformationFile = debugInformationFile; this.sourceMapsGenerated = sourceMapsGenerated;
}
public boolean isIncremental() {
return incremental;
}
public void setIncremental(boolean incremental) {
this.incremental = incremental;
}
public File getCacheDirectory() {
return cacheDirectory;
}
public void setCacheDirectory(File cacheDirectory) {
this.cacheDirectory = cacheDirectory;
} }
@Override @Override
@ -183,10 +205,10 @@ public class BuildJavascriptMojo extends AbstractMojo {
if (properties != null) { if (properties != null) {
tool.getProperties().putAll(properties); tool.getProperties().putAll(properties);
} }
if (isDebugInformationGenerated()) { tool.setCacheDirectory(cacheDirectory);
tool.setDebugInformation(debugInformationFile != null ? debugInformationFile : tool.setIncremental(incremental);
new File(targetDirectory, targetFileName + ".teavmdbg")); tool.setDebugInformationGenerated(debugInformationGenerated);
} tool.setSourceMapsFileGenerated(sourceMapsGenerated);
tool.generate(); tool.generate();
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw new MojoExecutionException("Unexpected error occured", e); throw new MojoExecutionException("Unexpected error occured", e);

View File

@ -68,6 +68,7 @@
<mainClass>org.teavm.samples.HelloWorld</mainClass> <mainClass>org.teavm.samples.HelloWorld</mainClass>
<mainPageIncluded>true</mainPageIncluded> <mainPageIncluded>true</mainPageIncluded>
<debugInformationGenerated>true</debugInformationGenerated> <debugInformationGenerated>true</debugInformationGenerated>
<sourceMapsGenerated>true</sourceMapsGenerated>
<targetDirectory>${project.build.directory}/javascript/hello</targetDirectory> <targetDirectory>${project.build.directory}/javascript/hello</targetDirectory>
</configuration> </configuration>
</execution> </execution>
@ -82,6 +83,7 @@
<mainClass>org.teavm.samples.MatrixMultiplication</mainClass> <mainClass>org.teavm.samples.MatrixMultiplication</mainClass>
<mainPageIncluded>true</mainPageIncluded> <mainPageIncluded>true</mainPageIncluded>
<debugInformationGenerated>true</debugInformationGenerated> <debugInformationGenerated>true</debugInformationGenerated>
<sourceMapsGenerated>true</sourceMapsGenerated>
<targetDirectory>${project.build.directory}/javascript/matrix</targetDirectory> <targetDirectory>${project.build.directory}/javascript/matrix</targetDirectory>
</configuration> </configuration>
</execution> </execution>
@ -96,6 +98,7 @@
<mainClass>org.teavm.samples.DateTime</mainClass> <mainClass>org.teavm.samples.DateTime</mainClass>
<targetDirectory>${project.build.directory}/javascript/datetime</targetDirectory> <targetDirectory>${project.build.directory}/javascript/datetime</targetDirectory>
<debugInformationGenerated>true</debugInformationGenerated> <debugInformationGenerated>true</debugInformationGenerated>
<sourceMapsGenerated>true</sourceMapsGenerated>
<properties> <properties>
<java.util.Locale.available>en_US,en_GB,ru_RU,ru_UA,nl_NL,nl_BE</java.util.Locale.available> <java.util.Locale.available>en_US,en_GB,ru_RU,ru_UA,nl_NL,nl_BE</java.util.Locale.available>
</properties> </properties>