Allow to run WASM generator via maven.

This commit is contained in:
Alexey Andreev 2016-09-03 23:48:05 +03:00
parent 8b3e160d8c
commit 40af27811f
4 changed files with 19 additions and 135 deletions

View File

@ -1,122 +0,0 @@
/*
* Copyright 2016 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.backend.wasm.binary;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
class MetadataWriter {
private byte[] data = new byte[256];
private int offset;
private Map<DataStructure, Integer> structureUsages = new HashMap<>();
void write(DataType[] types) {
writeLeb(types.length);
for (DataType type : types) {
write(type);
}
}
private void write(DataType type) {
if (type == DataPrimitives.BYTE) {
writeByte(0);
} else if (type == DataPrimitives.SHORT) {
writeByte(1);
} else if (type == DataPrimitives.INT) {
writeByte(2);
} else if (type == DataPrimitives.LONG) {
writeByte(3);
} else if (type == DataPrimitives.FLOAT) {
writeByte(4);
} else if (type == DataPrimitives.DOUBLE) {
writeByte(5);
} else if (type == DataPrimitives.ADDRESS) {
writeByte(6);
} else if (type instanceof DataArray) {
DataArray array = (DataArray) type;
writeByte(7);
writeLeb(array.getSize());
write(array.getComponentType());
} else if (type instanceof DataStructure) {
DataStructure structure = (DataStructure) type;
Integer usage = structureUsages.get(structure);
if (usage == null) {
structureUsages.put(structure, offset);
writeByte(8);
writeByte(structure.getAlignment());
DataType[] components = structure.getComponents();
writeLeb(components.length);
for (int i = 0; i < components.length; ++i) {
write(components[i]);
}
} else {
int start = offset;
structureUsages.put(structure, offset);
writeByte(9);
writeLeb(start - usage);
}
}
}
private void writeLeb(int value) {
if (value >>> 7 == 0) {
writeByte(value);
} else if (value >>> 14 == 0) {
byte first = (byte) ((value & 0x7F) | 0x80);
byte second = (byte) (value >>> 7);
writeBytes(new byte[] { first, second });
} else if (value >>> 21 == 0) {
byte first = (byte) ((value & 0x7F) | 0x80);
byte second = (byte) (((value >>> 7) & 0x7F) | 0x80);
byte third = (byte) (value >>> 14);
writeBytes(new byte[] { first, second, third });
} else if (value >>> 28 == 0) {
byte first = (byte) ((value & 0x7F) | 0x80);
byte second = (byte) (((value >>> 7) & 0x7F) | 0x80);
byte third = (byte) (((value >>> 14) & 0x7F) | 0x80);
byte fourth = (byte) (value >>> 21);
writeBytes(new byte[] { first, second, third, fourth });
} else {
byte first = (byte) ((value & 0x7F) | 0x80);
byte second = (byte) (((value >>> 7) & 0x7F) | 0x80);
byte third = (byte) (((value >>> 14) & 0x7F) | 0x80);
byte fourth = (byte) (((value >>> 21) & 0x7F) | 0x80);
byte fifth = (byte) (value >>> 28);
writeBytes(new byte[] { first, second, third, fourth, fifth });
}
}
private void writeByte(int value) {
if (offset >= data.length) {
data = Arrays.copyOf(data, data.length * 2);
}
data[offset++] = (byte) value;
}
private void writeBytes(byte[] values) {
int count = values.length;
if (offset + count > data.length) {
data = Arrays.copyOf(data, data.length * 2);
}
System.arraycopy(values, 0, data, offset, count);
offset += count;
}
public byte[] getData() {
return Arrays.copyOf(data, offset);
}
}

View File

@ -330,6 +330,8 @@ public class TeaVMTool implements BaseTeaVMTool {
private WasmTarget prepareWebAssemblyTarget() {
webAssemblyTarget = new WasmTarget();
webAssemblyTarget.setDebugging(debugInformationGenerated);
webAssemblyTarget.setCEmitted(debugInformationGenerated);
webAssemblyTarget.setWastEmitted(debugInformationGenerated);
return webAssemblyTarget;
}

View File

@ -38,11 +38,7 @@ import org.teavm.model.ClassHolderTransformer;
import org.teavm.tooling.BaseTeaVMTool;
import org.teavm.tooling.sources.SourceFileProvider;
/**
*
* @author Alexey Andreev
*/
public abstract class AbstractJavascriptMojo extends AbstractMojo {
public abstract class AbstractTeaVMMojo extends AbstractMojo {
@Component
protected MavenProject project;
@ -180,7 +176,7 @@ public abstract class AbstractJavascriptMojo extends AbstractMojo {
}
log.info("Using the following classpath for JavaScript generation: " + classpath);
classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]),
AbstractJavascriptMojo.class.getClassLoader());
AbstractTeaVMMojo.class.getClassLoader());
return classLoader;
} catch (MalformedURLException e) {
throw new MojoExecutionException("Error gathering classpath information", e);

View File

@ -27,19 +27,17 @@ import org.apache.maven.plugins.annotations.ResolutionScope;
import org.teavm.tooling.ClassAlias;
import org.teavm.tooling.MethodAlias;
import org.teavm.tooling.RuntimeCopyOperation;
import org.teavm.tooling.TeaVMTargetType;
import org.teavm.tooling.TeaVMTool;
import org.teavm.tooling.TeaVMToolException;
import org.teavm.tooling.sources.DirectorySourceFileProvider;
import org.teavm.tooling.sources.SourceFileProvider;
import org.teavm.vm.TeaVMOptimizationLevel;
/**
*
* @author Alexey Andreev
*/
@Mojo(name = "compile", requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME,
defaultPhase = LifecyclePhase.PROCESS_CLASSES)
public class BuildJavascriptMojo extends AbstractJavascriptMojo {
public class TeaVMCompileMojo extends AbstractTeaVMMojo {
@Parameter(defaultValue = "${project.build.directory}/javascript")
private File targetDirectory;
@ -47,7 +45,7 @@ public class BuildJavascriptMojo extends AbstractJavascriptMojo {
private File sourceDirectory;
@Parameter
private String targetFileName = "classes.js";
private String targetFileName = "";
@Parameter
private String mainClass;
@ -67,6 +65,12 @@ public class BuildJavascriptMojo extends AbstractJavascriptMojo {
@Parameter
protected RuntimeCopyOperation runtime = RuntimeCopyOperation.SEPARATE;
@Parameter
private TeaVMOptimizationLevel optimizationLevel = TeaVMOptimizationLevel.SIMPLE;
@Parameter
private TeaVMTargetType targetType = TeaVMTargetType.JAVASCRIPT;
@Parameter(defaultValue = "${project.build.directory}/teavm-cache")
protected File cacheDirectory;
@ -86,7 +90,10 @@ public class BuildJavascriptMojo extends AbstractJavascriptMojo {
tool.setMainClass(mainClass);
tool.setMainPageIncluded(mainPageIncluded);
tool.setRuntime(runtime);
tool.setTargetFileName(targetFileName);
if (!targetFileName.isEmpty()) {
tool.setTargetFileName(targetFileName);
}
tool.setOptimizationLevel(optimizationLevel);
if (classAliases != null) {
tool.getClassAliases().addAll(Arrays.asList(classAliases));
}
@ -94,6 +101,7 @@ public class BuildJavascriptMojo extends AbstractJavascriptMojo {
tool.getMethodAliases().addAll(Arrays.asList(methodAliases));
}
tool.setCacheDirectory(cacheDirectory);
tool.setTargetType(targetType);
tool.generate();
if (stopOnErrors && !tool.getProblemProvider().getSevereProblems().isEmpty()) {
throw new MojoExecutionException("Build error");