1# Unlinks a directory (given as the first argument), and re-creates that
2# directory as an actual directory. Then descends into the directory of
3# the same name in the origin (arg_2/arg_3) and symlinks the contents of
4# that directory into the passed end-location.
5unlinkDirReSymlinkContents() {
6 local dirToUnlink="$1"
7 local origin="$2"
8 local contentsLocation="$3"
9
10 unlink $dirToUnlink/$contentsLocation
11 mkdir -p $dirToUnlink/$contentsLocation
12 for f in $origin/$contentsLocation/*; do
13 ln -s -t "$dirToUnlink/$contentsLocation" "$f"
14 done
15}
16
17# Using unlinkDirReSymlinkContents, un-symlinks directories down to
18# $out/share/octave, and then creates the octave_packages directory.
19createOctavePackagesPath() {
20 local desiredOut=$1
21 local origin=$2
22
23 if [ -L "$out/share" ]; then
24 unlinkDirReSymlinkContents "$desiredOut" "$origin" "share"
25 fi
26
27 if [ -L "$out/share/octave" ]; then
28 unlinkDirReSymlinkContents "$desiredOut" "$origin" "share/octave"
29 fi
30
31 # Now that octave_packages has a path rather than symlinks, create the
32 # octave_packages directory for installed packages.
33 mkdir -p "$desiredOut/share/octave/octave_packages"
34}
35
36# First, descends down to $out/share/octave/site/m/startup/octaverc, and
37# copies that start-up file. Once done, it performs a `chmod` to allow
38# writing. Lastly, it `echo`s the location of the locally installed packages
39# to the startup file, allowing octave to discover installed packages.
40addPkgLocalList() {
41 local desiredOut=$1
42 local origin=$2
43 local octaveSite="share/octave/site"
44 local octaveSiteM="$octaveSite/m"
45 local octaveSiteStartup="$octaveSiteM/startup"
46 local siteOctavercStartup="$octaveSiteStartup/octaverc"
47
48 unlinkDirReSymlinkContents "$desiredOut" "$origin" "$octaveSite"
49 unlinkDirReSymlinkContents "$desiredOut" "$origin" "$octaveSiteM"
50 unlinkDirReSymlinkContents "$desiredOut" "$origin" "$octaveSiteStartup"
51
52 unlink "$out/$siteOctavercStartup"
53 cp "$origin/$siteOctavercStartup" "$desiredOut/$siteOctavercStartup"
54 chmod u+w "$desiredOut/$siteOctavercStartup"
55 echo "pkg local_list $out/.octave_packages" >> "$desiredOut/$siteOctavercStartup"
56}
57
58# Wrapper function for wrapOctaveProgramsIn. Takes one argument, a
59# space-delimited string of packages' paths that will be installed.
60wrapOctavePrograms() {
61 wrapOctaveProgramsIn "$out/bin" "$out" "$@"
62}
63
64# Wraps all octave programs in $out/bin with all the propagated inputs that
65# a particular package requires. $1 is the directory to look for binaries in
66# to wrap. $2 is the path to the octave ENVIRONMENT. $3 is the space-delimited
67# string of packages.
68wrapOctaveProgramsIn() {
69 local dir="$1"
70 local octavePath="$2"
71 local pkgs="$3"
72 local f
73
74 buildOctavePath "$octavePath" "$pkgs"
75
76 # Find all regular files in the output directory that are executable.
77 if [ -d "$dir" ]; then
78 find "$dir" -type f -perm -0100 -print0 | while read -d "" f; do
79 echo "wrapping \`$f'..."
80 local -a wrap_args=("$f"
81 --prefix PATH ':' "$program_PATH"
82 )
83 local -a wrapProgramArgs=("${wrap_args[@]}")
84 wrapProgram "${wrapProgramArgs[@]}"
85 done
86 fi
87}
88
89# Build the PATH environment variable by walking through the closure of
90# dependencies. Starts by constructing the `program_PATH` variable with the
91# environment's path, then adding the original octave's location, and marking
92# them in `octavePathsSeen`.
93buildOctavePath() {
94 local octavePath="$1"
95 local packages="$2"
96
97 local pathsToSearch="$octavePath $packages"
98
99 # Create an empty table of Octave paths.
100 declare -A octavePathsSeen=()
101 program_PATH=
102 octavePathsSeen["$out"]=1
103 octavePathsSeen["@octave@"]=1
104 addToSearchPath program_PATH "$out/bin"
105 addToSearchPath program_PATH "@octave@/bin"
106 echo "program_PATH to change to is: $program_PATH"
107 for path in $pathsToSearch; do
108 echo "Recurse to propagated-build-input: $path"
109 _addToOctavePath $path
110 done
111}
112
113# Adds the bin directories to the program_PATH variable.
114# Recurses on any paths declared in `propagated-build-inputs`, while avoiding
115# duplicating paths by flagging the directires it has seen in `octavePathsSeen`.
116_addToOctavePath() {
117 local dir="$1"
118 # Stop if we've already visited this path.
119 if [ -n "${octavePathsSeen[$dir]}" ]; then return; fi
120 octavePathsSeen[$dir]=1
121 # addToSearchPath is defined in stdenv/generic/setup.sh. It has the effect
122 # of calling `export X=$dir/...:$X`.
123 addToSearchPath program_PATH $dir/bin
124
125 # Inspect the propagated inputs (if they exist) and recur on them.
126 local prop="$dir/nix-support/propagated-build-inputs"
127 if [ -e $prop ]; then
128 for new_path in $(cat $prop); do
129 _addToOctavePath $new_path
130 done
131 fi
132}