├── LICENSE.txt ├── README.md └── localua.sh /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) Pierre Chapuis 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # localua.sh 2 | 3 | localua.sh is a Bash script to download and install a self-contained Lua and 4 | LuaRocks on Linux, macOS and MSYS2. 5 | 6 | It supports Lua 5.1 to 5.4. It does *not* officially support other operating 7 | systems or environments, and it does *not* support LuaJIT. 8 | 9 | Run the script without arguments to see how to use it. 10 | 11 | ## But I just want to type `lua` and `luarocks`! 12 | 13 | Say you store your local Lua environment in `.lua` in your project, just add 14 | `.lua/bin` at the beginning of your `$PATH`. This will only work from the root 15 | directory of the project though... but you can also add `../.lua/bin` etc if 16 | you want! 17 | 18 | ## MSYS2 dependencies 19 | 20 | Before running the script, install `base-devel`, `gcc` and `unzip`. 21 | 22 | ## Alternatives 23 | 24 | If this does not fit your needs, check out: 25 | 26 | - [hererocks](https://github.com/mpeterv/hererocks), in Python 27 | - [luawinmulti](https://github.com/Tieske/luawinmulti), on Windows 28 | 29 | Similar tools for other languages: 30 | 31 | - [lonesnake](https://github.com/pwalch/lonesnake) for Python 32 | - [rye](https://github.com/mitsuhiko/rye) for Python 33 | 34 | ## Copyright 35 | 36 | - Copyright (c) Pierre Chapuis 37 | -------------------------------------------------------------------------------- /localua.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Downloads and installs a self-contained Lua and LuaRocks. 4 | # Supports Linux, macOS and MSYS2. 5 | # Copyright (c) Pierre Chapuis, MIT Licensed. 6 | # Latest stable version available at: https://loadk.com/localua.sh 7 | # Maintained at: https://github.com/oploadk/localua 8 | 9 | DEFAULT_LUA_V="5.4.7" 10 | DEFAULT_LR_V="3.11.1" 11 | 12 | usage () { 13 | >&2 echo -e "USAGE: $0 output-dir [lua-version] [luarocks-version]\n" 14 | >&2 echo -n "The first optional argument is the Lua version, " 15 | >&2 echo -n "the second one is the LuaRocks version. " 16 | >&2 echo -e "Defaults are Lua $DEFAULT_LUA_V and LuaRocks $DEFAULT_LR_V.\n" 17 | >&2 echo -n "You can set a custom build directory with environment " 18 | >&2 echo -e "variable LOCALUA_BUILD_DIRECTORY (not useful in general).\n" 19 | >&2 echo -e "You can set a custom makefile target with LOCALUA_TARGET.\n" 20 | >&2 echo -e "You can disable LUA_COMPAT by setting LOCALUA_NO_COMPAT.\n" 21 | >&2 echo -e "You can skip luarocks by setting LOCALUA_NO_LUAROCKS." 22 | exit 1 23 | } 24 | 25 | # Set output directory, Lua version and LuaRocks version 26 | 27 | ODIR="$1" 28 | [ -z "$ODIR" ] && usage 29 | 30 | LUA_V="$2" 31 | [ -z "$LUA_V" ] && LUA_V="$DEFAULT_LUA_V" 32 | 33 | LUA_SHORTV="$(echo $LUA_V | cut -c 1-3)" 34 | LUA_SRCDIR="lua-${LUA_V}" 35 | if [ "$LUA_V" = "pallene" ]; then 36 | LUA_SHORTV="5.4" 37 | LUA_SRCDIR="lua-internals" 38 | fi 39 | LUA_SHORTV2="$(echo $LUA_SHORTV | tr -d '.')" 40 | 41 | LR_V="$3" 42 | [ -z "$LR_V" ] && LR_V="$DEFAULT_LR_V" 43 | 44 | PALLENE_ROCKSPEC="https://raw.githubusercontent.com/pallene-lang/pallene/master/pallene-dev-1.rockspec" 45 | 46 | # Set build directory 47 | 48 | BDIR="$LOCALUA_BUILD_DIRECTORY" 49 | [ -z "$BDIR" ] && BDIR="$(mktemp -d /tmp/localua-XXXXXX)" 50 | 51 | # Create output directory and get absolute path 52 | 53 | mkdir -p "$ODIR" 54 | >/dev/null pushd "$ODIR" 55 | ODIR="$(pwd)" 56 | >/dev/null popd 57 | 58 | # Download, unpack and build Lua and LuaRocks 59 | 60 | if [ -z "$LOCALUA_TARGET" ]; then 61 | case "$(uname)" in 62 | Linux) 63 | LOCALUA_TARGET="linux";; 64 | Darwin) 65 | LOCALUA_TARGET="macosx";; 66 | MSYS*) 67 | LOCALUA_TARGET="msys";; 68 | *) 69 | LOCALUA_TARGET="posix";; 70 | esac 71 | fi 72 | 73 | download_lua () { 74 | if [ "$LUA_V" = "pallene" ]; then 75 | git clone --depth 1 "git@github.com:pallene-lang/lua-internals.git" 76 | else 77 | curl "https://www.lua.org/ftp/lua-${LUA_V}.tar.gz" -O 78 | tar xf "lua-${LUA_V}.tar.gz" 79 | fi 80 | } 81 | 82 | cleanup_luarocks_isolation () { 83 | # See: 84 | # https://github.com/oploadk/localua/issues/3 85 | # https://github.com/oploadk/localua/issues/4 86 | 87 | echo "Cleaning up luarocks isolation..." 88 | 89 | >/dev/null pushd "$ODIR" 90 | # We must *not* be in the build directory, otherwise 91 | # it will take its config instead. 92 | 93 | rocks_dir="$("$ODIR/bin/luarocks" config rocks_dir)" 94 | lr_version="$("$ODIR/bin/luarocks" show luarocks --mversion)" 95 | lr_bin="$rocks_dir/luarocks/$lr_version/bin/luarocks" 96 | lr_config_file=$("$ODIR/bin/luarocks" config config_files.system.file) 97 | 98 | if [ ! -x "$lr_bin" ]; then 99 | >&2 echo -n "Could not cleanup luarocks isolation, " 100 | >&2 echo "executable $lr_bin not found." 101 | return 1 102 | fi 103 | 104 | if [ ! -f "$lr_config_file" ]; then 105 | >&2 echo -n "Could not cleanup luarocks isolation, " 106 | >&2 echo "config file $lr_config_file not found." 107 | return 1 108 | fi 109 | 110 | # Remove user tree from configuration file. 111 | sed -e '/name = "user"/d' "$lr_config_file" > "$BDIR/t" 112 | mv "$BDIR/t" "$lr_config_file" 113 | >/dev/null popd 114 | 115 | # Rebuild with the *local* Lua to avoid trash in wrapper scripts. 116 | "$ODIR/bin/lua" "$lr_bin" make --tree="$ODIR" 117 | } 118 | 119 | pushd "$BDIR" 120 | download_lua 121 | pushd "$LUA_SRCDIR" 122 | sed 's#"/usr/local/"#"'"$ODIR"'/"#' "src/luaconf.h" > "$BDIR/t" 123 | mv "$BDIR/t" "src/luaconf.h" 124 | if [ ! -z "$LOCALUA_NO_COMPAT" ]; then 125 | sed 's#-DLUA_COMPAT_5_.##' "src/Makefile" > "$BDIR/t" 126 | sed 's#-DLUA_COMPAT_ALL##' "$BDIR/t" > "src/Makefile" 127 | fi 128 | 129 | if [ "$LOCALUA_TARGET" = "msys" ]; then 130 | >> "src/Makefile" echo 131 | >> "src/Makefile" echo 'msys:' >> "src/Makefile" 132 | >> "src/Makefile" echo -ne "\t" 133 | >> "src/Makefile" echo '$(MAKE) "LUA_A=lua'"$LUA_SHORTV2"'.dll" "LUA_T=lua.exe" \' 134 | >> "src/Makefile" echo -ne "\t" 135 | >> "src/Makefile" echo '"AR=$(CC) -shared -Wl,--out-implib,liblua.dll.a -o" "RANLIB=strip --strip-unneeded" \' 136 | >> "src/Makefile" echo -ne "\t" 137 | >> "src/Makefile" echo '"SYSCFLAGS=-DLUA_BUILD_AS_DLL -DLUA_USE_POSIX -DLUA_USE_DLOPEN" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe' 138 | >> "src/Makefile" echo -ne "\t" 139 | >> "src/Makefile" echo '$(MAKE) "LUAC_T=luac.exe" luac.exe' 140 | 141 | make -C src "$LOCALUA_TARGET" || exit 1 142 | make \ 143 | TO_BIN="lua.exe luac.exe lua${LUA_SHORTV2}.dll" \ 144 | TO_LIB="liblua.a liblua.dll.a" \ 145 | INSTALL_TOP="$ODIR" install || exit 1 146 | else 147 | make "$LOCALUA_TARGET" || exit 1 148 | make INSTALL_TOP="$ODIR" install || exit 1 149 | fi 150 | popd 151 | if [ -z "$LOCALUA_NO_LUAROCKS" ]; then 152 | curl -L "https://luarocks.org/releases/luarocks-${LR_V}.tar.gz" -O 153 | tar xf "luarocks-${LR_V}.tar.gz" 154 | pushd "luarocks-${LR_V}" 155 | ./configure --with-lua="$ODIR" --prefix="$ODIR" \ 156 | --lua-version="$LUA_SHORTV" \ 157 | --sysconfdir="$ODIR/etc" --force-config 158 | make bootstrap 159 | if [ -z "$LOCALUA_NO_LUAROCKS_ISOLATION_CLEANUP" ]; then 160 | cleanup_luarocks_isolation 161 | fi 162 | popd 163 | if [ "$LUA_V" = "pallene" ]; then 164 | "$ODIR/bin/luarocks" install "$PALLENE_ROCKSPEC" 165 | fi 166 | fi 167 | popd 168 | 169 | # Cleanup 170 | 171 | rm -rf "$BDIR" 172 | --------------------------------------------------------------------------------