building.qbk 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. [chapter Building and Testing
  2. [quickbook 1.7]
  3. [authors [Abrahams, David]]
  4. [copyright 2002 - 2015 David Abrahams, Stefan Seefeld]
  5. [id building]
  6. ]
  7. [/ Copyright David Abrahams 2006. Distributed under the Boost
  8. / Software License, Version 1.0. (See accompanying
  9. / file LICENSE_1_0.txt or copy at
  10. / http://www.boost.org/LICENSE_1_0.txt)
  11. /]
  12. [section Requirements]
  13. Boost.Python requires [@http://www.python.org/2.2 Python 2.2]
  14. [footnote Note that although we tested earlier versions of Boost.Python
  15. with Python 2.2, and we don't *think* we've done anything to break
  16. compatibility, this release of Boost.Python may not have been tested
  17. with versions of Python earlier than 2.4, so we're not 100% sure that
  18. python 2.2 and 2.3 are supported.] *or* [@http://www.python.org newer].
  19. [endsect]
  20. [section Background]
  21. There are two basic models for combining C++ and Python:
  22. * [@http://www.python.org/doc/current/ext/intro.html extending],
  23. in which the end-user launches the Python interpreter
  24. executable and imports Python “extension modules” written in C++.
  25. Think of taking a library written in C++ and giving it a Python
  26. interface so Python programmers can use it. From Python, these
  27. modules look just like regular Python modules.
  28. * [@http://www.python.org/doc/current/ext/embedding.html embedding],
  29. in which the end-user launches a program written
  30. in C++ that in turn invokes the Python interpreter as a library
  31. subroutine. Think of adding scriptability to an existing
  32. application.
  33. The key distinction between extending and embedding is the location
  34. of the C++ `main()` function: in the Python interpreter executable,
  35. or in some other program, respectively. Note that even when
  36. embedding Python in another program, [@http://www.python.org/doc/current/ext/extending-with-embedding.html extension modules are often
  37. the best way to make C/C++ functionality accessible to Python
  38. code], so the use of extension modules is really at the heart of
  39. both models.
  40. Except in rare cases, extension modules are built as
  41. dynamically-loaded libraries with a single entry point, which means
  42. you can change them without rebuilding either the other extension
  43. modules or the executable containing `main()`.
  44. [endsect]
  45. [section No-Install Quickstart]
  46. There is no need to “install Boost” in order to get started using
  47. Boost.Python. These instructions use _bb_ projects,
  48. which will build those binaries as soon as they're needed. Your
  49. first tests may take a little longer while you wait for
  50. Boost.Python to build, but doing things this way will save you from
  51. worrying about build intricacies like which library binaries to use
  52. for a specific compiler configuration and figuring out the right
  53. compiler options to use yourself.
  54. [note Of course it's possible to use other build systems to
  55. build Boost.Python and its extensions, but they are not
  56. officially supported by Boost. Moreover *99% of all “I can't
  57. build Boost.Python” problems come from trying to use another
  58. build system* without first following these instructions.
  59. If you want to use another system anyway, we suggest that you
  60. follow these instructions, and then invoke `bjam` with the
  61. `-a -o`\ /filename/
  62. options to dump the build commands it executes to a file, so
  63. you can see what your alternate build system needs to do.]
  64. [section Basic Procedure]
  65. 1. Get Boost; see sections 1 and 2 of the _gsg_.
  66. 2. Get the `bjam` build driver. See section 5 of the _gsg_.
  67. 3. cd into the `example/quickstart/` directory of your
  68. Boost.Python installation, which contains a small example project.
  69. 4. Invoke `bjam`. Replace the “\ `stage`\ “ argument from the
  70. example invocation from section 5 of the _gsg_ with “\ `test`\ ,“ to
  71. build all the test targets. Also add the argument “\ `--verbose-test`\ ”
  72. to see the output generated by the tests when they are run.
  73. On Windows, your `bjam` invocation might look something like:
  74. ``
  75. C:\\...\\quickstart> bjam toolset=msvc --verbose-test test
  76. ``
  77. and on Unix variants, perhaps,
  78. ``
  79. .../quickstart$ bjam toolset=gcc --verbose-test test
  80. ``
  81. [note For the sake of concision, the rest of this guide will use
  82. unix-style forward slashes in pathnames instead of the
  83. backslashes with which Windows users may be more familiar. The forward
  84. slashes should work everywhere except in
  85. [@http://www.boost.org/more/getting_started/windows.html#command-prompt
  86. Command Prompt] windows, where you should use backslashes.]
  87. If you followed this procedure successfully, you will have built an
  88. extension module called `extending` and tested it by running a
  89. Python script called `test_extending.py`. You will also have
  90. built and run a simple application called `embedding` that embeds
  91. python.
  92. [endsect]
  93. [section In Case of Trouble]
  94. If you're seeing lots of compiler and/or linker error messages,
  95. it's probably because Boost.Build is having trouble finding your
  96. Python installation. You might want to pass the
  97. `--debug-configuration` option to `bjam` the first few times
  98. you invoke it, to make sure that Boost.Build is correctly locating
  99. all the parts of your Python installation. If it isn't, consider
  100. [link building.configuring_boost_build Configuring Boost.Build]
  101. as detailed below.
  102. If you're still having trouble, Someone on one of the following
  103. mailing lists may be able to help:
  104. * The _bb_list_ for issues related to Boost.Build
  105. * The _bp_list_ for issues specifically related to Boost.Python
  106. [endsect]
  107. [section In Case Everything Seemed to Work]
  108. Rejoice! If you're new to Boost.Python, at this point it might be
  109. a good idea to ignore build issues for a while and concentrate on
  110. learning the library by going through the _tutorial_ and perhaps
  111. some of the _reference_, trying out what you've
  112. learned about the API by modifying the quickstart project.
  113. [endsect]
  114. [section Modifying the Example Project]
  115. If you're content to keep your extension module forever in one
  116. source file called `extending.cpp`, inside your Boost.Python
  117. distribution, and import it forever as `extending`, then you can
  118. stop here. However, it's likely that you will want to make a few
  119. changes. There are a few things you can do without having to learn
  120. _bb_ in depth.
  121. The project you just built is specified in two files in the current
  122. directory: `boost-build.jam`, which tells `bjam` where it can
  123. find the interpreted code of the Boost build system, and
  124. `Jamroot`, which describes the targets you just built. These
  125. files are heavily commented, so they should be easy to modify.
  126. Take care, however, to preserve whitespace. Punctuation such as
  127. `;` will not be recognized as intended by `bjam` if it is not
  128. surrounded by whitespace.
  129. [section Relocate the Project]
  130. You'll probably want to copy this project elsewhere so you can
  131. change it without modifying your Boost distribution. To do that,
  132. simply
  133. a. copy the entire `example/quickstart/` directory
  134. into a new directory.
  135. b. In the new copies of `boost-build.jam` and `Jamroot`, locate
  136. the relative path near the top of the file that is clearly
  137. marked by a comment, and edit that path so that it refers to the
  138. same directory your Boost distribution as it referred to when
  139. the file was in its original location in the
  140. `example/quickstart/` directory.
  141. For example, if you moved the project from
  142. `/home/dave/boost_1_34_0/libs/python/example/quickstart` to
  143. `/home/dave/my-project`, you could change the first path in
  144. `boost-build.jam` from
  145. ``
  146. ../../../../tools/build/src
  147. ``
  148. to
  149. ``
  150. /home/dave/boost_1_34_0/tools/build/src
  151. ``
  152. and change the first path in `Jamroot` from
  153. ``
  154. ../../../..
  155. ``
  156. to
  157. ``
  158. /home/dave/boost_1_34_0
  159. ``
  160. [endsect]
  161. [section Add New or Change Names of Existing Source Files]
  162. The names of additional source files involved in building your
  163. extension module or embedding application can be listed in
  164. `Jamroot` right alongside `extending.cpp` or `embedding.cpp`
  165. respectively. Just be sure to leave whitespace around each
  166. filename:
  167. ``
  168. … file1.cpp file2.cpp file3.cpp …
  169. ``
  170. Naturally, if you want to change the name of a source file you can
  171. tell Boost.Build about it by editing the name in `Jamroot`.
  172. [endsect]
  173. [section Change the Name of your Extension Module]
  174. The name of the extension module is determined by two things:
  175. # the name in `Jamroot` immediately following `python-extension`, and
  176. # the name passed to `BOOST_PYTHON_MODULE` in `extending.cpp`.
  177. To change the name of the extension module from `extending` to
  178. `hello`, you'd edit `Jamroot`, changing
  179. ``
  180. python-extension extending : extending.cpp ;
  181. ``
  182. to
  183. ``
  184. python-extension hello : extending.cpp ;
  185. ``
  186. and you'd edit extending.cpp, changing
  187. ``
  188. BOOST_PYTHON_MODULE(extending)
  189. ``
  190. to
  191. ``
  192. BOOST_PYTHON_MODULE(hello)
  193. ``
  194. [endsect]
  195. [endsect]
  196. [endsect]
  197. [section Installing Boost.Python on your System]
  198. Since Boost.Python is a separately-compiled (as opposed to
  199. `header-only`) library, its user relies on the services of a
  200. Boost.Python library binary.
  201. If you need a regular installation of the Boost.Python library
  202. binaries on your system, the _gsg_ will
  203. walk you through the steps of creating one. If building binaries
  204. from source, you might want to supply the `--with-python`
  205. argument to `bjam` (or the `--with-libraries=python` argument
  206. to `configure`), so only the Boost.Python binary will be built,
  207. rather than all the Boost binaries.
  208. [endsect]
  209. [section Configuring Boost.Build]
  210. As described in the [@http://www.boost.org/build/doc/html/bbv2/overview/configuration.html Boost.Build Reference Manual], a file called
  211. `user-config.jam` in your home directory is used to
  212. specify the tools and libraries available to the build system. You
  213. may need to create or edit `user-config.jam` to tell Boost.Build
  214. how to invoke Python, `#include` its headers, and link with its
  215. libraries.
  216. [note If you are using a unix-variant OS and you ran Boost's
  217. `configure` script, it may have generated a
  218. `user-config.jam` for you. [footnote `configure` overwrites the existing
  219. `user-config.jam` in your home directory (if any) after making a backup of
  220. the old version.] If your `configure`\ /\ `make` sequence was successful and
  221. Boost.Python binaries were built, your `user-config.jam` file is probably already
  222. correct.]
  223. If you have one fairly “standard” python installation for your
  224. platform, you might not need to do anything special to describe it. If
  225. you haven't configured python in `user-config.jam` (and you don't
  226. specify `--without-python` on the Boost.Build command line),
  227. Boost.Build will automatically execute the equivalent of
  228. ``
  229. import toolset : using ;
  230. using python ;
  231. ``
  232. which automatically looks for Python in the most likely places.
  233. However, that only happens when using the Boost.Python project file
  234. (e.g. when referred to by another project as in the quickstart
  235. method). If instead you are linking against separately-compiled
  236. Boost.Python binaries, you should set up a `user-config.jam` file
  237. with at least the minimal incantation above.
  238. [section Python Configuration Parameters]
  239. If you have several versions of Python installed, or Python is
  240. installed in an unusual way, you may want to supply any or all of
  241. the following optional parameters to `using python`.
  242. [variablelist
  243. [[version]
  244. [the version of Python to use. Should be in Major.Minor
  245. format, for example, `2.3`. Do not include the subminor
  246. version (i.e. *not* `2.5.1`). If you have multiple Python
  247. versions installed, the version will usually be the only
  248. configuration argument required.]]
  249. [[cmd-or-prefix]
  250. [preferably, a command that invokes a Python interpreter.
  251. Alternatively, the installation prefix for Python libraries and
  252. header files. Only use the alternative formulation if there is
  253. no appropriate Python executable available.]]
  254. [[*includes*]
  255. [the `#include` paths for Python headers. Normally the correct
  256. path(s) will be automatically deduced from `version` and/or
  257. `cmd-or-prefix`.]]
  258. [[*libraries*]
  259. [the path to Python library binaries. On MacOS/Darwin,
  260. you can also pass the path of the Python framework. Normally the
  261. correct path(s) will be automatically deduced from `version`
  262. and/or `cmd-or-prefix`.]]
  263. [[*condition*]
  264. [if specified, should be a set of Boost.Build
  265. properties that are matched against the build configuration when
  266. Boost.Build selects a Python configuration to use. See examples
  267. below for details.]]
  268. [[*extension-suffix*]
  269. [A string to append to the name of extension
  270. modules before the true filename extension. You almost certainly
  271. don't need to use this. Usually this suffix is only used when
  272. targeting a Windows debug build of Python, and will be set
  273. automatically for you based on the value of the
  274. [link building.python_debugging_builds <python-debugging>] feature.
  275. However, at least one Linux distribution (Ubuntu Feisty Fawn) has
  276. a specially configured [@https://wiki.ubuntu.com/PyDbgBuilds <python-dbg>]
  277. package that claims to use such a suffix.]]
  278. ]
  279. [endsect]
  280. [section Examples]
  281. Note that in the examples below, case and *especially whitespace* are
  282. significant.
  283. * If you have both python 2.5 and python 2.4 installed,
  284. `user-config.jam` might contain
  285. ``
  286. using python : 2.5 ; # Make both versions of Python available
  287. using python : 2.4 ; # To build with python 2.4, add python=2.4
  288. # to your command line.
  289. ``
  290. The first version configured (2.5) becomes the default. To build
  291. against python 2.4, add `python=2.4` to the `bjam` command line.
  292. * If you have python installed in an unusual location, you might
  293. supply the path to the interpreter in the `cmd-or-prefix`
  294. parameter:
  295. ``
  296. using python : : /usr/local/python-2.6-beta/bin/python ;
  297. ``
  298. * If you have a separate build of Python for use with a particular
  299. toolset, you might supply that toolset in the `condition`
  300. parameter:
  301. ``
  302. using python ; # use for most toolsets
  303. # Use with Intel C++ toolset
  304. using python
  305. : # version
  306. : c:\\Devel\\Python-2.5-IntelBuild\\PCBuild\\python # cmd-or-prefix
  307. : # includes
  308. : # libraries
  309. : <toolset>intel # condition
  310. ;
  311. ``
  312. * If you have downloaded the Python sources and built both the
  313. normal and the [link building.python_debugging_builds "python debugging"]
  314. builds from source on Windows, you might see:
  315. ``
  316. using python : 2.5 : C:\\src\\Python-2.5\\PCBuild\\python ;
  317. using python : 2.5 : C:\\src\\Python-2.5\\PCBuild\\python_d
  318. : # includes
  319. : # libs
  320. : <python-debugging>on ;
  321. ``
  322. * You can set up your user-config.jam so a bjam built under Windows
  323. can build/test both Windows and Cygwin_ python extensions. Just pass
  324. `<target-os>cygwin` in the `condition` parameter
  325. for the cygwin python installation:
  326. ``
  327. # windows installation
  328. using python ;
  329. # cygwin installation
  330. using python : : c:\\cygwin\\bin\\python2.5 : : : <target-os>cygwin ;
  331. ``
  332. when you put target-os=cygwin in your build request, it should build
  333. with the cygwin version of python: [#flavor]_
  334. ``
  335. bjam target-os=cygwin toolset=gcc
  336. ``
  337. This is supposed to work the other way, too (targeting windows
  338. python with a [@http://cygwin.com Cygwin] bjam) but it seems as though the support in
  339. Boost.Build's toolsets for building that way is broken at the
  340. time of this writing.
  341. * Note that because of [@http://zigzag.cs.msu.su/boost.build/wiki/AlternativeSelection
  342. the way Boost.Build currently selects target alternatives], you might have be very
  343. explicit in your build requests. For example, given:
  344. ``
  345. using python : 2.5 ; # a regular windows build
  346. using python : 2.4 : : : : <target-os>cygwin ;
  347. ``
  348. building with
  349. ``
  350. bjam target-os=cygwin
  351. ``
  352. will yield an error. Instead, you'll need to write
  353. ``
  354. bjam target-os=cygwin/python=2.4
  355. ``
  356. [endsect]
  357. [endsect]
  358. [section Choosing a Boost.Python Library Binary]
  359. If—instead of letting Boost.Build construct and link with the right
  360. libraries automatically—you choose to use a pre-built Boost.Python
  361. library, you'll need to think about which one to link with. The
  362. Boost.Python binary comes in both static and dynamic flavors. Take
  363. care to choose the right flavor for your application. [footnote
  364. Information about how to identify the static and dynamic builds of Boost.Python on
  365. [@http://boost.org/more/getting_started/windows.html#library-naming Windows] /
  366. [@http://boost.org/more/getting_started/unix-variants.html#library-naming Unix variants]]
  367. [section The Dynamic Binary]
  368. The dynamic library is the safest and most-versatile choice:
  369. * A single copy of the library code is used by all extension
  370. modules built with a given toolset. [footnote Because of the way most \*nix platforms
  371. share symbols among dynamically-loaded objects, I'm not certain
  372. that extension modules built with different compiler toolsets
  373. will always use different copies of the Boost.Python library
  374. when loaded into the same Python instance. Not using different
  375. libraries could be a good thing if the compilers have compatible
  376. ABIs, because extension modules built with the two libraries
  377. would be interoperable. Otherwise, it could spell disaster,
  378. since an extension module and the Boost.Python library would
  379. have different ideas of such things as class layout. I would
  380. appreciate someone doing the experiment to find out what
  381. happens.]
  382. * The library contains a type conversion registry. Because one
  383. registry is shared among all extension modules, instances of a
  384. class exposed to Python in one dynamically-loaded extension
  385. module can be passed to functions exposed in another such module.
  386. [endsect]
  387. [section The Static Binary]
  388. It might be appropriate to use the static Boost.Python library in
  389. any of the following cases:
  390. * You are _extending_ python and the types exposed in your
  391. dynamically-loaded extension module don't need to be used by any
  392. other Boost.Python extension modules, and you don't care if the
  393. core library code is duplicated among them.
  394. * You are _embedding_ python in your application and either:
  395. * You are targeting a Unix variant OS other than MacOS or AIX,
  396. where the dynamically-loaded extension modules can “see” the
  397. Boost.Python library symbols that are part of the executable.
  398. * Or, you have statically linked some Boost.Python extension
  399. modules into your application and you don't care if any
  400. dynamically-loaded Boost.Python extension modules are able to
  401. use the types exposed by your statically-linked extension
  402. modules (and vice-versa).
  403. [endsect]
  404. [endsect]
  405. [section `#include` Issues]
  406. 1. If you should ever have occasion to `#include "python.h"`
  407. directly in a translation unit of a program using Boost.Python,
  408. use `#include "boost/python/detail/wrap_python.hpp"` instead.
  409. It handles several issues necessary for use with Boost.Python,
  410. one of which is mentioned in the next section.
  411. 2. Be sure not to `#include` any system headers before
  412. `wrap_python.hpp`. This restriction is actually imposed by
  413. Python, or more properly, by Python's interaction with your
  414. operating system. See
  415. [@http://docs.python.org/ext/simpleExample.html] for details.
  416. [endsect]
  417. [section Python Debugging Builds]
  418. Python can be built in a special “python debugging” configuration
  419. that adds extra checks and instrumentation that can be very useful
  420. for developers of extension modules. The data structures used by
  421. the debugging configuration contain additional members, so *a
  422. Python executable built with python debugging enabled cannot be
  423. used with an extension module or library compiled without it, and
  424. vice-versa.*
  425. Since pre-built “python debugging” versions of the Python
  426. executable and libraries are not supplied with most distributions
  427. of Python, [footnote On Unix and similar platforms, a debugging python and associated libraries are built by adding --with-pydebug when configuring the Python build. On Windows, the debugging version of Python is generated by the "Win32 Debug" target of the Visual Studio project in the PCBuild subdirectory of a full Python source code distribution.] and we didn't want to force our users
  428. to build them, Boost.Build does not automatically enable python
  429. debugging in its `debug` build variant (which is the default).
  430. Instead there is a special build property called
  431. `python-debugging` that, when used as a build property, will
  432. define the right preprocessor symbols and select the right
  433. libraries to link with.
  434. On unix-variant platforms, the debugging versions of Python's data
  435. structures will only be used if the symbol `Py_DEBUG` is defined.
  436. On many windows compilers, when extension modules are built with
  437. the preprocessor symbol `_DEBUG`, Python defaults to force
  438. linking with a special debugging version of the Python DLL. Since
  439. that symbol is very commonly used even when Python is not present,
  440. Boost.Python temporarily undefines `_DEBUG` when `Python.h`
  441. is #included from `boost/python/detail/wrap_python.hpp` - unless
  442. `BOOST_DEBUG_PYTHON` is defined. The upshot is that if you want
  443. “python debugging”and you aren't using Boost.Build, you should make
  444. sure `BOOST_DEBUG_PYTHON` is defined, or python debugging will be
  445. suppressed.
  446. [endsect]
  447. [section Testing Boost.Python]
  448. To run the full test suite for Boost.Python, invoke `bjam` in the
  449. `test` subdirectory of your Boost.Python distribution.
  450. [endsect]
  451. [section Notes for MinGW (and Cygwin with -mno-cygwin) GCC Users]
  452. If you are using a version of Python prior to 2.4.1 with a MinGW
  453. prior to 3.0.0 (with binutils-2.13.90-20030111-1), you will need to
  454. create a MinGW-compatible version of the Python library; the one
  455. shipped with Python will only work with a Microsoft-compatible
  456. linker. Follow the instructions in the “Non-Microsoft” section of
  457. the “Building Extensions: Tips And Tricks” chapter in
  458. [@https://docs.python.org/2/install/index.html Installing Python Modules]
  459. to create `libpythonXX.a`, where `XX` corresponds to the major and minor
  460. version numbers of your Python installation.
  461. [endsect]