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