diff options
Diffstat (limited to 'src/luarocks/cfg.lua')
| -rw-r--r-- | src/luarocks/cfg.lua | 242 |
1 files changed, 242 insertions, 0 deletions
diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua new file mode 100644 index 00000000..3c4c5aed --- /dev/null +++ b/src/luarocks/cfg.lua | |||
| @@ -0,0 +1,242 @@ | |||
| 1 | |||
| 2 | local rawset, next, table, pairs, print, require, io, os, setmetatable = | ||
| 3 | rawset, next, table, pairs, print, require, io, os, setmetatable | ||
| 4 | |||
| 5 | --- Configuration for LuaRocks. | ||
| 6 | -- Tries to load the user's configuration file and | ||
| 7 | -- defines defaults for unset values. See the | ||
| 8 | -- <a href="http://luarocks.org/en/Config_file_format">config | ||
| 9 | -- file format documentation</a> for details. | ||
| 10 | module("luarocks.cfg") | ||
| 11 | |||
| 12 | local persist = require("luarocks.persist") | ||
| 13 | |||
| 14 | local detected = {} | ||
| 15 | local system,proc | ||
| 16 | |||
| 17 | -- A proper installation of LuaRocks will hardcode the system | ||
| 18 | -- and proc values with LUAROCKS_UNAME_S and LUAROCKS_UNAME_M, | ||
| 19 | -- so that this detection does not run every time. When it is | ||
| 20 | -- performed, we use the Unix way to identify the system, | ||
| 21 | -- even on Windows (assuming UnxUtils or Cygwin). | ||
| 22 | system = LUAROCKS_UNAME_S or io.popen("uname -s"):read("*l") | ||
| 23 | proc = LUAROCKS_UNAME_M or io.popen("uname -m"):read("*l") | ||
| 24 | if proc:match("i[%d]86") then | ||
| 25 | proc = "x86" | ||
| 26 | elseif proc:match("amd64") or proc:match("x86_64") then | ||
| 27 | proc = "x86_64" | ||
| 28 | elseif proc:match("Power Macintosh") then | ||
| 29 | proc = "powerpc" | ||
| 30 | end | ||
| 31 | |||
| 32 | if system == "FreeBSD" then | ||
| 33 | detected.unix = true | ||
| 34 | detected.freebsd = true | ||
| 35 | elseif system == "Darwin" then | ||
| 36 | detected.unix = true | ||
| 37 | detected.macosx = true | ||
| 38 | elseif system == "Linux" then | ||
| 39 | detected.unix = true | ||
| 40 | detected.linux = true | ||
| 41 | elseif system and system:match("^CYGWIN") then | ||
| 42 | detected.unix = true | ||
| 43 | detected.cygwin = true | ||
| 44 | elseif system and system:match("^Windows") then | ||
| 45 | detected.windows = true | ||
| 46 | else | ||
| 47 | detected.unix = true | ||
| 48 | -- Fall back to Unix in unknown systems. | ||
| 49 | end | ||
| 50 | |||
| 51 | local sys_config_file, home_config_file, home_tree | ||
| 52 | if detected.windows then | ||
| 53 | home = os.getenv("APPDATA") or "c:" | ||
| 54 | sys_config_file = "c:/luarocks/config.lua" | ||
| 55 | home_config_file = home.."/luarocks/config.lua" | ||
| 56 | home_tree = home.."/luarocks/" | ||
| 57 | else | ||
| 58 | home = os.getenv("HOME") or "" | ||
| 59 | sys_config_file = "/etc/luarocks/config.lua" | ||
| 60 | home_config_file = home.."/.luarocks/config.lua" | ||
| 61 | home_tree = home.."/.luarocks/" | ||
| 62 | end | ||
| 63 | |||
| 64 | variables = {} | ||
| 65 | rocks_trees = {} | ||
| 66 | |||
| 67 | persist.load_into_table(LUAROCKS_SYSCONFIG or sys_config_file, _M) | ||
| 68 | |||
| 69 | if not LUAROCKS_FORCE_CONFIG then | ||
| 70 | home_config_file = os.getenv("LUAROCKS_CONFIG") or home_config_file | ||
| 71 | local home_overrides = persist.load_into_table(home_config_file, { home = home }) | ||
| 72 | if home_overrides then | ||
| 73 | local util = require("luarocks.util") | ||
| 74 | util.deep_merge(_M, home_overrides) | ||
| 75 | end | ||
| 76 | end | ||
| 77 | |||
| 78 | local defaults = { | ||
| 79 | arch = "unknown", | ||
| 80 | lib_extension = "unknown", | ||
| 81 | obj_extension = "unknown", | ||
| 82 | rocks_servers = { | ||
| 83 | "http://luarocks.luaforge.net/rocks" | ||
| 84 | }, | ||
| 85 | lua_extension = "lua", | ||
| 86 | lua_interpreter = LUA_INTERPRETER or "lua", | ||
| 87 | downloader = LUAROCKS_DOWNLOADER or "wget", | ||
| 88 | md5checker = LUAROCKS_MD5CHECKER or "md5sum", | ||
| 89 | variables = {} | ||
| 90 | } | ||
| 91 | |||
| 92 | defaults.external_deps_subdirs = { | ||
| 93 | bin = "bin", | ||
| 94 | lib = "lib", | ||
| 95 | include = "include" | ||
| 96 | } | ||
| 97 | defaults.runtime_external_deps_subdirs = defaults.external_deps_subdirs | ||
| 98 | |||
| 99 | if detected.windows then | ||
| 100 | home_config_file = home_config_file:gsub("\\","/") | ||
| 101 | defaults.lib_extension = "dll" | ||
| 102 | defaults.obj_extension = "obj" | ||
| 103 | local rootdir = LUAROCKS_ROCKS_TREE or home_tree | ||
| 104 | defaults.root_dir = rootdir | ||
| 105 | defaults.rocks_dir = rootdir.."/rocks/" | ||
| 106 | defaults.scripts_dir = rootdir.."/bin/" | ||
| 107 | defaults.external_deps_dirs = { "c:/external/" } | ||
| 108 | defaults.variables.LUA_BINDIR = LUA_BINDIR and LUA_BINDIR:gsub("\\", "/") or "c:/lua5.1/bin" | ||
| 109 | defaults.variables.LUA_INCDIR = LUA_INCDIR and LUA_INCDIR:gsub("\\", "/") or "c:/lua5.1/include" | ||
| 110 | defaults.variables.LUA_LIBDIR = LUA_LIBDIR and LUA_LIBDIR:gsub("\\", "/") or "c:/lua5.1/lib" | ||
| 111 | defaults.arch = "win32-"..proc | ||
| 112 | defaults.platforms = {"win32", "windows" } | ||
| 113 | defaults.cmake_generator = "MinGW Makefiles" | ||
| 114 | -- TODO: Split Windows flavors between mingw and msvc | ||
| 115 | -- defaults.make = "make" | ||
| 116 | -- defaults.makefile = "Makefile" | ||
| 117 | defaults.make = "nmake" | ||
| 118 | defaults.makefile = "Makefile.win" | ||
| 119 | defaults.variables.CC = "cl" | ||
| 120 | defaults.variables.LD = "link" | ||
| 121 | defaults.variables.MT = "mt" | ||
| 122 | defaults.variables.CFLAGS = "/MD /O2" | ||
| 123 | defaults.variables.LIBFLAG = "/dll" | ||
| 124 | defaults.external_deps_patterns = { | ||
| 125 | bin = { "?.exe", "?.bat" }, | ||
| 126 | lib = { "?.lib", "?.dll" }, | ||
| 127 | include = { "?.h" } | ||
| 128 | } | ||
| 129 | defaults.runtime_external_deps_patterns = { | ||
| 130 | bin = { "?.exe", "?.bat" }, | ||
| 131 | lib = { "?.dll" }, | ||
| 132 | include = { "?.h" } | ||
| 133 | } | ||
| 134 | end | ||
| 135 | |||
| 136 | if detected.unix then | ||
| 137 | defaults.lib_extension = "so" | ||
| 138 | defaults.obj_extension = "o" | ||
| 139 | local rootdir = LUAROCKS_ROCKS_TREE or home_tree | ||
| 140 | defaults.root_dir = rootdir | ||
| 141 | defaults.rocks_dir = rootdir.."/rocks/" | ||
| 142 | defaults.scripts_dir = rootdir.."/bin/" | ||
| 143 | defaults.external_deps_dirs = { "/usr/local", "/usr" } | ||
| 144 | defaults.variables.LUA_BINDIR = LUA_BINDIR or "/usr/local/bin" | ||
| 145 | defaults.variables.LUA_INCDIR = LUA_INCDIR or "/usr/local/include" | ||
| 146 | defaults.variables.LUA_LIBDIR = LUA_LIBDIR or "/usr/local/lib" | ||
| 147 | defaults.variables.CFLAGS = "-O2" | ||
| 148 | defaults.cmake_generator = "Unix Makefiles" | ||
| 149 | defaults.make = "make" | ||
| 150 | defaults.platforms = { "unix" } | ||
| 151 | defaults.variables.CC = "cc" | ||
| 152 | defaults.variables.LD = "ld" | ||
| 153 | defaults.variables.LIBFLAG = "-shared" | ||
| 154 | defaults.external_deps_patterns = { | ||
| 155 | bin = { "?" }, | ||
| 156 | lib = { "lib?.a", "lib?.so" }, | ||
| 157 | include = { "?.h" } | ||
| 158 | } | ||
| 159 | defaults.runtime_external_deps_patterns = { | ||
| 160 | bin = { "?" }, | ||
| 161 | lib = { "lib?.so" }, | ||
| 162 | include = { "?.h" } | ||
| 163 | } | ||
| 164 | end | ||
| 165 | |||
| 166 | if detected.cygwin then | ||
| 167 | defaults.lib_extension = "so" -- can be overridden in the config file for mingw builds | ||
| 168 | defaults.arch = "cygwin-"..proc | ||
| 169 | defaults.platforms = {"unix", "cygwin"} | ||
| 170 | defaults.cmake_generator = "Unix Makefiles" | ||
| 171 | defaults.variables.CC = "echo -llua | xargs gcc" | ||
| 172 | defaults.variables.LD = "echo -llua | xargs gcc" | ||
| 173 | defaults.variables.LIBFLAG = "-shared" | ||
| 174 | end | ||
| 175 | |||
| 176 | defaults.external_lib_extension = defaults.lib_extension | ||
| 177 | |||
| 178 | if detected.macosx then | ||
| 179 | defaults.external_lib_extension = "dylib" | ||
| 180 | defaults.arch = "macosx-"..proc | ||
| 181 | defaults.platforms = {"unix", "macosx"} | ||
| 182 | defaults.variables.CC = "export MACOSX_DEPLOYMENT_TARGET=10.3; gcc" | ||
| 183 | defaults.variables.LD = "export MACOSX_DEPLOYMENT_TARGET=10.3; gcc" | ||
| 184 | defaults.variables.LIBFLAG = "-bundle -undefined dynamic_lookup -all_load" | ||
| 185 | end | ||
| 186 | |||
| 187 | if detected.linux then | ||
| 188 | defaults.arch = "linux-"..proc | ||
| 189 | defaults.platforms = {"unix", "linux"} | ||
| 190 | defaults.variables.CC = "gcc" | ||
| 191 | defaults.variables.LD = "gcc" | ||
| 192 | defaults.variables.LIBFLAG = "-shared" | ||
| 193 | end | ||
| 194 | |||
| 195 | if detected.freebsd then | ||
| 196 | defaults.arch = "freebsd-"..proc | ||
| 197 | defaults.make = "gmake" | ||
| 198 | defaults.platforms = {"unix", "freebsd"} | ||
| 199 | defaults.variables.CC = "gcc" | ||
| 200 | defaults.variables.LD = "gcc" | ||
| 201 | defaults.variables.LIBFLAG = "-shared" | ||
| 202 | end | ||
| 203 | |||
| 204 | if proc == "x86_64" and not defaults.variables.CFLAGS:match("-fPIC") then | ||
| 205 | defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC" | ||
| 206 | end | ||
| 207 | |||
| 208 | -- Expose some more values detected by LuaRocks for use by rockspec authors. | ||
| 209 | defaults.variables.LUA = defaults.lua_interpreter | ||
| 210 | defaults.variables.LIB_EXTENSION = defaults.lib_extension | ||
| 211 | defaults.variables.OBJ_EXTENSION = defaults.obj_extension | ||
| 212 | defaults.variables.LUAROCKS_PREFIX = LUAROCKS_PREFIX | ||
| 213 | |||
| 214 | local cfg_mt = { | ||
| 215 | __index = function(t, k) | ||
| 216 | local default = defaults[k] | ||
| 217 | if default then | ||
| 218 | rawset(t, k, default) | ||
| 219 | end | ||
| 220 | return default | ||
| 221 | end | ||
| 222 | } | ||
| 223 | |||
| 224 | if not _M.variables then | ||
| 225 | _M.variables = {} | ||
| 226 | end | ||
| 227 | for k,v in pairs(defaults.variables) do | ||
| 228 | if not _M.variables[k] then | ||
| 229 | _M.variables[k] = v | ||
| 230 | end | ||
| 231 | end | ||
| 232 | |||
| 233 | setmetatable(_M, cfg_mt) | ||
| 234 | |||
| 235 | if not next(rocks_trees) then | ||
| 236 | if home_tree then | ||
| 237 | table.insert(rocks_trees, home_tree) | ||
| 238 | end | ||
| 239 | if LUAROCKS_ROCKS_TREE then | ||
| 240 | table.insert(rocks_trees, LUAROCKS_ROCKS_TREE) | ||
| 241 | end | ||
| 242 | end | ||
