diff --git a/.gitignore b/.gitignore index e2644d5ecc..17c95d3b7b 100644 --- a/.gitignore +++ b/.gitignore @@ -265,4 +265,7 @@ adlittle.lp qjh.mps *.whl -bazel* \ No newline at end of file +bazel* + +# webdemo +build_webdemo \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f103cdce7..d36d192bf3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,9 @@ option(PYTHON "Build Python interface" OFF) option(FORTRAN "Build Fortran interface" OFF) option(CSHARP "Build CSharp interface" OFF) +# emscripten +option(EMSCRIPTEN_HTML "Emscripten HTML output" OFF) + # set the correct rpath for OS X set(CMAKE_MACOSX_RPATH ON) diff --git a/README.md b/README.md index aa0a859eb0..12b1556b3d 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,24 @@ Javascript HiGHS can be used from javascript directly inside a web browser thanks to [highs-js](https://github.com/lovasoa/highs-js). See the [demo](https://lovasoa.github.io/highs-js/) and the [npm package](https://www.npmjs.com/package/highs). +Alternatively, HiGHS can directly be compiled into a single HTML file and used +in a browser. This requires `emscripten` to be installed from their website +(unfortunately, e.g. `sudo apt install emscripten` in Ubuntu Linux is broken): + + https://emscripten.org/docs/getting_started/downloads.html + +Then, run + + sh build_webdemo.sh + +This will create the file `build_webdemo/bin/highs.html`. For fast edit +iterations run + + find src app | entr -rs 'make -C build_webdemo highs; echo' + +This will rebuild `highs.html` every time a source file is modified (e.g. +from Visual Studio Code). + Python ------ diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index d6b1556a43..d02a07f3c3 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -10,6 +10,11 @@ endif() target_link_libraries(highs libhighs) +if(EMSCRIPTEN AND EMSCRIPTEN_HTML) + set(CMAKE_EXECUTABLE_SUFFIX ".html") + set_target_properties(highs PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/highs_webdemo_shell.html) +endif() + target_include_directories(highs PRIVATE $ ) diff --git a/app/highs_webdemo_shell.html b/app/highs_webdemo_shell.html new file mode 100644 index 0000000000..9ad4995092 --- /dev/null +++ b/app/highs_webdemo_shell.html @@ -0,0 +1,73 @@ + + + + + + + HiGHS + + +
+ + + +
+ {{{ SCRIPT }}} + + + \ No newline at end of file diff --git a/build_webdemo.sh b/build_webdemo.sh new file mode 100644 index 0000000000..682715c703 --- /dev/null +++ b/build_webdemo.sh @@ -0,0 +1,25 @@ +script_path=$(realpath $(dirname $0)) +build_dir="build_webdemo" + +# interesting for Node.js: -sNODERAWFS=1, allows direct os filesystem access +LDFLAGS=$(echo $(cat << EOF +-sINVOKE_RUN=0 +-sMODULARIZE=1 +-sEXPORTED_RUNTIME_METHODS=['FS','callMain'] +-sEXPORT_NAME='HiGHS' +-sSINGLE_FILE +-sALLOW_MEMORY_GROWTH=1 +--shell-file ${script_path}/app/highs_webdemo_shell.html +EOF +)) + +# compare https://stackoverflow.com/a/6481016 +physical_cores=$(grep ^cpu\\scores /proc/cpuinfo | uniq | awk '{print $4}') + +cd ${script_path} && +rm -rf ${build_dir} && +mkdir ${build_dir} && +cd ${build_dir} && +LDFLAGS=$LDFLAGS emcmake cmake .. \ +-DCMAKE_BUILD_TYPE=Release -DEMSCRIPTEN_HTML=on && +emmake make -j ${physical_cores} \ No newline at end of file