Can anyone tell me how to compile the Clang compiler into LLVM bytecode (that is, self-host Clang)? The reason I want to do this is so that I can take the resulting LLVM bytecode and then use Emscripten to produce a C to Javascript compiler.
Asked
Active
Viewed 616 times
1
-
Use -flto and then collect the resulting IR modules into a large single module. – SK-logic Sep 08 '14 at 14:34
1 Answers
3
You can get clang to output into LLVM bytecode by using the -emit-llvm
command-line flag, along with the -c
flag. (If you use the -S
flag instead of -c
, you get a textual representation of the LLVM bytecode.) You don't need to compile clang into LLVM bytecode for that to work.
If you want to try to run clang itself inside a browser, then you will need to compile all of clang into LLVM bytecode, and then link the object files together using llvm-link
. Then you'll need to figure out how to give the compiled compiler access to the system header files it needs. I don't know if there is a build option for all that, but I haven't ever seen anything in the ./configure
options for that, so I suspect not. But it's possible that it exists.

rici
- 234,347
- 28
- 237
- 341
-
There's a similar thread at http://stackoverflow.com/questions/12479458/how-to-build-clang-with-clang, but I'm a newbie to clang and compiling in general so I have no idea how to adopt it to compiling clang to LLVM. Do you think you could help me change this code slightly to compile into LLVM? – user3059347 Sep 08 '14 at 00:44
-
As I said in the answer, it's not a trivial process. I didn't mention that if you wanted to do all that in the browser, you'd also need to build emscripten itself, and that the result would mostly likely be impractically large. Are you certain that you need to do it? – rici Sep 08 '14 at 01:36