From d83b908a7371360d117cabbf6da099170f5fb178 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sat, 17 Feb 2024 15:46:01 -0300 Subject: drop cfg.lua_interpreter, use cfg.variables.LUA --- GNUmakefile | 6 +-- binary/all_in_one | 4 -- configure | 21 +++++----- install.bat | 26 ++++++++---- luarocks-dev-1.rockspec | 4 +- spec/util/test_env.lua | 96 ++++++++++++++++++++++++++----------------- src/luarocks/cmd.lua | 68 ++++++++++++++++++++++++------ src/luarocks/cmd/config.lua | 3 +- src/luarocks/cmd/init.lua | 14 +------ src/luarocks/core/cfg.lua | 42 +++++++++---------- src/luarocks/fs/lua.lua | 2 +- src/luarocks/fs/unix.lua | 2 +- src/luarocks/fs/win32.lua | 2 +- src/luarocks/test/command.lua | 3 +- src/luarocks/util.lua | 30 +++++++------- 15 files changed, 189 insertions(+), 134 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index cbb47996..22a1d817 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -7,8 +7,7 @@ INSTALL = install INSTALL_DATA = $(INSTALL) -m 644 BINARY_PLATFORM = unix -SHEBANG = \#!$(LUA_BINDIR)/$(LUA_INTERPRETER) -LUA = $(LUA_BINDIR)/$(LUA_INTERPRETER) +SHEBANG = \#!$(LUA) luarocksconfdir = $(sysconfdir)/luarocks luadir = $(datarootdir)/lua/$(LUA_VERSION) builddir = ./build @@ -38,12 +37,13 @@ $(builddir)/config-$(LUA_VERSION).lua: config.unix ' { name = "user", root = home .. "/.luarocks" };\n'\ "$$([ "$(rocks_tree)" != "$(HOME)/.luarocks" ] && printf ' { name = "system", root = "'"$(rocks_tree)"'" };\\n')"\ '}\n'\ - "$$([ -n "$(LUA_INTERPRETER)" ] && printf 'lua_interpreter = "%s";\\n' "$(LUA_INTERPRETER)")"\ 'variables = {\n'\ "$$([ -n "$(LUA_DIR)" ] && printf ' LUA_DIR = "%s";\\n' "$(LUA_DIR)")"\ "$$([ -n "$(LUA_INCDIR)" ] && printf ' LUA_INCDIR = "%s";\\n' "$(LUA_INCDIR)")"\ "$$([ -n "$(LUA_BINDIR)" ] && printf ' LUA_BINDIR = "%s";\\n' "$(LUA_BINDIR)")"\ "$$([ -n "$(LUA_LIBDIR)" ] && printf ' LUA_LIBDIR = "%s";\\n' "$(LUA_LIBDIR)")"\ + "$$([ -n "$(LUA_VERSION)" ] && printf ' LUA_VERSION = "%s";\\n' "$(LUA_VERSION)")"\ + "$$([ -n "$(LUA)" ] && printf ' LUA = "%s";\\n' "$(LUA)")"\ '}\n'\ > $@ diff --git a/binary/all_in_one b/binary/all_in_one index 9db25bf8..c3de9c2e 100755 --- a/binary/all_in_one +++ b/binary/all_in_one @@ -159,10 +159,6 @@ local function write_hardcoded_module(dir) IS_BINARY = true, SYSCONFDIR = if_platform("unix", SYSCONFDIR), - - LUA_DIR = if_platform("unix", cfg.variables.LUA_DIR), - LUA_BINDIR = if_platform("unix", cfg.variables.LUA_BINDIR), - LUA_INTERPRETER = if_platform("unix", cfg.lua_interpreter), } local name = dir .. "/luarocks/core/hardcoded.lua" diff --git a/configure b/configure index 4a5542b9..8e1ce94c 100755 --- a/configure +++ b/configure @@ -187,7 +187,8 @@ search_interpreter() { then if detect_lua_version "$lua_at/$name" then - echo "Lua interpreter found: $(GREEN "$lua_at/$name")" + LUA="$lua_at/$name" + echo "Lua interpreter found: $(GREEN "$LUA")" if [ "$LUA_BINDIR_SET" != "yes" ] then LUA_BINDIR="$lua_at" @@ -196,7 +197,7 @@ search_interpreter() { then LUA_DIR=$(dirname "$lua_at") fi - LUA_INTERPRETER="$name" + return 0 fi fi @@ -288,8 +289,8 @@ do ;; --with-lua-interpreter) [ -n "$value" ] || die "Missing value in flag $key." - LUA_INTERPRETER="$value" - LUA_INTERPRETER_SET=yes + LUA_EXE="$value" + LUA_EXE_SET=yes ;; # For specialized uses of LuaRocks: @@ -366,9 +367,9 @@ case "$LUA_VERSION" in ;; esac -if [ "$LUA_INTERPRETER_SET" = "yes" ] +if [ "$LUA_EXE_SET" = "yes" ] then - names="$LUA_INTERPRETER" + names="$LUA_EXE" fi for name in $names @@ -400,8 +401,8 @@ fi if [ "$LUA_VERSION_SET" = "yes" ] then - echo_n "Checking if $LUA_BINDIR/$LUA_INTERPRETER is Lua version $LUA_VERSION... " - if detect_lua_version "$LUA_BINDIR/$LUA_INTERPRETER" + echo_n "Checking if $LUA is Lua version $LUA_VERSION... " + if detect_lua_version "$LUA" then echo "yes" else @@ -433,7 +434,7 @@ check_incdir() { "$incdir/lua-$LUA_VERSION/lua.h" \ "$incdir/lua$(echo "$LUA_VERSION" | tr -d .)/lua.h" \ "$incdir/lua.h" \ - $("$LUA_BINDIR/$LUA_INTERPRETER" -e 'print(jit and [['"$incdir"'/luajit-]]..jit.version:match("(%d+%.%d+)")..[[/lua.h]] or "")') + $("$LUA" -e 'print(jit and [['"$incdir"'/luajit-]]..jit.version:match("(%d+%.%d+)")..[[/lua.h]] or "")') do if [ -f "$lua_h" ] then @@ -485,7 +486,7 @@ prefix=$prefix sysconfdir=$sysconfdir rocks_tree=$rocks_tree LUA_VERSION=$LUA_VERSION -LUA_INTERPRETER=$LUA_INTERPRETER +LUA=$LUA LUA_DIR=$LUA_DIR LUA_BINDIR=$LUA_BINDIR LUA_INCDIR=$LUA_INCDIR diff --git a/install.bat b/install.bat index 7a5df9cd..e84f78cf 100644 --- a/install.bat +++ b/install.bat @@ -15,8 +15,9 @@ vars.TREE_ROOT = nil vars.TREE_BIN = nil vars.TREE_LMODULE = nil vars.TREE_CMODULE = nil -vars.LUA_INTERPRETER = nil +vars.LUA = nil vars.LUA_PREFIX = nil +vars.LUA_DIR = nil vars.LUA_BINDIR = nil vars.LUA_INCDIR = nil vars.LUA_LIBDIR = nil @@ -320,7 +321,7 @@ local function look_for_interpreter(directory) end end - vars.LUA_INTERPRETER = name + vars.LUA = full_name vars.LUA_BINDIR = dir return true end @@ -398,7 +399,7 @@ end local function get_runtime() local f - vars.LUA_RUNTIME, f = pe.msvcrt(vars.LUA_BINDIR.."\\"..vars.LUA_INTERPRETER) + vars.LUA_RUNTIME, f = pe.msvcrt(vars.LUA) if type(vars.LUA_RUNTIME) ~= "string" then -- analysis failed, issue a warning vars.LUA_RUNTIME = "MSVCR80" @@ -411,9 +412,9 @@ end local function get_architecture() -- detect processor arch interpreter was compiled for - local proc = (pe.parse(vars.LUA_BINDIR.."\\"..vars.LUA_INTERPRETER) or {}).Machine + local proc = (pe.parse(vars.LUA) or {}).Machine if not proc then - die("Could not detect processor architecture used in "..vars.LUA_INTERPRETER) + die("Could not detect processor architecture used in "..vars.LUA) end print("arch: " .. proc .. " -> " .. pe.const.Machine[proc]) proc = pe.const.Machine[proc] -- collect name from constant value @@ -659,6 +660,7 @@ local function look_for_lua_install () print("Headers found, checking runtime to use...") if get_runtime() then print("Runtime check completed.") + vars.LUA_DIR = directory return true end end @@ -862,7 +864,8 @@ vars.INCDIR = S"$PREFIX\\include" vars.LUA_SHORTV = vars.LUA_VERSION:gsub("%.", "") if INSTALL_LUA then - vars.LUA_INTERPRETER = "lua5.1" + vars.LUA = vars.BINDIR .. "\\lua5.1.exe" + vars.LUA_DIR = vars.PREFIX vars.LUA_BINDIR = vars.BINDIR vars.LUA_LIBDIR = vars.LIBDIR vars.LUA_INCDIR = vars.INCDIR @@ -876,6 +879,9 @@ else vars.UNAME_M = get_architecture() -- can only do when installation was found end +assert(vars.LUA) +assert(vars.LUA_DIR) + -- check location of system tree if not vars.TREE_ROOT then -- no system tree location given, so we need to construct a default value @@ -918,7 +924,7 @@ LuaRocks : $PREFIX Config file : $CONFIG_FILE Rocktree : $TREE_ROOT -Lua interpreter : $LUA_BINDIR\$LUA_INTERPRETER +Lua interpreter : $LUA binaries : $LUA_BINDIR libraries : $LUA_LIBDIR includes : $LUA_INCDIR @@ -1031,7 +1037,7 @@ IF NOT "%LUA_PATH_5_3%"=="" ( SET "LUA_PATH_5_3=$LUADIR\?.lua;$LUADIR\?\init.lua;%LUA_PATH_5_3%" ) SET "PATH=$BINDIR;%PATH%" -"$LUA_BINDIR\$LUA_INTERPRETER" "$BINDIR\]]..c..[[.lua" %* +"$LUA" "$BINDIR\]]..c..[[.lua" %* SET EXITCODE=%ERRORLEVEL% IF NOT "%EXITCODE%"=="2" GOTO EXITLR @@ -1087,10 +1093,12 @@ vars.SYSTEM = USE_MINGW and "mingw" or "windows" local f = io.open(hardcoded_lua, "w") f:write(S[=[ return { + LUA_DIR=[[$LUA_DIR]], LUA_INCDIR=[[$LUA_INCDIR]], LUA_LIBDIR=[[$LUA_LIBDIR]], LUA_BINDIR=[[$LUA_BINDIR]], - LUA_INTERPRETER=[[$LUA_INTERPRETER]], + LUA_VERSION=[[$LUA_VERSION]], + LUA=[[$LUA]], SYSTEM = [[$SYSTEM]], PROCESSOR = [[$UNAME_M]], PREFIX = [[$PREFIX]], diff --git a/luarocks-dev-1.rockspec b/luarocks-dev-1.rockspec index 718ce338..677b07cf 100644 --- a/luarocks-dev-1.rockspec +++ b/luarocks-dev-1.rockspec @@ -28,10 +28,10 @@ test = { type = "busted", platforms = { windows = { - flags = { "--exclude-tags=ssh,git,unix", "-Xhelper", "lua_dir=$(LUA_DIR)", "-Xhelper", "lua_interpreter=$(LUA)" } + flags = { "--exclude-tags=ssh,git,unix", "-Xhelper", "lua_dir=$(LUA_DIR)", "-Xhelper", "lua=$(LUA)" } }, unix = { - flags = { "--exclude-tags=ssh,git", "-Xhelper", "lua_dir=$(LUA_DIR)", "-Xhelper", "lua_interpreter=$(LUA)" } + flags = { "--exclude-tags=ssh,git", "-Xhelper", "lua_dir=$(LUA_DIR)", "-Xhelper", "lua=$(LUA)" } } } } diff --git a/spec/util/test_env.lua b/spec/util/test_env.lua index ca520459..f9f83b8c 100644 --- a/spec/util/test_env.lua +++ b/spec/util/test_env.lua @@ -22,7 +22,7 @@ ARGUMENTS appveyor Add if running on Appveyor. os= Set OS ("linux", "osx", or "windows"). lua_dir= Path of Lua installation (default "/usr/local") - lua_interpreter= Name of the interpreter (default "lua") + lua= Name of the interpreter, may be full path (default "lua") ]] local function help() @@ -107,7 +107,7 @@ lfs.attributes = function(f, ...) -- luacheck: ignore return lfs_attributes(V(f), ...) end -function test_env.exists(path) +local function exists(path) return lfs.attributes(path, "mode") ~= nil end @@ -302,8 +302,8 @@ function test_env.set_args() test_env.MINGW = false elseif argument:find("^lua_dir=") then test_env.LUA_DIR = argument:match("^lua_dir=(.*)$") - elseif argument:find("^lua_interpreter=") then - test_env.LUA_INTERPRETER = argument:match("^lua_interpreter=(.*)$") + elseif argument:find("^lua=") then + test_env.LUA = argument:match("^lua=(.*)$") else help() end @@ -371,7 +371,7 @@ end function test_env.remove_dir(path) path = V(path) - if test_env.exists(path) then + if exists(path) then for file in lfs.dir(path) do if file ~= "." and file ~= ".." then local full_path = path..'/'..file @@ -393,7 +393,7 @@ end function test_env.remove_subdirs(path, pattern) path = V(path) - if test_env.exists(path) then + if exists(path) then for file in lfs.dir(path) do if file ~= "." and file ~= ".." then local full_path = path..'/'..file @@ -414,7 +414,7 @@ function test_env.remove_files(path, pattern) path = V(path) local result_check = false - if test_env.exists(path) then + if exists(path) then for file in lfs.dir(path) do if file ~= "." and file ~= ".." then if file:find(pattern) then @@ -445,7 +445,7 @@ local function download_rocks(urls, save_path) table.insert(fixtures, (url:gsub("^spec/fixtures", test_env.testing_paths.fixtures_dir))) else -- check if already downloaded - if not test_env.exists(save_path .. "/" .. url) then + if not exists(save_path .. "/" .. url) then table.insert(to_download, ((luarocks_repo .. url):gsub("org//", "org/"))) end end @@ -642,8 +642,8 @@ local function reset_environment(testing_paths, md5sums) end local function found_interpreter(testing_paths, luadir, lua_bindir) - local location = lua_bindir .. "/" .. testing_paths.lua_interpreter - if test_env.exists(location) then + local location = lua_bindir .. "/" .. testing_paths.lua_exe + if exists(location) then testing_paths.lua_bindir = lua_bindir testing_paths.luadir = luadir testing_paths.lua = location @@ -651,44 +651,57 @@ local function found_interpreter(testing_paths, luadir, lua_bindir) end end -local function create_paths(luaversion_full) +local function find_lua() + -- (1) LUA is a full path + if test_env.LUA and test_env.LUA:match("[/\\]") then - local testing_paths = {} - local try_dirs + local lua_bindir = test_env.LUA:match("^(.-)[/\\][^/\\]*$") + local luadir = test_env.LUA_DIR or lua_bindir:gsub("[/\\]bin$") + local lua = test_env.LUA - if test_env.TEST_TARGET_OS == "windows" then - try_dirs = { os.getenv("ProgramFiles(x86)").."/LuaRocks" } - testing_paths.luarocks_tmp = os.getenv("TEMP") - testing_paths.lua_interpreter = "lua.exe" - else - try_dirs = { "/usr/local", "/usr" } - testing_paths.luarocks_tmp = "/tmp/luarocks_testing" - testing_paths.lua_interpreter = "lua" + return lua_bindir, luadir, lua end + -- (2) LUA is just the interpreter name + local lua_exe = test_env.LUA + or ((test_env.TEST_TARGET_OS == "windows") and "lua.exe") + or "lua" + + -- (2.1) LUA_DIR was given if test_env.LUA_DIR then - table.insert(try_dirs, 1, test_env.LUA_DIR) - end - if test_env.LUA_INTERPRETER then - testing_paths.lua_interpreter = test_env.LUA_INTERPRETER - end + local luadir = test_env.LUA_DIR + local lua_bindir = exists(luadir .. "/bin") + and luadir .. "/bin" + or luadir + local lua = lua_bindir .. "/" .. lua_exe - local bindir, interp = testing_paths.lua_interpreter:match("^(.-)[/\\]([^/\\]*)$") - if bindir and interp then - testing_paths.lua_interpreter = interp - try_dirs = { bindir } + return lua_bindir, luadir, lua end - for _, try_dir in ipairs(try_dirs) do - if found_interpreter(testing_paths, try_dir, try_dir) - or found_interpreter(testing_paths, try_dir, try_dir .. "/bin") - then - break + -- (2.2) LUA_DIR was not given, try some default paths + local try_dirs = (test_env.TEST_TARGET_OS == "windows") + and { os.getenv("ProgramFiles(x86)").."/LuaRocks" } + or { "/usr/local", "/usr" } + + for _, luadir in ipairs(try_dirs) do + for _, lua_bindir in ipairs({ luadir, luadir .. "/bin" }) do + local lua = lua_bindir .. "/" .. lua_exe + if exists(lua) then + return lua_bindir, luadir, lua + end end end +end + +local function create_paths(luaversion_full) - assert(testing_paths.lua, "Lua interpreter not found! Run `busted -Xhelper help` for options") + local testing_paths = {} + + testing_paths.lua_bindir, testing_paths.luadir, testing_paths.lua = find_lua() + if (not testing_paths.lua) or (not exists(testing_paths.lua)) then + error("Lua interpreter not found! Run `busted -Xhelper help` for options") + end local base_dir = lfs.currentdir() @@ -713,6 +726,12 @@ local function create_paths(luaversion_full) testing_paths.testing_rocks = testing_paths.testing_tree .. "/lib/luarocks/rocks-" .. test_env.lua_version testing_paths.testing_sys_rocks = testing_paths.testing_sys_tree .. "/lib/luarocks/rocks-" .. test_env.lua_version + if test_env.TEST_TARGET_OS == "windows" then + testing_paths.luarocks_tmp = os.getenv("TEMP") + else + testing_paths.luarocks_tmp = "/tmp/luarocks_testing" + end + if test_env.TEST_TARGET_OS == "windows" then testing_paths.win_tools = base_dir .. "/win32/tools" end @@ -739,7 +758,7 @@ function test_env.setup_specs(extra_rocks) -- if global variable about successful creation of testing environment doesn't exist, build environment if not test_env.setup_done then if test_env.CI then - if not test_env.exists(os.getenv("HOME") .. "/.ssh/id_rsa.pub") then + if not exists(os.getenv("HOME") .. "/.ssh/id_rsa.pub") then execute_bool("ssh-keygen -t rsa -P \"\" -f ~/.ssh/id_rsa") execute_bool("cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys") execute_bool("chmod og-wx ~/.ssh/authorized_keys") @@ -914,7 +933,7 @@ local function setup_luarocks() ("SYSCONFDIR = %q,"):format(testing_paths.testing_lrprefix .. "/etc/luarocks"), ("LUA_DIR = %q,"):format(testing_paths.luadir), ("LUA_BINDIR = %q,"):format(testing_paths.lua_bindir), - ("LUA_INTERPRETER = %q,"):format(testing_paths.lua_interpreter), + ("LUA = %q,"):format(testing_paths.lua), } if test_env.TEST_TARGET_OS == "windows" then @@ -1083,6 +1102,7 @@ test_env.set_args() test_env.testing_paths = create_paths(test_env.LUA_V or test_env.LUAJIT_V) test_env.env_variables = create_env(test_env.testing_paths) test_env.run = make_run_functions() +test_env.exists = exists test_env.V = V return test_env diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua index 250e3cff..9c3d91db 100644 --- a/src/luarocks/cmd.lua +++ b/src/luarocks/cmd.lua @@ -184,7 +184,11 @@ local function die(message, exitcode) os.exit(exitcode or cmd.errorcodes.UNSPECIFIED) end -local function search_lua_in_path(lua_version, verbose) +local function search_lua(lua_version, verbose, search_at) + if search_at then + return util.find_lua(search_at, lua_version, verbose) + end + local path_sep = (package.config:sub(1, 1) == "\\" and ";" or ":") local all_tried = {} for bindir in (os.getenv("PATH") or ""):gmatch("[^"..path_sep.."]+") do @@ -284,7 +288,7 @@ do end if lua_version then - local detected = search_lua_in_path(lua_version) + local detected = search_lua(lua_version) if detected then return detected end @@ -497,6 +501,19 @@ Enabling completion for Fish: return parser end +local function get_first_arg() + if not arg then + return + end + local first_arg = arg[0] + local i = -1 + while arg[i] do + first_arg = arg[i] + i = i -1 + end + return first_arg +end + --- Main command-line processor. -- Parses input arguments and calls the appropriate driver function -- to execute the action requested on the command-line, forwarding @@ -610,20 +627,47 @@ function cmd.run_command(description, commands, external_namespace, ...) -- try again now. local tried if not lua_found then - if cfg.variables.LUA_DIR then - lua_found, tried = util.find_lua(cfg.variables.LUA_DIR, cfg.lua_version, args.verbose) - else - lua_found, tried = search_lua_in_path(cfg.lua_version, args.verbose) + local detected + detected, tried = search_lua(cfg.lua_version, args.verbose, cfg.variables.LUA_DIR) + if detected then + lua_found = true + cfg.variables.LUA = detected.lua + cfg.variables.LUA_DIR = detected.lua_dir + cfg.variables.LUA_BINDIR = detected.lua_bindir + if args.lua_dir then + cfg.variables.LUA_INCDIR = nil + cfg.variables.LUA_LIBDIR = nil + end end end - if not lua_found and args.command ~= "config" and args.command ~= "help" then - util.warning(tried .. - "\nModules may not install with the correct configurations. " .. - "You may want to configure the path prefix to your build " .. - "of Lua " .. cfg.lua_version .. " using\n\n" .. - " luarocks config --local lua_dir \n") + if lua_found then + assert(cfg.variables.LUA) + else + if args.command ~= "config" and args.command ~= "help" then + util.warning(tried .. + "\nModules may not install with the correct configurations. " .. + "You may want to configure the path prefix to your build " .. + "of Lua " .. cfg.lua_version .. " using\n\n" .. + " luarocks config --local lua_dir \n") + end + + -- Fallback producing _some_ Lua configuration based on the running interpreter. + -- Most likely won't produce correct results when running from the standalone binary, + -- so eventually we need to drop this and outright fail if Lua is not found + -- or explictly configured + if not cfg.variables.LUA then + local first_arg = get_first_arg() + cfg.variables.LUA_DIR = dir.dir_name(fs.absolute_name(first_arg)) + cfg.variables.LUA_BINDIR = cfg.variables.LUA_DIR + local exe = fs.base_name(first_arg) + exe = exe:match("rocks") and ("lua" .. (cfg.arch:match("win") and ".exe" or "")) or exe + cfg.variables.LUA = dir.path(cfg.variables.LUA_BINDIR, exe) + cfg.variables.LUA_INCDIR = nil + cfg.variables.LUA_LIBDIR = nil + end end + cfg.lua_found = lua_found if cfg.project_dir then diff --git a/src/luarocks/cmd/config.lua b/src/luarocks/cmd/config.lua index d9b679df..7ad28777 100644 --- a/src/luarocks/cmd/config.lua +++ b/src/luarocks/cmd/config.lua @@ -18,7 +18,6 @@ Query information about the LuaRocks configuration. any command-line flags passed) Examples: - luarocks config lua_interpreter luarocks config variables.LUA_INCDIR luarocks config lua_version @@ -300,7 +299,7 @@ function config_cmd.command(args) ["variables.LUA_BINDIR"] = cfg.variables.LUA_BINDIR, ["variables.LUA_INCDIR"] = cfg.variables.LUA_INCDIR, ["variables.LUA_LIBDIR"] = cfg.variables.LUA_LIBDIR, - ["lua_interpreter"] = cfg.lua_interpreter, + ["variables.LUA"] = cfg.variables.LUA, } if args.lua_version then local prefix = dir.dir_name(cfg.config_files[scope].file) diff --git a/src/luarocks/cmd/init.lua b/src/luarocks/cmd/init.lua index efd612e4..543a720b 100644 --- a/src/luarocks/cmd/init.lua +++ b/src/luarocks/cmd/init.lua @@ -78,8 +78,7 @@ local function write_wrapper_scripts(wrapper_dir, luarocks_wrapper, lua_wrapper) end if write_lua_wrapper then - local interp = dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter) - if util.check_lua_version(interp, cfg.lua_version) then + if util.check_lua_version(cfg.variables.LUA, cfg.lua_version) then util.printout("Preparing " .. lua_wrapper .. " for version " .. cfg.lua_version .. "...") path.use_tree(tree) fs.wrap_script(nil, lua_wrapper, "all") @@ -160,21 +159,12 @@ function init.command(args) local config_tbl, err = persist.load_config_file_if_basic(config_file, cfg) if config_tbl then - local globals = { - "lua_interpreter", - } - for _, v in ipairs(globals) do - if cfg[v] then - config_tbl[v] = cfg[v] - end - end - local varnames = { "LUA_DIR", "LUA_INCDIR", "LUA_LIBDIR", "LUA_BINDIR", - "LUA_INTERPRETER", + "LUA", } for _, varname in ipairs(varnames) do if cfg.variables[varname] then diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg.lua index 81987cc9..2e5301c1 100644 --- a/src/luarocks/core/cfg.lua +++ b/src/luarocks/core/cfg.lua @@ -175,7 +175,6 @@ local function make_defaults(lua_version, target_cpu, platforms, home) -- Configure defaults: local defaults = { - lua_interpreter = "lua", local_by_default = false, accept_unknown_fields = false, fs_use_modules = true, @@ -556,19 +555,6 @@ local function use_defaults(cfg, defaults) end end -local function get_first_arg() - if not arg then - return - end - local first_arg = arg[0] - local i = -1 - while arg[i] do - first_arg = arg[i] - i = i -1 - end - return first_arg -end - -------------------------------------------------------------------------------- local cfg = {} @@ -580,7 +566,7 @@ local cfg = {} -- * lua_version (in x.y format, e.g. "5.3") -- * lua_bindir (e.g. "/usr/local/bin") -- * lua_dir (e.g. "/usr/local") --- * lua_interpreter (e.g. "lua-5.3") +-- * lua (e.g. "/usr/local/bin/lua-5.3") -- * project_dir (a string with the path of the project directory -- when using per-project environments, as created with `luarocks init`) -- @param warning a logging function for warnings that takes a string @@ -615,16 +601,15 @@ function cfg.init(detected, warning) -- Use detected values as defaults, overridable via config files or CLI args - local first_arg = get_first_arg() - cfg.lua_version = detected.lua_version or hardcoded.LUA_VERSION or _VERSION:sub(5) - cfg.lua_interpreter = detected.lua_interpreter or hardcoded.LUA_INTERPRETER or (first_arg and first_arg:gsub(".*[\\/]", "")) or (is_windows and "lua.exe" or "lua") cfg.project_dir = (not hardcoded.FORCE_CONFIG) and detected.project_dir do - local lua_bindir = detected.lua_bindir or hardcoded.LUA_BINDIR or (first_arg and first_arg:gsub("[\\/][^\\/]+$", "")) - local lua_dir = detected.lua_dir or hardcoded.LUA_DIR or (lua_bindir and lua_bindir:gsub("[\\/]bin$", "")) + local lua = detected.lua or hardcoded.LUA + local lua_bindir = detected.lua_bindir or hardcoded.LUA_BINDIR + local lua_dir = detected.lua_dir or hardcoded.LUA_DIR cfg.variables = { + LUA = lua, LUA_DIR = lua_dir, LUA_BINDIR = lua_bindir, LUA_LIBDIR = hardcoded.LUA_LIBDIR, @@ -756,6 +741,11 @@ function cfg.init(detected, warning) end end + -- backwards compatibility: + if cfg.lua_interpreter and cfg.variables.LUA_BINDIR and not cfg.variables.LUA then + cfg.variables.LUA = (cfg.variables.LUA_BINDIR .. "/" .. cfg.lua_interpreter):gsub("\\", "/") + end + ---------------------------------------- -- Config files are loaded. -- Let's finish up the cfg table. @@ -765,11 +755,11 @@ function cfg.init(detected, warning) cfg.project_dir = detected.given_project_dir or cfg.project_dir cfg.lua_version = detected.given_lua_version or cfg.lua_version if detected.given_lua_dir then + cfg.variables.LUA = detected.lua cfg.variables.LUA_DIR = detected.given_lua_dir cfg.variables.LUA_BINDIR = detected.lua_bindir cfg.variables.LUA_LIBDIR = nil cfg.variables.LUA_INCDIR = nil - cfg.lua_interpreter = detected.lua_interpreter end -- Build a default list of rocks trees if not given @@ -794,9 +784,17 @@ function cfg.init(detected, warning) defaults.fs_use_modules = true end + -- if only cfg.variables.LUA is given in config files, + -- derive LUA_BINDIR and LUA_DIR from them. + if cfg.variables.LUA and not cfg.variables.LUA_BINDIR then + cfg.variables.LUA_BINDIR = cfg.variables.LUA:gsub("^(.*)[/\\][^/\\]*$") + if not cfg.variables.LUA_DIR then + cfg.variables.LUA_DIR = cfg.variables.LUA_BINDIR:gsub("[/\\]bin$", "") or cfg.variables.LUA_BINDIR + end + end + use_defaults(cfg, defaults) - cfg.variables.LUA = cfg.variables.LUA or (cfg.variables.LUA_BINDIR and (cfg.variables.LUA_BINDIR .. "/" .. cfg.lua_interpreter):gsub("//", "/")) cfg.user_agent = "LuaRocks/"..cfg.program_version.." "..cfg.arch cfg.config_files = { diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 0805efd2..e9b4c5d8 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -1188,7 +1188,7 @@ end -- @return boolean true, if it is a Lua script, false otherwise function fs_lua.is_lua(filename) filename = filename:gsub([[%\]],"/") -- normalize on fw slash to prevent escaping issues - local lua = fs.Q(dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)) -- get lua interpreter configured + local lua = fs.Q(cfg.variables.LUA) -- get lua interpreter configured -- execute on configured interpreter, might not be the same as the interpreter LR is run on local result = fs.execute_string(lua..[[ -e "if loadfile(']]..filename..[[') then os.exit(0) else os.exit(1) end"]]) return (result == true) diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index 61569e30..f3db5b30 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua @@ -108,7 +108,7 @@ function unix.wrap_script(script, target, deps_mode, name, version, ...) end local argv = { - fs.Q(dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)), + fs.Q(cfg.variables["LUA"]), "-e", fs.Q(table.concat(luainit, ";")), script and fs.Q(script) or [[$([ "$*" ] || echo -i)]], diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index 1a35c3d4..1842ac4c 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua @@ -195,7 +195,7 @@ function win32.wrap_script(script, target, deps_mode, name, version, ...) end local argv = { - fs.Qb(dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)), + fs.Qb(cfg.variables["LUA"]), "-e", fs.Qb(table.concat(luainit, ";")), script and fs.Qb(script) or "%I%", diff --git a/src/luarocks/test/command.lua b/src/luarocks/test/command.lua index 58fa22cb..afdb5cb6 100644 --- a/src/luarocks/test/command.lua +++ b/src/luarocks/test/command.lua @@ -2,7 +2,6 @@ local command = {} local fs = require("luarocks.fs") -local dir = require("luarocks.dir") local cfg = require("luarocks.core.cfg") local unpack = table.unpack or unpack @@ -31,7 +30,7 @@ function command.run_tests(test, args) if not fs.exists(test.script) then return nil, "Test script " .. test.script .. " does not exist" end - local lua = fs.Q(dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)) -- get lua interpreter configured + local lua = fs.Q(cfg.variables["LUA"]) -- get lua interpreter configured ok = fs.execute(lua, test.script, unpack(args)) elseif test.command then ok = fs.execute(test.command, unpack(args)) diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index dd05b4ac..0a900ecc 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -434,11 +434,11 @@ do return '"' .. pathname .. '"' end - function util.check_lua_version(lua_exe, luaver) - if not util.exists(lua_exe) then + function util.check_lua_version(lua, luaver) + if not util.exists(lua) then return nil end - local lv, err = util.popen_read(Q(lua_exe) .. ' -e "io.write(_VERSION:sub(5))"') + local lv, err = util.popen_read(Q(lua) .. ' -e "io.write(_VERSION:sub(5))"') if lv == "" then return nil end @@ -457,8 +457,8 @@ do local ljv if cfg.lua_version == "5.1" then - -- Ignores extra version info for custom builds, e.g. "LuaJIT 2.1.0-beta3 some-other-version-info" - ljv = util.popen_read(Q(cfg.variables["LUA_BINDIR"] .. "/" .. cfg.lua_interpreter) .. ' -e "io.write(tostring(jit and jit.version:gsub([[^%S+ (%S+).*]], [[%1]])))"') + -- Ignores extra version info for custom builds, e.g. "LuaJIT 2.1.0-beta3 some-other-version-info" + ljv = util.popen_read(Q(cfg.variables.LUA) .. ' -e "io.write(tostring(jit and jit.version:gsub([[^%S+ (%S+).*]], [[%1]])))"') if ljv == "nil" then ljv = nil end @@ -502,17 +502,17 @@ do local dir_sep = package.config:sub(1, 1) for _, d in ipairs({ prefix .. dir_sep .. "bin", prefix }) do for _, name in ipairs(names) do - local lua_exe = d .. dir_sep .. name - local is_wrapper, err = util.lua_is_wrapper(lua_exe) + local lua = d .. dir_sep .. name + local is_wrapper, err = util.lua_is_wrapper(lua) if is_wrapper == false then - local lv = util.check_lua_version(lua_exe, luaver) + local lv = util.check_lua_version(lua, luaver) if lv then - return name, d, lv + return lua, d, lv end elseif is_wrapper == true or err == nil then - table.insert(tried, lua_exe) + table.insert(tried, lua) else - table.insert(tried, string.format("%-13s (%s)", lua_exe, err)) + table.insert(tried, string.format("%-13s (%s)", lua, err)) end end end @@ -525,15 +525,15 @@ do end function util.find_lua(prefix, luaver, verbose) - local lua_interpreter, bindir - lua_interpreter, bindir, luaver = find_lua_bindir(prefix, luaver, verbose) - if not lua_interpreter then + local lua, bindir + lua, bindir, luaver = find_lua_bindir(prefix, luaver, verbose) + if not lua then return nil, bindir end return { lua_version = luaver, - lua_interpreter = lua_interpreter, + lua = lua, lua_dir = prefix, lua_bindir = bindir, } -- cgit v1.2.3-55-g6feb