custom.rst 15.4 KB
Newer Older
Chris Withers's avatar
Chris Withers committed
1
Creating eggs with extensions needing custom build settings
2
=============================================================
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

Sometimes, It's necessary to provide extra control over how an egg is
created.  This is commonly true for eggs with extension modules that
need to access libraries or include files.

The zc.recipe.egg:custom recipe can be used to define an egg with
custom build parameters.  The currently defined parameters are:

include-dirs
   A new-line separated list of directories to search for include
   files.

library-dirs
   A new-line separated list of directories to search for libraries
   to link with.

rpath
   A new-line separated list of directories to search for dynamic libraries
   at run time.

23 24 25 26
setup-eggs
   A new-line separated list of eggs that need to be installed
   beforehand. It is useful to meet the `setup_requires` requirement.

27 28 29 30 31 32 33 34 35 36 37 38 39
patch-binary
   The path to the patch executable.

patches
   A new-line separated list of patchs to apply when building.

patch-options
   Options to give to the patch program when applying patches.

patch-revision
   An integer to specify the revision (default is the number of
   patches).

40 41 42 43 44
define
   A comma-separated list of names of C preprocessor variables to
   define.

undef
pombredanne's avatar
pombredanne committed
45
   A comma-separated list of names of C preprocessor variables to
46 47 48 49
   undefine.

libraries
   The name of an additional library to link with.  Due to limitations
pombredanne's avatar
pombredanne committed
50
   in distutils and despite the option name, only a single library
51 52 53
   can be specified.

link-objects
Christian Zagrodnick's avatar
typo  
Christian Zagrodnick committed
54
   The name of an link object to link against.  Due to limitations
pombredanne's avatar
pombredanne committed
55
   in distutils and despite the option name, only a single link object
56 57 58 59 60 61 62 63 64 65 66 67 68 69
   can be specified.

debug
   Compile/link with debugging information

force
   Forcibly build everything (ignore file timestamps)

compiler
   Specify the compiler type

swig
   The path to the swig executable

Jim Fulton's avatar
Jim Fulton committed
70
swig-cpp
71 72 73 74 75
   Make SWIG create C++ files (default is C)

swig-opts
   List of SWIG command line options

76 77 78
In addition, the following options can be used to specify the egg:

egg
79 80
    An specification for the egg to be created, to install given as a
    setuptools requirement string.  This defaults to the part name.
81 82 83 84 85 86 87 88 89 90 91 92

find-links
   A list of URLs, files, or directories to search for distributions.

index
   The URL of an index server, or almost any other valid URL. :)

   If not specified, the Python Package Index,
   http://cheeseshop.python.org/pypi, is used.  You can specify an
   alternate index with this option.  If you use the links option and
   if the links point to the needed distributions, then the index can
   be anything and will be largely ignored.  In the examples, here,
Jim Fulton's avatar
Jim Fulton committed
93
   we'll just point to an empty directory on our link server.  This
94 95
   will make our examples run a little bit faster.

Christian Zagrodnick's avatar
typo  
Christian Zagrodnick committed
96
environment
97
   The name of a section with additional environment variables. The
98
   environment variables are set before the egg is built.
Christian Zagrodnick's avatar
typo  
Christian Zagrodnick committed
99

100 101 102 103 104 105 106 107 108 109 110
To illustrate this, we'll define a buildout that builds an egg for a
package that has a simple extension module::

  #include <Python.h>
  #include <extdemo.h>

  static PyMethodDef methods[] = {};

  PyMODINIT_FUNC
  initextdemo(void)
  {
111 112 113 114 115 116 117
      PyObject *m;
      m = Py_InitModule3("extdemo", methods, "");
  #ifdef TWO
      PyModule_AddObject(m, "val", PyInt_FromLong(2));
  #else
      PyModule_AddObject(m, "val", PyInt_FromLong(EXTDEMO));
  #endif
118 119
  }

Christian Zagrodnick's avatar
typo  
Christian Zagrodnick committed
120
The extension depends on a system-dependent include file, extdemo.h,
121 122 123 124 125 126 127 128
that defines a constant, EXTDEMO, that is exposed by the extension.

The extension module is available as a source distribution,
extdemo-1.4.tar.gz, on a distribution server.

We have a sample buildout that we'll add an include directory to with
the necessary include file:

129 130 131 132 133
    >>> mkdir('include')
    >>> write('include', 'extdemo.h',
    ... """
    ... #define EXTDEMO 42
    ... """)
