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