build.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. How to build lxml from source
  2. =============================
  3. To build lxml from source, you need libxml2 and libxslt properly
  4. installed, *including the header files*. These are likely shipped in
  5. separate ``-dev`` or ``-devel`` packages like ``libxml2-dev``, which
  6. you must install before trying to build lxml.
  7. .. contents::
  8. ..
  9. 1 Cython
  10. 2 Github, git and hg
  11. 3 Building the sources
  12. 4 Running the tests and reporting errors
  13. 5 Building an egg
  14. 6 Building lxml on MacOS-X
  15. 7 Static linking on Windows
  16. 8 Building Debian packages from SVN sources
  17. Cython
  18. ------
  19. .. _pip: http://pypi.python.org/pypi/pip
  20. .. _Cython: http://cython.org
  21. .. _wheel: http://wheel.readthedocs.org/en/latest/
  22. The lxml.etree and lxml.objectify modules are written in Cython_.
  23. Since we distribute the Cython-generated .c files with lxml releases,
  24. however, you do not need Cython to build lxml from the normal release
  25. sources. We even encourage you to *not install Cython* for a normal
  26. release build, as the generated C code can vary quite heavily between
  27. Cython versions, which may or may not generate correct code for lxml.
  28. The pre-generated release sources were tested and therefore are known
  29. to work.
  30. So, if you want a reliable build of lxml, we suggest to a) use a
  31. source release of lxml and b) disable or uninstall Cython for the
  32. build.
  33. *Only* if you are interested in building lxml from a checkout of the
  34. developer sources (e.g. to test a bug fix that has not been release
  35. yet) or if you want to be an lxml developer, then you do need a
  36. working Cython installation. You can use pip_ to install it::
  37. pip install -r requirements.txt
  38. https://github.com/lxml/lxml/blob/master/requirements.txt
  39. lxml currently requires at least Cython 0.20, later release versions
  40. should work as well.
  41. Github, git and hg
  42. -------------------
  43. The lxml package is developed in a repository on Github_ using
  44. Mercurial_ and the `hg-git`_ plugin. You can retrieve the current
  45. developer version using::
  46. hg clone git://github.com/lxml/lxml.git lxml
  47. This will create a directory ``lxml`` and download the source into it,
  48. including the complete development history. Don't be afraid, the
  49. download is fairly quick. You can also browse the `lxml repository`_
  50. through the web.
  51. .. _Github: https://github.com/lxml/
  52. .. _Mercurial: http://mercurial.selenic.com/
  53. .. _`hg-git`: http://hg-git.github.com/
  54. .. _`lxml repository`: https://github.com/lxml/lxml
  55. .. _`source tar-ball`: https://github.com/lxml/lxml/tarball/master
  56. Building the sources
  57. ---------------------
  58. Clone the source repository as described above (or download
  59. the `source tar-ball`_ and unpack it) and then type::
  60. python setup.py build
  61. or::
  62. python setup.py bdist_egg # requires 'setuptools' or 'distribute'
  63. To (re-)build the C sources with Cython, you must additionally pass the
  64. option ``--with-cython``::
  65. python setup.py build --with-cython
  66. If you want to test lxml from the source directory, it is better to build it
  67. in-place like this::
  68. python setup.py build_ext -i --with-cython
  69. or, in Unix-like environments::
  70. make inplace
  71. To speed up the build in test environments (e.g. on a continuous
  72. integration server), set the ``CFLAGS`` environment variable to
  73. disable C compiler optimisations (e.g. "-O0" for gcc, that's
  74. minus-oh-zero), for example::
  75. CFLAGS="-O0" make inplace
  76. If you get errors about missing header files (e.g. ``Python.h`` or
  77. ``libxml/xmlversion.h``) then you need to make sure the development
  78. packages of Python, libxml2 and libxslt are properly installed. On
  79. Linux distributions, they are usually called something like
  80. ``libxml2-dev`` or ``libxslt-devel``. If these packages were
  81. installed in non-standard places, try passing the following option to
  82. setup.py to make sure the right config is found::
  83. python setup.py build --with-xslt-config=/path/to/xslt-config
  84. If this doesn't help, you may have to add the location of the header
  85. files to the include path like::
  86. python setup.py build_ext -i -I /usr/include/libxml2
  87. where the file is in ``/usr/include/libxml2/libxml/xmlversion.h``
  88. To use lxml.etree in-place, you can place lxml's ``src`` directory
  89. on your Python module search path (PYTHONPATH) and then import
  90. ``lxml.etree`` to play with it::
  91. # cd lxml
  92. # PYTHONPATH=src python
  93. Python 2.7.2
  94. Type "help", "copyright", "credits" or "license" for more information.
  95. >>> from lxml import etree
  96. >>>
  97. To make sure everything gets recompiled cleanly after changes, you can
  98. run ``make clean`` or delete the file ``src/lxml/etree.c``.
  99. Running the tests and reporting errors
  100. --------------------------------------
  101. The source distribution (tgz) and the source repository contain a test
  102. suite for lxml. You can run it from the top-level directory::
  103. python test.py
  104. Note that the test script only tests the in-place build (see distutils
  105. building above), as it searches the ``src`` directory. You can use the
  106. following one-step command to trigger an in-place build and test it::
  107. make test
  108. This also runs the ElementTree and cElementTree compatibility tests. To call
  109. them separately, make sure you have lxml on your PYTHONPATH first, then run::
  110. python selftest.py
  111. and::
  112. python selftest2.py
  113. If the tests give failures, errors, or worse, segmentation faults, we'd really
  114. like to know. Please contact us on the `mailing list`_, and please specify
  115. the version of lxml, libxml2, libxslt and Python you were using, as well as
  116. your operating system type (Linux, Windows, MacOS-X, ...).
  117. .. _`mailing list`: http://lxml.de/mailinglist/
  118. Building an egg or wheel
  119. ------------------------
  120. This is the procedure to make an lxml egg or wheel_ for your platform.
  121. It assumes that you have ``setuptools`` or ``distribute`` installed, as well
  122. as the ``wheel`` package.
  123. First, download the lxml-x.y.tar.gz release. This contains the pregenerated
  124. C files so that you can be sure you build exactly from the release sources.
  125. Unpack them and ``cd`` into the resulting directory. Then, to build a wheel,
  126. simply run the command
  127. ::
  128. python setup.py bdist_wheel
  129. or, to build a statically linked wheel with all of libxml2, libxslt and
  130. friends compiled in, run
  131. python setup.py bdist_wheel --static-deps
  132. The resulting .whl file will be written into the ``dist`` directory.
  133. To build an egg file, run
  134. ::
  135. python setup.py build_egg
  136. If you are on a Unix-like platform, you can first build the extension modules
  137. using
  138. ::
  139. python setup.py build
  140. and then ``cd`` into the directory ``build/lib.your.platform`` to call
  141. ``strip`` on any ``.so`` file you find there. This reduces the size of
  142. the binary distribution considerably. Then, from the package root directory,
  143. call
  144. ::
  145. python setup.py bdist_egg
  146. This will quickly package the pre-built packages into an egg file and
  147. drop it into the ``dist`` directory.
  148. Building lxml on MacOS-X
  149. ------------------------
  150. Apple regularly ships new system releases with horribly outdated
  151. system libraries. This is specifically the case for libxml2 and
  152. libxslt, where the system provided versions are too old to build lxml.
  153. While the Unix environment in MacOS-X makes it relatively easy to
  154. install Unix/Linux style package management tools and new software, it
  155. actually seems to be hard to get libraries set up for exclusive usage
  156. that MacOS-X ships in an older version. Alternative distributions
  157. (like macports) install their libraries in addition to the system
  158. libraries, but the compiler and the runtime loader on MacOS still sees
  159. the system libraries before the new libraries. This can lead to
  160. undebuggable crashes where the newer library seems to be loaded but
  161. the older system library is used.
  162. Apple discourages static building against libraries, which would help
  163. working around this problem. Apple does not ship static library
  164. binaries with its system and several package management systems follow
  165. this decision. Therefore, building static binaries requires building
  166. the dependencies first. The ``setup.py`` script does this
  167. automatically when you call it like this::
  168. python setup.py build --static-deps
  169. This will download and build the latest versions of libxml2 and
  170. libxslt from the official FTP download site. If you want to use
  171. specific versions, or want to prevent any online access, you can
  172. download both ``tar.gz`` release files yourself, place them into a
  173. subdirectory ``libs`` in the lxml distribution, and call ``setup.py``
  174. with the desired target versions like this::
  175. python setup.py build --static-deps \
  176. --libxml2-version=2.7.8 \
  177. --libxslt-version=1.1.27 \
  178. sudo python setup.py install
  179. Instead of ``build``, you can use any target, like ``bdist_egg`` if
  180. you want to use setuptools to build an installable egg.
  181. Note that this also works with pip_. Since you can't pass
  182. command line options in this case, you have to use an environment
  183. variable instead::
  184. STATIC_DEPS=true pip install lxml
  185. To install the package into the system Python package directory,
  186. run the installation with "sudo"::
  187. STATIC_DEPS=true sudo pip install lxml
  188. The ``STATICBUILD`` environment variable is handled equivalently to
  189. the ``STATIC_DEPS`` variable, but is used by some other extension
  190. packages, too.
  191. Static linking on Windows
  192. -------------------------
  193. Most operating systems have proper package management that makes installing
  194. current versions of libxml2 and libxslt easy. The most famous exception is
  195. Microsoft Windows, which entirely lacks these capabilities. To work around
  196. the limits of this platform, lxml's installation can download pre-built
  197. packages of the dependencies and build statically against them. Assuming
  198. you have a proper C compiler setup to build Python extensions, this should
  199. work::
  200. python setup.py bdist_wininst --static-deps
  201. It should create a windows installer in the ``pkg`` directory.
  202. Building Debian packages from SVN sources
  203. -----------------------------------------
  204. `Andreas Pakulat`_ proposed the following approach.
  205. .. _`Andreas Pakulat`: http://thread.gmane.org/gmane.comp.python.lxml.devel/1239/focus=1249
  206. * ``apt-get source lxml``
  207. * remove the unpacked directory
  208. * tar.gz the lxml SVN version and replace the orig.tar.gz that lies in the
  209. directory
  210. * check md5sum of created tar.gz file and place new sum and size in dsc file
  211. * do ``dpkg-source -x lxml-[VERSION].dsc`` and cd into the newly created directory
  212. * run ``dch -i`` and add a comment like "use trunk version", this will
  213. increase the debian version number so apt/dpkg won't get confused
  214. * run ``dpkg-buildpackage -rfakeroot -us -uc`` to build the package
  215. In case ``dpkg-buildpackage`` tells you that some dependecies are missing, you
  216. can either install them manually or run ``apt-get build-dep lxml``.
  217. That will give you .deb packages in the parent directory which can be
  218. installed using ``dpkg -i``.