1# Inspired by python/wrapper.nix
2# Wrapper around wrapLuaProgramsIn, below. The $luaPath
3# variable is passed in from the buildLuarocksPackage function.
4set -e
5
6wrapLuaPrograms() {
7 wrapLuaProgramsIn "$out/bin" "$out $luaPath"
8}
9
10# Builds environment variables like LUA_PATH and PATH walking through closure
11# of dependencies.
12buildLuaPath() {
13 local luaPath="$1"
14 local path
15
16 # Create an empty table of paths (see doc on loadFromPropagatedInputs
17 # for how this is used). Build up the program_PATH and program_LUA_PATH
18 # variables.
19 declare -A luaPathsSeen=()
20 program_PATH=
21 luaPathsSeen["@lua@"]=1
22 addToSearchPath program_PATH @lua@/bin
23 for path in $luaPath; do
24 addToLuaPath "$path"
25 done
26}
27
28
29# with an executable shell script which will set some environment variables
30# and then call into the original binary (which has been given a .wrapped suffix).
31# luaPath is a list of directories
32wrapLuaProgramsIn() {
33 local dir="$1"
34 local luaPath="$2"
35 local f
36
37 buildLuaPath "$luaPath"
38
39 if [ ! -d "$dir" ]; then
40 nix_debug "$dir not a directory"
41 return
42 fi
43
44 nix_debug "wrapping programs in [$dir]"
45
46 # Find all regular files in the output directory that are executable.
47 find "$dir" -type f -perm -0100 -print0 | while read -d "" f; do
48 # Rewrite "#! .../env lua" to "#! /nix/store/.../lua".
49 # Strip suffix, like "3" or "2.7m" -- we don't have any choice on which
50 # Lua to use besides one with this hook anyway.
51 if head -n1 "$f" | grep -q '#!.*/env.*\(lua\)'; then
52 sed -i "$f" -e "1 s^.*/env[ ]*\(lua\)[^ ]*^#! @executable@^"
53 fi
54
55 # wrapProgram creates the executable shell script described
56 # above. The script will set LUA_(C)PATH and PATH variables!
57 # (see pkgs/build-support/setup-hooks/make-wrapper.sh)
58 local -a wrap_args=("$f"
59 --prefix PATH ':' "$program_PATH"
60 --prefix LUA_PATH ';' "$NIX_LUA_PATH"
61 --prefix LUA_CPATH ';' "$NIX_LUA_CPATH"
62 )
63
64 # Add any additional arguments provided by makeWrapperArgs
65 # argument to buildLuaPackage.
66 # makeWrapperArgs
67 local -a user_args="($makeWrapperArgs)"
68 local -a wrapProgramArgs=("${wrap_args[@]}" "${user_args[@]}")
69
70 # see setup-hooks/make-wrapper.sh
71 wrapProgram "${wrapProgramArgs[@]}"
72
73 done
74}
75
76# Adds the lib and bin directories to the LUA_PATH and PATH variables,
77# respectively. Recurses on any paths declared in
78# `propagated-native-build-inputs`, while avoiding duplicating paths by
79# flagging the directories it has visited in `luaPathsSeen`.
80loadFromPropagatedInputs() {
81 local dir="$1"
82 # Stop if we've already visited here.
83 if [ -n "${luaPathsSeen[$dir]}" ]; then
84 return;
85 fi
86 luaPathsSeen[$dir]=1
87
88 addToLuaPath "$dir"
89 addToSearchPath program_PATH $dir/bin
90
91 # Inspect the propagated inputs (if they exist) and recur on them.
92 local prop="$dir/nix-support/propagated-native-build-inputs"
93 if [ -e "$prop" ]; then
94 local new_path
95 for new_path in $(cat $prop); do
96 loadFromPropagatedInputs "$new_path"
97 done
98 fi
99}