Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at gcc-offload 671 lines 27 kB view raw
1{ lib 2, stdenv 3, fetchurl 4, fetchpatch 5, fetchgit 6 7# build dependencies 8, autoconf-archive 9, autoreconfHook 10, nukeReferences 11, pkg-config 12, python-setup-hook 13 14# runtime dependencies 15, bzip2 16, expat 17, libffi 18, libxcrypt 19, mpdecimal 20, ncurses 21, openssl 22, sqlite 23, xz 24, zlib 25 26# platform-specific dependencies 27, bash 28, apple-sdk_11 29, darwin 30, windows 31 32# optional dependencies 33, bluezSupport ? false, bluez 34, mimetypesSupport ? true, mailcap 35, tzdata 36, withGdbm ? !stdenv.hostPlatform.isWindows, gdbm 37, withReadline ? !stdenv.hostPlatform.isWindows, readline 38, x11Support ? false, tcl, tk, tclPackages, libX11, xorgproto 39 40# splicing/cross 41, pythonAttr ? "python${sourceVersion.major}${sourceVersion.minor}" 42, self 43, pkgsBuildBuild 44, pkgsBuildHost 45, pkgsBuildTarget 46, pkgsHostHost 47, pkgsTargetTarget 48 49# build customization 50, sourceVersion 51, hash 52, passthruFun 53, stripConfig ? false 54, stripIdlelib ? false 55, stripTests ? false 56, stripTkinter ? false 57, rebuildBytecode ? true 58, stripBytecode ? true 59, includeSiteCustomize ? true 60, static ? stdenv.hostPlatform.isStatic 61, enableFramework ? false 62, noldconfigPatch ? ./. + "/${sourceVersion.major}.${sourceVersion.minor}/no-ldconfig.patch" 63, enableGIL ? true 64 65# pgo (not reproducible) + -fno-semantic-interposition 66# https://docs.python.org/3/using/configure.html#cmdoption-enable-optimizations 67, enableOptimizations ? false 68 69# improves performance, but remains reproducible 70, enableNoSemanticInterposition ? true 71 72# enabling LTO on 32bit arch causes downstream packages to fail when linking 73, enableLTO ? stdenv.hostPlatform.isDarwin || (stdenv.hostPlatform.is64bit && stdenv.hostPlatform.isLinux) 74 75# enable asserts to ensure the build remains reproducible 76, reproducibleBuild ? false 77 78# for the Python package set 79, packageOverrides ? (self: super: {}) 80 81# tests 82, testers 83 84} @ inputs: 85 86# Note: this package is used for bootstrapping fetchurl, and thus 87# cannot use fetchpatch! All mutable patches (generated by GitHub or 88# cgit) that are needed here should be included directly in Nixpkgs as 89# files. 90 91assert x11Support -> tcl != null 92 && tk != null 93 && xorgproto != null 94 && libX11 != null; 95 96assert bluezSupport -> bluez != null; 97 98assert lib.assertMsg (enableFramework -> stdenv.hostPlatform.isDarwin) 99 "Framework builds are only supported on Darwin."; 100 101assert lib.assertMsg (reproducibleBuild -> stripBytecode) 102 "Deterministic builds require stripping bytecode."; 103 104assert lib.assertMsg (reproducibleBuild -> (!enableOptimizations)) 105 "Deterministic builds are not achieved when optimizations are enabled."; 106 107assert lib.assertMsg (reproducibleBuild -> (!rebuildBytecode)) 108 "Deterministic builds are not achieved when (default unoptimized) bytecode is created."; 109 110let 111 inherit (lib) 112 concatMapStringsSep 113 concatStringsSep 114 enableFeature 115 getDev 116 getLib 117 optionals 118 optionalString 119 replaceStrings 120 versionOlder 121 ; 122 123 # mixes libc and libxcrypt headers and libs and causes segfaults on importing crypt 124 libxcrypt = if stdenv.hostPlatform.isFreeBSD then null else inputs.libxcrypt; 125 126 buildPackages = pkgsBuildHost; 127 inherit (passthru) pythonOnBuildForHost; 128 129 tzdataSupport = tzdata != null && passthru.pythonAtLeast "3.9"; 130 131 passthru = let 132 # When we override the interpreter we also need to override the spliced versions of the interpreter 133 # bluez is excluded manually to break an infinite recursion. 134 inputs' = lib.filterAttrs (n: v: n != "bluez" && n != "passthruFun" && ! lib.isDerivation v) inputs; 135 override = attr: let python = attr.override (inputs' // { self = python; }); in python; 136 in passthruFun rec { 137 inherit self sourceVersion packageOverrides; 138 implementation = "cpython"; 139 libPrefix = "python${pythonVersion}${lib.optionalString (!enableGIL) "t"}"; 140 executable = libPrefix; 141 pythonVersion = with sourceVersion; "${major}.${minor}"; 142 sitePackages = "lib/${libPrefix}/site-packages"; 143 inherit hasDistutilsCxxPatch pythonAttr; 144 pythonOnBuildForBuild = override pkgsBuildBuild.${pythonAttr}; 145 pythonOnBuildForHost = override pkgsBuildHost.${pythonAttr}; 146 pythonOnBuildForTarget = override pkgsBuildTarget.${pythonAttr}; 147 pythonOnHostForHost = override pkgsHostHost.${pythonAttr}; 148 pythonOnTargetForTarget = lib.optionalAttrs (lib.hasAttr pythonAttr pkgsTargetTarget) (override pkgsTargetTarget.${pythonAttr}); 149 }; 150 151 version = with sourceVersion; "${major}.${minor}.${patch}${suffix}"; 152 153 nativeBuildInputs = [ 154 nukeReferences 155 ] ++ optionals (!stdenv.hostPlatform.isDarwin) [ 156 autoconf-archive # needed for AX_CHECK_COMPILE_FLAG 157 autoreconfHook 158 pkg-config 159 ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) [ 160 buildPackages.stdenv.cc 161 pythonOnBuildForHost 162 ] ++ optionals (stdenv.cc.isClang && (!stdenv.hostPlatform.useAndroidPrebuilt or false) && (enableLTO || enableOptimizations)) [ 163 stdenv.cc.cc.libllvm.out 164 ]; 165 166 buildInputs = lib.filter (p: p != null) ([ 167 bzip2 168 expat 169 libffi 170 libxcrypt 171 mpdecimal 172 ncurses 173 openssl 174 sqlite 175 xz 176 zlib 177 ] ++ optionals bluezSupport [ 178 bluez 179 ] ++ optionals enableFramework [ 180 darwin.apple_sdk.frameworks.Cocoa 181 ] ++ optionals stdenv.hostPlatform.isDarwin [ 182 # Work around for ld64 crashes on x86_64-darwin. Remove once 11.0 becomes the default. 183 apple-sdk_11 184 ] ++ optionals stdenv.hostPlatform.isMinGW [ 185 windows.dlfcn 186 windows.mingw_w64_pthreads 187 ] ++ optionals tzdataSupport [ 188 tzdata 189 ] ++ optionals withGdbm [ 190 gdbm 191 ] ++ optionals withReadline [ 192 readline 193 ] ++ optionals x11Support [ 194 libX11 195 tcl 196 tk 197 xorgproto 198 ]); 199 200 hasDistutilsCxxPatch = !(stdenv.cc.isGNU or false); 201 202 pythonOnBuildForHostInterpreter = if stdenv.hostPlatform == stdenv.buildPlatform then 203 "$out/bin/python" 204 else pythonOnBuildForHost.interpreter; 205 206 src = fetchurl { 207 url = with sourceVersion; "https://www.python.org/ftp/python/${major}.${minor}.${patch}/Python-${version}.tar.xz"; 208 inherit hash; 209 }; 210 211 # win32 is added by Fedora’s patch 212 machdep = if stdenv.hostPlatform.isWindows then 213 "win32" 214 else 215 stdenv.hostPlatform.parsed.kernel.name; 216 217 # https://github.com/python/cpython/blob/e488e300f5c01289c10906c2e53a8e43d6de32d8/configure.ac#L428 218 # The configure script uses "arm" as the CPU name for all 32-bit ARM 219 # variants when cross-compiling, but native builds include the version 220 # suffix, so we do the same. 221 pythonHostPlatform = let 222 cpu = { 223 # According to PEP600, Python's name for the Power PC 224 # architecture is "ppc", not "powerpc". Without the Rosetta 225 # Stone below, the PEP600 requirement that "${ARCH} matches 226 # the return value from distutils.util.get_platform()" fails. 227 # https://peps.python.org/pep-0600/ 228 powerpc = "ppc"; 229 powerpcle = "ppcle"; 230 powerpc64 = "ppc64"; 231 powerpc64le = "ppc64le"; 232 }.${stdenv.hostPlatform.parsed.cpu.name} or stdenv.hostPlatform.parsed.cpu.name; 233 in "${machdep}-${cpu}"; 234 235 execSuffix = stdenv.hostPlatform.extensions.executable; 236in with passthru; stdenv.mkDerivation (finalAttrs: { 237 pname = "python3"; 238 inherit src version; 239 240 inherit nativeBuildInputs; 241 buildInputs = lib.optionals (!stdenv.hostPlatform.isWindows) [ 242 bash # only required for patchShebangs 243 ] ++ buildInputs; 244 245 prePatch = optionalString stdenv.hostPlatform.isDarwin '' 246 substituteInPlace configure --replace-fail '`/usr/bin/arch`' '"i386"' 247 '' + optionalString (pythonOlder "3.9" && stdenv.hostPlatform.isDarwin && x11Support) '' 248 # Broken on >= 3.9; replaced with ./3.9/darwin-tcl-tk.patch 249 substituteInPlace setup.py --replace-fail /Library/Frameworks /no-such-path 250 ''; 251 252 patches = [ 253 # Disable the use of ldconfig in ctypes.util.find_library (since 254 # ldconfig doesn't work on NixOS), and don't use 255 # ctypes.util.find_library during the loading of the uuid module 256 # (since it will do a futile invocation of gcc (!) to find 257 # libuuid, slowing down program startup a lot). 258 noldconfigPatch 259 ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform && stdenv.hostPlatform.isFreeBSD) [ 260 # Cross compilation only supports a limited number of "known good" 261 # configurations. If you're reading this and it's been a long time 262 # since this diff, consider submitting this patch upstream! 263 ./freebsd-cross.patch 264 ] ++ optionals (pythonOlder "3.13") [ 265 # Make sure that the virtualenv activation scripts are 266 # owner-writable, so venvs can be recreated without permission 267 # errors. 268 ./virtualenv-permissions.patch 269 ] ++ optionals (pythonAtLeast "3.13") [ 270 ./3.13/virtualenv-permissions.patch 271 ] ++ optionals mimetypesSupport [ 272 # Make the mimetypes module refer to the right file 273 ./mimetypes.patch 274 ] ++ optionals (pythonAtLeast "3.9" && pythonOlder "3.11" && stdenv.hostPlatform.isDarwin) [ 275 # Stop checking for TCL/TK in global macOS locations 276 ./3.9/darwin-tcl-tk.patch 277 ] ++ optionals (hasDistutilsCxxPatch && pythonOlder "3.12") [ 278 # Fix for http://bugs.python.org/issue1222585 279 # Upstream distutils is calling C compiler to compile C++ code, which 280 # only works for GCC and Apple Clang. This makes distutils to call C++ 281 # compiler when needed. 282 ( 283 if pythonAtLeast "3.7" && pythonOlder "3.11" then 284 ./3.7/python-3.x-distutils-C++.patch 285 else if pythonAtLeast "3.11" then 286 ./3.11/python-3.x-distutils-C++.patch 287 else 288 fetchpatch { 289 url = "https://bugs.python.org/file48016/python-3.x-distutils-C++.patch"; 290 sha256 = "1h18lnpx539h5lfxyk379dxwr8m2raigcjixkf133l4xy3f4bzi2"; 291 } 292 ) 293 ] ++ optionals (pythonAtLeast "3.7" && pythonOlder "3.12") [ 294 # LDSHARED now uses $CC instead of gcc. Fixes cross-compilation of extension modules. 295 ./3.8/0001-On-all-posix-systems-not-just-Darwin-set-LDSHARED-if.patch 296 # Use sysconfigdata to find headers. Fixes cross-compilation of extension modules. 297 ./3.7/fix-finding-headers-when-cross-compiling.patch 298 ] ++ optionals (pythonOlder "3.12") [ 299 # https://github.com/python/cpython/issues/90656 300 ./loongarch-support.patch 301 ] ++ optionals (pythonAtLeast "3.11" && pythonOlder "3.13") [ 302 # backport fix for https://github.com/python/cpython/issues/95855 303 ./platform-triplet-detection.patch 304 ] ++ optionals (stdenv.hostPlatform.isMinGW) (let 305 # https://src.fedoraproject.org/rpms/mingw-python3 306 mingw-patch = fetchgit { 307 name = "mingw-python-patches"; 308 url = "https://src.fedoraproject.org/rpms/mingw-python3.git"; 309 rev = "3edecdbfb4bbf1276d09cd5e80e9fb3dd88c9511"; # for python 3.11.9 at the time of writing. 310 hash = "sha256-kpXoIHlz53+0FAm/fK99ZBdNUg0u13erOr1XP2FSkQY="; 311 }; 312 in ( 313 builtins.map (f: "${mingw-patch}/${f}") 314 [ 315 # The other patches in that repo are already applied to 3.11.10 316 "mingw-python3_distutils.patch" 317 "mingw-python3_frozenmain.patch" 318 "mingw-python3_make-sysconfigdata.py-relocatable.patch" 319 "mingw-python3_mods-failed.patch" 320 "mingw-python3_module-select.patch" 321 "mingw-python3_module-socket.patch" 322 "mingw-python3_modules.patch" 323 "mingw-python3_platform-mingw.patch" 324 "mingw-python3_posix-layout.patch" 325 "mingw-python3_pthread_threadid.patch" 326 "mingw-python3_pythonw.patch" 327 "mingw-python3_setenv.patch" 328 "mingw-python3_win-modules.patch" 329 ]) 330 ); 331 332 postPatch = optionalString (!stdenv.hostPlatform.isWindows) '' 333 substituteInPlace Lib/subprocess.py \ 334 --replace-fail "'/bin/sh'" "'${bash}/bin/sh'" 335 '' + optionalString mimetypesSupport '' 336 substituteInPlace Lib/mimetypes.py \ 337 --replace-fail "@mime-types@" "${mailcap}" 338 '' + optionalString (pythonOlder "3.13" && x11Support && ((tclPackages.tix or null) != null)) '' 339 substituteInPlace "Lib/tkinter/tix.py" --replace-fail \ 340 "os.environ.get('TIX_LIBRARY')" \ 341 "os.environ.get('TIX_LIBRARY') or '${tclPackages.tix}/lib'" 342 ''; 343 344 env = { 345 CPPFLAGS = concatStringsSep " " (map (p: "-I${getDev p}/include") buildInputs); 346 LDFLAGS = concatStringsSep " " (map (p: "-L${getLib p}/lib") buildInputs); 347 LIBS = "${optionalString (!stdenv.hostPlatform.isDarwin) "-lcrypt"}"; 348 NIX_LDFLAGS = lib.optionalString (stdenv.cc.isGNU && !stdenv.hostPlatform.isStatic) ({ 349 "glibc" = "-lgcc_s"; 350 "musl" = "-lgcc_eh"; 351 }."${stdenv.hostPlatform.libc}" or ""); 352 # Determinism: We fix the hashes of str, bytes and datetime objects. 353 PYTHONHASHSEED=0; 354 }; 355 356 # https://docs.python.org/3/using/configure.html 357 configureFlags = [ 358 "--without-ensurepip" 359 "--with-system-expat" 360 "--with-system-libmpdec" 361 ] ++ optionals (openssl != null) [ 362 "--with-openssl=${openssl.dev}" 363 ] ++ optionals tzdataSupport [ 364 "--with-tzpath=${tzdata}/share/zoneinfo" 365 ] ++ optionals (execSuffix != "") [ 366 "--with-suffix=${execSuffix}" 367 ] ++ optionals enableLTO [ 368 "--with-lto" 369 ] ++ optionals (!static && !enableFramework) [ 370 "--enable-shared" 371 ] ++ optionals enableFramework [ 372 "--enable-framework=${placeholder "out"}/Library/Frameworks" 373 ] ++ optionals (pythonAtLeast "3.13") [ 374 (enableFeature enableGIL "gil") 375 ] ++ optionals enableOptimizations [ 376 "--enable-optimizations" 377 ] ++ optionals (sqlite != null) [ 378 "--enable-loadable-sqlite-extensions" 379 ] ++ optionals (libxcrypt != null) [ 380 "CFLAGS=-I${libxcrypt}/include" 381 "LIBS=-L${libxcrypt}/lib" 382 ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) [ 383 "ac_cv_buggy_getaddrinfo=no" 384 # Assume little-endian IEEE 754 floating point when cross compiling 385 "ac_cv_little_endian_double=yes" 386 "ac_cv_big_endian_double=no" 387 "ac_cv_mixed_endian_double=no" 388 "ac_cv_x87_double_rounding=yes" 389 "ac_cv_tanh_preserves_zero_sign=yes" 390 # Generally assume that things are present and work 391 "ac_cv_posix_semaphores_enabled=yes" 392 "ac_cv_broken_sem_getvalue=no" 393 "ac_cv_wchar_t_signed=yes" 394 "ac_cv_rshift_extends_sign=yes" 395 "ac_cv_broken_nice=no" 396 "ac_cv_broken_poll=no" 397 "ac_cv_working_tzset=yes" 398 "ac_cv_have_long_long_format=yes" 399 "ac_cv_have_size_t_format=yes" 400 "ac_cv_computed_gotos=yes" 401 # Both fail when building for windows, normally configure checks this by itself but on other platforms this is set to yes always. 402 "ac_cv_file__dev_ptmx=${if stdenv.hostPlatform.isWindows then "no" else "yes"}" 403 "ac_cv_file__dev_ptc=${if stdenv.hostPlatform.isWindows then "no" else "yes"}" 404 ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform && pythonAtLeast "3.11") [ 405 "--with-build-python=${pythonOnBuildForHostInterpreter}" 406 ] ++ optionals stdenv.hostPlatform.isLinux [ 407 # Never even try to use lchmod on linux, 408 # don't rely on detecting glibc-isms. 409 "ac_cv_func_lchmod=no" 410 ] ++ optionals static [ 411 "LDFLAGS=-static" 412 "MODULE_BUILDTYPE=static" 413 ] ++ optionals (stdenv.hostPlatform.isStatic && stdenv.hostPlatform.isMusl) [ 414 # dlopen is a no-op in static musl builds, and since we build everything without -fPIC it's better not to pretend. 415 "ac_cv_func_dlopen=no" 416 ]; 417 418 preConfigure = '' 419 # Attempt to purify some of the host info collection 420 sed -E -i -e 's/uname -r/echo/g' -e 's/uname -n/echo nixpkgs/g' config.guess 421 sed -E -i -e 's/uname -r/echo/g' -e 's/uname -n/echo nixpkgs/g' configure 422 '' + optionalString (pythonOlder "3.12") '' 423 # Improve purity 424 for path in /usr /sw /opt /pkg; do 425 substituteInPlace ./setup.py --replace-warn $path /no-such-path 426 done 427 '' + optionalString stdenv.hostPlatform.isDarwin '' 428 # Override the auto-detection in setup.py, which assumes a universal build 429 export PYTHON_DECIMAL_WITH_MACHINE=${if stdenv.hostPlatform.isAarch64 then "uint128" else "x64"} 430 # Ensure that modern platform features are enabled on Darwin in spite of having no version suffix. 431 sed -E -i -e 's|Darwin/\[12\]\[0-9\]\.\*|Darwin/*|' configure 432 '' + optionalString (pythonAtLeast "3.11") '' 433 # Also override the auto-detection in `configure`. 434 substituteInPlace configure \ 435 --replace-fail 'libmpdec_machine=universal' 'libmpdec_machine=${if stdenv.hostPlatform.isAarch64 then "uint128" else "x64"}' 436 '' + optionalString (stdenv.hostPlatform.isDarwin && x11Support && pythonAtLeast "3.11") '' 437 export TCLTK_LIBS="-L${tcl}/lib -L${tk}/lib -l${tcl.libPrefix} -l${tk.libPrefix}" 438 export TCLTK_CFLAGS="-I${tcl}/include -I${tk}/include" 439 '' + optionalString stdenv.hostPlatform.isMusl '' 440 export NIX_CFLAGS_COMPILE+=" -DTHREAD_STACK_SIZE=0x100000" 441 '' + 442 443 # enableNoSemanticInterposition essentially sets that CFLAG -fno-semantic-interposition 444 # which changes how symbols are looked up. This essentially means we can't override 445 # libpython symbols via LD_PRELOAD anymore. This is common enough as every build 446 # that uses --enable-optimizations has the same "issue". 447 # 448 # The Fedora wiki has a good article about their journey towards enabling this flag: 449 # https://fedoraproject.org/wiki/Changes/PythonNoSemanticInterpositionSpeedup 450 optionalString enableNoSemanticInterposition '' 451 export CFLAGS_NODIST="-fno-semantic-interposition" 452 ''; 453 454 setupHook = python-setup-hook sitePackages; 455 456 postInstall = let 457 # References *not* to nuke from (sys)config files 458 keep-references = concatMapStringsSep " " (val: "-e ${val}") ([ 459 (placeholder "out") 460 ] ++ lib.optional (libxcrypt != null) libxcrypt 461 ++ lib.optional tzdataSupport tzdata 462 ); 463 in lib.optionalString enableFramework '' 464 for dir in include lib share; do 465 ln -s $out/Library/Frameworks/Python.framework/Versions/Current/$dir $out/$dir 466 done 467 '' + '' 468 # needed for some packages, especially packages that backport functionality 469 # to 2.x from 3.x 470 for item in $out/lib/${libPrefix}/test/*; do 471 if [[ "$item" != */test_support.py* 472 && "$item" != */test/support 473 && "$item" != */test/libregrtest 474 && "$item" != */test/regrtest.py* ]]; then 475 rm -rf "$item" 476 else 477 echo $item 478 fi 479 done 480 touch $out/lib/${libPrefix}/test/__init__.py 481 482 # Determinism: Windows installers were not deterministic. 483 # We're also not interested in building Windows installers. 484 find "$out" -name 'wininst*.exe' | xargs -r rm -f 485 486 # Use Python3 as default python 487 ln -s "$out/bin/idle3" "$out/bin/idle" 488 ln -s "$out/bin/pydoc3" "$out/bin/pydoc" 489 ln -s "$out/bin/python3${execSuffix}" "$out/bin/python${execSuffix}" 490 ln -s "$out/bin/python3-config" "$out/bin/python-config" 491 ln -s "$out/lib/pkgconfig/python3.pc" "$out/lib/pkgconfig/python.pc" 492 ln -sL "$out/share/man/man1/python3.1.gz" "$out/share/man/man1/python.1.gz" 493 494 # Get rid of retained dependencies on -dev packages, and remove 495 # some $TMPDIR references to improve binary reproducibility. 496 # Note that the .pyc file of _sysconfigdata.py should be regenerated! 497 for i in $out/lib/${libPrefix}/_sysconfigdata*.py $out/lib/${libPrefix}/config-${sourceVersion.major}${sourceVersion.minor}*/Makefile; do 498 sed -i $i -e "s|$TMPDIR|/no-such-path|g" 499 done 500 501 # Further get rid of references. https://github.com/NixOS/nixpkgs/issues/51668 502 find $out/lib/python*/config-* -type f -print -exec nuke-refs ${keep-references} '{}' + 503 find $out/lib -name '_sysconfigdata*.py*' -print -exec nuke-refs ${keep-references} '{}' + 504 505 # Make the sysconfigdata module accessible on PYTHONPATH 506 # This allows build Python to import host Python's sysconfigdata 507 mkdir -p "$out/${sitePackages}" 508 ln -s "$out/lib/${libPrefix}/"_sysconfigdata*.py "$out/${sitePackages}/" 509 '' + optionalString stripConfig '' 510 rm -R $out/bin/python*-config $out/lib/python*/config-* 511 '' + optionalString stripIdlelib '' 512 # Strip IDLE (and turtledemo, which uses it) 513 rm -R $out/bin/idle* $out/lib/python*/{idlelib,turtledemo} 514 '' + optionalString stripTkinter '' 515 rm -R $out/lib/python*/tkinter 516 '' + optionalString stripTests '' 517 # Strip tests 518 rm -R $out/lib/python*/test $out/lib/python*/**/test{,s} 519 '' + optionalString includeSiteCustomize '' 520 # Include a sitecustomize.py file 521 cp ${../sitecustomize.py} $out/${sitePackages}/sitecustomize.py 522 '' + optionalString stripBytecode '' 523 # Determinism: deterministic bytecode 524 # First we delete all old bytecode. 525 find $out -type d -name __pycache__ -print0 | xargs -0 -I {} rm -rf "{}" 526 '' + optionalString rebuildBytecode '' 527 # Python 3.7 implements PEP 552, introducing support for deterministic bytecode. 528 # compileall uses the therein introduced checked-hash method by default when 529 # `SOURCE_DATE_EPOCH` is set. 530 # We exclude lib2to3 because that's Python 2 code which fails 531 # We build 3 levels of optimized bytecode. Note the default level, without optimizations, 532 # is not reproducible yet. https://bugs.python.org/issue29708 533 # Not creating bytecode will result in a large performance loss however, so we do build it. 534 find $out -name "*.py" | ${pythonOnBuildForHostInterpreter} -m compileall -q -f -x "lib2to3" -i - 535 find $out -name "*.py" | ${pythonOnBuildForHostInterpreter} -O -m compileall -q -f -x "lib2to3" -i - 536 find $out -name "*.py" | ${pythonOnBuildForHostInterpreter} -OO -m compileall -q -f -x "lib2to3" -i - 537 '' + '' 538 # *strip* shebang from libpython gdb script - it should be dual-syntax and 539 # interpretable by whatever python the gdb in question is using, which may 540 # not even match the major version of this python. doing this after the 541 # bytecode compilations for the same reason - we don't want bytecode generated. 542 mkdir -p $out/share/gdb 543 sed '/^#!/d' Tools/gdb/libpython.py > $out/share/gdb/libpython.py 544 545 # Disable system-wide pip installation. See https://peps.python.org/pep-0668/. 546 cat <<'EXTERNALLY_MANAGED' > $out/lib/${libPrefix}/EXTERNALLY-MANAGED 547 [externally-managed] 548 Error=This command has been disabled as it tries to modify the immutable 549 `/nix/store` filesystem. 550 551 To use Python with Nix and nixpkgs, have a look at the online documentation: 552 <https://nixos.org/manual/nixpkgs/stable/#python>. 553 EXTERNALLY_MANAGED 554 '' + optionalString stdenv.hostPlatform.isWindows '' 555 # Shebang files that link against the build python. Shebang dont work on windows 556 rm $out/bin/2to3* 557 rm $out/bin/idle* 558 rm $out/bin/pydoc* 559 560 echo linking DLLs for pythons compiled librairies 561 linkDLLsInfolder $out/lib/python*/lib-dynload/ 562 ''; 563 564 preFixup = lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) '' 565 # Ensure patch-shebangs uses shebangs of host interpreter. 566 export PATH=${lib.makeBinPath [ "$out" ]}:$PATH 567 ''; 568 569 # Add CPython specific setup-hook that configures distutils.sysconfig to 570 # always load sysconfigdata from host Python. 571 postFixup = lib.optionalString (!stdenv.hostPlatform.isDarwin) '' 572 # https://github.com/python/cpython/blob/e488e300f5c01289c10906c2e53a8e43d6de32d8/configure.ac#L78 573 sysconfigdataName="$(make --eval $'print-sysconfigdata-name: 574 \t@echo _sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH) ' print-sysconfigdata-name)" 575 576 # The CPython interpreter contains a _sysconfigdata_<platform specific suffix> 577 # module that is imported by the sysconfig and distutils.sysconfig modules. 578 # The sysconfigdata module is generated at build time and contains settings 579 # required for building Python extension modules, such as include paths and 580 # other compiler flags. By default, the sysconfigdata module is loaded from 581 # the currently running interpreter (ie. the build platform interpreter), but 582 # when cross-compiling we want to load it from the host platform interpreter. 583 # This can be done using the _PYTHON_SYSCONFIGDATA_NAME environment variable. 584 # The _PYTHON_HOST_PLATFORM variable also needs to be set to get the correct 585 # platform suffix on extension modules. The correct values for these variables 586 # are not documented, and must be derived from the configure script (see links 587 # below). 588 cat <<EOF >> "$out/nix-support/setup-hook" 589 sysconfigdataHook() { 590 if [ "\$1" = '$out' ]; then 591 export _PYTHON_HOST_PLATFORM='${pythonHostPlatform}' 592 export _PYTHON_SYSCONFIGDATA_NAME='$sysconfigdataName' 593 fi 594 } 595 596 addEnvHooks "\$hostOffset" sysconfigdataHook 597 EOF 598 ''; 599 600 # Enforce that we don't have references to the OpenSSL -dev package, which we 601 # explicitly specify in our configure flags above. 602 disallowedReferences = lib.optionals (openssl != null && !static && !enableFramework) [ 603 openssl.dev 604 ] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [ 605 # Ensure we don't have references to build-time packages. 606 # These typically end up in shebangs. 607 pythonOnBuildForHost buildPackages.bash 608 ]; 609 610 separateDebugInfo = true; 611 612 passthru = passthru // { 613 doc = stdenv.mkDerivation { 614 inherit src; 615 name = "python${pythonVersion}-${version}-doc"; 616 617 patches = optionals (pythonAtLeast "3.9" && pythonOlder "3.10") [ 618 # https://github.com/python/cpython/issues/98366 619 (fetchpatch { 620 url = "https://github.com/python/cpython/commit/5612471501b05518287ed61c1abcb9ed38c03942.patch"; 621 hash = "sha256-p41hJwAiyRgyVjCVQokMSpSFg/VDDrqkCSxsodVb6vY="; 622 }) 623 ]; 624 625 dontConfigure = true; 626 627 dontBuild = true; 628 629 sphinxRoot = "Doc"; 630 631 postInstallSphinx = '' 632 mv $out/share/doc/* $out/share/doc/python${pythonVersion}-${version} 633 ''; 634 635 nativeBuildInputs = with pkgsBuildBuild.python3.pkgs; [ sphinxHook python-docs-theme ]; 636 }; 637 638 tests = passthru.tests // { 639 pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage; 640 }; 641 }; 642 643 enableParallelBuilding = true; 644 645 meta = with lib; { 646 homepage = "https://www.python.org"; 647 changelog = let 648 majorMinor = versions.majorMinor version; 649 dashedVersion = replaceStrings [ "." "a" "b" ] [ "-" "-alpha-" "-beta-" ] version; 650 in 651 if sourceVersion.suffix == "" then 652 "https://docs.python.org/release/${version}/whatsnew/changelog.html" 653 else 654 "https://docs.python.org/${majorMinor}/whatsnew/changelog.html#python-${dashedVersion}"; 655 description = "High-level dynamically-typed programming language"; 656 longDescription = '' 657 Python is a remarkably powerful dynamic programming language that 658 is used in a wide variety of application domains. Some of its key 659 distinguishing features include: clear, readable syntax; strong 660 introspection capabilities; intuitive object orientation; natural 661 expression of procedural code; full modularity, supporting 662 hierarchical packages; exception-based error handling; and very 663 high level dynamic data types. 664 ''; 665 license = licenses.psfl; 666 pkgConfigModules = [ "python3" ]; 667 platforms = platforms.linux ++ platforms.darwin ++ platforms.windows ++ platforms.freebsd; 668 mainProgram = executable; 669 maintainers = lib.teams.python.members; 670 }; 671})