mirror of
				https://github.com/python/cpython.git
				synced 2025-10-30 21:21:22 +00:00 
			
		
		
		
	Added an execution layer to be able to customize per-extension
building.
This commit is contained in:
		
							parent
							
								
									d30e587e00
								
							
						
					
					
						commit
						49c994239f
					
				
					 1 changed files with 80 additions and 79 deletions
				
			
		|  | @ -356,104 +356,105 @@ def get_outputs (self): | ||||||
| 
 | 
 | ||||||
|     # get_outputs () |     # get_outputs () | ||||||
| 
 | 
 | ||||||
| 
 |     def build_extensions(self): | ||||||
|     def build_extensions (self): |  | ||||||
| 
 | 
 | ||||||
|         # First, sanity-check the 'extensions' list |         # First, sanity-check the 'extensions' list | ||||||
|         self.check_extensions_list(self.extensions) |         self.check_extensions_list(self.extensions) | ||||||
| 
 | 
 | ||||||
|         for ext in self.extensions: |         for ext in self.extensions: | ||||||
|             sources = ext.sources |             self.build_extension(ext) | ||||||
|             if sources is None or type(sources) not in (ListType, TupleType): |  | ||||||
|                 raise DistutilsSetupError, \ |  | ||||||
|                       ("in 'ext_modules' option (extension '%s'), " + |  | ||||||
|                        "'sources' must be present and must be " + |  | ||||||
|                        "a list of source filenames") % ext.name |  | ||||||
|             sources = list(sources) |  | ||||||
| 
 | 
 | ||||||
|             fullname = self.get_ext_fullname(ext.name) |     def build_extension(self, ext): | ||||||
|             if self.inplace: |  | ||||||
|                 # ignore build-lib -- put the compiled extension into |  | ||||||
|                 # the source tree along with pure Python modules |  | ||||||
| 
 | 
 | ||||||
|                 modpath = string.split(fullname, '.') |         sources = ext.sources | ||||||
|                 package = string.join(modpath[0:-1], '.') |         if sources is None or type(sources) not in (ListType, TupleType): | ||||||
|                 base = modpath[-1] |             raise DistutilsSetupError, \ | ||||||
|  |                   ("in 'ext_modules' option (extension '%s'), " + | ||||||
|  |                    "'sources' must be present and must be " + | ||||||
|  |                    "a list of source filenames") % ext.name | ||||||
|  |         sources = list(sources) | ||||||
| 
 | 
 | ||||||
|                 build_py = self.get_finalized_command('build_py') |         fullname = self.get_ext_fullname(ext.name) | ||||||
|                 package_dir = build_py.get_package_dir(package) |         if self.inplace: | ||||||
|                 ext_filename = os.path.join(package_dir, |             # ignore build-lib -- put the compiled extension into | ||||||
|                                             self.get_ext_filename(base)) |             # the source tree along with pure Python modules | ||||||
|             else: |  | ||||||
|                 ext_filename = os.path.join(self.build_lib, |  | ||||||
|                                             self.get_ext_filename(fullname)) |  | ||||||
| 
 | 
 | ||||||
|             if not (self.force or newer_group(sources, ext_filename, 'newer')): |             modpath = string.split(fullname, '.') | ||||||
|                 self.announce("skipping '%s' extension (up-to-date)" % |             package = string.join(modpath[0:-1], '.') | ||||||
|                               ext.name) |             base = modpath[-1] | ||||||
|                 continue # 'for' loop over all extensions |  | ||||||
|             else: |  | ||||||
|                 self.announce("building '%s' extension" % ext.name) |  | ||||||
| 
 | 
 | ||||||
|             # First, scan the sources for SWIG definition files (.i), run |             build_py = self.get_finalized_command('build_py') | ||||||
|             # SWIG on 'em to create .c files, and modify the sources list |             package_dir = build_py.get_package_dir(package) | ||||||
|             # accordingly. |             ext_filename = os.path.join(package_dir, | ||||||
|             sources = self.swig_sources(sources) |                                         self.get_ext_filename(base)) | ||||||
|  |         else: | ||||||
|  |             ext_filename = os.path.join(self.build_lib, | ||||||
|  |                                         self.get_ext_filename(fullname)) | ||||||
| 
 | 
 | ||||||
|             # Next, compile the source code to object files. |         if not (self.force or newer_group(sources, ext_filename, 'newer')): | ||||||
|  |             self.announce("skipping '%s' extension (up-to-date)" % | ||||||
|  |                           ext.name) | ||||||
|  |             return | ||||||
|  |         else: | ||||||
|  |             self.announce("building '%s' extension" % ext.name) | ||||||
| 
 | 
 | ||||||
|             # XXX not honouring 'define_macros' or 'undef_macros' -- the |         # First, scan the sources for SWIG definition files (.i), run | ||||||
|             # CCompiler API needs to change to accommodate this, and I |         # SWIG on 'em to create .c files, and modify the sources list | ||||||
|             # want to do one thing at a time! |         # accordingly. | ||||||
|  |         sources = self.swig_sources(sources) | ||||||
| 
 | 
 | ||||||
|             # Two possible sources for extra compiler arguments: |         # Next, compile the source code to object files. | ||||||
|             #   - 'extra_compile_args' in Extension object |  | ||||||
|             #   - CFLAGS environment variable (not particularly |  | ||||||
|             #     elegant, but people seem to expect it and I |  | ||||||
|             #     guess it's useful) |  | ||||||
|             # The environment variable should take precedence, and |  | ||||||
|             # any sensible compiler will give precedence to later |  | ||||||
|             # command line args.  Hence we combine them in order: |  | ||||||
|             extra_args = ext.extra_compile_args or [] |  | ||||||
| 
 | 
 | ||||||
|             macros = ext.define_macros[:] |         # XXX not honouring 'define_macros' or 'undef_macros' -- the | ||||||
|             for undef in ext.undef_macros: |         # CCompiler API needs to change to accommodate this, and I | ||||||
|                 macros.append((undef,)) |         # want to do one thing at a time! | ||||||
| 
 | 
 | ||||||
|             # XXX and if we support CFLAGS, why not CC (compiler |         # Two possible sources for extra compiler arguments: | ||||||
|             # executable), CPPFLAGS (pre-processor options), and LDFLAGS |         #   - 'extra_compile_args' in Extension object | ||||||
|             # (linker options) too? |         #   - CFLAGS environment variable (not particularly | ||||||
|             # XXX should we use shlex to properly parse CFLAGS? |         #     elegant, but people seem to expect it and I | ||||||
|  |         #     guess it's useful) | ||||||
|  |         # The environment variable should take precedence, and | ||||||
|  |         # any sensible compiler will give precedence to later | ||||||
|  |         # command line args.  Hence we combine them in order: | ||||||
|  |         extra_args = ext.extra_compile_args or [] | ||||||
| 
 | 
 | ||||||
|             if os.environ.has_key('CFLAGS'): |         macros = ext.define_macros[:] | ||||||
|                 extra_args.extend(string.split(os.environ['CFLAGS'])) |         for undef in ext.undef_macros: | ||||||
|                  |             macros.append((undef,)) | ||||||
|             objects = self.compiler.compile(sources, |  | ||||||
|                                             output_dir=self.build_temp, |  | ||||||
|                                             macros=macros, |  | ||||||
|                                             include_dirs=ext.include_dirs, |  | ||||||
|                                             debug=self.debug, |  | ||||||
|                                             extra_postargs=extra_args) |  | ||||||
| 
 | 
 | ||||||
|             # Now link the object files together into a "shared object" -- |         # XXX and if we support CFLAGS, why not CC (compiler | ||||||
|             # of course, first we have to figure out all the other things |         # executable), CPPFLAGS (pre-processor options), and LDFLAGS | ||||||
|             # that go into the mix. |         # (linker options) too? | ||||||
|             if ext.extra_objects: |         # XXX should we use shlex to properly parse CFLAGS? | ||||||
|                 objects.extend(ext.extra_objects) | 
 | ||||||
|             extra_args = ext.extra_link_args or [] |         if os.environ.has_key('CFLAGS'): | ||||||
|  |             extra_args.extend(string.split(os.environ['CFLAGS'])) | ||||||
|  | 
 | ||||||
|  |         objects = self.compiler.compile(sources, | ||||||
|  |                                         output_dir=self.build_temp, | ||||||
|  |                                         macros=macros, | ||||||
|  |                                         include_dirs=ext.include_dirs, | ||||||
|  |                                         debug=self.debug, | ||||||
|  |                                         extra_postargs=extra_args) | ||||||
|  | 
 | ||||||
|  |         # Now link the object files together into a "shared object" -- | ||||||
|  |         # of course, first we have to figure out all the other things | ||||||
|  |         # that go into the mix. | ||||||
|  |         if ext.extra_objects: | ||||||
|  |             objects.extend(ext.extra_objects) | ||||||
|  |         extra_args = ext.extra_link_args or [] | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|             self.compiler.link_shared_object( |         self.compiler.link_shared_object( | ||||||
|                 objects, ext_filename,  |             objects, ext_filename,  | ||||||
|                 libraries=self.get_libraries(ext), |             libraries=self.get_libraries(ext), | ||||||
|                 library_dirs=ext.library_dirs, |             library_dirs=ext.library_dirs, | ||||||
|                 runtime_library_dirs=ext.runtime_library_dirs, |             runtime_library_dirs=ext.runtime_library_dirs, | ||||||
|                 extra_postargs=extra_args, |             extra_postargs=extra_args, | ||||||
|                 export_symbols=self.get_export_symbols(ext),  |             export_symbols=self.get_export_symbols(ext),  | ||||||
|                 debug=self.debug, |             debug=self.debug, | ||||||
|                 build_temp=self.build_temp) |             build_temp=self.build_temp) | ||||||
| 
 |  | ||||||
|     # build_extensions () |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     def swig_sources (self, sources): |     def swig_sources (self, sources): | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Marc-André Lemburg
						Marc-André Lemburg