| 1 |
"""distutils.command.build_clib |
| 2 |
|
| 3 |
Implements the Distutils 'build_clib' command, to build a C/C++ library |
| 4 |
that is included in the module distribution and needed by an extension |
| 5 |
module.""" |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import os |
| 18 |
from distutils.core import Command |
| 19 |
from distutils.errors import * |
| 20 |
from distutils.sysconfig import customize_compiler |
| 21 |
from distutils import log |
| 22 |
|
| 23 |
def show_compilers(): |
| 24 |
from distutils.ccompiler import show_compilers |
| 25 |
show_compilers() |
| 26 |
|
| 27 |
|
| 28 |
class build_clib(Command): |
| 29 |
|
| 30 |
description = "build C/C++ libraries used by Python extensions" |
| 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 |
|
| 57 |
self.libraries = None |
| 58 |
|
| 59 |
|
| 60 |
self.include_dirs = None |
| 61 |
self.define = None |
| 62 |
self.undef = None |
| 63 |
self.debug = None |
| 64 |
self.force = 0 |
| 65 |
self.compiler = None |
| 66 |
|
| 67 |
|
| 68 |
def finalize_options(self): |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
self.set_undefined_options('build', |
| 75 |
('build_temp', 'build_clib'), |
| 76 |
('build_temp', 'build_temp'), |
| 77 |
('compiler', 'compiler'), |
| 78 |
('debug', 'debug'), |
| 79 |
('force', 'force')) |
| 80 |
|
| 81 |
self.libraries = self.distribution.libraries |
| 82 |
if self.libraries: |
| 83 |
self.check_library_list(self.libraries) |
| 84 |
|
| 85 |
if self.include_dirs is None: |
| 86 |
self.include_dirs = self.distribution.include_dirs or [] |
| 87 |
if isinstance(self.include_dirs, str): |
| 88 |
self.include_dirs = self.include_dirs.split(os.pathsep) |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
def run(self): |
| 95 |
if not self.libraries: |
| 96 |
return |
| 97 |
|
| 98 |
|
| 99 |
from distutils.ccompiler import new_compiler |
| 100 |
self.compiler = new_compiler(compiler=self.compiler, |
| 101 |
dry_run=self.dry_run, |
| 102 |
force=self.force) |
| 103 |
customize_compiler(self.compiler) |
| 104 |
|
| 105 |
if self.include_dirs is not None: |
| 106 |
self.compiler.set_include_dirs(self.include_dirs) |
| 107 |
if self.define is not None: |
| 108 |
|
| 109 |
for (name,value) in self.define: |
| 110 |
self.compiler.define_macro(name, value) |
| 111 |
if self.undef is not None: |
| 112 |
for macro in self.undef: |
| 113 |
self.compiler.undefine_macro(macro) |
| 114 |
|
| 115 |
self.build_libraries(self.libraries) |
| 116 |
|
| 117 |
|
| 118 |
def check_library_list(self, libraries): |
| 119 |
"""Ensure that the list of libraries is valid. |
| 120 |
|
| 121 |
`library` is presumably provided as a command option 'libraries'. |
| 122 |
This method checks that it is a list of 2-tuples, where the tuples |
| 123 |
are (library_name, build_info_dict). |
| 124 |
|
| 125 |
Raise DistutilsSetupError if the structure is invalid anywhere; |
| 126 |
just returns otherwise. |
| 127 |
""" |
| 128 |
if not isinstance(libraries, list): |
| 129 |
raise DistutilsSetupError( |
| 130 |
"'libraries' option must be a list of tuples") |
| 131 |
|
| 132 |
for lib in libraries: |
| 133 |
if not isinstance(lib, tuple) and len(lib) != 2: |
| 134 |
raise DistutilsSetupError( |
| 135 |
"each element of 'libraries' must a 2-tuple") |
| 136 |
|
| 137 |
name, build_info = lib |
| 138 |
|
| 139 |
if not isinstance(name, str): |
| 140 |
raise DistutilsSetupError( |
| 141 |
"first element of each tuple in 'libraries' " |
| 142 |
"must be a string (the library name)") |
| 143 |
|
| 144 |
if '/' in name or (os.sep != '/' and os.sep in name): |
| 145 |
raise DistutilsSetupError("bad library name '%s': " |
| 146 |
"may not contain directory separators" % lib[0]) |
| 147 |
|
| 148 |
if not isinstance(build_info, dict): |
| 149 |
raise DistutilsSetupError( |
| 150 |
"second element of each tuple in 'libraries' " |
| 151 |
"must be a dictionary (build info)") |
| 152 |
|
| 153 |
|
| 154 |
def get_library_names(self): |
| 155 |
|
| 156 |
|
| 157 |
if not self.libraries: |
| 158 |
return None |
| 159 |
|
| 160 |
lib_names = [] |
| 161 |
for (lib_name, build_info) in self.libraries: |
| 162 |
lib_names.append(lib_name) |
| 163 |
return lib_names |
| 164 |
|
| 165 |
|
| 166 |
def get_source_files(self): |
| 167 |
self.check_library_list(self.libraries) |
| 168 |
filenames = [] |
| 169 |
for (lib_name, build_info) in self.libraries: |
| 170 |
sources = build_info.get('sources') |
| 171 |
if sources is None or not isinstance(sources, (list, tuple)): |
| 172 |
raise DistutilsSetupError( |
| 173 |
"in 'libraries' option (library '%s'), " |
| 174 |
"'sources' must be present and must be " |
| 175 |
"a list of source filenames" % lib_name) |
| 176 |
|
| 177 |
filenames.extend(sources) |
| 178 |
return filenames |
| 179 |
|
| 180 |
|
| 181 |
def build_libraries(self, libraries): |
| 182 |
for (lib_name, build_info) in libraries: |
| 183 |
sources = build_info.get('sources') |
| 184 |
if sources is None or not isinstance(sources, (list, tuple)): |
| 185 |
raise DistutilsSetupError( |
| 186 |
"in 'libraries' option (library '%s'), " |
| 187 |
"'sources' must be present and must be " |
| 188 |
"a list of source filenames" % lib_name) |
| 189 |
sources = list(sources) |
| 190 |
|
| 191 |
log.info("building '%s' library", lib_name) |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
macros = build_info.get('macros') |
| 197 |
include_dirs = build_info.get('include_dirs') |
| 198 |
objects = self.compiler.compile(sources, |
| 199 |
output_dir=self.build_temp, |
| 200 |
macros=macros, |
| 201 |
include_dirs=include_dirs, |
| 202 |
debug=self.debug) |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
self.compiler.create_static_lib(objects, lib_name, |
| 208 |
output_dir=self.build_clib, |
| 209 |
debug=self.debug) |