134 135 136 137 138 139 140 141 142 143 144 145 146 147

We'll also update the buildout configuration file to define a part for
the egg:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... parts = extdemo
    ...
    ... [extdemo]
    ... recipe = zc.recipe.egg:custom
    ... find-links = %(server)s
    ... index = %(server)s/index
    ... include-dirs = include
148
    ...
149 150
    ... """ % dict(server=link_server))

Jim Fulton's avatar
Jim Fulton committed
151 152
    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
    Installing extdemo...
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

We got the zip_safe warning because the source distribution we used
wasn't setuptools based and thus didn't set the option.

The egg is created in the develop-eggs directory *not* the eggs
directory because it depends on buildout-specific parameters and the
eggs directory can be shared across multiple buildouts.

    >>> ls(sample_buildout, 'develop-eggs')
    d  extdemo-1.4-py2.4-unix-i686.egg
    -  zc.recipe.egg.egg-link

Note that no scripts or dependencies are installed.  To install
dependencies or scripts for a custom egg, define another part and use
the zc.recipe.egg recipe, listing the custom egg as one of the eggs to
be installed.  The zc.recipe.egg recipe will use the installed egg.
169 170 171 172 173 174

Let's define a script that uses out ext demo:

    >>> mkdir('demo')
    >>> write('demo', 'demo.py',
    ... """
Jim Fulton's avatar
Jim Fulton committed
175 176
    ... import extdemo, sys
    ... def print_(*args):
Jim Fulton's avatar
Jim Fulton committed
177
    ...     sys.stdout.write(' '.join(map(str, args)) + '\\n')
178
    ... def main():
Jim Fulton's avatar
Jim Fulton committed
179
    ...     print_(extdemo.val)
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    ... """)

    >>> write('demo', 'setup.py',
    ... """
    ... from setuptools import setup
    ... setup(name='demo')
    ... """)


    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = demo
    ... parts = extdemo demo
    ...
    ... [extdemo]
    ... recipe = zc.recipe.egg:custom
    ... find-links = %(server)s
    ... index = %(server)s/index
    ... include-dirs = include
    ...
    ... [demo]
    ... recipe = zc.recipe.egg
Jim Fulton's avatar
Jim Fulton committed
203
    ... eggs = demo
204 205 206 207
    ...        extdemo
    ... entry-points = demo=demo:main
    ... """ % dict(server=link_server))

Jim Fulton's avatar
Jim Fulton committed
208
    >>> print_(system(buildout), end='')
209 210 211
    Develop: '/sample-buildout/demo'
    Updating extdemo.
    Installing demo.
212
    Generated script '/sample-buildout/bin/demo'...
213 214 215

When we run the script, we'll 42 printed:

Jim Fulton's avatar
Jim Fulton committed
216
    >>> print_(system(join('bin', 'demo')), end='')
217 218
    42

219 220 221 222 223 224 225 226 227 228 229 230
Updating
--------

The custom recipe will normally check for new source distributions
that meet the given specification.  This can be suppressed using the
buildout non-newest and offline modes.  We'll generate a new source
distribution for extdemo:

    >>> update_extdemo()

If we run the buildout in non-newest or offline modes:

Jim Fulton's avatar
Jim Fulton committed
231
    >>> print_(system(buildout+' -N'), end='')
232 233 234
    Develop: '/sample-buildout/demo'
    Updating extdemo.
    Updating demo.
235

Jim Fulton's avatar
Jim Fulton committed
236
    >>> print_(system(buildout+' -o'), end='')
237 238 239
    Develop: '/sample-buildout/demo'
    Updating extdemo.
    Updating demo.
240 241 242 243 244 245 246 247 248

We won't get an update.

    >>> ls(sample_buildout, 'develop-eggs')
    -  demo.egg-link
    d  extdemo-1.4-py2.4-unix-i686.egg
    -  zc.recipe.egg.egg-link

But if we run the buildout in the default on-line and newest modes, we
249 250
will. This time we also get the test-variable message again, because the new
version is imported:
251

252
    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
253 254
    Develop: '/sample-buildout/demo'
    Updating extdemo.
255
    zip_safe flag not set; analyzing archive contents...
256
    Updating demo.
257
    ...
258 259 260 261 262 263 264

    >>> ls(sample_buildout, 'develop-eggs')
    -  demo.egg-link
    d  extdemo-1.4-py2.4-linux-i686.egg
    d  extdemo-1.5-py2.4-linux-i686.egg
    -  zc.recipe.egg.egg-link

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
Controlling the version used
----------------------------

We can specify a specific version using the egg option:

    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = demo
    ... parts = extdemo demo
    ...
    ... [extdemo]
    ... recipe = zc.recipe.egg:custom
    ... egg = extdemo ==1.4
    ... find-links = %(server)s
    ... index = %(server)s/index
    ... include-dirs = include
    ...
    ... [demo]
    ... recipe = zc.recipe.egg
Jim Fulton's avatar
Jim Fulton committed
285
    ... eggs = demo
286 287 288 289
    ...        extdemo ==1.4
    ... entry-points = demo=demo:main
    ... """ % dict(server=link_server))

290
    >>> print_(system(buildout+' -D'), end='') # doctest: +ELLIPSIS
291
    Develop: '/sample-buildout/demo'
292
    ...
293 294 295 296 297

    >>> ls(sample_buildout, 'develop-eggs')
    -  demo.egg-link
    d  extdemo-1.4-py2.4-linux-i686.egg
    -  zc.recipe.egg.egg-link
298

299 300 301 302 303 304 305

Controlling environment variables
+++++++++++++++++++++++++++++++++

To set additional environment variables, the `environment` option is used.

Let's create a recipe which prints out environment variables. We need this to
pombredanne's avatar
pombredanne committed
306
make sure the set environment variables are removed after the egg:custom
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
recipe was run.

    >>> mkdir(sample_buildout, 'recipes')
    >>> write(sample_buildout, 'recipes', 'environ.py',
    ... """
    ... import logging, os, zc.buildout
    ...
    ... class Environ:
    ...
    ...     def __init__(self, buildout, name, options):
    ...         self.name = name
    ...
    ...     def install(self):
    ...         logging.getLogger(self.name).info(
    ...             'test-variable left over: %s' % (
    ...                 'test-variable' in os.environ))
    ...         return []
    ...
    ...     def update(self):
    ...         self.install()
    ... """)
    >>> write(sample_buildout, 'recipes', 'setup.py',
    ... """
    ... from setuptools import setup
    ...
    ... setup(
    ...     name = "recipes",
    ...     entry_points = {'zc.buildout': ['environ = environ:Environ']},
    ...     )
    ... """)


Create our buildout:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = extdemo checkenv
    ...
    ... [extdemo-env]
    ... test-variable = foo
    ...
    ... [extdemo]
    ... recipe = zc.recipe.egg:custom
    ... find-links = %(server)s
    ... index = %(server)s/index
    ... include-dirs = include
    ... environment = extdemo-env
    ...
    ... [checkenv]
    ... recipe = recipes:environ
    ...
    ... """ % dict(server=link_server))
361
    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
362 363 364 365 366 367 368
    Develop: '/sample-buildout/recipes'
    Uninstalling demo.
    Uninstalling extdemo.
    Installing extdemo.
    Have environment test-variable: foo
    zip_safe flag not set; analyzing archive contents...
    Installing checkenv.
369
    ...
370 371 372 373 374 375 376 377 378 379 380


The setup.py also printed out that we have set the environment `test-variable`
to foo. After the buildout the variable is reset to its original value (i.e.
removed).

When an environment variable has a value before zc.recipe.egg:custom is run,
the original value will be restored:

    >>> import os
    >>> os.environ['test-variable'] = 'bar'
Jim Fulton's avatar
Jim Fulton committed
381
    >>> print_(system(buildout), end='')
382 383 384 385 386 387 388 389 390 391
    Develop: '/sample-buildout/recipes'
    Updating extdemo.
    Updating checkenv.
    checkenv: test-variable left over: True

    >>> os.environ['test-variable']
    'bar'


Sometimes it is required to prepend or append to an existing environment
pombredanne's avatar
pombredanne committed
392
variable, for instance for adding something to the PATH. Therefore all variables
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
are interpolated with os.environ before the're set:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = extdemo checkenv
    ...
    ... [extdemo-env]
    ... test-variable = foo:%%(test-variable)s
    ...
    ... [extdemo]
    ... recipe = zc.recipe.egg:custom
    ... find-links = %(server)s
    ... index = %(server)s/index
    ... include-dirs = include
    ... environment = extdemo-env
    ...
    ... [checkenv]
    ... recipe = recipes:environ
    ...
    ... """ % dict(server=link_server))
415
    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
416 417 418 419 420 421
    Develop: '/sample-buildout/recipes'
    Uninstalling extdemo.
    Installing extdemo.
    Have environment test-variable: foo:bar
    zip_safe flag not set; analyzing archive contents...
    Updating checkenv.
422
    ...
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

    >>> os.environ['test-variable']
    'bar'
    >>> del os.environ['test-variable']


Create a clean buildout.cfg w/o the checkenv recipe, and delete the recipe:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = extdemo
    ...
    ... [extdemo]
    ... recipe = zc.recipe.egg:custom
    ... find-links = %(server)s
    ... index = %(server)s/index
    ... include-dirs = include
    ...
    ... """ % dict(server=link_server))
