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