From a2986e40eeea57a893495ef87b230304955c00fa Mon Sep 17 00:00:00 2001 From: Fabio Niephaus Date: Wed, 9 Oct 2024 22:36:02 +0200 Subject: [PATCH] Fix and improve resource setup. --- .../graalwasm-embed-c-code-guide/README.md | 21 ++++++++++++++----- .../graalwasm-embed-c-code-guide/pom.xml | 16 ++++++++++++-- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/graalwasm/graalwasm-embed-c-code-guide/README.md b/graalwasm/graalwasm-embed-c-code-guide/README.md index ce5ba55..f6be66b 100644 --- a/graalwasm/graalwasm-embed-c-code-guide/README.md +++ b/graalwasm/graalwasm-embed-c-code-guide/README.md @@ -89,8 +89,7 @@ emcc --no-entry -s EXPORTED_FUNCTIONS=_floyd -o target/classes/com/example/floyd > The exported functions must be prefixed by `_`. If you reference that function in the Java code, the exported name should not contain the underscore. -It produces a standalone file _floyd.wasm_ in _target/classes/com/example_, which is where Maven puts resource files for the `com.example` package. -This lets us load the file as a resource from the `com.example.App` class. +It produces a standalone file _floyd.wasm_ in _target/classes/com/example/_, which lets us load the file as a resource. #### Using Maven to Compile C Code @@ -105,13 +104,25 @@ We can automate the C compilation and make it a part of the Maven build process 1.2.1 + create-output-directory + generate-resources + + exec + + + mkdir + -p ${project.build.outputDirectory}/com/example/ + + + + compile-c-into-wasm generate-resources exec emcc - --no-entry -s EXPORTED_FUNCTIONS=_floyd -o target/classes/com/example/floyd.wasm src/main/c/floyd.c + --no-entry -s EXPORTED_FUNCTIONS=_floyd -o ${project.build.outputDirectory}/com/example/floyd.wasm ${project.basedir}/src/main/c/floyd.c @@ -121,7 +132,7 @@ We can automate the C compilation and make it a part of the Maven build process ``` This binds the `exec-maven-plugin:exec` goal to the `generate-resources` phase of the build lifecycle. -The `exec` goal runs `emcc` with the same command line arguments as above, ensuring that the generated WebAssembly module file is included as a resource file in the final JAR. +The `exec` goal runs `mkdir` and `emcc` with the same command line arguments as above, ensuring that the generated WebAssembly module file is included as a resource file in the final JAR. ### 4. Using the WebAssembly Module from Java @@ -140,7 +151,7 @@ import org.graalvm.polyglot.Value; public class App { public static void main(String[] args) throws IOException { // Find the WebAssembly module resource - URL wasmFile = App.class.getResource("floyd.wasm"); + URL wasmFile = App.class.getResource("/floyd.wasm"); // Setup context Context.Builder contextBuilder = Context.newBuilder("wasm").option("wasm.Builtins", "wasi_snapshot_preview1"); diff --git a/graalwasm/graalwasm-embed-c-code-guide/pom.xml b/graalwasm/graalwasm-embed-c-code-guide/pom.xml index 78237fd..d54c41a 100644 --- a/graalwasm/graalwasm-embed-c-code-guide/pom.xml +++ b/graalwasm/graalwasm-embed-c-code-guide/pom.xml @@ -104,14 +104,26 @@ 1.2.1 - process-resources + create-output-directory + generate-resources + + exec + + + mkdir + -p ${project.build.outputDirectory}/com/example + + + + compile-c-into-wasm + generate-resources exec emcc --no-entry -s EXPORTED_FUNCTIONS=_floyd -o - ${project.build.outputDirectory}/com/example/floyd.wasm src/main/c/floyd.c + ${project.build.outputDirectory}/com/example/floyd.wasm ${project.basedir}/src/main/c/floyd.c