Python Un*x build with a Python shared library

Ulrich Berning berning at teuto.de
Tue May 15 17:57:39 EDT 2001


Oleg Broytmann wrote:

> On Tue, 15 May 2001, Frederic Giacometti wrote:
> > Changes requested on the current Python 2.1 source distribution:
> > ==========================================
> > Changes should be required in only two location:
> >     - main Python Makefile
> >     - the distutil code that links the dynamic extensions.
>
>    Nice to see someone returned to the issue. The following shell script
> works for me perfectly (Linux, of course):
>
> -----
> #! /bin/sh
>
> # For Linux systems, the simplest method of producing libpython.so seems to be
> # (originally from the Minotaur project web page, http://mini.net/pub/ts2/minotaur.html)
>
> make distclean
> OPT="-fpic -O2" CPPFLAGS=-I/usr/local/include/ncurses mconfig $*
> make || exit 1
>
> mkdir .extract
> (cd .extract; ar xv ../libpython2.1.a)
> gcc -shared -o libpython2.1.so .extract/*.o
> rm -rf .extract
> -----
>
>    But of course it'd be nice to allow Distutils recognize a platform and
> do appropriate build.
>
> Oleg.
> ----
>      Oleg Broytmann           http://phd.pp.ru/            phd at phd.pp.ru
>            Programmers don't die, they just GOSUB without RETURN.

Hi,

we use Python embeded in CATIA (A high end CAD system) for over three years now.
We use Python to build EDM/PDM systems inside the CAD system.

The only way, to extend CATIA is to build shared modules, that are loaded by CATIA
when needed.
On the the other side, we have wrapped hundreds of CATIA API functions into a
shared Python module.

So you have CATIA that loads a shared CATIA module that starts a Python interpreter
that loads a shared Python module. This can't be done if you don't have a shared
Python library.

We have created shared libraries for the follwing systems (the systems where CATIA runs):

AIX4, HP-UX, IRIX64, SunOS

For applications, that run outside CATIA, we create executable binaries and install the
binaries together with the compiled Python modules at customer side. So our customers
does not have any source code available (except customizeable modules with constants or
database connection parameters etc.). This is also done for Linux and the Win32 platforms.

So for me, it makes really sense to build libpython as a shared library by default for
every
platform that supports shared libraries.

I use the following script to build a shared library for Linux and relink the python
executable
(assuming we are in the root source tree):

-8<------------------------------------------------->8-
#!/bin/bash

user=`whoami`

if [ $user != 'root' ]; then
  echo "Try again as root ..."
  exit 1
fi

echo "Creating temporary directory '.extract' ..."
mkdir .extract

echo "Extraction object files from static library ..."
(cd .extract; ar xv ../libpython2.0.a)

echo "Creating shared library 'libpython2.0.so.0' ..."
gcc -shared -o libpython2.0.so.0 .extract/*.o

echo "Removing temporary directory '.extract' ..."
rm -rf .extract

echo "Installing shared library 'libpython2.0.so.0 in /usr/lib ..."
cp libpython2.0.so.0 /usr/lib/
strip /usr/lib/libpython2.0.so.0
chmod 555 /usr/lib/libpython2.0.so.0
ln -sf /usr/lib/libpython2.0.so.0 /usr/lib/libpython2.0.so
ldconfig
rm -f libpython2.0.so.0

echo "Linking new python executable with shared library ..."
gcc -Xlinker -export-dynamic Modules/python.o -o python_shared -lpython2.0 -lpthread -ldl
-lutil -lm

echo "Removing old executables in /usr/bin ..."
rm /usr/bin/python
rm /usr/bin/python2.0

echo "Installing new binary in /usr/bin ..."
cp python_shared /usr/bin/python2.0
strip /usr/bin/python2.0
ln -f /usr/bin/python2.0 /usr/bin/python
rm -f python_shared

echo "Calling ldd on new executable to check the use of the shared library ..."
ldd /usr/bin/python2.0

echo "All done ..."
-8<------------------------------------------------->8-

and the following to build a shared library for AIX and relink the python executable
(assuming we are in the root source tree):

-8<------------------------------------------------->8-
#!/bin/ksh

user=`whoami`

if [ $user != 'root' ]; then
  echo "Try again as root ..."
  exit 1
fi

echo "Creating temporary directory '.extract' ..."
mkdir .extract

echo "Creating a shared object from the static library ..."
ld -o .extract/shr.o libpython2.0.a -bnoentry -bM:SRE -bE:Modules/python.exp -H512 -T512
-ldl -lm -lc
chmod 555 .extract/shr.o

echo "Creating shared library 'libpython2.0.so' ..."
ar vr libpython2.0.so .extract/shr.o

echo "Removing temporary directory '.extract' ..."
rm -rf .extract

echo "Installing shared library 'libpython2.0.so' as 'libpython2.0.a' in /usr/local/lib
..."
slibclean
cp libpython2.0.so /usr/local/lib/libpython2.0.a
chmod 555 /usr/local/lib/libpython2.0.a
strip /usr/local/lib/libpython2.0.a
rm -f libpython2.0.so

echo "Linking new python executable with shared library ..."
cc -o python_shared Modules/python.o -L/usr/local/lib -lpython2.0 -ldl -lm -lc

echo "Removing old executables in /usr/local/bin ..."
rm /usr/local/bin/python
rm /usr/local/bin/python2.0

echo "Installing new binary in /usr/local/lib ..."
cp python_shared /usr/local/bin/python2.0
strip /usr/local/bin/python2.0
chmod 755 /usr/local/bin/python2.0
ln -f /usr/local/bin/python2.0 /usr/local/bin/python
rm -f python_shared

echo "Calling dump -H on new executable to check the use of the shared library ..."
dump -H /usr/local/bin/python2.0

echo "All done ..."
-8<------------------------------------------------->8-


Ciao Ulli

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20010515/f8e2aebf/attachment.html>


More information about the Python-list mailing list