Java – Is it viable to make a port from a C++ application to Java through LLVM

cjavaporting

how viable is it to port a C++ application to Java bytecode using LLVM (I guess LLJVM)?

The thing is that we currently have a process written in C++ but a new client has made mandatory to been able to run the program in a multiplatform way, using the Java Virtual Machine with obviously no native code (no JNI). The idea is to be able to take the generated jar and copy then to different systems (Linux, Win, 32 bits – 64 bits) and it should just work.

Looking around looks like it is possible to compile C++ to LLVM IR code and then that code to java bytecode. There is no need of the generated code to be readable.

I have test a bit with similar things using emscripten, this takes C++ code and compile it to JavaScript. The result is valid JS but totally unreadable (looks like assambler).

  • Does anybody done a port of an application from C++ to Java bytecode using this tecnique?
  • What problems could we face?
  • Is a valid approach for production code?

To make more clear my point after some comments, maybe port is not well used, I do not expect readable source code as a result, just java bytecode, so it is not a 'port' which will be developed for anymore, just that the target plattform must be the java JVM not the native assamblear.

Note: I am aware that currently we have some non standard C++ and close source libraries, we are looking to removing this non standard code and all close source libraries and use Free Libre Open Source Software, so lets suppose all code is standard C++ code with all code available at compile time.

Note2: It is not an option to write portable C++ code and then compile it to the desired target platform, the compiled program must be multiplatform, thus the use of JVM .

Note3: Right now we are not looking into similar solutions applied to Python or other language base, but i would also like to heard about it. With this I mean that our target executable must be java bytecode but if there are options to compile C++ to valid python compiled code I would also like to hear about them.

Best Answer

I really doubt this will work. You might be able to translate your code into Java byte code, but it will not magically translate library calls into equivalent calls to the Java runtime and libraries. There may not even be equivalent Java runtime calls! Even if you eliminate all proprietary libraries you're still left with the C++ standard library.

To make this concrete: your C++ program may contain a call to fprintf(). That function is implemented in the C standard library and it's perfectly legit for a C++ program to call it. The LLVM to LLJVM translator is probably not going to magically figure out the sequence of Java run time calls that will produce the equivalent result to fprintf() and substitute those in. To provide that facility would require essentially reimplementing the C and C++ runtimes in Java byte code.

There are some tools that perform C++ to Java translation but they only convert a handful of the simpler runtime library calls. The rest are left to you to figure out.

Related Topic