Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

compile to JavaScript

compile to JavaScript

Hello,
I am trying to compile the example model Modelica.Blocks.Examples.PID_Controller to JavaScript using emscripten, as done and described by Tom Short here:
https://github.com/tshort/openmodelica-javascript
The resulting JavaScript should then be embedded in a markdown file as shown here:
https://github.com/tshort/mdpad/tree/gh-pages/
I installed all required tools on Xubuntu 16.04, emscripten had to be installed from source, because the version installed by apt-get from the Ubuntu servers produced a lot of warnings. When I now call a mos file with the following content:

Code:

loadModel(Modelica);

setCommandLineOptions("+simCodeTarget=JavaScript");
buildModel(Modelica.Blocks.Examples.PID_Controller);

a lot of files are produced, but not the main JS file.

Has somebody successfully compiled to JavaScript? Did I miss anything obvious?
I also filed an issue, describing what I have done so far here:
https://github.com/tshort/openmodelica- … t/issues/2

Thanks in advance,
Matthis

Edited by: matth - Jun-13-16 09:10:51

Re: compile to JavaScript

Hi
I have the same issue. I tried both on Windows and on Linux. With both OpenModelica 1.9.3 and OpenModelica 1.9.6.

The _node.js file is generated, but the direct .js file is missing. It is required by the _node.js file:

"
var mod = require('./Modelica.Electrical.Analog.Examples.ChuaCircuit.js');
"
There is a direct C file: "Modelica.Electrical.Analog.Examples.ChuaCircuit.c"  but that is no use when trying to generate a javascript file. I have followed exactly all the steps to install OpenModelica and emscripten, on both Windows and Linux. Has anyone else seen the problem? Found a way around this problem?

Thank you for your help,

Thierry

Re: compile to JavaScript

In the log file, the "pre.js" is missing. It is being looked for in an absolute path: /usr/lib/x86_64-linux-gnu/omc/emcc.
I have copied the pre.js file there. Now the compile stops by not finding four libraries:

WARNING  root: memcc: cannot find library "blas"
WARNING  root: memcc: cannot find library "lapack"
WARNING  root: memcc: cannot find library "expat"
WARNING  root: memcc: cannot find library "SimulationRuntimeC"

It may be the compiled libraries are not found: the .so.

It seems one cannot use an OpenModelica installed with apt-get, but it needs to be compiled from sources, to get the compiled libraries in the right path.

Re: compile to JavaScript

finding the various lib*.s and putting them exactly in the directory "/usr/lib/x86_64-linux-gnu/omc/emcc" has reduced the number of problems.

There is now a message stating the production of the js file has failed because there is no target:

"emcc -I. -o Modelica.Electrical.Analog.Examples.ChuaCircuit.js Modelica.Electrical.Analog.Examples.ChuaCircuit.o Modelica.Electrical.Analog.Examples.ChuaCircuit_functions.o Modelica.Electrical.Analog.Examples.ChuaCircuit_records.o Modelica.Electrical.Analog.Examples.ChuaCircuit_01exo.o Modelica.Electrical.Analog.Examples.ChuaCircuit_02nls.o Modelica.Electrical.Analog.Examples.ChuaCircuit_03lsy.o Modelica.Electrical.Analog.Examples.ChuaCircuit_04set.o Modelica.Electrical.Analog.Examples.ChuaCircuit_05evt.o Modelica.Electrical.Analog.Examples.ChuaCircuit_06inz.o Modelica.Electrical.Analog.Examples.ChuaCircuit_07dly.o Modelica.Electrical.Analog.Examples.ChuaCircuit_08bnd.o Modelica.Electrical.Analog.Examples.ChuaCircuit_09alg.o Modelica.Electrical.Analog.Examples.ChuaCircuit_10asr.o Modelica.Electrical.Analog.Examples.ChuaCircuit_11mix.o Modelica.Electrical.Analog.Examples.ChuaCircuit_12jac.o Modelica.Electrical.Analog.Examples.ChuaCircuit_13opt.o Modelica.Electrical.Analog.Examples.ChuaCircuit_14lnz.o Modelica.Electrical.Analog.Examples.ChuaCircuit_15syn.o -I"/usr/include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_EMCC -L"/usr/lib/omlibrary/Modelica 3.2.2/Electrical/Analog/Examples"     -Os -Wno-warn-absolute-paths    -L"/usr/lib/omlibrary/Modelica 3.2.2/Electrical/Analog/Examples" -L'/usr/lib/x86_64-linux-gnu/omc/emcc' -lblas -llapack -lexpat -lSimulationRuntimeC -s TOTAL_MEMORY=805306368 -s OUTLINING_LIMIT=20000 --pre-js /usr/lib/x86_64-linux-gnu/omc/emcc/pre.js
INFO     root: =======================================
INFO     root: bootstrapping relooper...
INFO     root:   bootstrap phase 1
/home/tcoq/emsdk_portable/clang/fastcomp/build_master_64/bin/lli: error creating EE: No available targets are compatible with this triple, see -version for the available targets.
FAIL: Running the generated program failed!
Traceback (most recent call last):
  File "/usr/share/emscripten/emcc", line 1864, in <module>
    final = shared.Building.emscripten(final, append_ext=False, extra_args=extra_args)
  File "/usr/share/emscripten/tools/shared.py", line 1276, in emscripten
    assert os.path.exists(filename + '.o.js') and len(open(filename + '.o.js', 'r').read()) > 0, 'Emscripten failed to generate .js: ' + str(compiler_output)
AssertionError: Emscripten failed to generate .js:
ERROR    root: bootstrapping relooper failed. You may need to manually create relooper.js by compiling it, see src/relooper/emscripten
Traceback (most recent call last):
  File "/usr/share/emscripten/emscripten.py", line 1352, in <module>
    _main(environ=os.environ)
  File "/usr/share/emscripten/emscripten.py", line 1340, in _main
    temp_files.run_and_clean(lambda: main(
  File "/usr/share/emscripten/tools/tempfiles.py", line 39, in run_and_clean
    return func()
  File "/usr/share/emscripten/emscripten.py", line 1348, in <lambda>
    DEBUG_CACHE=DEBUG_CACHE,
  File "/usr/share/emscripten/emscripten.py", line 1226, in main
    shared.Building.ensure_relooper(relooper)
  File "/usr/share/emscripten/tools/shared.py", line 1521, in ensure_relooper
    1/0
ZeroDivisionError: integer division or modulo by zero
Traceback (most recent call last):
  File "/usr/bin/emcc", line 1864, in <module>
    final = shared.Building.emscripten(final, append_ext=False, extra_args=extra_args)
  File "/usr/share/emscripten/tools/shared.py", line 1276, in emscripten
    assert os.path.exists(filename + '.o.js') and len(open(filename + '.o.js', 'r').read()) > 0, 'Emscripten failed to generate .js: ' + str(compiler_output)
AssertionError: Emscripten failed to generate .js:
make: *** [omc_main_target] Erreur 1
"

Anyone knows how to solve this?

Re: compile to JavaScript

"apt-get install emscripten" is a bad idea. It is required to install emscripten as described in the portable version, or some things will be missing.
The compilation now goes further and halts on a error in parse-js.js:

"
emsdk_portable/emscripten/master/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:272
        throw new JS_Parse_Error(message, line, col, pos);
        ^
TypeError: (intermediate value) is not a function
    at new JS_Parse_Error (emsdk_portable/emscripten/master/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:261:21)
    at js_error (emsdk_portable/emscripten/master/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:272:15)
    at croak (emsdk_portable/emscripten/master/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:742:17)
    at token_error (emsdk_portable/emscripten/master/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:749:17)
    at unexpected (emsdk_portable/emscripten/master/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:755:17)
    at block_ (emsdk_portable/emscripten/master/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:998:40)
    at emsdk_portable/emscripten/master/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:978:43
    at emsdk_portable/emscripten/master/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:982:29
    at emsdk_portable/emscripten/master/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:1112:43
    at maybe_unary (emsdk_portable/emscripten/master/tools/eliminator/node_modules/uglify-js/lib/parse-js.js:1209:27)
Traceback (most recent call last):
  File "emsdk_portable/emscripten/master/emcc", line 13, in <module>
    emcc.run()
  File "emsdk_portable/emscripten/master/emcc.py", line 1668, in run
    JSOptimizer.flush()
  File "emsdk_portable/emscripten/master/emcc.py", line 1573, in flush
    run_passes(chunks[i], 'js_opts_' + str(i), just_split='receiveJSON' in chunks[i], just_concat='emitJSON' in chunks[i])
  File "emsdk_portable/emscripten/master/emcc.py", line 1543, in run_passes
    final = shared.Building.js_optimizer(final, passes, debug_level >= 4, JSOptimizer.extra_info, just_split=just_split, just_concat=just_concat)
  File "emsdk_portable/emscripten/master/tools/shared.py", line 1651, in js_optimizer
    ret = js_optimizer.run(filename, passes, NODE_JS, debug, extra_info, just_split, just_concat)
  File "emsdk_portable/emscripten/master/tools/js_optimizer.py", line 538, in run
    return temp_files.run_and_clean(lambda: run_on_js(filename, passes, js_engine, source_map, extra_info, just_split, just_concat))
  File "emsdk_portable/emscripten/master/tools/tempfiles.py", line 64, in run_and_clean
    return func()
  File "emsdk_portable/emscripten/master/tools/js_optimizer.py", line 538, in <lambda>
    return temp_files.run_and_clean(lambda: run_on_js(filename, passes, js_engine, source_map, extra_info, just_split, just_concat))
  File "emsdk_portable/emscripten/master/tools/js_optimizer.py", line 483, in run_on_js
    assert proc.returncode == 0
AssertionError
make: *** [omc_main_target] Erreur 1

Still looking...

Re: compile to JavaScript

Hi,
I have the same issue, both on Windows 10 and Ubuntu 16.04.

In Windows 10 emcc command is not recognized when mos script is launched (emcc is in path).

In Ubuntu 16.04 the _node.js file is generated, but the direct .js file is missing.
The error in log is: ChuaCircuit.o is not valid LLVM bitcode.

Any suggestions?

Thank you very much,
Thomas

Re: compile to JavaScript

Hi Thomas,
I am also still interested in getting this to work.
Did you also read the github issue linked in the first post?
https://github.com/tshort/openmodelica- … t/issues/2
Especially, installing emscripten was a bit cumbersome.
Are you able to run the emscripten basic examples as described on the emscripten homepage?
http://kripken.github.io/emscripten-sit … nvironment

The error you mention also appeared on my machine after I changed the simCodeTarget.
What simcodeTarget did you define: C or Javascript??
Regards,
Matthis

Re: compile to JavaScript

Hi Matthis,
thank you for your suggestions.
Yes I was able to run emscripten basic examples (as described on the emscripten homepage), but I wasn't able to compile my modelica model.
I defined my simcodeTarget as Javascript.
Still trying...

Regards,
Thomas

Re: compile to JavaScript

We are currently also pursuing this topic. Any solution so far? Thanks!

Re: compile to JavaScript

I did not get it to work so far. If you do, I would be interested in instructions.

Re: compile to JavaScript

bolognesit wrote:


In Windows 10 emcc command is not recognized when mos script is launched (emcc is in path).

I found out the problem with Windows not finding the emcc command. The problem is with batch file "OpenModelica1.9.7\share\omc\scripts\Compile.bat". They override PATH environment variable on line 35


set PATH=%CD%;%CD%\..\libexec\gcc\mingw32\4.4.0\;



my solution:


set PATH=%CD%;%CD%\..\libexec\gcc\mingw32\4.4.0\;%OLD_PATH%;



After this you will be able to use emcc command (if your emcc works from clean command line)

The only problem left with my compilation was that the libraries in https://github.com/tshort/openmodelica-javascript are starting with "lib*" and emcc expects just "l*" (small L). After that the compilation threw some error, but the JS file was properly generated.

Edited by: euardF - Jan-15-17 14:09:30

Re: compile to JavaScript

thank you for your suggestions.
Yes I was able to run emscripten basic examples (as described on the emscripten homepage), but I wasn't able to compile my modeling model.

goldenslot

There are 0 guests and 0 other users also viewing this topic
You are here: