at master 85 lines 3.2 kB view raw
1_handleCmdOutput(){ 2 local command=("$1" "$2") 3 local versionOutput 4 5 local envArgs=() 6 if [[ "$3" != "*" ]]; then 7 envArgs+=("--ignore-environment") 8 for var in $3; do 9 envArgs+=("$var=${!var}") 10 done 11 fi 12 13 versionOutput="$(env \ 14 --chdir=/ \ 15 --argv0="$(basename "${command[0]}")" \ 16 "${envArgs[@]}" \ 17 "${command[@]}" 2>&1 \ 18 | sed -e 's|@storeDir@/[^/ ]*/|{{storeDir}}/|g' \ 19 || true)" 20 if [[ "$versionOutput" =~ "$version" ]]; then 21 echoPrefix="Successfully managed to" 22 else 23 echoPrefix="Did not" 24 fi 25 # The return value of this function is this variable: 26 echo "$echoPrefix" 27 # And in anycase we want these to be printed in the build log, useful for 28 # debugging, so we print these to stderr. 29 echo "$echoPrefix" find version "$version" in the output of the command \ 30 "${command[@]}" >&2 31 echo "$versionOutput" >&2 32} 33versionCheckHook(){ 34 runHook preVersionCheck 35 echo Executing versionCheckPhase 36 37 # Don't keep any environment variables by default 38 : "${versionCheckKeepEnvironment:=}" 39 40 local cmdProgram cmdArg echoPrefix 41 if [[ ! -z "${versionCheckProgram-}" ]]; then 42 cmdProgram="$versionCheckProgram" 43 elif [[ ! -z "${NIX_MAIN_PROGRAM-}" ]]; then 44 cmdProgram="${!outputBin}/bin/${NIX_MAIN_PROGRAM}" 45 elif [[ ! -z "${pname-}" ]]; then 46 echo "versionCheckHook: Package \`${pname}\` does not have the \`meta.mainProgram\` attribute." \ 47 "We'll assume that the main program has the same name for now, but this behavior is deprecated," \ 48 "because it leads to surprising errors when the assumption does not hold." \ 49 "If the package has a main program, please set \`meta.mainProgram\` in its definition to make this warning go away." \ 50 "Should the binary that outputs the intended version differ from \`meta.mainProgram\`, consider setting \`versionCheckProgram\` instead." >&2 51 cmdProgram="${!outputBin}/bin/${pname}" 52 else 53 echo "versionCheckHook: \$NIX_MAIN_PROGRAM, \$versionCheckProgram and \$pname are all empty, so" \ 54 "we don't know how to run the versionCheckPhase." \ 55 "To fix this, set one of \`meta.mainProgram\` or \`versionCheckProgram\`." >&2 56 exit 2 57 fi 58 59 if [[ ! -x "$cmdProgram" ]]; then 60 echo "versionCheckHook: $cmdProgram was not found, or is not an executable" >&2 61 exit 2 62 fi 63 if [[ -z "${versionCheckProgramArg}" ]]; then 64 for cmdArg in "--help" "--version"; do 65 echoPrefix="$(_handleCmdOutput "$cmdProgram" "$cmdArg" "$versionCheckKeepEnvironment")" 66 if [[ "$echoPrefix" == "Successfully managed to" ]]; then 67 break 68 fi 69 done 70 else 71 cmdArg="$versionCheckProgramArg" 72 echoPrefix="$(_handleCmdOutput "$cmdProgram" "$cmdArg" "$versionCheckKeepEnvironment")" 73 fi 74 if [[ "$echoPrefix" == "Did not" ]]; then 75 exit 2 76 fi 77 78 runHook postVersionCheck 79 echo Finished versionCheckPhase 80} 81 82if [[ -z "${dontVersionCheck-}" ]]; then 83 echo "Using versionCheckHook" 84 preInstallCheckHooks+=(versionCheckHook) 85fi