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