codekingpro/portable-devtools
115k
1"""distutils.command.build2 3Implements the Distutils 'build' command."""4 5from __future__ import annotations6 7import os8import sys9import sysconfig10from collections.abc import Callable11from typing import ClassVar12 13from ..ccompiler import show_compilers14from ..core import Command15from ..errors import DistutilsOptionError16from ..util import get_platform17 18 19class build(Command):20 description = "build everything needed to install"21 22 user_options = [23 ('build-base=', 'b', "base directory for build library"),24 ('build-purelib=', None, "build directory for platform-neutral distributions"),25 ('build-platlib=', None, "build directory for platform-specific distributions"),26 (27 'build-lib=',28 None,29 "build directory for all distribution (defaults to either build-purelib or build-platlib",30 ),31 ('build-scripts=', None, "build directory for scripts"),32 ('build-temp=', 't', "temporary build directory"),33 (34 'plat-name=',35 'p',36 f"platform name to build for, if supported [default: {get_platform()}]",37 ),38 ('compiler=', 'c', "specify the compiler type"),39 ('parallel=', 'j', "number of parallel build jobs"),40 ('debug', 'g', "compile extensions and libraries with debugging information"),41 ('force', 'f', "forcibly build everything (ignore file timestamps)"),42 ('executable=', 'e', "specify final destination interpreter path (build.py)"),43 ]44 45 boolean_options: ClassVar[list[str]] = ['debug', 'force']46 47 help_options: ClassVar[list[tuple[str, str | None, str, Callable[[], object]]]] = [48 ('help-compiler', None, "list available compilers", show_compilers),49 ]50 51 def initialize_options(self):52 self.build_base = 'build'53 # these are decided only after 'build_base' has its final value54 # (unless overridden by the user or client)55 self.build_purelib = None56 self.build_platlib = None57 self.build_lib = None58 self.build_temp = None59 self.build_scripts = None60 self.compiler = None61 self.plat_name = None62 self.debug = None63 self.force = False64 self.executable = None65 self.parallel = None66 67 def finalize_options(self) -> None: # noqa: C90168 if self.plat_name is None:69 self.plat_name = get_platform()70 else:71 # plat-name only supported for windows (other platforms are72 # supported via ./configure flags, if at all). Avoid misleading73 # other platforms.74 if os.name != 'nt':75 raise DistutilsOptionError(76 "--plat-name only supported on Windows (try "77 "using './configure --help' on your platform)"78 )79 80 plat_specifier = f".{self.plat_name}-{sys.implementation.cache_tag}"81 82 # Python 3.13+ with --disable-gil shouldn't share build directories83 if sysconfig.get_config_var('Py_GIL_DISABLED'):84 plat_specifier += 't'85 86 # Make it so Python 2.x and Python 2.x with --with-pydebug don't87 # share the same build directories. Doing so confuses the build88 # process for C modules89 if hasattr(sys, 'gettotalrefcount'):90 plat_specifier += '-pydebug'91 92 # 'build_purelib' and 'build_platlib' just default to 'lib' and93 # 'lib.<plat>' under the base build directory. We only use one of94 # them for a given distribution, though --95 if self.build_purelib is None:96 self.build_purelib = os.path.join(self.build_base, 'lib')97 if self.build_platlib is None:98 self.build_platlib = os.path.join(self.build_base, 'lib' + plat_specifier)99 100 # 'build_lib' is the actual directory that we will use for this101 # particular module distribution -- if user didn't supply it, pick102 # one of 'build_purelib' or 'build_platlib'.103 if self.build_lib is None:104 if self.distribution.has_ext_modules():105 self.build_lib = self.build_platlib106 else:107 self.build_lib = self.build_purelib108 109 # 'build_temp' -- temporary directory for compiler turds,110 # "build/temp.<plat>"111 if self.build_temp is None:112 self.build_temp = os.path.join(self.build_base, 'temp' + plat_specifier)113 if self.build_scripts is None:114 self.build_scripts = os.path.join(115 self.build_base,116 f'scripts-{sys.version_info.major}.{sys.version_info.minor}',117 )118 119 if self.executable is None and sys.executable:120 self.executable = os.path.normpath(sys.executable)121 122 if isinstance(self.parallel, str):123 try:124 self.parallel = int(self.parallel)125 except ValueError:126 raise DistutilsOptionError("parallel should be an integer")127 128 def run(self) -> None:129 # Run all relevant sub-commands. This will be some subset of:130 # - build_py - pure Python modules131 # - build_clib - standalone C libraries132 # - build_ext - Python extensions133 # - build_scripts - (Python) scripts134 for cmd_name in self.get_sub_commands():135 self.run_command(cmd_name)136 137 # -- Predicates for the sub-command list ---------------------------138 139 def has_pure_modules(self):140 return self.distribution.has_pure_modules()141 142 def has_c_libraries(self):143 return self.distribution.has_c_libraries()144 145 def has_ext_modules(self):146 return self.distribution.has_ext_modules()147 148 def has_scripts(self):149 return self.distribution.has_scripts()150 151 sub_commands = [152 ('build_py', has_pure_modules),153 ('build_clib', has_c_libraries),154 ('build_ext', has_ext_modules),155 ('build_scripts', has_scripts),156 ]157 