at 18.03-beta 1232 lines 35 kB view raw
1set -eu 2set -o pipefail 3 4if (( "${NIX_DEBUG:-0}" >= 6 )); then 5 set -x 6fi 7 8: ${outputs:=out} 9 10 11###################################################################### 12# Hook handling. 13 14 15# Run all hooks with the specified name in the order in which they 16# were added, stopping if any fails (returns a non-zero exit 17# code). The hooks for <hookName> are the shell function or variable 18# <hookName>, and the values of the shell array ‘<hookName>Hooks’. 19runHook() { 20 local oldOpts="$(shopt -po nounset)" 21 set -u # May be called from elsewhere, so do `set -u`. 22 23 local hookName="$1" 24 shift 25 local hooksSlice="${hookName%Hook}Hooks[@]" 26 27 local hook 28 # Hack around old bash being bad and thinking empty arrays are 29 # undefined. 30 for hook in "_callImplicitHook 0 $hookName" ${!hooksSlice+"${!hooksSlice}"}; do 31 _eval "$hook" "$@" 32 set -u # To balance `_eval` 33 done 34 35 eval "${oldOpts}" 36 return 0 37} 38 39 40# Run all hooks with the specified name, until one succeeds (returns a 41# zero exit code). If none succeed, return a non-zero exit code. 42runOneHook() { 43 local oldOpts="$(shopt -po nounset)" 44 set -u # May be called from elsewhere, so do `set -u`. 45 46 local hookName="$1" 47 shift 48 local hooksSlice="${hookName%Hook}Hooks[@]" 49 50 local hook ret=1 51 # Hack around old bash like above 52 for hook in "_callImplicitHook 1 $hookName" ${!hooksSlice+"${!hooksSlice}"}; do 53 if _eval "$hook" "$@"; then 54 ret=0 55 break 56 fi 57 set -u # To balance `_eval` 58 done 59 60 eval "${oldOpts}" 61 return "$ret" 62} 63 64 65# Run the named hook, either by calling the function with that name or 66# by evaluating the variable with that name. This allows convenient 67# setting of hooks both from Nix expressions (as attributes / 68# environment variables) and from shell scripts (as functions). If you 69# want to allow multiple hooks, use runHook instead. 70_callImplicitHook() { 71 set -u 72 local def="$1" 73 local hookName="$2" 74 case "$(type -t "$hookName")" in 75 (function|alias|builtin) 76 set +u 77 "$hookName";; 78 (file) 79 set +u 80 source "$hookName";; 81 (keyword) :;; 82 (*) if [ -z "${!hookName:-}" ]; then 83 return "$def"; 84 else 85 set +u 86 eval "${!hookName}" 87 fi;; 88 esac 89 # `_eval` expects hook to need nounset disable and leave it 90 # disabled anyways, so Ok to to delegate. The alternative of a 91 # return trap is no good because it would affect nested returns. 92} 93 94 95# A function wrapper around ‘eval’ that ensures that ‘return’ inside 96# hooks exits the hook, not the caller. Also will only pass args if 97# command can take them 98_eval() { 99 if [ "$(type -t "$1")" = function ]; then 100 set +u 101 "$@" # including args 102 else 103 set +u 104 eval "$1" 105 fi 106 # `run*Hook` reenables `set -u` 107} 108 109 110###################################################################### 111# Logging. 112 113# Obsolete. 114stopNest() { true; } 115header() { echo "$1"; } 116closeNest() { true; } 117 118# Prints a command such that all word splits are unambiguous. We need 119# to split the command in three parts because the middle format string 120# will be, and must be, repeated for each argument. The first argument 121# goes before the ':' and is just for convenience. 122echoCmd() { 123 printf "%s:" "$1" 124 shift 125 printf ' %q' "$@" 126 echo 127} 128 129 130###################################################################### 131# Error handling. 132 133exitHandler() { 134 exitCode="$?" 135 set +e 136 137 if [ -n "${showBuildStats:-}" ]; then 138 times > "$NIX_BUILD_TOP/.times" 139 local -a times=($(cat "$NIX_BUILD_TOP/.times")) 140 # Print the following statistics: 141 # - user time for the shell 142 # - system time for the shell 143 # - user time for all child processes 144 # - system time for all child processes 145 echo "build time elapsed: " "${times[@]}" 146 fi 147 148 if (( "$exitCode" != 0 )); then 149 runHook failureHook 150 151 # If the builder had a non-zero exit code and 152 # $succeedOnFailure is set, create the file 153 # ‘$out/nix-support/failed’ to signal failure, and exit 154 # normally. Otherwise, return the original exit code. 155 if [ -n "${succeedOnFailure:-}" ]; then 156 echo "build failed with exit code $exitCode (ignored)" 157 mkdir -p "$out/nix-support" 158 printf "%s" "$exitCode" > "$out/nix-support/failed" 159 exit 0 160 fi 161 162 else 163 runHook exitHook 164 fi 165 166 exit "$exitCode" 167} 168 169trap "exitHandler" EXIT 170 171 172###################################################################### 173# Helper functions. 174 175 176addToSearchPathWithCustomDelimiter() { 177 local delimiter="$1" 178 local varName="$2" 179 local dir="$3" 180 if [ -d "$dir" ]; then 181 export "${varName}=${!varName:+${!varName}${delimiter}}${dir}" 182 fi 183} 184 185PATH_DELIMITER=':' 186 187addToSearchPath() { 188 addToSearchPathWithCustomDelimiter "${PATH_DELIMITER}" "$@" 189} 190 191# Add $1/lib* into rpaths. 192# The function is used in multiple-outputs.sh hook, 193# so it is defined here but tried after the hook. 194_addRpathPrefix() { 195 if [ "${NIX_NO_SELF_RPATH:-0}" != 1 ]; then 196 export NIX_LDFLAGS="-rpath $1/lib $NIX_LDFLAGS" 197 if [ -n "${NIX_LIB64_IN_SELF_RPATH:-}" ]; then 198 export NIX_LDFLAGS="-rpath $1/lib64 $NIX_LDFLAGS" 199 fi 200 if [ -n "${NIX_LIB32_IN_SELF_RPATH:-}" ]; then 201 export NIX_LDFLAGS="-rpath $1/lib32 $NIX_LDFLAGS" 202 fi 203 fi 204} 205 206# Return success if the specified file is an ELF object. 207isELF() { 208 local fn="$1" 209 local fd 210 local magic 211 exec {fd}< "$fn" 212 read -r -n 4 -u "$fd" magic 213 exec {fd}<&- 214 if [[ "$magic" =~ ELF ]]; then return 0; else return 1; fi 215} 216 217# Return success if the specified file is a script (i.e. starts with 218# "#!"). 219isScript() { 220 local fn="$1" 221 local fd 222 local magic 223 if ! [ -x /bin/sh ]; then return 0; fi 224 exec {fd}< "$fn" 225 read -r -n 2 -u "$fd" magic 226 exec {fd}<&- 227 if [[ "$magic" =~ \#! ]]; then return 0; else return 1; fi 228} 229 230# printf unfortunately will print a trailing newline regardless 231printLines() { 232 (( "$#" > 0 )) || return 0 233 printf '%s\n' "$@" 234} 235 236printWords() { 237 (( "$#" > 0 )) || return 0 238 printf '%s ' "$@" 239} 240 241###################################################################### 242# Initialisation. 243 244 245# Set a fallback default value for SOURCE_DATE_EPOCH, used by some 246# build tools to provide a deterministic substitute for the "current" 247# time. Note that 1 = 1970-01-01 00:00:01. We don't use 0 because it 248# confuses some applications. 249export SOURCE_DATE_EPOCH 250: ${SOURCE_DATE_EPOCH:=1} 251 252 253# Wildcard expansions that don't match should expand to an empty list. 254# This ensures that, for instance, "for i in *; do ...; done" does the 255# right thing. 256shopt -s nullglob 257 258 259# Set up the initial path. 260PATH= 261for i in $initialPath; do 262 if [ "$i" = / ]; then i=; fi 263 addToSearchPath PATH "$i/bin" 264done 265 266if (( "${NIX_DEBUG:-0}" >= 1 )); then 267 echo "initial path: $PATH" 268fi 269 270 271# Check that the pre-hook initialised SHELL. 272if [ -z "${SHELL:-}" ]; then echo "SHELL not set"; exit 1; fi 273BASH="$SHELL" 274export CONFIG_SHELL="$SHELL" 275 276 277# Dummy implementation of the paxmark function. On Linux, this is 278# overwritten by paxctl's setup hook. 279paxmark() { true; } 280 281 282# Execute the pre-hook. 283if [ -z "${shell:-}" ]; then export shell="$SHELL"; fi 284runHook preHook 285 286 287# Allow the caller to augment buildInputs (it's not always possible to 288# do this before the call to setup.sh, since the PATH is empty at that 289# point; here we have a basic Unix environment). 290runHook addInputsHook 291 292 293# Package accumulators 294 295# shellcheck disable=SC2034 296declare -a pkgsBuildBuild pkgsBuildHost pkgsBuildTarget 297declare -a pkgsHostHost pkgsHostTarget 298declare -a pkgsTargetTarget 299 300declare -ra pkgBuildAccumVars=(pkgsBuildBuild pkgsBuildHost pkgsBuildTarget) 301declare -ra pkgHostAccumVars=(pkgsHostHost pkgsHostTarget) 302declare -ra pkgTargetAccumVars=(pkgsTargetTarget) 303 304declare -ra pkgAccumVarVars=(pkgBuildAccumVars pkgHostAccumVars pkgTargetAccumVars) 305 306 307# Hooks 308 309declare -a envBuildBuildHooks envBuildHostHooks envBuildTargetHooks 310declare -a envHostHostHooks envHostTargetHooks 311declare -a envTargetTargetHooks 312 313declare -ra pkgBuildHookVars=(envBuildBuildHook envBuildHostHook envBuildTargetHook) 314declare -ra pkgHostHookVars=(envHostHostHook envHostTargetHook) 315declare -ra pkgTargetHookVars=(envTargetTargetHook) 316 317declare -ra pkgHookVarVars=(pkgBuildHookVars pkgHostHookVars pkgTargetHookVars) 318 319# Add env hooks for all sorts of deps with the specified host offset. 320addEnvHooks() { 321 local depHostOffset="$1" 322 shift 323 local pkgHookVarsSlice="${pkgHookVarVars[$depHostOffset + 1]}[@]" 324 local pkgHookVar 325 for pkgHookVar in "${!pkgHookVarsSlice}"; do 326 eval "${pkgHookVar}s"'+=("$@")' 327 done 328} 329 330 331# Propagated dep files 332 333declare -ra propagatedBuildDepFiles=( 334 propagated-build-build-deps 335 propagated-native-build-inputs # Legacy name for back-compat 336 propagated-build-target-deps 337) 338declare -ra propagatedHostDepFiles=( 339 propagated-host-host-deps 340 propagated-build-inputs # Legacy name for back-compat 341) 342declare -ra propagatedTargetDepFiles=( 343 propagated-target-target-deps 344) 345declare -ra propagatedDepFilesVars=( 346 propagatedBuildDepFiles 347 propagatedHostDepFiles 348 propagatedTargetDepFiles 349) 350 351# Platform offsets: build = -1, host = 0, target = 1 352declare -ra allPlatOffsets=(-1 0 1) 353 354 355# Mutually-recursively find all build inputs. See the dependency section of the 356# stdenv chapter of the Nixpkgs manual for the specification this algorithm 357# implements. 358findInputs() { 359 local -r pkg="$1" 360 local -ri hostOffset="$2" 361 local -ri targetOffset="$3" 362 363 # Sanity check 364 (( "$hostOffset" <= "$targetOffset" )) || exit -1 365 366 local varVar="${pkgAccumVarVars[$hostOffset + 1]}" 367 local varRef="$varVar[\$targetOffset - \$hostOffset]" 368 local var="${!varRef}" 369 unset -v varVar varRef 370 371 # TODO(@Ericson2314): Restore using associative array once Darwin 372 # nix-shell doesn't use impure bash. This should replace the O(n) 373 # case with an O(1) hash map lookup, assuming bash is implemented 374 # well :D. 375 local varSlice="$var[*]" 376 # ${..-} to hack around old bash empty array problem 377 case "${!varSlice-}" in 378 *" $pkg "*) return 0 ;; 379 esac 380 unset -v varSlice 381 382 eval "$var"'+=("$pkg")' 383 384 if ! [ -e "$pkg" ]; then 385 echo "build input $pkg does not exist" >&2 386 exit 1 387 fi 388 389 # The current package's host and target offset together 390 # provide a <=-preserving homomorphism from the relative 391 # offsets to current offset 392 function mapOffset() { 393 local -ri inputOffset="$1" 394 if (( "$inputOffset" <= 0 )); then 395 local -ri outputOffset="$inputOffset + $hostOffset" 396 else 397 local -ri outputOffset="$inputOffset - 1 + $targetOffset" 398 fi 399 echo "$outputOffset" 400 } 401 402 # Host offset relative to that of the package whose immediate 403 # dependencies we are currently exploring. 404 local -i relHostOffset 405 for relHostOffset in "${allPlatOffsets[@]}"; do 406 # `+ 1` so we start at 0 for valid index 407 local files="${propagatedDepFilesVars[$relHostOffset + 1]}" 408 409 # Host offset relative to the package currently being 410 # built---as absolute an offset as will be used. 411 local -i hostOffsetNext 412 hostOffsetNext="$(mapOffset relHostOffset)" 413 414 # Ensure we're in bounds relative to the package currently 415 # being built. 416 [[ "${allPlatOffsets[*]}" = *"$hostOffsetNext"* ]] || continue 417 418 # Target offset relative to the *host* offset of the package 419 # whose immediate dependencies we are currently exploring. 420 local -i relTargetOffset 421 for relTargetOffset in "${allPlatOffsets[@]}"; do 422 (( "$relHostOffset" <= "$relTargetOffset" )) || continue 423 424 local fileRef="${files}[$relTargetOffset - $relHostOffset]" 425 local file="${!fileRef}" 426 unset -v fileRef 427 428 # Target offset relative to the package currently being 429 # built. 430 local -i targetOffsetNext 431 targetOffsetNext="$(mapOffset relTargetOffset)" 432 433 # Once again, ensure we're in bounds relative to the 434 # package currently being built. 435 [[ "${allPlatOffsets[*]}" = *"$targetOffsetNext"* ]] || continue 436 437 [[ -f "$pkg/nix-support/$file" ]] || continue 438 439 local pkgNext 440 for pkgNext in $(< "$pkg/nix-support/$file"); do 441 findInputs "$pkgNext" "$hostOffsetNext" "$targetOffsetNext" 442 done 443 done 444 done 445} 446 447# Make sure all are at least defined as empty 448: ${depsBuildBuild=} ${depsBuildBuildPropagated=} 449: ${nativeBuildInputs=} ${propagatedNativeBuildInputs=} ${defaultNativeBuildInputs=} 450: ${depsBuildTarget=} ${depsBuildTargetPropagated=} 451: ${depsHostHost=} ${depsHostHostPropagated=} 452: ${buildInputs=} ${propagatedBuildInputs=} ${defaultBuildInputs=} 453: ${depsTargetTarget=} ${depsTargetTargetPropagated=} 454 455for pkg in $depsBuildBuild $depsBuildBuildPropagated; do 456 findInputs "$pkg" -1 -1 457done 458for pkg in $nativeBuildInputs $propagatedNativeBuildInputs; do 459 findInputs "$pkg" -1 0 460done 461for pkg in $depsBuildTarget $depsBuildTargetPropagated; do 462 findInputs "$pkg" -1 1 463done 464for pkg in $depsHostHost $depsHostHostPropagated; do 465 findInputs "$pkg" 0 0 466done 467for pkg in $buildInputs $propagatedBuildInputs ; do 468 findInputs "$pkg" 0 1 469done 470for pkg in $depsTargetTarget $depsTargetTargetPropagated; do 471 findInputs "$pkg" 1 1 472done 473# Default inputs must be processed last 474for pkg in $defaultNativeBuildInputs; do 475 findInputs "$pkg" -1 0 476done 477for pkg in $defaultBuildInputs; do 478 findInputs "$pkg" 0 1 479done 480 481# Add package to the future PATH and run setup hooks 482activatePackage() { 483 local pkg="$1" 484 local -ri hostOffset="$2" 485 local -ri targetOffset="$3" 486 487 # Sanity check 488 (( "$hostOffset" <= "$targetOffset" )) || exit -1 489 490 if [ -f "$pkg" ]; then 491 local oldOpts="$(shopt -po nounset)" 492 set +u 493 source "$pkg" 494 eval "$oldOpts" 495 fi 496 497 # Only dependencies whose host platform is guaranteed to match the 498 # build platform are included here. That would be `depsBuild*`, 499 # and legacy `nativeBuildInputs`, in general. If we aren't cross 500 # compiling, however, everything can be put on the PATH. To ease 501 # the transition, we do include everything in thatcase. 502 # 503 # TODO(@Ericson2314): Don't special-case native compilation 504 if [[ ( -z "${crossConfig-}" || "$hostOffset" -le -1 ) && -d "$pkg/bin" ]]; then 505 addToSearchPath _PATH "$pkg/bin" 506 fi 507 508 if [[ -f "$pkg/nix-support/setup-hook" ]]; then 509 local oldOpts="$(shopt -po nounset)" 510 set +u 511 source "$pkg/nix-support/setup-hook" 512 eval "$oldOpts" 513 fi 514} 515 516_activatePkgs() { 517 local -i hostOffset targetOffset 518 local pkg 519 520 for hostOffset in "${allPlatOffsets[@]}"; do 521 local pkgsVar="${pkgAccumVarVars[$hostOffset + 1]}" 522 for targetOffset in "${allPlatOffsets[@]}"; do 523 (( "$hostOffset" <= "$targetOffset" )) || continue 524 local pkgsRef="${pkgsVar}[$targetOffset - $hostOffset]" 525 local pkgsSlice="${!pkgsRef}[@]" 526 for pkg in ${!pkgsSlice+"${!pkgsSlice}"}; do 527 activatePackage "$pkg" "$hostOffset" "$targetOffset" 528 done 529 done 530 done 531} 532 533# Run the package setup hooks and build _PATH 534_activatePkgs 535 536# Set the relevant environment variables to point to the build inputs 537# found above. 538# 539# These `depOffset`s, beyond indexing the arrays, also tell the env 540# hook what sort of dependency (ignoring propagatedness) is being 541# passed to the env hook. In a real language, we'd append a closure 542# with this information to the relevant env hook array, but bash 543# doesn't have closures, so it's easier to just pass this in. 544_addToEnv() { 545 local -i depHostOffset depTargetOffset 546 local pkg 547 548 for depHostOffset in "${allPlatOffsets[@]}"; do 549 local hookVar="${pkgHookVarVars[$depHostOffset + 1]}" 550 local pkgsVar="${pkgAccumVarVars[$depHostOffset + 1]}" 551 for depTargetOffset in "${allPlatOffsets[@]}"; do 552 (( "$depHostOffset" <= "$depTargetOffset" )) || continue 553 local hookRef="${hookVar}[$depTargetOffset - $depHostOffset]" 554 if [[ -z "${crossConfig-}" ]]; then 555 # Apply environment hooks to all packages during native 556 # compilation to ease the transition. 557 # 558 # TODO(@Ericson2314): Don't special-case native compilation 559 for pkg in \ 560 ${pkgsBuildBuild+"${pkgsBuildBuild[@]}"} \ 561 ${pkgsBuildHost+"${pkgsBuildHost[@]}"} \ 562 ${pkgsBuildTarget+"${pkgsBuildTarget[@]}"} \ 563 ${pkgsHostHost+"${pkgsHostHost[@]}"} \ 564 ${pkgsHostTarget+"${pkgsHostTarget[@]}"} \ 565 ${pkgsTargetTarget+"${pkgsTargetTarget[@]}"} 566 do 567 runHook "${!hookRef}" "$pkg" 568 done 569 else 570 local pkgsRef="${pkgsVar}[$depTargetOffset - $depHostOffset]" 571 local pkgsSlice="${!pkgsRef}[@]" 572 for pkg in ${!pkgsSlice+"${!pkgsSlice}"}; do 573 runHook "${!hookRef}" "$pkg" 574 done 575 fi 576 done 577 done 578} 579 580# Run the package-specific hooks set by the setup-hook scripts. 581_addToEnv 582 583 584_addRpathPrefix "$out" 585 586 587# Set the TZ (timezone) environment variable, otherwise commands like 588# `date' will complain (e.g., `Tue Mar 9 10:01:47 Local time zone must 589# be set--see zic manual page 2004'). 590export TZ=UTC 591 592 593# Set the prefix. This is generally $out, but it can be overriden, 594# for instance if we just want to perform a test build/install to a 595# temporary location and write a build report to $out. 596if [ -z "${prefix:-}" ]; then 597 prefix="$out"; 598fi 599 600if [ "${useTempPrefix:-}" = 1 ]; then 601 prefix="$NIX_BUILD_TOP/tmp_prefix"; 602fi 603 604 605PATH="${_PATH-}${_PATH:+${PATH:+:}}$PATH" 606if (( "${NIX_DEBUG:-0}" >= 1 )); then 607 echo "final path: $PATH" 608fi 609 610 611# Make GNU Make produce nested output. 612export NIX_INDENT_MAKE=1 613 614 615# Normalize the NIX_BUILD_CORES variable. The value might be 0, which 616# means that we're supposed to try and auto-detect the number of 617# available CPU cores at run-time. 618 619if [ -z "${NIX_BUILD_CORES:-}" ]; then 620 NIX_BUILD_CORES="1" 621elif [ "$NIX_BUILD_CORES" -le 0 ]; then 622 NIX_BUILD_CORES=$(nproc 2>/dev/null || true) 623 if expr >/dev/null 2>&1 "$NIX_BUILD_CORES" : "^[0-9][0-9]*$"; then 624 : 625 else 626 NIX_BUILD_CORES="1" 627 fi 628fi 629export NIX_BUILD_CORES 630 631 632# Prevent OpenSSL-based applications from using certificates in 633# /etc/ssl. 634# Leave it in shells for convenience. 635if [ -z "${SSL_CERT_FILE:-}" ] && [ -z "${IN_NIX_SHELL:-}" ]; then 636 export SSL_CERT_FILE=/no-cert-file.crt 637fi 638 639 640###################################################################### 641# Textual substitution functions. 642 643 644substitute() { 645 local input="$1" 646 local output="$2" 647 shift 2 648 649 if [ ! -f "$input" ]; then 650 echo "substitute(): ERROR: file '$input' does not exist" >&2 651 return 1 652 fi 653 654 local content 655 # read returns non-0 on EOF, so we want read to fail 656 if IFS='' read -r -N 0 content < "$input"; then 657 echo "substitute(): ERROR: File \"$input\" has null bytes, won't process" >&2 658 return 1 659 fi 660 661 while (( "$#" )); do 662 case "$1" in 663 --replace) 664 pattern="$2" 665 replacement="$3" 666 shift 3 667 ;; 668 669 --subst-var) 670 local varName="$2" 671 shift 2 672 # check if the used nix attribute name is a valid bash name 673 if ! [[ "$varName" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then 674 echo "substitute(): ERROR: substitution variables must be valid Bash names, \"$varName\" isn't." >&2 675 return 1 676 fi 677 pattern="@$varName@" 678 replacement="${!varName}" 679 ;; 680 681 --subst-var-by) 682 pattern="@$2@" 683 replacement="$3" 684 shift 3 685 ;; 686 687 *) 688 echo "substitute(): ERROR: Invalid command line argument: $1" >&2 689 return 1 690 ;; 691 esac 692 693 content="${content//"$pattern"/$replacement}" 694 done 695 696 if [ -e "$output" ]; then chmod +w "$output"; fi 697 printf "%s" "$content" > "$output" 698} 699 700 701substituteInPlace() { 702 local fileName="$1" 703 shift 704 substitute "$fileName" "$fileName" "$@" 705} 706 707 708# Substitute all environment variables that start with a lowercase character and 709# are valid Bash names. 710substituteAll() { 711 local input="$1" 712 local output="$2" 713 local -a args=() 714 715 for varName in $(awk 'BEGIN { for (v in ENVIRON) if (v ~ /^[a-z][a-zA-Z0-9_]*$/) print v }'); do 716 if (( "${NIX_DEBUG:-0}" >= 1 )); then 717 printf "@%s@ -> %q\n" "${varName}" "${!varName}" 718 fi 719 args+=("--subst-var" "$varName") 720 done 721 722 substitute "$input" "$output" "${args[@]}" 723} 724 725 726substituteAllInPlace() { 727 local fileName="$1" 728 shift 729 substituteAll "$fileName" "$fileName" "$@" 730} 731 732 733###################################################################### 734# What follows is the generic builder. 735 736 737# This function is useful for debugging broken Nix builds. It dumps 738# all environment variables to a file `env-vars' in the build 739# directory. If the build fails and the `-K' option is used, you can 740# then go to the build directory and source in `env-vars' to reproduce 741# the environment used for building. 742dumpVars() { 743 if [ "${noDumpEnvVars:-0}" != 1 ]; then 744 export > "$NIX_BUILD_TOP/env-vars" || true 745 fi 746} 747 748 749# Utility function: echo the base name of the given path, with the 750# prefix `HASH-' removed, if present. 751stripHash() { 752 local strippedName 753 # On separate line for `set -e` 754 strippedName="$(basename "$1")" 755 if echo "$strippedName" | grep -q '^[a-z0-9]\{32\}-'; then 756 echo "$strippedName" | cut -c34- 757 else 758 echo "$strippedName" 759 fi 760} 761 762 763unpackCmdHooks+=(_defaultUnpack) 764_defaultUnpack() { 765 local fn="$1" 766 767 if [ -d "$fn" ]; then 768 769 # We can't preserve hardlinks because they may have been 770 # introduced by store optimization, which might break things 771 # in the build. 772 cp -pr --reflink=auto -- "$fn" "$(stripHash "$fn")" 773 774 else 775 776 case "$fn" in 777 *.tar.xz | *.tar.lzma) 778 # Don't rely on tar knowing about .xz. 779 xz -d < "$fn" | tar xf - 780 ;; 781 *.tar | *.tar.* | *.tgz | *.tbz2) 782 # GNU tar can automatically select the decompression method 783 # (info "(tar) gzip"). 784 tar xf "$fn" 785 ;; 786 *) 787 return 1 788 ;; 789 esac 790 791 fi 792} 793 794 795unpackFile() { 796 curSrc="$1" 797 header "unpacking source archive $curSrc" 3 798 if ! runOneHook unpackCmd "$curSrc"; then 799 echo "do not know how to unpack source archive $curSrc" 800 exit 1 801 fi 802} 803 804 805unpackPhase() { 806 runHook preUnpack 807 808 if [ -z "${srcs:-}" ]; then 809 if [ -z "${src:-}" ]; then 810 # shellcheck disable=SC2016 811 echo 'variable $src or $srcs should point to the source' 812 exit 1 813 fi 814 srcs="$src" 815 fi 816 817 # To determine the source directory created by unpacking the 818 # source archives, we record the contents of the current 819 # directory, then look below which directory got added. Yeah, 820 # it's rather hacky. 821 local dirsBefore="" 822 for i in *; do 823 if [ -d "$i" ]; then 824 dirsBefore="$dirsBefore $i " 825 fi 826 done 827 828 # Unpack all source archives. 829 for i in $srcs; do 830 unpackFile "$i" 831 done 832 833 # Find the source directory. 834 835 # set to empty if unset 836 : ${sourceRoot=} 837 838 if [ -n "${setSourceRoot:-}" ]; then 839 runOneHook setSourceRoot 840 elif [ -z "$sourceRoot" ]; then 841 for i in *; do 842 if [ -d "$i" ]; then 843 case $dirsBefore in 844 *\ $i\ *) 845 ;; 846 *) 847 if [ -n "$sourceRoot" ]; then 848 echo "unpacker produced multiple directories" 849 exit 1 850 fi 851 sourceRoot="$i" 852 ;; 853 esac 854 fi 855 done 856 fi 857 858 if [ -z "$sourceRoot" ]; then 859 echo "unpacker appears to have produced no directories" 860 exit 1 861 fi 862 863 echo "source root is $sourceRoot" 864 865 # By default, add write permission to the sources. This is often 866 # necessary when sources have been copied from other store 867 # locations. 868 if [ "${dontMakeSourcesWritable:-0}" != 1 ]; then 869 chmod -R u+w -- "$sourceRoot" 870 fi 871 872 runHook postUnpack 873} 874 875 876patchPhase() { 877 runHook prePatch 878 879 for i in ${patches:-}; do 880 header "applying patch $i" 3 881 local uncompress=cat 882 case "$i" in 883 *.gz) 884 uncompress="gzip -d" 885 ;; 886 *.bz2) 887 uncompress="bzip2 -d" 888 ;; 889 *.xz) 890 uncompress="xz -d" 891 ;; 892 *.lzma) 893 uncompress="lzma -d" 894 ;; 895 esac 896 # "2>&1" is a hack to make patch fail if the decompressor fails (nonexistent patch, etc.) 897 # shellcheck disable=SC2086 898 $uncompress < "$i" 2>&1 | patch ${patchFlags:--p1} 899 done 900 901 runHook postPatch 902} 903 904 905fixLibtool() { 906 sed -i -e 's^eval sys_lib_.*search_path=.*^^' "$1" 907} 908 909 910configurePhase() { 911 runHook preConfigure 912 913 # set to empty if unset 914 : ${configureScript=} 915 : ${configureFlags=} 916 917 if [[ -z "$configureScript" && -x ./configure ]]; then 918 configureScript=./configure 919 fi 920 921 if [ -z "${dontFixLibtool:-}" ]; then 922 local i 923 find . -iname "ltmain.sh" -print0 | while IFS='' read -r -d '' i; do 924 echo "fixing libtool script $i" 925 fixLibtool "$i" 926 done 927 fi 928 929 if [[ -z "${dontAddPrefix:-}" && -n "$prefix" ]]; then 930 configureFlags="${prefixKey:---prefix=}$prefix $configureFlags" 931 fi 932 933 # Add --disable-dependency-tracking to speed up some builds. 934 if [ -z "${dontAddDisableDepTrack:-}" ]; then 935 if [ -f "$configureScript" ] && grep -q dependency-tracking "$configureScript"; then 936 configureFlags="--disable-dependency-tracking $configureFlags" 937 fi 938 fi 939 940 # By default, disable static builds. 941 if [ -z "${dontDisableStatic:-}" ]; then 942 if [ -f "$configureScript" ] && grep -q enable-static "$configureScript"; then 943 configureFlags="--disable-static $configureFlags" 944 fi 945 fi 946 947 if [ -n "$configureScript" ]; then 948 # Old bash empty array hack 949 # shellcheck disable=SC2086 950 local flagsArray=( 951 $configureFlags ${configureFlagsArray+"${configureFlagsArray[@]}"} 952 ) 953 echoCmd 'configure flags' "${flagsArray[@]}" 954 # shellcheck disable=SC2086 955 $configureScript "${flagsArray[@]}" 956 unset flagsArray 957 else 958 echo "no configure script, doing nothing" 959 fi 960 961 runHook postConfigure 962} 963 964 965buildPhase() { 966 runHook preBuild 967 968 # set to empty if unset 969 : ${makeFlags=} 970 971 if [[ -z "$makeFlags" && ! ( -n "${makefile:-}" || -e Makefile || -e makefile || -e GNUmakefile ) ]]; then 972 echo "no Makefile, doing nothing" 973 else 974 # See https://github.com/NixOS/nixpkgs/pull/1354#issuecomment-31260409 975 makeFlags="SHELL=$SHELL $makeFlags" 976 977 # Old bash empty array hack 978 # shellcheck disable=SC2086 979 local flagsArray=( 980 ${enableParallelBuilding:+-j${NIX_BUILD_CORES} -l${NIX_BUILD_CORES}} 981 $makeFlags ${makeFlagsArray+"${makeFlagsArray[@]}"} 982 $buildFlags ${buildFlagsArray+"${buildFlagsArray[@]}"} 983 ) 984 985 echoCmd 'build flags' "${flagsArray[@]}" 986 make ${makefile:+-f $makefile} "${flagsArray[@]}" 987 unset flagsArray 988 fi 989 990 runHook postBuild 991} 992 993 994checkPhase() { 995 runHook preCheck 996 997 # Old bash empty array hack 998 # shellcheck disable=SC2086 999 local flagsArray=( 1000 ${enableParallelBuilding:+-j${NIX_BUILD_CORES} -l${NIX_BUILD_CORES}} 1001 $makeFlags ${makeFlagsArray+"${makeFlagsArray[@]}"} 1002 ${checkFlags:-VERBOSE=y} ${checkFlagsArray+"${checkFlagsArray[@]}"} 1003 ${checkTarget:-check} 1004 ) 1005 1006 echoCmd 'check flags' "${flagsArray[@]}" 1007 make ${makefile:+-f $makefile} "${flagsArray[@]}" 1008 unset flagsArray 1009 1010 runHook postCheck 1011} 1012 1013 1014installPhase() { 1015 runHook preInstall 1016 1017 if [ -n "$prefix" ]; then 1018 mkdir -p "$prefix" 1019 fi 1020 1021 installTargets="${installTargets:-install}" 1022 1023 # Old bash empty array hack 1024 # shellcheck disable=SC2086 1025 local flagsArray=( 1026 $installTargets 1027 $makeFlags ${makeFlagsArray+"${makeFlagsArray[@]}"} 1028 $installFlags ${installFlagsArray+"${installFlagsArray[@]}"} 1029 ) 1030 1031 echoCmd 'install flags' "${flagsArray[@]}" 1032 make ${makefile:+-f $makefile} "${flagsArray[@]}" 1033 unset flagsArray 1034 1035 runHook postInstall 1036} 1037 1038 1039# The fixup phase performs generic, package-independent stuff, like 1040# stripping binaries, running patchelf and setting 1041# propagated-build-inputs. 1042fixupPhase() { 1043 # Make sure everything is writable so "strip" et al. work. 1044 local output 1045 for output in $outputs; do 1046 if [ -e "${!output}" ]; then chmod -R u+w "${!output}"; fi 1047 done 1048 1049 runHook preFixup 1050 1051 # Apply fixup to each output. 1052 local output 1053 for output in $outputs; do 1054 prefix="${!output}" runHook fixupOutput 1055 done 1056 1057 1058 # Propagate dependencies & setup hook into the development output. 1059 declare -ra flatVars=( 1060 # Build 1061 depsBuildBuildPropagated 1062 propagatedNativeBuildInputs 1063 depsBuildTargetPropagated 1064 # Host 1065 depsHostHostPropagated 1066 propagatedBuildInputs 1067 # Target 1068 depsTargetTargetPropagated 1069 ) 1070 declare -ra flatFiles=( 1071 "${propagatedBuildDepFiles[@]}" 1072 "${propagatedHostDepFiles[@]}" 1073 "${propagatedTargetDepFiles[@]}" 1074 ) 1075 1076 local propagatedInputsIndex 1077 for propagatedInputsIndex in "${!flatVars[@]}"; do 1078 local propagatedInputsSlice="${flatVars[$propagatedInputsIndex]}[@]" 1079 local propagatedInputsFile="${flatFiles[$propagatedInputsIndex]}" 1080 1081 [[ "${!propagatedInputsSlice}" ]] || continue 1082 1083 mkdir -p "${!outputDev}/nix-support" 1084 # shellcheck disable=SC2086 1085 printWords ${!propagatedInputsSlice} > "${!outputDev}/nix-support/$propagatedInputsFile" 1086 done 1087 1088 1089 if [ -n "${setupHook:-}" ]; then 1090 mkdir -p "${!outputDev}/nix-support" 1091 substituteAll "$setupHook" "${!outputDev}/nix-support/setup-hook" 1092 fi 1093 1094 # Propagate user-env packages into the output with binaries, TODO? 1095 1096 if [ -n "${propagatedUserEnvPkgs:-}" ]; then 1097 mkdir -p "${!outputBin}/nix-support" 1098 # shellcheck disable=SC2086 1099 printWords $propagatedUserEnvPkgs > "${!outputBin}/nix-support/propagated-user-env-packages" 1100 fi 1101 1102 runHook postFixup 1103} 1104 1105 1106installCheckPhase() { 1107 runHook preInstallCheck 1108 1109 # Old bash empty array hack 1110 # shellcheck disable=SC2086 1111 local flagsArray=( 1112 ${enableParallelBuilding:+-j${NIX_BUILD_CORES} -l${NIX_BUILD_CORES}} 1113 $makeFlags ${makeFlagsArray+"${makeFlagsArray[@]}"} 1114 $installCheckFlags ${installCheckFlagsArray+"${installCheckFlagsArray[@]}"} 1115 ${installCheckTarget:-installcheck} 1116 ) 1117 1118 echoCmd 'installcheck flags' "${flagsArray[@]}" 1119 make ${makefile:+-f $makefile} "${flagsArray[@]}" 1120 unset flagsArray 1121 1122 runHook postInstallCheck 1123} 1124 1125 1126distPhase() { 1127 runHook preDist 1128 1129 # Old bash empty array hack 1130 # shellcheck disable=SC2086 1131 local flagsArray=( 1132 $distFlags ${distFlagsArray+"${distFlagsArray[@]}"} ${distTarget:-dist} 1133 ) 1134 1135 echo 'dist flags: %q' "${flagsArray[@]}" 1136 make ${makefile:+-f $makefile} "${flagsArray[@]}" 1137 1138 if [ "${dontCopyDist:-0}" != 1 ]; then 1139 mkdir -p "$out/tarballs" 1140 1141 # Note: don't quote $tarballs, since we explicitly permit 1142 # wildcards in there. 1143 # shellcheck disable=SC2086 1144 cp -pvd ${tarballs:-*.tar.gz} "$out/tarballs" 1145 fi 1146 1147 runHook postDist 1148} 1149 1150 1151showPhaseHeader() { 1152 local phase="$1" 1153 case "$phase" in 1154 unpackPhase) header "unpacking sources";; 1155 patchPhase) header "patching sources";; 1156 configurePhase) header "configuring";; 1157 buildPhase) header "building";; 1158 checkPhase) header "running tests";; 1159 installPhase) header "installing";; 1160 fixupPhase) header "post-installation fixup";; 1161 installCheckPhase) header "running install tests";; 1162 *) header "$phase";; 1163 esac 1164} 1165 1166 1167genericBuild() { 1168 if [ -f "${buildCommandPath:-}" ]; then 1169 local oldOpts="$(shopt -po nounset)" 1170 set +u 1171 source "$buildCommandPath" 1172 eval "$oldOpts" 1173 return 1174 fi 1175 if [ -n "${buildCommand:-}" ]; then 1176 local oldOpts="$(shopt -po nounset)" 1177 set +u 1178 eval "$buildCommand" 1179 eval "$oldOpts" 1180 return 1181 fi 1182 1183 if [ -z "${phases:-}" ]; then 1184 phases="${prePhases:-} unpackPhase patchPhase ${preConfigurePhases:-} \ 1185 configurePhase ${preBuildPhases:-} buildPhase checkPhase \ 1186 ${preInstallPhases:-} installPhase ${preFixupPhases:-} fixupPhase installCheckPhase \ 1187 ${preDistPhases:-} distPhase ${postPhases:-}"; 1188 fi 1189 1190 for curPhase in $phases; do 1191 if [[ "$curPhase" = buildPhase && -n "${dontBuild:-}" ]]; then continue; fi 1192 if [[ "$curPhase" = checkPhase && -z "${doCheck:-}" ]]; then continue; fi 1193 if [[ "$curPhase" = installPhase && -n "${dontInstall:-}" ]]; then continue; fi 1194 if [[ "$curPhase" = fixupPhase && -n "${dontFixup:-}" ]]; then continue; fi 1195 if [[ "$curPhase" = installCheckPhase && -z "${doInstallCheck:-}" ]]; then continue; fi 1196 if [[ "$curPhase" = distPhase && -z "${doDist:-}" ]]; then continue; fi 1197 1198 if [[ -n $NIX_LOG_FD ]]; then 1199 echo "@nix { \"action\": \"setPhase\", \"phase\": \"$curPhase\" }" >&$NIX_LOG_FD 1200 fi 1201 1202 showPhaseHeader "$curPhase" 1203 dumpVars 1204 1205 # Evaluate the variable named $curPhase if it exists, otherwise the 1206 # function named $curPhase. 1207 local oldOpts="$(shopt -po nounset)" 1208 set +u 1209 eval "${!curPhase:-$curPhase}" 1210 eval "$oldOpts" 1211 1212 if [ "$curPhase" = unpackPhase ]; then 1213 cd "${sourceRoot:-.}" 1214 fi 1215 done 1216} 1217 1218 1219# Execute the post-hooks. 1220runHook postHook 1221 1222 1223# Execute the global user hook (defined through the Nixpkgs 1224# configuration option ‘stdenv.userHook’). This can be used to set 1225# global compiler optimisation flags, for instance. 1226runHook userHook 1227 1228 1229dumpVars 1230 1231# Disable nounset for nix-shell. 1232set +u