/src/luarocks/cfg.lua

http://github.com/keplerproject/luarocks · Lua · 730 lines · 641 code · 47 blank · 42 comment · 73 complexity · ac48942e467a4073d186c05e4de1c268 MD5 · raw file

  1. --- Configuration for LuaRocks.
  2. -- Tries to load the user's configuration file and
  3. -- defines defaults for unset values. See the
  4. -- <a href="http://luarocks.org/en/Config_file_format">config
  5. -- file format documentation</a> for details.
  6. --
  7. -- End-users shouldn't edit this file. They can override any defaults
  8. -- set in this file using their system-wide or user-specific configuration
  9. -- files. Run `luarocks` with no arguments to see the locations of
  10. -- these files in your platform.
  11. local rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, tonumber, type, assert, _VERSION =
  12. rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, tonumber, type, assert, _VERSION
  13. --module("luarocks.cfg")
  14. local cfg = {}
  15. package.loaded["luarocks.cfg"] = cfg
  16. local util = require("luarocks.util")
  17. cfg.lua_version = _VERSION:sub(5)
  18. local version_suffix = cfg.lua_version:gsub("%.", "_")
  19. -- Load site-local global configurations
  20. local ok, site_config = pcall(require, "luarocks.site_config_"..version_suffix)
  21. if not ok then
  22. ok, site_config = pcall(require, "luarocks.site_config")
  23. end
  24. if not ok then
  25. io.stderr:write("Site-local luarocks/site_config.lua file not found. Incomplete installation?\n")
  26. site_config = {}
  27. end
  28. cfg.program_version = "scm"
  29. cfg.program_series = "2.2"
  30. cfg.major_version = (cfg.program_version:match("([^.]%.[^.])")) or cfg.program_series
  31. cfg.variables = {}
  32. cfg.rocks_trees = {}
  33. cfg.platforms = {}
  34. local persist = require("luarocks.persist")
  35. cfg.errorcodes = setmetatable({
  36. OK = 0,
  37. UNSPECIFIED = 1,
  38. PERMISSIONDENIED = 2,
  39. CONFIGFILE = 3,
  40. CRASH = 99
  41. },{
  42. __index = function(t, key)
  43. local val = rawget(t, key)
  44. if not val then
  45. error("'"..tostring(key).."' is not a valid errorcode", 2)
  46. end
  47. return val
  48. end
  49. })
  50. local popen_ok, popen_result = pcall(io.popen, "")
  51. if popen_ok then
  52. if popen_result then
  53. popen_result:close()
  54. end
  55. else
  56. io.stderr:write("Your version of Lua does not support io.popen,\n")
  57. io.stderr:write("which is required by LuaRocks. Please check your Lua installation.\n")
  58. os.exit(cfg.errorcodes.UNSPECIFIED)
  59. end
  60. -- System detection:
  61. -- A proper installation of LuaRocks will hardcode the system
  62. -- and proc values with site_config.LUAROCKS_UNAME_S and site_config.LUAROCKS_UNAME_M,
  63. -- so that this detection does not run every time. When it is
  64. -- performed, we use the Unix way to identify the system,
  65. -- even on Windows (assuming UnxUtils or Cygwin).
  66. local system = site_config.LUAROCKS_UNAME_S or io.popen("uname -s"):read("*l")
  67. local proc = site_config.LUAROCKS_UNAME_M or io.popen("uname -m"):read("*l")
  68. if proc:match("i[%d]86") then
  69. cfg.target_cpu = "x86"
  70. elseif proc:match("amd64") or proc:match("x86_64") then
  71. cfg.target_cpu = "x86_64"
  72. elseif proc:match("Power Macintosh") then
  73. cfg.target_cpu = "powerpc"
  74. else
  75. cfg.target_cpu = proc
  76. end
  77. if system == "FreeBSD" then
  78. cfg.platforms.unix = true
  79. cfg.platforms.freebsd = true
  80. cfg.platforms.bsd = true
  81. elseif system == "OpenBSD" then
  82. cfg.platforms.unix = true
  83. cfg.platforms.openbsd = true
  84. cfg.platforms.bsd = true
  85. elseif system == "NetBSD" then
  86. cfg.platforms.unix = true
  87. cfg.platforms.netbsd = true
  88. cfg.platforms.bsd = true
  89. elseif system == "Darwin" then
  90. cfg.platforms.unix = true
  91. cfg.platforms.macosx = true
  92. cfg.platforms.bsd = true
  93. elseif system == "Linux" then
  94. cfg.platforms.unix = true
  95. cfg.platforms.linux = true
  96. elseif system == "SunOS" then
  97. cfg.platforms.unix = true
  98. cfg.platforms.solaris = true
  99. elseif system and system:match("^CYGWIN") then
  100. cfg.platforms.unix = true
  101. cfg.platforms.cygwin = true
  102. elseif system and system:match("^MSYS") then
  103. cfg.platforms.unix = true
  104. cfg.platforms.msys = true
  105. cfg.platforms.cygwin = true
  106. elseif system and system:match("^Windows") then
  107. cfg.platforms.windows = true
  108. cfg.platforms.win32 = true
  109. elseif system and system:match("^MINGW") then
  110. cfg.platforms.windows = true
  111. cfg.platforms.mingw32 = true
  112. cfg.platforms.win32 = true
  113. elseif system == "Haiku" then
  114. cfg.platforms.unix = true
  115. cfg.platforms.haiku = true
  116. else
  117. cfg.platforms.unix = true
  118. -- Fall back to Unix in unknown systems.
  119. end
  120. -- Set order for platform overrides
  121. local platform_order = {
  122. -- Unixes
  123. unix = 1,
  124. bsd = 2,
  125. solaris = 3,
  126. netbsd = 4,
  127. openbsd = 5,
  128. freebsd = 6,
  129. linux = 7,
  130. macosx = 8,
  131. cygwin = 9,
  132. msys = 10,
  133. haiku = 11,
  134. -- Windows
  135. win32 = 12,
  136. mingw32 = 13,
  137. windows = 14 }
  138. -- Path configuration:
  139. local sys_config_file, home_config_file
  140. local sys_config_file_default, home_config_file_default
  141. local sys_config_dir, home_config_dir
  142. local sys_config_ok, home_config_ok = false, false
  143. local extra_luarocks_module_dir
  144. sys_config_dir = site_config.LUAROCKS_SYSCONFDIR or site_config.LUAROCKS_PREFIX
  145. if cfg.platforms.windows then
  146. cfg.home = os.getenv("APPDATA") or "c:"
  147. sys_config_dir = sys_config_dir or "c:/luarocks"
  148. home_config_dir = cfg.home.."/luarocks"
  149. cfg.home_tree = cfg.home.."/luarocks/"
  150. else
  151. cfg.home = os.getenv("HOME") or ""
  152. sys_config_dir = sys_config_dir or "/etc/luarocks"
  153. home_config_dir = cfg.home.."/.luarocks"
  154. cfg.home_tree = (os.getenv("USER") ~= "root") and cfg.home.."/.luarocks/"
  155. end
  156. -- Create global environment for the config files;
  157. local env_for_config_file = function()
  158. local e
  159. e = {
  160. home = cfg.home,
  161. lua_version = cfg.lua_version,
  162. platforms = util.make_shallow_copy(cfg.platforms),
  163. processor = cfg.target_cpu, -- remains for compat reasons
  164. target_cpu = cfg.target_cpu, -- replaces `processor`
  165. os_getenv = os.getenv,
  166. dump_env = function()
  167. -- debug function, calling it from a config file will show all
  168. -- available globals to that config file
  169. print(util.show_table(e, "global environment"))
  170. end,
  171. }
  172. return e
  173. end
  174. -- Merge values from config files read into the `cfg` table
  175. local merge_overrides = function(overrides)
  176. -- remove some stuff we do not want to integrate
  177. overrides.os_getenv = nil
  178. overrides.dump_env = nil
  179. -- remove tables to be copied verbatim instead of deeply merged
  180. if overrides.rocks_trees then cfg.rocks_trees = nil end
  181. if overrides.rocks_servers then cfg.rocks_servers = nil end
  182. -- perform actual merge
  183. util.deep_merge(cfg, overrides)
  184. end
  185. -- load config file from a list until first succesful one. Info is
  186. -- added to `cfg` module table, returns filepath of succesfully loaded
  187. -- file or nil if it failed
  188. local load_config_file = function(list)
  189. for _, filepath in ipairs(list) do
  190. local result, err, errcode = persist.load_into_table(filepath, env_for_config_file())
  191. if (not result) and errcode ~= "open" then
  192. -- errcode is either "load" or "run"; bad config file, so error out
  193. io.stderr:write(err.."\n")
  194. os.exit(cfg.errorcodes.CONFIGFILE)
  195. end
  196. if result then
  197. -- succes in loading and running, merge contents and exit
  198. merge_overrides(result)
  199. return filepath
  200. end
  201. end
  202. return nil -- nothing was loaded
  203. end
  204. -- Load system configuration file
  205. do
  206. sys_config_file_default = sys_config_dir.."/config-"..cfg.lua_version..".lua"
  207. sys_config_file = load_config_file({
  208. site_config.LUAROCKS_SYSCONFIG or sys_config_file_default,
  209. sys_config_dir.."/config.lua",
  210. })
  211. sys_config_ok = (sys_config_file ~= nil)
  212. end
  213. -- Load user configuration file (if allowed)
  214. if not site_config.LUAROCKS_FORCE_CONFIG then
  215. home_config_file_default = home_config_dir.."/config-"..cfg.lua_version..".lua"
  216. local config_env_var = "LUAROCKS_CONFIG_" .. version_suffix
  217. local config_env_value = os.getenv(config_env_var)
  218. if not config_env_value then
  219. config_env_var = "LUAROCKS_CONFIG"
  220. config_env_value = os.getenv(config_env_var)
  221. end
  222. -- first try environment provided file, so we can explicitly warn when it is missing
  223. if config_env_value then
  224. local list = { config_env_value }
  225. home_config_file = load_config_file(list)
  226. home_config_ok = (home_config_file ~= nil)
  227. if not home_config_ok then
  228. io.stderr:write("Warning: could not load configuration file `"..config_env_value.."` given in environment variable "..config_env_var.."\n")
  229. end
  230. end
  231. -- try the alternative defaults if there was no environment specified file or it didn't work
  232. if not home_config_ok then
  233. local list = {
  234. home_config_file_default,
  235. home_config_dir.."/config.lua",
  236. }
  237. home_config_file = load_config_file(list)
  238. home_config_ok = (home_config_file ~= nil)
  239. end
  240. end
  241. if not next(cfg.rocks_trees) then
  242. if cfg.home_tree then
  243. table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree } )
  244. end
  245. if site_config.LUAROCKS_ROCKS_TREE then
  246. table.insert(cfg.rocks_trees, { name = "system", root = site_config.LUAROCKS_ROCKS_TREE } )
  247. end
  248. end
  249. -- update platforms list; keyed -> array
  250. do
  251. local lst = {} -- use temp array to not confuse `pairs` in loop
  252. for plat in pairs(cfg.platforms) do
  253. if cfg.platforms[plat] then -- entries set to 'false' skipped
  254. if not platform_order[plat] then
  255. local pl = ""
  256. for k,_ in pairs(platform_order) do pl = pl .. ", " .. k end
  257. io.stderr:write("Bad platform given; "..tostring(plat)..". Valid entries are: "..pl:sub(3,-1) ..".\n")
  258. os.exit(cfg.errorcodes.CONFIGFILE)
  259. end
  260. table.insert(lst, plat)
  261. else
  262. cfg.platforms[plat] = nil
  263. end
  264. end
  265. -- platform overrides depent on the order, so set priorities
  266. table.sort(lst, function(key1, key2) return platform_order[key1] < platform_order[key2] end)
  267. util.deep_merge(cfg.platforms, lst)
  268. end
  269. -- Configure defaults:
  270. local defaults = {
  271. local_by_default = false,
  272. accept_unknown_fields = false,
  273. fs_use_modules = true,
  274. hooks_enabled = true,
  275. deps_mode = "one",
  276. check_certificates = false,
  277. perm_read = "0644",
  278. perm_exec = "0755",
  279. lua_modules_path = "/share/lua/"..cfg.lua_version,
  280. lib_modules_path = "/lib/lua/"..cfg.lua_version,
  281. rocks_subdir = site_config.LUAROCKS_ROCKS_SUBDIR or "/lib/luarocks/rocks",
  282. arch = "unknown",
  283. lib_extension = "unknown",
  284. obj_extension = "unknown",
  285. rocks_servers = {
  286. {
  287. "https://luarocks.org",
  288. "https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/",
  289. "http://luafr.org/moonrocks/",
  290. "http://luarocks.logiceditor.com/rocks",
  291. }
  292. },
  293. disabled_servers = {},
  294. upload = {
  295. server = "https://luarocks.org",
  296. tool_version = "1.0.0",
  297. api_version = "1",
  298. },
  299. lua_extension = "lua",
  300. lua_interpreter = site_config.LUA_INTERPRETER or "lua",
  301. downloader = site_config.LUAROCKS_DOWNLOADER or "wget",
  302. md5checker = site_config.LUAROCKS_MD5CHECKER or "md5sum",
  303. connection_timeout = 30, -- 0 = no timeout
  304. variables = {
  305. MAKE = "make",
  306. CC = "cc",
  307. LD = "ld",
  308. CVS = "cvs",
  309. GIT = "git",
  310. SSCM = "sscm",
  311. SVN = "svn",
  312. HG = "hg",
  313. RSYNC = "rsync",
  314. WGET = "wget",
  315. SCP = "scp",
  316. CURL = "curl",
  317. PWD = "pwd",
  318. MKDIR = "mkdir",
  319. RMDIR = "rmdir",
  320. CP = "cp",
  321. LS = "ls",
  322. RM = "rm",
  323. FIND = "find",
  324. TEST = "test",
  325. CHMOD = "chmod",
  326. MKTEMP = "mktemp",
  327. ZIP = "zip",
  328. UNZIP = "unzip -n",
  329. GUNZIP = "gunzip",
  330. BUNZIP2 = "bunzip2",
  331. TAR = "tar",
  332. MD5SUM = "md5sum",
  333. OPENSSL = "openssl",
  334. MD5 = "md5",
  335. STAT = "stat",
  336. TOUCH = "touch",
  337. CMAKE = "cmake",
  338. SEVENZ = "7z",
  339. RSYNCFLAGS = "--exclude=.git -Oavz",
  340. STATFLAG = "-c '%a'",
  341. CURLNOCERTFLAG = "",
  342. WGETNOCERTFLAG = "",
  343. },
  344. external_deps_subdirs = site_config.LUAROCKS_EXTERNAL_DEPS_SUBDIRS or {
  345. bin = "bin",
  346. lib = "lib",
  347. include = "include"
  348. },
  349. runtime_external_deps_subdirs = site_config.LUAROCKS_RUNTIME_EXTERNAL_DEPS_SUBDIRS or {
  350. bin = "bin",
  351. lib = "lib",
  352. include = "include"
  353. },
  354. rocks_provided = {}
  355. }
  356. if cfg.platforms.windows then
  357. local full_prefix = (site_config.LUAROCKS_PREFIX or (os.getenv("PROGRAMFILES")..[[\LuaRocks]]))
  358. extra_luarocks_module_dir = full_prefix.."/lua/?.lua"
  359. home_config_file = home_config_file and home_config_file:gsub("\\","/")
  360. defaults.fs_use_modules = false
  361. defaults.arch = "win32-"..cfg.target_cpu
  362. defaults.lib_extension = "dll"
  363. defaults.external_lib_extension = "dll"
  364. defaults.obj_extension = "obj"
  365. defaults.external_deps_dirs = { "c:/external/" }
  366. defaults.variables.LUA_BINDIR = site_config.LUA_BINDIR and site_config.LUA_BINDIR:gsub("\\", "/") or "c:/lua"..cfg.lua_version.."/bin"
  367. defaults.variables.LUA_INCDIR = site_config.LUA_INCDIR and site_config.LUA_INCDIR:gsub("\\", "/") or "c:/lua"..cfg.lua_version.."/include"
  368. defaults.variables.LUA_LIBDIR = site_config.LUA_LIBDIR and site_config.LUA_LIBDIR:gsub("\\", "/") or "c:/lua"..cfg.lua_version.."/lib"
  369. defaults.makefile = "Makefile.win"
  370. defaults.variables.MAKE = "nmake"
  371. defaults.variables.CC = "cl"
  372. defaults.variables.RC = "rc"
  373. defaults.variables.WRAPPER = full_prefix.."\\rclauncher.c"
  374. defaults.variables.LD = "link"
  375. defaults.variables.MT = "mt"
  376. defaults.variables.LUALIB = "lua"..cfg.lua_version..".lib"
  377. defaults.variables.CFLAGS = "/nologo /MD /O2"
  378. defaults.variables.LIBFLAG = "/nologo /dll"
  379. local bins = { "SEVENZ", "CP", "FIND", "LS", "MD5SUM",
  380. "MKDIR", "MV", "PWD", "RMDIR", "TEST", "UNAME", "WGET" }
  381. for _, var in ipairs(bins) do
  382. if defaults.variables[var] then
  383. defaults.variables[var] = full_prefix.."\\tools\\"..defaults.variables[var]
  384. end
  385. end
  386. defaults.external_deps_patterns = {
  387. bin = { "?.exe", "?.bat" },
  388. lib = { "?.lib", "?.dll", "lib?.dll" },
  389. include = { "?.h" }
  390. }
  391. defaults.runtime_external_deps_patterns = {
  392. bin = { "?.exe", "?.bat" },
  393. lib = { "?.dll", "lib?.dll" },
  394. include = { "?.h" }
  395. }
  396. defaults.export_path = "SET PATH=%s"
  397. defaults.export_path_separator = ";"
  398. defaults.export_lua_path = "SET LUA_PATH=%s"
  399. defaults.export_lua_cpath = "SET LUA_CPATH=%s"
  400. defaults.wrapper_suffix = ".bat"
  401. local localappdata = os.getenv("LOCALAPPDATA")
  402. if not localappdata then
  403. -- for Windows versions below Vista
  404. localappdata = os.getenv("USERPROFILE").."/Local Settings/Application Data"
  405. end
  406. defaults.local_cache = localappdata.."/LuaRocks/Cache"
  407. defaults.web_browser = "start"
  408. end
  409. if cfg.platforms.mingw32 then
  410. defaults.obj_extension = "o"
  411. defaults.cmake_generator = "MinGW Makefiles"
  412. defaults.variables.MAKE = "mingw32-make"
  413. defaults.variables.CC = "mingw32-gcc"
  414. defaults.variables.RC = "windres"
  415. defaults.variables.LD = "mingw32-gcc"
  416. defaults.variables.CFLAGS = "-O2"
  417. defaults.variables.LIBFLAG = "-shared"
  418. defaults.external_deps_patterns = {
  419. bin = { "?.exe", "?.bat" },
  420. -- mingw lookup list from http://stackoverflow.com/a/15853231/1793220
  421. -- ...should we keep ?.lib at the end? It's not in the above list.
  422. lib = { "lib?.dll.a", "?.dll.a", "lib?.a", "cyg?.dll", "lib?.dll", "?.dll", "?.lib" },
  423. include = { "?.h" }
  424. }
  425. defaults.runtime_external_deps_patterns = {
  426. bin = { "?.exe", "?.bat" },
  427. lib = { "cyg?.dll", "?.dll", "lib?.dll" },
  428. include = { "?.h" }
  429. }
  430. end
  431. if cfg.platforms.unix then
  432. defaults.lib_extension = "so"
  433. defaults.external_lib_extension = "so"
  434. defaults.obj_extension = "o"
  435. defaults.external_deps_dirs = { "/usr/local", "/usr" }
  436. defaults.variables.LUA_BINDIR = site_config.LUA_BINDIR or "/usr/local/bin"
  437. defaults.variables.LUA_INCDIR = site_config.LUA_INCDIR or "/usr/local/include"
  438. defaults.variables.LUA_LIBDIR = site_config.LUA_LIBDIR or "/usr/local/lib"
  439. defaults.variables.CFLAGS = "-O2"
  440. defaults.cmake_generator = "Unix Makefiles"
  441. defaults.variables.CC = "gcc"
  442. defaults.variables.LD = "gcc"
  443. defaults.gcc_rpath = true
  444. defaults.variables.LIBFLAG = "-shared"
  445. defaults.external_deps_patterns = {
  446. bin = { "?" },
  447. lib = { "lib?.a", "lib?.so", "lib?.so.*" },
  448. include = { "?.h" }
  449. }
  450. defaults.runtime_external_deps_patterns = {
  451. bin = { "?" },
  452. lib = { "lib?.so", "lib?.so.*" },
  453. include = { "?.h" }
  454. }
  455. defaults.export_path = "export PATH='%s'"
  456. defaults.export_path_separator = ":"
  457. defaults.export_lua_path = "export LUA_PATH='%s'"
  458. defaults.export_lua_cpath = "export LUA_CPATH='%s'"
  459. defaults.wrapper_suffix = ""
  460. defaults.local_cache = cfg.home.."/.cache/luarocks"
  461. if not defaults.variables.CFLAGS:match("-fPIC") then
  462. defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC"
  463. end
  464. defaults.web_browser = "xdg-open"
  465. end
  466. if cfg.platforms.cygwin then
  467. defaults.lib_extension = "so" -- can be overridden in the config file for mingw builds
  468. defaults.arch = "cygwin-"..cfg.target_cpu
  469. defaults.cmake_generator = "Unix Makefiles"
  470. defaults.variables.CC = "echo -llua | xargs gcc"
  471. defaults.variables.LD = "echo -llua | xargs gcc"
  472. defaults.variables.LIBFLAG = "-shared"
  473. end
  474. if cfg.platforms.msys then
  475. -- msys is basically cygwin made out of mingw, meaning the subsytem is unixish
  476. -- enough, yet we can freely mix with native win32
  477. defaults.external_deps_patterns = {
  478. bin = { "?.exe", "?.bat", "?" },
  479. lib = { "lib?.so", "lib?.so.*", "lib?.dll.a", "?.dll.a",
  480. "lib?.a", "lib?.dll", "?.dll", "?.lib" },
  481. include = { "?.h" }
  482. }
  483. defaults.runtime_external_deps_patterns = {
  484. bin = { "?.exe", "?.bat" },
  485. lib = { "lib?.so", "?.dll", "lib?.dll" },
  486. include = { "?.h" }
  487. }
  488. end
  489. if cfg.platforms.bsd then
  490. defaults.variables.MAKE = "gmake"
  491. defaults.variables.STATFLAG = "-f '%OLp'"
  492. end
  493. if cfg.platforms.macosx then
  494. defaults.variables.MAKE = "make"
  495. defaults.external_lib_extension = "dylib"
  496. defaults.arch = "macosx-"..cfg.target_cpu
  497. defaults.variables.LIBFLAG = "-bundle -undefined dynamic_lookup -all_load"
  498. defaults.variables.STAT = "/usr/bin/stat"
  499. defaults.variables.STATFLAG = "-f '%A'"
  500. local version = io.popen("sw_vers -productVersion"):read("*l")
  501. version = tonumber(version and version:match("^[^.]+%.([^.]+)")) or 3
  502. if version >= 10 then
  503. version = 8
  504. elseif version >= 5 then
  505. version = 5
  506. else
  507. defaults.gcc_rpath = false
  508. end
  509. defaults.variables.CC = "env MACOSX_DEPLOYMENT_TARGET=10."..version.." gcc"
  510. defaults.variables.LD = "env MACOSX_DEPLOYMENT_TARGET=10."..version.." gcc"
  511. defaults.web_browser = "open"
  512. end
  513. if cfg.platforms.linux then
  514. defaults.arch = "linux-"..cfg.target_cpu
  515. end
  516. if cfg.platforms.freebsd then
  517. defaults.arch = "freebsd-"..cfg.target_cpu
  518. defaults.gcc_rpath = false
  519. defaults.variables.CC = "cc"
  520. defaults.variables.LD = "cc"
  521. end
  522. if cfg.platforms.openbsd then
  523. defaults.arch = "openbsd-"..cfg.target_cpu
  524. end
  525. if cfg.platforms.netbsd then
  526. defaults.arch = "netbsd-"..cfg.target_cpu
  527. end
  528. if cfg.platforms.solaris then
  529. defaults.arch = "solaris-"..cfg.target_cpu
  530. --defaults.platforms = {"unix", "solaris"}
  531. defaults.variables.MAKE = "gmake"
  532. end
  533. -- Expose some more values detected by LuaRocks for use by rockspec authors.
  534. defaults.variables.LIB_EXTENSION = defaults.lib_extension
  535. defaults.variables.OBJ_EXTENSION = defaults.obj_extension
  536. defaults.variables.LUAROCKS_PREFIX = site_config.LUAROCKS_PREFIX
  537. defaults.variables.LUA = site_config.LUA_DIR_SET and (defaults.variables.LUA_BINDIR.."/"..defaults.lua_interpreter) or defaults.lua_interpreter
  538. -- Add built-in modules to rocks_provided
  539. defaults.rocks_provided["lua"] = cfg.lua_version.."-1"
  540. if bit32 then -- Lua 5.2+
  541. defaults.rocks_provided["bit32"] = cfg.lua_version.."-1"
  542. end
  543. if utf8 then -- Lua 5.3+
  544. defaults.rocks_provided["utf8"] = cfg.lua_version.."-1"
  545. end
  546. if package.loaded.jit then
  547. -- LuaJIT
  548. local lj_version = package.loaded.jit.version:match("LuaJIT (.*)"):gsub("%-","")
  549. --defaults.rocks_provided["luajit"] = lj_version.."-1"
  550. defaults.rocks_provided["luabitop"] = lj_version.."-1"
  551. end
  552. -- Use defaults:
  553. -- Populate some arrays with values from their 'defaults' counterparts
  554. -- if they were not already set by user.
  555. for _, entry in ipairs({"variables", "rocks_provided"}) do
  556. if not cfg[entry] then
  557. cfg[entry] = {}
  558. end
  559. for k,v in pairs(defaults[entry]) do
  560. if not cfg[entry][k] then
  561. cfg[entry][k] = v
  562. end
  563. end
  564. end
  565. -- For values not set in the config file, use values from the 'defaults' table.
  566. local cfg_mt = {
  567. __index = function(t, k)
  568. local default = defaults[k]
  569. if default then
  570. rawset(t, k, default)
  571. end
  572. return default
  573. end
  574. }
  575. setmetatable(cfg, cfg_mt)
  576. if not cfg.check_certificates then
  577. cfg.variables.CURLNOCERTFLAG = "-k"
  578. cfg.variables.WGETNOCERTFLAG = "--no-check-certificate"
  579. end
  580. function cfg.make_paths_from_tree(tree)
  581. local lua_path, lib_path, bin_path
  582. if type(tree) == "string" then
  583. lua_path = tree..cfg.lua_modules_path
  584. lib_path = tree..cfg.lib_modules_path
  585. bin_path = tree.."/bin"
  586. else
  587. lua_path = tree.lua_dir or tree.root..cfg.lua_modules_path
  588. lib_path = tree.lib_dir or tree.root..cfg.lib_modules_path
  589. bin_path = tree.bin_dir or tree.root.."/bin"
  590. end
  591. return lua_path, lib_path, bin_path
  592. end
  593. function cfg.package_paths(current)
  594. local new_path, new_cpath, new_bin = {}, {}, {}
  595. local function add_tree_to_paths(tree)
  596. local lua_path, lib_path, bin_path = cfg.make_paths_from_tree(tree)
  597. table.insert(new_path, lua_path.."/?.lua")
  598. table.insert(new_path, lua_path.."/?/init.lua")
  599. table.insert(new_cpath, lib_path.."/?."..cfg.lib_extension)
  600. table.insert(new_bin, bin_path)
  601. end
  602. if current then
  603. add_tree_to_paths(current)
  604. end
  605. for _,tree in ipairs(cfg.rocks_trees) do
  606. add_tree_to_paths(tree)
  607. end
  608. if extra_luarocks_module_dir then
  609. table.insert(new_path, extra_luarocks_module_dir)
  610. end
  611. return table.concat(new_path, ";"), table.concat(new_cpath, ";"), table.concat(new_bin, cfg.export_path_separator)
  612. end
  613. function cfg.init_package_paths()
  614. local lr_path, lr_cpath, lr_bin = cfg.package_paths()
  615. package.path = util.remove_path_dupes(package.path .. ";" .. lr_path, ";")
  616. package.cpath = util.remove_path_dupes(package.cpath .. ";" .. lr_cpath, ";")
  617. end
  618. function cfg.which_config()
  619. local ret = {
  620. system = {
  621. file = sys_config_file or sys_config_file_default,
  622. ok = sys_config_ok,
  623. },
  624. user = {
  625. file = home_config_file or home_config_file_default,
  626. ok = home_config_ok,
  627. }
  628. }
  629. ret.nearest = (ret.user.ok and ret.user.file) or ret.system.file
  630. return ret
  631. end
  632. cfg.user_agent = "LuaRocks/"..cfg.program_version.." "..cfg.arch
  633. cfg.http_proxy = os.getenv("http_proxy")
  634. cfg.https_proxy = os.getenv("https_proxy")
  635. cfg.no_proxy = os.getenv("no_proxy")
  636. --- Check if platform was detected
  637. -- @param query string: The platform name to check.
  638. -- @return boolean: true if LuaRocks is currently running on queried platform.
  639. function cfg.is_platform(query)
  640. assert(type(query) == "string")
  641. for _, platform in ipairs(cfg.platforms) do
  642. if platform == query then
  643. return true
  644. end
  645. end
  646. end
  647. return cfg