at master 21 kB view raw
1{ 2 lib, 3 stdenv, 4 fetchurl, 5 perl, 6 gcc, 7 ncurses5, 8 ncurses6, 9 gmp, 10 libiconv, 11 numactl, 12 libffi, 13 llvmPackages, 14 replaceVarsWith, 15 coreutils, 16 targetPackages, 17 18 # minimal = true; will remove files that aren't strictly necessary for 19 # regular builds and GHC bootstrapping. 20 # This is "useful" for staying within hydra's output limits for at least the 21 # aarch64-linux architecture. 22 minimal ? false, 23}: 24 25# Prebuilt only does native 26assert stdenv.targetPlatform == stdenv.hostPlatform; 27 28let 29 downloadsUrl = "https://downloads.haskell.org/ghc"; 30 31 # Copy sha256 from https://downloads.haskell.org/~ghc/9.0.2/SHA256SUMS 32 version = "9.0.2"; 33 34 # Information about available bindists that we use in the build. 35 # 36 # # Bindist library checking 37 # 38 # The field `archSpecificLibraries` also provides a way for us get notified 39 # early when the upstream bindist changes its dependencies (e.g. because a 40 # newer Debian version is used that uses a new `ncurses` version). 41 # 42 # Usage: 43 # 44 # * You can find the `fileToCheckFor` of libraries by running `readelf -d` 45 # on the compiler binary (`exePathForLibraryCheck`). 46 # * To skip library checking for an architecture, 47 # set `exePathForLibraryCheck = null`. 48 # * To skip file checking for a specific arch specific library, 49 # set `fileToCheckFor = null`. 50 ghcBinDists = { 51 # Binary distributions for the default libc (e.g. glibc, or libSystem on Darwin) 52 # nixpkgs uses for the respective system. 53 defaultLibc = { 54 i686-linux = { 55 variantSuffix = ""; 56 src = { 57 url = "${downloadsUrl}/${version}/ghc-${version}-i386-deb9-linux.tar.xz"; 58 sha256 = "fdeb9f8928fbe994064778a8e1e85bb1a58a6cd3dd7b724fcc2a1dcfda6cad47"; 59 }; 60 exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2"; 61 archSpecificLibraries = [ 62 { 63 nixPackage = gmp; 64 fileToCheckFor = null; 65 } 66 # The i686-linux bindist provided by GHC HQ is currently built on Debian 9, 67 # which link it against `libtinfo.so.5` (ncurses 5). 68 # Other bindists are linked `libtinfo.so.6` (ncurses 6). 69 { 70 nixPackage = ncurses5; 71 fileToCheckFor = "libtinfo.so.5"; 72 } 73 ]; 74 }; 75 x86_64-linux = { 76 variantSuffix = ""; 77 src = { 78 url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-deb10-linux.tar.xz"; 79 sha256 = "5d0b9414b10cfb918453bcd01c5ea7a1824fe95948b08498d6780f20ba247afc"; 80 }; 81 exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2"; 82 archSpecificLibraries = [ 83 { 84 nixPackage = gmp; 85 fileToCheckFor = null; 86 } 87 { 88 nixPackage = ncurses6; 89 fileToCheckFor = "libtinfo.so.6"; 90 } 91 { 92 nixPackage = numactl; 93 fileToCheckFor = null; 94 } 95 ]; 96 }; 97 aarch64-linux = { 98 variantSuffix = ""; 99 src = { 100 url = "${downloadsUrl}/${version}/ghc-${version}-aarch64-deb10-linux.tar.xz"; 101 sha256 = "cb016344c70a872738a24af60bd15d3b18749087b9905c1b3f1b1549dc01f46d"; 102 }; 103 exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2"; 104 archSpecificLibraries = [ 105 { 106 nixPackage = gmp; 107 fileToCheckFor = null; 108 } 109 { 110 nixPackage = ncurses6; 111 fileToCheckFor = "libtinfo.so.6"; 112 } 113 { 114 nixPackage = numactl; 115 fileToCheckFor = null; 116 } 117 ]; 118 }; 119 x86_64-darwin = { 120 variantSuffix = ""; 121 src = { 122 url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-apple-darwin.tar.xz"; 123 sha256 = "e1fe990eb987f5c4b03e0396f9c228a10da71769c8a2bc8fadbc1d3b10a0f53a"; 124 }; 125 exePathForLibraryCheck = null; # we don't have a library check for darwin yet 126 archSpecificLibraries = [ 127 { 128 nixPackage = gmp; 129 fileToCheckFor = null; 130 } 131 { 132 nixPackage = ncurses6; 133 fileToCheckFor = null; 134 } 135 { 136 nixPackage = libiconv; 137 fileToCheckFor = null; 138 } 139 ]; 140 isHadrian = true; 141 }; 142 aarch64-darwin = { 143 variantSuffix = ""; 144 src = { 145 url = "${downloadsUrl}/${version}/ghc-${version}-aarch64-apple-darwin.tar.xz"; 146 sha256 = "b1fcab17fe48326d2ff302d70c12bc4cf4d570dfbbce68ab57c719cfec882b05"; 147 }; 148 exePathForLibraryCheck = null; # we don't have a library check for darwin yet 149 archSpecificLibraries = [ 150 { 151 nixPackage = gmp; 152 fileToCheckFor = null; 153 } 154 { 155 nixPackage = ncurses6; 156 fileToCheckFor = null; 157 } 158 { 159 nixPackage = libiconv; 160 fileToCheckFor = null; 161 } 162 ]; 163 isHadrian = true; 164 }; 165 }; 166 # Binary distributions for the musl libc for the respective system. 167 musl = { 168 x86_64-linux = { 169 variantSuffix = "-musl"; 170 src = { 171 url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-alpine3.12-linux-gmp.tar.xz"; 172 sha256 = "5bb1e7192c2b9fcff68930dbdc65509d345138e9a43c5d447056a68decc05ec8"; 173 }; 174 exePathForLibraryCheck = "bin/ghc"; 175 archSpecificLibraries = [ 176 { 177 nixPackage = gmp; 178 fileToCheckFor = null; 179 } 180 { 181 nixPackage = ncurses6; 182 fileToCheckFor = "libncursesw.so.6"; 183 } 184 ]; 185 isHadrian = true; 186 }; 187 }; 188 }; 189 190 distSetName = if stdenv.hostPlatform.isMusl then "musl" else "defaultLibc"; 191 192 binDistUsed = 193 ghcBinDists.${distSetName}.${stdenv.hostPlatform.system} 194 or (throw "cannot bootstrap GHC on this platform ('${stdenv.hostPlatform.system}' with libc '${distSetName}')"); 195 196 gmpUsed = 197 (builtins.head ( 198 builtins.filter ( 199 drv: lib.hasPrefix "gmp" (drv.nixPackage.name or "") 200 ) binDistUsed.archSpecificLibraries 201 )).nixPackage; 202 203 useLLVM = !(import ./common-have-ncg.nix { inherit lib stdenv version; }); 204 205 libPath = lib.makeLibraryPath ( 206 # Add arch-specific libraries. 207 map ({ nixPackage, ... }: nixPackage) binDistUsed.archSpecificLibraries 208 ); 209 210 libEnvVar = lib.optionalString stdenv.hostPlatform.isDarwin "DY" + "LD_LIBRARY_PATH"; 211 212 runtimeDeps = [ 213 targetPackages.stdenv.cc 214 targetPackages.stdenv.cc.bintools 215 coreutils # for cat 216 ] 217 ++ lib.optionals useLLVM [ 218 # Allow the use of newer LLVM versions; see the script for details. 219 (replaceVarsWith { 220 name = "subopt"; 221 src = ./subopt.bash; 222 dir = "bin"; 223 isExecutable = true; 224 preBuild = '' 225 name=opt 226 ''; 227 replacements = { 228 inherit (stdenv) shell; 229 opt = lib.getExe' llvmPackages.llvm "opt"; 230 }; 231 }) 232 (lib.getBin llvmPackages.llvm) 233 ] 234 # On darwin, we need unwrapped bintools as well (for otool) 235 ++ lib.optionals (stdenv.targetPlatform.linker == "cctools") [ 236 targetPackages.stdenv.cc.bintools.bintools 237 ]; 238 239in 240 241stdenv.mkDerivation { 242 inherit version; 243 pname = "ghc-binary${binDistUsed.variantSuffix}"; 244 245 src = fetchurl binDistUsed.src; 246 247 nativeBuildInputs = [ perl ]; 248 249 # Set LD_LIBRARY_PATH or equivalent so that the programs running as part 250 # of the bindist installer can find the libraries they expect. 251 # Cannot patchelf beforehand due to relative RPATHs that anticipate 252 # the final install location. 253 ${libEnvVar} = libPath; 254 255 postUnpack = 256 # Verify our assumptions of which `libtinfo.so` (ncurses) version is used, 257 # so that we know when ghc bindists upgrade that and we need to update the 258 # version used in `libPath`. 259 lib.optionalString (binDistUsed.exePathForLibraryCheck != null) 260 # Note the `*` glob because some GHCs have a suffix when unpacked, e.g. 261 # the musl bindist has dir `ghc-VERSION-x86_64-unknown-linux/`. 262 # As a result, don't shell-quote this glob when splicing the string. 263 ( 264 let 265 buildExeGlob = ''ghc-${version}*/"${binDistUsed.exePathForLibraryCheck}"''; 266 in 267 lib.concatStringsSep "\n" [ 268 ('' 269 shopt -u nullglob 270 echo "Checking that ghc binary exists in bindist at ${buildExeGlob}" 271 if ! test -e ${buildExeGlob}; then 272 echo >&2 "GHC binary ${binDistUsed.exePathForLibraryCheck} could not be found in the bindist build directory (at ${buildExeGlob}) for arch ${stdenv.hostPlatform.system}, please check that ghcBinDists correctly reflect the bindist dependencies!"; exit 1; 273 fi 274 '') 275 (lib.concatMapStringsSep "\n" ( 276 { fileToCheckFor, nixPackage }: 277 lib.optionalString (fileToCheckFor != null) '' 278 echo "Checking bindist for ${fileToCheckFor} to ensure that is still used" 279 if ! readelf -d ${buildExeGlob} | grep "${fileToCheckFor}"; then 280 echo >&2 "File ${fileToCheckFor} could not be found in ${binDistUsed.exePathForLibraryCheck} for arch ${stdenv.hostPlatform.system}, please check that ghcBinDists correctly reflect the bindist dependencies!"; exit 1; 281 fi 282 283 echo "Checking that the nix package ${nixPackage} contains ${fileToCheckFor}" 284 if ! test -e "${lib.getLib nixPackage}/lib/${fileToCheckFor}"; then 285 echo >&2 "Nix package ${nixPackage} did not contain ${fileToCheckFor} for arch ${stdenv.hostPlatform.system}, please check that ghcBinDists correctly reflect the bindist dependencies!"; exit 1; 286 fi 287 '' 288 ) binDistUsed.archSpecificLibraries) 289 ] 290 ) 291 # GHC has dtrace probes, which causes ld to try to open /usr/lib/libdtrace.dylib 292 # during linking 293 + lib.optionalString stdenv.hostPlatform.isDarwin '' 294 export NIX_LDFLAGS+=" -no_dtrace_dof" 295 # not enough room in the object files for the full path to libiconv :( 296 for exe in $(find . -type f -executable); do 297 isScript $exe && continue 298 ln -fs ${libiconv}/lib/libiconv.dylib $(dirname $exe)/libiconv.dylib 299 install_name_tool -change /usr/lib/libiconv.2.dylib @executable_path/libiconv.dylib $exe 300 done 301 '' 302 + 303 304 # Some scripts used during the build need to have their shebangs patched 305 '' 306 patchShebangs ghc-${version}/utils/ 307 patchShebangs ghc-${version}/configure 308 test -d ghc-${version}/inplace/bin && \ 309 patchShebangs ghc-${version}/inplace/bin 310 '' 311 + 312 # We have to patch the GMP paths for the integer-gmp package. 313 '' 314 find . -name ghc-bignum.buildinfo \ 315 -exec sed -i "s@extra-lib-dirs: @extra-lib-dirs: ${lib.getLib gmpUsed}/lib@" {} \; 316 317 # we need to modify the package db directly for hadrian bindists 318 find . -name 'ghc-bignum*.conf' \ 319 -exec sed -e '/^[a-z-]*library-dirs/a \ ${lib.getLib gmpUsed}/lib' -i {} \; 320 '' 321 + lib.optionalString stdenv.hostPlatform.isDarwin '' 322 # we need to modify the package db directly for hadrian bindists 323 # (all darwin bindists are hadrian-based for 9.2.2) 324 find . -name 'base*.conf' \ 325 -exec sed -e '/^[a-z-]*library-dirs/a \ ${lib.getLib libiconv}/lib' -i {} \; 326 327 # To link RTS in the end we also need libffi now 328 find . -name 'rts*.conf' \ 329 -exec sed -e '/^[a-z-]*library-dirs/a \ ${lib.getLib libffi}/lib' \ 330 -e 's@/.*/Developer/.*/usr/include/ffi@${lib.getDev libffi}/include@' \ 331 -i {} \; 332 '' 333 + 334 # Some platforms do HAVE_NUMA so -lnuma requires it in library-dirs in rts/package.conf.in 335 # FFI_LIB_DIR is a good indication of places it must be needed. 336 lib.optionalString 337 ( 338 lib.meta.availableOn stdenv.hostPlatform numactl 339 && builtins.any ({ nixPackage, ... }: nixPackage == numactl) binDistUsed.archSpecificLibraries 340 ) 341 '' 342 find . -name package.conf.in \ 343 -exec sed -i "s@FFI_LIB_DIR@FFI_LIB_DIR ${numactl.out}/lib@g" {} \; 344 '' 345 + 346 # Rename needed libraries and binaries, fix interpreter 347 lib.optionalString stdenv.hostPlatform.isLinux '' 348 find . -type f -executable -exec patchelf \ 349 --interpreter ${stdenv.cc.bintools.dynamicLinker} {} \; 350 ''; 351 352 # fix for `configure: error: Your linker is affected by binutils #16177` 353 preConfigure = lib.optionalString stdenv.targetPlatform.isAarch32 "LD=ld.gold"; 354 355 configurePlatforms = [ ]; 356 configureFlags = [ 357 "--with-gmp-includes=${lib.getDev gmpUsed}/include" 358 # Note `--with-gmp-libraries` does nothing for GHC bindists: 359 # https://gitlab.haskell.org/ghc/ghc/-/merge_requests/6124 360 ] 361 ++ lib.optional stdenv.hostPlatform.isDarwin "--with-gcc=${./gcc-clang-wrapper.sh}" 362 # From: https://github.com/NixOS/nixpkgs/pull/43369/commits 363 ++ lib.optional stdenv.hostPlatform.isMusl "--disable-ld-override"; 364 365 # No building is necessary, but calling make without flags ironically 366 # calls install-strip ... 367 dontBuild = true; 368 369 # GHC tries to remove xattrs when installing to work around Gatekeeper 370 # (see https://gitlab.haskell.org/ghc/ghc/-/issues/17418). This step normally 371 # succeeds in nixpkgs because xattrs are not allowed in the store, but it 372 # can fail when a file has the `com.apple.provenance` xattr, and it can’t be 373 # modified (such as target of the symlink to `libiconv.dylib`). 374 # The `com.apple.provenance` xattr is a new feature of macOS as of macOS 13. 375 # See: https://eclecticlight.co/2023/03/13/ventura-has-changed-app-quarantine-with-a-new-xattr/ 376 makeFlags = lib.optionals stdenv.buildPlatform.isDarwin [ "XATTR=/does-not-exist" ]; 377 378 # Patch scripts to include runtime dependencies in $PATH. 379 postInstall = '' 380 for i in "$out/bin/"*; do 381 test ! -h "$i" || continue 382 isScript "$i" || continue 383 sed -i -e '2i export PATH="${lib.makeBinPath runtimeDeps}:$PATH"' "$i" 384 done 385 '' 386 # On Darwin, GHC doesn't install a bundled libffi.so, but instead uses the 387 # system one (see postUnpack). Due to a bug in Hadrian, the (bundled) libffi 388 # headers are installed anyways. This problem has been fixed in GHC 9.2: 389 # https://gitlab.haskell.org/ghc/ghc/-/merge_requests/8189. While the system 390 # header should shadow the GHC installed ones, remove them to be safe. 391 + lib.optionalString (stdenv.hostPlatform.isDarwin && binDistUsed.isHadrian or false) '' 392 echo Deleting redundant libffi headers: 393 find "$out" '(' -name ffi.h -or -name ffitarget.h ')' -print -delete 394 ''; 395 396 # Apparently necessary for the ghc Alpine (musl) bindist: 397 # When we strip, and then run the 398 # patchelf --set-rpath "${libPath}:$(patchelf --print-rpath $p)" $p 399 # below, running ghc (e.g. during `installCheckPhase)` gives some apparently 400 # corrupted rpath or whatever makes the loader work on nonsensical strings: 401 # running install tests 402 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: : symbol not found 403 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: ir6zf6c9f86pfx8sr30n2vjy-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/../lib/x86_64-linux-ghc-8.10.5/libHSexceptions-0.10.4-ghc8.10.5.so: symbol not found 404 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: y/lib/ghc-8.10.5/bin/../lib/x86_64-linux-ghc-8.10.5/libHStemplate-haskell-2.16.0.0-ghc8.10.5.so: symbol not found 405 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: 8.10.5/libHStemplate-haskell-2.16.0.0-ghc8.10.5.so: symbol not found 406 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: �: symbol not found 407 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: �?: symbol not found 408 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: 64-linux-ghc-8.10.5/libHSexceptions-0.10.4-ghc8.10.5.so: symbol not found 409 # This is extremely bogus and should be investigated. 410 dontStrip = if stdenv.hostPlatform.isMusl then true else false; # `if` for explicitness 411 412 # On Linux, use patchelf to modify the executables so that they can 413 # find editline/gmp. 414 postFixup = 415 lib.optionalString (stdenv.hostPlatform.isLinux && !(binDistUsed.isStatic or false)) ( 416 if stdenv.hostPlatform.isAarch64 then 417 # Keep rpath as small as possible on aarch64 for patchelf#244. All Elfs 418 # are 2 directories deep from $out/lib, so pooling symlinks there makes 419 # a short rpath. 420 '' 421 (cd $out/lib; ln -s ${ncurses6.out}/lib/libtinfo.so.6) 422 (cd $out/lib; ln -s ${lib.getLib gmpUsed}/lib/libgmp.so.10) 423 (cd $out/lib; ln -s ${numactl.out}/lib/libnuma.so.1) 424 for p in $(find "$out/lib" -type f -name "*\.so*"); do 425 (cd $out/lib; ln -s $p) 426 done 427 428 for p in $(find "$out/lib" -type f -executable); do 429 if isELF "$p"; then 430 echo "Patchelfing $p" 431 patchelf --set-rpath "\$ORIGIN:\$ORIGIN/../.." $p 432 fi 433 done 434 '' 435 else 436 '' 437 for p in $(find "$out" -type f -executable); do 438 if isELF "$p"; then 439 echo "Patchelfing $p" 440 patchelf --set-rpath "${libPath}:$(patchelf --print-rpath $p)" $p 441 fi 442 done 443 '' 444 ) 445 + lib.optionalString stdenv.hostPlatform.isDarwin '' 446 # not enough room in the object files for the full path to libiconv :( 447 for exe in $(find "$out" -type f -executable); do 448 isScript $exe && continue 449 ln -fs ${libiconv}/lib/libiconv.dylib $(dirname $exe)/libiconv.dylib 450 install_name_tool -change /usr/lib/libiconv.2.dylib @executable_path/libiconv.dylib $exe 451 done 452 453 for file in $(find "$out" -name setup-config); do 454 substituteInPlace $file --replace /usr/bin/ranlib "$(type -P ranlib)" 455 done 456 '' 457 + lib.optionalString minimal '' 458 # Remove profiling files 459 find $out -type f -name '*.p_o' -delete 460 find $out -type f -name '*.p_hi' -delete 461 find $out -type f -name '*_p.a' -delete 462 # `-f` because e.g. musl bindist does not have this file. 463 rm -f $out/lib/ghc-*/bin/ghc-iserv-prof 464 # Hydra will redistribute this derivation, so we have to keep the docs for 465 # legal reasons (retaining the legal notices etc) 466 # As a last resort we could unpack the docs separately and symlink them in. 467 # They're in $out/share/{doc,man}. 468 '' 469 # Recache package db which needs to happen for Hadrian bindists 470 # where we modify the package db before installing 471 + '' 472 shopt -s nullglob 473 package_db=("$out"/lib/ghc-*/lib/package.conf.d "$out"/lib/ghc-*/package.conf.d) 474 "$out/bin/ghc-pkg" --package-db="$package_db" recache 475 ''; 476 477 # GHC cannot currently produce outputs that are ready for `-pie` linking. 478 # Thus, disable `pie` hardening, otherwise `recompile with -fPIE` errors appear. 479 # See: 480 # * https://github.com/NixOS/nixpkgs/issues/129247 481 # * https://gitlab.haskell.org/ghc/ghc/-/issues/19580 482 hardeningDisable = [ "pie" ]; 483 484 doInstallCheck = true; 485 installCheckPhase = '' 486 # Sanity check, can ghc create executables? 487 cd $TMP 488 mkdir test-ghc; cd test-ghc 489 cat > main.hs << EOF 490 {-# LANGUAGE TemplateHaskell #-} 491 module Main where 492 main = putStrLn \$([|"yes"|]) 493 EOF 494 env -i $out/bin/ghc --make main.hs || exit 1 495 echo compilation ok 496 [ $(./main) == "yes" ] 497 ''; 498 499 passthru = { 500 targetPrefix = ""; 501 enableShared = true; 502 503 inherit llvmPackages; 504 505 # Our Cabal compiler name 506 haskellCompilerName = "ghc-${version}"; 507 } 508 # We duplicate binDistUsed here since we have a sensible default even if no bindist is available, 509 # this makes sure that getting the `meta` attribute doesn't throw even on unsupported platforms. 510 // lib.optionalAttrs (ghcBinDists.${distSetName}.${stdenv.hostPlatform.system}.isHadrian or false) { 511 # Normal GHC derivations expose the hadrian derivation used to build them 512 # here. In the case of bindists we just make sure that the attribute exists, 513 # as it is used for checking if a GHC derivation has been built with hadrian. 514 # The isHadrian mechanism will become obsolete with GHCs that use hadrian 515 # exclusively, i.e. 9.6 (and 9.4?). 516 hadrian = null; 517 }; 518 519 meta = rec { 520 homepage = "http://haskell.org/ghc"; 521 description = "Glasgow Haskell Compiler"; 522 license = lib.licenses.bsd3; 523 # HACK: since we can't encode the libc / abi in platforms, we need 524 # to make the platform list dependent on the evaluation platform 525 # in order to avoid eval errors with musl which supports less 526 # platforms than the default libcs (i. e. glibc / libSystem). 527 # This is done for the benefit of Hydra, so `packagePlatforms` 528 # won't return any platforms that would cause an evaluation 529 # failure for `pkgsMusl.haskell.compiler.ghc922Binary`, as 530 # long as the evaluator runs on a platform that supports 531 # `pkgsMusl`. 532 platforms = builtins.attrNames ghcBinDists.${distSetName}; 533 teams = [ lib.teams.haskell ]; 534 }; 535}