diff options
author | V1K1NGbg <victor@ilchev.com> | 2024-08-22 17:48:59 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2024-10-21 13:30:51 -0300 |
commit | ca858c676ec8147dcae59a1d383ca4bfb912aea5 (patch) | |
tree | a3f84087aaf94d951b49f8933ef7366ddd8200f8 | |
parent | 9d56fce973f186c09587077f8650f0b24592e8de (diff) | |
download | luarocks-ca858c676ec8147dcae59a1d383ca4bfb912aea5.tar.gz luarocks-ca858c676ec8147dcae59a1d383ca4bfb912aea5.tar.bz2 luarocks-ca858c676ec8147dcae59a1d383ca4bfb912aea5.zip |
Teal: convert luarocks.build.builtin
-rw-r--r-- | src/luarocks/build/builtin.tl (renamed from src/luarocks/build/builtin.lua) | 106 |
1 files changed, 57 insertions, 49 deletions
diff --git a/src/luarocks/build/builtin.lua b/src/luarocks/build/builtin.tl index 4c15d2bf..51d75900 100644 --- a/src/luarocks/build/builtin.lua +++ b/src/luarocks/build/builtin.tl | |||
@@ -1,13 +1,20 @@ | |||
1 | 1 | ||
2 | --- A builtin build system: back-end to provide a portable way of building C-based Lua modules. | 2 | --- A builtin build system: back-end to provide a portable way of building C-based Lua modules. |
3 | local builtin = {} | 3 | local record builtin |
4 | skip_lua_inc_lib_check: boolean | ||
5 | end | ||
6 | |||
7 | local type Rockspec = require("luarocks.core.types.rockspec").Rockspec | ||
8 | |||
9 | local type BuiltinBuild = require("luarocks.core.types.build").BuiltinBuild | ||
10 | local type Build = require("luarocks.core.types.build").Build | ||
11 | local type Module = BuiltinBuild.Module | ||
4 | 12 | ||
5 | -- This build driver checks LUA_INCDIR and LUA_LIBDIR on demand, | 13 | -- This build driver checks LUA_INCDIR and LUA_LIBDIR on demand, |
6 | -- so that pure-Lua rocks don't need to have development headers | 14 | -- so that pure-Lua rocks don't need to have development headers |
7 | -- installed. | 15 | -- installed. |
8 | builtin.skip_lua_inc_lib_check = true | 16 | builtin.skip_lua_inc_lib_check = true |
9 | 17 | ||
10 | local unpack = unpack or table.unpack | ||
11 | local dir_sep = package.config:sub(1, 1) | 18 | local dir_sep = package.config:sub(1, 1) |
12 | 19 | ||
13 | local fs = require("luarocks.fs") | 20 | local fs = require("luarocks.fs") |
@@ -17,13 +24,13 @@ local cfg = require("luarocks.core.cfg") | |||
17 | local dir = require("luarocks.dir") | 24 | local dir = require("luarocks.dir") |
18 | local deps = require("luarocks.deps") | 25 | local deps = require("luarocks.deps") |
19 | 26 | ||
20 | local function autoextract_libs(external_dependencies, variables) | 27 | local function autoextract_libs(external_dependencies: {string: {string: string}}, variables: {string: string}): {string}, {string}, {string} |
21 | if not external_dependencies then | 28 | if not external_dependencies then |
22 | return nil, nil, nil | 29 | return nil, nil, nil |
23 | end | 30 | end |
24 | local libs = {} | 31 | local libs: {string} = {} |
25 | local incdirs = {} | 32 | local incdirs: {string} = {} |
26 | local libdirs = {} | 33 | local libdirs: {string} = {} |
27 | for name, data in pairs(external_dependencies) do | 34 | for name, data in pairs(external_dependencies) do |
28 | if data.library then | 35 | if data.library then |
29 | table.insert(libs, data.library) | 36 | table.insert(libs, data.library) |
@@ -35,7 +42,7 @@ local function autoextract_libs(external_dependencies, variables) | |||
35 | end | 42 | end |
36 | 43 | ||
37 | do | 44 | do |
38 | local function get_cmod_name(file) | 45 | local function get_cmod_name(file: string): string |
39 | local fd = io.open(dir.path(fs.current_dir(), file), "r") | 46 | local fd = io.open(dir.path(fs.current_dir(), file), "r") |
40 | if not fd then return nil end | 47 | if not fd then return nil end |
41 | local data = fd:read("*a") | 48 | local data = fd:read("*a") |
@@ -43,7 +50,7 @@ do | |||
43 | return (data:match("int%s+luaopen_([a-zA-Z0-9_]+)")) | 50 | return (data:match("int%s+luaopen_([a-zA-Z0-9_]+)")) |
44 | end | 51 | end |
45 | 52 | ||
46 | local skiplist = { | 53 | local skiplist: {string: boolean} = { |
47 | ["spec"] = true, | 54 | ["spec"] = true, |
48 | [".luarocks"] = true, | 55 | [".luarocks"] = true, |
49 | ["lua_modules"] = true, | 56 | ["lua_modules"] = true, |
@@ -51,10 +58,10 @@ do | |||
51 | ["tests.lua"] = true, | 58 | ["tests.lua"] = true, |
52 | } | 59 | } |
53 | 60 | ||
54 | function builtin.autodetect_modules(libs, incdirs, libdirs) | 61 | function builtin.autodetect_modules(libs: {string}, incdirs: {string}, libdirs: {string}): {string : string | Module}, Build.Install, {string} |
55 | local modules = {} | 62 | local modules: {string: (string | Module)} = {} |
56 | local install | 63 | local install: Build.Install |
57 | local copy_directories | 64 | local copy_directories: {string} |
58 | 65 | ||
59 | local prefix = "" | 66 | local prefix = "" |
60 | for _, parent in ipairs({"src", "lua", "lib"}) do | 67 | for _, parent in ipairs({"src", "lua", "lib"}) do |
@@ -74,7 +81,7 @@ do | |||
74 | else | 81 | else |
75 | local cmod = file:match("(.*)%.c$") | 82 | local cmod = file:match("(.*)%.c$") |
76 | if cmod then | 83 | if cmod then |
77 | local modname = get_cmod_name(file) or path.path_to_module(file:gsub("%.c$", ".lua")) | 84 | local modname = get_cmod_name(file) or path.path_to_module((file:gsub("%.c$", ".lua"))) |
78 | modules[modname] = { | 85 | modules[modname] = { |
79 | sources = prefix..file, | 86 | sources = prefix..file, |
80 | libraries = libs, | 87 | libraries = libs, |
@@ -95,7 +102,7 @@ do | |||
95 | if bindir then | 102 | if bindir then |
96 | install = { bin = {} } | 103 | install = { bin = {} } |
97 | for _, file in ipairs(fs.list_dir(bindir)) do | 104 | for _, file in ipairs(fs.list_dir(bindir)) do |
98 | table.insert(install.bin, dir.path(bindir, file)) | 105 | table.insert((install.bin as {string}), dir.path(bindir, file)) |
99 | end | 106 | end |
100 | end | 107 | end |
101 | 108 | ||
@@ -115,7 +122,7 @@ end | |||
115 | --- Run a command displaying its execution on standard output. | 122 | --- Run a command displaying its execution on standard output. |
116 | -- @return boolean: true if command succeeds (status code 0), false | 123 | -- @return boolean: true if command succeeds (status code 0), false |
117 | -- otherwise. | 124 | -- otherwise. |
118 | local function execute(...) | 125 | local function execute(...: string): boolean, string, string |
119 | io.stdout:write(table.concat({...}, " ").."\n") | 126 | io.stdout:write(table.concat({...}, " ").."\n") |
120 | return fs.execute(...) | 127 | return fs.execute(...) |
121 | end | 128 | end |
@@ -124,11 +131,12 @@ end | |||
124 | -- @param rockspec table: the loaded rockspec. | 131 | -- @param rockspec table: the loaded rockspec. |
125 | -- @return boolean or (nil, string): true if no errors occurred, | 132 | -- @return boolean or (nil, string): true if no errors occurred, |
126 | -- nil and an error message otherwise. | 133 | -- nil and an error message otherwise. |
127 | function builtin.run(rockspec, no_install) | 134 | function builtin.run(rockspec: Rockspec, no_install: boolean): boolean, string, string |
128 | assert(rockspec:type() == "rockspec") | 135 | local compile_object: function(string, string, {string}, {string}): boolean, string, string |
129 | local compile_object, compile_library, compile_static_library | 136 | local compile_library: function(string, {string}, {string}, {string}, string): boolean, string, string |
137 | local compile_static_library: function(string, {string}, {string}, {string}, string): boolean, string, string | ||
130 | 138 | ||
131 | local build = rockspec.build | 139 | local build = rockspec.build as BuiltinBuild |
132 | local variables = rockspec.variables | 140 | local variables = rockspec.variables |
133 | local checked_lua_h = false | 141 | local checked_lua_h = false |
134 | 142 | ||
@@ -136,9 +144,9 @@ function builtin.run(rockspec, no_install) | |||
136 | variables[var] = variables[var] or os.getenv(var) or "" | 144 | variables[var] = variables[var] or os.getenv(var) or "" |
137 | end | 145 | end |
138 | 146 | ||
139 | local function add_flags(extras, flag, flags) | 147 | local function add_flags(extras: {string}, flag: string, flags: {string}) |
140 | if flags then | 148 | if flags then |
141 | if type(flags) ~= "table" then | 149 | if not flags is {string} then |
142 | flags = { tostring(flags) } | 150 | flags = { tostring(flags) } |
143 | end | 151 | end |
144 | util.variable_substitutions(flags, variables) | 152 | util.variable_substitutions(flags, variables) |
@@ -149,14 +157,14 @@ function builtin.run(rockspec, no_install) | |||
149 | end | 157 | end |
150 | 158 | ||
151 | if cfg.is_platform("mingw32") then | 159 | if cfg.is_platform("mingw32") then |
152 | compile_object = function(object, source, defines, incdirs) | 160 | compile_object = function(object: string, source: string, defines: {string}, incdirs: {string}): boolean, string, string |
153 | local extras = {} | 161 | local extras = {} |
154 | add_flags(extras, "-D%s", defines) | 162 | add_flags(extras, "-D%s", defines) |
155 | add_flags(extras, "-I%s", incdirs) | 163 | add_flags(extras, "-I%s", incdirs) |
156 | return execute(variables.CC.." "..variables.CFLAGS, "-c", "-o", object, "-I"..variables.LUA_INCDIR, source, unpack(extras)) | 164 | return execute(variables.CC.." "..variables.CFLAGS, "-c", "-o", object, "-I"..variables.LUA_INCDIR, source, table.unpack(extras)) |
157 | end | 165 | end |
158 | compile_library = function(library, objects, libraries, libdirs, name) | 166 | compile_library = function(library: string, objects: {string}, libraries: {string}, libdirs: {string}, name: string): boolean, string, string |
159 | local extras = { unpack(objects) } | 167 | local extras = { table.unpack(objects) } |
160 | add_flags(extras, "-L%s", libdirs) | 168 | add_flags(extras, "-L%s", libdirs) |
161 | add_flags(extras, "-l%s", libraries) | 169 | add_flags(extras, "-l%s", libraries) |
162 | extras[#extras+1] = dir.path(variables.LUA_LIBDIR, variables.LUALIB) | 170 | extras[#extras+1] = dir.path(variables.LUA_LIBDIR, variables.LUALIB) |
@@ -169,7 +177,7 @@ function builtin.run(rockspec, no_install) | |||
169 | extras[#extras+1] = "-l" .. (variables.MSVCRT or "m") | 177 | extras[#extras+1] = "-l" .. (variables.MSVCRT or "m") |
170 | end | 178 | end |
171 | 179 | ||
172 | local ok = execute(variables.LD.." "..variables.LDFLAGS.." "..variables.LIBFLAG, "-o", library, unpack(extras)) | 180 | local ok = execute(variables.LD.." "..variables.LDFLAGS.." "..variables.LIBFLAG, "-o", library, table.unpack(extras)) |
173 | return ok | 181 | return ok |
174 | end | 182 | end |
175 | --[[ TODO disable static libs until we fix the conflict in the manifest, which will take extending the manifest format. | 183 | --[[ TODO disable static libs until we fix the conflict in the manifest, which will take extending the manifest format. |
@@ -182,14 +190,14 @@ function builtin.run(rockspec, no_install) | |||
182 | end | 190 | end |
183 | ]] | 191 | ]] |
184 | elseif cfg.is_platform("win32") then | 192 | elseif cfg.is_platform("win32") then |
185 | compile_object = function(object, source, defines, incdirs) | 193 | compile_object = function(object: string, source: string, defines: {string}, incdirs: {string}): boolean, string, string |
186 | local extras = {} | 194 | local extras = {} |
187 | add_flags(extras, "-D%s", defines) | 195 | add_flags(extras, "-D%s", defines) |
188 | add_flags(extras, "-I%s", incdirs) | 196 | add_flags(extras, "-I%s", incdirs) |
189 | return execute(variables.CC.." "..variables.CFLAGS, "-c", "-Fo"..object, "-I"..variables.LUA_INCDIR, source, unpack(extras)) | 197 | return execute(variables.CC.." "..variables.CFLAGS, "-c", "-Fo"..object, "-I"..variables.LUA_INCDIR, source, table.unpack(extras)) |
190 | end | 198 | end |
191 | compile_library = function(library, objects, libraries, libdirs, name) | 199 | compile_library = function(library: string, objects: {string}, libraries: {string}, libdirs: {string}, name: string): boolean, string, string |
192 | local extras = { unpack(objects) } | 200 | local extras = { table.unpack(objects) } |
193 | add_flags(extras, "-libpath:%s", libdirs) | 201 | add_flags(extras, "-libpath:%s", libdirs) |
194 | add_flags(extras, "%s.lib", libraries) | 202 | add_flags(extras, "%s.lib", libraries) |
195 | local basename = dir.base_name(library):gsub(".[^.]*$", "") | 203 | local basename = dir.base_name(library):gsub(".[^.]*$", "") |
@@ -200,9 +208,9 @@ function builtin.run(rockspec, no_install) | |||
200 | def:write("EXPORTS\n") | 208 | def:write("EXPORTS\n") |
201 | def:write("luaopen_"..exported_name.."\n") | 209 | def:write("luaopen_"..exported_name.."\n") |
202 | def:close() | 210 | def:close() |
203 | local ok = execute(variables.LD, "-dll", "-def:"..deffile, "-out:"..library, dir.path(variables.LUA_LIBDIR, variables.LUALIB), unpack(extras)) | 211 | local ok = execute(variables.LD, "-dll", "-def:"..deffile, "-out:"..library, dir.path(variables.LUA_LIBDIR, variables.LUALIB), table.unpack(extras)) |
204 | local basedir = "" | 212 | local basedir = "" |
205 | if name:find("%.") ~= nil then | 213 | if name:find("%.") then |
206 | basedir = name:gsub("%.%w+$", "\\") | 214 | basedir = name:gsub("%.%w+$", "\\") |
207 | basedir = basedir:gsub("%.", "\\") | 215 | basedir = basedir:gsub("%.", "\\") |
208 | end | 216 | end |
@@ -220,14 +228,14 @@ function builtin.run(rockspec, no_install) | |||
220 | end | 228 | end |
221 | ]] | 229 | ]] |
222 | else | 230 | else |
223 | compile_object = function(object, source, defines, incdirs) | 231 | compile_object = function(object: string, source: string, defines: {string}, incdirs: {string}): boolean, string, string |
224 | local extras = {} | 232 | local extras = {} |
225 | add_flags(extras, "-D%s", defines) | 233 | add_flags(extras, "-D%s", defines) |
226 | add_flags(extras, "-I%s", incdirs) | 234 | add_flags(extras, "-I%s", incdirs) |
227 | return execute(variables.CC.." "..variables.CFLAGS, "-I"..variables.LUA_INCDIR, "-c", source, "-o", object, unpack(extras)) | 235 | return execute(variables.CC.." "..variables.CFLAGS, "-I"..variables.LUA_INCDIR, "-c", source, "-o", object, table.unpack(extras)) |
228 | end | 236 | end |
229 | compile_library = function (library, objects, libraries, libdirs) | 237 | compile_library = function (library: string, objects: {string}, libraries: {string}, libdirs: {string}): boolean, string, string |
230 | local extras = { unpack(objects) } | 238 | local extras = { table.unpack(objects) } |
231 | add_flags(extras, "-L%s", libdirs) | 239 | add_flags(extras, "-L%s", libdirs) |
232 | if cfg.gcc_rpath then | 240 | if cfg.gcc_rpath then |
233 | add_flags(extras, "-Wl,-rpath,%s", libdirs) | 241 | add_flags(extras, "-Wl,-rpath,%s", libdirs) |
@@ -237,10 +245,10 @@ function builtin.run(rockspec, no_install) | |||
237 | extras[#extras+1] = "-L"..variables.LUA_LIBDIR | 245 | extras[#extras+1] = "-L"..variables.LUA_LIBDIR |
238 | extras[#extras+1] = "-llua" | 246 | extras[#extras+1] = "-llua" |
239 | end | 247 | end |
240 | return execute(variables.LD.." "..variables.LDFLAGS.." "..variables.LIBFLAG, "-o", library, unpack(extras)) | 248 | return execute(variables.LD.." "..variables.LDFLAGS.." "..variables.LIBFLAG, "-o", library, table.unpack(extras)) |
241 | end | 249 | end |
242 | compile_static_library = function(library, objects, libraries, libdirs, name) -- luacheck: ignore 211 | 250 | compile_static_library = function(library: string, objects: {string}, libraries: {string}, libdirs: {string}, name: string): boolean, string, string -- luacheck: ignore 211 |
243 | local ok = execute(variables.AR, "rc", library, unpack(objects)) | 251 | local ok = execute(variables.AR, "rc", library, table.unpack(objects)) |
244 | if ok then | 252 | if ok then |
245 | ok = execute(variables.RANLIB, library) | 253 | ok = execute(variables.RANLIB, library) |
246 | end | 254 | end |
@@ -248,7 +256,7 @@ function builtin.run(rockspec, no_install) | |||
248 | end | 256 | end |
249 | end | 257 | end |
250 | 258 | ||
251 | local ok, err | 259 | local ok, err: boolean, string |
252 | local lua_modules = {} | 260 | local lua_modules = {} |
253 | local lib_modules = {} | 261 | local lib_modules = {} |
254 | local luadir = path.lua_dir(rockspec.name, rockspec.version) | 262 | local luadir = path.lua_dir(rockspec.name, rockspec.version) |
@@ -258,7 +266,7 @@ function builtin.run(rockspec, no_install) | |||
258 | 266 | ||
259 | if not build.modules then | 267 | if not build.modules then |
260 | if rockspec:format_is_at_least("3.0") then | 268 | if rockspec:format_is_at_least("3.0") then |
261 | local install, copy_directories | 269 | local install, copy_directories: Build.Install, {string} |
262 | build.modules, install, copy_directories = builtin.autodetect_modules(autolibs, autoincdirs, autolibdirs) | 270 | build.modules, install, copy_directories = builtin.autodetect_modules(autolibs, autoincdirs, autolibdirs) |
263 | build.install = build.install or install | 271 | build.install = build.install or install |
264 | build.copy_directories = build.copy_directories or copy_directories | 272 | build.copy_directories = build.copy_directories or copy_directories |
@@ -267,10 +275,10 @@ function builtin.run(rockspec, no_install) | |||
267 | end | 275 | end |
268 | end | 276 | end |
269 | 277 | ||
270 | local compile_temp_dir | 278 | local compile_temp_dir: string |
271 | 279 | ||
272 | local mkdir_cache = {} | 280 | local mkdir_cache = {} |
273 | local function cached_make_dir(name) | 281 | local function cached_make_dir(name: string): boolean, string |
274 | if name == "" or mkdir_cache[name] then | 282 | if name == "" or mkdir_cache[name] then |
275 | return true | 283 | return true |
276 | end | 284 | end |
@@ -280,7 +288,7 @@ function builtin.run(rockspec, no_install) | |||
280 | 288 | ||
281 | for name, info in pairs(build.modules) do | 289 | for name, info in pairs(build.modules) do |
282 | local moddir = path.module_to_path(name) | 290 | local moddir = path.module_to_path(name) |
283 | if type(info) == "string" then | 291 | if info is string then |
284 | local ext = info:match("%.([^.]+)$") | 292 | local ext = info:match("%.([^.]+)$") |
285 | if ext == "lua" then | 293 | if ext == "lua" then |
286 | local filename = dir.base_name(info) | 294 | local filename = dir.base_name(info) |
@@ -296,7 +304,7 @@ function builtin.run(rockspec, no_install) | |||
296 | info = {info} | 304 | info = {info} |
297 | end | 305 | end |
298 | end | 306 | end |
299 | if type(info) == "table" then | 307 | if info is Module then |
300 | if not checked_lua_h then | 308 | if not checked_lua_h then |
301 | local ok, err, errcode = deps.check_lua_incdir(rockspec.variables) | 309 | local ok, err, errcode = deps.check_lua_incdir(rockspec.variables) |
302 | if not ok then | 310 | if not ok then |
@@ -314,12 +322,12 @@ function builtin.run(rockspec, no_install) | |||
314 | local objects = {} | 322 | local objects = {} |
315 | local sources = info.sources | 323 | local sources = info.sources |
316 | if info[1] then sources = info end | 324 | if info[1] then sources = info end |
317 | if type(sources) == "string" then sources = {sources} end | 325 | if sources is string then sources = {sources} end |
318 | if type(sources) ~= "table" then | 326 | if not sources is {string} then |
319 | return nil, "error in rockspec: module '" .. name .. "' entry has no 'sources' list" | 327 | return nil, "error in rockspec: module '" .. name .. "' entry has no 'sources' list" |
320 | end | 328 | end |
321 | for _, source in ipairs(sources) do | 329 | for _, source in ipairs(sources) do |
322 | if type(source) ~= "string" then | 330 | if not source is string then |
323 | return nil, "error in rockspec: module '" .. name .. "' does not specify source correctly." | 331 | return nil, "error in rockspec: module '" .. name .. "' does not specify source correctly." |
324 | end | 332 | end |
325 | local object = source:gsub("%.[^.]*$", "."..cfg.obj_extension) | 333 | local object = source:gsub("%.[^.]*$", "."..cfg.obj_extension) |
@@ -348,7 +356,7 @@ function builtin.run(rockspec, no_install) | |||
348 | cached_make_dir(build_dir) | 356 | cached_make_dir(build_dir) |
349 | 357 | ||
350 | lib_modules[build_name] = dir.path(libdir, module_name) | 358 | lib_modules[build_name] = dir.path(libdir, module_name) |
351 | ok = compile_library(build_name, objects, info.libraries, info.libdirs or autolibdirs, name) | 359 | ok = compile_library(build_name, objects, info.libraries as {string}, info.libdirs or autolibdirs, name) |
352 | if not ok then | 360 | if not ok then |
353 | return nil, "Failed compiling module "..module_name | 361 | return nil, "Failed compiling module "..module_name |
354 | end | 362 | end |