PageRenderTime 34ms CodeModel.GetById 2ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/luarocks/cfg.lua

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