blob: 5388ccd4d47af002bbd83ce0bf6e07f2063b7fca [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
Éric Araujo229011d2011-09-18 20:11:48 +020019from packaging.compiler import customize_compiler, new_compiler
Tarek Ziade1231a4e2011-05-19 13:07:25 +020020from 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!
Tarek Ziade1231a4e2011-05-19 13:07:25 +020096 self.compiler = new_compiler(compiler=self.compiler,
97 dry_run=self.dry_run,
98 force=self.force)
99 customize_compiler(self.compiler)
100
101 if self.include_dirs is not None:
102 self.compiler.set_include_dirs(self.include_dirs)
103 if self.define is not None:
104 # 'define' option is a list of (name,value) tuples
105 for name, value in self.define:
106 self.compiler.define_macro(name, value)
107 if self.undef is not None:
108 for macro in self.undef:
109 self.compiler.undefine_macro(macro)
110
111 self.build_libraries(self.libraries)
112
113
114 def check_library_list(self, libraries):
115 """Ensure that the list of libraries is valid.
116
117 `library` is presumably provided as a command option 'libraries'.
118 This method checks that it is a list of 2-tuples, where the tuples
119 are (library_name, build_info_dict).
120
121 Raise PackagingSetupError if the structure is invalid anywhere;
122 just returns otherwise.
123 """
124 if not isinstance(libraries, list):
125 raise PackagingSetupError("'libraries' option must be a list of tuples")
126
127 for lib in libraries:
128 if not isinstance(lib, tuple) and len(lib) != 2:
129 raise PackagingSetupError("each element of 'libraries' must a 2-tuple")
130
131 name, build_info = lib
132
133 if not isinstance(name, str):
134 raise PackagingSetupError("first element of each tuple in 'libraries' " + \
135 "must be a string (the library name)")
136 if '/' in name or (os.sep != '/' and os.sep in name):
137 raise PackagingSetupError(("bad library name '%s': " +
138 "may not contain directory separators") % \
139 lib[0])
140
141 if not isinstance(build_info, dict):
142 raise PackagingSetupError("second element of each tuple in 'libraries' " + \
143 "must be a dictionary (build info)")
144
145 def get_library_names(self):
146 # Assume the library list is valid -- 'check_library_list()' is
147 # called from 'finalize_options()', so it should be!
148 if not self.libraries:
149 return None
150
151 lib_names = []
152 for lib_name, build_info in self.libraries:
153 lib_names.append(lib_name)
154 return lib_names
155
156
157 def get_source_files(self):
158 self.check_library_list(self.libraries)
159 filenames = []
160 for lib_name, build_info in self.libraries:
161 sources = build_info.get('sources')
162 if sources is None or not isinstance(sources, (list, tuple)):
163 raise PackagingSetupError(("in 'libraries' option (library '%s'), "
164 "'sources' must be present and must be "
165 "a list of source filenames") % lib_name)
166
167 filenames.extend(sources)
168 return filenames
169
170 def build_libraries(self, libraries):
171 for lib_name, build_info in libraries:
172 sources = build_info.get('sources')
173 if sources is None or not isinstance(sources, (list, tuple)):
174 raise PackagingSetupError(("in 'libraries' option (library '%s'), " +
175 "'sources' must be present and must be " +
176 "a list of source filenames") % lib_name)
177 sources = list(sources)
178
179 logger.info("building '%s' library", lib_name)
180
181 # First, compile the source code to object files in the library
182 # directory. (This should probably change to putting object
183 # files in a temporary build directory.)
184 macros = build_info.get('macros')
185 include_dirs = build_info.get('include_dirs')
186 objects = self.compiler.compile(sources,
187 output_dir=self.build_temp,
188 macros=macros,
189 include_dirs=include_dirs,
190 debug=self.debug)
191
192 # Now "link" the object files together into a static library.
193 # (On Unix at least, this isn't really linking -- it just
194 # builds an archive. Whatever.)
195 self.compiler.create_static_lib(objects, lib_name,
196 output_dir=self.build_clib,
197 debug=self.debug)