btrbk: add mainProgram (#356350)
[NixPkgs.git] / pkgs / development / interpreters / lua-5 / utils.sh
blob2365af08dc9cbe17cae8ed79325d8d25235973dc
1 #!/bin/bash
3 declare -gA luaPathsSeen=()
5 # shellcheck disable=SC2164,SC2041
6 nix_print() {
7 if [ ${NIX_DEBUG:-0} -ge $1 ]; then
8 echo "$2"
9 fi
12 nix_debug() {
13 nix_print 3 "$1"
16 addToLuaSearchPathWithCustomDelimiter() {
17 local varName="$1"
18 local absPattern="$2"
20 # export only if we haven't already got this dir in the search path
21 if [[ ${!varName-} == *"$absPattern"* ]]; then return; fi
23 # if the path variable has not yet been set, initialize it to ";;"
24 # this is a magic value that will be replaced by the default,
25 # allowing relative modules to be used even when there are system modules.
26 if [[ ! -v "${varName}" ]]; then export "${varName}=;;"; fi
28 # export only if the folder contains lua files
29 shopt -s globstar
31 local adjustedPattern="${absPattern/\?/\*\*\/\*}"
32 for _file in $adjustedPattern; do
33 export "${varName}=${!varName:+${!varName};}${absPattern}"
34 shopt -u globstar
35 return;
36 done
37 shopt -u globstar
40 # used in setup Hooks to load LUA_PATH and LUA_CPATH
41 # luaEnvHook
42 luaEnvHook() {
43 _addToLuaPath "$1"
46 addToLuaPath() {
47 local dir="$1"
49 if [ ! -d "$dir" ]; then
50 nix_debug "$dir not a directory abort"
51 return 0
53 cd "$dir"
54 for pattern in @luapathsearchpaths@; do
55 addToLuaSearchPathWithCustomDelimiter LUA_PATH "$PWD/$pattern"
56 done
58 # LUA_CPATH
59 for pattern in @luacpathsearchpaths@; do
60 addToLuaSearchPathWithCustomDelimiter LUA_CPATH "$PWD/$pattern"
61 done
62 cd - >/dev/null
66 _addToLuaPath() {
67 local dir="$1"
69 echo "_addToLuaPath called for dir $dir"
71 if [[ ! -d "$dir" ]]; then
72 nix_debug "$dir not a directory abort"
73 return 0
76 # set -x
77 # if [ -n "${pythonPathsSeen[$dir]}" ]; then return; fi
78 if [[ -n "${luaPathsSeen[$dir]:-}" ]]; then
79 # if [ -n "${luaPathsSeen[$dir]}" ]; then
80 echo "$dir already parsed"
81 return
84 luaPathsSeen["$dir"]=true
86 # shellcheck disable=SC2164
87 cd "$dir"
88 for pattern in @luapathsearchpaths@; do
89 addToLuaSearchPathWithCustomDelimiter LUA_PATH "$PWD/$pattern"
90 done
92 # LUA_CPATH
93 for pattern in @luacpathsearchpaths@; do
94 addToLuaSearchPathWithCustomDelimiter LUA_CPATH "$PWD/$pattern"
95 done
97 cd - >/dev/null
99 addToSearchPath program_PATH "$dir"/bin
101 # Inspect the propagated inputs (if they exist) and recur on them.
102 local prop="$dir/nix-support/propagated-build-inputs"
103 if [ -e "$prop" ]; then
104 local new_path
105 for new_path in $(cat $prop); do
106 echo "newpath: $new_path"
107 _addToLuaPath "$new_path"
108 done
113 # Builds environment variables like LUA_PATH and PATH walking through closure
114 # of dependencies.
115 buildLuaPath() {
116 local luaPath="$1"
117 local path
119 echo "BUILD_LUA_PATH"
121 # # set -x
122 # # Create an empty table of paths (see doc on loadFromPropagatedInputs
123 # # for how this is used). Build up the program_PATH and program_LUA_PATH
124 # # variables.
125 # declare -gA luaPathsSeen=()
126 # # shellcheck disable=SC2034
127 program_PATH=
128 luaPathsSeen["@lua@"]=1
129 # addToSearchPath program_PATH @lua@/bin
130 for path in $luaPath; do
131 _addToLuaPath "$path"
132 done