Skip to content

Commit

Permalink
wasm gc: improve gradle plugin settings
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Oct 15, 2024
1 parent 312d8ab commit 61e4bb5
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 32 deletions.
10 changes: 6 additions & 4 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ val jsOutputPackageDir = jsOutputDir.map { it.dir("org/teavm/backend/wasm") }
val jsInputDir = layout.projectDirectory.dir("src/main/js/wasm-gc-runtime")
val jsInput = jsInputDir.file("runtime.js")

fun registerRuntimeTasks(taskName: String, wrapperType: String, outputName: String) {
fun registerRuntimeTasks(taskName: String, wrapperType: String, outputName: String, module: Boolean) {
val generateTask by tasks.register<DefaultTask>("generate${taskName}Runtime") {
dependsOn(tasks.npmInstall)
val wrapperFile = jsInputDir.file(wrapperType)
Expand Down Expand Up @@ -80,7 +80,9 @@ fun registerRuntimeTasks(taskName: String, wrapperType: String, outputName: Stri
args.addAll(provider {
listOf(
"--",
"-m", "--module", "--toplevel",
"-m", "--toplevel",
*(if (module) arrayOf("--module") else emptyArray()),
"--mangle", "reserved=['TeaVM']",
inputFiles.singleFile.absolutePath,
"-o", outputFile.get().asFile.absolutePath
)
Expand All @@ -93,8 +95,8 @@ fun registerRuntimeTasks(taskName: String, wrapperType: String, outputName: Stri
}
}

registerRuntimeTasks("Simple", "simple-wrapper.js", "wasm-gc-runtime")
registerRuntimeTasks("Module", "module-wrapper.js", "wasm-gc-module-runtime")
registerRuntimeTasks("Simple", "simple-wrapper.js", "wasm-gc-runtime", module = false)
registerRuntimeTasks("Module", "module-wrapper.js", "wasm-gc-module-runtime", module = true)

teavmPublish {
artifactId = "teavm-core"
Expand Down
2 changes: 1 addition & 1 deletion samples/benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ teavm {
wasmGC {
addedToWebApp = true
mainClass = "org.teavm.samples.benchmark.teavm.BenchmarkStarter"
sourceMap = true
debugInformation = true
}
wasm {
addedToWebApp = true
Expand Down
6 changes: 5 additions & 1 deletion samples/benchmark/src/main/webapp/teavm-wasm-gc.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
</head>
<script type="text/javascript">
function launch() {
TeaVM.wasmGC.load("wasm-gc/benchmark.wasm").then(teavm => teavm.exports.main([]));
TeaVM.wasmGC.load("wasm-gc/benchmark.wasm", {
stackDeobfuscator: {
enabled: true
}
}).then(teavm => teavm.exports.main([]));
}
</script>
<body onload="launch()">
Expand Down
2 changes: 1 addition & 1 deletion tools/deobfuscator-wasm-gc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ val generateWasm by tasks.register<JavaExec>("generateWasm") {
args(
"org.teavm.tooling.deobfuscate.wasmgc.DeobfuscatorFactory",
layout.buildDirectory.dir("teavm").get().asFile.absolutePath,
"deobfuscator.wasm"
"org/teavm/backend/wasm/deobfuscator.wasm"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void setupJsDefaults() {
.orElse(OptimizationLevel.BALANCED));
js.getSourceFilePolicy().convention(property("js.sourceFilePolicy")
.map(SourceFilePolicy::valueOf)
.orElse(SourceFilePolicy.DO_NOTHING));
.orElse(SourceFilePolicy.LINK_LOCAL_FILES));
js.getDevServer().getStackDeobfuscated().convention(property("js.devServer.stackDeobfuscated")
.map(Boolean::parseBoolean));
js.getDevServer().getIndicator().convention(property("js.devServer.indicator").map(Boolean::parseBoolean));
Expand Down Expand Up @@ -120,6 +120,9 @@ private void setupWasmGCDefaults() {
wasmGC.getDebugInfoLevel().convention(property("wasm-gc.debugInformation.level")
.map(v -> WasmDebugInfoLevel.valueOf(v.toUpperCase())).orElse(WasmDebugInfoLevel.DEOBFUSCATION));
wasmGC.getSourceMap().convention(property("wasm-gc.sourceMap").map(Boolean::parseBoolean).orElse(false));
wasmGC.getSourceFilePolicy().convention(property("wasm-gc.sourceFilePolicy")
.map(SourceFilePolicy::valueOf)
.orElse(SourceFilePolicy.LINK_LOCAL_FILES));
}

private void setupWasiDefaults() {
Expand Down
2 changes: 2 additions & 0 deletions tools/gradle/src/main/java/org/teavm/gradle/TeaVMPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ private void registerWasmGCTask(Project project, Configuration configuration) {
task.getObfuscated().convention(wasmGC.getObfuscated());
task.getStrict().convention(wasmGC.getStrict());
task.getSourceMap().convention(wasmGC.getSourceMap());
task.getSourceFilePolicy().convention(wasmGC.getSourceFilePolicy());
setupSources(task.getSourceFiles(), project);
});
project.getTasks().create(WASM_GC_COPY_RUNTIME_TASK_NAME, CopyWasmGCRuntimeTask.class, task -> {
task.setGroup(TASK_GROUP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public interface TeaVMWasmGCConfiguration extends TeaVMCommonConfiguration, TeaV
Property<WasmDebugInfoLevel> getDebugInfoLevel();

Property<Boolean> getSourceMap();

Property<SourceFilePolicy> getSourceFilePolicy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,51 @@
import java.nio.file.StandardCopyOption;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;

public abstract class CopyWasmGCRuntimeTask extends DefaultTask {
public CopyWasmGCRuntimeTask() {
getModular().convention(false);
getObfuscated().convention(true);
}

@Input
public abstract Property<Boolean> getModular();

@Input
public abstract Property<Boolean> getObfuscated();

@OutputFile
public abstract RegularFileProperty getOutputFile();

@OutputFile
public abstract RegularFileProperty getDeobfuscator();

@TaskAction
public void copyRuntime() throws IOException {
var resourceName = "org/teavm/backend/wasm/wasm-gc-runtime.min.js";
var name = new StringBuilder("wasm-gc");
if (getModular().get()) {
name.append("-modular");
}
if (getObfuscated().get()) {
name.append("-min");
}
var resourceName = "org/teavm/backend/wasm/" + name + ".js";
var classLoader = CopyWasmGCRuntimeTask.class.getClassLoader();
var output = getOutputFile().get().getAsFile();
try (var input = classLoader.getResourceAsStream(resourceName)) {
Files.copy(input, output.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

if (getDeobfuscator().isPresent()) {
resourceName = "org/teavm/backend/wasm/deobfuscator.wasm";
output = getDeobfuscator().get().getAsFile();
try (var input = classLoader.getResourceAsStream(resourceName)) {
Files.copy(input, output.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.gradle.api.tasks.Optional;
import org.teavm.gradle.api.JSModuleType;
import org.teavm.gradle.api.SourceFilePolicy;
import org.teavm.tooling.TeaVMSourceFilePolicy;
import org.teavm.tooling.TeaVMTargetType;
import org.teavm.tooling.builder.BuildStrategy;

Expand All @@ -32,7 +31,7 @@ public GenerateJavaScriptTask() {
getStrict().convention(false);
getModuleType().convention(JSModuleType.UMD);
getSourceMap().convention(false);
getSourceFilePolicy().convention(SourceFilePolicy.DO_NOTHING);
getSourceFilePolicy().convention(SourceFilePolicy.LINK_LOCAL_FILES);
getEntryPointName().convention("main");
}

Expand Down Expand Up @@ -91,25 +90,7 @@ protected void setupBuilder(BuildStrategy builder) {
}
builder.setSourceMapsFileGenerated(getSourceMap().get());
builder.setEntryPointName(getEntryPointName().get());
for (var file : getSourceFiles()) {
if (file.isFile()) {
if (file.getName().endsWith(".jar") || file.getName().endsWith(".zip")) {
builder.addSourcesJar(file.getAbsolutePath());
}
} else if (file.isDirectory()) {
builder.addSourcesDirectory(file.getAbsolutePath());
}
}
switch (getSourceFilePolicy().get()) {
case DO_NOTHING:
builder.setSourceFilePolicy(TeaVMSourceFilePolicy.DO_NOTHING);
break;
case COPY:
builder.setSourceFilePolicy(TeaVMSourceFilePolicy.COPY);
break;
case LINK_LOCAL_FILES:
builder.setSourceFilePolicy(TeaVMSourceFilePolicy.LINK_LOCAL_FILES);
break;
}
TaskUtils.applySourceFiles(getSourceFiles(), builder);
TaskUtils.applySourceFilePolicy(getSourceFilePolicy(), builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package org.teavm.gradle.tasks;

import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Optional;
import org.teavm.gradle.api.SourceFilePolicy;
import org.teavm.gradle.api.WasmDebugInfoLevel;
import org.teavm.gradle.api.WasmDebugInfoLocation;
import org.teavm.tooling.TeaVMTargetType;
Expand All @@ -30,6 +34,7 @@ public GenerateWasmGCTask() {
getDebugInfoLevel().convention(WasmDebugInfoLevel.DEOBFUSCATION);
getDebugInfoLocation().convention(WasmDebugInfoLocation.EXTERNAL);
getSourceMap().convention(false);
getSourceFilePolicy().convention(SourceFilePolicy.LINK_LOCAL_FILES);
}

@Input
Expand All @@ -50,6 +55,13 @@ public GenerateWasmGCTask() {
@Input
public abstract Property<Boolean> getSourceMap();

@InputFiles
public abstract ConfigurableFileCollection getSourceFiles();

@Input
@Optional
public abstract Property<SourceFilePolicy> getSourceFilePolicy();

@Override
protected void setupBuilder(BuildStrategy builder) {
builder.setStrict(getStrict().get());
Expand All @@ -73,5 +85,7 @@ protected void setupBuilder(BuildStrategy builder) {
break;
}
builder.setTargetType(TeaVMTargetType.WEBASSEMBLY_GC);
TaskUtils.applySourceFiles(getSourceFiles(), builder);
TaskUtils.applySourceFilePolicy(getSourceFilePolicy(), builder);
}
}
53 changes: 53 additions & 0 deletions tools/gradle/src/main/java/org/teavm/gradle/tasks/TaskUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 konsoletyper.
*
* 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.gradle.tasks;

import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.provider.Property;
import org.teavm.gradle.api.SourceFilePolicy;
import org.teavm.tooling.TeaVMSourceFilePolicy;
import org.teavm.tooling.builder.BuildStrategy;

final class TaskUtils {
private TaskUtils() {
}

static void applySourceFiles(ConfigurableFileCollection sourceFiles, BuildStrategy builder) {
for (var file : sourceFiles) {
if (file.isFile()) {
if (file.getName().endsWith(".jar") || file.getName().endsWith(".zip")) {
builder.addSourcesJar(file.getAbsolutePath());
}
} else if (file.isDirectory()) {
builder.addSourcesDirectory(file.getAbsolutePath());
}
}
}

static void applySourceFilePolicy(Property<SourceFilePolicy> policy, BuildStrategy builder) {
switch (policy.get()) {
case DO_NOTHING:
builder.setSourceFilePolicy(TeaVMSourceFilePolicy.DO_NOTHING);
break;
case COPY:
builder.setSourceFilePolicy(TeaVMSourceFilePolicy.COPY);
break;
case LINK_LOCAL_FILES:
builder.setSourceFilePolicy(TeaVMSourceFilePolicy.LINK_LOCAL_FILES);
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void additionalOutput(File outputPath, File outputPathForMethod, TeaVMTestConfig
getExtension() + "-deobfuscator.wasm");
try {
TestUtil.resourceToFile("org/teavm/backend/wasm/wasm-gc-runtime.js", testPath, Map.of());
TestUtil.resourceToFile("deobfuscator.wasm", testDeobfuscatorPath, Map.of());
TestUtil.resourceToFile("org/teavm/backend/wasm/deobfuscator.wasm", testDeobfuscatorPath, Map.of());
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 61e4bb5

Please sign in to comment.