From d1a32ba6b1fd5a829aa551aeb37a0c558b09660f Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 17 Apr 2020 18:47:30 -0300 Subject: Print more info about the location of Lua --- spec/cmd_spec.lua | 10 +++++----- src/luarocks/cmd.lua | 43 ++++++++++++++++++++++++++++++++++++++++--- src/luarocks/cmd/config.lua | 4 ++++ src/luarocks/core/cfg.lua | 2 ++ src/luarocks/deps.lua | 28 +++++++++++++++++++--------- 5 files changed, 70 insertions(+), 17 deletions(-) diff --git a/spec/cmd_spec.lua b/spec/cmd_spec.lua index 2c3f9f47..ad6ae7ad 100644 --- a/spec/cmd_spec.lua +++ b/spec/cmd_spec.lua @@ -56,7 +56,7 @@ describe("LuaRocks command line #integration", function() it("warns but continues if given an invalid version", function() local output = run.luarocks("--lua-version=1.0") assert.match("Warning: Lua 1.0 interpreter not found", output, 1, true) - assert.match("Lua version: 1.0", output, 1, true) + assert.match("Version%s*:%s*1.0", output) end) it("sets the version independently of project tree", function() @@ -64,10 +64,10 @@ describe("LuaRocks command line #integration", function() assert.truthy(run.luarocks_bool("init --lua-version=" .. test_env.lua_version .. " --lua-versions=" .. test_env.lua_version)) local output = run.luarocks("--lua-version=1.0") - assert.match("Lua version: 1.0", output, 1, true) + assert.match("Version%s*:%s*1.0", output) output = run.luarocks("--lua-version=1.0 --project-tree=.") - assert.match("Lua version: 1.0", output, 1, true) + assert.match("Version%s*:%s*1.0", output) end, finally) end) end) @@ -83,7 +83,7 @@ describe("LuaRocks command line #integration", function() lfs.chdir("bbb") local output = run.luarocks("") - assert.match("Lua version: 1.0", output, 1, true) + assert.match("Version%s*:%s*1.0", output) end, finally) end) @@ -101,7 +101,7 @@ describe("LuaRocks command line #integration", function() lfs.chdir("bbb") local output = run.luarocks("") - assert.match("Lua version: 5.2", output, 1, true) + assert.match("Version%s*:%s*5.2", output) end, finally) end) diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua index be863cfa..37266b40 100644 --- a/src/luarocks/cmd.lua +++ b/src/luarocks/cmd.lua @@ -266,7 +266,10 @@ do if args.lua_dir then local detected, err = util.find_lua(args.lua_dir, lua_version) if not detected then - die(err) + local suggestion = (not args.lua_version) + and "\nYou may want to specify a different Lua version with --lua-version\n" + or "" + die(err .. suggestion) end return detected end @@ -324,11 +327,45 @@ local function get_status(status) return status and "ok" or "not found" end +local function use_to_fix_location(key) + local buf = " ****************************************\n" + buf = buf .. " Use the command\n\n" + buf = buf .. " luarocks config " .. key .. " \n\n" + buf = buf .. " to fix the location\n" + buf = buf .. " ****************************************\n" + return buf +end + local function get_config_text(cfg) - local buf = "Configuration:\n Lua version: "..cfg.lua_version.."\n" + local deps = require("luarocks.deps") + + local libdir_ok = deps.check_lua_libdir(cfg.variables) + local incdir_ok = deps.check_lua_incdir(cfg.variables) + local bindir_ok = fs.exists(cfg.variables.LUA_BINDIR) + local luadir_ok = fs.exists(cfg.variables.LUA_DIR) + local lua_ok = fs.exists(cfg.variables.LUA) + + local buf = "Configuration:\n" + buf = buf.." Lua:\n" + buf = buf.." Version : "..cfg.lua_version.."\n" if cfg.luajit_version then - buf = buf.." LuaJIT version: "..cfg.luajit_version.."\n" + buf = buf.." LuaJIT : "..cfg.luajit_version.."\n" end + buf = buf.." Interpreter: "..(cfg.variables.LUA or "").." ("..get_status(lua_ok)..")\n" + buf = buf.." LUA_DIR : "..(cfg.variables.LUA_DIR or "").." ("..get_status(luadir_ok)..")\n" + if not lua_ok then + buf = buf .. use_to_fix_location("lua_dir") + end + buf = buf.." LUA_BINDIR : "..(cfg.variables.LUA_BINDIR or "").." ("..get_status(bindir_ok)..")\n" + buf = buf.." LUA_INCDIR : "..(cfg.variables.LUA_INCDIR or "").." ("..get_status(incdir_ok)..")\n" + if lua_ok and not incdir_ok then + buf = buf .. use_to_fix_location("variables.LUA_INCDIR") + end + buf = buf.." LUA_LIBDIR : "..(cfg.variables.LUA_LIBDIR or "").." ("..get_status(libdir_ok)..")\n" + if lua_ok and not libdir_ok then + buf = buf .. use_to_fix_location("variables.LUA_LIBDIR") + end + buf = buf.."\n Configuration files:\n" local conf = cfg.config_files buf = buf.." System : "..fs.absolute_name(conf.system.file).." ("..get_status(conf.system.found)..")\n" diff --git a/src/luarocks/cmd/config.lua b/src/luarocks/cmd/config.lua index bdabe960..5e92634a 100644 --- a/src/luarocks/cmd/config.lua +++ b/src/luarocks/cmd/config.lua @@ -297,6 +297,10 @@ function config_cmd.command(args) ["variables.LUA_LIBDIR"] = cfg.variables.LUA_LIBDIR, ["lua_interpreter"] = cfg.lua_interpreter, } + if args.lua_version then + local prefix = dir.dir_name(cfg.config_files[scope].file) + persist.save_default_lua_version(prefix, args.lua_version) + end return write_entries(keys, scope, args.unset) end diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg.lua index 0c79fc63..3917e2af 100644 --- a/src/luarocks/core/cfg.lua +++ b/src/luarocks/core/cfg.lua @@ -690,6 +690,8 @@ function cfg.init(detected, warning) if detected.given_lua_dir then 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 diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua index 0eb80193..d935a5c8 100644 --- a/src/luarocks/deps.lua +++ b/src/luarocks/deps.lua @@ -649,6 +649,16 @@ function deps.scan_deps(results, manifest, name, version, deps_mode) end end +local function lua_h_exists(d) + local lua_h = dir.path(d, "lua.h") + local fd = io.open(lua_h) + if fd then + -- TODO check that LUA_VERSION_MAJOR and LUA_VERSION_MINOR match luaver + fd:close() + return d + end +end + local function find_lua_incdir(prefix, luaver, luajitver) luajitver = luajitver and luajitver:gsub("%-.*", "") local shortv = luaver:gsub("%.", "") @@ -662,11 +672,7 @@ local function find_lua_incdir(prefix, luaver, luajitver) luajitver and prefix .. "/include/luajit-" .. luajitver:match("^(%d+%.%d+)"), } for _, d in ipairs(incdirs) do - local lua_h = dir.path(d, "lua.h") - local fd = io.open(lua_h) - if fd then - -- TODO check that LUA_VERSION_MAJOR and LUA_VERSION_MINOR match luaver - fd:close() + if lua_h_exists(d) then return d end end @@ -678,14 +684,18 @@ end function deps.check_lua_incdir(vars) local ljv = util.get_luajit_version() - if (not vars.LUA_INCDIR) and vars.LUA_DIR then + if vars.LUA_INCDIR then + return lua_h_exists(vars.LUA_INCDIR) + end + + if vars.LUA_DIR then vars.LUA_INCDIR = find_lua_incdir(vars.LUA_DIR, cfg.lua_version, ljv) - if vars.LUA_INCDIR == nil then - return nil, "Failed finding Lua header files. You may need to install them or configure LUA_INCDIR.", "dependency" + if vars.LUA_INCDIR then + return true end end - return true + return nil, "Failed finding Lua header files. You may need to install them or configure LUA_INCDIR.", "dependency" end function deps.check_lua_libdir(vars) -- cgit v1.2.3-55-g6feb