Compile Gccpy
Gccpy isn't part of mainline gcc, so you have to check it out from the version control branch.
$ git clone git://gcc.gnu.org/git/gcc.git $ git checkout --track -b gccpy origin/gccpy
Now you have checked out the code we need to start a gcc build:
$ apt-get install bison flex build-essential libgmp-dev libmpfr-dev libmpc-dev
Remember _never_ build gcc within the same checkout driectory!
$ cd ../ $ mkdir gccpy-build $ cd gccpy-build $ ../gccpy/configure --enable-languages=c,python --disable-bootstrap $ make $ make install
Tested on: Ubuntu 1204, centos 6.3 and Mac OSX.
Development
I tend to use redbrain-github for all my development but i the Git branch in the GCC repo is the main base where you should work from.
Using
When compiling python code there are several loop holes nessecary. As Python has no reserved 'main' method like C or JAVA, it simply starts where you told the interpreter to we have to compile our code such that we 'compile in' a main method using -fpy-gen-main. There is also another option -fpy-dump-dot which generates textual output of the DOT IL and the types generated for each module and class 'gccpy-il.dot gccpy-types.dot' these are for debugging use only.
$ $EDITOR foobar.py $ gccpy -g -O2 -fpy-gen-main -fpy-dump-dot foobar.py -o foobar $ ./foobar
Imports and Linking
Consider the follwing python code in 2 files:
# foo1.py import foo2 print foo2.test (1)
And the import:
# foo2.py def test (x): return x + 1 print test (5)
As you would expect on initial import of foo2 the test funcion would go into the global namespace and the function would be called with the argument 5 only once because of the import. When we compile these we need to think of the code with dependancies like you would with C/C++.
Meaning you will have to compile foo2.py before you compile foo1.py and then link them both together example:
all: gccpy -O0 -fdump-tree-gimple -fpy-dump-dot -c foo2.py -o foo2.o gccpy -O0 -fdump-tree-gimple -fpy-dump-dot -fpy-gen-main -c foo1.py -o foo1.o gccpy -O0 -o test foo1.o foo2.o
This compiles each python file creating the foo1.py as the main, and links both together. You then run the program via $ ./test. Whats interesting is that when compiling these files the each generate an export file:
$ cat foo*.export.gpyx MODULE "foo1" { HAS_MAIN True ENTRY "foo1.__main_start__" } MODULE "foo2" { HAS_MAIN False ENTRY "foo2.__main_start__" }
This is required such that the importer knows how to call into the importee in this case foo2.py as a user you should never have to care about these.