blob: 4a249964c5a560abf86c3747f07cd97589ace085 [file] [log] [blame]
Tarek Ziade1231a4e2011-05-19 13:07:25 +02001"""Build C/C++ libraries.
2
3This command is useful to build libraries that are included in the
4distribution and needed by extension modules.
5"""
6
7# XXX this module has *lots* of code ripped-off quite transparently from
8# build_ext.py -- not surprisingly really, as the work required to build
9# a static library from a collection of C source files is not really all
10# that different from what's required to build a shared object file from
11# a collection of C source files. Nevertheless, I haven't done the
12# necessary refactoring to account for the overlap in code between the
13# two modules, mainly because a number of subtle details changed in the
14# cut 'n paste. Sigh.
15
16import os
17from packaging.command.cmd import Command
18from packaging.errors import PackagingSetupError
19from packaging.compiler import customize_compiler
20from packaging import logger
21
22
23def show_compilers():
24 from packaging.compiler import show_compilers
25 show_compilers()
26
27
28class build_clib(Command):
29
30 description = "build C/C++ libraries used by extension modules"
31
32 user_options = [
33 ('build-clib=', 'b',
34 "directory to build C/C++ libraries to"),
35 ('build-temp=', 't',
36 "directory to put temporary build by-products"),
37 ('debug', 'g',
38 "compile with debugging information"),
39 ('force', 'f',
40 "forcibly build everything (ignore file timestamps)"),
41 ('compiler=', 'c',
42 "specify the compiler type"),
43 ]
44
45 boolean_options = ['debug', 'force']
46
47 help_options = [
48 ('help-compiler', None,
49 "list available compilers", show_compilers),
50 ]
51
52 def initialize_options(self):
53 self.build_clib = None
54 self.build_temp = None
55
56 # List of libraries to build
57 self.libraries = None
58
59 # Compilation options for all libraries
60 self.include_dirs = None
61 self.define = None
62 self.undef = None
63 self.debug = None
64 self.force = False
65 self.compiler = None
66
67
68 def finalize_options(self):
69 # This might be confusing: both build-clib and build-temp default
70 # to build-temp as defined by the "build" command. This is because
71 # I think that C libraries are really just temporary build
72 # by-products, at least from the point of view of building Python
73 # extensions -- but I want to keep my options open.
74 self.set_undefined_options('build',
75 ('build_temp', 'build_clib'),
76 ('build_temp', 'build_temp'),
77 'compiler', 'debug', 'force')
78
79 self.libraries = self.distribution.libraries
80 if self.libraries:
81 self.check_library_list(self.libraries)
82
83 if self.include_dirs is None:
84 self.include_dirs = self.distribution.include_dirs or []
85 if isinstance(self.include_dirs, str):
86 self.include_dirs = self.include_dirs.split(os.pathsep)
87
88 # XXX same as for build_ext -- what about 'self.define' and
89 # 'self.undef' ?
90
91 def run(self):
92 if not self.libraries:
93 return
94
95 # Yech -- this is cut 'n pasted from build_ext.py!
96 from packaging.compiler import new_compiler
97 self.compiler = new_compiler(compiler=self.compiler,
98 dry_run=self.dry_run,
99 force=self.force)
100 customize_compiler(self.compiler)
101
102 if self.include_dirs is not None:
103 self.compiler.set_include_dirs(self.include_dirs)
104 if self.define is not None:
105 # 'define' option is a list of (name,value) tuples
106 for name, value in self.define:
107 self.compiler.define_macro(name, value)
108 if self.undef is not None:
109 for macro in self.undef:
110 self.compiler.undefine_macro(macro)
111
112 self.build_libraries(self.libraries)
113
114
115 def check_library_list(self, libraries):
116 """Ensure that the list of libraries is valid.
117
118 `library` is presumably provided as a command option 'libraries'.
119 This method checks that it is a list of 2-tuples, where the tuples
120 are (library_name, build_info_dict).
121
122 Raise PackagingSetupError if the structure is invalid anywhere;
123 just returns otherwise.
124 """
125 if not isinstance(libraries, list):
126 raise PackagingSetupError("'libraries' option must be a list of tuples")
127
128 for lib in libraries:
129 if not isinstance(lib, tuple) and len(lib) != 2:
130 raise PackagingSetupError("each element of 'libraries' must a 2-tuple")
131
132 name, build_info = lib
133
134 if not isinstance(name, str):
135 raise PackagingSetupError("first element of each tuple in 'libraries' " + \
136 "must be a string (the library name)")
137 if '/' in name or (os.sep != '/' and os.sep in name):
138 raise PackagingSetupError(("bad library name '%s': " +
139 "may not contain directory separators") % \
140 lib[0])
141
142 if not isinstance(build_info, dict):
143 raise PackagingSetupError("second element of each tuple in 'libraries' " + \
144 "must be a dictionary (build info)")
145
146 def get_library_names(self):
147 # Assume the library list is valid -- 'check_library_list()' is
148 # called from 'finalize_options()', so it should be!
149 if not self.libraries:
150 return None
151
152 lib_names = []
153 for lib_name, build_info in self.libraries:
154 lib_names.append(lib_name)
155 return lib_names
156
157
158 def get_source_files(self):
159 self.check_library_list(self.libraries)
160 filenames = []
161 for lib_name, build_info in self.libraries:
162 sources = build_info.get('sources')
163 if sources is None or not isinstance(sources, (list, tuple)):
164 raise PackagingSetupError(("in 'libraries' option (library '%s'), "
165 "'sources' must be present and must be "
166 "a list of source filenames") % lib_name)
167
168 filenames.extend(sources)
169 return filenames
170
171 def build_libraries(self, libraries):
172 for lib_name, build_info in libraries:
173 sources = build_info.get('sources')
174 if sources is None or not isinstance(sources, (list, tuple)):
175 raise PackagingSetupError(("in 'libraries' option (library '%s'), " +
176 "'sources' must be present and must be " +
177 "a list of source filenames") % lib_name)
178 sources = list(sources)
179
180 logger.info("building '%s' library", lib_name)
181
182 # First, compile the source code to object files in the library
183 # directory. (This should probably change to putting object
184 # files in a temporary build directory.)
185 macros = build_info.get('macros')
186 include_dirs = build_info.get('include_dirs')
187 objects = self.compiler.compile(sources,
188 output_dir=self.build_temp,
189 macros=macros,
190 include_dirs=include_dirs,
191 debug=self.debug)
192
193 # Now "link" the object files together into a static library.
194 # (On Unix at least, this isn't really linking -- it just
195 # builds an archive. Whatever.)
196 self.compiler.create_static_lib(objects, lib_name,
197 output_dir=self.build_clib,
198 debug=self.debug)