Skip to content

Commit

Permalink
Fix and improve resource setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Oct 9, 2024
1 parent d6ed3c8 commit a2986e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
21 changes: 16 additions & 5 deletions graalwasm/graalwasm-embed-c-code-guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -105,13 +104,25 @@ We can automate the C compilation and make it a part of the Maven build process
<version>1.2.1</version>
<executions>
<execution>
<id>create-output-directory</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mkdir</executable>
<commandlineArgs>-p ${project.build.outputDirectory}/com/example/</commandlineArgs>
</configuration>
</execution>
<execution>
<id>compile-c-into-wasm</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>emcc</executable>
<commandlineArgs>--no-entry -s EXPORTED_FUNCTIONS=_floyd -o target/classes/com/example/floyd.wasm src/main/c/floyd.c</commandlineArgs>
<commandlineArgs>--no-entry -s EXPORTED_FUNCTIONS=_floyd -o ${project.build.outputDirectory}/com/example/floyd.wasm ${project.basedir}/src/main/c/floyd.c</commandlineArgs>
</configuration>
</execution>
</executions>
Expand All @@ -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

Expand All @@ -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");
Expand Down
16 changes: 14 additions & 2 deletions graalwasm/graalwasm-embed-c-code-guide/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,26 @@
<version>1.2.1</version>
<executions>
<execution>
<phase>process-resources</phase>
<id>create-output-directory</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mkdir</executable>
<commandlineArgs>-p ${project.build.outputDirectory}/com/example</commandlineArgs>
</configuration>
</execution>
<execution>
<id>compile-c-into-wasm</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>emcc</executable>
<commandlineArgs>--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
</commandlineArgs>
</configuration>
</execution>
Expand Down

0 comments on commit a2986e4

Please sign in to comment.