Greg Ward | 76ec0d6 | 2000-03-02 01:57:12 +0000 | [diff] [blame] | 1 | """distutils.command.build_clib |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 2 | |
Greg Ward | 76ec0d6 | 2000-03-02 01:57:12 +0000 | [diff] [blame] | 3 | Implements the Distutils 'build_clib' command, to build a C/C++ library |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 4 | that is included in the module distribution and needed by an extension |
| 5 | module.""" |
| 6 | |
| 7 | # created (an empty husk) 1999/12/18, Greg Ward |
| 8 | # fleshed out 2000/02/03-04 |
| 9 | |
Greg Ward | 3ce77fd | 2000-03-02 01:49:45 +0000 | [diff] [blame] | 10 | __revision__ = "$Id$" |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 11 | |
| 12 | |
| 13 | # XXX this module has *lots* of code ripped-off quite transparently from |
| 14 | # build_ext.py -- not surprisingly really, as the work required to build |
| 15 | # a static library from a collection of C source files is not really all |
| 16 | # that different from what's required to build a shared object file from |
| 17 | # a collection of C source files. Nevertheless, I haven't done the |
| 18 | # necessary refactoring to account for the overlap in code between the |
| 19 | # two modules, mainly because a number of subtle details changed in the |
| 20 | # cut 'n paste. Sigh. |
| 21 | |
| 22 | import os, string |
| 23 | from types import * |
| 24 | from distutils.core import Command |
| 25 | from distutils.errors import * |
| 26 | from distutils.ccompiler import new_compiler |
| 27 | |
| 28 | |
Greg Ward | 76ec0d6 | 2000-03-02 01:57:12 +0000 | [diff] [blame] | 29 | class build_clib (Command): |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 30 | |
Greg Ward | 833dfd5 | 2000-03-02 01:27:36 +0000 | [diff] [blame] | 31 | description = "build C/C++ libraries used by Python extensions" |
| 32 | |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 33 | user_options = [ |
Greg Ward | 833dfd5 | 2000-03-02 01:27:36 +0000 | [diff] [blame] | 34 | ('build-clib', 'b', |
| 35 | "directory to build C/C++ libraries to"), |
| 36 | ('build-temp', 't', |
| 37 | "directory to put temporary build by-products"), |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 38 | ('debug', 'g', |
| 39 | "compile with debugging information"), |
| 40 | ] |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 41 | |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame] | 42 | def initialize_options (self): |
Greg Ward | 833dfd5 | 2000-03-02 01:27:36 +0000 | [diff] [blame] | 43 | self.build_clib = None |
| 44 | self.build_temp = None |
| 45 | |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 46 | # List of libraries to build |
| 47 | self.libraries = None |
| 48 | |
| 49 | # Compilation options for all libraries |
| 50 | self.include_dirs = None |
| 51 | self.define = None |
| 52 | self.undef = None |
Greg Ward | e8c6ce4 | 2000-02-09 02:20:14 +0000 | [diff] [blame] | 53 | self.debug = None |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 54 | |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame] | 55 | # initialize_options() |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 56 | |
Greg Ward | 833dfd5 | 2000-03-02 01:27:36 +0000 | [diff] [blame] | 57 | |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame] | 58 | def finalize_options (self): |
Greg Ward | 833dfd5 | 2000-03-02 01:27:36 +0000 | [diff] [blame] | 59 | |
| 60 | # This might be confusing: both build-clib and build-temp default |
| 61 | # to build-temp as defined by the "build" command. This is because |
| 62 | # I think that C libraries are really just temporary build |
| 63 | # by-products, at least from the point of view of building Python |
| 64 | # extensions -- but I want to keep my options open. |
Greg Ward | e8c6ce4 | 2000-02-09 02:20:14 +0000 | [diff] [blame] | 65 | self.set_undefined_options ('build', |
Greg Ward | 833dfd5 | 2000-03-02 01:27:36 +0000 | [diff] [blame] | 66 | ('build_temp', 'build_clib'), |
| 67 | ('build_temp', 'build_temp'), |
Greg Ward | e8c6ce4 | 2000-02-09 02:20:14 +0000 | [diff] [blame] | 68 | ('debug', 'debug')) |
Greg Ward | 833dfd5 | 2000-03-02 01:27:36 +0000 | [diff] [blame] | 69 | |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 70 | self.libraries = self.distribution.libraries |
Greg Ward | 833dfd5 | 2000-03-02 01:27:36 +0000 | [diff] [blame] | 71 | if self.libraries: |
| 72 | self.check_library_list (self.libraries) |
| 73 | |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 74 | if self.include_dirs is None: |
| 75 | self.include_dirs = self.distribution.include_dirs or [] |
| 76 | if type (self.include_dirs) is StringType: |
| 77 | self.include_dirs = string.split (self.include_dirs, |
| 78 | os.pathsep) |
| 79 | |
| 80 | # XXX same as for build_ext -- what about 'self.define' and |
| 81 | # 'self.undef' ? |
| 82 | |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame] | 83 | # finalize_options() |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 84 | |
| 85 | |
| 86 | def run (self): |
| 87 | |
| 88 | if not self.libraries: |
| 89 | return |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 90 | |
| 91 | # Yech -- this is cut 'n pasted from build_ext.py! |
| 92 | self.compiler = new_compiler (plat=os.environ.get ('PLAT'), |
| 93 | verbose=self.verbose, |
| 94 | dry_run=self.dry_run, |
| 95 | force=self.force) |
| 96 | if self.include_dirs is not None: |
| 97 | self.compiler.set_include_dirs (self.include_dirs) |
| 98 | if self.define is not None: |
| 99 | # 'define' option is a list of (name,value) tuples |
| 100 | for (name,value) in self.define: |
| 101 | self.compiler.define_macro (name, value) |
| 102 | if self.undef is not None: |
| 103 | for macro in self.undef: |
| 104 | self.compiler.undefine_macro (macro) |
| 105 | |
| 106 | self.build_libraries (self.libraries) |
| 107 | |
| 108 | # run() |
| 109 | |
| 110 | |
| 111 | def check_library_list (self, libraries): |
| 112 | """Ensure that the list of libraries (presumably provided as a |
| 113 | command option 'libraries') is valid, i.e. it is a list of |
| 114 | 2-tuples, where the tuples are (library_name, build_info_dict). |
| 115 | Raise DistutilsValueError if the structure is invalid anywhere; |
| 116 | just returns otherwise.""" |
| 117 | |
| 118 | # Yechh, blecch, ackk: this is ripped straight out of build_ext.py, |
| 119 | # with only names changed to protect the innocent! |
| 120 | |
| 121 | if type (libraries) is not ListType: |
| 122 | raise DistutilsValueError, \ |
| 123 | "'libraries' option must be a list of tuples" |
| 124 | |
| 125 | for lib in libraries: |
| 126 | if type (lib) is not TupleType and len (lib) != 2: |
| 127 | raise DistutilsValueError, \ |
| 128 | "each element of 'libraries' must a 2-tuple" |
| 129 | |
| 130 | if type (lib[0]) is not StringType: |
| 131 | raise DistutilsValueError, \ |
| 132 | "first element of each tuple in 'libraries' " + \ |
| 133 | "must be a string (the library name)" |
Greg Ward | 833dfd5 | 2000-03-02 01:27:36 +0000 | [diff] [blame] | 134 | if '/' in lib[0] or (os.sep != '/' and os.sep in lib[0]): |
| 135 | raise DistutilsValueError, \ |
| 136 | ("bad library name '%s': " + |
| 137 | "may not contain directory separators") % \ |
| 138 | lib[0] |
| 139 | |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 140 | if type (lib[1]) is not DictionaryType: |
| 141 | raise DistutilsValueError, \ |
| 142 | "second element of each tuple in 'libraries' " + \ |
| 143 | "must be a dictionary (build info)" |
| 144 | # for lib |
| 145 | |
| 146 | # check_library_list () |
| 147 | |
| 148 | |
Greg Ward | 833dfd5 | 2000-03-02 01:27:36 +0000 | [diff] [blame] | 149 | def get_library_names (self): |
| 150 | # Assume the library list is valid -- 'check_library_list()' is |
| 151 | # called from 'finalize_options()', so it should be! |
| 152 | |
| 153 | if not self.libraries: |
| 154 | return None |
| 155 | |
| 156 | lib_names = [] |
| 157 | for (lib_name, build_info) in self.libraries: |
| 158 | lib_names.append (lib_name) |
| 159 | return lib_names |
| 160 | |
| 161 | # get_library_names () |
| 162 | |
| 163 | |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 164 | def build_libraries (self, libraries): |
| 165 | |
| 166 | compiler = self.compiler |
| 167 | |
| 168 | for (lib_name, build_info) in libraries: |
| 169 | sources = build_info.get ('sources') |
| 170 | if sources is None or type (sources) not in (ListType, TupleType): |
| 171 | raise DistutilsValueError, \ |
| 172 | ("in 'libraries' option (library '%s'), " + |
| 173 | "'sources' must be present and must be " + |
| 174 | "a list of source filenames") % lib_name |
| 175 | sources = list (sources) |
| 176 | |
| 177 | self.announce ("building '%s' library" % lib_name) |
| 178 | |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 179 | # First, compile the source code to object files in the library |
| 180 | # directory. (This should probably change to putting object |
| 181 | # files in a temporary build directory.) |
| 182 | macros = build_info.get ('macros') |
| 183 | include_dirs = build_info.get ('include_dirs') |
| 184 | objects = self.compiler.compile (sources, |
Greg Ward | 833dfd5 | 2000-03-02 01:27:36 +0000 | [diff] [blame] | 185 | output_dir=self.build_temp, |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 186 | macros=macros, |
| 187 | include_dirs=include_dirs, |
Greg Ward | e8c6ce4 | 2000-02-09 02:20:14 +0000 | [diff] [blame] | 188 | debug=self.debug) |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 189 | |
| 190 | # Now "link" the object files together into a static library. |
| 191 | # (On Unix at least, this isn't really linking -- it just |
| 192 | # builds an archive. Whatever.) |
Greg Ward | 833dfd5 | 2000-03-02 01:27:36 +0000 | [diff] [blame] | 193 | self.compiler.link_static_lib (objects, lib_name, |
| 194 | output_dir=self.build_clib, |
| 195 | debug=self.debug) |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 196 | |
| 197 | # for libraries |
| 198 | |
| 199 | # build_libraries () |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 200 | |
| 201 | # class BuildLib |