about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/interpreters/tcl/tcl-package-hook.sh
blob: 747783cb1c27daaf0c62ca0193e7d0327e16821d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# This hook ensures that we do the following in post-fixup:
# * wrap any installed executables with a wrapper that configures TCLLIBPATH
# * write a setup hook that extends the TCLLIBPATH of any anti-dependencies

# Add a directory to TCLLIBPATH, provided that it exists
_addToTclLibPath() {
    local tclPkg="$1"
    if [[ -z "$tclPkg" ]]; then
        return
    fi

    if [[ ! -d "$tclPkg" ]]; then
        >&2 echo "can't add $tclPkg to TCLLIBPATH; that directory doesn't exist"
        exit 1
    fi

    if [[ "$tclPkg" == *" "* ]]; then
        tclPkg="{$tclPkg}"
    fi

    if [[ -z "${TCLLIBPATH-}" ]]; then
        export TCLLIBPATH="$tclPkg"
    else
        if [[ "$TCLLIBPATH" != *"$tclPkg "* && "$TCLLIBPATH" != *"$tclPkg" ]]; then
            export TCLLIBPATH="${TCLLIBPATH} $tclPkg"
        fi
    fi
}

# Locate any directory containing an installed pkgIndex file
findInstalledTclPkgs() {
    local -r newLibDir="${!outputLib}/lib"
    if [[ ! -d "$newLibDir" ]]; then
        >&2 echo "Assuming no loadable tcl packages installed ($newLibDir does not exist)"
        return
    fi
    echo "$(find "$newLibDir" -name pkgIndex.tcl -exec dirname {} \;)"
}

# Wrap any freshly-installed binaries and set up their TCLLIBPATH
wrapTclBins() {
    if [[ -z "${TCLLIBPATH-}" ]]; then
        echo "skipping automatic Tcl binary wrapping (nothing to do)"
        return
    fi

    local -r tclBinsDir="${!outputBin}/bin"
    if [[ ! -d "$tclBinsDir" ]]; then
        echo "No outputBin found, not using any TCLLIBPATH wrapper"
        return
    fi

    find "$tclBinsDir" -type f -executable -print |
        while read -r someBin; do
            echo "Adding TCLLIBPATH wrapper for $someBin"
            wrapProgram "$someBin" --prefix TCLLIBPATH ' ' "$TCLLIBPATH"
        done
}

# Generate hook to adjust TCLLIBPATH in anti-dependencies
writeTclLibPathHook() {
    local -r hookPath="${!outputLib}/nix-support/setup-hook"
    mkdir -p "$(dirname "$hookPath")"

    typeset -f _addToTclLibPath >> "$hookPath"
    local -r tclPkgs=$(findInstalledTclPkgs)
    while IFS= read -r tclPkg; do
        echo "_addToTclLibPath \"$tclPkg\"" >> "$hookPath"
        _addToTclLibPath "$tclPkg" true
    done <<< "$tclPkgs"
}

postFixupHooks+=(writeTclLibPathHook)
postFixupHooks+=(wrapTclBins)