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
7 # created (an empty husk) 1999/12/18, Greg Ward
8 # fleshed out 2000/02/03-04
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
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
34 class build_clib (Command
):
36 description
= "build C/C++ libraries used by Python extensions"
40 "directory to build C/C++ libraries to"),
42 "directory to put temporary build by-products"),
44 "compile with debugging information"),
46 "forcibly build everything (ignore file timestamps)"),
48 "specify the compiler type"),
52 ('help-compiler', None,
53 "list available compilers", show_compilers
),
56 def initialize_options (self
):
57 self
.build_clib
= None
58 self
.build_temp
= None
60 # List of libraries to build
63 # Compilation options for all libraries
64 self
.include_dirs
= None
71 # initialize_options()
74 def finalize_options (self
):
76 # This might be confusing: both build-clib and build-temp default
77 # to build-temp as defined by the "build" command. This is because
78 # I think that C libraries are really just temporary build
79 # by-products, at least from the point of view of building Python
80 # extensions -- but I want to keep my options open.
81 self
.set_undefined_options ('build',
82 ('build_temp', 'build_clib'),
83 ('build_temp', 'build_temp'),
84 ('compiler', 'compiler'),
88 self
.libraries
= self
.distribution
.libraries
90 self
.check_library_list (self
.libraries
)
92 if self
.include_dirs
is None:
93 self
.include_dirs
= self
.distribution
.include_dirs
or []
94 if type (self
.include_dirs
) is StringType
:
95 self
.include_dirs
= string
.split (self
.include_dirs
,
98 # XXX same as for build_ext -- what about 'self.define' and
106 if not self
.libraries
:
109 # Yech -- this is cut 'n pasted from build_ext.py!
110 from distutils
.ccompiler
import new_compiler
111 self
.compiler
= new_compiler (compiler
=self
.compiler
,
112 verbose
=self
.verbose
,
113 dry_run
=self
.dry_run
,
115 customize_compiler(self
.compiler
)
117 if self
.include_dirs
is not None:
118 self
.compiler
.set_include_dirs (self
.include_dirs
)
119 if self
.define
is not None:
120 # 'define' option is a list of (name,value) tuples
121 for (name
,value
) in self
.define
:
122 self
.compiler
.define_macro (name
, value
)
123 if self
.undef
is not None:
124 for macro
in self
.undef
:
125 self
.compiler
.undefine_macro (macro
)
127 self
.build_libraries (self
.libraries
)
132 def check_library_list (self
, libraries
):
133 """Ensure that the list of libraries (presumably provided as a
134 command option 'libraries') is valid, i.e. it is a list of
135 2-tuples, where the tuples are (library_name, build_info_dict).
136 Raise DistutilsSetupError if the structure is invalid anywhere;
137 just returns otherwise."""
139 # Yechh, blecch, ackk: this is ripped straight out of build_ext.py,
140 # with only names changed to protect the innocent!
142 if type (libraries
) is not ListType
:
143 raise DistutilsSetupError
, \
144 "'libraries' option must be a list of tuples"
146 for lib
in libraries
:
147 if type (lib
) is not TupleType
and len (lib
) != 2:
148 raise DistutilsSetupError
, \
149 "each element of 'libraries' must a 2-tuple"
151 if type (lib
[0]) is not StringType
:
152 raise DistutilsSetupError
, \
153 "first element of each tuple in 'libraries' " + \
154 "must be a string (the library name)"
155 if '/' in lib
[0] or (os
.sep
!= '/' and os
.sep
in lib
[0]):
156 raise DistutilsSetupError
, \
157 ("bad library name '%s': " +
158 "may not contain directory separators") % \
161 if type (lib
[1]) is not DictionaryType
:
162 raise DistutilsSetupError
, \
163 "second element of each tuple in 'libraries' " + \
164 "must be a dictionary (build info)"
167 # check_library_list ()
170 def get_library_names (self
):
171 # Assume the library list is valid -- 'check_library_list()' is
172 # called from 'finalize_options()', so it should be!
174 if not self
.libraries
:
178 for (lib_name
, build_info
) in self
.libraries
:
179 lib_names
.append (lib_name
)
182 # get_library_names ()
185 def build_libraries (self
, libraries
):
187 compiler
= self
.compiler
189 for (lib_name
, build_info
) in libraries
:
190 sources
= build_info
.get ('sources')
191 if sources
is None or type (sources
) not in (ListType
, TupleType
):
192 raise DistutilsSetupError
, \
193 ("in 'libraries' option (library '%s'), " +
194 "'sources' must be present and must be " +
195 "a list of source filenames") % lib_name
196 sources
= list (sources
)
198 self
.announce ("building '%s' library" % lib_name
)
200 # First, compile the source code to object files in the library
201 # directory. (This should probably change to putting object
202 # files in a temporary build directory.)
203 macros
= build_info
.get ('macros')
204 include_dirs
= build_info
.get ('include_dirs')
205 objects
= self
.compiler
.compile (sources
,
206 output_dir
=self
.build_temp
,
208 include_dirs
=include_dirs
,
211 # Now "link" the object files together into a static library.
212 # (On Unix at least, this isn't really linking -- it just
213 # builds an archive. Whatever.)
214 self
.compiler
.create_static_lib (objects
, lib_name
,
215 output_dir
=self
.build_clib
,