diff options
Diffstat (limited to 'src/luarocks/cfg.lua')
-rw-r--r-- | src/luarocks/cfg.lua | 120 |
1 files changed, 62 insertions, 58 deletions
diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index e7e091e9..21cfbf6d 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua | |||
@@ -13,10 +13,12 @@ | |||
13 | local rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, tonumber, type, assert, _VERSION = | 13 | local rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, tonumber, type, assert, _VERSION = |
14 | rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, tonumber, type, assert, _VERSION | 14 | rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, tonumber, type, assert, _VERSION |
15 | 15 | ||
16 | module("luarocks.cfg") | 16 | --module("luarocks.cfg") |
17 | local cfg = {} | ||
18 | package.loaded["luarocks.cfg"] = cfg | ||
17 | 19 | ||
18 | lua_version = _VERSION:sub(5) | 20 | cfg.lua_version = _VERSION:sub(5) |
19 | local version_suffix = lua_version:gsub("%.", "_") | 21 | local version_suffix = cfg.lua_version:gsub("%.", "_") |
20 | 22 | ||
21 | -- Load site-local global configurations | 23 | -- Load site-local global configurations |
22 | local ok, site_config = pcall(require, "luarocks.site_config_"..version_suffix) | 24 | local ok, site_config = pcall(require, "luarocks.site_config_"..version_suffix) |
@@ -28,14 +30,14 @@ if not ok then | |||
28 | site_config = {} | 30 | site_config = {} |
29 | end | 31 | end |
30 | 32 | ||
31 | _M.site_config = site_config | 33 | cfg.site_config = site_config |
32 | 34 | ||
33 | program_version = "2.1.2" | 35 | cfg.program_version = "2.1.2" |
34 | major_version = program_version:match("([^.]%.[^.])") | 36 | cfg.major_version = cfg.program_version:match("([^.]%.[^.])") |
35 | 37 | ||
36 | local persist = require("luarocks.persist") | 38 | local persist = require("luarocks.persist") |
37 | 39 | ||
38 | _M.errorcodes = setmetatable({ | 40 | cfg.errorcodes = setmetatable({ |
39 | OK = 0, | 41 | OK = 0, |
40 | UNSPECIFIED = 1, | 42 | UNSPECIFIED = 1, |
41 | PERMISSIONDENIED = 2, | 43 | PERMISSIONDENIED = 2, |
@@ -58,7 +60,7 @@ if popen_ok then | |||
58 | else | 60 | else |
59 | io.stderr:write("Your version of Lua does not support io.popen,\n") | 61 | io.stderr:write("Your version of Lua does not support io.popen,\n") |
60 | io.stderr:write("which is required by LuaRocks. Please check your Lua installation.\n") | 62 | io.stderr:write("which is required by LuaRocks. Please check your Lua installation.\n") |
61 | os.exit(_M.errorcodes.UNSPECIFIED) | 63 | os.exit(cfg.errorcodes.UNSPECIFIED) |
62 | end | 64 | end |
63 | 65 | ||
64 | -- System detection: | 66 | -- System detection: |
@@ -124,27 +126,27 @@ local sys_config_ok, home_config_ok = false, false | |||
124 | local extra_luarocks_module_dir | 126 | local extra_luarocks_module_dir |
125 | sys_config_dir = site_config.LUAROCKS_SYSCONFDIR | 127 | sys_config_dir = site_config.LUAROCKS_SYSCONFDIR |
126 | if detected.windows then | 128 | if detected.windows then |
127 | home = os.getenv("APPDATA") or "c:" | 129 | cfg.home = os.getenv("APPDATA") or "c:" |
128 | sys_config_dir = sys_config_dir or "c:/luarocks" | 130 | sys_config_dir = sys_config_dir or "c:/luarocks" |
129 | home_config_dir = home.."/luarocks" | 131 | home_config_dir = cfg.home.."/luarocks" |
130 | home_tree = home.."/luarocks/" | 132 | cfg.home_tree = cfg.home.."/luarocks/" |
131 | else | 133 | else |
132 | home = os.getenv("HOME") or "" | 134 | cfg.home = os.getenv("HOME") or "" |
133 | sys_config_dir = sys_config_dir or "/etc/luarocks" | 135 | sys_config_dir = sys_config_dir or "/etc/luarocks" |
134 | home_config_dir = home.."/.luarocks" | 136 | home_config_dir = cfg.home.."/.luarocks" |
135 | home_tree = home.."/.luarocks/" | 137 | cfg.home_tree = cfg.home.."/.luarocks/" |
136 | end | 138 | end |
137 | 139 | ||
138 | variables = {} | 140 | cfg.variables = {} |
139 | rocks_trees = {} | 141 | cfg.rocks_trees = {} |
140 | 142 | ||
141 | sys_config_file = site_config.LUAROCKS_SYSCONFIG or sys_config_dir.."/config-"..lua_version..".lua" | 143 | sys_config_file = site_config.LUAROCKS_SYSCONFIG or sys_config_dir.."/config-"..cfg.lua_version..".lua" |
142 | local err | 144 | local err |
143 | sys_config_ok, err = persist.load_into_table(sys_config_file, _M) | 145 | sys_config_ok, err = persist.load_into_table(sys_config_file, cfg) |
144 | 146 | ||
145 | if not sys_config_ok then | 147 | if not sys_config_ok then |
146 | sys_config_file = sys_config_dir.."/config.lua" | 148 | sys_config_file = sys_config_dir.."/config.lua" |
147 | sys_config_ok, err = persist.load_into_table(sys_config_file, _M) | 149 | sys_config_ok, err = persist.load_into_table(sys_config_file, cfg) |
148 | end | 150 | end |
149 | if err and sys_config_ok == nil then | 151 | if err and sys_config_ok == nil then |
150 | io.stderr:write(err.."\n") | 152 | io.stderr:write(err.."\n") |
@@ -154,25 +156,25 @@ if not site_config.LUAROCKS_FORCE_CONFIG then | |||
154 | local home_overrides, err | 156 | local home_overrides, err |
155 | home_config_file = os.getenv("LUAROCKS_CONFIG_" .. version_suffix) or os.getenv("LUAROCKS_CONFIG") | 157 | home_config_file = os.getenv("LUAROCKS_CONFIG_" .. version_suffix) or os.getenv("LUAROCKS_CONFIG") |
156 | if home_config_file then | 158 | if home_config_file then |
157 | home_overrides, err = persist.load_into_table(home_config_file, { home = home, lua_version = lua_version }) | 159 | home_overrides, err = persist.load_into_table(home_config_file, { home = cfg.home, lua_version = cfg.lua_version }) |
158 | else | 160 | else |
159 | home_config_file = home_config_dir.."/config-"..lua_version..".lua" | 161 | home_config_file = home_config_dir.."/config-"..cfg.lua_version..".lua" |
160 | home_overrides, err = persist.load_into_table(home_config_file, { home = home, lua_version = lua_version }) | 162 | home_overrides, err = persist.load_into_table(home_config_file, { home = cfg.home, lua_version = cfg.lua_version }) |
161 | if not home_overrides then | 163 | if not home_overrides then |
162 | home_config_file = home_config_dir.."/config.lua" | 164 | home_config_file = home_config_dir.."/config.lua" |
163 | home_overrides, err = persist.load_into_table(home_config_file, { home = home, lua_version = lua_version }) | 165 | home_overrides, err = persist.load_into_table(home_config_file, { home = cfg.home, lua_version = cfg.lua_version }) |
164 | end | 166 | end |
165 | end | 167 | end |
166 | if home_overrides then | 168 | if home_overrides then |
167 | home_config_ok = true | 169 | home_config_ok = true |
168 | local util = require("luarocks.util") | 170 | local util = require("luarocks.util") |
169 | if home_overrides.rocks_trees then | 171 | if home_overrides.rocks_trees then |
170 | _M.rocks_trees = nil | 172 | cfg.rocks_trees = nil |
171 | end | 173 | end |
172 | if home_overrides.rocks_servers then | 174 | if home_overrides.rocks_servers then |
173 | _M.rocks_servers = nil | 175 | cfg.rocks_servers = nil |
174 | end | 176 | end |
175 | util.deep_merge(_M, home_overrides) | 177 | util.deep_merge(cfg, home_overrides) |
176 | else -- nil or false | 178 | else -- nil or false |
177 | home_config_ok = home_overrides | 179 | home_config_ok = home_overrides |
178 | if err and home_config_ok == nil then | 180 | if err and home_config_ok == nil then |
@@ -181,18 +183,18 @@ if not site_config.LUAROCKS_FORCE_CONFIG then | |||
181 | end | 183 | end |
182 | end | 184 | end |
183 | 185 | ||
184 | if not next(rocks_trees) then | 186 | if not next(cfg.rocks_trees) then |
185 | if home_tree then | 187 | if cfg.home_tree then |
186 | table.insert(rocks_trees, { name = "user", root = home_tree } ) | 188 | table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree } ) |
187 | end | 189 | end |
188 | if site_config.LUAROCKS_ROCKS_TREE then | 190 | if site_config.LUAROCKS_ROCKS_TREE then |
189 | table.insert(rocks_trees, { name = "system", root = site_config.LUAROCKS_ROCKS_TREE } ) | 191 | table.insert(cfg.rocks_trees, { name = "system", root = site_config.LUAROCKS_ROCKS_TREE } ) |
190 | end | 192 | end |
191 | end | 193 | end |
192 | 194 | ||
193 | -- Configure defaults: | 195 | -- Configure defaults: |
194 | 196 | ||
195 | local root = rocks_trees[#rocks_trees] | 197 | local root = cfg.rocks_trees[#cfg.rocks_trees] |
196 | local defaults = { | 198 | local defaults = { |
197 | 199 | ||
198 | local_by_default = false, | 200 | local_by_default = false, |
@@ -202,8 +204,8 @@ local defaults = { | |||
202 | hooks_enabled = true, | 204 | hooks_enabled = true, |
203 | deps_mode = "one", | 205 | deps_mode = "one", |
204 | 206 | ||
205 | lua_modules_path = "/share/lua/"..lua_version, | 207 | lua_modules_path = "/share/lua/"..cfg.lua_version, |
206 | lib_modules_path = "/lib/lua/"..lua_version, | 208 | lib_modules_path = "/lib/lua/"..cfg.lua_version, |
207 | rocks_subdir = site_config.LUAROCKS_ROCKS_SUBDIR or "/lib/luarocks/rocks", | 209 | rocks_subdir = site_config.LUAROCKS_ROCKS_SUBDIR or "/lib/luarocks/rocks", |
208 | 210 | ||
209 | arch = "unknown", | 211 | arch = "unknown", |
@@ -286,7 +288,7 @@ local defaults = { | |||
286 | } | 288 | } |
287 | 289 | ||
288 | if detected.windows then | 290 | if detected.windows then |
289 | local full_prefix = site_config.LUAROCKS_PREFIX.."\\"..major_version | 291 | local full_prefix = site_config.LUAROCKS_PREFIX.."\\"..cfg.major_version |
290 | extra_luarocks_module_dir = full_prefix.."\\lua\\?.lua" | 292 | extra_luarocks_module_dir = full_prefix.."\\lua\\?.lua" |
291 | 293 | ||
292 | home_config_file = home_config_file and home_config_file:gsub("\\","/") | 294 | home_config_file = home_config_file and home_config_file:gsub("\\","/") |
@@ -297,9 +299,9 @@ if detected.windows then | |||
297 | defaults.external_lib_extension = "dll" | 299 | defaults.external_lib_extension = "dll" |
298 | defaults.obj_extension = "obj" | 300 | defaults.obj_extension = "obj" |
299 | defaults.external_deps_dirs = { "c:/external/" } | 301 | defaults.external_deps_dirs = { "c:/external/" } |
300 | defaults.variables.LUA_BINDIR = site_config.LUA_BINDIR and site_config.LUA_BINDIR:gsub("\\", "/") or "c:/lua"..lua_version.."/bin" | 302 | defaults.variables.LUA_BINDIR = site_config.LUA_BINDIR and site_config.LUA_BINDIR:gsub("\\", "/") or "c:/lua"..cfg.lua_version.."/bin" |
301 | defaults.variables.LUA_INCDIR = site_config.LUA_INCDIR and site_config.LUA_INCDIR:gsub("\\", "/") or "c:/lua"..lua_version.."/include" | 303 | defaults.variables.LUA_INCDIR = site_config.LUA_INCDIR and site_config.LUA_INCDIR:gsub("\\", "/") or "c:/lua"..cfg.lua_version.."/include" |
302 | defaults.variables.LUA_LIBDIR = site_config.LUA_LIBDIR and site_config.LUA_LIBDIR:gsub("\\", "/") or "c:/lua"..lua_version.."/lib" | 304 | defaults.variables.LUA_LIBDIR = site_config.LUA_LIBDIR and site_config.LUA_LIBDIR:gsub("\\", "/") or "c:/lua"..cfg.lua_version.."/lib" |
303 | defaults.cmake_generator = "MinGW Makefiles" | 305 | defaults.cmake_generator = "MinGW Makefiles" |
304 | defaults.makefile = "Makefile.win" | 306 | defaults.makefile = "Makefile.win" |
305 | defaults.variables.MAKE = "nmake" | 307 | defaults.variables.MAKE = "nmake" |
@@ -308,7 +310,7 @@ if detected.windows then | |||
308 | defaults.variables.WRAPPER = full_prefix.."\\rclauncher.c" | 310 | defaults.variables.WRAPPER = full_prefix.."\\rclauncher.c" |
309 | defaults.variables.LD = "link" | 311 | defaults.variables.LD = "link" |
310 | defaults.variables.MT = "mt" | 312 | defaults.variables.MT = "mt" |
311 | defaults.variables.LUALIB = "lua"..lua_version..".lib" | 313 | defaults.variables.LUALIB = "lua"..cfg.lua_version..".lib" |
312 | defaults.variables.CFLAGS = "/MD /O2" | 314 | defaults.variables.CFLAGS = "/MD /O2" |
313 | defaults.variables.LIBFLAG = "/dll" | 315 | defaults.variables.LIBFLAG = "/dll" |
314 | 316 | ||
@@ -400,7 +402,7 @@ if detected.unix then | |||
400 | defaults.export_lua_path = "export LUA_PATH='%s'" | 402 | defaults.export_lua_path = "export LUA_PATH='%s'" |
401 | defaults.export_lua_cpath = "export LUA_CPATH='%s'" | 403 | defaults.export_lua_cpath = "export LUA_CPATH='%s'" |
402 | defaults.wrapper_suffix = "" | 404 | defaults.wrapper_suffix = "" |
403 | defaults.local_cache = home.."/.cache/luarocks" | 405 | defaults.local_cache = cfg.home.."/.cache/luarocks" |
404 | if not defaults.variables.CFLAGS:match("-fPIC") then | 406 | if not defaults.variables.CFLAGS:match("-fPIC") then |
405 | defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC" | 407 | defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC" |
406 | end | 408 | end |
@@ -474,11 +476,11 @@ defaults.variables.LUAROCKS_PREFIX = site_config.LUAROCKS_PREFIX | |||
474 | defaults.variables.LUA = site_config.LUA_DIR_SET and (defaults.variables.LUA_BINDIR.."/"..defaults.lua_interpreter) or defaults.lua_interpreter | 476 | defaults.variables.LUA = site_config.LUA_DIR_SET and (defaults.variables.LUA_BINDIR.."/"..defaults.lua_interpreter) or defaults.lua_interpreter |
475 | 477 | ||
476 | -- Add built-in modules to rocks_provided | 478 | -- Add built-in modules to rocks_provided |
477 | defaults.rocks_provided["lua"] = lua_version.."-1" | 479 | defaults.rocks_provided["lua"] = cfg.lua_version.."-1" |
478 | 480 | ||
479 | if lua_version >= "5.2" then | 481 | if cfg.lua_version >= "5.2" then |
480 | -- Lua 5.2+ | 482 | -- Lua 5.2+ |
481 | defaults.rocks_provided["bit32"] = lua_version.."-1" | 483 | defaults.rocks_provided["bit32"] = cfg.lua_version.."-1" |
482 | end | 484 | end |
483 | 485 | ||
484 | if package.loaded.jit then | 486 | if package.loaded.jit then |
@@ -493,12 +495,12 @@ end | |||
493 | -- Populate some arrays with values from their 'defaults' counterparts | 495 | -- Populate some arrays with values from their 'defaults' counterparts |
494 | -- if they were not already set by user. | 496 | -- if they were not already set by user. |
495 | for _, entry in ipairs({"variables", "rocks_provided"}) do | 497 | for _, entry in ipairs({"variables", "rocks_provided"}) do |
496 | if not _M[entry] then | 498 | if not cfg[entry] then |
497 | _M[entry] = {} | 499 | cfg[entry] = {} |
498 | end | 500 | end |
499 | for k,v in pairs(defaults[entry]) do | 501 | for k,v in pairs(defaults[entry]) do |
500 | if not _M[entry][k] then | 502 | if not cfg[entry][k] then |
501 | _M[entry][k] = v | 503 | cfg[entry][k] = v |
502 | end | 504 | end |
503 | end | 505 | end |
504 | end | 506 | end |
@@ -513,38 +515,40 @@ local cfg_mt = { | |||
513 | return default | 515 | return default |
514 | end | 516 | end |
515 | } | 517 | } |
516 | setmetatable(_M, cfg_mt) | 518 | setmetatable(cfg, cfg_mt) |
517 | 519 | ||
518 | function package_paths() | 520 | function cfg.package_paths() |
519 | local new_path, new_cpath = { extra_luarocks_module_dir }, {} | 521 | local new_path, new_cpath = { extra_luarocks_module_dir }, {} |
520 | for _,tree in ipairs(rocks_trees) do | 522 | for _,tree in ipairs(cfg.rocks_trees) do |
521 | if type(tree) == "string" then | 523 | if type(tree) == "string" then |
522 | table.insert(new_path, 1, tree..lua_modules_path.."/?.lua;"..tree..lua_modules_path.."/?/init.lua") | 524 | table.insert(new_path, 1, tree..cfg.lua_modules_path.."/?.lua;"..tree..cfg.lua_modules_path.."/?/init.lua") |
523 | table.insert(new_cpath, 1, tree..lib_modules_path.."/?."..lib_extension) | 525 | table.insert(new_cpath, 1, tree..cfg.lib_modules_path.."/?."..cfg.lib_extension) |
524 | else | 526 | else |
525 | table.insert(new_path, 1, (tree.lua_dir or tree.root..lua_modules_path).."/?.lua;".. | 527 | table.insert(new_path, 1, (tree.lua_dir or tree.root..cfg.lua_modules_path).."/?.lua;".. |
526 | (tree.lua_dir or tree.root..lua_modules_path).."/?/init.lua") | 528 | (tree.lua_dir or tree.root..cfg.lua_modules_path).."/?/init.lua") |
527 | table.insert(new_cpath, 1, (tree.lib_dir or tree.root..lib_modules_path).."/?."..lib_extension) | 529 | table.insert(new_cpath, 1, (tree.lib_dir or tree.root..cfg.lib_modules_path).."/?."..cfg.lib_extension) |
528 | end | 530 | end |
529 | end | 531 | end |
530 | return table.concat(new_path, ";"), table.concat(new_cpath, ";") | 532 | return table.concat(new_path, ";"), table.concat(new_cpath, ";") |
531 | end | 533 | end |
532 | 534 | ||
533 | function which_config() | 535 | function cfg.which_config() |
534 | return sys_config_file, sys_config_ok, home_config_file, home_config_ok | 536 | return sys_config_file, sys_config_ok, home_config_file, home_config_ok |
535 | end | 537 | end |
536 | 538 | ||
537 | user_agent = "LuaRocks/"..program_version.." "..arch | 539 | cfg.user_agent = "LuaRocks/"..cfg.program_version.." "..cfg.arch |
538 | 540 | ||
539 | --- Check if platform was detected | 541 | --- Check if platform was detected |
540 | -- @param query string: The platform name to check. | 542 | -- @param query string: The platform name to check. |
541 | -- @return boolean: true if LuaRocks is currently running on queried platform. | 543 | -- @return boolean: true if LuaRocks is currently running on queried platform. |
542 | function is_platform(query) | 544 | function cfg.is_platform(query) |
543 | assert(type(query) == "string") | 545 | assert(type(query) == "string") |
544 | 546 | ||
545 | for _, platform in ipairs(platforms) do | 547 | for _, platform in ipairs(cfg.platforms) do |
546 | if platform == query then | 548 | if platform == query then |
547 | return true | 549 | return true |
548 | end | 550 | end |
549 | end | 551 | end |
550 | end | 552 | end |
553 | |||
554 | return cfg | ||