1# This hook ensures that we do the following in post-fixup:
2# * wrap any installed executables with a wrapper that configures TCLLIBPATH
3# * write a setup hook that extends the TCLLIBPATH of any anti-dependencies
4
5tclWrapperArgs=( ${tclWrapperArgs-} )
6
7# Add a directory to TCLLIBPATH, provided that it exists
8_addToTclLibPath() {
9 local tclPkg="$1"
10 if [[ -z "$tclPkg" ]]; then
11 return
12 fi
13
14 if [[ ! -d "$tclPkg" ]]; then
15 >&2 echo "can't add $tclPkg to TCLLIBPATH; that directory doesn't exist"
16 exit 1
17 fi
18
19 if [[ "$tclPkg" == *" "* ]]; then
20 tclPkg="{$tclPkg}"
21 fi
22
23 if [[ -z "${TCLLIBPATH-}" ]]; then
24 export TCLLIBPATH="$tclPkg"
25 else
26 if [[ "$TCLLIBPATH " != *"$tclPkg "* ]]; then
27 export TCLLIBPATH="${TCLLIBPATH} $tclPkg"
28 fi
29 fi
30}
31
32# Locate any directory containing an installed pkgIndex file
33findInstalledTclPkgs() {
34 local -r newLibDir="${!outputLib}/lib"
35 if [[ ! -d "$newLibDir" ]]; then
36 >&2 echo "Assuming no loadable tcl packages installed ($newLibDir does not exist)"
37 return
38 fi
39 echo "$(find "$newLibDir" -name pkgIndex.tcl -exec dirname {} \;)"
40}
41
42# Wrap any freshly-installed binaries and set up their TCLLIBPATH
43wrapTclBins() {
44 if [ "$dontWrapTclBinaries" ]; then return; fi
45
46 if [[ -z "${TCLLIBPATH-}" ]]; then
47 echo "skipping automatic Tcl binary wrapping (nothing to do)"
48 return
49 fi
50
51 local -r tclBinsDir="${!outputBin}/bin"
52 if [[ ! -d "$tclBinsDir" ]]; then
53 echo "No outputBin found, not using any TCLLIBPATH wrapper"
54 return
55 fi
56
57 tclWrapperArgs+=(--prefix TCLLIBPATH ' ' "$TCLLIBPATH")
58
59 find "$tclBinsDir" -type f -executable -print |
60 while read -r someBin; do
61 echo "Adding TCLLIBPATH wrapper for $someBin"
62 wrapProgram "$someBin" "${tclWrapperArgs[@]}"
63 done
64}
65
66# Generate hook to adjust TCLLIBPATH in anti-dependencies
67writeTclLibPathHook() {
68 local -r hookPath="${!outputLib}/nix-support/setup-hook"
69 mkdir -p "$(dirname "$hookPath")"
70
71 typeset -f _addToTclLibPath >> "$hookPath"
72 local -r tclPkgs=$(findInstalledTclPkgs)
73 while IFS= read -r tclPkg; do
74 echo "_addToTclLibPath \"$tclPkg\"" >> "$hookPath"
75 _addToTclLibPath "$tclPkg" true
76 done <<< "$tclPkgs"
77}
78
79postFixupHooks+=(writeTclLibPathHook)
80postFixupHooks+=(wrapTclBins)