10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""distutils.command.build
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30a8c90248264a8b26970b4473770bcc3df8515fJosh GaoImplements the Distutils 'build' command."""
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__revision__ = "$Id$"
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys, os
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom distutils.util import get_platform
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom distutils.core import Command
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom distutils.errors import DistutilsOptionError
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef show_compilers():
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    from distutils.ccompiler import show_compilers
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    show_compilers()
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass build(Command):
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    description = "build everything needed to install"
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    user_options = [
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('build-base=', 'b',
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "base directory for build library"),
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('build-purelib=', None,
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "build directory for platform-neutral distributions"),
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('build-platlib=', None,
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "build directory for platform-specific distributions"),
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('build-lib=', None,
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "build directory for all distribution (defaults to either " +
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "build-purelib or build-platlib"),
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('build-scripts=', None,
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "build directory for scripts"),
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('build-temp=', 't',
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "temporary build directory"),
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('plat-name=', 'p',
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "platform name to build for, if supported "
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "(default: %s)" % get_platform()),
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('compiler=', 'c',
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "specify the compiler type"),
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('debug', 'g',
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "compile extensions and libraries with debugging information"),
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('force', 'f',
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "forcibly build everything (ignore file timestamps)"),
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('executable=', 'e',
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "specify final destination interpreter path (build.py)"),
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ]
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    boolean_options = ['debug', 'force']
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    help_options = [
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('help-compiler', None,
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "list available compilers", show_compilers),
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ]
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def initialize_options(self):
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.build_base = 'build'
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # these are decided only after 'build_base' has its final value
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # (unless overridden by the user or client)
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.build_purelib = None
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.build_platlib = None
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.build_lib = None
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.build_temp = None
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.build_scripts = None
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.compiler = None
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.plat_name = None
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.debug = None
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.force = 0
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.executable = None
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def finalize_options(self):
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.plat_name is None:
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.plat_name = get_platform()
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # plat-name only supported for windows (other platforms are
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # supported via ./configure flags, if at all).  Avoid misleading
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # other platforms.
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if os.name != 'nt':
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise DistutilsOptionError(
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                            "--plat-name only supported on Windows (try "
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                            "using './configure --help' on your platform)")
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        plat_specifier = ".%s-%s" % (self.plat_name, sys.version[0:3])
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Make it so Python 2.x and Python 2.x with --with-pydebug don't
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # share the same build directories. Doing so confuses the build
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # process for C modules
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if hasattr(sys, 'gettotalrefcount'):
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            plat_specifier += '-pydebug'
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 'build_purelib' and 'build_platlib' just default to 'lib' and
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 'lib.<plat>' under the base build directory.  We only use one of
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # them for a given distribution, though --
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.build_purelib is None:
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.build_purelib = os.path.join(self.build_base, 'lib')
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.build_platlib is None:
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.build_platlib = os.path.join(self.build_base,
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                              'lib' + plat_specifier)
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 'build_lib' is the actual directory that we will use for this
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # particular module distribution -- if user didn't supply it, pick
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # one of 'build_purelib' or 'build_platlib'.
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.build_lib is None:
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self.distribution.ext_modules:
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.build_lib = self.build_platlib
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.build_lib = self.build_purelib
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 'build_temp' -- temporary directory for compiler turds,
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # "build/temp.<plat>"
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.build_temp is None:
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.build_temp = os.path.join(self.build_base,
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                           'temp' + plat_specifier)
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.build_scripts is None:
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.build_scripts = os.path.join(self.build_base,
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                              'scripts-' + sys.version[0:3])
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.executable is None:
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.executable = os.path.normpath(sys.executable)
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def run(self):
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Run all relevant sub-commands.  This will be some subset of:
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #  - build_py      - pure Python modules
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #  - build_clib    - standalone C libraries
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #  - build_ext     - Python extensions
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #  - build_scripts - (Python) scripts
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for cmd_name in self.get_sub_commands():
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.run_command(cmd_name)
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # -- Predicates for the sub-command list ---------------------------
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def has_pure_modules (self):
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.distribution.has_pure_modules()
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def has_c_libraries (self):
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.distribution.has_c_libraries()
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def has_ext_modules (self):
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.distribution.has_ext_modules()
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def has_scripts (self):
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.distribution.has_scripts()
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    sub_commands = [('build_py',      has_pure_modules),
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    ('build_clib',    has_c_libraries),
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    ('build_ext',     has_ext_modules),
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    ('build_scripts', has_scripts),
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   ]
148