blob: 08b0cc2ceda23e662020b695fef295e4df79beef [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. highlightlang:: c
2
3
4.. _building:
5
6********************************************
7Building C and C++ Extensions with distutils
8********************************************
9
10.. sectionauthor:: Martin v. Lรถwis <martin@v.loewis.de>
11
12
13Starting in Python 1.4, Python provides, on Unix, a special make file for
14building make files for building dynamically-linked extensions and custom
15interpreters. Starting with Python 2.0, this mechanism (known as related to
16Makefile.pre.in, and Setup files) is no longer supported. Building custom
17interpreters was rarely used, and extension modules can be built using
18distutils.
19
20Building an extension module using distutils requires that distutils is
21installed on the build machine, which is included in Python 2.x and available
22separately for Python 1.5. Since distutils also supports creation of binary
23packages, users don't necessarily need a compiler and distutils to install the
24extension.
25
26A distutils package contains a driver script, :file:`setup.py`. This is a plain
27Python 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
40With this :file:`setup.py`, and a file :file:`demo.c`, running ::
41
Georg Brandl48310cd2009-01-03 21:18:54 +000042 python setup.py build
Georg Brandl116aa622007-08-15 14:28:22 +000043
44will compile :file:`demo.c`, and produce an extension module named ``demo`` in
45the :file:`build` directory. Depending on the system, the module file will end
46up in a subdirectory :file:`build/lib.system`, and may have a name like
47:file:`demo.so` or :file:`demo.pyd`.
48
49In the :file:`setup.py`, all execution is performed by calling the ``setup``
50function. This takes a variable number of keyword arguments, of which the
51example above uses only a subset. Specifically, the example specifies
52meta-information to build packages, and it specifies the contents of the
53package. Normally, a package will contain of addition modules, like Python
54source modules, documentation, subpackages, etc. Please refer to the distutils
55documentation in :ref:`distutils-index` to learn more about the features of
56distutils; this section explains building extension modules only.
57
58It is common to pre-compute arguments to :func:`setup`, to better structure the
59driver 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
Serhiy Storchaka0b68a2d2013-10-09 13:26:17 +030061the :class:`~distutils.extension.Extension`. In the example, the instance
62defines an extension named ``demo`` which is build by compiling a single source
63file, :file:`demo.c`.
Georg Brandl116aa622007-08-15 14:28:22 +000064
65In many cases, building an extension is more complex, since additional
66preprocessor defines and libraries may be needed. This is demonstrated in the
67example 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',
Georg Brandl86def6c2008-01-21 20:36:10 +000084 url = 'http://docs.python.org/extending/building',
Georg Brandl116aa622007-08-15 14:28:22 +000085 long_description = '''
86 This is really just a demo package.
87 ''',
88 ext_modules = [module1])
89
90
91In this example, :func:`setup` is called with additional meta-information, which
92is recommended when distribution packages have to be built. For the extension
93itself, it specifies preprocessor defines, include directories, library
94directories, and libraries. Depending on the compiler, distutils passes this
95information in different ways to the compiler. For example, on Unix, this may
96result 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
102These lines are for demonstration purposes only; distutils users should trust
103that distutils gets the invocations right.
104
105
106.. _distributing:
107
108Distributing your extension modules
109===================================
110
111When an extension has been successfully build, there are three ways to use it.
112
113End-users will typically want to install the module, they do so by running ::
114
115 python setup.py install
116
117Module maintainers should produce source packages; to do so, they run ::
118
119 python setup.py sdist
120
121In some cases, additional files need to be included in a source distribution;
122this is done through a :file:`MANIFEST.in` file; see the distutils documentation
123for details.
124
125If the source distribution has been build successfully, maintainers can also
126create binary distributions. Depending on the platform, one of the following
127commands 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