Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 1 | """distutils.command.build_lib |
| 2 | |
| 3 | Implements the Distutils 'build_lib' command, to build a C/C++ library |
| 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 | |
| 10 | __rcsid__ = "$Id$" |
| 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 | |
| 29 | class BuildLib (Command): |
| 30 | |
Greg Ward | e8c6ce4 | 2000-02-09 02:20:14 +0000 | [diff] [blame^] | 31 | options = [('debug', 'g', |
| 32 | "compile with debugging information"), |
| 33 | ] |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 34 | |
| 35 | def set_default_options (self): |
| 36 | # List of libraries to build |
| 37 | self.libraries = None |
| 38 | |
| 39 | # Compilation options for all libraries |
| 40 | self.include_dirs = None |
| 41 | self.define = None |
| 42 | self.undef = None |
Greg Ward | e8c6ce4 | 2000-02-09 02:20:14 +0000 | [diff] [blame^] | 43 | self.debug = None |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 44 | |
| 45 | # set_default_options() |
| 46 | |
| 47 | def set_final_options (self): |
Greg Ward | e8c6ce4 | 2000-02-09 02:20:14 +0000 | [diff] [blame^] | 48 | self.set_undefined_options ('build', |
| 49 | ('debug', 'debug')) |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 50 | self.libraries = self.distribution.libraries |
| 51 | if self.include_dirs is None: |
| 52 | self.include_dirs = self.distribution.include_dirs or [] |
| 53 | if type (self.include_dirs) is StringType: |
| 54 | self.include_dirs = string.split (self.include_dirs, |
| 55 | os.pathsep) |
| 56 | |
| 57 | # XXX same as for build_ext -- what about 'self.define' and |
| 58 | # 'self.undef' ? |
| 59 | |
| 60 | # set_final_options() |
| 61 | |
| 62 | |
| 63 | def run (self): |
| 64 | |
| 65 | if not self.libraries: |
| 66 | return |
| 67 | self.check_library_list (self.libraries) |
| 68 | |
| 69 | # Yech -- this is cut 'n pasted from build_ext.py! |
| 70 | self.compiler = new_compiler (plat=os.environ.get ('PLAT'), |
| 71 | verbose=self.verbose, |
| 72 | dry_run=self.dry_run, |
| 73 | force=self.force) |
| 74 | if self.include_dirs is not None: |
| 75 | self.compiler.set_include_dirs (self.include_dirs) |
| 76 | if self.define is not None: |
| 77 | # 'define' option is a list of (name,value) tuples |
| 78 | for (name,value) in self.define: |
| 79 | self.compiler.define_macro (name, value) |
| 80 | if self.undef is not None: |
| 81 | for macro in self.undef: |
| 82 | self.compiler.undefine_macro (macro) |
| 83 | |
| 84 | self.build_libraries (self.libraries) |
| 85 | |
| 86 | # run() |
| 87 | |
| 88 | |
| 89 | def check_library_list (self, libraries): |
| 90 | """Ensure that the list of libraries (presumably provided as a |
| 91 | command option 'libraries') is valid, i.e. it is a list of |
| 92 | 2-tuples, where the tuples are (library_name, build_info_dict). |
| 93 | Raise DistutilsValueError if the structure is invalid anywhere; |
| 94 | just returns otherwise.""" |
| 95 | |
| 96 | # Yechh, blecch, ackk: this is ripped straight out of build_ext.py, |
| 97 | # with only names changed to protect the innocent! |
| 98 | |
| 99 | if type (libraries) is not ListType: |
| 100 | raise DistutilsValueError, \ |
| 101 | "'libraries' option must be a list of tuples" |
| 102 | |
| 103 | for lib in libraries: |
| 104 | if type (lib) is not TupleType and len (lib) != 2: |
| 105 | raise DistutilsValueError, \ |
| 106 | "each element of 'libraries' must a 2-tuple" |
| 107 | |
| 108 | if type (lib[0]) is not StringType: |
| 109 | raise DistutilsValueError, \ |
| 110 | "first element of each tuple in 'libraries' " + \ |
| 111 | "must be a string (the library name)" |
| 112 | if type (lib[1]) is not DictionaryType: |
| 113 | raise DistutilsValueError, \ |
| 114 | "second element of each tuple in 'libraries' " + \ |
| 115 | "must be a dictionary (build info)" |
| 116 | # for lib |
| 117 | |
| 118 | # check_library_list () |
| 119 | |
| 120 | |
| 121 | def build_libraries (self, libraries): |
| 122 | |
| 123 | compiler = self.compiler |
| 124 | |
| 125 | for (lib_name, build_info) in libraries: |
| 126 | sources = build_info.get ('sources') |
| 127 | if sources is None or type (sources) not in (ListType, TupleType): |
| 128 | raise DistutilsValueError, \ |
| 129 | ("in 'libraries' option (library '%s'), " + |
| 130 | "'sources' must be present and must be " + |
| 131 | "a list of source filenames") % lib_name |
| 132 | sources = list (sources) |
| 133 | |
| 134 | self.announce ("building '%s' library" % lib_name) |
| 135 | |
| 136 | # Extract the directory the library is intended to go in -- |
| 137 | # note translation from "universal" slash-separated form to |
| 138 | # current platform's pathname convention (so we can use the |
| 139 | # string for actual filesystem use). |
| 140 | path = tuple (string.split (lib_name, '/')[:-1]) |
| 141 | if path: |
| 142 | lib_dir = apply (os.path.join, path) |
| 143 | else: |
| 144 | lib_dir = '' |
| 145 | |
| 146 | # First, compile the source code to object files in the library |
| 147 | # directory. (This should probably change to putting object |
| 148 | # files in a temporary build directory.) |
| 149 | macros = build_info.get ('macros') |
| 150 | include_dirs = build_info.get ('include_dirs') |
| 151 | objects = self.compiler.compile (sources, |
| 152 | macros=macros, |
| 153 | include_dirs=include_dirs, |
Greg Ward | e8c6ce4 | 2000-02-09 02:20:14 +0000 | [diff] [blame^] | 154 | output_dir=lib_dir, |
| 155 | debug=self.debug) |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 156 | |
| 157 | # Now "link" the object files together into a static library. |
| 158 | # (On Unix at least, this isn't really linking -- it just |
| 159 | # builds an archive. Whatever.) |
Greg Ward | e8c6ce4 | 2000-02-09 02:20:14 +0000 | [diff] [blame^] | 160 | self.compiler.link_static_lib (objects, lib_name, debug=self.debug) |
Greg Ward | aaf27ee | 2000-02-05 02:23:59 +0000 | [diff] [blame] | 161 | |
| 162 | # for libraries |
| 163 | |
| 164 | # build_libraries () |
| 165 | |
| 166 | |
| 167 | # class BuildLib |