-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[emscripten] build using emscripten possible
- Loading branch information
Nicolas Tran
committed
Dec 14, 2020
1 parent
42b75a6
commit 323a8b3
Showing
12 changed files
with
196 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Planning in the browser | ||
- Tested with emscripten version 2.0.8 | ||
|
||
## Building the JS+WASM files: | ||
|
||
- make sure the emscripten sdk is installed and executables (mainly em++ and emcmake) are on the PATH. | ||
|
||
- cmake needs to be installed | ||
|
||
- you can use the `./configure_cmake.sh` file to configure cmake for an emscripten or native build using `./configure_cmake.sh emscripten` or `./configure_cmake.sh native` respectively. | ||
|
||
- use above mentioned to switch to emscripten build type, this executes `cmake` with custom parameters: `CMAKE_CXX_COMPILER=em++` and `EMSCRIPTEN=1`. | ||
|
||
- build the WebAssembly and Javascript files using `make` in the `/src` directory, output files can be found in `src/bin`. | ||
|
||
- as the `CMAKE_CXX_COMPILER` stays to be `em++`, you need to change it back in case you want to build the native version. The configuration script assums `g++` to be your default native compiler. | ||
|
||
## Using the JS+WASM files: | ||
|
||
- instead of using STDIN to pass the translated `output.sas` file to the program, we added the option to use the `--input` argument, which takes a file handle and treats it as input stream | ||
|
||
- thus, before calling the search, but after the Module was loaded, we need to save the translated output file on the browsers virtual filesystem | ||
|
||
- There are multiple ways to do this, an example however can be found under `usage_example.js` loaded at the end of `index.html`. The example assumes `output.sas`, `downward.js` as well as `downward.wasm` files in the working directory | ||
|
||
- the `runFastDownward()` function executes the example. Make sure to have added the necessary files in beforehand. | ||
|
||
## Debug | ||
|
||
- you can use the included function `Module.getExceptionMessage(pointer)` to get a more detailed exception message in case you need it for debugging: | ||
in the `downward.js` file, you need to replace the original exception logging by the result of `Module.getExceptionMessage(pointer)`. I.e. replace `err('exception thrown: ' + toLog);` by | ||
`err('exception thrown: ' + Module.getExceptionMessage(toLog));` | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/bin/sh | ||
cd "$(dirname "$0")/../" | ||
if [ $# -eq 0 ]; then | ||
echo "No arguments provided" | ||
echo "Run either with " | ||
echo "./configure_cmake.sh emscripten" | ||
echo "or " | ||
echo "./configure_cmake.sh native" | ||
exit 1 | ||
fi | ||
|
||
if [ "$1" = "emscripten" ]; | ||
then | ||
echo 'setting cmake settings for building with emscripten' | ||
# in case of switch we need to execute twice, as cmake notices changed variables and executes itself again but fails as EMSCRIPTEN variable is not passed | ||
emcmake cmake -DEMSCRIPTEN=True -DCMAKE_CXX_COMPILER=$(which em++) . | ||
emcmake cmake -DEMSCRIPTEN=True -DCMAKE_CXX_COMPILER=$(which em++) . | ||
elif [ "$1" = "native" ]; | ||
then | ||
echo "setting cmake settings for native build" | ||
# in case of switch we need to execute twice, as cmake notices changed variables and executes itself again but fails as EMSCRIPTEN variable is not passed | ||
cmake -DEMSCRIPTEN=False -DCMAKE_CXX_COMPILER=$(which g++) . | ||
cmake -DEMSCRIPTEN=False -DCMAKE_CXX_COMPILER=$(which g++) . | ||
else | ||
echo "argument not recognized" | ||
echo "Run either with " | ||
echo "./configure_cmake.sh emscripten" | ||
echo "or " | ||
echo "./configure_cmake.sh native" | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
|
||
<title>Planning in the browser</title> | ||
</head> | ||
<body> | ||
<h1>Planning in the browser</h1> | ||
</body> | ||
|
||
<script type="text/javascript" src='usage_example.js'></script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
FILE_WASM='downward.wasm' | ||
FILE_JAVASCRIPT='downward.js' | ||
PATH_TO_INPUT='output.sas' | ||
|
||
// Load the javascript file containing the Downward module | ||
downwardscript = document.createElement('script'); | ||
downwardscript.src = FILE_JAVASCRIPT; | ||
downwardscript.onload = function() { | ||
// Fetch input data via XHR and save it on the browsers virtual filesystem once it is loaded | ||
console.log("Fetching data from " + PATH_TO_INPUT); | ||
var inputURL = PATH_TO_INPUT; | ||
var inputXHR = new XMLHttpRequest(); | ||
inputXHR.open('GET', inputURL, true); | ||
inputXHR.responseType = 'text'; | ||
inputXHR.onload = function() { | ||
if (inputXHR.status === 200 || inputXHR.status === 0) { | ||
let data = new TextEncoder().encode(inputXHR.response); | ||
let stream = FS.open('output.sas', 'w+'); | ||
FS.write(stream, data, 0, data.length, 0); | ||
FS.close(stream); | ||
console.log('wrote to output.sas'); | ||
} | ||
} | ||
inputXHR.send(); | ||
} | ||
document.body.appendChild(downwardscript); | ||
|
||
// function to start the actual program | ||
function runFastDownward() { | ||
// define parameters and split them to a list | ||
let parameter_string = "--search astar(lmcut()) --input output.sas" | ||
let parameter_list = parameter_string.split(" ") | ||
|
||
Module.callMain(parameter_list); | ||
|
||
// results are saved on the virtual filesystem in the browser | ||
let result = new TextDecoder().decode(FS.readFile('sas_plan')); | ||
console.log(result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters