-
Notifications
You must be signed in to change notification settings - Fork 0
Getting started
You will need to get and build LLVM and Clang (or, instead of Clang you can use llvm-gcc). You should really read their docs, but here is a short sketch:
- Get LLVM 2.9 (other versions may not work)
- Get Clang 2.9 and place it in your LLVM directory under tools/clang (note: not clang-2.9 or anything like that - simply 'clang')
- Make a
cbuild/directory inside the LLVM (not Clang) directory - In
cbuild/, runcmake ..(or../configure) and thenmake. This will build LLVM and Clang.
If you want to use llvm-gcc instead of Clang, do the above except for the part about Clang, then do this:
- Get LLVM-GCC 2.9
- As before make a cbuild directory inside it
- In
cbuild/run../configure --prefix=pwd/install --program-prefix=llvm- --enable-llvm=ABSOLUTE_PATH_TO_LLVM/cbuild --enable-languages=c,c++ --target=i686-pc-linux-gnu --with-tune=generic --with-arch=pentium4for x86. Or, this seems to work for x86_64:../configure --prefix=pwd/install --program-prefix=llvm- --enable-llvm=/home/alon/Dev/llvm-2.9/cbuild --enable-languages=c,c++ --enable-checking. - Note: Must run configure with flex and bison already installed
- Note: Need ABSOLUTE path as shown there!
- Then do
make. Takes a very long time. - Then
make install. Don't forget this, it sets up acbuild/build-i686-linux[..](or similar) directory, with the actual gcc, g++ etc. - Note that you will need to edit the emscripten test settings file at tests/settings.py, commenting out the line with Clang and uncommenting the one with llvm-gcc.
Note: LLVM 2.7 and 2.8 may also work, but currently all testing is done on 2.9.
Next you need a JavaScript engine like SpiderMonkey or V8. Get one of those and build it. Or better yet get both.
Note that you should get a recent version - from hg for SpiderMonkey, and svn for V8.
- For V8, build it with
scons d8to build the d8 shell.
- In the top directory, run
python tests/runner.py. That will run all the tests. They should all pass. This will take a long time - we have a lot of tests! - Note that the first time you run the tests (or emscripten.py), it will create a config file, ~/.emscripten. It's initial values are taken from tests/settings.py. You should edit the paths in ~/.emscripten to those relevant for you, and other settings therein.
- You can do
python tests/runner.py clang_0_0.test_Xwhere X is one of the tests in runner.py (for example,intvarsorfannkuch). This will run that single test. You can then view the generated code by going to the temp directory mentioned in settings.cfg, and looking atsrc.cpp.o.js.- clang_0_0 in the example above defines how to compile it. First is the compiler name, clang or llvm_gcc. Then whether to run LLVM optimizations (1 for yes, 0 for no), and whether to optimize&reloop (1 for yes, 0 for no).
Run
./emscripten.py INFILE PATH-TO-JS-ENGINE
where INFILE is the path of the file you want to compile. The file should contain LLVM bitcode in human-readable assembly form (that is, as text, and probably beginning with something like ; ModuleID =). Note: the .ll file must have annotations, use -show-annotations in llvm-dis. PATH-TO-JS-ENGINE is the JavaScript engine to use to run the compiler.
The output will be sent to stdout, so you might want to add > OUTFILE to send it to a file. You can then run that file in a JavaScript engine.
If you have issues with compiling or with the generated code, see the Debugging page.
By default the generated code is meant to run in a console engine, which is useful for the automated tests (for example, arguments is then equal to the commandline arguments). To run that code in a browser, a few trivial changes should be done. For an example, see demos/cubescript.html and demos/cubescript.js. The basic steps are:
- Define
arguments(which in the console would be the commandline arguments), say as[](just because the code expects it to exist). - Likely you will want to disable the call to
run()or call it yourself at the right time. - Connect your web JavaScript to the Emscriptened JavaScript. Currently this must be done manually. Again, see the cubescript demo mentioned before, but in general
- Calling Emscriptened code requires you to use the name-mangled function names (
thirdparty/demangler.pycan help unmangle them). - Parameters need to be converted. Integers and floats should be fine, but strings need to go through
Pointer_make(intArrayFromString(...))(which makes a string into an int array, then copies that to the heap and returns a pointer to it). Arrays should go throughPointer_make(...). - Optionally, provide some
print()function, if you expect the code to call that function to show output.