[2] | 1 | .. highlightlang:: c
|
---|
| 2 |
|
---|
| 3 |
|
---|
| 4 | .. _building:
|
---|
| 5 |
|
---|
| 6 | ********************************************
|
---|
| 7 | Building C and C++ Extensions with distutils
|
---|
| 8 | ********************************************
|
---|
| 9 |
|
---|
| 10 | .. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
|
---|
| 11 |
|
---|
| 12 |
|
---|
| 13 | Starting in Python 1.4, Python provides, on Unix, a special make file for
|
---|
| 14 | building make files for building dynamically-linked extensions and custom
|
---|
| 15 | interpreters. Starting with Python 2.0, this mechanism (known as related to
|
---|
| 16 | Makefile.pre.in, and Setup files) is no longer supported. Building custom
|
---|
| 17 | interpreters was rarely used, and extension modules can be built using
|
---|
| 18 | distutils.
|
---|
| 19 |
|
---|
| 20 | Building an extension module using distutils requires that distutils is
|
---|
| 21 | installed on the build machine, which is included in Python 2.x and available
|
---|
| 22 | separately for Python 1.5. Since distutils also supports creation of binary
|
---|
| 23 | packages, users don't necessarily need a compiler and distutils to install the
|
---|
| 24 | extension.
|
---|
| 25 |
|
---|
| 26 | A distutils package contains a driver script, :file:`setup.py`. This is a plain
|
---|
| 27 | Python file, which, in the most simple case, could look like this::
|
---|
| 28 |
|
---|
| 29 | from distutils.core import setup, Extension
|
---|
| 30 |
|
---|
| 31 | module1 = Extension('demo',
|
---|
| 32 | sources = ['demo.c'])
|
---|
| 33 |
|
---|
| 34 | setup (name = 'PackageName',
|
---|
| 35 | version = '1.0',
|
---|
| 36 | description = 'This is a demo package',
|
---|
| 37 | ext_modules = [module1])
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | With this :file:`setup.py`, and a file :file:`demo.c`, running ::
|
---|
| 41 |
|
---|
| 42 | python setup.py build
|
---|
| 43 |
|
---|
| 44 | will compile :file:`demo.c`, and produce an extension module named ``demo`` in
|
---|
| 45 | the :file:`build` directory. Depending on the system, the module file will end
|
---|
| 46 | up in a subdirectory :file:`build/lib.system`, and may have a name like
|
---|
| 47 | :file:`demo.so` or :file:`demo.pyd`.
|
---|
| 48 |
|
---|
| 49 | In the :file:`setup.py`, all execution is performed by calling the ``setup``
|
---|
| 50 | function. This takes a variable number of keyword arguments, of which the
|
---|
| 51 | example above uses only a subset. Specifically, the example specifies
|
---|
| 52 | meta-information to build packages, and it specifies the contents of the
|
---|
| 53 | package. Normally, a package will contain of addition modules, like Python
|
---|
| 54 | source modules, documentation, subpackages, etc. Please refer to the distutils
|
---|
| 55 | documentation in :ref:`distutils-index` to learn more about the features of
|
---|
| 56 | distutils; this section explains building extension modules only.
|
---|
| 57 |
|
---|
| 58 | It is common to pre-compute arguments to :func:`setup`, to better structure the
|
---|
| 59 | driver script. In the example above, the\ ``ext_modules`` argument to
|
---|
| 60 | :func:`setup` is a list of extension modules, each of which is an instance of
|
---|
[391] | 61 | the :class:`~distutils.extension.Extension`. In the example, the instance
|
---|
| 62 | defines an extension named ``demo`` which is build by compiling a single source
|
---|
| 63 | file, :file:`demo.c`.
|
---|
[2] | 64 |
|
---|
| 65 | In many cases, building an extension is more complex, since additional
|
---|
| 66 | preprocessor defines and libraries may be needed. This is demonstrated in the
|
---|
| 67 | example below. ::
|
---|
| 68 |
|
---|
| 69 | from distutils.core import setup, Extension
|
---|
| 70 |
|
---|
| 71 | module1 = Extension('demo',
|
---|
| 72 | define_macros = [('MAJOR_VERSION', '1'),
|
---|
| 73 | ('MINOR_VERSION', '0')],
|
---|
| 74 | include_dirs = ['/usr/local/include'],
|
---|
| 75 | libraries = ['tcl83'],
|
---|
| 76 | library_dirs = ['/usr/local/lib'],
|
---|
| 77 | sources = ['demo.c'])
|
---|
| 78 |
|
---|
| 79 | setup (name = 'PackageName',
|
---|
| 80 | version = '1.0',
|
---|
| 81 | description = 'This is a demo package',
|
---|
| 82 | author = 'Martin v. Loewis',
|
---|
| 83 | author_email = 'martin@v.loewis.de',
|
---|
| 84 | url = 'http://docs.python.org/extending/building',
|
---|
| 85 | long_description = '''
|
---|
| 86 | This is really just a demo package.
|
---|
| 87 | ''',
|
---|
| 88 | ext_modules = [module1])
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | In this example, :func:`setup` is called with additional meta-information, which
|
---|
| 92 | is recommended when distribution packages have to be built. For the extension
|
---|
| 93 | itself, it specifies preprocessor defines, include directories, library
|
---|
| 94 | directories, and libraries. Depending on the compiler, distutils passes this
|
---|
| 95 | information in different ways to the compiler. For example, on Unix, this may
|
---|
| 96 | result in the compilation commands ::
|
---|
| 97 |
|
---|
| 98 | gcc -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -DMAJOR_VERSION=1 -DMINOR_VERSION=0 -I/usr/local/include -I/usr/local/include/python2.2 -c demo.c -o build/temp.linux-i686-2.2/demo.o
|
---|
| 99 |
|
---|
| 100 | gcc -shared build/temp.linux-i686-2.2/demo.o -L/usr/local/lib -ltcl83 -o build/lib.linux-i686-2.2/demo.so
|
---|
| 101 |
|
---|
| 102 | These lines are for demonstration purposes only; distutils users should trust
|
---|
| 103 | that distutils gets the invocations right.
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | .. _distributing:
|
---|
| 107 |
|
---|
| 108 | Distributing your extension modules
|
---|
| 109 | ===================================
|
---|
| 110 |
|
---|
| 111 | When an extension has been successfully build, there are three ways to use it.
|
---|
| 112 |
|
---|
| 113 | End-users will typically want to install the module, they do so by running ::
|
---|
| 114 |
|
---|
| 115 | python setup.py install
|
---|
| 116 |
|
---|
| 117 | Module maintainers should produce source packages; to do so, they run ::
|
---|
| 118 |
|
---|
| 119 | python setup.py sdist
|
---|
| 120 |
|
---|
| 121 | In some cases, additional files need to be included in a source distribution;
|
---|
| 122 | this is done through a :file:`MANIFEST.in` file; see the distutils documentation
|
---|
| 123 | for details.
|
---|
| 124 |
|
---|
| 125 | If the source distribution has been build successfully, maintainers can also
|
---|
| 126 | create binary distributions. Depending on the platform, one of the following
|
---|
| 127 | commands can be used to do so. ::
|
---|
| 128 |
|
---|
| 129 | python setup.py bdist_wininst
|
---|
| 130 | python setup.py bdist_rpm
|
---|
| 131 | python setup.py bdist_dumb
|
---|
| 132 |
|
---|