444
    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
445 446 447
    Develop: '/sample-buildout/recipes'
    Uninstalling checkenv.
    Uninstalling extdemo.
448
    Installing extdemo...
Jim Fulton's avatar
Jim Fulton committed
449

450 451 452
    >>> rmdir(sample_buildout, 'recipes')


453 454 455 456 457 458
Controlling develop-egg generation
==================================

If you want to provide custom build options for a develop egg, you can
use the develop recipe.  The recipe has the following options:

459
setup
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
   The path to a setup script or directory containing a startup
   script. This is required.

include-dirs
   A new-line separated list of directories to search for include
   files.

library-dirs
   A new-line separated list of directories to search for libraries
   to link with.

rpath
   A new-line separated list of directories to search for dynamic libraries
   at run time.

475 476 477 478
setup-eggs
   A new-line separated list of eggs that need to be installed
   beforehand. It is useful to meet the `setup_requires` requirement.

479 480 481 482 483
define
   A comma-separated list of names of C preprocessor variables to
   define.

undef
pombredanne's avatar
pombredanne committed
484
   A comma-separated list of names of C preprocessor variables to
485 486 487 488
   undefine.

libraries
   The name of an additional library to link with.  Due to limitations
pombredanne's avatar
pombredanne committed
489
   in distutils and despite the option name, only a single library
490 491 492
   can be specified.

link-objects
Christian Zagrodnick's avatar
typo  
Christian Zagrodnick committed
493
   The name of an link object to link against.  Due to limitations
pombredanne's avatar
pombredanne committed
494
   in distutils and despite the option name, only a single link object
495 496 497 498 499 500 501 502 503 504 505 506 507 508
   can be specified.

debug
   Compile/link with debugging information

force
   Forcibly build everything (ignore file timestamps)

compiler
   Specify the compiler type

swig
   The path to the swig executable

Jim Fulton's avatar
Jim Fulton committed
509
swig-cpp
510 511 512 513 514
   Make SWIG create C++ files (default is C)

swig-opts
   List of SWIG command line options

515 516 517 518
environment
   The name of a section with additional environment variables. The
   environment variables are set before the egg is built.

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
To illustrate this, we'll use a directory containing the extdemo
example from the earlier section:

    >>> ls(extdemo)
    -  MANIFEST
    -  MANIFEST.in
    -  README
    -  extdemo.c
    -  setup.py

    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = demo
    ... parts = extdemo demo
    ...
    ... [extdemo]
    ... setup = %(extdemo)s
    ... recipe = zc.recipe.egg:develop
    ... include-dirs = include
    ... define = TWO
    ...
    ... [demo]
    ... recipe = zc.recipe.egg
Jim Fulton's avatar
Jim Fulton committed
543
    ... eggs = demo
544 545 546 547 548 549 550 551
    ...        extdemo
    ... entry-points = demo=demo:main
    ... """ % dict(extdemo=extdemo))

Note that we added a define option to cause the preprocessor variable
TWO to be defined.  This will cause the module-variable, 'val', to be
set with a value of 2.

552
    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
553 554 555 556
    Develop: '/sample-buildout/demo'
    Uninstalling extdemo.
    Installing extdemo.
    Installing demo.
557
    ...
558 559 560 561 562 563 564 565 566 567

Our develop-eggs now includes an egg link for extdemo:

    >>> ls('develop-eggs')
    -  demo.egg-link
    -  extdemo.egg-link
    -  zc.recipe.egg.egg-link

and the extdemo now has a built extension:

568
    >>> contents = os.listdir(extdemo)
Jim Fulton's avatar
Jim Fulton committed
569
    >>> bool([f for f in contents if f.endswith('.so') or f.endswith('.pyd')])
570
    True
571 572 573 574

Because develop eggs take precedence over non-develop eggs, the demo
script will use the new develop egg:

Jim Fulton's avatar
Jim Fulton committed
575
    >>> print_(system(join('bin', 'demo')), end='')
576
    2