mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Added the ability to do byte-compilation at build time, currently off
by default (since compiling at install time works just fine). Details: - added 'compile' and 'optimize' options - added 'byte_compile()' method - changed 'get_outputs()' so it includes bytecode files A lot of the code added is very similar to code in install_lib.py; would be nice to factor it out further.
This commit is contained in:
		
							parent
							
								
									8161022d4d
								
							
						
					
					
						commit
						73a6c942cd
					
				
					 1 changed files with 51 additions and 5 deletions
				
			
		|  | @ -20,10 +20,16 @@ class build_py (Command): | ||||||
| 
 | 
 | ||||||
|     user_options = [ |     user_options = [ | ||||||
|         ('build-lib=', 'd', "directory to \"build\" (copy) to"), |         ('build-lib=', 'd', "directory to \"build\" (copy) to"), | ||||||
|  |         ('compile', 'c', "compile .py to .pyc"), | ||||||
|  |         ('no-compile', None, "don't compile .py files [default]"), | ||||||
|  |         ('optimize=', 'O', | ||||||
|  |          "also compile with optimization: -O1 for \"python -O\", " | ||||||
|  |          "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"), | ||||||
|         ('force', 'f', "forcibly build everything (ignore file timestamps)"), |         ('force', 'f', "forcibly build everything (ignore file timestamps)"), | ||||||
|         ] |         ] | ||||||
| 
 | 
 | ||||||
|     boolean_options = ['force'] |     boolean_options = ['compile', 'force'] | ||||||
|  |     negative_opt = {'no-compile' : 'compile'} | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     def initialize_options (self): |     def initialize_options (self): | ||||||
|  | @ -31,6 +37,8 @@ def initialize_options (self): | ||||||
|         self.py_modules = None |         self.py_modules = None | ||||||
|         self.package = None |         self.package = None | ||||||
|         self.package_dir = None |         self.package_dir = None | ||||||
|  |         self.compile = 0 | ||||||
|  |         self.optimize = 0 | ||||||
|         self.force = None |         self.force = None | ||||||
| 
 | 
 | ||||||
|     def finalize_options (self): |     def finalize_options (self): | ||||||
|  | @ -44,6 +52,14 @@ def finalize_options (self): | ||||||
|         self.py_modules = self.distribution.py_modules |         self.py_modules = self.distribution.py_modules | ||||||
|         self.package_dir = self.distribution.package_dir |         self.package_dir = self.distribution.package_dir | ||||||
| 
 | 
 | ||||||
|  |         # Ick, copied straight from install_lib.py (fancy_getopt needs a | ||||||
|  |         # type system!  Hell, *everything* needs a type system!!!) | ||||||
|  |         if type(self.optimize) is not IntType: | ||||||
|  |             try: | ||||||
|  |                 self.optimize = int(self.optimize) | ||||||
|  |                 assert 0 <= self.optimize <= 2 | ||||||
|  |             except (ValueError, AssertionError): | ||||||
|  |                 raise DistutilsOptionError, "optimize must be 0, 1, or 2" | ||||||
| 
 | 
 | ||||||
|     def run (self): |     def run (self): | ||||||
| 
 | 
 | ||||||
|  | @ -87,6 +103,8 @@ def run (self): | ||||||
|         else: |         else: | ||||||
|             self.build_packages() |             self.build_packages() | ||||||
| 
 | 
 | ||||||
|  |         self.byte_compile(self.get_outputs(include_bytecode=0)) | ||||||
|  | 
 | ||||||
|     # run () |     # run () | ||||||
|          |          | ||||||
| 
 | 
 | ||||||
|  | @ -284,13 +302,19 @@ def get_module_outfile (self, build_dir, package, module): | ||||||
|         return apply(os.path.join, outfile_path) |         return apply(os.path.join, outfile_path) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     def get_outputs (self): |     def get_outputs (self, include_bytecode=1): | ||||||
|         modules = self.find_all_modules() |         modules = self.find_all_modules() | ||||||
|         outputs = [] |         outputs = [] | ||||||
|         for (package, module, module_file) in modules: |         for (package, module, module_file) in modules: | ||||||
|             package = string.split(package, '.') |             package = string.split(package, '.') | ||||||
|             outputs.append(self.get_module_outfile(self.build_lib, |             filename = self.get_module_outfile(self.build_lib, package, module) | ||||||
|                                                    package, module)) |             outputs.append(filename) | ||||||
|  |             if include_bytecode: | ||||||
|  |                 if self.compile: | ||||||
|  |                     outputs.append(filename + "c") | ||||||
|  |                 if self.optimize > 0: | ||||||
|  |                     outputs.append(filename + "o") | ||||||
|  | 
 | ||||||
|         return outputs |         return outputs | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -347,5 +371,27 @@ def build_packages (self): | ||||||
|                 self.build_module(module, module_file, package) |                 self.build_module(module, module_file, package) | ||||||
| 
 | 
 | ||||||
|     # build_packages () |     # build_packages () | ||||||
|                         | 
 | ||||||
|  | 
 | ||||||
|  |     def byte_compile (self, files): | ||||||
|  |         from distutils.util import byte_compile | ||||||
|  |         prefix = self.build_lib | ||||||
|  |         if prefix[-1] != os.sep: | ||||||
|  |             prefix = prefix + os.sep | ||||||
|  | 
 | ||||||
|  |         # XXX this code is essentially the same as the 'byte_compile() | ||||||
|  |         # method of the "install_lib" command, except for the determination | ||||||
|  |         # of the 'prefix' string.  Hmmm. | ||||||
|  | 
 | ||||||
|  |         if self.compile: | ||||||
|  |             byte_compile(files, optimize=0, | ||||||
|  |                          force=self.force, | ||||||
|  |                          prefix=prefix, | ||||||
|  |                          verbose=self.verbose, dry_run=self.dry_run) | ||||||
|  |         if self.optimize > 0: | ||||||
|  |             byte_compile(files, optimize=self.optimize, | ||||||
|  |                          force=self.force, | ||||||
|  |                          prefix=prefix, | ||||||
|  |                          verbose=self.verbose, dry_run=self.dry_run) | ||||||
|  | 
 | ||||||
| # class build_py | # class build_py | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Greg Ward
						Greg Ward