Compiling Java to Native with XMLVM, Linux and BoehmGC
Actually I watched XMLVM for quite a while now. XMLVM makes it possible to write iPhone-Applications in Java by converting the Class-Files to XML and this XML to ObjectiveC. Crazy, isn’t it? There is even a way to write an Android-Application (you use a subset of the Android-API) and then compile that to iPhone.
Now there is a new target:POSIX that let you transform your java-code to c including some kind of garbage-collector. All you have to do is to write: make! Well, theoretically! This seems to work on the mac as it is still the main task to ‘crosscompile’ java/xmlvm to iPhone/iOS. Actually I don’t have a Mac but would like to compile my java application using Linux. Here a short instruction how I got it work.
1.Get XMLVM( http://xmlvm.org/overview/ ):
In the console type:
svn co https://xmlvm.svn.sourceforge.net/svnroot/xmlvm/trunk/xmlvm xmlvm
2.Rename boehmgc.jar:
cd xmlvm
mv lib/boehmgc.jar lib/boehmgc.jar__
You ask why? Somehow using this version of boehmgc doesn’t work for me and renaming this will prevent that lots of header- and c-files are extracted to your converted source. I read about a -DXMLVM_NO_GC Command but I wasn’t sure where to use this…(using id with during ant-compile didn’t work for me)
3. Create xmlvm.jar
ant
4. get boemgc
cd ..
cvs -d:pserver:anonymous@bdwgc.cvs.sourceforge.net:/cvsroot/bdwgc co bdwgc
5. get cmake/cmake-gui
In order to compile boemgc with the right options using cmake is the easiest way. So if you don’t have it already get cmake:
apt-get install cmake cmake-gui
6.create Makefiles:
- the path where we just downloaded the boehmgc-sourcecode
- the build-path were we create the makefile
- Press Configure and specify “Unix Makefile” as generator and leave default native compiler checked
- check enable_cplusplus
- check enable_parallel_mark
- Generate (creates a makefile in the specified directory)
(Just for Info: I was not sure if threads are needed to be checked here. Keep it in mind…)
Change to the build-directory:
make
This will create two files. Two(?) static libs (libgc-lib.a, libgcmt-lib.a) and one shared-library(libgcmt-dll.so). In this example I will only use the shared library nevertheless lets copy that libraries to /usr/lib:
sudo cp lib* /usr/lib
ldconfig -n /usr/lib
and copy the header to /usr/include
sudo mkdir /usr/include/gc
sudo cp -R include/* /usr/include/gc
Might be possible that you usually have to use /usr/local/include for that but somehow that path was not found for dynamic linking…(hey, I’m a java-coder! And I have only a bit of a clue about what I’m doing here )
7. Install gobjc++
In order to compile some of the ObjectiveC-files we need gobjc:
sudo apt-get install gobjc++ gobjc-4.5
Might be possible that gobjc++ is enough! I first tried only gobjc-4.5 which let the compiler cry for ‘cc1obj’
8. Create a simple Java-Project
Let’s start testing with a very simple project…(press the Image for full-screen)
If you want to use this project: http://thomas.trocha.com/misc/XMLVM_Test.tar.gz
9. Convert java to c
Now go to the console and go into the root-directory of your simple test-project.
In order to convert the code to c use following line(don’t forget to put in the path to your xmlvm-directory):
java -jar YOURPATH/dist/xmlvm.jar --target=posix --in=bin --out=c
This will create a new folder c (–out=c) and use the posix-target which will copy lots of dependencies also to that directory. All may be compiled with the Makefile located in c/dist.
10. Some changes at Makefile and native_jave_io_File.c
BUT first we need to add something to the Makefile (because we have to use our own boehmgc):
Open the Makefile in an editor (e.g. vim) and additional header-directories and libraries so it looks afterwards like this:
CC=gcc
CFLAGS=-w -std=c99 -I$(SRCROOT) -I/usr/include/gc -I/usr/include/gc/private
LD=$(CC)
LDFLAGS=-L/usr/lib -lgcmt-dll -lpthread
In CFLAGS we added the directories to the garbage-collector and in LD-Flags the shared-library of the garbage-collector plus pthreads. Actually I think a static link would be much better here but I didn’t find out how to get this work. Got still “unreferenced….”-errors.
Next open ../src/native_java_io_File.c go to Line 94 and replace:
return buf.st_mode & S_IFDIR;
with
return buf.st_mode & __S_IFDIR;
Info: S_IFDIR is somehow not defined in sys/stat.h so this is something like a dirty fix! Actually I’m not sure if it is a fix at all. At least it compiles…
11. Lets MAKE it
Ok. Now let’s try it:
make
If you went through the make-process without any error you can enter the c/build-directory and start your application named c. My demo-application looks like this:
I hope that instructions was for some people helpful. Actually I don’t have any experiences with XMLVM yet and I have quite few experience with C/C++. If you got it compiled right now you have the same knowledge than me :D. Usually I should have worked on my android-game but that is postponed until tomorrow
Keep on rocking, yours dertom