blob: 9c2667cfd2576cda705e9ce0bec234d63779d486 [file] [log] [blame]
Greg Ward13ae1c81999-03-22 14:55:25 +00001"""distutils.command.build
2
3Implements the Distutils 'build' command."""
4
Greg Ward3ce77fd2000-03-02 01:49:45 +00005__revision__ = "$Id$"
Greg Ward13ae1c81999-03-22 14:55:25 +00006
Greg Ward42a3bf52000-03-01 01:26:45 +00007import sys, os
Greg Ward13ae1c81999-03-22 14:55:25 +00008from distutils.core import Command
Christian Heimes5e696852008-04-09 08:37:03 +00009from distutils.errors import DistutilsOptionError
Greg Ward42a3bf52000-03-01 01:26:45 +000010from distutils.util import get_platform
Greg Ward34593812000-06-24 01:23:37 +000011
12
Collin Winter5b7e9d72007-08-30 03:52:21 +000013def show_compilers():
Greg Ward34593812000-06-24 01:23:37 +000014 from distutils.ccompiler import show_compilers
15 show_compilers()
16
Greg Ward13ae1c81999-03-22 14:55:25 +000017
Collin Winter5b7e9d72007-08-30 03:52:21 +000018class build(Command):
Greg Ward13ae1c81999-03-22 14:55:25 +000019
Greg Ward37bc8152000-01-30 18:34:15 +000020 description = "build everything needed to install"
21
Greg Wardbbeceea2000-02-18 00:25:39 +000022 user_options = [
23 ('build-base=', 'b',
24 "base directory for build library"),
Greg Ward42a3bf52000-03-01 01:26:45 +000025 ('build-purelib=', None,
26 "build directory for platform-neutral distributions"),
27 ('build-platlib=', None,
28 "build directory for platform-specific distributions"),
29 ('build-lib=', None,
30 "build directory for all distribution (defaults to either " +
31 "build-purelib or build-platlib"),
Greg Ward8d5881a2000-05-25 01:19:18 +000032 ('build-scripts=', None,
33 "build directory for scripts"),
Greg Ward42a3bf52000-03-01 01:26:45 +000034 ('build-temp=', 't',
35 "temporary build directory"),
Christian Heimes5e696852008-04-09 08:37:03 +000036 ('plat-name=', 'p',
37 "platform name to build for, if supported "
38 "(default: %s)" % get_platform()),
Gregory P. Smith9668b782000-05-12 00:33:14 +000039 ('compiler=', 'c',
40 "specify the compiler type"),
Greg Wardbbeceea2000-02-18 00:25:39 +000041 ('debug', 'g',
42 "compile extensions and libraries with debugging information"),
Greg Wardc41d6b32000-04-10 00:19:42 +000043 ('force', 'f',
Gregory P. Smith9668b782000-05-12 00:33:14 +000044 "forcibly build everything (ignore file timestamps)"),
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +000045 ('executable=', 'e',
46 "specify final destination interpreter path (build.py)"),
Greg Wardbbeceea2000-02-18 00:25:39 +000047 ]
Greg Ward2ff78872000-06-24 00:23:20 +000048
Greg Ward99b032e2000-09-25 01:41:15 +000049 boolean_options = ['debug', 'force']
50
Greg Ward9d17a7a2000-06-07 03:00:06 +000051 help_options = [
52 ('help-compiler', None,
Greg Ward2ff78872000-06-24 00:23:20 +000053 "list available compilers", show_compilers),
Greg Wardfa9ff762000-10-14 04:06:40 +000054 ]
Greg Ward13ae1c81999-03-22 14:55:25 +000055
Collin Winter5b7e9d72007-08-30 03:52:21 +000056 def initialize_options(self):
Greg Warde6ac2fc1999-09-29 12:38:18 +000057 self.build_base = 'build'
58 # these are decided only after 'build_base' has its final value
Greg Ward13ae1c81999-03-22 14:55:25 +000059 # (unless overridden by the user or client)
Greg Ward42a3bf52000-03-01 01:26:45 +000060 self.build_purelib = None
Greg Warde6ac2fc1999-09-29 12:38:18 +000061 self.build_platlib = None
Greg Ward42a3bf52000-03-01 01:26:45 +000062 self.build_lib = None
63 self.build_temp = None
Greg Ward8d5881a2000-05-25 01:19:18 +000064 self.build_scripts = None
Gregory P. Smith9668b782000-05-12 00:33:14 +000065 self.compiler = None
Christian Heimes5e696852008-04-09 08:37:03 +000066 self.plat_name = None
Greg Ward32462002000-02-09 02:19:49 +000067 self.debug = None
Greg Wardc41d6b32000-04-10 00:19:42 +000068 self.force = 0
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +000069 self.executable = None
Greg Ward13ae1c81999-03-22 14:55:25 +000070
Collin Winter5b7e9d72007-08-30 03:52:21 +000071 def finalize_options(self):
Christian Heimes5e696852008-04-09 08:37:03 +000072 if self.plat_name is None:
73 self.plat_name = get_platform()
74 else:
75 # plat-name only supported for windows (other platforms are
76 # supported via ./configure flags, if at all). Avoid misleading
77 # other platforms.
78 if os.name != 'nt':
79 raise DistutilsOptionError(
80 "--plat-name only supported on Windows (try "
81 "using './configure --help' on your platform)")
82
83 plat_specifier = ".%s-%s" % (self.plat_name, sys.version[0:3])
Greg Ward42900942000-09-16 01:54:46 +000084
Georg Brandl86def6c2008-01-21 20:36:10 +000085 # Make it so Python 2.x and Python 2.x with --with-pydebug don't
86 # share the same build directories. Doing so confuses the build
87 # process for C modules
88 if hasattr(sys, 'gettotalrefcount'):
89 plat_specifier += '-pydebug'
90
Greg Ward42a3bf52000-03-01 01:26:45 +000091 # 'build_purelib' and 'build_platlib' just default to 'lib' and
92 # 'lib.<plat>' under the base build directory. We only use one of
93 # them for a given distribution, though --
94 if self.build_purelib is None:
Greg Wardcb1f4c42000-09-30 18:27:54 +000095 self.build_purelib = os.path.join(self.build_base, 'lib')
Greg Warde6ac2fc1999-09-29 12:38:18 +000096 if self.build_platlib is None:
Greg Wardcb1f4c42000-09-30 18:27:54 +000097 self.build_platlib = os.path.join(self.build_base,
98 'lib' + plat_specifier)
Greg Ward42a3bf52000-03-01 01:26:45 +000099
100 # 'build_lib' is the actual directory that we will use for this
101 # particular module distribution -- if user didn't supply it, pick
102 # one of 'build_purelib' or 'build_platlib'.
103 if self.build_lib is None:
104 if self.distribution.ext_modules:
105 self.build_lib = self.build_platlib
106 else:
107 self.build_lib = self.build_purelib
108
109 # 'build_temp' -- temporary directory for compiler turds,
110 # "build/temp.<plat>"
111 if self.build_temp is None:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000112 self.build_temp = os.path.join(self.build_base,
113 'temp' + plat_specifier)
Greg Ward8d5881a2000-05-25 01:19:18 +0000114 if self.build_scripts is None:
Michael W. Hudson49bdaed2001-12-10 15:28:30 +0000115 self.build_scripts = os.path.join(self.build_base,
116 'scripts-' + sys.version[0:3])
Greg Ward53db8152000-09-16 02:06:45 +0000117
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +0000118 if self.executable is None:
119 self.executable = os.path.normpath(sys.executable)
Greg Ward13ae1c81999-03-22 14:55:25 +0000120
Collin Winter5b7e9d72007-08-30 03:52:21 +0000121 def run(self):
Greg Ward64d855a2000-09-30 17:08:12 +0000122 # Run all relevant sub-commands. This will be some subset of:
123 # - build_py - pure Python modules
124 # - build_clib - standalone C libraries
125 # - build_ext - Python extensions
126 # - build_scripts - (Python) scripts
127 for cmd_name in self.get_sub_commands():
128 self.run_command(cmd_name)
Greg Ward13ae1c81999-03-22 14:55:25 +0000129
Greg Ward13ae1c81999-03-22 14:55:25 +0000130
Greg Ward64d855a2000-09-30 17:08:12 +0000131 # -- Predicates for the sub-command list ---------------------------
Greg Ward5f7c18e2000-02-05 02:24:16 +0000132
Collin Winter5b7e9d72007-08-30 03:52:21 +0000133 def has_pure_modules(self):
Greg Ward64d855a2000-09-30 17:08:12 +0000134 return self.distribution.has_pure_modules()
Greg Ward13ae1c81999-03-22 14:55:25 +0000135
Collin Winter5b7e9d72007-08-30 03:52:21 +0000136 def has_c_libraries(self):
Greg Ward64d855a2000-09-30 17:08:12 +0000137 return self.distribution.has_c_libraries()
138
Collin Winter5b7e9d72007-08-30 03:52:21 +0000139 def has_ext_modules(self):
Greg Ward64d855a2000-09-30 17:08:12 +0000140 return self.distribution.has_ext_modules()
141
Collin Winter5b7e9d72007-08-30 03:52:21 +0000142 def has_scripts(self):
Greg Ward64d855a2000-09-30 17:08:12 +0000143 return self.distribution.has_scripts()
144
145
146 sub_commands = [('build_py', has_pure_modules),
147 ('build_clib', has_c_libraries),
148 ('build_ext', has_ext_modules),
149 ('build_scripts', has_scripts),
150 ]