Saturday, December 12, 2015

Build Python from source

Build Python from source

Here is the regular buiding process of python from source after you download the source tarball

Python is built from source using standard autotools. For example, to install it in /usr to override the old python2
tar xzvf Python-2.7.11.tgz
cd Python-2.7.11
./configure --prefix=/usr --enable-shared --with-system-ffi --enable-unicode=ucs4
make
sudo make install
This will build IDLE, too. (The --enable-shared option is needed if you ever build Python extension modules or other programs that link with Python.)

Important!
If you have an old python in your system and you want to install the new version to override the old one, you should remove the old lib files(/usr/lib/python_dir, /usr/local/lib/python_dir) in case that when you execute make and it issues an error like this:
above simple shows an error make: *** [libinstall] Error 1 without telling any useful information about what causes this error.

NOTE:
If you want to check configure options before you set the configuration, type ./configure --help

As you see in the above example, we specify a --enable-shared option, this will
generate a shared lib namely libpython2.7.so in /usr/lib/ directory, and if you don't specify this option, you may encounter compile errors when other libraries is build with python support.
I encountered this error when I compile YouCompleteMe library:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libpython2.7.a(abstract.o): relocation R_X86_64_32S against `_Py_NotImplementedStruct' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libpython2.7.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
ycm/CMakeFiles/ycm_client_support.dir/build.make:408: recipe for target '/home/xxx/.vim/bundle/YouCompleteMe/third_party/ycmd/ycm_client_support.so' failed
make[3]: *** 
...

the above error simply means it can't find libpython2.7.so, so it links to a static library which is libpython2.7.a, however it found bad values (e.g. undefined variable names). To solve this, rebuild python by specifying option  --enable-shared

Another, if you don't want to rebuild python and you know you have a libpython2.7.so in other directories(e.g. /usr/lib/x86_64-linux-gnu/libpython2.7.so), you can create a symbolic link like this: ln -s /usr/lib/x86_64-linux-gnu/libpython2.7.so /usr/lib/libpython2.7.so


It is recommended that you build with libffi dependency: --with-system-ffiAlso, you can also specify options to build OpenSSL-1.0.2e, SQLite-3.9.2 and Tk-8.6.4 Modules,

--enable-unicode=ucs4
By default Python is build with ucs2 for Unicode, however almost all linux distribution overrides it and uses ucs4. If you have a vim that is built with ucs4, and then you build python with ucs2 (by default), you will get an error like this:
error:
vim: symbol lookup error: vim: undefined symbol: PyUnicodeUCS4_AsEncodedString

Some other options:
--enable-ipv6           Enable ipv6 (with ipv4) support
--with-dbmliborder=db1:db2:...
                          order to check db backends for dbm. Valid value is a
                          colon separated string with the backend names
                          `ndbm', `gdbm' and `bdb'.
--with-fpectl           enable SIGFPE catching

after you execute make, you can do make -k test to test the result, and then do make install


Other options:

--with-system-expat: This switch enables linking against system version of Expat.
--with-system-ffi: This switch enables linking against system version of libffi. Remove if you have not installed libffi-3.2.1.
--enable-unicode=ucs4: This switch enables 32bit Unicode support in Python.


here is an nice script that can simplify the process

5 comments: