/Doc/distutils/configfile.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 130 lines · 102 code · 28 blank · 0 comment · 0 complexity · f22ab5ba2ea8442c311f31c67665d6ee MD5 · raw file

  1. .. _setup-config:
  2. ************************************
  3. Writing the Setup Configuration File
  4. ************************************
  5. Often, it's not possible to write down everything needed to build a distribution
  6. *a priori*: you may need to get some information from the user, or from the
  7. user's system, in order to proceed. As long as that information is fairly
  8. simple---a list of directories to search for C header files or libraries, for
  9. example---then providing a configuration file, :file:`setup.cfg`, for users to
  10. edit is a cheap and easy way to solicit it. Configuration files also let you
  11. provide default values for any command option, which the installer can then
  12. override either on the command-line or by editing the config file.
  13. The setup configuration file is a useful middle-ground between the setup script
  14. ---which, ideally, would be opaque to installers [#]_---and the command-line to
  15. the setup script, which is outside of your control and entirely up to the
  16. installer. In fact, :file:`setup.cfg` (and any other Distutils configuration
  17. files present on the target system) are processed after the contents of the
  18. setup script, but before the command-line. This has several useful
  19. consequences:
  20. .. % (If you have more advanced needs, such as determining which extensions
  21. .. % to build based on what capabilities are present on the target system,
  22. .. % then you need the Distutils ``auto-configuration'' facility. This
  23. .. % started to appear in Distutils 0.9 but, as of this writing, isn't mature
  24. .. % or stable enough yet for real-world use.)
  25. * installers can override some of what you put in :file:`setup.py` by editing
  26. :file:`setup.cfg`
  27. * you can provide non-standard defaults for options that are not easily set in
  28. :file:`setup.py`
  29. * installers can override anything in :file:`setup.cfg` using the command-line
  30. options to :file:`setup.py`
  31. The basic syntax of the configuration file is simple::
  32. [command]
  33. option=value
  34. ...
  35. where *command* is one of the Distutils commands (e.g. :command:`build_py`,
  36. :command:`install`), and *option* is one of the options that command supports.
  37. Any number of options can be supplied for each command, and any number of
  38. command sections can be included in the file. Blank lines are ignored, as are
  39. comments, which run from a ``'#'`` character until the end of the line. Long
  40. option values can be split across multiple lines simply by indenting the
  41. continuation lines.
  42. You can find out the list of options supported by a particular command with the
  43. universal :option:`--help` option, e.g. ::
  44. > python setup.py --help build_ext
  45. [...]
  46. Options for 'build_ext' command:
  47. --build-lib (-b) directory for compiled extension modules
  48. --build-temp (-t) directory for temporary files (build by-products)
  49. --inplace (-i) ignore build-lib and put compiled extensions into the
  50. source directory alongside your pure Python modules
  51. --include-dirs (-I) list of directories to search for header files
  52. --define (-D) C preprocessor macros to define
  53. --undef (-U) C preprocessor macros to undefine
  54. --swig-opts list of SWIG command line options
  55. [...]
  56. Note that an option spelled :option:`--foo-bar` on the command-line is spelled
  57. :option:`foo_bar` in configuration files.
  58. For example, say you want your extensions to be built "in-place"---that is, you
  59. have an extension :mod:`pkg.ext`, and you want the compiled extension file
  60. (:file:`ext.so` on Unix, say) to be put in the same source directory as your
  61. pure Python modules :mod:`pkg.mod1` and :mod:`pkg.mod2`. You can always use the
  62. :option:`--inplace` option on the command-line to ensure this::
  63. python setup.py build_ext --inplace
  64. But this requires that you always specify the :command:`build_ext` command
  65. explicitly, and remember to provide :option:`--inplace`. An easier way is to
  66. "set and forget" this option, by encoding it in :file:`setup.cfg`, the
  67. configuration file for this distribution::
  68. [build_ext]
  69. inplace=1
  70. This will affect all builds of this module distribution, whether or not you
  71. explicitly specify :command:`build_ext`. If you include :file:`setup.cfg` in
  72. your source distribution, it will also affect end-user builds---which is
  73. probably a bad idea for this option, since always building extensions in-place
  74. would break installation of the module distribution. In certain peculiar cases,
  75. though, modules are built right in their installation directory, so this is
  76. conceivably a useful ability. (Distributing extensions that expect to be built
  77. in their installation directory is almost always a bad idea, though.)
  78. Another example: certain commands take a lot of options that don't change from
  79. run to run; for example, :command:`bdist_rpm` needs to know everything required
  80. to generate a "spec" file for creating an RPM distribution. Some of this
  81. information comes from the setup script, and some is automatically generated by
  82. the Distutils (such as the list of files installed). But some of it has to be
  83. supplied as options to :command:`bdist_rpm`, which would be very tedious to do
  84. on the command-line for every run. Hence, here is a snippet from the Distutils'
  85. own :file:`setup.cfg`::
  86. [bdist_rpm]
  87. release = 1
  88. packager = Greg Ward <gward@python.net>
  89. doc_files = CHANGES.txt
  90. README.txt
  91. USAGE.txt
  92. doc/
  93. examples/
  94. Note that the :option:`doc_files` option is simply a whitespace-separated string
  95. split across multiple lines for readability.
  96. .. seealso::
  97. :ref:`inst-config-syntax` in "Installing Python Modules"
  98. More information on the configuration files is available in the manual for
  99. system administrators.
  100. .. rubric:: Footnotes
  101. .. [#] This ideal probably won't be achieved until auto-configuration is fully
  102. supported by the Distutils.