blob: 955cf5650cc4547ff2c2d2e413a9ff9d73859bd8 [file] [log] [blame]
Greg Wardaaf27ee2000-02-05 02:23:59 +00001"""distutils.command.build_lib
2
3Implements the Distutils 'build_lib' command, to build a C/C++ library
4that is included in the module distribution and needed by an extension
5module."""
6
7# created (an empty husk) 1999/12/18, Greg Ward
8# fleshed out 2000/02/03-04
9
Greg Ward3ce77fd2000-03-02 01:49:45 +000010__revision__ = "$Id$"
Greg Wardaaf27ee2000-02-05 02:23:59 +000011
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
22import os, string
23from types import *
24from distutils.core import Command
25from distutils.errors import *
26from distutils.ccompiler import new_compiler
27
28
Greg Ward1993f9a2000-02-18 00:13:53 +000029class build_lib (Command):
Greg Wardaaf27ee2000-02-05 02:23:59 +000030
Greg Ward833dfd52000-03-02 01:27:36 +000031 description = "build C/C++ libraries used by Python extensions"
32
Greg Wardbbeceea2000-02-18 00:25:39 +000033 user_options = [
Greg Ward833dfd52000-03-02 01:27:36 +000034 ('build-clib', 'b',
35 "directory to build C/C++ libraries to"),
36 ('build-temp', 't',
37 "directory to put temporary build by-products"),
Greg Wardbbeceea2000-02-18 00:25:39 +000038 ('debug', 'g',
39 "compile with debugging information"),
40 ]
Greg Wardaaf27ee2000-02-05 02:23:59 +000041
Greg Warde01149c2000-02-18 00:35:22 +000042 def initialize_options (self):
Greg Ward833dfd52000-03-02 01:27:36 +000043 self.build_clib = None
44 self.build_temp = None
45
Greg Wardaaf27ee2000-02-05 02:23:59 +000046 # 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 Warde8c6ce42000-02-09 02:20:14 +000053 self.debug = None
Greg Wardaaf27ee2000-02-05 02:23:59 +000054
Greg Warde01149c2000-02-18 00:35:22 +000055 # initialize_options()
Greg Wardaaf27ee2000-02-05 02:23:59 +000056
Greg Ward833dfd52000-03-02 01:27:36 +000057
Greg Warde01149c2000-02-18 00:35:22 +000058 def finalize_options (self):
Greg Ward833dfd52000-03-02 01:27:36 +000059
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 Warde8c6ce42000-02-09 02:20:14 +000065 self.set_undefined_options ('build',
Greg Ward833dfd52000-03-02 01:27:36 +000066 ('build_temp', 'build_clib'),
67 ('build_temp', 'build_temp'),
Greg Warde8c6ce42000-02-09 02:20:14 +000068 ('debug', 'debug'))
Greg Ward833dfd52000-03-02 01:27:36 +000069
Greg Wardaaf27ee2000-02-05 02:23:59 +000070 self.libraries = self.distribution.libraries
Greg Ward833dfd52000-03-02 01:27:36 +000071 if self.libraries:
72 self.check_library_list (self.libraries)
73
Greg Wardaaf27ee2000-02-05 02:23:59 +000074 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 Warde01149c2000-02-18 00:35:22 +000083 # finalize_options()
Greg Wardaaf27ee2000-02-05 02:23:59 +000084
85
86 def run (self):
87
88 if not self.libraries:
89 return
Greg Wardaaf27ee2000-02-05 02:23:59 +000090
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 Ward833dfd52000-03-02 01:27:36 +0000134 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 Wardaaf27ee2000-02-05 02:23:59 +0000140 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 Ward833dfd52000-03-02 01:27:36 +0000149 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 Wardaaf27ee2000-02-05 02:23:59 +0000164 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 Wardaaf27ee2000-02-05 02:23:59 +0000179 # 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 Ward833dfd52000-03-02 01:27:36 +0000185 output_dir=self.build_temp,
186 keep_dir=1,
Greg Wardaaf27ee2000-02-05 02:23:59 +0000187 macros=macros,
188 include_dirs=include_dirs,
Greg Warde8c6ce42000-02-09 02:20:14 +0000189 debug=self.debug)
Greg Wardaaf27ee2000-02-05 02:23:59 +0000190
191 # Now "link" the object files together into a static library.
192 # (On Unix at least, this isn't really linking -- it just
193 # builds an archive. Whatever.)
Greg Ward833dfd52000-03-02 01:27:36 +0000194 self.compiler.link_static_lib (objects, lib_name,
195 output_dir=self.build_clib,
196 debug=self.debug)
Greg Wardaaf27ee2000-02-05 02:23:59 +0000197
198 # for libraries
199
200 # build_libraries ()
Greg Wardaaf27ee2000-02-05 02:23:59 +0000201
202# class BuildLib