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 --- 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 +++++++++---------- 9 files changed, 98 insertions(+), 68 deletions(-) (limited to 'src') 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