aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-08-22 17:48:58 -0300
committerHisham Muhammad <hisham@gobolinux.org>2024-10-21 13:30:51 -0300
commit7aaed945f73fc1bd5da8c5c7510a637f38aae71a (patch)
treec6a38771f2058b9aeae4aac001a70ef48980cd40
parente184a7a138ce887fffd96b27c8d9d445c11f9483 (diff)
downloadluarocks-7aaed945f73fc1bd5da8c5c7510a637f38aae71a.tar.gz
luarocks-7aaed945f73fc1bd5da8c5c7510a637f38aae71a.tar.bz2
luarocks-7aaed945f73fc1bd5da8c5c7510a637f38aae71a.zip
Teal: convert luarocks.build
-rw-r--r--src/luarocks/build.tl (renamed from src/luarocks/build.lua)147
1 files changed, 66 insertions, 81 deletions
diff --git a/src/luarocks/build.lua b/src/luarocks/build.tl
index 56e5ebd4..7fbe021f 100644
--- a/src/luarocks/build.lua
+++ b/src/luarocks/build.tl
@@ -1,5 +1,10 @@
1 1
2local build = {} 2local record build
3 record Builder
4 skip_lua_inc_lib_check: boolean
5 run: function(Rockspec, boolean): boolean, string, string
6 end
7end
3 8
4local path = require("luarocks.path") 9local path = require("luarocks.path")
5local util = require("luarocks.util") 10local util = require("luarocks.util")
@@ -11,28 +16,26 @@ local deps = require("luarocks.deps")
11local cfg = require("luarocks.core.cfg") 16local cfg = require("luarocks.core.cfg")
12local vers = require("luarocks.core.vers") 17local vers = require("luarocks.core.vers")
13local repos = require("luarocks.repos") 18local repos = require("luarocks.repos")
14local writer = require("luarocks.manif.writer") 19local repo_writer = require("luarocks.repo_writer")
15local deplocks = require("luarocks.deplocks") 20local deplocks = require("luarocks.deplocks")
16 21
17build.opts = util.opts_table("build.opts", { 22local type Rockspec = require("luarocks.core.types.rockspec").Rockspec
18 need_to_fetch = "boolean", 23
19 minimal_mode = "boolean", 24local type InstallDirs = require("luarocks.core.types.installs").InstallDirs
20 deps_mode = "string", 25local type InstallDir = require("luarocks.core.types.installs").InstallDir
21 build_only_deps = "boolean", 26
22 namespace = "string?", 27local type Tree = require("luarocks.core.types.tree").Tree
23 branch = "string?", 28
24 verify = "boolean", 29local type BOpts = require("luarocks.core.types.bopts").BOpts
25 check_lua_versions = "boolean", 30
26 pin = "boolean", 31
27 rebuild = "boolean", 32local type Builder = build.Builder
28 no_install = "boolean"
29})
30 33
31do 34do
32 --- Write to the current directory the contents of a table, 35 --- Write to the current directory the contents of a table,
33 -- where each key is a file name and its value is the file content. 36 -- where each key is a file name and its value is the file content.
34 -- @param files table: The table of files to be written. 37 -- @param files table: The table of files to be written.
35 local function extract_from_rockspec(files) 38 local function extract_from_rockspec(files: {string: string})
36 for name, content in pairs(files) do 39 for name, content in pairs(files) do
37 local fd = io.open(dir.path(fs.current_dir(), name), "w+") 40 local fd = io.open(dir.path(fs.current_dir(), name), "w+")
38 fd:write(content) 41 fd:write(content)
@@ -46,8 +49,7 @@ do
46 -- @param rockspec table: A rockspec table. 49 -- @param rockspec table: A rockspec table.
47 -- @return boolean or (nil, string): True if succeeded or 50 -- @return boolean or (nil, string): True if succeeded or
48 -- nil and an error message. 51 -- nil and an error message.
49 function build.apply_patches(rockspec) 52 function build.apply_patches(rockspec: Rockspec): boolean, string
50 assert(rockspec:type() == "rockspec")
51 53
52 if not (rockspec.build.extra_files or rockspec.build.patches) then 54 if not (rockspec.build.extra_files or rockspec.build.patches) then
53 return true 55 return true
@@ -82,13 +84,14 @@ do
82 end 84 end
83end 85end
84 86
85local function check_macosx_deployment_target(rockspec) 87local function check_macosx_deployment_target(rockspec: Rockspec): boolean, string
86 local target = rockspec.build.macosx_deployment_target 88 local target = rockspec.build.macosx_deployment_target
87 local function patch_variable(var) 89 local function patch_variable(var: string)
88 if rockspec.variables[var]:match("MACOSX_DEPLOYMENT_TARGET") then 90 local rockspec_variables = rockspec.variables
89 rockspec.variables[var] = (rockspec.variables[var]):gsub("MACOSX_DEPLOYMENT_TARGET=[^ ]*", "MACOSX_DEPLOYMENT_TARGET="..target) 91 if rockspec_variables[var]:match("MACOSX_DEPLOYMENT_TARGET") then
92 rockspec_variables[var] = (rockspec_variables[var]):gsub("MACOSX_DEPLOYMENT_TARGET=[^ ]*", "MACOSX_DEPLOYMENT_TARGET="..target)
90 else 93 else
91 rockspec.variables[var] = "env MACOSX_DEPLOYMENT_TARGET="..target.." "..rockspec.variables[var] 94 rockspec_variables[var] = "env MACOSX_DEPLOYMENT_TARGET="..target.." "..rockspec_variables[var]
92 end 95 end
93 end 96 end
94 if cfg.is_platform("macosx") and rockspec:format_is_at_least("3.0") and target then 97 if cfg.is_platform("macosx") and rockspec:format_is_at_least("3.0") and target then
@@ -104,7 +107,7 @@ local function check_macosx_deployment_target(rockspec)
104 return true 107 return true
105end 108end
106 109
107local function process_dependencies(rockspec, opts, cwd) 110local function process_dependencies(rockspec: Rockspec, opts: BOpts, cwd: string): boolean, string, string
108 if not opts.build_only_deps then 111 if not opts.build_only_deps then
109 local ok, err, errcode = deps.check_external_deps(rockspec, "build") 112 local ok, err, errcode = deps.check_external_deps(rockspec, "build")
110 if err then 113 if err then
@@ -135,7 +138,7 @@ local function process_dependencies(rockspec, opts, cwd)
135 cfg.lua_modules_path = cfg.lua_modules_path:gsub(user_lua_version:gsub("%.", "%%."), running_lua_version) 138 cfg.lua_modules_path = cfg.lua_modules_path:gsub(user_lua_version:gsub("%.", "%%."), running_lua_version)
136 cfg.lib_modules_path = cfg.lib_modules_path:gsub(user_lua_version:gsub("%.", "%%."), running_lua_version) 139 cfg.lib_modules_path = cfg.lib_modules_path:gsub(user_lua_version:gsub("%.", "%%."), running_lua_version)
137 cfg.rocks_subdir = cfg.rocks_subdir:gsub(user_lua_version:gsub("%.", "%%."), running_lua_version) 140 cfg.rocks_subdir = cfg.rocks_subdir:gsub(user_lua_version:gsub("%.", "%%."), running_lua_version)
138 path.use_tree(cfg.root_dir) 141 path.use_tree(cfg.root_dir as Tree)
139 end 142 end
140 143
141 local ok, err, errcode = deps.fulfill_dependencies(rockspec, "build_dependencies", "all", opts.verify, deplock_dir) 144 local ok, err, errcode = deps.fulfill_dependencies(rockspec, "build_dependencies", "all", opts.verify, deplock_dir)
@@ -148,7 +151,7 @@ local function process_dependencies(rockspec, opts, cwd)
148 cfg.lua_modules_path = cfg.lua_modules_path:gsub(running_lua_version:gsub("%.", "%%."), user_lua_version) 151 cfg.lua_modules_path = cfg.lua_modules_path:gsub(running_lua_version:gsub("%.", "%%."), user_lua_version)
149 cfg.lib_modules_path = cfg.lib_modules_path:gsub(running_lua_version:gsub("%.", "%%."), user_lua_version) 152 cfg.lib_modules_path = cfg.lib_modules_path:gsub(running_lua_version:gsub("%.", "%%."), user_lua_version)
150 cfg.rocks_subdir = cfg.rocks_subdir:gsub(running_lua_version:gsub("%.", "%%."), user_lua_version) 153 cfg.rocks_subdir = cfg.rocks_subdir:gsub(running_lua_version:gsub("%.", "%%."), user_lua_version)
151 path.use_tree(cfg.root_dir) 154 path.use_tree(cfg.root_dir as Tree)
152 end 155 end
153 156
154 if err then 157 if err then
@@ -160,7 +163,7 @@ local function process_dependencies(rockspec, opts, cwd)
160 return deps.fulfill_dependencies(rockspec, "dependencies", opts.deps_mode, opts.verify, deplock_dir) 163 return deps.fulfill_dependencies(rockspec, "dependencies", opts.deps_mode, opts.verify, deplock_dir)
161end 164end
162 165
163local function fetch_and_change_to_source_dir(rockspec, opts) 166local function fetch_and_change_to_source_dir(rockspec: Rockspec, opts: BOpts): boolean, string, string
164 if opts.minimal_mode or opts.build_only_deps then 167 if opts.minimal_mode or opts.build_only_deps then
165 return true 168 return true
166 end 169 end
@@ -168,11 +171,11 @@ local function fetch_and_change_to_source_dir(rockspec, opts)
168 if opts.branch then 171 if opts.branch then
169 rockspec.source.branch = opts.branch 172 rockspec.source.branch = opts.branch
170 end 173 end
171 local ok, source_dir, errcode = fetch.fetch_sources(rockspec, true) 174 local oks, source_dir, errcode = fetch.fetch_sources(rockspec, true)
172 if not ok then 175 if not oks then
173 return nil, source_dir, errcode 176 return nil, source_dir, errcode
174 end 177 end
175 local err 178 local ok, err: boolean, string
176 ok, err = fs.change_dir(source_dir) 179 ok, err = fs.change_dir(source_dir)
177 if not ok then 180 if not ok then
178 return nil, err 181 return nil, err
@@ -193,15 +196,15 @@ local function fetch_and_change_to_source_dir(rockspec, opts)
193 return true 196 return true
194end 197end
195 198
196local function prepare_install_dirs(name, version) 199local function prepare_install_dirs(name: string, version: string): InstallDirs, string
197 local dirs = { 200 local dirs: InstallDirs = {
198 lua = { name = path.lua_dir(name, version), is_module_path = true, perms = "read" }, 201 lua = { name = path.lua_dir(name, version), is_module_path = true, perms = "read" },
199 lib = { name = path.lib_dir(name, version), is_module_path = true, perms = "exec" }, 202 lib = { name = path.lib_dir(name, version), is_module_path = true, perms = "exec" },
200 bin = { name = path.bin_dir(name, version), is_module_path = false, perms = "exec" }, 203 bin = { name = path.bin_dir(name, version), is_module_path = false, perms = "exec" },
201 conf = { name = path.conf_dir(name, version), is_module_path = false, perms = "read" }, 204 conf = { name = path.conf_dir(name, version), is_module_path = false, perms = "read" },
202 } 205 }
203 206
204 for _, d in pairs(dirs) do 207 for _, d in pairs(dirs as {string: InstallDir}) do
205 local ok, err = fs.make_dir(d.name) 208 local ok, err = fs.make_dir(d.name)
206 if not ok then 209 if not ok then
207 return nil, err 210 return nil, err
@@ -211,7 +214,7 @@ local function prepare_install_dirs(name, version)
211 return dirs 214 return dirs
212end 215end
213 216
214local function run_build_driver(rockspec, no_install) 217local function run_build_driver(rockspec: Rockspec, no_install: boolean): boolean, string, string
215 local btype = rockspec.build.type 218 local btype = rockspec.build.type
216 if btype == "none" then 219 if btype == "none" then
217 return true 220 return true
@@ -222,12 +225,15 @@ local function run_build_driver(rockspec, no_install)
222 btype = "builtin" 225 btype = "builtin"
223 rockspec.build.type = btype 226 rockspec.build.type = btype
224 end 227 end
228 local driver: Builder
225 if cfg.accepted_build_types and not fun.contains(cfg.accepted_build_types, btype) then 229 if cfg.accepted_build_types and not fun.contains(cfg.accepted_build_types, btype) then
226 return nil, "This rockspec uses the '"..btype.."' build type, which is blocked by the 'accepted_build_types' setting in your LuaRocks configuration." 230 return nil, "This rockspec uses the '"..btype.."' build type, which is blocked by the 'accepted_build_types' setting in your LuaRocks configuration."
227 end 231 end
228 local pok, driver = pcall(require, "luarocks.build." .. btype) 232 local pok, driver_str = pcall(require, "luarocks.build." .. btype) as (boolean, Builder | string)
229 if not pok or type(driver) ~= "table" then 233 if not driver_str is Builder then
230 return nil, "Failed initializing build back-end for build type '"..btype.."': "..driver 234 return nil, "Failed initializing build back-end for build type '"..btype.."': "..driver_str
235 else
236 driver = driver_str as Builder
231 end 237 end
232 238
233 if not driver.skip_lua_inc_lib_check then 239 if not driver.skip_lua_inc_lib_check then
@@ -237,7 +243,7 @@ local function run_build_driver(rockspec, no_install)
237 end 243 end
238 244
239 if cfg.link_lua_explicitly then 245 if cfg.link_lua_explicitly then
240 local ok, err, errcode = deps.check_lua_libdir(rockspec.variables) 246 ok, err, errcode = deps.check_lua_libdir(rockspec.variables)
241 if not ok then 247 if not ok then
242 return nil, err, errcode 248 return nil, err, errcode
243 end 249 end
@@ -251,7 +257,7 @@ local function run_build_driver(rockspec, no_install)
251 return true 257 return true
252end 258end
253 259
254local install_files 260local install_files: function(Rockspec, InstallDirs): boolean, string
255do 261do
256 --- Install files to a given location. 262 --- Install files to a given location.
257 -- Takes a table where the array part is a list of filenames to be copied. 263 -- Takes a table where the array part is a list of filenames to be copied.
@@ -270,16 +276,14 @@ do
270 -- Directories are always created with the default permissions. 276 -- Directories are always created with the default permissions.
271 -- @return boolean or (nil, string): True if succeeded or 277 -- @return boolean or (nil, string): True if succeeded or
272 -- nil and an error message. 278 -- nil and an error message.
273 local function install_to(files, location, is_module_path, perms) 279 local function install_to(files: {string: string}, location: string, is_module_path: boolean, perms: string): boolean, string
274 assert(type(files) == "table" or not files)
275 assert(type(location) == "string")
276 if not files then 280 if not files then
277 return true 281 return true
278 end 282 end
279 for k, file in pairs(files) do 283 for k, file in pairs(files) do
280 local dest = location 284 local dest = location
281 local filename = dir.base_name(file) 285 local filename = dir.base_name(file)
282 if type(k) == "string" then 286 if k is string then
283 local modname = k 287 local modname = k
284 if is_module_path then 288 if is_module_path then
285 dest = dir.path(location, path.module_to_path(modname)) 289 dest = dir.path(location, path.module_to_path(modname))
@@ -307,7 +311,7 @@ do
307 return true 311 return true
308 end 312 end
309 313
310 local function install_default_docs(name, version) 314 local function install_default_docs(name: string, version: string)
311 local patterns = { "readme", "license", "copying", ".*%.md" } 315 local patterns = { "readme", "license", "copying", ".*%.md" }
312 local dest = dir.path(path.install_dir(name, version), "doc") 316 local dest = dir.path(path.install_dir(name, version), "doc")
313 local has_dir = false 317 local has_dir = false
@@ -325,12 +329,12 @@ do
325 end 329 end
326 end 330 end
327 331
328 install_files = function(rockspec, dirs) 332 install_files = function(rockspec: Rockspec, dirs: InstallDirs): boolean, string
329 local name, version = rockspec.name, rockspec.version 333 local name, version = rockspec.name, rockspec.version
330 334
331 if rockspec.build.install then 335 if rockspec.build.install then
332 for k, d in pairs(dirs) do 336 for k, d in pairs(dirs as {string: InstallDir}) do
333 local ok, err = install_to(rockspec.build.install[k], d.name, d.is_module_path, d.perms) 337 local ok, err = install_to((rockspec.build.install as {string: {string: string}})[k], d.name, d.is_module_path, d.perms)
334 if not ok then return nil, err end 338 if not ok then return nil, err end
335 end 339 end
336 end 340 end
@@ -343,7 +347,7 @@ do
343 end 347 end
344 348
345 local any_docs = false 349 local any_docs = false
346 for _, copy_dir in pairs(copy_directories) do 350 for _, copy_dir in ipairs(copy_directories) do
347 if fs.is_dir(copy_dir) then 351 if fs.is_dir(copy_dir) then
348 local dest = dir.path(path.install_dir(name, version), copy_dir) 352 local dest = dir.path(path.install_dir(name, version), copy_dir)
349 fs.make_dir(dest) 353 fs.make_dir(dest)
@@ -363,35 +367,12 @@ do
363 end 367 end
364end 368end
365 369
366local function write_rock_dir_files(rockspec, opts)
367 local name, version = rockspec.name, rockspec.version
368
369 fs.copy(rockspec.local_abs_filename, path.rockspec_file(name, version), "read")
370
371 local deplock_file = deplocks.get_abs_filename(rockspec.name)
372 if deplock_file then
373 fs.copy(deplock_file, dir.path(path.install_dir(name, version), "luarocks.lock"), "read")
374 end
375
376 local ok, err = writer.make_rock_manifest(name, version)
377 if not ok then return nil, err end
378
379 ok, err = writer.make_namespace_file(name, version, opts.namespace)
380 if not ok then return nil, err end
381
382 return true
383end
384
385--- Build and install a rock given a rockspec. 370--- Build and install a rock given a rockspec.
386-- @param rockspec rockspec: the rockspec to build 371-- @param rockspec rockspec: the rockspec to build
387-- @param opts table: build options table 372-- @param opts table: build options table
388-- @param cwd string or nil: The current working directory 373-- @param cwd string or nil: The current working directory
389-- @return (string, string) or (nil, string, [string]): Name and version of 374-- @return: Name and version of installed rock if succeeded or nil and an error message.
390-- installed rock if succeeded or nil and an error message followed by an error code. 375function build.build_rockspec(rockspec: Rockspec, opts: BOpts, cwd: string): string, string
391function build.build_rockspec(rockspec, opts, cwd)
392 assert(rockspec:type() == "rockspec")
393 assert(opts:type() == "build.opts")
394 assert(type(cwd) == "string" or type(cwd) == nil)
395 376
396 cwd = cwd or dir.path(".") 377 cwd = cwd or dir.path(".")
397 378
@@ -431,11 +412,11 @@ function build.build_rockspec(rockspec, opts, cwd)
431 return name, version 412 return name, version
432 end 413 end
433 414
434 local dirs, err 415 local dirs, err: InstallDirs, string
435 local rollback 416 local rollback: util.Fn
436 if not opts.no_install then 417 if not opts.no_install then
437 if repos.is_installed(name, version) then 418 if repos.is_installed(name, version) then
438 repos.delete_version(name, version, opts.deps_mode) 419 repo_writer.delete_version(name, version, opts.deps_mode)
439 end 420 end
440 421
441 dirs, err = prepare_install_dirs(name, version) 422 dirs, err = prepare_install_dirs(name, version)
@@ -467,7 +448,7 @@ function build.build_rockspec(rockspec, opts, cwd)
467 ok, err = install_files(rockspec, dirs) 448 ok, err = install_files(rockspec, dirs)
468 if not ok then return nil, err end 449 if not ok then return nil, err end
469 450
470 for _, d in pairs(dirs) do 451 for _, d in pairs(dirs as {string: InstallDir}) do
471 fs.remove_dir_if_empty(d.name) 452 fs.remove_dir_if_empty(d.name)
472 end 453 end
473 454
@@ -480,15 +461,19 @@ function build.build_rockspec(rockspec, opts, cwd)
480 deplocks.write_file() 461 deplocks.write_file()
481 end 462 end
482 463
483 ok, err = write_rock_dir_files(rockspec, opts) 464 fs.copy(rockspec.local_abs_filename, path.rockspec_file(name, version), "read")
484 if not ok then return nil, err end 465
466 local deplock_file = deplocks.get_abs_filename(name)
467 if deplock_file then
468 fs.copy(deplock_file, dir.path(path.install_dir(name, version), "luarocks.lock"), "read")
469 end
485 470
486 ok, err = repos.deploy_files(name, version, repos.should_wrap_bin_scripts(rockspec), opts.deps_mode) 471 ok, err = repo_writer.deploy_files(name, version, repos.should_wrap_bin_scripts(rockspec), opts.deps_mode, opts.namespace)
487 if not ok then return nil, err end 472 if not ok then return nil, err end
488 473
489 util.remove_scheduled_function(rollback) 474 util.remove_scheduled_function(rollback)
490 rollback = util.schedule_function(function() 475 rollback = util.schedule_function(function()
491 repos.delete_version(name, version, opts.deps_mode) 476 repo_writer.delete_version(name, version, opts.deps_mode)
492 end) 477 end)
493 478
494 ok, err = repos.run_hook(rockspec, "post_install") 479 ok, err = repos.run_hook(rockspec, "post_install")