From 4cfcf9d8df1abd7cca9f2c3590402ada543df327 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 31 Oct 2023 21:28:45 -0300 Subject: feat: auto-add luarocks-build- build dependency (#1542) --- spec/rockspecs_spec.lua | 126 +++++++++++++++++++++++++++++++++++++++++++++ src/luarocks/rockspecs.lua | 30 +++++++++++ 2 files changed, 156 insertions(+) create mode 100644 spec/rockspecs_spec.lua diff --git a/spec/rockspecs_spec.lua b/spec/rockspecs_spec.lua new file mode 100644 index 00000000..76b33f65 --- /dev/null +++ b/spec/rockspecs_spec.lua @@ -0,0 +1,126 @@ + +local rockspecs = require("luarocks.rockspecs") +local cfg = require("luarocks.core.cfg") +local test_env = require("spec.util.test_env") +local lfs = require("lfs") + +describe("luarocks.rockspecs", function() + + setup(function() + cfg.init() + end) + + it("auto adds a build dependency for non-vendored build types", function() + local filename = "test-1.0-1.rockspec" + local rockspec = { + package = "test", + source = { + url = "", + }, + build = { + type = "foo" + }, + } + local globals = {} + local quick = true + + local out = rockspecs.from_persisted_table(filename, rockspec, globals, quick) + + assert(rockspec == out) + assert.same(rockspec.build_dependencies, { + { name = "luarocks-build-foo", constraints = {} }, + }) + end) + + it("does not add a build dependency for non-vendored build type if it's already ther", function() + local filename = "test-1.0-1.rockspec" + local rockspec = { + package = "test", + source = { + url = "", + }, + build_dependencies = { + "luarocks-build-cpp >= 1.0", + }, + build = { + type = "cpp" + }, + } + local globals = {} + local quick = true + + local out = rockspecs.from_persisted_table(filename, rockspec, globals, quick) + + assert(rockspec == out) + + assert.same(rockspec.build_dependencies, { + { name = "luarocks-build-cpp", constraints = { { op = ">=", version = { string = "1.0", 1, 0 } } } }, + }) + end) + + it("does not add a build dependency for 'none' build type", function() + local filename = "test-1.0-1.rockspec" + local rockspec = { + package = "test", + source = { + url = "", + }, + build = { + type = "none" + }, + } + local globals = {} + local quick = true + + local out = rockspecs.from_persisted_table(filename, rockspec, globals, quick) + + assert(rockspec == out) + assert.same(rockspec.build_dependencies, {}) + end) + + it("does not add a build dependency for 'module' build type", function() + local filename = "test-1.0-1.rockspec" + local rockspec = { + package = "test", + source = { + url = "", + }, + build = { + type = "none" + }, + } + local globals = {} + local quick = true + + local out = rockspecs.from_persisted_table(filename, rockspec, globals, quick) + + assert(rockspec == out) + assert.same(rockspec.build_dependencies, {}) + end) + + for d in lfs.dir(test_env.testing_paths.src_dir .. "/luarocks/build") do + local name = d:match("(.*)%.lua") + if name then + it("does not add a build dependency for vendored '" .. name .. "' type", function() + local filename = "test-1.0-1.rockspec" + local rockspec = { + package = "test", + source = { + url = "", + }, + build = { + type = name + }, + } + local globals = {} + local quick = true + + local out = rockspecs.from_persisted_table(filename, rockspec, globals, quick) + + assert(rockspec == out) + assert.same(rockspec.build_dependencies, {}) + end) + end + end + +end) diff --git a/src/luarocks/rockspecs.lua b/src/luarocks/rockspecs.lua index 94462951..c9e17530 100644 --- a/src/luarocks/rockspecs.lua +++ b/src/luarocks/rockspecs.lua @@ -8,6 +8,15 @@ local type_rockspec = require("luarocks.type.rockspec") local util = require("luarocks.util") local vers = require("luarocks.core.vers") +local vendored_build_type_set = { + ["builtin"] = true, + ["cmake"] = true, + ["command"] = true, + ["make"] = true, + ["module"] = true, -- compatibility alias + ["none"] = true, +} + local rockspec_mt = {} rockspec_mt.__index = rockspec_mt @@ -151,6 +160,27 @@ function rockspecs.from_persisted_table(filename, rockspec, globals, quick) end end + if rockspec.build + and rockspec.build.type + and not vendored_build_type_set[rockspec.build.type] then + local build_pkg_name = "luarocks-build-" .. rockspec.build.type + if not rockspec.build_dependencies then + rockspec.build_dependencies = {} + end + + local found = false + for _, dep in ipairs(rockspec.build_dependencies) do + if dep.name == build_pkg_name then + found = true + break + end + end + + if not found then + table.insert(rockspec.build_dependencies, queries.from_dep_string(build_pkg_name)) + end + end + if not quick then configure_paths(rockspec) end -- cgit v1.2.3-55-g6feb