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