Skip to content

Commit

Permalink
setup.sh: arithmetic fixes
Browse files Browse the repository at this point in the history
this one is a little more controversial
see oilshell/oil#864
for more information
  • Loading branch information
happysalada committed Jun 22, 2021
1 parent 707984e commit 59ec2c7
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions pkgs/stdenv/generic/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -342,22 +342,22 @@ declare -a allPlatOffsets=(-1 0 1)
# implements.
findInputs() {
local -r pkg="$1"
local -ri hostOffset="$2"
local -ri targetOffset="$3"
local -r hostOffset="$2"
local -r targetOffset="$3"

# Sanity check
(( "$hostOffset" <= "$targetOffset" )) || exit -1
(( "$hostOffset" <= "$targetOffset" )) || exit 1

local varVar="${pkgAccumVarVars[$hostOffset + 1]}"
local varRef="$varVar[\$targetOffset - \$hostOffset]"
local varVar="${pkgAccumVarVars[$(( hostOffset + 1 ))]}"
local varRef="${varVar}[$(( targetOffset - hostOffset ))]"
local var="${!varRef}"
unset -v varVar varRef

# TODO(@Ericson2314): Restore using associative array once Darwin
# nix-shell doesn't use impure bash. This should replace the O(n)
# case with an O(1) hash map lookup, assuming bash is implemented
# well :D.
local varSlice="$var[*]"
local varSlice="${var}[*]"
# ${..-} to hack around old bash empty array problem
case "${!varSlice-}" in
*" $pkg "*) return 0 ;;
Expand All @@ -374,47 +374,47 @@ findInputs() {
# The current package's host and target offset together
# provide a <=-preserving homomorphism from the relative
# offsets to current offset
local -i mapOffsetResult
local mapOffsetResult
function mapOffset() {
local -ri inputOffset="$1"
local -r inputOffset="$1"
if (( "$inputOffset" <= 0 )); then
local -ri outputOffset="$inputOffset + $hostOffset"
local -r outputOffset=$(( inputOffset + hostOffset ))
else
local -ri outputOffset="$inputOffset - 1 + $targetOffset"
local -r outputOffset=$(( inputOffset - 1 + targetOffset ))
fi
mapOffsetResult="$outputOffset"
}

# Host offset relative to that of the package whose immediate
# dependencies we are currently exploring.
local -i relHostOffset
local relHostOffset
for relHostOffset in "${allPlatOffsets[@]}"; do
# `+ 1` so we start at 0 for valid index
local files="${propagatedDepFilesVars[$relHostOffset + 1]}"
local files="${propagatedDepFilesVars[$(( relHostOffset + 1 ))]}"

# Host offset relative to the package currently being
# built---as absolute an offset as will be used.
mapOffset relHostOffset
local -i hostOffsetNext="$mapOffsetResult"
local hostOffsetNext="$mapOffsetResult"

# Ensure we're in bounds relative to the package currently
# being built.
[[ "${allPlatOffsets[*]}" = *"$hostOffsetNext"* ]] || continue

# Target offset relative to the *host* offset of the package
# whose immediate dependencies we are currently exploring.
local -i relTargetOffset
local relTargetOffset
for relTargetOffset in "${allPlatOffsets[@]}"; do
(( "$relHostOffset" <= "$relTargetOffset" )) || continue

local fileRef="${files}[$relTargetOffset - $relHostOffset]"
local fileRef="${files}[$(( relTargetOffset - relHostOffset ))]"
local file="${!fileRef}"
unset -v fileRef

# Target offset relative to the package currently being
# built.
mapOffset relTargetOffset
local -i targetOffsetNext="$mapOffsetResult"
local targetOffsetNext="$mapOffsetResult"

# Once again, ensure we're in bounds relative to the
# package currently being built.
Expand Down Expand Up @@ -468,11 +468,11 @@ done
# Add package to the future PATH and run setup hooks
activatePackage() {
local pkg="$1"
local -ri hostOffset="$2"
local -ri targetOffset="$3"
local -r hostOffset="$2"
local -r targetOffset="$3"

# Sanity check
(( "$hostOffset" <= "$targetOffset" )) || exit -1
(( "$hostOffset" <= "$targetOffset" )) || exit 1

if [ -f "$pkg" ]; then
source "$pkg"
Expand Down Expand Up @@ -503,14 +503,14 @@ activatePackage() {
}

_activatePkgs() {
local -i hostOffset targetOffset
local hostOffset targetOffset
local pkg

for hostOffset in "${allPlatOffsets[@]}"; do
local pkgsVar="${pkgAccumVarVars[$hostOffset + 1]}"
local pkgsVar="${pkgAccumVarVars[$(( hostOffset + 1 ))]}"
for targetOffset in "${allPlatOffsets[@]}"; do
(( "$hostOffset" <= "$targetOffset" )) || continue
local pkgsRef="${pkgsVar}[$targetOffset - $hostOffset]"
local pkgsRef="${pkgsVar}[$(( targetOffset - hostOffset ))]"
local pkgsSlice="${!pkgsRef}[@]"
for pkg in ${!pkgsSlice+"${!pkgsSlice}"}; do
activatePackage "$pkg" "$hostOffset" "$targetOffset"
Expand All @@ -531,15 +531,15 @@ _activatePkgs
# with this information to the relevant env hook array, but bash
# doesn't have closures, so it's easier to just pass this in.
_addToEnv() {
local -i depHostOffset depTargetOffset
local depHostOffset depTargetOffset
local pkg

for depHostOffset in "${allPlatOffsets[@]}"; do
local hookVar="${pkgHookVarVars[$depHostOffset + 1]}"
local pkgsVar="${pkgAccumVarVars[$depHostOffset + 1]}"
local hookVar="${pkgHookVarVars[$(( depHostOffset + 1 ))]}"
local pkgsVar="${pkgAccumVarVars[$(( depHostOffset + 1 ))]}"
for depTargetOffset in "${allPlatOffsets[@]}"; do
(( "$depHostOffset" <= "$depTargetOffset" )) || continue
local hookRef="${hookVar}[$depTargetOffset - $depHostOffset]"
local hookRef="${hookVar}[$(( depTargetOffset - depHostOffset ))]"
if [[ -z "${strictDeps-}" ]]; then

# Keep track of which packages we have visited before.
Expand All @@ -564,7 +564,7 @@ _addToEnv() {
visitedPkgs+=" $pkg"
done
else
local pkgsRef="${pkgsVar}[$depTargetOffset - $depHostOffset]"
local pkgsRef="${pkgsVar}[$(( depTargetOffset - depHostOffset ))]"
local pkgsSlice="${!pkgsRef}[@]"
for pkg in ${!pkgsSlice+"${!pkgsSlice}"}; do
runHook "${!hookRef}" "$pkg"
Expand Down

0 comments on commit 59ec2c7

Please sign in to comment.