Porting Python
Python is a popular scripting language that, if ported, can allow you to do a lot of powerful tasks using simple scripts. I gained a lot from this and this. Amazing what a simple Google search can do!
Prerequisites
Porting Python is not for the faint of heart. You should know a lot about configure scripts and the options of your toolchain.
You will also need a cross toolchain that supports C++ (See OS Specific Toolchain) and has a C++ standard library.
The Process
Download the Python 2.5 source code. You need to patch it using this patch in order to make the configure process work properly for the cross-compile.
Basically, this patched configure script now uses two variables: HOSTPYTHON and HOSTPGEN in the later stages of the build. See, Python's build process builds Python itself, then uses the built Python binaries to build the modules. In the case of a cross-compiler, this won't work, so we need to point Python to a working Python binary.
Run:
./configure && make python Parser/pgen
in the Python-2.5 directory.
This will build a version of Python using your host compiler.
Now we need to make that the host Python and get ready for the real cross compile:
mv python hostpython mv Parser/pgen Parser/hostpgen make distclean
This is where things get complicated. We now have to configure for the new target. Before you do this, export CC, CXX, AR and RANLIB for your target. In my case, I'll use "i686-myos":
./configure --host=i686-myos --prefix=/usr/python-cross
If things go wrong, you need to make sure your libc and libstdc++ provides the necessary functionality.
At this stage, Python is configured to use your cross toolchain. Now we build Python:
make python Parser/pgen
Right, that wasn't so hard, was it? So now you have the Python binary ready to run on your OS. However, it's missing libraries and modules. Let's build them now:
make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED="i686-myos-gcc -shared" CROSS_COMPILE=yes
You will need to edit "setup.py" to modify the default include and libraries directories to those which your compiler uses.
Basically, the Makefile now calls "$HOSTPYTHON ./setup.py" to continue the build. That's why we need to set HOSTPYTHON now. Another important thing to mention is BLDSHARED. If your cross-toolchain can't build shared objects, you need to use one that can. If you're on Linux and you're using ELF in your OS, just set BLDSHARED="gcc -shared". If not, you're out of luck until your cross-toolchain can build shared objects.
Your modules will now build. Keep track of error messages if you can, there will be modules that won't work until you add more functionality to your libc.
To install, run:
make install prefix=<install_dir> HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED="i686-myos-gcc -shared" CROSS_COMPILE=yes
And you're done!
Caveats
You will need dlopen, dlsym and dlclose to be able to do things like "import socket" which indirectly import "_socket" (<prefix>/lib/python2.5/lib-dynload/_socket.so). By this stage you should already have shared object support so this should be simple!
Final Word
Porting Python is really not that complex if you just work around some minor complexities that the build process uses. Good luck!