aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-06-09 22:37:52 +0200
committerV1K1NGbg <victor@ilchev.com>2024-08-05 20:49:17 +0300
commit932fbc517f4cf8aaca54de8737d7b8220af1de4e (patch)
treeb53cc8f3ee3480b6e7a5d0d3065c135cab619173
parent1f35152ae6282cca0b40f433a9c667fdaa3cf201 (diff)
downloadluarocks-932fbc517f4cf8aaca54de8737d7b8220af1de4e.tar.gz
luarocks-932fbc517f4cf8aaca54de8737d7b8220af1de4e.tar.bz2
luarocks-932fbc517f4cf8aaca54de8737d7b8220af1de4e.zip
cfg - almost nowhere
-rw-r--r--src/luarocks/core/cfg (conflicted).tl978
-rw-r--r--src/luarocks/core/cfg-original.lua (renamed from src/luarocks/core/cfg.lua)0
-rw-r--r--src/luarocks/core/cfg.tl981
3 files changed, 1959 insertions, 0 deletions
diff --git a/src/luarocks/core/cfg (conflicted).tl b/src/luarocks/core/cfg (conflicted).tl
new file mode 100644
index 00000000..f4c78ebf
--- /dev/null
+++ b/src/luarocks/core/cfg (conflicted).tl
@@ -0,0 +1,978 @@
1
2--- Configuration for LuaRocks.
3-- Tries to load the user's configuration file and
4-- defines defaults for unset values. See the
5-- <a href="http://luarocks.org/en/Config_file_format">config
6-- file format documentation</a> for details.
7--
8-- End-users shouldn't edit this file. They can override any defaults
9-- set in this file using their system-wide or user-specific configuration
10-- files. Run `luarocks` with no arguments to see the locations of
11-- these files in your platform.
12
13local table, pairs, require, os, pcall, ipairs, package, type, assert = --!
14 table, pairs, require, os, pcall, ipairs, package, type, assert
15
16local dir = require("luarocks.core.dir") --!
17local util = require("luarocks.core.util")
18local persist = require("luarocks.core.persist")
19local sysdetect = require("luarocks.core.sysdetect")
20local vers = require("luarocks.core.vers")
21
22local record cfg
23
24end
25
26--------------------------------------------------------------------------------
27
28local program_version = "dev" --!
29
30local is_windows = package.config:sub(1,1) == "\\" --!
31
32-- Set order for platform overrides.
33-- More general platform identifiers should be listed first,
34-- more specific ones later.
35local platform_order = {
36 -- Unixes
37 "unix",
38 "bsd",
39 "solaris",
40 "netbsd",
41 "openbsd",
42 "freebsd",
43 "dragonfly",
44 "linux",
45 "macosx",
46 "cygwin",
47 "msys",
48 "haiku",
49 -- Windows
50 "windows",
51 "win32",
52 "mingw",
53 "mingw32",
54 "msys2_mingw_w64",
55}
56
57local type Config = record --!
58 home: any
59 lua_version: any
60 target_cpu: any
61 variables: {any: any}
62 os_getenv: any
63 rocks_trees: any
64 rocks_servers: any
65 dump_env: function()
66end
67
68local function detect_sysconfdir()
69 if not debug then
70 return
71 end
72 local src = debug.getinfo(1, "S").source
73 if not src then
74 return
75 end
76 src = dir.normalize(src)
77 if src:sub(1, 1) == "@" then
78 src = src:sub(2)
79 end
80 local basedir = src:match("^(.*)[\\/]luarocks[\\/]core[\\/]cfg.lua$")
81 if not basedir then
82 return
83 end
84 -- If installed in a Unix-like tree, use a Unix-like sysconfdir
85 local installdir = basedir:match("^(.*)[\\/]share[\\/]lua[\\/][^/]*$")
86 if installdir then
87 if installdir == "/usr" then
88 return "/etc/luarocks" --!
89 end
90 return dir.path(installdir, "etc", "luarocks")
91 end
92 -- Otherwise, use base directory of sources
93 return basedir
94end
95
96-- local load_config_file --!
97do
98 -- Create global environment for the config files;
99 local function env_for_config_file(cfg: Config, platforms: {any: any}): {any: any} --!
100 local platforms_copy = {}
101 for k,v in pairs(platforms) do
102 platforms_copy[k] = v
103 end
104
105 local e: {any: any} --!
106 e = {
107 home = cfg.home,
108 lua_version = cfg.lua_version,
109 platforms = platforms_copy,
110 processor = cfg.target_cpu, -- remains for compat reasons
111 target_cpu = cfg.target_cpu, -- replaces `processor`
112 os_getenv = os.getenv,
113 variables = cfg.variables or {},
114 dump_env = function()
115 -- debug function, calling it from a config file will show all
116 -- available globals to that config file
117 print(util.show_table(e, "global environment"))
118 end,
119 }
120 return e
121 end
122
123 -- Merge values from config files read into the `cfg` table
124 local function merge_overrides(cfg: Config, overrides: Config)
125 -- remove some stuff we do not want to integrate
126 overrides.os_getenv = nil
127 overrides.dump_env = nil
128 -- remove tables to be copied verbatim instead of deeply merged
129 if overrides.rocks_trees then cfg.rocks_trees = nil end
130 if overrides.rocks_servers then cfg.rocks_servers = nil end
131 -- perform actual merge
132 util.deep_merge(cfg, overrides)
133 end
134
135 local function update_platforms(platforms: {any: any}, overrides: {any: any})
136 if overrides[1] then
137 for k, _ in pairs(platforms) do
138 platforms[k] = nil
139 end
140 for _, v in ipairs(overrides) do
141 platforms[v] = true
142 end
143 -- set some fallback default in case the user provides an incomplete configuration.
144 -- LuaRocks expects a set of defaults to be available.
145 if not (platforms.unix or platforms.windows) then
146 platforms[is_windows and "windows" or "unix"] = true
147 end
148 end
149 end
150
151 -- Load config file and merge its contents into the `cfg` module table.
152 -- @return filepath of successfully loaded file or nil if it failed
153 local load_config_file = function(cfg: Config, platforms: {any: any}, filepath: string): string --!
154 local result, err, errcode = persist.load_into_table(filepath, env_for_config_file(cfg, platforms))
155 if (not result) and errcode ~= "open" then
156 -- errcode is either "load" or "run"; bad config file, so error out
157 return nil, err, "config"
158 end
159 if result then
160 -- success in loading and running, merge contents and exit
161 update_platforms(platforms, result.platforms)
162 result.platforms = nil
163 merge_overrides(cfg, result)
164 return filepath
165 end
166 return nil -- nothing was loaded
167 end
168end
169
170local platform_sets = {
171 freebsd = { unix = true, bsd = true, freebsd = true },
172 openbsd = { unix = true, bsd = true, openbsd = true },
173 dragonfly = { unix = true, bsd = true, dragonfly = true },
174 solaris = { unix = true, solaris = true },
175 windows = { windows = true, win32 = true },
176 cygwin = { unix = true, cygwin = true },
177 macosx = { unix = true, bsd = true, macosx = true, macos = true },
178 netbsd = { unix = true, bsd = true, netbsd = true },
179 haiku = { unix = true, haiku = true },
180 linux = { unix = true, linux = true },
181 mingw = { windows = true, win32 = true, mingw32 = true, mingw = true },
182 msys = { unix = true, cygwin = true, msys = true },
183 msys2_mingw_w64 = { windows = true, win32 = true, mingw32 = true, mingw = true, msys = true, msys2_mingw_w64 = true },
184}
185
186local function make_platforms(system: string): {any: boolean} --! :System
187 -- fallback to Unix in unknown systems
188 return platform_sets[system] or { unix = true }
189end
190
191--------------------------------------------------------------------------------
192
193local function make_defaults(lua_version: string, target_cpu: string, platforms: {any: any}, home: string): {any: any} --!
194
195 -- Configure defaults:
196 local defaults = {
197
198 local_by_default = false,
199 accept_unknown_fields = false,
200 fs_use_modules = true,
201 hooks_enabled = true,
202 deps_mode = "one",
203 no_manifest = false,
204 check_certificates = false,
205
206 cache_timeout = 60,
207 cache_fail_timeout = 86400,
208
209 lua_modules_path = dir.path("share", "lua", lua_version),
210 lib_modules_path = dir.path("lib", "lua", lua_version),
211 rocks_subdir = dir.path("lib", "luarocks", "rocks-"..lua_version),
212
213 arch = "unknown",
214 lib_extension = "unknown",
215 external_lib_extension = "unknown", --?
216 static_lib_extension = "unknown", --?
217 obj_extension = "unknown",
218 link_lua_explicitly = false,
219
220 external_deps_dirs = {}, --?
221 external_deps_patterns = {}, --?
222 runtime_external_deps_patterns = {}, --?
223
224 export_path_separator = "", --?
225 wrapper_suffix = "", --?
226
227
228 rocks_servers = {
229 {
230 "https://luarocks.org",
231 "https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/",
232 "https://loadk.com/luarocks/",
233 }
234 },
235 disabled_servers = {},
236
237 upload = {
238 server = "https://luarocks.org",
239 tool_version = "1.0.0",
240 api_version = "1",
241 },
242
243 lua_extension = "lua",
244 connection_timeout = 30, -- 0 = no timeout
245
246 makefile = "", --?
247 local_cache = "" , --?
248 web_browser = "", --?
249 cmake_generator = "", --?
250 gcc_rpath = false, --?
251
252 variables = {
253 MAKE = os.getenv("MAKE") or "make",
254 CC = os.getenv("CC") or "cc",
255 LD = os.getenv("CC") or "ld",
256 AR = os.getenv("AR") or "ar",
257 RANLIB = os.getenv("RANLIB") or "ranlib",
258
259 CVS = "cvs",
260 GIT = "git",
261 SSCM = "sscm",
262 SVN = "svn",
263 HG = "hg",
264
265 GPG = "gpg",
266
267 RSYNC = "rsync",
268 WGET = "wget",
269 SCP = "scp",
270 CURL = "curl",
271
272 PWD = "pwd",
273 MKDIR = "mkdir",
274 RMDIR = "rmdir",
275 CP = "cp",
276 LN = "ln",
277 LS = "ls",
278 RM = "rm",
279 FIND = "find",
280 CHMOD = "chmod",
281 ICACLS = "icacls",
282 MKTEMP = "mktemp",
283
284 ZIP = "zip",
285 UNZIP = "unzip -n",
286 GUNZIP = "gunzip",
287 BUNZIP2 = "bunzip2",
288 TAR = "tar",
289
290 MD5SUM = "md5sum",
291 OPENSSL = "openssl",
292 MD5 = "md5",
293 TOUCH = "touch",
294
295 CMAKE = "cmake",
296 SEVENZ = "7z",
297
298 RSYNCFLAGS = "--exclude=.git -Oavz",
299 CURLNOCERTFLAG = "",
300 WGETNOCERTFLAG = "",
301
302 RC = "", --?
303 MT = "", --?
304 CFLAGS = "", --?
305 LDFLAGS = "", --?
306 LIBFLAG = "", --?
307 TEST = "", --?
308 },
309
310 external_deps_subdirs = {
311 bin = "bin",
312 lib = "lib",
313 include = "include"
314 },
315 runtime_external_deps_subdirs = {
316 bin = "bin",
317 lib = "lib",
318 include = "include"
319 },
320 }
321
322 if platforms.windows then
323
324 defaults.arch = "win32-"..target_cpu
325 defaults.lib_extension = "dll"
326 defaults.external_lib_extension = "dll"
327 defaults.static_lib_extension = "lib"
328 defaults.obj_extension = "obj"
329 defaults.external_deps_dirs = {
330 dir.path("c:", "external"),
331 dir.path("c:", "windows", "system32"),
332 }
333
334 defaults.makefile = "Makefile.win"
335 defaults.variables.PWD = "echo %cd%"
336 defaults.variables.MKDIR = "md"
337 defaults.variables.MAKE = os.getenv("MAKE") or "nmake"
338 defaults.variables.CC = os.getenv("CC") or "cl"
339 defaults.variables.RC = os.getenv("WINDRES") or "rc"
340 defaults.variables.LD = os.getenv("LINK") or "link"
341 defaults.variables.MT = os.getenv("MT") or "mt"
342 defaults.variables.AR = os.getenv("AR") or "lib"
343 defaults.variables.CFLAGS = os.getenv("CFLAGS") or "/nologo /MD /O2"
344 defaults.variables.LDFLAGS = os.getenv("LDFLAGS")
345 defaults.variables.LIBFLAG = "/nologo /dll"
346
347 defaults.external_deps_patterns = {
348 bin = { "?.exe", "?.bat" },
349 lib = { "?.lib", "lib?.lib", "?.dll", "lib?.dll" },
350 include = { "?.h" }
351 }
352 defaults.runtime_external_deps_patterns = {
353 bin = { "?.exe", "?.bat" },
354 lib = { "?.dll", "lib?.dll" },
355 include = { "?.h" }
356 }
357 defaults.export_path_separator = ";"
358 defaults.wrapper_suffix = ".bat"
359
360 local localappdata = os.getenv("LOCALAPPDATA")
361 if not localappdata then
362 -- for Windows versions below Vista
363 localappdata = dir.path((os.getenv("USERPROFILE") or dir.path("c:", "Users", "All Users")), "Local Settings", "Application Data")
364 end
365 defaults.local_cache = dir.path(localappdata, "LuaRocks", "Cache")
366 defaults.web_browser = "start"
367
368 defaults.external_deps_subdirs.lib = { "lib", "", "bin" }
369 defaults.runtime_external_deps_subdirs.lib = { "lib", "", "bin" }
370 defaults.link_lua_explicitly = true
371 defaults.fs_use_modules = false
372 end
373
374 if platforms.mingw32 then
375 defaults.obj_extension = "o"
376 defaults.static_lib_extension = "a"
377 defaults.external_deps_dirs = {
378 dir.path("c:", "external"),
379 dir.path("c:", "mingw"),
380 dir.path("c:", "windows", "system32"),
381 }
382 defaults.cmake_generator = "MinGW Makefiles"
383 defaults.variables.MAKE = os.getenv("MAKE") or "mingw32-make"
384 if target_cpu == "x86_64" then
385 defaults.variables.CC = os.getenv("CC") or "x86_64-w64-mingw32-gcc"
386 defaults.variables.LD = os.getenv("CC") or "x86_64-w64-mingw32-gcc"
387 else
388 defaults.variables.CC = os.getenv("CC") or "mingw32-gcc"
389 defaults.variables.LD = os.getenv("CC") or "mingw32-gcc"
390 end
391 defaults.variables.AR = os.getenv("AR") or "ar"
392 defaults.variables.RC = os.getenv("WINDRES") or "windres"
393 defaults.variables.RANLIB = os.getenv("RANLIB") or "ranlib"
394 defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2"
395 defaults.variables.LDFLAGS = os.getenv("LDFLAGS")
396 defaults.variables.LIBFLAG = "-shared"
397 defaults.makefile = "Makefile"
398 defaults.external_deps_patterns = {
399 bin = { "?.exe", "?.bat" },
400 -- mingw lookup list from http://stackoverflow.com/a/15853231/1793220
401 -- ...should we keep ?.lib at the end? It's not in the above list.
402 lib = { "lib?.dll.a", "?.dll.a", "lib?.a", "cyg?.dll", "lib?.dll", "?.dll", "?.lib" },
403 include = { "?.h" }
404 }
405 defaults.runtime_external_deps_patterns = {
406 bin = { "?.exe", "?.bat" },
407 lib = { "cyg?.dll", "?.dll", "lib?.dll" },
408 include = { "?.h" }
409 }
410 defaults.link_lua_explicitly = true
411 end
412
413 if platforms.unix then
414 defaults.lib_extension = "so"
415 defaults.static_lib_extension = "a"
416 defaults.external_lib_extension = "so"
417 defaults.obj_extension = "o"
418 defaults.external_deps_dirs = { "/usr/local", "/usr", "/" }
419
420 defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2"
421 -- we pass -fPIC via CFLAGS because of old Makefile-based Lua projects
422 -- which didn't have -fPIC in their Makefiles but which honor CFLAGS
423 if not defaults.variables.CFLAGS:match("-fPIC") then
424 defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC"
425 end
426
427 defaults.variables.LDFLAGS = os.getenv("LDFLAGS")
428
429 defaults.cmake_generator = "Unix Makefiles"
430 defaults.variables.CC = os.getenv("CC") or "gcc"
431 defaults.variables.LD = os.getenv("CC") or "gcc"
432 defaults.gcc_rpath = true
433 defaults.variables.LIBFLAG = "-shared"
434 defaults.variables.TEST = "test"
435
436 defaults.external_deps_patterns = {
437 bin = { "?" },
438 lib = { "lib?.a", "lib?.so", "lib?.so.*" },
439 include = { "?.h" }
440 }
441 defaults.runtime_external_deps_patterns = {
442 bin = { "?" },
443 lib = { "lib?.so", "lib?.so.*" },
444 include = { "?.h" }
445 }
446 defaults.export_path_separator = ":"
447 defaults.wrapper_suffix = ""
448 local xdg_cache_home = os.getenv("XDG_CACHE_HOME") or home.."/.cache"
449 defaults.local_cache = xdg_cache_home.."/luarocks"
450 defaults.web_browser = "xdg-open"
451 end
452
453 if platforms.cygwin then
454 defaults.lib_extension = "so" -- can be overridden in the config file for mingw builds
455 defaults.arch = "cygwin-"..target_cpu
456 defaults.cmake_generator = "Unix Makefiles"
457 defaults.variables.CC = "echo -llua | xargs " .. (os.getenv("CC") or "gcc")
458 defaults.variables.LD = "echo -llua | xargs " .. (os.getenv("CC") or "gcc")
459 defaults.variables.LIBFLAG = "-shared"
460 defaults.link_lua_explicitly = true
461 end
462
463 if platforms.msys then
464 -- msys is basically cygwin made out of mingw, meaning the subsytem is unixish
465 -- enough, yet we can freely mix with native win32
466 defaults.external_deps_patterns = {
467 bin = { "?.exe", "?.bat", "?" },
468 lib = { "lib?.so", "lib?.so.*", "lib?.dll.a", "?.dll.a",
469 "lib?.a", "lib?.dll", "?.dll", "?.lib" },
470 include = { "?.h" }
471 }
472 defaults.runtime_external_deps_patterns = {
473 bin = { "?.exe", "?.bat" },
474 lib = { "lib?.so", "?.dll", "lib?.dll" },
475 include = { "?.h" }
476 }
477 if platforms.mingw then
478 -- MSYS2 can build Windows programs that depend on
479 -- msys-2.0.dll (based on Cygwin) but MSYS2 is also designed
480 -- for building native Windows programs by MinGW. These
481 -- programs don't depend on msys-2.0.dll.
482 local pipe = io.popen("cygpath --windows %MINGW_PREFIX%")
483 local mingw_prefix = pipe:read("*l")
484 pipe:close()
485 defaults.external_deps_dirs = {
486 mingw_prefix,
487 dir.path("c:", "windows", "system32"),
488 }
489 defaults.makefile = "Makefile"
490 defaults.cmake_generator = "MSYS Makefiles"
491 defaults.local_cache = dir.path(home, ".cache", "luarocks")
492 defaults.variables.MAKE = os.getenv("MAKE") or "make"
493 defaults.variables.CC = os.getenv("CC") or "gcc"
494 defaults.variables.RC = os.getenv("WINDRES") or "windres"
495 defaults.variables.LD = os.getenv("CC") or "gcc"
496 defaults.variables.MT = os.getenv("MT") or nil
497 defaults.variables.AR = os.getenv("AR") or "ar"
498 defaults.variables.RANLIB = os.getenv("RANLIB") or "ranlib"
499
500 defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2 -fPIC"
501 if not defaults.variables.CFLAGS:match("-fPIC") then
502 defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC"
503 end
504
505 defaults.variables.LIBFLAG = "-shared"
506 end
507 end
508
509 if platforms.bsd then
510 defaults.variables.MAKE = "gmake"
511 defaults.gcc_rpath = false
512 defaults.variables.CC = os.getenv("CC") or "cc"
513 defaults.variables.LD = os.getenv("CC") or defaults.variables.CC
514 end
515
516 if platforms.macosx then
517 defaults.variables.MAKE = os.getenv("MAKE") or "make"
518 defaults.external_lib_extension = "dylib"
519 defaults.arch = "macosx-"..target_cpu
520 defaults.variables.LIBFLAG = "-bundle -undefined dynamic_lookup -all_load"
521 local version = util.popen_read("sw_vers -productVersion")
522 if not (version:match("^%d+%.%d+%.%d+$") or version:match("^%d+%.%d+$")) then
523 version = "10.3"
524 end
525 version = vers.parse_version(version)
526 if version >= vers.parse_version("11.0") then
527 version = vers.parse_version("11.0")
528 elseif version >= vers.parse_version("10.10") then
529 version = vers.parse_version("10.8")
530 elseif version >= vers.parse_version("10.5") then
531 version = vers.parse_version("10.5")
532 else
533 defaults.gcc_rpath = false
534 end
535 defaults.variables.CC = "env MACOSX_DEPLOYMENT_TARGET="..tostring(version).." gcc"
536 defaults.variables.LD = "env MACOSX_DEPLOYMENT_TARGET="..tostring(version).." gcc"
537 defaults.web_browser = "open"
538
539 -- XCode SDK
540 local sdk_path = util.popen_read("xcrun --show-sdk-path 2>/dev/null")
541 if sdk_path then
542 table.insert(defaults.external_deps_dirs, sdk_path .. "/usr")
543 table.insert(defaults.external_deps_patterns.lib, 1, "lib?.tbd")
544 table.insert(defaults.runtime_external_deps_patterns.lib, 1, "lib?.tbd")
545 end
546
547 -- Homebrew
548 table.insert(defaults.external_deps_dirs, "/usr/local/opt")
549 defaults.external_deps_subdirs.lib = { "lib", "" }
550 defaults.runtime_external_deps_subdirs.lib = { "lib", "" }
551 table.insert(defaults.external_deps_patterns.lib, 1, "/?/lib/lib?.dylib")
552 table.insert(defaults.runtime_external_deps_patterns.lib, 1, "/?/lib/lib?.dylib")
553 end
554
555 if platforms.linux then
556 defaults.arch = "linux-"..target_cpu
557
558 local gcc_arch = util.popen_read("gcc -print-multiarch 2>/dev/null")
559 if gcc_arch and gcc_arch ~= "" then
560 defaults.external_deps_subdirs.lib = { "lib/" .. gcc_arch, "lib64", "lib" }
561 defaults.runtime_external_deps_subdirs.lib = { "lib/" .. gcc_arch, "lib64", "lib" }
562 else
563 defaults.external_deps_subdirs.lib = { "lib64", "lib" }
564 defaults.runtime_external_deps_subdirs.lib = { "lib64", "lib" }
565 end
566 end
567
568 if platforms.freebsd then
569 defaults.arch = "freebsd-"..target_cpu
570 elseif platforms.dragonfly then
571 defaults.arch = "dragonfly-"..target_cpu
572 elseif platforms.openbsd then
573 defaults.arch = "openbsd-"..target_cpu
574 elseif platforms.netbsd then
575 defaults.arch = "netbsd-"..target_cpu
576 elseif platforms.solaris then
577 defaults.arch = "solaris-"..target_cpu
578 defaults.variables.MAKE = "gmake"
579 end
580
581 -- Expose some more values detected by LuaRocks for use by rockspec authors.
582 defaults.variables.LIB_EXTENSION = defaults.lib_extension
583 defaults.variables.OBJ_EXTENSION = defaults.obj_extension
584
585 return defaults
586end
587
588local function use_defaults(cfg, defaults)
589
590 -- Populate variables with values from their 'defaults' counterparts
591 -- if they were not already set by user.
592 if not cfg.variables then
593 cfg.variables = {}
594 end
595 for k,v in pairs(defaults.variables) do
596 if not cfg.variables[k] then
597 cfg.variables[k] = v
598 end
599 end
600
601 util.deep_merge_under(cfg, defaults)
602
603 -- FIXME get rid of this
604 if not cfg.check_certificates then
605 cfg.variables.CURLNOCERTFLAG = "-k"
606 cfg.variables.WGETNOCERTFLAG = "--no-check-certificate"
607 end
608end
609
610--------------------------------------------------------------------------------
611
612local cfg = {}
613
614--- Initializes the LuaRocks configuration for variables, paths
615-- and OS detection.
616-- @param detected table containing information detected about the
617-- environment. All fields below are optional:
618-- * lua_version (in x.y format, e.g. "5.3")
619-- * lua_bindir (e.g. "/usr/local/bin")
620-- * lua_dir (e.g. "/usr/local")
621-- * lua (e.g. "/usr/local/bin/lua-5.3")
622-- * project_dir (a string with the path of the project directory
623-- when using per-project environments, as created with `luarocks init`)
624-- @param warning a logging function for warnings that takes a string
625-- @return true on success; nil and an error message on failure.
626function cfg.init(detected, warning)
627 detected = detected or {}
628
629 local exit_ok = true
630 local exit_err = nil
631 local exit_what = nil
632
633 local hc_ok, hardcoded = pcall(require, "luarocks.core.hardcoded")
634 if not hc_ok then
635 hardcoded = {}
636 end
637
638 local init = cfg.init
639
640 ----------------------------------------
641 -- Reset the cfg table.
642 ----------------------------------------
643
644 for k, _ in pairs(cfg) do
645 cfg[k] = nil
646 end
647
648 cfg.program_version = program_version
649
650 if hardcoded.IS_BINARY then
651 cfg.is_binary = true
652 end
653
654 -- Use detected values as defaults, overridable via config files or CLI args
655
656 local hardcoded_lua = hardcoded.LUA
657 local hardcoded_lua_dir = hardcoded.LUA_DIR
658 local hardcoded_lua_bindir = hardcoded.LUA_BINDIR
659 local hardcoded_lua_incdir = hardcoded.LUA_INCDIR
660 local hardcoded_lua_libdir = hardcoded.LUA_LIBDIR
661 local hardcoded_lua_version = hardcoded.LUA_VERSION or _VERSION:sub(5)
662
663 -- if --lua-version or --lua-dir are passed from the CLI,
664 -- don't use the hardcoded paths at all
665 if detected.given_lua_version or detected.given_lua_dir then
666 hardcoded_lua = nil
667 hardcoded_lua_dir = nil
668 hardcoded_lua_bindir = nil
669 hardcoded_lua_incdir = nil
670 hardcoded_lua_libdir = nil
671 hardcoded_lua_version = nil
672 end
673
674 cfg.lua_version = detected.lua_version or hardcoded_lua_version
675 cfg.project_dir = (not hardcoded.FORCE_CONFIG) and detected.project_dir
676
677 do
678 local lua = detected.lua or hardcoded_lua
679 local lua_dir = detected.lua_dir or hardcoded_lua_dir
680 local lua_bindir = detected.lua_bindir or hardcoded_lua_bindir
681 cfg.variables = {
682 LUA = lua,
683 LUA_DIR = lua_dir,
684 LUA_BINDIR = lua_bindir,
685 LUA_LIBDIR = hardcoded_lua_libdir,
686 LUA_INCDIR = hardcoded_lua_incdir,
687 }
688 end
689
690 cfg.init = init
691
692 ----------------------------------------
693 -- System detection.
694 ----------------------------------------
695
696 -- A proper build of LuaRocks will hardcode the system
697 -- and proc values with hardcoded.SYSTEM and hardcoded.PROCESSOR.
698 -- If that is not available, we try to identify the system.
699 local system, processor = sysdetect.detect()
700 if hardcoded.SYSTEM then
701 system = hardcoded.SYSTEM
702 end
703 if hardcoded.PROCESSOR then
704 processor = hardcoded.PROCESSOR
705 end
706
707 if system == "windows" then
708 if os.getenv("VCINSTALLDIR") then
709 -- running from the Development Command prompt for VS 2017
710 system = "windows"
711 else
712 local msystem = os.getenv("MSYSTEM")
713 if msystem == nil then
714 system = "mingw"
715 elseif msystem == "MSYS" then
716 system = "msys"
717 else
718 -- MINGW32 or MINGW64
719 system = "msys2_mingw_w64"
720 end
721 end
722 end
723
724 cfg.target_cpu = processor
725
726 local platforms = make_platforms(system)
727
728 ----------------------------------------
729 -- Platform is determined.
730 -- Let's load the config files.
731 ----------------------------------------
732
733 local sys_config_file
734 local home_config_file
735 local project_config_file
736
737 local config_file_name = "config-"..cfg.lua_version..".lua"
738
739 do
740 local sysconfdir = os.getenv("LUAROCKS_SYSCONFDIR") or hardcoded.SYSCONFDIR
741 if platforms.windows and not platforms.msys2_mingw_w64 then
742 cfg.home = os.getenv("APPDATA") or "c:"
743 cfg.home_tree = dir.path(cfg.home, "luarocks")
744 cfg.sysconfdir = sysconfdir or dir.path((os.getenv("PROGRAMFILES") or "c:"), "luarocks")
745 else
746 cfg.home = os.getenv("HOME") or ""
747 cfg.home_tree = dir.path(cfg.home, ".luarocks")
748 cfg.sysconfdir = sysconfdir or detect_sysconfdir() or "/etc/luarocks"
749 end
750 end
751
752 -- Load system configuration file
753 sys_config_file = dir.path(cfg.sysconfdir, config_file_name)
754 local sys_config_ok, err = load_config_file(cfg, platforms, sys_config_file)
755 if err then
756 exit_ok, exit_err, exit_what = nil, err, "config"
757 end
758
759 -- Load user configuration file (if allowed)
760 local home_config_ok
761 local project_config_ok
762 if not hardcoded.FORCE_CONFIG then
763 local env_var = "LUAROCKS_CONFIG_" .. cfg.lua_version:gsub("%.", "_")
764 local env_value = os.getenv(env_var)
765 if not env_value then
766 env_var = "LUAROCKS_CONFIG"
767 env_value = os.getenv(env_var)
768 end
769 -- first try environment provided file, so we can explicitly warn when it is missing
770 if env_value then
771 local env_ok, err = load_config_file(cfg, platforms, env_value)
772 if err then
773 exit_ok, exit_err, exit_what = nil, err, "config"
774 elseif warning and not env_ok then
775 warning("Warning: could not load configuration file `"..env_value.."` given in environment variable "..env_var.."\n")
776 end
777 if env_ok then
778 home_config_ok = true
779 home_config_file = env_value
780 end
781 end
782
783 -- try XDG config home
784 if platforms.unix and not home_config_ok then
785 local xdg_config_home = os.getenv("XDG_CONFIG_HOME") or dir.path(cfg.home, ".config")
786 cfg.homeconfdir = dir.path(xdg_config_home, "luarocks")
787 home_config_file = dir.path(cfg.homeconfdir, config_file_name)
788 home_config_ok, err = load_config_file(cfg, platforms, home_config_file)
789 if err then
790 exit_ok, exit_err, exit_what = nil, err, "config"
791 end
792 end
793
794 -- try the alternative defaults if there was no environment specified file or it didn't work
795 if not home_config_ok then
796 cfg.homeconfdir = cfg.home_tree
797 home_config_file = dir.path(cfg.homeconfdir, config_file_name)
798 home_config_ok, err = load_config_file(cfg, platforms, home_config_file)
799 if err then
800 exit_ok, exit_err, exit_what = nil, err, "config"
801 end
802 end
803
804 -- finally, use the project-specific config file if any
805 if cfg.project_dir then
806 project_config_file = dir.path(cfg.project_dir, ".luarocks", config_file_name)
807 project_config_ok, err = load_config_file(cfg, platforms, project_config_file)
808 if err then
809 exit_ok, exit_err, exit_what = nil, err, "config"
810 end
811 end
812 end
813
814 -- backwards compatibility:
815 if cfg.lua_interpreter and cfg.variables.LUA_BINDIR and not cfg.variables.LUA then
816 cfg.variables.LUA = dir.path(cfg.variables.LUA_BINDIR, cfg.lua_interpreter)
817 end
818
819 ----------------------------------------
820 -- Config files are loaded.
821 -- Let's finish up the cfg table.
822 ----------------------------------------
823
824 -- Settings given via the CLI (i.e. --lua-dir) take precedence over config files.
825 cfg.project_dir = detected.given_project_dir or cfg.project_dir
826 cfg.lua_version = detected.given_lua_version or cfg.lua_version
827 if detected.given_lua_dir then
828 cfg.variables.LUA = detected.lua
829 cfg.variables.LUA_DIR = detected.given_lua_dir
830 cfg.variables.LUA_BINDIR = detected.lua_bindir
831 cfg.variables.LUA_LIBDIR = nil
832 cfg.variables.LUA_INCDIR = nil
833 end
834
835 -- Build a default list of rocks trees if not given
836 if cfg.rocks_trees == nil then
837 cfg.rocks_trees = {}
838 if cfg.home_tree then
839 table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree } )
840 end
841 if hardcoded.PREFIX and hardcoded.PREFIX ~= cfg.home_tree then
842 table.insert(cfg.rocks_trees, { name = "system", root = hardcoded.PREFIX } )
843 end
844 end
845
846 local defaults = make_defaults(cfg.lua_version, processor, platforms, cfg.home)
847
848 if platforms.windows and hardcoded.WIN_TOOLS then
849 local tools = { "SEVENZ", "CP", "FIND", "LS", "MD5SUM", "WGET", }
850 for _, tool in ipairs(tools) do
851 defaults.variables[tool] = '"' .. dir.path(hardcoded.WIN_TOOLS, defaults.variables[tool] .. '.exe') .. '"'
852 end
853 else
854 defaults.fs_use_modules = true
855 end
856
857 -- if only cfg.variables.LUA is given in config files,
858 -- derive LUA_BINDIR and LUA_DIR from them.
859 if cfg.variables.LUA and not cfg.variables.LUA_BINDIR then
860 cfg.variables.LUA_BINDIR = cfg.variables.LUA:match("^(.*)[\\/][^\\/]*$")
861 if not cfg.variables.LUA_DIR then
862 cfg.variables.LUA_DIR = cfg.variables.LUA_BINDIR:gsub("[\\/]bin$", "") or cfg.variables.LUA_BINDIR
863 end
864 end
865
866 use_defaults(cfg, defaults)
867
868 cfg.user_agent = "LuaRocks/"..cfg.program_version.." "..cfg.arch
869
870 cfg.config_files = {
871 project = cfg.project_dir and {
872 file = project_config_file,
873 found = not not project_config_ok,
874 },
875 system = {
876 file = sys_config_file,
877 found = not not sys_config_ok,
878 },
879 user = {
880 file = home_config_file,
881 found = not not home_config_ok,
882 },
883 nearest = project_config_ok
884 and project_config_file
885 or (home_config_ok
886 and home_config_file
887 or sys_config_file),
888 }
889
890 cfg.cache = {}
891
892 ----------------------------------------
893 -- Attributes of cfg are set.
894 -- Let's add some methods.
895 ----------------------------------------
896
897 do
898 local function make_paths_from_tree(tree)
899 local lua_path, lib_path, bin_path
900 if type(tree) == "string" then
901 lua_path = dir.path(tree, cfg.lua_modules_path)
902 lib_path = dir.path(tree, cfg.lib_modules_path)
903 bin_path = dir.path(tree, "bin")
904 else
905 lua_path = tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path)
906 lib_path = tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path)
907 bin_path = tree.bin_dir or dir.path(tree.root, "bin")
908 end
909 return lua_path, lib_path, bin_path
910 end
911
912 function cfg.package_paths(current)
913 local new_path, new_cpath, new_bin = {}, {}, {}
914 local function add_tree_to_paths(tree)
915 local lua_path, lib_path, bin_path = make_paths_from_tree(tree)
916 table.insert(new_path, dir.path(lua_path, "?.lua"))
917 table.insert(new_path, dir.path(lua_path, "?", "init.lua"))
918 table.insert(new_cpath, dir.path(lib_path, "?."..cfg.lib_extension))
919 table.insert(new_bin, bin_path)
920 end
921 if current then
922 add_tree_to_paths(current)
923 end
924 for _,tree in ipairs(cfg.rocks_trees) do
925 add_tree_to_paths(tree)
926 end
927 return table.concat(new_path, ";"), table.concat(new_cpath, ";"), table.concat(new_bin, cfg.export_path_separator)
928 end
929 end
930
931 function cfg.init_package_paths()
932 local lr_path, lr_cpath, lr_bin = cfg.package_paths()
933 package.path = util.cleanup_path(package.path .. ";" .. lr_path, ";", cfg.lua_version, true)
934 package.cpath = util.cleanup_path(package.cpath .. ";" .. lr_cpath, ";", cfg.lua_version, true)
935 end
936
937 --- Check if platform was detected
938 -- @param name string: The platform name to check.
939 -- @return boolean: true if LuaRocks is currently running on queried platform.
940 function cfg.is_platform(name)
941 assert(type(name) == "string")
942 return platforms[name]
943 end
944
945 -- @param direction (optional) "least-specific-first" (default) or "most-specific-first"
946 function cfg.each_platform(direction)
947 direction = direction or "least-specific-first"
948 local i, delta
949 if direction == "least-specific-first" then
950 i = 0
951 delta = 1
952 else
953 i = #platform_order + 1
954 delta = -1
955 end
956 return function()
957 local p
958 repeat
959 i = i + delta
960 p = platform_order[i]
961 until (not p) or platforms[p]
962 return p
963 end
964 end
965
966 function cfg.print_platforms()
967 local platform_keys = {}
968 for k,_ in pairs(platforms) do
969 table.insert(platform_keys, k)
970 end
971 table.sort(platform_keys)
972 return table.concat(platform_keys, ", ")
973 end
974
975 return exit_ok, exit_err, exit_what
976end
977
978return cfg
diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg-original.lua
index 6d8fe55b..6d8fe55b 100644
--- a/src/luarocks/core/cfg.lua
+++ b/src/luarocks/core/cfg-original.lua
diff --git a/src/luarocks/core/cfg.tl b/src/luarocks/core/cfg.tl
new file mode 100644
index 00000000..77605554
--- /dev/null
+++ b/src/luarocks/core/cfg.tl
@@ -0,0 +1,981 @@
1
2--- Configuration for LuaRocks.
3-- Tries to load the user's configuration file and
4-- defines defaults for unset values. See the
5-- <a href="http://luarocks.org/en/Config_file_format">config
6-- file format documentation</a> for details.
7--
8-- End-users shouldn't edit this file. They can override any defaults
9-- set in this file using their system-wide or user-specific configuration
10-- files. Run `luarocks` with no arguments to see the locations of
11-- these files in your platform.
12
13local table, pairs, require, os, pcall, ipairs, package, type, assert = --!
14 table, pairs, require, os, pcall, ipairs, package, type, assert
15
16local dir = require("luarocks.core.dir") --!
17local util = require("luarocks.core.util")
18local persist = require("luarocks.core.persist")
19local sysdetect = require("luarocks.core.sysdetect")
20local vers = require("luarocks.core.vers")
21
22local record cfg
23
24end
25
26--------------------------------------------------------------------------------
27
28local program_version = "dev" --!
29
30local is_windows = package.config:sub(1,1) == "\\" --!
31
32-- Set order for platform overrides.
33-- More general platform identifiers should be listed first,
34-- more specific ones later.
35local platform_order = {
36 -- Unixes
37 "unix",
38 "bsd",
39 "solaris",
40 "netbsd",
41 "openbsd",
42 "freebsd",
43 "dragonfly",
44 "linux",
45 "macosx",
46 "cygwin",
47 "msys",
48 "haiku",
49 -- Windows
50 "windows",
51 "win32",
52 "mingw",
53 "mingw32",
54 "msys2_mingw_w64",
55}
56
57local type Config = record --!
58 home: any
59 lua_version: any
60 target_cpu: any
61 variables: {any: any}
62 os_getenv: any
63 rocks_trees: any
64 rocks_servers: any
65 check_certificates: any
66 dump_env: function()
67end
68
69local function detect_sysconfdir()
70 if not debug then
71 return
72 end
73 local src = debug.getinfo(1, "S").source
74 if not src then
75 return
76 end
77 src = dir.normalize(src)
78 if src:sub(1, 1) == "@" then
79 src = src:sub(2)
80 end
81 local basedir = src:match("^(.*)[\\/]luarocks[\\/]core[\\/]cfg.lua$")
82 if not basedir then
83 return
84 end
85 -- If installed in a Unix-like tree, use a Unix-like sysconfdir
86 local installdir = basedir:match("^(.*)[\\/]share[\\/]lua[\\/][^/]*$")
87 if installdir then
88 if installdir == "/usr" then
89 return "/etc/luarocks" --!
90 end
91 return dir.path(installdir, "etc", "luarocks")
92 end
93 -- Otherwise, use base directory of sources
94 return basedir
95end
96
97-- local load_config_file --!
98do
99 -- Create global environment for the config files;
100 local function env_for_config_file(cfg: Config, platforms: {any: any}): {any: any} --!
101 local platforms_copy = {}
102 for k,v in pairs(platforms) do
103 platforms_copy[k] = v
104 end
105
106 local e: {any: any} --!
107 e = {
108 home = cfg.home,
109 lua_version = cfg.lua_version,
110 platforms = platforms_copy,
111 processor = cfg.target_cpu, -- remains for compat reasons
112 target_cpu = cfg.target_cpu, -- replaces `processor`
113 os_getenv = os.getenv,
114 variables = cfg.variables or {},
115 dump_env = function()
116 -- debug function, calling it from a config file will show all
117 -- available globals to that config file
118 print(util.show_table(e, "global environment"))
119 end,
120 }
121 return e
122 end
123
124 -- Merge values from config files read into the `cfg` table
125 local function merge_overrides(cfg: Config, overrides: Config)
126 -- remove some stuff we do not want to integrate
127 overrides.os_getenv = nil
128 overrides.dump_env = nil
129 -- remove tables to be copied verbatim instead of deeply merged
130 if overrides.rocks_trees then cfg.rocks_trees = nil end
131 if overrides.rocks_servers then cfg.rocks_servers = nil end
132 -- perform actual merge
133 util.deep_merge(cfg, overrides)
134 end
135
136 local function update_platforms(platforms: {any: any}, overrides: {any: any})
137 if overrides[1] then
138 for k, _ in pairs(platforms) do
139 platforms[k] = nil
140 end
141 for _, v in ipairs(overrides) do
142 platforms[v] = true
143 end
144 -- set some fallback default in case the user provides an incomplete configuration.
145 -- LuaRocks expects a set of defaults to be available.
146 if not (platforms.unix or platforms.windows) then
147 platforms[is_windows and "windows" or "unix"] = true
148 end
149 end
150 end
151
152 -- Load config file and merge its contents into the `cfg` module table.
153 -- @return filepath of successfully loaded file or nil if it failed
154 local load_config_file = function(cfg: Config, platforms: {any: any}, filepath: string): string --!
155 local result, err, errcode = persist.load_into_table(filepath, env_for_config_file(cfg, platforms))
156 if (not result) and errcode ~= "open" then
157 -- errcode is either "load" or "run"; bad config file, so error out
158 return nil, err, "config"
159 end
160 if result then
161 -- success in loading and running, merge contents and exit
162 update_platforms(platforms, result.platforms)
163 result.platforms = nil
164 merge_overrides(cfg, result)
165 return filepath
166 end
167 return nil -- nothing was loaded
168 end
169end
170
171local platform_sets = {
172 freebsd = { unix = true, bsd = true, freebsd = true },
173 openbsd = { unix = true, bsd = true, openbsd = true },
174 dragonfly = { unix = true, bsd = true, dragonfly = true },
175 solaris = { unix = true, solaris = true },
176 windows = { windows = true, win32 = true },
177 cygwin = { unix = true, cygwin = true },
178 macosx = { unix = true, bsd = true, macosx = true, macos = true },
179 netbsd = { unix = true, bsd = true, netbsd = true },
180 haiku = { unix = true, haiku = true },
181 linux = { unix = true, linux = true },
182 mingw = { windows = true, win32 = true, mingw32 = true, mingw = true },
183 msys = { unix = true, cygwin = true, msys = true },
184 msys2_mingw_w64 = { windows = true, win32 = true, mingw32 = true, mingw = true, msys = true, msys2_mingw_w64 = true },
185}
186
187local function make_platforms(system: string): {any: boolean} --! :System
188 -- fallback to Unix in unknown systems
189 return platform_sets[system] or { unix = true }
190end
191
192--------------------------------------------------------------------------------
193
194local function make_defaults(lua_version: string, target_cpu: string, platforms: {any: any}, home: string): {any: any} --!
195
196 -- Configure defaults:
197 local defaults = {
198
199 local_by_default = false,
200 accept_unknown_fields = false,
201 fs_use_modules = true,
202 hooks_enabled = true,
203 deps_mode = "one",
204 no_manifest = false,
205 check_certificates = false,
206
207 cache_timeout = 60,
208 cache_fail_timeout = 86400,
209
210 lua_modules_path = dir.path("share", "lua", lua_version),
211 lib_modules_path = dir.path("lib", "lua", lua_version),
212 rocks_subdir = dir.path("lib", "luarocks", "rocks-"..lua_version),
213
214 arch = "unknown",
215 lib_extension = "unknown",
216 external_lib_extension = "unknown", --?
217 static_lib_extension = "unknown", --?
218 obj_extension = "unknown",
219 link_lua_explicitly = false,
220
221 external_deps_dirs = {}, --?
222 external_deps_patterns: {string} = {}, --?
223 runtime_external_deps_patterns: {string} = {}, --?
224
225 export_path_separator = "", --?
226 wrapper_suffix = "", --?
227
228
229 rocks_servers = {
230 {
231 "https://luarocks.org",
232 "https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/",
233 "https://loadk.com/luarocks/",
234 }
235 },
236 disabled_servers = {},
237
238 upload = {
239 server = "https://luarocks.org",
240 tool_version = "1.0.0",
241 api_version = "1",
242 },
243
244 lua_extension = "lua",
245 connection_timeout = 30, -- 0 = no timeout
246
247 makefile = "", --?
248 local_cache = "", --? "" | {""}
249 web_browser = "", --? "" | {""}
250 cmake_generator = "", --?
251 gcc_rpath = false, --?
252
253 variables = {
254 MAKE = os.getenv("MAKE") or "make",
255 CC = os.getenv("CC") or "cc",
256 LD = os.getenv("CC") or "ld",
257 AR = os.getenv("AR") or "ar",
258 RANLIB = os.getenv("RANLIB") or "ranlib",
259
260 CVS = "cvs",
261 GIT = "git",
262 SSCM = "sscm",
263 SVN = "svn",
264 HG = "hg",
265
266 GPG = "gpg",
267
268 RSYNC = "rsync",
269 WGET = "wget",
270 SCP = "scp",
271 CURL = "curl",
272
273 PWD = "pwd",
274 MKDIR = "mkdir",
275 RMDIR = "rmdir",
276 CP = "cp",
277 LN = "ln",
278 LS = "ls",
279 RM = "rm",
280 FIND = "find",
281 CHMOD = "chmod",
282 ICACLS = "icacls",
283 MKTEMP = "mktemp",
284
285 ZIP = "zip",
286 UNZIP = "unzip -n",
287 GUNZIP = "gunzip",
288 BUNZIP2 = "bunzip2",
289 TAR = "tar",
290
291 MD5SUM = "md5sum",
292 OPENSSL = "openssl",
293 MD5 = "md5",
294 TOUCH = "touch",
295
296 CMAKE = "cmake",
297 SEVENZ = "7z",
298
299 RSYNCFLAGS = "--exclude=.git -Oavz",
300 CURLNOCERTFLAG = "",
301 WGETNOCERTFLAG = "",
302
303 RC = "", --?
304 MT = "", --?
305 CFLAGS = "", --?
306 LDFLAGS = "", --?
307 LIBFLAG = "", --?
308 TEST = "", --?
309 LIB_EXTENSION = "", --?
310 OBJ_EXTENSION = "", --?
311 },
312
313 external_deps_subdirs = {
314 bin = "bin",
315 lib = "lib",
316 include = "include"
317 },
318 runtime_external_deps_subdirs = {
319 bin = "bin",
320 lib = "lib",
321 include = "include"
322 },
323 }
324
325 if platforms.windows then
326
327 defaults.arch = "win32-"..target_cpu
328 defaults.lib_extension = "dll"
329 defaults.external_lib_extension = "dll"
330 defaults.static_lib_extension = "lib"
331 defaults.obj_extension = "obj"
332 defaults.external_deps_dirs = {
333 dir.path("c:", "external"),
334 dir.path("c:", "windows", "system32"),
335 }
336
337 defaults.makefile = "Makefile.win"
338 defaults.variables.PWD = "echo %cd%"
339 defaults.variables.MKDIR = "md"
340 defaults.variables.MAKE = os.getenv("MAKE") or "nmake"
341 defaults.variables.CC = os.getenv("CC") or "cl"
342 defaults.variables.RC = os.getenv("WINDRES") or "rc"
343 defaults.variables.LD = os.getenv("LINK") or "link"
344 defaults.variables.MT = os.getenv("MT") or "mt"
345 defaults.variables.AR = os.getenv("AR") or "lib"
346 defaults.variables.CFLAGS = os.getenv("CFLAGS") or "/nologo /MD /O2"
347 defaults.variables.LDFLAGS = os.getenv("LDFLAGS")
348 defaults.variables.LIBFLAG = "/nologo /dll"
349
350 defaults.external_deps_patterns = {
351 bin = { "?.exe", "?.bat" },
352 lib = { "?.lib", "lib?.lib", "?.dll", "lib?.dll" },
353 include = { "?.h" }
354 }
355 defaults.runtime_external_deps_patterns = {
356 bin = { "?.exe", "?.bat" },
357 lib = { "?.dll", "lib?.dll" },
358 include = { "?.h" }
359 }
360 defaults.export_path_separator = ";"
361 defaults.wrapper_suffix = ".bat"
362
363 local localappdata = os.getenv("LOCALAPPDATA")
364 if not localappdata then
365 -- for Windows versions below Vista
366 localappdata = dir.path((os.getenv("USERPROFILE") or dir.path("c:", "Users", "All Users")), "Local Settings", "Application Data")
367 end
368 defaults.local_cache = dir.path(localappdata, "LuaRocks", "Cache")
369 defaults.web_browser = "start"
370
371 defaults.external_deps_subdirs.lib = { "lib", "", "bin" }
372 defaults.runtime_external_deps_subdirs.lib = { "lib", "", "bin" }
373 defaults.link_lua_explicitly = true
374 defaults.fs_use_modules = false
375 end
376
377 if platforms.mingw32 then
378 defaults.obj_extension = "o"
379 defaults.static_lib_extension = "a"
380 defaults.external_deps_dirs = {
381 dir.path("c:", "external"),
382 dir.path("c:", "mingw"),
383 dir.path("c:", "windows", "system32"),
384 }
385 defaults.cmake_generator = "MinGW Makefiles"
386 defaults.variables.MAKE = os.getenv("MAKE") or "mingw32-make"
387 if target_cpu == "x86_64" then
388 defaults.variables.CC = os.getenv("CC") or "x86_64-w64-mingw32-gcc"
389 defaults.variables.LD = os.getenv("CC") or "x86_64-w64-mingw32-gcc"
390 else
391 defaults.variables.CC = os.getenv("CC") or "mingw32-gcc"
392 defaults.variables.LD = os.getenv("CC") or "mingw32-gcc"
393 end
394 defaults.variables.AR = os.getenv("AR") or "ar"
395 defaults.variables.RC = os.getenv("WINDRES") or "windres"
396 defaults.variables.RANLIB = os.getenv("RANLIB") or "ranlib"
397 defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2"
398 defaults.variables.LDFLAGS = os.getenv("LDFLAGS")
399 defaults.variables.LIBFLAG = "-shared"
400 defaults.makefile = "Makefile"
401 defaults.external_deps_patterns = {
402 bin = { "?.exe", "?.bat" },
403 -- mingw lookup list from http://stackoverflow.com/a/15853231/1793220
404 -- ...should we keep ?.lib at the end? It's not in the above list.
405 lib = { "lib?.dll.a", "?.dll.a", "lib?.a", "cyg?.dll", "lib?.dll", "?.dll", "?.lib" },
406 include = { "?.h" }
407 }
408 defaults.runtime_external_deps_patterns = {
409 bin = { "?.exe", "?.bat" },
410 lib = { "cyg?.dll", "?.dll", "lib?.dll" },
411 include = { "?.h" }
412 }
413 defaults.link_lua_explicitly = true
414 end
415
416 if platforms.unix then
417 defaults.lib_extension = "so"
418 defaults.static_lib_extension = "a"
419 defaults.external_lib_extension = "so"
420 defaults.obj_extension = "o"
421 defaults.external_deps_dirs = { "/usr/local", "/usr", "/" }
422
423 defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2"
424 -- we pass -fPIC via CFLAGS because of old Makefile-based Lua projects
425 -- which didn't have -fPIC in their Makefiles but which honor CFLAGS
426 if not defaults.variables.CFLAGS:match("-fPIC") then
427 defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC"
428 end
429
430 defaults.variables.LDFLAGS = os.getenv("LDFLAGS")
431
432 defaults.cmake_generator = "Unix Makefiles"
433 defaults.variables.CC = os.getenv("CC") or "gcc"
434 defaults.variables.LD = os.getenv("CC") or "gcc"
435 defaults.gcc_rpath = true
436 defaults.variables.LIBFLAG = "-shared"
437 defaults.variables.TEST = "test"
438
439 defaults.external_deps_patterns = {
440 bin = { "?" },
441 lib = { "lib?.a", "lib?.so", "lib?.so.*" },
442 include = { "?.h" }
443 }
444 defaults.runtime_external_deps_patterns = {
445 bin = { "?" },
446 lib = { "lib?.so", "lib?.so.*" },
447 include = { "?.h" }
448 }
449 defaults.export_path_separator = ":"
450 defaults.wrapper_suffix = ""
451 local xdg_cache_home = os.getenv("XDG_CACHE_HOME") or home.."/.cache"
452 defaults.local_cache = xdg_cache_home.."/luarocks"
453 defaults.web_browser = "xdg-open"
454 end
455
456 if platforms.cygwin then
457 defaults.lib_extension = "so" -- can be overridden in the config file for mingw builds
458 defaults.arch = "cygwin-"..target_cpu
459 defaults.cmake_generator = "Unix Makefiles"
460 defaults.variables.CC = "echo -llua | xargs " .. (os.getenv("CC") or "gcc")
461 defaults.variables.LD = "echo -llua | xargs " .. (os.getenv("CC") or "gcc")
462 defaults.variables.LIBFLAG = "-shared"
463 defaults.link_lua_explicitly = true
464 end
465
466 if platforms.msys then
467 -- msys is basically cygwin made out of mingw, meaning the subsytem is unixish
468 -- enough, yet we can freely mix with native win32
469 defaults.external_deps_patterns = {
470 bin = { "?.exe", "?.bat", "?" },
471 lib = { "lib?.so", "lib?.so.*", "lib?.dll.a", "?.dll.a",
472 "lib?.a", "lib?.dll", "?.dll", "?.lib" },
473 include = { "?.h" }
474 }
475 defaults.runtime_external_deps_patterns = {
476 bin = { "?.exe", "?.bat" },
477 lib = { "lib?.so", "?.dll", "lib?.dll" },
478 include = { "?.h" }
479 }
480 if platforms.mingw then
481 -- MSYS2 can build Windows programs that depend on
482 -- msys-2.0.dll (based on Cygwin) but MSYS2 is also designed
483 -- for building native Windows programs by MinGW. These
484 -- programs don't depend on msys-2.0.dll.
485 local pipe = io.popen("cygpath --windows %MINGW_PREFIX%")
486 local mingw_prefix = pipe:read("*l")
487 pipe:close()
488 defaults.external_deps_dirs = {
489 mingw_prefix,
490 dir.path("c:", "windows", "system32"),
491 }
492 defaults.makefile = "Makefile"
493 defaults.cmake_generator = "MSYS Makefiles"
494 defaults.local_cache = dir.path(home, ".cache", "luarocks")
495 defaults.variables.MAKE = os.getenv("MAKE") or "make"
496 defaults.variables.CC = os.getenv("CC") or "gcc"
497 defaults.variables.RC = os.getenv("WINDRES") or "windres"
498 defaults.variables.LD = os.getenv("CC") or "gcc"
499 defaults.variables.MT = os.getenv("MT") or nil
500 defaults.variables.AR = os.getenv("AR") or "ar"
501 defaults.variables.RANLIB = os.getenv("RANLIB") or "ranlib"
502
503 defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2 -fPIC"
504 if not defaults.variables.CFLAGS:match("-fPIC") then
505 defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC"
506 end
507
508 defaults.variables.LIBFLAG = "-shared"
509 end
510 end
511
512 if platforms.bsd then
513 defaults.variables.MAKE = "gmake"
514 defaults.gcc_rpath = false
515 defaults.variables.CC = os.getenv("CC") or "cc"
516 defaults.variables.LD = os.getenv("CC") or defaults.variables.CC
517 end
518
519 if platforms.macosx then
520 defaults.variables.MAKE = os.getenv("MAKE") or "make"
521 defaults.external_lib_extension = "dylib"
522 defaults.arch = "macosx-"..target_cpu
523 defaults.variables.LIBFLAG = "-bundle -undefined dynamic_lookup -all_load"
524 local version = util.popen_read("sw_vers -productVersion")
525 if not (version:match("^%d+%.%d+%.%d+$") or version:match("^%d+%.%d+$")) then
526 version = "10.3"
527 end
528 version = vers.parse_version(version)
529 if version >= vers.parse_version("11.0") then
530 version = vers.parse_version("11.0")
531 elseif version >= vers.parse_version("10.10") then
532 version = vers.parse_version("10.8")
533 elseif version >= vers.parse_version("10.5") then
534 version = vers.parse_version("10.5")
535 else
536 defaults.gcc_rpath = false
537 end
538 defaults.variables.CC = "env MACOSX_DEPLOYMENT_TARGET="..tostring(version).." gcc"
539 defaults.variables.LD = "env MACOSX_DEPLOYMENT_TARGET="..tostring(version).." gcc"
540 defaults.web_browser = "open"
541
542 -- XCode SDK
543 local sdk_path = util.popen_read("xcrun --show-sdk-path 2>/dev/null")
544 if sdk_path then
545 table.insert(defaults.external_deps_dirs, sdk_path .. "/usr")
546 table.insert(defaults.external_deps_patterns.lib, 1, "lib?.tbd")
547 table.insert(defaults.runtime_external_deps_patterns.lib, 1, "lib?.tbd")
548 end
549
550 -- Homebrew
551 table.insert(defaults.external_deps_dirs, "/usr/local/opt")
552 defaults.external_deps_subdirs.lib = { "lib", "" }
553 defaults.runtime_external_deps_subdirs.lib = { "lib", "" }
554 table.insert(defaults.external_deps_patterns.lib, 1, "/?/lib/lib?.dylib")
555 table.insert(defaults.runtime_external_deps_patterns.lib, 1, "/?/lib/lib?.dylib")
556 end
557
558 if platforms.linux then
559 defaults.arch = "linux-"..target_cpu
560
561 local gcc_arch = util.popen_read("gcc -print-multiarch 2>/dev/null")
562 if gcc_arch and gcc_arch ~= "" then
563 defaults.external_deps_subdirs.lib = { "lib/" .. gcc_arch, "lib64", "lib" }
564 defaults.runtime_external_deps_subdirs.lib = { "lib/" .. gcc_arch, "lib64", "lib" }
565 else
566 defaults.external_deps_subdirs.lib = { "lib64", "lib" }
567 defaults.runtime_external_deps_subdirs.lib = { "lib64", "lib" }
568 end
569 end
570
571 if platforms.freebsd then
572 defaults.arch = "freebsd-"..target_cpu
573 elseif platforms.dragonfly then
574 defaults.arch = "dragonfly-"..target_cpu
575 elseif platforms.openbsd then
576 defaults.arch = "openbsd-"..target_cpu
577 elseif platforms.netbsd then
578 defaults.arch = "netbsd-"..target_cpu
579 elseif platforms.solaris then
580 defaults.arch = "solaris-"..target_cpu
581 defaults.variables.MAKE = "gmake"
582 end
583
584 -- Expose some more values detected by LuaRocks for use by rockspec authors.
585 defaults.variables.LIB_EXTENSION = defaults.lib_extension
586 defaults.variables.OBJ_EXTENSION = defaults.obj_extension
587
588 return defaults
589end
590
591local function use_defaults(cfg: Config, defaults: Config) --!
592
593 -- Populate variables with values from their 'defaults' counterparts
594 -- if they were not already set by user.
595 if not cfg.variables then
596 cfg.variables = {}
597 end
598 for k,v in pairs(defaults.variables) do
599 if not cfg.variables[k] then
600 cfg.variables[k] = v
601 end
602 end
603
604 util.deep_merge_under(cfg, defaults)
605
606 -- FIXME get rid of this
607 if not cfg.check_certificates then
608 cfg.variables.CURLNOCERTFLAG = "-k"
609 cfg.variables.WGETNOCERTFLAG = "--no-check-certificate"
610 end
611end
612
613--------------------------------------------------------------------------------
614
615local cfg = {}
616
617--- Initializes the LuaRocks configuration for variables, paths
618-- and OS detection.
619-- @param detected table containing information detected about the
620-- environment. All fields below are optional:
621-- * lua_version (in x.y format, e.g. "5.3")
622-- * lua_bindir (e.g. "/usr/local/bin")
623-- * lua_dir (e.g. "/usr/local")
624-- * lua (e.g. "/usr/local/bin/lua-5.3")
625-- * project_dir (a string with the path of the project directory
626-- when using per-project environments, as created with `luarocks init`)
627-- @param warning a logging function for warnings that takes a string
628-- @return true on success; nil and an error message on failure.
629function cfg.init(detected: {any: any}, warning: function(string)): nil, string, string --!
630 detected = detected or {}
631
632 local exit_ok = true
633 local exit_err = nil
634 local exit_what = nil
635
636 local hc_ok, hardcoded = pcall(require, "luarocks.core.hardcoded")
637 if not hc_ok then
638 hardcoded = {}
639 end
640
641 local init = cfg.init
642
643 ----------------------------------------
644 -- Reset the cfg table.
645 ----------------------------------------
646
647 for k, _ in pairs(cfg) do
648 cfg[k] = nil
649 end
650
651 cfg.program_version = program_version
652
653 if hardcoded.IS_BINARY then
654 cfg.is_binary = true
655 end
656
657 -- Use detected values as defaults, overridable via config files or CLI args
658
659 local hardcoded_lua = hardcoded.LUA
660 local hardcoded_lua_dir = hardcoded.LUA_DIR
661 local hardcoded_lua_bindir = hardcoded.LUA_BINDIR
662 local hardcoded_lua_incdir = hardcoded.LUA_INCDIR
663 local hardcoded_lua_libdir = hardcoded.LUA_LIBDIR
664 local hardcoded_lua_version = hardcoded.LUA_VERSION or _VERSION:sub(5)
665
666 -- if --lua-version or --lua-dir are passed from the CLI,
667 -- don't use the hardcoded paths at all
668 if detected.given_lua_version or detected.given_lua_dir then
669 hardcoded_lua = nil
670 hardcoded_lua_dir = nil
671 hardcoded_lua_bindir = nil
672 hardcoded_lua_incdir = nil
673 hardcoded_lua_libdir = nil
674 hardcoded_lua_version = nil
675 end
676
677 cfg.lua_version = detected.lua_version or hardcoded_lua_version
678 cfg.project_dir = (not hardcoded.FORCE_CONFIG) and detected.project_dir
679
680 do
681 local lua = detected.lua or hardcoded_lua
682 local lua_dir = detected.lua_dir or hardcoded_lua_dir
683 local lua_bindir = detected.lua_bindir or hardcoded_lua_bindir
684 cfg.variables = {
685 LUA = lua,
686 LUA_DIR = lua_dir,
687 LUA_BINDIR = lua_bindir,
688 LUA_LIBDIR = hardcoded_lua_libdir,
689 LUA_INCDIR = hardcoded_lua_incdir,
690 }
691 end
692
693 cfg.init = init
694
695 ----------------------------------------
696 -- System detection.
697 ----------------------------------------
698
699 -- A proper build of LuaRocks will hardcode the system
700 -- and proc values with hardcoded.SYSTEM and hardcoded.PROCESSOR.
701 -- If that is not available, we try to identify the system.
702 local system, processor = sysdetect.detect()
703 if hardcoded.SYSTEM then
704 system = hardcoded.SYSTEM
705 end
706 if hardcoded.PROCESSOR then
707 processor = hardcoded.PROCESSOR
708 end
709
710 if system == "windows" then
711 if os.getenv("VCINSTALLDIR") then
712 -- running from the Development Command prompt for VS 2017
713 system = "windows"
714 else
715 local msystem = os.getenv("MSYSTEM")
716 if msystem == nil then
717 system = "mingw"
718 elseif msystem == "MSYS" then
719 system = "msys"
720 else
721 -- MINGW32 or MINGW64
722 system = "msys2_mingw_w64"
723 end
724 end
725 end
726
727 cfg.target_cpu = processor
728
729 local platforms = make_platforms(system)
730
731 ----------------------------------------
732 -- Platform is determined.
733 -- Let's load the config files.
734 ----------------------------------------
735
736 local sys_config_file: string --?
737 local home_config_file: string
738 local project_config_file: string
739
740 local config_file_name = "config-"..cfg.lua_version..".lua"
741
742 do
743 local sysconfdir = os.getenv("LUAROCKS_SYSCONFDIR") or hardcoded.SYSCONFDIR
744 if platforms.windows and not platforms.msys2_mingw_w64 then
745 cfg.home = os.getenv("APPDATA") or "c:" --!
746 cfg.home_tree = dir.path(cfg.home, "luarocks") --!
747 cfg.sysconfdir = sysconfdir or dir.path((os.getenv("PROGRAMFILES") or "c:"), "luarocks") --!
748 else
749 cfg.home = os.getenv("HOME") or "" --!
750 cfg.home_tree = dir.path(cfg.home, ".luarocks") --!
751 cfg.sysconfdir = sysconfdir or detect_sysconfdir() or "/etc/luarocks" --!
752 end
753 end
754
755 -- Load system configuration file
756 sys_config_file = dir.path(cfg.sysconfdir, config_file_name)
757 local sys_config_ok, err = load_config_file(cfg, platforms, sys_config_file)
758 if err then
759 exit_ok, exit_err, exit_what = nil, err, "config"
760 end
761
762 -- Load user configuration file (if allowed)
763 local home_config_ok: boolean
764 local project_config_ok: boolean
765 if not hardcoded.FORCE_CONFIG then
766 local env_var = "LUAROCKS_CONFIG_" .. cfg.lua_version:gsub("%.", "_")
767 local env_value = os.getenv(env_var)
768 if not env_value then
769 env_var = "LUAROCKS_CONFIG"
770 env_value = os.getenv(env_var)
771 end
772 -- first try environment provided file, so we can explicitly warn when it is missing
773 if env_value then
774 local env_ok, err = load_config_file(cfg, platforms, env_value)
775 if err then
776 exit_ok, exit_err, exit_what = nil, err, "config"
777 elseif warning and not env_ok then
778 warning("Warning: could not load configuration file `"..env_value.."` given in environment variable "..env_var.."\n")
779 end
780 if env_ok then
781 home_config_ok = true
782 home_config_file = env_value
783 end
784 end
785
786 -- try XDG config home
787 if platforms.unix and not home_config_ok then
788 local xdg_config_home = os.getenv("XDG_CONFIG_HOME") or dir.path(cfg.home, ".config")
789 cfg.homeconfdir = dir.path(xdg_config_home, "luarocks")
790 home_config_file = dir.path(cfg.homeconfdir, config_file_name)
791 home_config_ok, err = load_config_file(cfg, platforms, home_config_file)
792 if err then
793 exit_ok, exit_err, exit_what = nil, err, "config"
794 end
795 end
796
797 -- try the alternative defaults if there was no environment specified file or it didn't work
798 if not home_config_ok then
799 cfg.homeconfdir = cfg.home_tree
800 home_config_file = dir.path(cfg.homeconfdir, config_file_name)
801 home_config_ok, err = load_config_file(cfg, platforms, home_config_file)
802 if err then
803 exit_ok, exit_err, exit_what = nil, err, "config"
804 end
805 end
806
807 -- finally, use the project-specific config file if any
808 if cfg.project_dir then
809 project_config_file = dir.path(cfg.project_dir, ".luarocks", config_file_name)
810 project_config_ok, err = load_config_file(cfg, platforms, project_config_file)
811 if err then
812 exit_ok, exit_err, exit_what = nil, err, "config"
813 end
814 end
815 end
816
817 -- backwards compatibility:
818 if cfg.lua_interpreter and cfg.variables.LUA_BINDIR and not cfg.variables.LUA then
819 cfg.variables.LUA = dir.path(cfg.variables.LUA_BINDIR, cfg.lua_interpreter)
820 end
821
822 ----------------------------------------
823 -- Config files are loaded.
824 -- Let's finish up the cfg table.
825 ----------------------------------------
826
827 -- Settings given via the CLI (i.e. --lua-dir) take precedence over config files.
828 cfg.project_dir = detected.given_project_dir or cfg.project_dir
829 cfg.lua_version = detected.given_lua_version or cfg.lua_version
830 if detected.given_lua_dir then
831 cfg.variables.LUA = detected.lua
832 cfg.variables.LUA_DIR = detected.given_lua_dir
833 cfg.variables.LUA_BINDIR = detected.lua_bindir
834 cfg.variables.LUA_LIBDIR = nil
835 cfg.variables.LUA_INCDIR = nil
836 end
837
838 -- Build a default list of rocks trees if not given
839 if cfg.rocks_trees == nil then
840 cfg.rocks_trees = {}
841 if cfg.home_tree then
842 table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree } )
843 end
844 if hardcoded.PREFIX and hardcoded.PREFIX ~= cfg.home_tree then
845 table.insert(cfg.rocks_trees, { name = "system", root = hardcoded.PREFIX } )
846 end
847 end
848
849 local defaults = make_defaults(cfg.lua_version, processor, platforms, cfg.home)
850
851 if platforms.windows and hardcoded.WIN_TOOLS then
852 local tools = { "SEVENZ", "CP", "FIND", "LS", "MD5SUM", "WGET", }
853 for _, tool in ipairs(tools) do
854 defaults.variables[tool] = '"' .. dir.path(hardcoded.WIN_TOOLS, defaults.variables[tool] .. '.exe') .. '"'
855 end
856 else
857 defaults.fs_use_modules = true
858 end
859
860 -- if only cfg.variables.LUA is given in config files,
861 -- derive LUA_BINDIR and LUA_DIR from them.
862 if cfg.variables.LUA and not cfg.variables.LUA_BINDIR then
863 cfg.variables.LUA_BINDIR = cfg.variables.LUA:match("^(.*)[\\/][^\\/]*$")
864 if not cfg.variables.LUA_DIR then
865 cfg.variables.LUA_DIR = cfg.variables.LUA_BINDIR:gsub("[\\/]bin$", "") or cfg.variables.LUA_BINDIR
866 end
867 end
868
869 use_defaults(cfg, defaults)
870
871 cfg.user_agent = "LuaRocks/"..cfg.program_version.." "..cfg.arch
872
873 cfg.config_files = {
874 project = cfg.project_dir and {
875 file = project_config_file,
876 found = not not project_config_ok,
877 },
878 system = {
879 file = sys_config_file,
880 found = not not sys_config_ok,
881 },
882 user = {
883 file = home_config_file,
884 found = not not home_config_ok,
885 },
886 nearest = project_config_ok
887 and project_config_file
888 or (home_config_ok
889 and home_config_file
890 or sys_config_file),
891 }
892
893 cfg.cache = {}
894
895 ----------------------------------------
896 -- Attributes of cfg are set.
897 -- Let's add some methods.
898 ----------------------------------------
899
900 do
901 local function make_paths_from_tree(tree: {any: string}): string, string, string
902 local lua_path, lib_path, bin_path: string, string, string
903 if type(tree) == "string" then
904 lua_path = dir.path(tree, cfg.lua_modules_path)
905 lib_path = dir.path(tree, cfg.lib_modules_path)
906 bin_path = dir.path(tree, "bin")
907 else
908 lua_path = tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path)
909 lib_path = tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path)
910 bin_path = tree.bin_dir or dir.path(tree.root, "bin")
911 end
912 return lua_path, lib_path, bin_path
913 end
914
915 function cfg.package_paths(current)
916 local new_path, new_cpath, new_bin = {}, {}, {}
917 local function add_tree_to_paths(tree)
918 local lua_path, lib_path, bin_path = make_paths_from_tree(tree)
919 table.insert(new_path, dir.path(lua_path, "?.lua"))
920 table.insert(new_path, dir.path(lua_path, "?", "init.lua"))
921 table.insert(new_cpath, dir.path(lib_path, "?."..cfg.lib_extension))
922 table.insert(new_bin, bin_path)
923 end
924 if current then
925 add_tree_to_paths(current)
926 end
927 for _,tree in ipairs(cfg.rocks_trees) do
928 add_tree_to_paths(tree)
929 end
930 return table.concat(new_path, ";"), table.concat(new_cpath, ";"), table.concat(new_bin, cfg.export_path_separator)
931 end
932 end
933
934 function cfg.init_package_paths()
935 local lr_path, lr_cpath, lr_bin = cfg.package_paths()
936 package.path = util.cleanup_path(package.path .. ";" .. lr_path, ";", cfg.lua_version, true)
937 package.cpath = util.cleanup_path(package.cpath .. ";" .. lr_cpath, ";", cfg.lua_version, true)
938 end
939
940 --- Check if platform was detected
941 -- @param name string: The platform name to check.
942 -- @return boolean: true if LuaRocks is currently running on queried platform.
943 function cfg.is_platform(name: string): boolean
944 assert(type(name) == "string")
945 return platforms[name]
946 end
947
948 -- @param direction (optional) "least-specific-first" (default) or "most-specific-first"
949 function cfg.each_platform(direction: string): function(): string
950 direction = direction or "least-specific-first"
951 local i, delta: integer, integer
952 if direction == "least-specific-first" then
953 i = 0
954 delta = 1
955 else
956 i = #platform_order + 1
957 delta = -1
958 end
959 return function()
960 local p: string
961 repeat
962 i = i + delta
963 p = platform_order[i]
964 until (not p) or platforms[p]
965 return p
966 end
967 end
968
969 function cfg.print_platforms(): string
970 local platform_keys = {}
971 for k,_ in pairs(platforms) do
972 table.insert(platform_keys, k)
973 end
974 table.sort(platform_keys)
975 return table.concat(platform_keys, ", ")
976 end
977
978 return exit_ok, exit_err, exit_what
979end
980
981return cfg \ No newline at end of file