From c13ff298bdd424d7b7401fce4f0379cda0348af8 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 19 Feb 2024 11:55:39 -0300 Subject: fix(build): don't look for Lua headers when installing pure-Lua rocks This only applies to 'builtin' as we can't know about other modes, but this should be good enough. Fixes #1275. --- spec/build_spec.lua | 117 --------------------------------- spec/deps_spec.lua | 143 +++++++++++++++++++++++++++++++++++++++++ src/luarocks/build.lua | 34 +++++----- src/luarocks/build/builtin.lua | 52 +++++---------- src/luarocks/cmd.lua | 2 +- src/luarocks/deps.lua | 54 +++++++++++++++- 6 files changed, 228 insertions(+), 174 deletions(-) diff --git a/spec/build_spec.lua b/spec/build_spec.lua index 653f2160..3b33a1aa 100644 --- a/spec/build_spec.lua +++ b/spec/build_spec.lua @@ -598,123 +598,6 @@ describe("LuaRocks build #unit", function() end, finally) end) - describe("builtin.autodetect_external_dependencies", function() - it("returns false if the given build table has no external dependencies", function() - local build_table = { - type = "builtin" - } - - assert.falsy(build_builtin.autodetect_external_dependencies(build_table)) - end) - - it("returns a table of the external dependencies found in the given build table", function() - local build_table = { - type = "builtin", - modules = { - module1 = { - libraries = { "foo1", "foo2" }, - }, - module2 = { - libraries = "foo3" - }, - } - } - - local extdeps = build_builtin.autodetect_external_dependencies(build_table) - assert.same(extdeps["FOO1"], { library = "foo1" }) - assert.same(extdeps["FOO2"], { library = "foo2" }) - assert.same(extdeps["FOO3"], { library = "foo3" }) - end) - - it("adds proper include and library dirs to the given build table", function() - local build_table - - build_table = { - type = "builtin", - modules = { - module1 = { - libraries = "foo" - } - } - } - build_builtin.autodetect_external_dependencies(build_table) - assert.same(build_table, { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - incdirs = { "$(FOO_INCDIR)" }, - libdirs = { "$(FOO_LIBDIR)" } - } - } - }) - - build_table = { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - incdirs = { "INCDIRS" } - } - } - } - build_builtin.autodetect_external_dependencies(build_table) - assert.same(build_table, { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - incdirs = { "INCDIRS" }, - libdirs = { "$(FOO_LIBDIR)" } - } - } - }) - - build_table = { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - libdirs = { "LIBDIRS" } - } - } - } - build_builtin.autodetect_external_dependencies(build_table) - assert.same(build_table, { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - incdirs = { "$(FOO_INCDIR)" }, - libdirs = { "LIBDIRS" } - } - } - }) - - build_table = { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - incdirs = { "INCDIRS" }, - libdirs = { "LIBDIRS" } - } - } - } - build_builtin.autodetect_external_dependencies(build_table) - assert.same(build_table, { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - incdirs = { "INCDIRS" }, - libdirs = { "LIBDIRS" } - } - } - }) - end) - end) - describe("builtin.autodetect_modules", function() local tmpdir local olddir diff --git a/spec/deps_spec.lua b/spec/deps_spec.lua index b0064298..32f68f51 100644 --- a/spec/deps_spec.lua +++ b/spec/deps_spec.lua @@ -116,3 +116,146 @@ describe("LuaRocks deps-mode #integration", function() assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/lxsh/${LXSH}/lxsh-${LXSH}.rockspec")) end) end) + +test_env.unload_luarocks() +test_env.setup_specs() +local cfg = require("luarocks.core.cfg") +local deps = require("luarocks.deps") +local fs = require("luarocks.fs") + +describe("LuaRocks deps #unit", function() + local runner + + lazy_setup(function() + runner = require("luacov.runner") + runner.init(testing_paths.testrun_dir .. "/luacov.config") + runner.tick = true + cfg.init() + fs.init() + deps.check_lua_incdir(cfg.variables) + deps.check_lua_libdir(cfg.variables) + end) + + lazy_teardown(function() + runner.shutdown() + end) + + describe("deps", function() + describe("deps.autodetect_external_dependencies", function() + it("returns false if the given build table has no external dependencies", function() + local build_table = { + type = "builtin" + } + + assert.falsy(deps.autodetect_external_dependencies(build_table)) + end) + + it("returns a table of the external dependencies found in the given build table", function() + local build_table = { + type = "builtin", + modules = { + module1 = { + libraries = { "foo1", "foo2" }, + }, + module2 = { + libraries = "foo3" + }, + } + } + + local extdeps = deps.autodetect_external_dependencies(build_table) + assert.same(extdeps["FOO1"], { library = "foo1" }) + assert.same(extdeps["FOO2"], { library = "foo2" }) + assert.same(extdeps["FOO3"], { library = "foo3" }) + end) + + it("adds proper include and library dirs to the given build table", function() + local build_table + + build_table = { + type = "builtin", + modules = { + module1 = { + libraries = "foo" + } + } + } + deps.autodetect_external_dependencies(build_table) + assert.same(build_table, { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + incdirs = { "$(FOO_INCDIR)" }, + libdirs = { "$(FOO_LIBDIR)" } + } + } + }) + + build_table = { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + incdirs = { "INCDIRS" } + } + } + } + deps.autodetect_external_dependencies(build_table) + assert.same(build_table, { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + incdirs = { "INCDIRS" }, + libdirs = { "$(FOO_LIBDIR)" } + } + } + }) + + build_table = { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + libdirs = { "LIBDIRS" } + } + } + } + deps.autodetect_external_dependencies(build_table) + assert.same(build_table, { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + incdirs = { "$(FOO_INCDIR)" }, + libdirs = { "LIBDIRS" } + } + } + }) + + build_table = { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + incdirs = { "INCDIRS" }, + libdirs = { "LIBDIRS" } + } + } + } + deps.autodetect_external_dependencies(build_table) + assert.same(build_table, { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + incdirs = { "INCDIRS" }, + libdirs = { "LIBDIRS" } + } + } + }) + end) + end) + end) +end) diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index 471de427..55242e60 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua @@ -111,18 +111,6 @@ local function process_dependencies(rockspec, opts) end end - local ok, err, errcode = deps.check_lua_incdir(rockspec.variables) - if not ok then - return nil, err, errcode - end - - if cfg.link_lua_explicitly then - local ok, err, errcode = deps.check_lua_libdir(rockspec.variables) - if not ok then - return nil, err, errcode - end - end - if opts.deps_mode == "none" then return true end @@ -165,11 +153,8 @@ local function process_dependencies(rockspec, opts) end end end - ok, err, errcode = deps.fulfill_dependencies(rockspec, "dependencies", opts.deps_mode, opts.verify) - if err then - return nil, err, errcode - end - return true + + return deps.fulfill_dependencies(rockspec, "dependencies", opts.deps_mode, opts.verify) end local function fetch_and_change_to_source_dir(rockspec, opts) @@ -241,6 +226,21 @@ local function run_build_driver(rockspec, no_install) if not pok or type(driver) ~= "table" then return nil, "Failed initializing build back-end for build type '"..btype.."': "..driver end + + if not driver.skip_lua_inc_lib_check then + local ok, err, errcode = deps.check_lua_incdir(rockspec.variables) + if not ok then + return nil, err, errcode + end + + if cfg.link_lua_explicitly then + local ok, err, errcode = deps.check_lua_libdir(rockspec.variables) + if not ok then + return nil, err, errcode + end + end + end + local ok, err = driver.run(rockspec, no_install) if not ok then return nil, "Build error: " .. err diff --git a/src/luarocks/build/builtin.lua b/src/luarocks/build/builtin.lua index 70210bab..c55b61a0 100644 --- a/src/luarocks/build/builtin.lua +++ b/src/luarocks/build/builtin.lua @@ -2,6 +2,11 @@ --- A builtin build system: back-end to provide a portable way of building C-based Lua modules. local builtin = {} +-- This build driver checks LUA_INCDIR and LUA_LIBDIR on demand, +-- so that pure-Lua rocks don't need to have development headers +-- installed. +builtin.skip_lua_inc_lib_check = true + local unpack = unpack or table.unpack local fs = require("luarocks.fs") @@ -9,38 +14,7 @@ local path = require("luarocks.path") local util = require("luarocks.util") local cfg = require("luarocks.core.cfg") local dir = require("luarocks.dir") - -function builtin.autodetect_external_dependencies(build) - if not build or not build.modules then - return nil - end - local extdeps = {} - local any = false - for _, data in pairs(build.modules) do - if type(data) == "table" and data.libraries then - local libraries = data.libraries - if type(libraries) == "string" then - libraries = { libraries } - end - local incdirs = {} - local libdirs = {} - for _, lib in ipairs(libraries) do - local upper = lib:upper():gsub("%+", "P"):gsub("[^%w]", "_") - any = true - extdeps[upper] = { library = lib } - table.insert(incdirs, "$(" .. upper .. "_INCDIR)") - table.insert(libdirs, "$(" .. upper .. "_LIBDIR)") - end - if not data.incdirs then - data.incdirs = incdirs - end - if not data.libdirs then - data.libdirs = libdirs - end - end - end - return any and extdeps or nil -end +local deps = require("luarocks.deps") local function autoextract_libs(external_dependencies, variables) if not external_dependencies then @@ -323,10 +297,16 @@ function builtin.run(rockspec, no_install) end if type(info) == "table" then if not checked_lua_h then - local lua_incdir, lua_h = variables.LUA_INCDIR, "lua.h" - if not fs.exists(dir.path(lua_incdir, lua_h)) then - return nil, "Lua header file "..lua_h.." not found (looked in "..lua_incdir.."). \n" .. - "You need to install the Lua development package for your system." + local ok, err, errcode = deps.check_lua_incdir(rockspec.variables) + if not ok then + return nil, err, errcode + end + + if cfg.link_lua_explicitly then + local ok, err, errcode = deps.check_lua_libdir(rockspec.variables) + if not ok then + return nil, err, errcode + end end checked_lua_h = true end diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua index 3cbe3852..3067f4ce 100644 --- a/src/luarocks/cmd.lua +++ b/src/luarocks/cmd.lua @@ -561,7 +561,7 @@ function cmd.run_command(description, commands, external_namespace, ...) util.warning("command module " .. module .. " does not implement command(), skipping") end else - util.warning("failed to load command module " .. module) + util.warning("failed to load command module " .. module .. ": " .. mod) end end diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua index 8af28327..2680b64b 100644 --- a/src/luarocks/deps.lua +++ b/src/luarocks/deps.lua @@ -10,7 +10,6 @@ local fun = require("luarocks.fun") local util = require("luarocks.util") local vers = require("luarocks.core.vers") local queries = require("luarocks.queries") -local builtin = require("luarocks.build.builtin") local deplocks = require("luarocks.deplocks") --- Generate a function that matches dep queries against the manifest, @@ -570,6 +569,40 @@ local function check_external_dependency(name, ext_files, vars, mode, cache) return nil, err_dirname, err_testfile, err_files end +function deps.autodetect_external_dependencies(build) + -- only applies to the 'builtin' build type + if not build or not build.modules then + return nil + end + + local extdeps = {} + local any = false + for _, data in pairs(build.modules) do + if type(data) == "table" and data.libraries then + local libraries = data.libraries + if type(libraries) == "string" then + libraries = { libraries } + end + local incdirs = {} + local libdirs = {} + for _, lib in ipairs(libraries) do + local upper = lib:upper():gsub("%+", "P"):gsub("[^%w]", "_") + any = true + extdeps[upper] = { library = lib } + table.insert(incdirs, "$(" .. upper .. "_INCDIR)") + table.insert(libdirs, "$(" .. upper .. "_LIBDIR)") + end + if not data.incdirs then + data.incdirs = incdirs + end + if not data.libdirs then + data.libdirs = libdirs + end + end + end + return any and extdeps or nil +end + --- Set up path-related variables for external dependencies. -- For each key in the external_dependencies table in the -- rockspec file, four variables are created: _DIR, _BINDIR, @@ -587,7 +620,7 @@ function deps.check_external_deps(rockspec, mode) assert(rockspec:type() == "rockspec") if not rockspec.external_dependencies then - rockspec.external_dependencies = builtin.autodetect_external_dependencies(rockspec.build) + rockspec.external_dependencies = deps.autodetect_external_dependencies(rockspec.build) end if not rockspec.external_dependencies then return true @@ -706,16 +739,25 @@ local function find_lua_incdir(prefix, luaver, luajitver) end function deps.check_lua_incdir(vars) + if vars.LUA_INCDIR_OK == true + then return true + end + local ljv = util.get_luajit_version() if vars.LUA_INCDIR then - return lua_h_exists(vars.LUA_INCDIR, cfg.lua_version) + local ok, err = lua_h_exists(vars.LUA_INCDIR, cfg.lua_version) + if ok then + vars.LUA_INCDIR_OK = true + end + return ok, err end if vars.LUA_DIR then local d, err = find_lua_incdir(vars.LUA_DIR, cfg.lua_version, ljv) if d then vars.LUA_INCDIR = d + vars.LUA_INCDIR_OK = true return true end return nil, err @@ -725,10 +767,15 @@ function deps.check_lua_incdir(vars) end function deps.check_lua_libdir(vars) + if vars.LUA_LIBDIR_OK == true + then return true + end + local fs = require("luarocks.fs") local ljv = util.get_luajit_version() if vars.LUA_LIBDIR and vars.LUALIB and fs.exists(dir.path(vars.LUA_LIBDIR, vars.LUALIB)) then + vars.LUA_LIBDIR_OK = true return true end @@ -768,6 +815,7 @@ function deps.check_lua_libdir(vars) if ok then vars.LUALIB = vars.LUA_LIBDIR_FILE + vars.LUA_LIBDIR_OK = true return true else err = err or "Failed finding Lua library. You may need to configure LUA_LIBDIR." -- cgit v1.2.3-55-g6feb