py-cvs-2001_07_13 (Rev 1.3) merge
[python/dscho.git] / Lib / distutils / command / build_clib.py
blob063da915524461236db11e850cfedcf08915efeb
1 """distutils.command.build_clib
3 Implements the Distutils 'build_clib' command, to build a C/C++ library
4 that is included in the module distribution and needed by an extension
5 module."""
7 # created (an empty husk) 1999/12/18, Greg Ward
8 # fleshed out 2000/02/03-04
10 __revision__ = "$Id$"
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.
22 import os, string
23 from types import *
24 from distutils.core import Command
25 from distutils.errors import *
26 from distutils.sysconfig import customize_compiler
29 def show_compilers ():
30 from distutils.ccompiler import show_compilers
31 show_compilers()
34 class build_clib (Command):
36 description = "build C/C++ libraries used by Python extensions"
38 user_options = [
39 ('build-clib', 'b',
40 "directory to build C/C++ libraries to"),
41 ('build-temp', 't',
42 "directory to put temporary build by-products"),
43 ('debug', 'g',
44 "compile with debugging information"),
45 ('force', 'f',
46 "forcibly build everything (ignore file timestamps)"),
47 ('compiler=', 'c',
48 "specify the compiler type"),
51 boolean_options = ['debug', 'force']
53 help_options = [
54 ('help-compiler', None,
55 "list available compilers", show_compilers),
58 def initialize_options (self):
59 self.build_clib = None
60 self.build_temp = None
62 # List of libraries to build
63 self.libraries = None
65 # Compilation options for all libraries
66 self.include_dirs = None
67 self.define = None
68 self.undef = None
69 self.debug = None
70 self.force = 0
71 self.compiler = None
73 # initialize_options()
76 def finalize_options (self):
78 # This might be confusing: both build-clib and build-temp default
79 # to build-temp as defined by the "build" command. This is because
80 # I think that C libraries are really just temporary build
81 # by-products, at least from the point of view of building Python
82 # extensions -- but I want to keep my options open.
83 self.set_undefined_options('build',
84 ('build_temp', 'build_clib'),
85 ('build_temp', 'build_temp'),
86 ('compiler', 'compiler'),
87 ('debug', 'debug'),
88 ('force', 'force'))
90 self.libraries = self.distribution.libraries
91 if self.libraries:
92 self.check_library_list(self.libraries)
94 if self.include_dirs is None:
95 self.include_dirs = self.distribution.include_dirs or []
96 if type(self.include_dirs) is StringType:
97 self.include_dirs = string.split(self.include_dirs,
98 os.pathsep)
100 # XXX same as for build_ext -- what about 'self.define' and
101 # 'self.undef' ?
103 # finalize_options()
106 def run (self):
108 if not self.libraries:
109 return
111 # Yech -- this is cut 'n pasted from build_ext.py!
112 from distutils.ccompiler import new_compiler
113 self.compiler = new_compiler(compiler=self.compiler,
114 verbose=self.verbose,
115 dry_run=self.dry_run,
116 force=self.force)
117 customize_compiler(self.compiler)
119 if self.include_dirs is not None:
120 self.compiler.set_include_dirs(self.include_dirs)
121 if self.define is not None:
122 # 'define' option is a list of (name,value) tuples
123 for (name,value) in self.define:
124 self.compiler.define_macro(name, value)
125 if self.undef is not None:
126 for macro in self.undef:
127 self.compiler.undefine_macro(macro)
129 self.build_libraries(self.libraries)
131 # run()
134 def check_library_list (self, libraries):
135 """Ensure that the list of libraries (presumably provided as a
136 command option 'libraries') is valid, i.e. it is a list of
137 2-tuples, where the tuples are (library_name, build_info_dict).
138 Raise DistutilsSetupError if the structure is invalid anywhere;
139 just returns otherwise."""
141 # Yechh, blecch, ackk: this is ripped straight out of build_ext.py,
142 # with only names changed to protect the innocent!
144 if type(libraries) is not ListType:
145 raise DistutilsSetupError, \
146 "'libraries' option must be a list of tuples"
148 for lib in libraries:
149 if type(lib) is not TupleType and len(lib) != 2:
150 raise DistutilsSetupError, \
151 "each element of 'libraries' must a 2-tuple"
153 if type(lib[0]) is not StringType:
154 raise DistutilsSetupError, \
155 "first element of each tuple in 'libraries' " + \
156 "must be a string (the library name)"
157 if '/' in lib[0] or (os.sep != '/' and os.sep in lib[0]):
158 raise DistutilsSetupError, \
159 ("bad library name '%s': " +
160 "may not contain directory separators") % \
161 lib[0]
163 if type(lib[1]) is not DictionaryType:
164 raise DistutilsSetupError, \
165 "second element of each tuple in 'libraries' " + \
166 "must be a dictionary (build info)"
167 # for lib
169 # check_library_list ()
172 def get_library_names (self):
173 # Assume the library list is valid -- 'check_library_list()' is
174 # called from 'finalize_options()', so it should be!
176 if not self.libraries:
177 return None
179 lib_names = []
180 for (lib_name, build_info) in self.libraries:
181 lib_names.append(lib_name)
182 return lib_names
184 # get_library_names ()
187 def build_libraries (self, libraries):
189 compiler = self.compiler
191 for (lib_name, build_info) in libraries:
192 sources = build_info.get('sources')
193 if sources is None or type(sources) not in (ListType, TupleType):
194 raise DistutilsSetupError, \
195 ("in 'libraries' option (library '%s'), " +
196 "'sources' must be present and must be " +
197 "a list of source filenames") % lib_name
198 sources = list(sources)
200 self.announce("building '%s' library" % lib_name)
202 # First, compile the source code to object files in the library
203 # directory. (This should probably change to putting object
204 # files in a temporary build directory.)
205 macros = build_info.get('macros')
206 include_dirs = build_info.get('include_dirs')
207 objects = self.compiler.compile(sources,
208 output_dir=self.build_temp,
209 macros=macros,
210 include_dirs=include_dirs,
211 debug=self.debug)
213 # Now "link" the object files together into a static library.
214 # (On Unix at least, this isn't really linking -- it just
215 # builds an archive. Whatever.)
216 self.compiler.create_static_lib(objects, lib_name,
217 output_dir=self.build_clib,
218 debug=self.debug)
220 # for libraries
222 # build_libraries ()
224 # class build_lib