-
Notifications
You must be signed in to change notification settings - Fork 2
/
localua.sh
executable file
·171 lines (142 loc) · 5.64 KB
/
localua.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# Downloads and installs a self-contained Lua and LuaRocks.
# Supports Linux, macOS and MSYS2.
# Copyright (c) Pierre Chapuis, MIT Licensed.
# Latest stable version available at: https://loadk.com/localua.sh
# Maintained at: https://github.com/oploadk/localua
DEFAULT_LUA_V="5.4.7"
DEFAULT_LR_V="3.11.1"
usage () {
>&2 echo -e "USAGE: $0 output-dir [lua-version] [luarocks-version]\n"
>&2 echo -n "The first optional argument is the Lua version, "
>&2 echo -n "the second one is the LuaRocks version. "
>&2 echo -e "Defaults are Lua $DEFAULT_LUA_V and LuaRocks $DEFAULT_LR_V.\n"
>&2 echo -n "You can set a custom build directory with environment "
>&2 echo -e "variable LOCALUA_BUILD_DIRECTORY (not useful in general).\n"
>&2 echo -e "You can set a custom makefile target with LOCALUA_TARGET.\n"
>&2 echo -e "You can disable LUA_COMPAT by setting LOCALUA_NO_COMPAT.\n"
>&2 echo -e "You can skip luarocks by setting LOCALUA_NO_LUAROCKS."
exit 1
}
# Set output directory, Lua version and LuaRocks version
ODIR="$1"
[ -z "$ODIR" ] && usage
LUA_V="$2"
[ -z "$LUA_V" ] && LUA_V="$DEFAULT_LUA_V"
LUA_SHORTV="$(echo $LUA_V | cut -c 1-3)"
LUA_SRCDIR="lua-${LUA_V}"
if [ "$LUA_V" = "pallene" ]; then
LUA_SHORTV="5.4"
LUA_SRCDIR="lua-internals"
fi
LUA_SHORTV2="$(echo $LUA_SHORTV | tr -d '.')"
LR_V="$3"
[ -z "$LR_V" ] && LR_V="$DEFAULT_LR_V"
PALLENE_ROCKSPEC="https://raw.githubusercontent.com/pallene-lang/pallene/master/pallene-dev-1.rockspec"
# Set build directory
BDIR="$LOCALUA_BUILD_DIRECTORY"
[ -z "$BDIR" ] && BDIR="$(mktemp -d /tmp/localua-XXXXXX)"
# Create output directory and get absolute path
mkdir -p "$ODIR"
>/dev/null pushd "$ODIR"
ODIR="$(pwd)"
>/dev/null popd
# Download, unpack and build Lua and LuaRocks
if [ -z "$LOCALUA_TARGET" ]; then
case "$(uname)" in
Linux)
LOCALUA_TARGET="linux";;
Darwin)
LOCALUA_TARGET="macosx";;
MSYS*)
LOCALUA_TARGET="msys";;
*)
LOCALUA_TARGET="posix";;
esac
fi
download_lua () {
if [ "$LUA_V" = "pallene" ]; then
git clone --depth 1 "[email protected]:pallene-lang/lua-internals.git"
else
curl "https://www.lua.org/ftp/lua-${LUA_V}.tar.gz" -O
tar xf "lua-${LUA_V}.tar.gz"
fi
}
cleanup_luarocks_isolation () {
# See:
# https://github.com/oploadk/localua/issues/3
# https://github.com/oploadk/localua/issues/4
echo "Cleaning up luarocks isolation..."
>/dev/null pushd "$ODIR"
# We must *not* be in the build directory, otherwise
# it will take its config instead.
rocks_dir="$("$ODIR/bin/luarocks" config rocks_dir)"
lr_version="$("$ODIR/bin/luarocks" show luarocks --mversion)"
lr_bin="$rocks_dir/luarocks/$lr_version/bin/luarocks"
lr_config_file=$("$ODIR/bin/luarocks" config config_files.system.file)
if [ ! -x "$lr_bin" ]; then
>&2 echo -n "Could not cleanup luarocks isolation, "
>&2 echo "executable $lr_bin not found."
return 1
fi
if [ ! -f "$lr_config_file" ]; then
>&2 echo -n "Could not cleanup luarocks isolation, "
>&2 echo "config file $lr_config_file not found."
return 1
fi
# Remove user tree from configuration file.
sed -e '/name = "user"/d' "$lr_config_file" > "$BDIR/t"
mv "$BDIR/t" "$lr_config_file"
>/dev/null popd
# Rebuild with the *local* Lua to avoid trash in wrapper scripts.
"$ODIR/bin/lua" "$lr_bin" make --tree="$ODIR"
}
pushd "$BDIR"
download_lua
pushd "$LUA_SRCDIR"
sed 's#"/usr/local/"#"'"$ODIR"'/"#' "src/luaconf.h" > "$BDIR/t"
mv "$BDIR/t" "src/luaconf.h"
if [ ! -z "$LOCALUA_NO_COMPAT" ]; then
sed 's#-DLUA_COMPAT_5_.##' "src/Makefile" > "$BDIR/t"
sed 's#-DLUA_COMPAT_ALL##' "$BDIR/t" > "src/Makefile"
fi
if [ "$LOCALUA_TARGET" = "msys" ]; then
>> "src/Makefile" echo
>> "src/Makefile" echo 'msys:' >> "src/Makefile"
>> "src/Makefile" echo -ne "\t"
>> "src/Makefile" echo '$(MAKE) "LUA_A=lua'"$LUA_SHORTV2"'.dll" "LUA_T=lua.exe" \'
>> "src/Makefile" echo -ne "\t"
>> "src/Makefile" echo '"AR=$(CC) -shared -Wl,--out-implib,liblua.dll.a -o" "RANLIB=strip --strip-unneeded" \'
>> "src/Makefile" echo -ne "\t"
>> "src/Makefile" echo '"SYSCFLAGS=-DLUA_BUILD_AS_DLL -DLUA_USE_POSIX -DLUA_USE_DLOPEN" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe'
>> "src/Makefile" echo -ne "\t"
>> "src/Makefile" echo '$(MAKE) "LUAC_T=luac.exe" luac.exe'
make -C src "$LOCALUA_TARGET" || exit 1
make \
TO_BIN="lua.exe luac.exe lua${LUA_SHORTV2}.dll" \
TO_LIB="liblua.a liblua.dll.a" \
INSTALL_TOP="$ODIR" install || exit 1
else
make "$LOCALUA_TARGET" || exit 1
make INSTALL_TOP="$ODIR" install || exit 1
fi
popd
if [ -z "$LOCALUA_NO_LUAROCKS" ]; then
curl -L "https://luarocks.org/releases/luarocks-${LR_V}.tar.gz" -O
tar xf "luarocks-${LR_V}.tar.gz"
pushd "luarocks-${LR_V}"
./configure --with-lua="$ODIR" --prefix="$ODIR" \
--lua-version="$LUA_SHORTV" \
--sysconfdir="$ODIR/etc" --force-config
make bootstrap
if [ -z "$LOCALUA_NO_LUAROCKS_ISOLATION_CLEANUP" ]; then
cleanup_luarocks_isolation
fi
popd
if [ "$LUA_V" = "pallene" ]; then
"$ODIR/bin/luarocks" install "$PALLENE_ROCKSPEC"
fi
fi
popd
# Cleanup
rm -rf "$BDIR"