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