summaryrefslogtreecommitdiffstats
path: root/pkgs/stdenv/generic/setup.sh
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/stdenv/generic/setup.sh')
-rw-r--r--pkgs/stdenv/generic/setup.sh210
1 files changed, 145 insertions, 65 deletions
diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh
index d7a4781448ae..03fa589138d5 100644
--- a/pkgs/stdenv/generic/setup.sh
+++ b/pkgs/stdenv/generic/setup.sh
@@ -211,7 +211,7 @@ isELF() {
exec {fd}< "$fn"
read -r -n 4 -u "$fd" magic
exec {fd}<&-
- if [[ "$magic" =~ ELF ]]; then return 0; else return 1; fi
+ if [ "$magic" = $'\177ELF' ]; then return 0; else return 1; fi
}
# Return success if the specified file is a script (i.e. starts with
@@ -220,7 +220,6 @@ isScript() {
local fn="$1"
local fd
local magic
- if ! [ -x /bin/sh ]; then return 0; fi
exec {fd}< "$fn"
read -r -n 2 -u "$fd" magic
exec {fd}<&-
@@ -258,9 +257,17 @@ shopt -s nullglob
# Set up the initial path.
PATH=
+HOST_PATH=
for i in $initialPath; do
if [ "$i" = / ]; then i=; fi
addToSearchPath PATH "$i/bin"
+
+ # For backward compatibility, we add initial path to HOST_PATH so
+ # it can be used in auto patch-shebangs. Unfortunately this will
+ # not work with cross compilation.
+ if [ -z "${strictDeps-}" ]; then
+ addToSearchPath HOST_PATH "$i/bin"
+ fi
done
if (( "${NIX_DEBUG:-0}" >= 1 )); then
@@ -274,11 +281,6 @@ BASH="$SHELL"
export CONFIG_SHELL="$SHELL"
-# Dummy implementation of the paxmark function. On Linux, this is
-# overwritten by paxctl's setup hook.
-paxmark() { true; }
-
-
# Execute the pre-hook.
if [ -z "${shell:-}" ]; then export shell="$SHELL"; fi
runHook preHook
@@ -501,10 +503,14 @@ activatePackage() {
# the transition, we do include everything in thatcase.
#
# TODO(@Ericson2314): Don't special-case native compilation
- if [[ ( -z "${crossConfig-}" || "$hostOffset" -le -1 ) && -d "$pkg/bin" ]]; then
+ if [[ ( -z "${strictDeps-}" || "$hostOffset" -le -1 ) && -d "$pkg/bin" ]]; then
addToSearchPath _PATH "$pkg/bin"
fi
+ if [[ "$hostOffset" -eq 0 && -d "$pkg/bin" ]]; then
+ addToSearchPath HOST_PATH "$pkg/bin"
+ fi
+
if [[ -f "$pkg/nix-support/setup-hook" ]]; then
local oldOpts="$(shopt -po nounset)"
set +u
@@ -551,7 +557,7 @@ _addToEnv() {
for depTargetOffset in "${allPlatOffsets[@]}"; do
(( "$depHostOffset" <= "$depTargetOffset" )) || continue
local hookRef="${hookVar}[$depTargetOffset - $depHostOffset]"
- if [[ -z "${crossConfig-}" ]]; then
+ if [[ -z "${strictDeps-}" ]]; then
# Apply environment hooks to all packages during native
# compilation to ease the transition.
#
@@ -641,29 +647,25 @@ fi
# Textual substitution functions.
-substitute() {
- local input="$1"
- local output="$2"
+substituteStream() {
+ local var=$1
+ local description=$2
shift 2
- if [ ! -f "$input" ]; then
- echo "substitute(): ERROR: file '$input' does not exist" >&2
- return 1
- fi
-
- local content
- # read returns non-0 on EOF, so we want read to fail
- if IFS='' read -r -N 0 content < "$input"; then
- echo "substitute(): ERROR: File \"$input\" has null bytes, won't process" >&2
- return 1
- fi
-
while (( "$#" )); do
case "$1" in
--replace)
pattern="$2"
replacement="$3"
shift 3
+ local savedvar
+ savedvar="${!var}"
+ eval "$var"'=${'"$var"'//"$pattern"/"$replacement"}'
+ if [ "$pattern" != "$replacement" ]; then
+ if [ "${!var}" == "$savedvar" ]; then
+ echo "substituteStream(): WARNING: pattern '$pattern' doesn't match anything in $description" >&2
+ fi
+ fi
;;
--subst-var)
@@ -671,32 +673,59 @@ substitute() {
shift 2
# check if the used nix attribute name is a valid bash name
if ! [[ "$varName" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
- echo "substitute(): ERROR: substitution variables must be valid Bash names, \"$varName\" isn't." >&2
+ echo "substituteStream(): ERROR: substitution variables must be valid Bash names, \"$varName\" isn't." >&2
+ return 1
+ fi
+ if [ -z ${!varName+x} ]; then
+ echo "substituteStream(): ERROR: variable \$$varName is unset" >&2
return 1
fi
pattern="@$varName@"
replacement="${!varName}"
+ eval "$var"'=${'"$var"'//"$pattern"/"$replacement"}'
;;
--subst-var-by)
pattern="@$2@"
replacement="$3"
+ eval "$var"'=${'"$var"'//"$pattern"/"$replacement"}'
shift 3
;;
*)
- echo "substitute(): ERROR: Invalid command line argument: $1" >&2
+ echo "substituteStream(): ERROR: Invalid command line argument: $1" >&2
return 1
;;
esac
-
- content="${content//"$pattern"/$replacement}"
done
- if [ -e "$output" ]; then chmod +w "$output"; fi
- printf "%s" "$content" > "$output"
+ printf "%s" "${!var}"
+}
+
+consumeEntire() {
+ # read returns non-0 on EOF, so we want read to fail
+ if IFS='' read -r -N 0 $1; then
+ echo "consumeEntire(): ERROR: Input null bytes, won't process" >&2
+ return 1
+ fi
}
+substitute() {
+ local input="$1"
+ local output="$2"
+ shift 2
+
+ if [ ! -f "$input" ]; then
+ echo "substitute(): ERROR: file '$input' does not exist" >&2
+ return 1
+ fi
+
+ local content
+ consumeEntire content < "$input"
+
+ if [ -e "$output" ]; then chmod +w "$output"; fi
+ substituteStream content "file '$input'" "$@" > "$output"
+}
substituteInPlace() {
local fileName="$1"
@@ -704,20 +733,30 @@ substituteInPlace() {
substitute "$fileName" "$fileName" "$@"
}
+_allFlags() {
+ for varName in $(awk 'BEGIN { for (v in ENVIRON) if (v ~ /^[a-z][a-zA-Z0-9_]*$/) print v }'); do
+ if (( "${NIX_DEBUG:-0}" >= 1 )); then
+ printf "@%s@ -> %q\n" "${varName}" "${!varName}"
+ fi
+ args+=("--subst-var" "$varName")
+ done
+}
+
+substituteAllStream() {
+ local -a args=()
+ _allFlags
+
+ substituteStream "$1" "$2" "${args[@]}"
+}
# Substitute all environment variables that start with a lowercase character and
# are valid Bash names.
substituteAll() {
local input="$1"
local output="$2"
- local -a args=()
- for varName in $(awk 'BEGIN { for (v in ENVIRON) if (v ~ /^[a-z][a-zA-Z0-9_]*$/) print v }'); do
- if (( "${NIX_DEBUG:-0}" >= 1 )); then
- printf "@%s@ -> %q\n" "${varName}" "${!varName}"
- fi
- args+=("--subst-var" "$varName")
- done
+ local -a args=()
+ _allFlags
substitute "$input" "$output" "${args[@]}"
}
@@ -774,11 +813,11 @@ _defaultUnpack() {
else
case "$fn" in
- *.tar.xz | *.tar.lzma)
+ *.tar.xz | *.tar.lzma | *.txz)
# Don't rely on tar knowing about .xz.
xz -d < "$fn" | tar xf -
;;
- *.tar | *.tar.* | *.tgz | *.tbz2)
+ *.tar | *.tar.* | *.tgz | *.tbz2 | *.tbz)
# GNU tar can automatically select the decompression method
# (info "(tar) gzip").
tar xf "$fn"
@@ -968,9 +1007,11 @@ buildPhase() {
# set to empty if unset
: ${makeFlags=}
- if [[ -z "$makeFlags" && ! ( -n "${makefile:-}" || -e Makefile || -e makefile || -e GNUmakefile ) ]]; then
+ if [[ -z "$makeFlags" && -z "${makefile:-}" && ! ( -e Makefile || -e makefile || -e GNUmakefile ) ]]; then
echo "no Makefile, doing nothing"
else
+ foundMakefile=1
+
# See https://github.com/NixOS/nixpkgs/pull/1354#issuecomment-31260409
makeFlags="SHELL=$SHELL $makeFlags"
@@ -994,18 +1035,38 @@ buildPhase() {
checkPhase() {
runHook preCheck
- # Old bash empty array hack
- # shellcheck disable=SC2086
- local flagsArray=(
- ${enableParallelBuilding:+-j${NIX_BUILD_CORES} -l${NIX_BUILD_CORES}}
- $makeFlags ${makeFlagsArray+"${makeFlagsArray[@]}"}
- ${checkFlags:-VERBOSE=y} ${checkFlagsArray+"${checkFlagsArray[@]}"}
- ${checkTarget:-check}
- )
+ if [[ -z "${foundMakefile:-}" ]]; then
+ echo "no Makefile or custom buildPhase, doing nothing"
+ runHook postCheck
+ return
+ fi
- echoCmd 'check flags' "${flagsArray[@]}"
- make ${makefile:+-f $makefile} "${flagsArray[@]}"
- unset flagsArray
+ if [[ -z "${checkTarget:-}" ]]; then
+ #TODO(@oxij): should flagsArray influence make -n?
+ if make -n ${makefile:+-f $makefile} check >/dev/null 2>&1; then
+ checkTarget=check
+ elif make -n ${makefile:+-f $makefile} test >/dev/null 2>&1; then
+ checkTarget=test
+ fi
+ fi
+
+ if [[ -z "${checkTarget:-}" ]]; then
+ echo "no check/test target in ${makefile:-Makefile}, doing nothing"
+ else
+ # Old bash empty array hack
+ # shellcheck disable=SC2086
+ local flagsArray=(
+ ${enableParallelChecking:+-j${NIX_BUILD_CORES} -l${NIX_BUILD_CORES}}
+ $makeFlags ${makeFlagsArray+"${makeFlagsArray[@]}"}
+ ${checkFlags:-VERBOSE=y} ${checkFlagsArray+"${checkFlagsArray[@]}"}
+ ${checkTarget}
+ )
+
+ echoCmd 'check flags' "${flagsArray[@]}"
+ make ${makefile:+-f $makefile} "${flagsArray[@]}"
+
+ unset flagsArray
+ fi
runHook postCheck
}
@@ -1018,14 +1079,12 @@ installPhase() {
mkdir -p "$prefix"
fi
- installTargets="${installTargets:-install}"
-
# Old bash empty array hack
# shellcheck disable=SC2086
local flagsArray=(
- $installTargets
$makeFlags ${makeFlagsArray+"${makeFlagsArray[@]}"}
$installFlags ${installFlagsArray+"${installFlagsArray[@]}"}
+ ${installTargets:-install}
)
echoCmd 'install flags' "${flagsArray[@]}"
@@ -1091,6 +1150,19 @@ fixupPhase() {
substituteAll "$setupHook" "${!outputDev}/nix-support/setup-hook"
fi
+ # TODO(@Ericson2314): Remove after https://github.com/NixOS/nixpkgs/pull/31414
+ if [ -n "${setupHooks:-}" ]; then
+ mkdir -p "${!outputDev}/nix-support"
+ local hook
+ for hook in $setupHooks; do
+ local content
+ consumeEntire content < "$hook"
+ substituteAllStream content "file '$hook'" >> "${!outputDev}/nix-support/setup-hook"
+ unset -v content
+ done
+ unset -v hook
+ fi
+
# Propagate user-env packages into the output with binaries, TODO?
if [ -n "${propagatedUserEnvPkgs:-}" ]; then
@@ -1106,18 +1178,26 @@ fixupPhase() {
installCheckPhase() {
runHook preInstallCheck
- # Old bash empty array hack
- # shellcheck disable=SC2086
- local flagsArray=(
- ${enableParallelBuilding:+-j${NIX_BUILD_CORES} -l${NIX_BUILD_CORES}}
- $makeFlags ${makeFlagsArray+"${makeFlagsArray[@]}"}
- $installCheckFlags ${installCheckFlagsArray+"${installCheckFlagsArray[@]}"}
- ${installCheckTarget:-installcheck}
- )
+ if [[ -z "${foundMakefile:-}" ]]; then
+ echo "no Makefile or custom buildPhase, doing nothing"
+ #TODO(@oxij): should flagsArray influence make -n?
+ elif [[ -z "${installCheckTarget:-}" ]] \
+ && ! make -n ${makefile:+-f $makefile} ${installCheckTarget:-installcheck} >/dev/null 2>&1; then
+ echo "no installcheck target in ${makefile:-Makefile}, doing nothing"
+ else
+ # Old bash empty array hack
+ # shellcheck disable=SC2086
+ local flagsArray=(
+ ${enableParallelChecking:+-j${NIX_BUILD_CORES} -l${NIX_BUILD_CORES}}
+ $makeFlags ${makeFlagsArray+"${makeFlagsArray[@]}"}
+ $installCheckFlags ${installCheckFlagsArray+"${installCheckFlagsArray[@]}"}
+ ${installCheckTarget:-installcheck}
+ )
- echoCmd 'installcheck flags' "${flagsArray[@]}"
- make ${makefile:+-f $makefile} "${flagsArray[@]}"
- unset flagsArray
+ echoCmd 'installcheck flags' "${flagsArray[@]}"
+ make ${makefile:+-f $makefile} "${flagsArray[@]}"
+ unset flagsArray
+ fi
runHook postInstallCheck
}