From 3bab28f0f51aa39f3d704afac920257a6c9cb095 Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Wed, 12 Jun 2024 02:45:29 +0200 Subject: some more errors fixed --- src/luarocks/core/cfg-original.lua | 940 ----------------------------------- src/luarocks/core/cfg.d.tl | 7 + src/luarocks/core/cfg.lua | 372 +++++++------- src/luarocks/core/cfg.tl | 982 ------------------------------------- src/luarocks/core/persist.lua | 22 +- src/luarocks/core/persist.tl | 33 +- src/luarocks/core/sysdetect.tl | 22 +- src/luarocks/core/util.lua | 65 ++- src/luarocks/core/util.tl | 120 ++--- 9 files changed, 281 insertions(+), 2282 deletions(-) delete mode 100644 src/luarocks/core/cfg-original.lua create mode 100644 src/luarocks/core/cfg.d.tl delete mode 100644 src/luarocks/core/cfg.tl (limited to 'src') diff --git a/src/luarocks/core/cfg-original.lua b/src/luarocks/core/cfg-original.lua deleted file mode 100644 index 6d8fe55b..00000000 --- a/src/luarocks/core/cfg-original.lua +++ /dev/null @@ -1,940 +0,0 @@ - ---- Configuration for LuaRocks. --- Tries to load the user's configuration file and --- defines defaults for unset values. See the --- config --- file format documentation for details. --- --- End-users shouldn't edit this file. They can override any defaults --- set in this file using their system-wide or user-specific configuration --- files. Run `luarocks` with no arguments to see the locations of --- these files in your platform. - -local table, pairs, require, os, pcall, ipairs, package, type, assert = - table, pairs, require, os, pcall, ipairs, package, type, assert - -local dir = require("luarocks.core.dir") -local util = require("luarocks.core.util") -local persist = require("luarocks.core.persist") -local sysdetect = require("luarocks.core.sysdetect") -local vers = require("luarocks.core.vers") - --------------------------------------------------------------------------------- - -local program_version = "dev" - -local is_windows = package.config:sub(1,1) == "\\" - --- Set order for platform overrides. --- More general platform identifiers should be listed first, --- more specific ones later. -local platform_order = { - -- Unixes - "unix", - "bsd", - "solaris", - "netbsd", - "openbsd", - "freebsd", - "dragonfly", - "linux", - "macosx", - "cygwin", - "msys", - "haiku", - -- Windows - "windows", - "win32", - "mingw", - "mingw32", - "msys2_mingw_w64", -} - -local function detect_sysconfdir() - if not debug then - return - end - local src = debug.getinfo(1, "S").source - if not src then - return - end - src = dir.normalize(src) - if src:sub(1, 1) == "@" then - src = src:sub(2) - end - local basedir = src:match("^(.*)[\\/]luarocks[\\/]core[\\/]cfg.lua$") - if not basedir then - return - end - -- If installed in a Unix-like tree, use a Unix-like sysconfdir - local installdir = basedir:match("^(.*)[\\/]share[\\/]lua[\\/][^/]*$") - if installdir then - if installdir == "/usr" then - return "/etc/luarocks" - end - return dir.path(installdir, "etc", "luarocks") - end - -- Otherwise, use base directory of sources - return basedir -end - -local load_config_file -do - -- Create global environment for the config files; - local function env_for_config_file(cfg, platforms) - local platforms_copy = {} - for k,v in pairs(platforms) do - platforms_copy[k] = v - end - - local e - e = { - home = cfg.home, - lua_version = cfg.lua_version, - platforms = platforms_copy, - processor = cfg.target_cpu, -- remains for compat reasons - target_cpu = cfg.target_cpu, -- replaces `processor` - os_getenv = os.getenv, - variables = cfg.variables or {}, - dump_env = function() - -- debug function, calling it from a config file will show all - -- available globals to that config file - print(util.show_table(e, "global environment")) - end, - } - return e - end - - -- Merge values from config files read into the `cfg` table - local function merge_overrides(cfg, overrides) - -- remove some stuff we do not want to integrate - overrides.os_getenv = nil - overrides.dump_env = nil - -- remove tables to be copied verbatim instead of deeply merged - if overrides.rocks_trees then cfg.rocks_trees = nil end - if overrides.rocks_servers then cfg.rocks_servers = nil end - -- perform actual merge - util.deep_merge(cfg, overrides) - end - - local function update_platforms(platforms, overrides) - if overrides[1] then - for k, _ in pairs(platforms) do - platforms[k] = nil - end - for _, v in ipairs(overrides) do - platforms[v] = true - end - -- set some fallback default in case the user provides an incomplete configuration. - -- LuaRocks expects a set of defaults to be available. - if not (platforms.unix or platforms.windows) then - platforms[is_windows and "windows" or "unix"] = true - end - end - end - - -- Load config file and merge its contents into the `cfg` module table. - -- @return filepath of successfully loaded file or nil if it failed - load_config_file = function(cfg, platforms, filepath) - local result, err, errcode = persist.load_into_table(filepath, env_for_config_file(cfg, platforms)) - if (not result) and errcode ~= "open" then - -- errcode is either "load" or "run"; bad config file, so error out - return nil, err, "config" - end - if result then - -- success in loading and running, merge contents and exit - update_platforms(platforms, result.platforms) - result.platforms = nil - merge_overrides(cfg, result) - return filepath - end - return nil -- nothing was loaded - end -end - -local platform_sets = { - freebsd = { unix = true, bsd = true, freebsd = true }, - openbsd = { unix = true, bsd = true, openbsd = true }, - dragonfly = { unix = true, bsd = true, dragonfly = true }, - solaris = { unix = true, solaris = true }, - windows = { windows = true, win32 = true }, - cygwin = { unix = true, cygwin = true }, - macosx = { unix = true, bsd = true, macosx = true, macos = true }, - netbsd = { unix = true, bsd = true, netbsd = true }, - haiku = { unix = true, haiku = true }, - linux = { unix = true, linux = true }, - mingw = { windows = true, win32 = true, mingw32 = true, mingw = true }, - msys = { unix = true, cygwin = true, msys = true }, - msys2_mingw_w64 = { windows = true, win32 = true, mingw32 = true, mingw = true, msys = true, msys2_mingw_w64 = true }, -} - -local function make_platforms(system) - -- fallback to Unix in unknown systems - return platform_sets[system] or { unix = true } -end - --------------------------------------------------------------------------------- - -local function make_defaults(lua_version, target_cpu, platforms, home) - - -- Configure defaults: - local defaults = { - - local_by_default = false, - accept_unknown_fields = false, - fs_use_modules = true, - hooks_enabled = true, - deps_mode = "one", - no_manifest = false, - check_certificates = false, - - cache_timeout = 60, - cache_fail_timeout = 86400, - - lua_modules_path = dir.path("share", "lua", lua_version), - lib_modules_path = dir.path("lib", "lua", lua_version), - rocks_subdir = dir.path("lib", "luarocks", "rocks-"..lua_version), - - arch = "unknown", - lib_extension = "unknown", - obj_extension = "unknown", - link_lua_explicitly = false, - - rocks_servers = { - { - "https://luarocks.org", - "https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/", - "https://loadk.com/luarocks/", - } - }, - disabled_servers = {}, - - upload = { - server = "https://luarocks.org", - tool_version = "1.0.0", - api_version = "1", - }, - - lua_extension = "lua", - connection_timeout = 30, -- 0 = no timeout - - variables = { - MAKE = os.getenv("MAKE") or "make", - CC = os.getenv("CC") or "cc", - LD = os.getenv("CC") or "ld", - AR = os.getenv("AR") or "ar", - RANLIB = os.getenv("RANLIB") or "ranlib", - - CVS = "cvs", - GIT = "git", - SSCM = "sscm", - SVN = "svn", - HG = "hg", - - GPG = "gpg", - - RSYNC = "rsync", - WGET = "wget", - SCP = "scp", - CURL = "curl", - - PWD = "pwd", - MKDIR = "mkdir", - RMDIR = "rmdir", - CP = "cp", - LN = "ln", - LS = "ls", - RM = "rm", - FIND = "find", - CHMOD = "chmod", - ICACLS = "icacls", - MKTEMP = "mktemp", - - ZIP = "zip", - UNZIP = "unzip -n", - GUNZIP = "gunzip", - BUNZIP2 = "bunzip2", - TAR = "tar", - - MD5SUM = "md5sum", - OPENSSL = "openssl", - MD5 = "md5", - TOUCH = "touch", - - CMAKE = "cmake", - SEVENZ = "7z", - - RSYNCFLAGS = "--exclude=.git -Oavz", - CURLNOCERTFLAG = "", - WGETNOCERTFLAG = "", - }, - - external_deps_subdirs = { - bin = "bin", - lib = "lib", - include = "include" - }, - runtime_external_deps_subdirs = { - bin = "bin", - lib = "lib", - include = "include" - }, - } - - if platforms.windows then - - defaults.arch = "win32-"..target_cpu - defaults.lib_extension = "dll" - defaults.external_lib_extension = "dll" - defaults.static_lib_extension = "lib" - defaults.obj_extension = "obj" - defaults.external_deps_dirs = { - dir.path("c:", "external"), - dir.path("c:", "windows", "system32"), - } - - defaults.makefile = "Makefile.win" - defaults.variables.PWD = "echo %cd%" - defaults.variables.MKDIR = "md" - defaults.variables.MAKE = os.getenv("MAKE") or "nmake" - defaults.variables.CC = os.getenv("CC") or "cl" - defaults.variables.RC = os.getenv("WINDRES") or "rc" - defaults.variables.LD = os.getenv("LINK") or "link" - defaults.variables.MT = os.getenv("MT") or "mt" - defaults.variables.AR = os.getenv("AR") or "lib" - defaults.variables.CFLAGS = os.getenv("CFLAGS") or "/nologo /MD /O2" - defaults.variables.LDFLAGS = os.getenv("LDFLAGS") - defaults.variables.LIBFLAG = "/nologo /dll" - - defaults.external_deps_patterns = { - bin = { "?.exe", "?.bat" }, - lib = { "?.lib", "lib?.lib", "?.dll", "lib?.dll" }, - include = { "?.h" } - } - defaults.runtime_external_deps_patterns = { - bin = { "?.exe", "?.bat" }, - lib = { "?.dll", "lib?.dll" }, - include = { "?.h" } - } - defaults.export_path_separator = ";" - defaults.wrapper_suffix = ".bat" - - local localappdata = os.getenv("LOCALAPPDATA") - if not localappdata then - -- for Windows versions below Vista - localappdata = dir.path((os.getenv("USERPROFILE") or dir.path("c:", "Users", "All Users")), "Local Settings", "Application Data") - end - defaults.local_cache = dir.path(localappdata, "LuaRocks", "Cache") - defaults.web_browser = "start" - - defaults.external_deps_subdirs.lib = { "lib", "", "bin" } - defaults.runtime_external_deps_subdirs.lib = { "lib", "", "bin" } - defaults.link_lua_explicitly = true - defaults.fs_use_modules = false - end - - if platforms.mingw32 then - defaults.obj_extension = "o" - defaults.static_lib_extension = "a" - defaults.external_deps_dirs = { - dir.path("c:", "external"), - dir.path("c:", "mingw"), - dir.path("c:", "windows", "system32"), - } - defaults.cmake_generator = "MinGW Makefiles" - defaults.variables.MAKE = os.getenv("MAKE") or "mingw32-make" - if target_cpu == "x86_64" then - defaults.variables.CC = os.getenv("CC") or "x86_64-w64-mingw32-gcc" - defaults.variables.LD = os.getenv("CC") or "x86_64-w64-mingw32-gcc" - else - defaults.variables.CC = os.getenv("CC") or "mingw32-gcc" - defaults.variables.LD = os.getenv("CC") or "mingw32-gcc" - end - defaults.variables.AR = os.getenv("AR") or "ar" - defaults.variables.RC = os.getenv("WINDRES") or "windres" - defaults.variables.RANLIB = os.getenv("RANLIB") or "ranlib" - defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2" - defaults.variables.LDFLAGS = os.getenv("LDFLAGS") - defaults.variables.LIBFLAG = "-shared" - defaults.makefile = "Makefile" - defaults.external_deps_patterns = { - bin = { "?.exe", "?.bat" }, - -- mingw lookup list from http://stackoverflow.com/a/15853231/1793220 - -- ...should we keep ?.lib at the end? It's not in the above list. - lib = { "lib?.dll.a", "?.dll.a", "lib?.a", "cyg?.dll", "lib?.dll", "?.dll", "?.lib" }, - include = { "?.h" } - } - defaults.runtime_external_deps_patterns = { - bin = { "?.exe", "?.bat" }, - lib = { "cyg?.dll", "?.dll", "lib?.dll" }, - include = { "?.h" } - } - defaults.link_lua_explicitly = true - end - - if platforms.unix then - defaults.lib_extension = "so" - defaults.static_lib_extension = "a" - defaults.external_lib_extension = "so" - defaults.obj_extension = "o" - defaults.external_deps_dirs = { "/usr/local", "/usr", "/" } - - defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2" - -- we pass -fPIC via CFLAGS because of old Makefile-based Lua projects - -- which didn't have -fPIC in their Makefiles but which honor CFLAGS - if not defaults.variables.CFLAGS:match("-fPIC") then - defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC" - end - - defaults.variables.LDFLAGS = os.getenv("LDFLAGS") - - defaults.cmake_generator = "Unix Makefiles" - defaults.variables.CC = os.getenv("CC") or "gcc" - defaults.variables.LD = os.getenv("CC") or "gcc" - defaults.gcc_rpath = true - defaults.variables.LIBFLAG = "-shared" - defaults.variables.TEST = "test" - - defaults.external_deps_patterns = { - bin = { "?" }, - lib = { "lib?.a", "lib?.so", "lib?.so.*" }, - include = { "?.h" } - } - defaults.runtime_external_deps_patterns = { - bin = { "?" }, - lib = { "lib?.so", "lib?.so.*" }, - include = { "?.h" } - } - defaults.export_path_separator = ":" - defaults.wrapper_suffix = "" - local xdg_cache_home = os.getenv("XDG_CACHE_HOME") or home.."/.cache" - defaults.local_cache = xdg_cache_home.."/luarocks" - defaults.web_browser = "xdg-open" - end - - if platforms.cygwin then - defaults.lib_extension = "so" -- can be overridden in the config file for mingw builds - defaults.arch = "cygwin-"..target_cpu - defaults.cmake_generator = "Unix Makefiles" - defaults.variables.CC = "echo -llua | xargs " .. (os.getenv("CC") or "gcc") - defaults.variables.LD = "echo -llua | xargs " .. (os.getenv("CC") or "gcc") - defaults.variables.LIBFLAG = "-shared" - defaults.link_lua_explicitly = true - end - - if platforms.msys then - -- msys is basically cygwin made out of mingw, meaning the subsytem is unixish - -- enough, yet we can freely mix with native win32 - defaults.external_deps_patterns = { - bin = { "?.exe", "?.bat", "?" }, - lib = { "lib?.so", "lib?.so.*", "lib?.dll.a", "?.dll.a", - "lib?.a", "lib?.dll", "?.dll", "?.lib" }, - include = { "?.h" } - } - defaults.runtime_external_deps_patterns = { - bin = { "?.exe", "?.bat" }, - lib = { "lib?.so", "?.dll", "lib?.dll" }, - include = { "?.h" } - } - if platforms.mingw then - -- MSYS2 can build Windows programs that depend on - -- msys-2.0.dll (based on Cygwin) but MSYS2 is also designed - -- for building native Windows programs by MinGW. These - -- programs don't depend on msys-2.0.dll. - local pipe = io.popen("cygpath --windows %MINGW_PREFIX%") - local mingw_prefix = pipe:read("*l") - pipe:close() - defaults.external_deps_dirs = { - mingw_prefix, - dir.path("c:", "windows", "system32"), - } - defaults.makefile = "Makefile" - defaults.cmake_generator = "MSYS Makefiles" - defaults.local_cache = dir.path(home, ".cache", "luarocks") - defaults.variables.MAKE = os.getenv("MAKE") or "make" - defaults.variables.CC = os.getenv("CC") or "gcc" - defaults.variables.RC = os.getenv("WINDRES") or "windres" - defaults.variables.LD = os.getenv("CC") or "gcc" - defaults.variables.MT = os.getenv("MT") or nil - defaults.variables.AR = os.getenv("AR") or "ar" - defaults.variables.RANLIB = os.getenv("RANLIB") or "ranlib" - - defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2 -fPIC" - if not defaults.variables.CFLAGS:match("-fPIC") then - defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC" - end - - defaults.variables.LIBFLAG = "-shared" - end - end - - if platforms.bsd then - defaults.variables.MAKE = "gmake" - defaults.gcc_rpath = false - defaults.variables.CC = os.getenv("CC") or "cc" - defaults.variables.LD = os.getenv("CC") or defaults.variables.CC - end - - if platforms.macosx then - defaults.variables.MAKE = os.getenv("MAKE") or "make" - defaults.external_lib_extension = "dylib" - defaults.arch = "macosx-"..target_cpu - defaults.variables.LIBFLAG = "-bundle -undefined dynamic_lookup -all_load" - local version = util.popen_read("sw_vers -productVersion") - if not (version:match("^%d+%.%d+%.%d+$") or version:match("^%d+%.%d+$")) then - version = "10.3" - end - version = vers.parse_version(version) - if version >= vers.parse_version("11.0") then - version = vers.parse_version("11.0") - elseif version >= vers.parse_version("10.10") then - version = vers.parse_version("10.8") - elseif version >= vers.parse_version("10.5") then - version = vers.parse_version("10.5") - else - defaults.gcc_rpath = false - end - defaults.variables.CC = "env MACOSX_DEPLOYMENT_TARGET="..tostring(version).." gcc" - defaults.variables.LD = "env MACOSX_DEPLOYMENT_TARGET="..tostring(version).." gcc" - defaults.web_browser = "open" - - -- XCode SDK - local sdk_path = util.popen_read("xcrun --show-sdk-path 2>/dev/null") - if sdk_path then - table.insert(defaults.external_deps_dirs, sdk_path .. "/usr") - table.insert(defaults.external_deps_patterns.lib, 1, "lib?.tbd") - table.insert(defaults.runtime_external_deps_patterns.lib, 1, "lib?.tbd") - end - - -- Homebrew - table.insert(defaults.external_deps_dirs, "/usr/local/opt") - defaults.external_deps_subdirs.lib = { "lib", "" } - defaults.runtime_external_deps_subdirs.lib = { "lib", "" } - table.insert(defaults.external_deps_patterns.lib, 1, "/?/lib/lib?.dylib") - table.insert(defaults.runtime_external_deps_patterns.lib, 1, "/?/lib/lib?.dylib") - end - - if platforms.linux then - defaults.arch = "linux-"..target_cpu - - local gcc_arch = util.popen_read("gcc -print-multiarch 2>/dev/null") - if gcc_arch and gcc_arch ~= "" then - defaults.external_deps_subdirs.lib = { "lib/" .. gcc_arch, "lib64", "lib" } - defaults.runtime_external_deps_subdirs.lib = { "lib/" .. gcc_arch, "lib64", "lib" } - else - defaults.external_deps_subdirs.lib = { "lib64", "lib" } - defaults.runtime_external_deps_subdirs.lib = { "lib64", "lib" } - end - end - - if platforms.freebsd then - defaults.arch = "freebsd-"..target_cpu - elseif platforms.dragonfly then - defaults.arch = "dragonfly-"..target_cpu - elseif platforms.openbsd then - defaults.arch = "openbsd-"..target_cpu - elseif platforms.netbsd then - defaults.arch = "netbsd-"..target_cpu - elseif platforms.solaris then - defaults.arch = "solaris-"..target_cpu - defaults.variables.MAKE = "gmake" - end - - -- Expose some more values detected by LuaRocks for use by rockspec authors. - defaults.variables.LIB_EXTENSION = defaults.lib_extension - defaults.variables.OBJ_EXTENSION = defaults.obj_extension - - return defaults -end - -local function use_defaults(cfg, defaults) - - -- Populate variables with values from their 'defaults' counterparts - -- if they were not already set by user. - if not cfg.variables then - cfg.variables = {} - end - for k,v in pairs(defaults.variables) do - if not cfg.variables[k] then - cfg.variables[k] = v - end - end - - util.deep_merge_under(cfg, defaults) - - -- FIXME get rid of this - if not cfg.check_certificates then - cfg.variables.CURLNOCERTFLAG = "-k" - cfg.variables.WGETNOCERTFLAG = "--no-check-certificate" - end -end - --------------------------------------------------------------------------------- - -local cfg = {} - ---- Initializes the LuaRocks configuration for variables, paths --- and OS detection. --- @param detected table containing information detected about the --- environment. All fields below are optional: --- * lua_version (in x.y format, e.g. "5.3") --- * lua_bindir (e.g. "/usr/local/bin") --- * lua_dir (e.g. "/usr/local") --- * lua (e.g. "/usr/local/bin/lua-5.3") --- * project_dir (a string with the path of the project directory --- when using per-project environments, as created with `luarocks init`) --- @param warning a logging function for warnings that takes a string --- @return true on success; nil and an error message on failure. -function cfg.init(detected, warning) - detected = detected or {} - - local exit_ok = true - local exit_err = nil - local exit_what = nil - - local hc_ok, hardcoded = pcall(require, "luarocks.core.hardcoded") - if not hc_ok then - hardcoded = {} - end - - local init = cfg.init - - ---------------------------------------- - -- Reset the cfg table. - ---------------------------------------- - - for k, _ in pairs(cfg) do - cfg[k] = nil - end - - cfg.program_version = program_version - - if hardcoded.IS_BINARY then - cfg.is_binary = true - end - - -- Use detected values as defaults, overridable via config files or CLI args - - local hardcoded_lua = hardcoded.LUA - local hardcoded_lua_dir = hardcoded.LUA_DIR - local hardcoded_lua_bindir = hardcoded.LUA_BINDIR - local hardcoded_lua_incdir = hardcoded.LUA_INCDIR - local hardcoded_lua_libdir = hardcoded.LUA_LIBDIR - local hardcoded_lua_version = hardcoded.LUA_VERSION or _VERSION:sub(5) - - -- if --lua-version or --lua-dir are passed from the CLI, - -- don't use the hardcoded paths at all - if detected.given_lua_version or detected.given_lua_dir then - hardcoded_lua = nil - hardcoded_lua_dir = nil - hardcoded_lua_bindir = nil - hardcoded_lua_incdir = nil - hardcoded_lua_libdir = nil - hardcoded_lua_version = nil - end - - cfg.lua_version = detected.lua_version or hardcoded_lua_version - cfg.project_dir = (not hardcoded.FORCE_CONFIG) and detected.project_dir - - do - local lua = detected.lua or hardcoded_lua - local lua_dir = detected.lua_dir or hardcoded_lua_dir - local lua_bindir = detected.lua_bindir or hardcoded_lua_bindir - cfg.variables = { - LUA = lua, - LUA_DIR = lua_dir, - LUA_BINDIR = lua_bindir, - LUA_LIBDIR = hardcoded_lua_libdir, - LUA_INCDIR = hardcoded_lua_incdir, - } - end - - cfg.init = init - - ---------------------------------------- - -- System detection. - ---------------------------------------- - - -- A proper build of LuaRocks will hardcode the system - -- and proc values with hardcoded.SYSTEM and hardcoded.PROCESSOR. - -- If that is not available, we try to identify the system. - local system, processor = sysdetect.detect() - if hardcoded.SYSTEM then - system = hardcoded.SYSTEM - end - if hardcoded.PROCESSOR then - processor = hardcoded.PROCESSOR - end - - if system == "windows" then - if os.getenv("VCINSTALLDIR") then - -- running from the Development Command prompt for VS 2017 - system = "windows" - else - local msystem = os.getenv("MSYSTEM") - if msystem == nil then - system = "mingw" - elseif msystem == "MSYS" then - system = "msys" - else - -- MINGW32 or MINGW64 - system = "msys2_mingw_w64" - end - end - end - - cfg.target_cpu = processor - - local platforms = make_platforms(system) - - ---------------------------------------- - -- Platform is determined. - -- Let's load the config files. - ---------------------------------------- - - local sys_config_file - local home_config_file - local project_config_file - - local config_file_name = "config-"..cfg.lua_version..".lua" - - do - local sysconfdir = os.getenv("LUAROCKS_SYSCONFDIR") or hardcoded.SYSCONFDIR - if platforms.windows and not platforms.msys2_mingw_w64 then - cfg.home = os.getenv("APPDATA") or "c:" - cfg.home_tree = dir.path(cfg.home, "luarocks") - cfg.sysconfdir = sysconfdir or dir.path((os.getenv("PROGRAMFILES") or "c:"), "luarocks") - else - cfg.home = os.getenv("HOME") or "" - cfg.home_tree = dir.path(cfg.home, ".luarocks") - cfg.sysconfdir = sysconfdir or detect_sysconfdir() or "/etc/luarocks" - end - end - - -- Load system configuration file - sys_config_file = dir.path(cfg.sysconfdir, config_file_name) - local sys_config_ok, err = load_config_file(cfg, platforms, sys_config_file) - if err then - exit_ok, exit_err, exit_what = nil, err, "config" - end - - -- Load user configuration file (if allowed) - local home_config_ok - local project_config_ok - if not hardcoded.FORCE_CONFIG then - local env_var = "LUAROCKS_CONFIG_" .. cfg.lua_version:gsub("%.", "_") - local env_value = os.getenv(env_var) - if not env_value then - env_var = "LUAROCKS_CONFIG" - env_value = os.getenv(env_var) - end - -- first try environment provided file, so we can explicitly warn when it is missing - if env_value then - local env_ok, err = load_config_file(cfg, platforms, env_value) - if err then - exit_ok, exit_err, exit_what = nil, err, "config" - elseif warning and not env_ok then - warning("Warning: could not load configuration file `"..env_value.."` given in environment variable "..env_var.."\n") - end - if env_ok then - home_config_ok = true - home_config_file = env_value - end - end - - -- try XDG config home - if platforms.unix and not home_config_ok then - local xdg_config_home = os.getenv("XDG_CONFIG_HOME") or dir.path(cfg.home, ".config") - cfg.homeconfdir = dir.path(xdg_config_home, "luarocks") - home_config_file = dir.path(cfg.homeconfdir, config_file_name) - home_config_ok, err = load_config_file(cfg, platforms, home_config_file) - if err then - exit_ok, exit_err, exit_what = nil, err, "config" - end - end - - -- try the alternative defaults if there was no environment specified file or it didn't work - if not home_config_ok then - cfg.homeconfdir = cfg.home_tree - home_config_file = dir.path(cfg.homeconfdir, config_file_name) - home_config_ok, err = load_config_file(cfg, platforms, home_config_file) - if err then - exit_ok, exit_err, exit_what = nil, err, "config" - end - end - - -- finally, use the project-specific config file if any - if cfg.project_dir then - project_config_file = dir.path(cfg.project_dir, ".luarocks", config_file_name) - project_config_ok, err = load_config_file(cfg, platforms, project_config_file) - if err then - exit_ok, exit_err, exit_what = nil, err, "config" - end - end - end - - -- backwards compatibility: - if cfg.lua_interpreter and cfg.variables.LUA_BINDIR and not cfg.variables.LUA then - cfg.variables.LUA = dir.path(cfg.variables.LUA_BINDIR, cfg.lua_interpreter) - end - - ---------------------------------------- - -- Config files are loaded. - -- Let's finish up the cfg table. - ---------------------------------------- - - -- Settings given via the CLI (i.e. --lua-dir) take precedence over config files. - cfg.project_dir = detected.given_project_dir or cfg.project_dir - cfg.lua_version = detected.given_lua_version or cfg.lua_version - if detected.given_lua_dir then - cfg.variables.LUA = detected.lua - cfg.variables.LUA_DIR = detected.given_lua_dir - cfg.variables.LUA_BINDIR = detected.lua_bindir - cfg.variables.LUA_LIBDIR = nil - cfg.variables.LUA_INCDIR = nil - end - - -- Build a default list of rocks trees if not given - if cfg.rocks_trees == nil then - cfg.rocks_trees = {} - if cfg.home_tree then - table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree } ) - end - if hardcoded.PREFIX and hardcoded.PREFIX ~= cfg.home_tree then - table.insert(cfg.rocks_trees, { name = "system", root = hardcoded.PREFIX } ) - end - end - - local defaults = make_defaults(cfg.lua_version, processor, platforms, cfg.home) - - if platforms.windows and hardcoded.WIN_TOOLS then - local tools = { "SEVENZ", "CP", "FIND", "LS", "MD5SUM", "WGET", } - for _, tool in ipairs(tools) do - defaults.variables[tool] = '"' .. dir.path(hardcoded.WIN_TOOLS, defaults.variables[tool] .. '.exe') .. '"' - end - else - defaults.fs_use_modules = true - end - - -- if only cfg.variables.LUA is given in config files, - -- derive LUA_BINDIR and LUA_DIR from them. - if cfg.variables.LUA and not cfg.variables.LUA_BINDIR then - cfg.variables.LUA_BINDIR = cfg.variables.LUA:match("^(.*)[\\/][^\\/]*$") - if not cfg.variables.LUA_DIR then - cfg.variables.LUA_DIR = cfg.variables.LUA_BINDIR:gsub("[\\/]bin$", "") or cfg.variables.LUA_BINDIR - end - end - - use_defaults(cfg, defaults) - - cfg.user_agent = "LuaRocks/"..cfg.program_version.." "..cfg.arch - - cfg.config_files = { - project = cfg.project_dir and { - file = project_config_file, - found = not not project_config_ok, - }, - system = { - file = sys_config_file, - found = not not sys_config_ok, - }, - user = { - file = home_config_file, - found = not not home_config_ok, - }, - nearest = project_config_ok - and project_config_file - or (home_config_ok - and home_config_file - or sys_config_file), - } - - cfg.cache = {} - - ---------------------------------------- - -- Attributes of cfg are set. - -- Let's add some methods. - ---------------------------------------- - - do - local function make_paths_from_tree(tree) - local lua_path, lib_path, bin_path - if type(tree) == "string" then - lua_path = dir.path(tree, cfg.lua_modules_path) - lib_path = dir.path(tree, cfg.lib_modules_path) - bin_path = dir.path(tree, "bin") - else - lua_path = tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path) - lib_path = tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path) - bin_path = tree.bin_dir or dir.path(tree.root, "bin") - end - return lua_path, lib_path, bin_path - end - - function cfg.package_paths(current) - local new_path, new_cpath, new_bin = {}, {}, {} - local function add_tree_to_paths(tree) - local lua_path, lib_path, bin_path = make_paths_from_tree(tree) - table.insert(new_path, dir.path(lua_path, "?.lua")) - table.insert(new_path, dir.path(lua_path, "?", "init.lua")) - table.insert(new_cpath, dir.path(lib_path, "?."..cfg.lib_extension)) - table.insert(new_bin, bin_path) - end - if current then - add_tree_to_paths(current) - end - for _,tree in ipairs(cfg.rocks_trees) do - add_tree_to_paths(tree) - end - return table.concat(new_path, ";"), table.concat(new_cpath, ";"), table.concat(new_bin, cfg.export_path_separator) - end - end - - function cfg.init_package_paths() - local lr_path, lr_cpath, lr_bin = cfg.package_paths() - package.path = util.cleanup_path(package.path .. ";" .. lr_path, ";", cfg.lua_version, true) - package.cpath = util.cleanup_path(package.cpath .. ";" .. lr_cpath, ";", cfg.lua_version, true) - end - - --- Check if platform was detected - -- @param name string: The platform name to check. - -- @return boolean: true if LuaRocks is currently running on queried platform. - function cfg.is_platform(name) - assert(type(name) == "string") - return platforms[name] - end - - -- @param direction (optional) "least-specific-first" (default) or "most-specific-first" - function cfg.each_platform(direction) - direction = direction or "least-specific-first" - local i, delta - if direction == "least-specific-first" then - i = 0 - delta = 1 - else - i = #platform_order + 1 - delta = -1 - end - return function() - local p - repeat - i = i + delta - p = platform_order[i] - until (not p) or platforms[p] - return p - end - end - - function cfg.print_platforms() - local platform_keys = {} - for k,_ in pairs(platforms) do - table.insert(platform_keys, k) - end - table.sort(platform_keys) - return table.concat(platform_keys, ", ") - end - - return exit_ok, exit_err, exit_what -end - -return cfg diff --git a/src/luarocks/core/cfg.d.tl b/src/luarocks/core/cfg.d.tl new file mode 100644 index 00000000..09815260 --- /dev/null +++ b/src/luarocks/core/cfg.d.tl @@ -0,0 +1,7 @@ +local record cfg + detect_sysconfdir: function(): string + make_platforms: function(system: string): {any: boolean} + make_defaults: function(lua_version: string, target_cpu: string, platforms: {any: any}, home: string): {any: any} + use_defaults: function(cfg, defaults: {any: any}) + --! +end \ No newline at end of file diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg.lua index 4ae9bd6e..6d8fe55b 100644 --- a/src/luarocks/core/cfg.lua +++ b/src/luarocks/core/cfg.lua @@ -1,17 +1,17 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local debug = _tl_compat and _tl_compat.debug or debug; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local os = _tl_compat and _tl_compat.os or os; local package = _tl_compat and _tl_compat.package or package; local pairs = _tl_compat and _tl_compat.pairs or pairs; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table +--- Configuration for LuaRocks. +-- Tries to load the user's configuration file and +-- defines defaults for unset values. See the +-- config +-- file format documentation for details. +-- +-- End-users shouldn't edit this file. They can override any defaults +-- set in this file using their system-wide or user-specific configuration +-- files. Run `luarocks` with no arguments to see the locations of +-- these files in your platform. - - - - - - - - - -local table, pairs, require, os, pcall, ipairs, package, type, assert = -table, pairs, require, os, pcall, ipairs, package, type, assert +local table, pairs, require, os, pcall, ipairs, package, type, assert = + table, pairs, require, os, pcall, ipairs, package, type, assert local dir = require("luarocks.core.dir") local util = require("luarocks.core.util") @@ -19,21 +19,17 @@ local persist = require("luarocks.core.persist") local sysdetect = require("luarocks.core.sysdetect") local vers = require("luarocks.core.vers") -local cfg = {} - - - - +-------------------------------------------------------------------------------- local program_version = "dev" -local is_windows = package.config:sub(1, 1) == "\\" - - - +local is_windows = package.config:sub(1,1) == "\\" +-- Set order for platform overrides. +-- More general platform identifiers should be listed first, +-- more specific ones later. local platform_order = { - + -- Unixes "unix", "bsd", "solaris", @@ -46,7 +42,7 @@ local platform_order = { "cygwin", "msys", "haiku", - + -- Windows "windows", "win32", "mingw", @@ -54,17 +50,6 @@ local platform_order = { "msys2_mingw_w64", } - - - - - - - - - - - local function detect_sysconfdir() if not debug then return @@ -81,7 +66,7 @@ local function detect_sysconfdir() if not basedir then return end - + -- If installed in a Unix-like tree, use a Unix-like sysconfdir local installdir = basedir:match("^(.*)[\\/]share[\\/]lua[\\/][^/]*$") if installdir then if installdir == "/usr" then @@ -89,16 +74,16 @@ local function detect_sysconfdir() end return dir.path(installdir, "etc", "luarocks") end - + -- Otherwise, use base directory of sources return basedir end - +local load_config_file do - + -- Create global environment for the config files; local function env_for_config_file(cfg, platforms) local platforms_copy = {} - for k, v in pairs(platforms) do + for k,v in pairs(platforms) do platforms_copy[k] = v end @@ -107,28 +92,28 @@ do home = cfg.home, lua_version = cfg.lua_version, platforms = platforms_copy, - processor = cfg.target_cpu, - target_cpu = cfg.target_cpu, + processor = cfg.target_cpu, -- remains for compat reasons + target_cpu = cfg.target_cpu, -- replaces `processor` os_getenv = os.getenv, variables = cfg.variables or {}, dump_env = function() - - + -- debug function, calling it from a config file will show all + -- available globals to that config file print(util.show_table(e, "global environment")) end, } return e end - + -- Merge values from config files read into the `cfg` table local function merge_overrides(cfg, overrides) - + -- remove some stuff we do not want to integrate overrides.os_getenv = nil overrides.dump_env = nil - - if overrides.rocks_trees then cfg.rocks_trees = nil end + -- remove tables to be copied verbatim instead of deeply merged + if overrides.rocks_trees then cfg.rocks_trees = nil end if overrides.rocks_servers then cfg.rocks_servers = nil end - + -- perform actual merge util.deep_merge(cfg, overrides) end @@ -140,30 +125,30 @@ do for _, v in ipairs(overrides) do platforms[v] = true end - - + -- set some fallback default in case the user provides an incomplete configuration. + -- LuaRocks expects a set of defaults to be available. if not (platforms.unix or platforms.windows) then platforms[is_windows and "windows" or "unix"] = true end end end - - - local load_config_file = function(cfg, platforms, filepath) + -- Load config file and merge its contents into the `cfg` module table. + -- @return filepath of successfully loaded file or nil if it failed + load_config_file = function(cfg, platforms, filepath) local result, err, errcode = persist.load_into_table(filepath, env_for_config_file(cfg, platforms)) if (not result) and errcode ~= "open" then - + -- errcode is either "load" or "run"; bad config file, so error out return nil, err, "config" end if result then - + -- success in loading and running, merge contents and exit update_platforms(platforms, result.platforms) result.platforms = nil merge_overrides(cfg, result) return filepath end - return nil + return nil -- nothing was loaded end end @@ -184,15 +169,15 @@ local platform_sets = { } local function make_platforms(system) - + -- fallback to Unix in unknown systems return platform_sets[system] or { unix = true } end - +-------------------------------------------------------------------------------- local function make_defaults(lua_version, target_cpu, platforms, home) - + -- Configure defaults: local defaults = { local_by_default = false, @@ -208,29 +193,19 @@ local function make_defaults(lua_version, target_cpu, platforms, home) lua_modules_path = dir.path("share", "lua", lua_version), lib_modules_path = dir.path("lib", "lua", lua_version), - rocks_subdir = dir.path("lib", "luarocks", "rocks-" .. lua_version), + rocks_subdir = dir.path("lib", "luarocks", "rocks-"..lua_version), arch = "unknown", lib_extension = "unknown", - external_lib_extension = "unknown", - static_lib_extension = "unknown", obj_extension = "unknown", link_lua_explicitly = false, - external_deps_dirs = {}, - external_deps_patterns = {}, - runtime_external_deps_patterns = {}, - - export_path_separator = "", - wrapper_suffix = "", - - rocks_servers = { { - "https://luarocks.org", - "https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/", - "https://loadk.com/luarocks/", - }, + "https://luarocks.org", + "https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/", + "https://loadk.com/luarocks/", + } }, disabled_servers = {}, @@ -241,13 +216,7 @@ local function make_defaults(lua_version, target_cpu, platforms, home) }, lua_extension = "lua", - connection_timeout = 30, - - makefile = "", - local_cache = "", - web_browser = "", - cmake_generator = "", - gcc_rpath = false, + connection_timeout = 30, -- 0 = no timeout variables = { MAKE = os.getenv("MAKE") or "make", @@ -298,30 +267,23 @@ local function make_defaults(lua_version, target_cpu, platforms, home) RSYNCFLAGS = "--exclude=.git -Oavz", CURLNOCERTFLAG = "", WGETNOCERTFLAG = "", - - RC = "", - MT = "", - CFLAGS = "", - LDFLAGS = "", - LIBFLAG = "", - TEST = "", }, external_deps_subdirs = { bin = "bin", lib = "lib", - include = "include", + include = "include" }, runtime_external_deps_subdirs = { bin = "bin", lib = "lib", - include = "include", + include = "include" }, } if platforms.windows then - defaults.arch = "win32-" .. target_cpu + defaults.arch = "win32-"..target_cpu defaults.lib_extension = "dll" defaults.external_lib_extension = "dll" defaults.static_lib_extension = "lib" @@ -347,19 +309,19 @@ local function make_defaults(lua_version, target_cpu, platforms, home) defaults.external_deps_patterns = { bin = { "?.exe", "?.bat" }, lib = { "?.lib", "lib?.lib", "?.dll", "lib?.dll" }, - include = { "?.h" }, + include = { "?.h" } } defaults.runtime_external_deps_patterns = { bin = { "?.exe", "?.bat" }, lib = { "?.dll", "lib?.dll" }, - include = { "?.h" }, + include = { "?.h" } } defaults.export_path_separator = ";" defaults.wrapper_suffix = ".bat" local localappdata = os.getenv("LOCALAPPDATA") if not localappdata then - + -- for Windows versions below Vista localappdata = dir.path((os.getenv("USERPROFILE") or dir.path("c:", "Users", "All Users")), "Local Settings", "Application Data") end defaults.local_cache = dir.path(localappdata, "LuaRocks", "Cache") @@ -397,15 +359,15 @@ local function make_defaults(lua_version, target_cpu, platforms, home) defaults.makefile = "Makefile" defaults.external_deps_patterns = { bin = { "?.exe", "?.bat" }, - - + -- mingw lookup list from http://stackoverflow.com/a/15853231/1793220 + -- ...should we keep ?.lib at the end? It's not in the above list. lib = { "lib?.dll.a", "?.dll.a", "lib?.a", "cyg?.dll", "lib?.dll", "?.dll", "?.lib" }, - include = { "?.h" }, + include = { "?.h" } } defaults.runtime_external_deps_patterns = { bin = { "?.exe", "?.bat" }, lib = { "cyg?.dll", "?.dll", "lib?.dll" }, - include = { "?.h" }, + include = { "?.h" } } defaults.link_lua_explicitly = true end @@ -418,10 +380,10 @@ local function make_defaults(lua_version, target_cpu, platforms, home) defaults.external_deps_dirs = { "/usr/local", "/usr", "/" } defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2" - - + -- we pass -fPIC via CFLAGS because of old Makefile-based Lua projects + -- which didn't have -fPIC in their Makefiles but which honor CFLAGS if not defaults.variables.CFLAGS:match("-fPIC") then - defaults.variables.CFLAGS = defaults.variables.CFLAGS .. " -fPIC" + defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC" end defaults.variables.LDFLAGS = os.getenv("LDFLAGS") @@ -436,23 +398,23 @@ local function make_defaults(lua_version, target_cpu, platforms, home) defaults.external_deps_patterns = { bin = { "?" }, lib = { "lib?.a", "lib?.so", "lib?.so.*" }, - include = { "?.h" }, + include = { "?.h" } } defaults.runtime_external_deps_patterns = { bin = { "?" }, lib = { "lib?.so", "lib?.so.*" }, - include = { "?.h" }, + include = { "?.h" } } defaults.export_path_separator = ":" defaults.wrapper_suffix = "" - local xdg_cache_home = os.getenv("XDG_CACHE_HOME") or home .. "/.cache" - defaults.local_cache = xdg_cache_home .. "/luarocks" + local xdg_cache_home = os.getenv("XDG_CACHE_HOME") or home.."/.cache" + defaults.local_cache = xdg_cache_home.."/luarocks" defaults.web_browser = "xdg-open" end if platforms.cygwin then - defaults.lib_extension = "so" - defaults.arch = "cygwin-" .. target_cpu + defaults.lib_extension = "so" -- can be overridden in the config file for mingw builds + defaults.arch = "cygwin-"..target_cpu defaults.cmake_generator = "Unix Makefiles" defaults.variables.CC = "echo -llua | xargs " .. (os.getenv("CC") or "gcc") defaults.variables.LD = "echo -llua | xargs " .. (os.getenv("CC") or "gcc") @@ -461,24 +423,24 @@ local function make_defaults(lua_version, target_cpu, platforms, home) end if platforms.msys then - - + -- msys is basically cygwin made out of mingw, meaning the subsytem is unixish + -- enough, yet we can freely mix with native win32 defaults.external_deps_patterns = { bin = { "?.exe", "?.bat", "?" }, lib = { "lib?.so", "lib?.so.*", "lib?.dll.a", "?.dll.a", -"lib?.a", "lib?.dll", "?.dll", "?.lib", }, - include = { "?.h" }, + "lib?.a", "lib?.dll", "?.dll", "?.lib" }, + include = { "?.h" } } defaults.runtime_external_deps_patterns = { bin = { "?.exe", "?.bat" }, lib = { "lib?.so", "?.dll", "lib?.dll" }, - include = { "?.h" }, + include = { "?.h" } } if platforms.mingw then - - - - + -- MSYS2 can build Windows programs that depend on + -- msys-2.0.dll (based on Cygwin) but MSYS2 is also designed + -- for building native Windows programs by MinGW. These + -- programs don't depend on msys-2.0.dll. local pipe = io.popen("cygpath --windows %MINGW_PREFIX%") local mingw_prefix = pipe:read("*l") pipe:close() @@ -499,7 +461,7 @@ local function make_defaults(lua_version, target_cpu, platforms, home) defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2 -fPIC" if not defaults.variables.CFLAGS:match("-fPIC") then - defaults.variables.CFLAGS = defaults.variables.CFLAGS .. " -fPIC" + defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC" end defaults.variables.LIBFLAG = "-shared" @@ -516,7 +478,7 @@ local function make_defaults(lua_version, target_cpu, platforms, home) if platforms.macosx then defaults.variables.MAKE = os.getenv("MAKE") or "make" defaults.external_lib_extension = "dylib" - defaults.arch = "macosx-" .. target_cpu + defaults.arch = "macosx-"..target_cpu defaults.variables.LIBFLAG = "-bundle -undefined dynamic_lookup -all_load" local version = util.popen_read("sw_vers -productVersion") if not (version:match("^%d+%.%d+%.%d+$") or version:match("^%d+%.%d+$")) then @@ -532,11 +494,11 @@ local function make_defaults(lua_version, target_cpu, platforms, home) else defaults.gcc_rpath = false end - defaults.variables.CC = "env MACOSX_DEPLOYMENT_TARGET=" .. tostring(version) .. " gcc" - defaults.variables.LD = "env MACOSX_DEPLOYMENT_TARGET=" .. tostring(version) .. " gcc" + defaults.variables.CC = "env MACOSX_DEPLOYMENT_TARGET="..tostring(version).." gcc" + defaults.variables.LD = "env MACOSX_DEPLOYMENT_TARGET="..tostring(version).." gcc" defaults.web_browser = "open" - + -- XCode SDK local sdk_path = util.popen_read("xcrun --show-sdk-path 2>/dev/null") if sdk_path then table.insert(defaults.external_deps_dirs, sdk_path .. "/usr") @@ -544,7 +506,7 @@ local function make_defaults(lua_version, target_cpu, platforms, home) table.insert(defaults.runtime_external_deps_patterns.lib, 1, "lib?.tbd") end - + -- Homebrew table.insert(defaults.external_deps_dirs, "/usr/local/opt") defaults.external_deps_subdirs.lib = { "lib", "" } defaults.runtime_external_deps_subdirs.lib = { "lib", "" } @@ -553,7 +515,7 @@ local function make_defaults(lua_version, target_cpu, platforms, home) end if platforms.linux then - defaults.arch = "linux-" .. target_cpu + defaults.arch = "linux-"..target_cpu local gcc_arch = util.popen_read("gcc -print-multiarch 2>/dev/null") if gcc_arch and gcc_arch ~= "" then @@ -566,19 +528,19 @@ local function make_defaults(lua_version, target_cpu, platforms, home) end if platforms.freebsd then - defaults.arch = "freebsd-" .. target_cpu + defaults.arch = "freebsd-"..target_cpu elseif platforms.dragonfly then - defaults.arch = "dragonfly-" .. target_cpu + defaults.arch = "dragonfly-"..target_cpu elseif platforms.openbsd then - defaults.arch = "openbsd-" .. target_cpu + defaults.arch = "openbsd-"..target_cpu elseif platforms.netbsd then - defaults.arch = "netbsd-" .. target_cpu + defaults.arch = "netbsd-"..target_cpu elseif platforms.solaris then - defaults.arch = "solaris-" .. target_cpu + defaults.arch = "solaris-"..target_cpu defaults.variables.MAKE = "gmake" end - + -- Expose some more values detected by LuaRocks for use by rockspec authors. defaults.variables.LIB_EXTENSION = defaults.lib_extension defaults.variables.OBJ_EXTENSION = defaults.obj_extension @@ -587,12 +549,12 @@ end local function use_defaults(cfg, defaults) - - + -- Populate variables with values from their 'defaults' counterparts + -- if they were not already set by user. if not cfg.variables then cfg.variables = {} end - for k, v in pairs(defaults.variables) do + for k,v in pairs(defaults.variables) do if not cfg.variables[k] then cfg.variables[k] = v end @@ -600,29 +562,29 @@ local function use_defaults(cfg, defaults) util.deep_merge_under(cfg, defaults) - + -- FIXME get rid of this if not cfg.check_certificates then cfg.variables.CURLNOCERTFLAG = "-k" cfg.variables.WGETNOCERTFLAG = "--no-check-certificate" end end - +-------------------------------------------------------------------------------- local cfg = {} - - - - - - - - - - - - +--- Initializes the LuaRocks configuration for variables, paths +-- and OS detection. +-- @param detected table containing information detected about the +-- environment. All fields below are optional: +-- * lua_version (in x.y format, e.g. "5.3") +-- * lua_bindir (e.g. "/usr/local/bin") +-- * lua_dir (e.g. "/usr/local") +-- * lua (e.g. "/usr/local/bin/lua-5.3") +-- * project_dir (a string with the path of the project directory +-- when using per-project environments, as created with `luarocks init`) +-- @param warning a logging function for warnings that takes a string +-- @return true on success; nil and an error message on failure. function cfg.init(detected, warning) detected = detected or {} @@ -637,9 +599,9 @@ function cfg.init(detected, warning) local init = cfg.init - - - + ---------------------------------------- + -- Reset the cfg table. + ---------------------------------------- for k, _ in pairs(cfg) do cfg[k] = nil @@ -651,7 +613,7 @@ function cfg.init(detected, warning) cfg.is_binary = true end - + -- Use detected values as defaults, overridable via config files or CLI args local hardcoded_lua = hardcoded.LUA local hardcoded_lua_dir = hardcoded.LUA_DIR @@ -660,8 +622,8 @@ function cfg.init(detected, warning) local hardcoded_lua_libdir = hardcoded.LUA_LIBDIR local hardcoded_lua_version = hardcoded.LUA_VERSION or _VERSION:sub(5) - - + -- if --lua-version or --lua-dir are passed from the CLI, + -- don't use the hardcoded paths at all if detected.given_lua_version or detected.given_lua_dir then hardcoded_lua = nil hardcoded_lua_dir = nil @@ -689,13 +651,13 @@ function cfg.init(detected, warning) cfg.init = init + ---------------------------------------- + -- System detection. + ---------------------------------------- - - - - - - + -- A proper build of LuaRocks will hardcode the system + -- and proc values with hardcoded.SYSTEM and hardcoded.PROCESSOR. + -- If that is not available, we try to identify the system. local system, processor = sysdetect.detect() if hardcoded.SYSTEM then system = hardcoded.SYSTEM @@ -706,7 +668,7 @@ function cfg.init(detected, warning) if system == "windows" then if os.getenv("VCINSTALLDIR") then - + -- running from the Development Command prompt for VS 2017 system = "windows" else local msystem = os.getenv("MSYSTEM") @@ -715,7 +677,7 @@ function cfg.init(detected, warning) elseif msystem == "MSYS" then system = "msys" else - + -- MINGW32 or MINGW64 system = "msys2_mingw_w64" end end @@ -725,16 +687,16 @@ function cfg.init(detected, warning) local platforms = make_platforms(system) - - - - + ---------------------------------------- + -- Platform is determined. + -- Let's load the config files. + ---------------------------------------- local sys_config_file local home_config_file local project_config_file - local config_file_name = "config-" .. cfg.lua_version .. ".lua" + local config_file_name = "config-"..cfg.lua_version..".lua" do local sysconfdir = os.getenv("LUAROCKS_SYSCONFDIR") or hardcoded.SYSCONFDIR @@ -749,30 +711,30 @@ function cfg.init(detected, warning) end end - + -- Load system configuration file sys_config_file = dir.path(cfg.sysconfdir, config_file_name) local sys_config_ok, err = load_config_file(cfg, platforms, sys_config_file) if err then exit_ok, exit_err, exit_what = nil, err, "config" end - + -- Load user configuration file (if allowed) local home_config_ok local project_config_ok if not hardcoded.FORCE_CONFIG then - local env_var = "LUAROCKS_CONFIG_" .. cfg.lua_version:gsub("%.", "_") + local env_var = "LUAROCKS_CONFIG_" .. cfg.lua_version:gsub("%.", "_") local env_value = os.getenv(env_var) if not env_value then - env_var = "LUAROCKS_CONFIG" + env_var = "LUAROCKS_CONFIG" env_value = os.getenv(env_var) end - + -- first try environment provided file, so we can explicitly warn when it is missing if env_value then local env_ok, err = load_config_file(cfg, platforms, env_value) if err then exit_ok, exit_err, exit_what = nil, err, "config" elseif warning and not env_ok then - warning("Warning: could not load configuration file `" .. env_value .. "` given in environment variable " .. env_var .. "\n") + warning("Warning: could not load configuration file `"..env_value.."` given in environment variable "..env_var.."\n") end if env_ok then home_config_ok = true @@ -780,7 +742,7 @@ function cfg.init(detected, warning) end end - + -- try XDG config home if platforms.unix and not home_config_ok then local xdg_config_home = os.getenv("XDG_CONFIG_HOME") or dir.path(cfg.home, ".config") cfg.homeconfdir = dir.path(xdg_config_home, "luarocks") @@ -791,7 +753,7 @@ function cfg.init(detected, warning) end end - + -- try the alternative defaults if there was no environment specified file or it didn't work if not home_config_ok then cfg.homeconfdir = cfg.home_tree home_config_file = dir.path(cfg.homeconfdir, config_file_name) @@ -801,7 +763,7 @@ function cfg.init(detected, warning) end end - + -- finally, use the project-specific config file if any if cfg.project_dir then project_config_file = dir.path(cfg.project_dir, ".luarocks", config_file_name) project_config_ok, err = load_config_file(cfg, platforms, project_config_file) @@ -811,17 +773,17 @@ function cfg.init(detected, warning) end end - + -- backwards compatibility: if cfg.lua_interpreter and cfg.variables.LUA_BINDIR and not cfg.variables.LUA then cfg.variables.LUA = dir.path(cfg.variables.LUA_BINDIR, cfg.lua_interpreter) end + ---------------------------------------- + -- Config files are loaded. + -- Let's finish up the cfg table. + ---------------------------------------- - - - - - + -- Settings given via the CLI (i.e. --lua-dir) take precedence over config files. cfg.project_dir = detected.given_project_dir or cfg.project_dir cfg.lua_version = detected.given_lua_version or cfg.lua_version if detected.given_lua_dir then @@ -832,21 +794,21 @@ function cfg.init(detected, warning) cfg.variables.LUA_INCDIR = nil end - + -- Build a default list of rocks trees if not given if cfg.rocks_trees == nil then cfg.rocks_trees = {} if cfg.home_tree then - table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree }) + table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree } ) end if hardcoded.PREFIX and hardcoded.PREFIX ~= cfg.home_tree then - table.insert(cfg.rocks_trees, { name = "system", root = hardcoded.PREFIX }) + table.insert(cfg.rocks_trees, { name = "system", root = hardcoded.PREFIX } ) end end local defaults = make_defaults(cfg.lua_version, processor, platforms, cfg.home) if platforms.windows and hardcoded.WIN_TOOLS then - local tools = { "SEVENZ", "CP", "FIND", "LS", "MD5SUM", "WGET" } + local tools = { "SEVENZ", "CP", "FIND", "LS", "MD5SUM", "WGET", } for _, tool in ipairs(tools) do defaults.variables[tool] = '"' .. dir.path(hardcoded.WIN_TOOLS, defaults.variables[tool] .. '.exe') .. '"' end @@ -854,8 +816,8 @@ function cfg.init(detected, warning) defaults.fs_use_modules = true end - - + -- if only cfg.variables.LUA is given in config files, + -- derive LUA_BINDIR and LUA_DIR from them. if cfg.variables.LUA and not cfg.variables.LUA_BINDIR then cfg.variables.LUA_BINDIR = cfg.variables.LUA:match("^(.*)[\\/][^\\/]*$") if not cfg.variables.LUA_DIR then @@ -865,7 +827,7 @@ function cfg.init(detected, warning) use_defaults(cfg, defaults) - cfg.user_agent = "LuaRocks/" .. cfg.program_version .. " " .. cfg.arch + cfg.user_agent = "LuaRocks/"..cfg.program_version.." "..cfg.arch cfg.config_files = { project = cfg.project_dir and { @@ -880,19 +842,19 @@ function cfg.init(detected, warning) file = home_config_file, found = not not home_config_ok, }, - nearest = project_config_ok and - project_config_file or - (home_config_ok and - home_config_file or - sys_config_file), + nearest = project_config_ok + and project_config_file + or (home_config_ok + and home_config_file + or sys_config_file), } cfg.cache = {} - - - - + ---------------------------------------- + -- Attributes of cfg are set. + -- Let's add some methods. + ---------------------------------------- do local function make_paths_from_tree(tree) @@ -913,15 +875,15 @@ function cfg.init(detected, warning) local new_path, new_cpath, new_bin = {}, {}, {} local function add_tree_to_paths(tree) local lua_path, lib_path, bin_path = make_paths_from_tree(tree) - table.insert(new_path, dir.path(lua_path, "?.lua")) - table.insert(new_path, dir.path(lua_path, "?", "init.lua")) - table.insert(new_cpath, dir.path(lib_path, "?." .. cfg.lib_extension)) + table.insert(new_path, dir.path(lua_path, "?.lua")) + table.insert(new_path, dir.path(lua_path, "?", "init.lua")) + table.insert(new_cpath, dir.path(lib_path, "?."..cfg.lib_extension)) table.insert(new_bin, bin_path) end if current then add_tree_to_paths(current) end - for _, tree in ipairs(cfg.rocks_trees) do + for _,tree in ipairs(cfg.rocks_trees) do add_tree_to_paths(tree) end return table.concat(new_path, ";"), table.concat(new_cpath, ";"), table.concat(new_bin, cfg.export_path_separator) @@ -934,15 +896,15 @@ function cfg.init(detected, warning) package.cpath = util.cleanup_path(package.cpath .. ";" .. lr_cpath, ";", cfg.lua_version, true) end - - - + --- Check if platform was detected + -- @param name string: The platform name to check. + -- @return boolean: true if LuaRocks is currently running on queried platform. function cfg.is_platform(name) assert(type(name) == "string") return platforms[name] end - + -- @param direction (optional) "least-specific-first" (default) or "most-specific-first" function cfg.each_platform(direction) direction = direction or "least-specific-first" local i, delta @@ -965,7 +927,7 @@ function cfg.init(detected, warning) function cfg.print_platforms() local platform_keys = {} - for k, _ in pairs(platforms) do + for k,_ in pairs(platforms) do table.insert(platform_keys, k) end table.sort(platform_keys) diff --git a/src/luarocks/core/cfg.tl b/src/luarocks/core/cfg.tl deleted file mode 100644 index 3613e9dc..00000000 --- a/src/luarocks/core/cfg.tl +++ /dev/null @@ -1,982 +0,0 @@ - ---- Configuration for LuaRocks. --- Tries to load the user's configuration file and --- defines defaults for unset values. See the --- config --- file format documentation for details. --- --- End-users shouldn't edit this file. They can override any defaults --- set in this file using their system-wide or user-specific configuration --- files. Run `luarocks` with no arguments to see the locations of --- these files in your platform. - -local table, pairs, require, os, pcall, ipairs, package, type, assert = --! - table, pairs, require, os, pcall, ipairs, package, type, assert - -local dir = require("luarocks.core.dir") --! -local util = require("luarocks.core.util") -local persist = require("luarocks.core.persist") -local sysdetect = require("luarocks.core.sysdetect") -local vers = require("luarocks.core.vers") - -local record cfg - detect_sysconfdir: function(): string - make_platforms: function(system: string): {any: boolean} - make_defaults: function(lua_version: string, target_cpu: string, platforms: {any: any}, home: string): {any: any} - use_defaults: function(cfg, defaults: {any: any}) - --! -end - --------------------------------------------------------------------------------- - -local program_version = "dev" --! - -local is_windows = package.config:sub(1,1) == "\\" --! - --- Set order for platform overrides. --- More general platform identifiers should be listed first, --- more specific ones later. -local platform_order = { - -- Unixes - "unix", - "bsd", - "solaris", - "netbsd", - "openbsd", - "freebsd", - "dragonfly", - "linux", - "macosx", - "cygwin", - "msys", - "haiku", - -- Windows - "windows", - "win32", - "mingw", - "mingw32", - "msys2_mingw_w64", -} - -local type Config = record --! - home: any - lua_version: any - target_cpu: any - variables: {any: any} - os_getenv: any - rocks_trees: any - rocks_servers: any - dump_env: function() -end - -local function detect_sysconfdir() - if not debug then - return - end - local src = debug.getinfo(1, "S").source - if not src then - return - end - src = dir.normalize(src) - if src:sub(1, 1) == "@" then - src = src:sub(2) - end - local basedir = src:match("^(.*)[\\/]luarocks[\\/]core[\\/]cfg.lua$") - if not basedir then - return - end - -- If installed in a Unix-like tree, use a Unix-like sysconfdir - local installdir = basedir:match("^(.*)[\\/]share[\\/]lua[\\/][^/]*$") - if installdir then - if installdir == "/usr" then - return "/etc/luarocks" --! - end - return dir.path(installdir, "etc", "luarocks") - end - -- Otherwise, use base directory of sources - return basedir -end - --- local load_config_file --! -do - -- Create global environment for the config files; - local function env_for_config_file(cfg: Config, platforms: {any: any}): {any: any} --! - local platforms_copy = {} - for k,v in pairs(platforms) do - platforms_copy[k] = v - end - - local e: {any: any} --! - e = { - home = cfg.home, - lua_version = cfg.lua_version, - platforms = platforms_copy, - processor = cfg.target_cpu, -- remains for compat reasons - target_cpu = cfg.target_cpu, -- replaces `processor` - os_getenv = os.getenv, - variables = cfg.variables or {}, - dump_env = function() - -- debug function, calling it from a config file will show all - -- available globals to that config file - print(util.show_table(e, "global environment")) - end, - } - return e - end - - -- Merge values from config files read into the `cfg` table - local function merge_overrides(cfg: Config, overrides: Config) - -- remove some stuff we do not want to integrate - overrides.os_getenv = nil - overrides.dump_env = nil - -- remove tables to be copied verbatim instead of deeply merged - if overrides.rocks_trees then cfg.rocks_trees = nil end - if overrides.rocks_servers then cfg.rocks_servers = nil end - -- perform actual merge - util.deep_merge(cfg, overrides) - end - - local function update_platforms(platforms: {any: any}, overrides: {any: any}) - if overrides[1] then - for k, _ in pairs(platforms) do - platforms[k] = nil - end - for _, v in ipairs(overrides) do - platforms[v] = true - end - -- set some fallback default in case the user provides an incomplete configuration. - -- LuaRocks expects a set of defaults to be available. - if not (platforms.unix or platforms.windows) then - platforms[is_windows and "windows" or "unix"] = true - end - end - end - - -- Load config file and merge its contents into the `cfg` module table. - -- @return filepath of successfully loaded file or nil if it failed - local load_config_file = function(cfg: Config, platforms: {any: any}, filepath: string): string --! - local result, err, errcode = persist.load_into_table(filepath, env_for_config_file(cfg, platforms)) - if (not result) and errcode ~= "open" then - -- errcode is either "load" or "run"; bad config file, so error out - return nil, err, "config" - end - if result then - -- success in loading and running, merge contents and exit - update_platforms(platforms, result.platforms) - result.platforms = nil - merge_overrides(cfg, result) - return filepath - end - return nil -- nothing was loaded - end -end - -local platform_sets = { - freebsd = { unix = true, bsd = true, freebsd = true }, - openbsd = { unix = true, bsd = true, openbsd = true }, - dragonfly = { unix = true, bsd = true, dragonfly = true }, - solaris = { unix = true, solaris = true }, - windows = { windows = true, win32 = true }, - cygwin = { unix = true, cygwin = true }, - macosx = { unix = true, bsd = true, macosx = true, macos = true }, - netbsd = { unix = true, bsd = true, netbsd = true }, - haiku = { unix = true, haiku = true }, - linux = { unix = true, linux = true }, - mingw = { windows = true, win32 = true, mingw32 = true, mingw = true }, - msys = { unix = true, cygwin = true, msys = true }, - msys2_mingw_w64 = { windows = true, win32 = true, mingw32 = true, mingw = true, msys = true, msys2_mingw_w64 = true }, -} - -local function make_platforms(system: string): {any: boolean} --! :System - -- fallback to Unix in unknown systems - return platform_sets[system] or { unix = true } -end - --------------------------------------------------------------------------------- - -local function make_defaults(lua_version: string, target_cpu: string, platforms: {any: any}, home: string): {any: any} --! - - -- Configure defaults: - local defaults = { - - local_by_default = false, - accept_unknown_fields = false, - fs_use_modules = true, - hooks_enabled = true, - deps_mode = "one", - no_manifest = false, - check_certificates = false, - - cache_timeout = 60, - cache_fail_timeout = 86400, - - lua_modules_path = dir.path("share", "lua", lua_version), - lib_modules_path = dir.path("lib", "lua", lua_version), - rocks_subdir = dir.path("lib", "luarocks", "rocks-"..lua_version), - - arch = "unknown", - lib_extension = "unknown", - external_lib_extension = "unknown", --? - static_lib_extension = "unknown", --? - obj_extension = "unknown", - link_lua_explicitly = false, - - external_deps_dirs = {}, --? - external_deps_patterns = {}, --? - runtime_external_deps_patterns = {}, --? - - export_path_separator = "", --? - wrapper_suffix = "", --? - - - rocks_servers = { - { - "https://luarocks.org", - "https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/", - "https://loadk.com/luarocks/", - } - }, - disabled_servers = {}, - - upload = { - server = "https://luarocks.org", - tool_version = "1.0.0", - api_version = "1", - }, - - lua_extension = "lua", - connection_timeout = 30, -- 0 = no timeout - - makefile = "", --? - local_cache = "" , --? - web_browser = "", --? - cmake_generator = "", --? - gcc_rpath = false, --? - - variables = { - MAKE = os.getenv("MAKE") or "make", - CC = os.getenv("CC") or "cc", - LD = os.getenv("CC") or "ld", - AR = os.getenv("AR") or "ar", - RANLIB = os.getenv("RANLIB") or "ranlib", - - CVS = "cvs", - GIT = "git", - SSCM = "sscm", - SVN = "svn", - HG = "hg", - - GPG = "gpg", - - RSYNC = "rsync", - WGET = "wget", - SCP = "scp", - CURL = "curl", - - PWD = "pwd", - MKDIR = "mkdir", - RMDIR = "rmdir", - CP = "cp", - LN = "ln", - LS = "ls", - RM = "rm", - FIND = "find", - CHMOD = "chmod", - ICACLS = "icacls", - MKTEMP = "mktemp", - - ZIP = "zip", - UNZIP = "unzip -n", - GUNZIP = "gunzip", - BUNZIP2 = "bunzip2", - TAR = "tar", - - MD5SUM = "md5sum", - OPENSSL = "openssl", - MD5 = "md5", - TOUCH = "touch", - - CMAKE = "cmake", - SEVENZ = "7z", - - RSYNCFLAGS = "--exclude=.git -Oavz", - CURLNOCERTFLAG = "", - WGETNOCERTFLAG = "", - - RC = "", --? - MT = "", --? - CFLAGS = "", --? - LDFLAGS = "", --? - LIBFLAG = "", --? - TEST = "", --? - }, - - external_deps_subdirs = { - bin = "bin", - lib = "lib", - include = "include" - }, - runtime_external_deps_subdirs = { - bin = "bin", - lib = "lib", - include = "include" - }, - } - - if platforms.windows then - - defaults.arch = "win32-"..target_cpu - defaults.lib_extension = "dll" - defaults.external_lib_extension = "dll" - defaults.static_lib_extension = "lib" - defaults.obj_extension = "obj" - defaults.external_deps_dirs = { - dir.path("c:", "external"), - dir.path("c:", "windows", "system32"), - } - - defaults.makefile = "Makefile.win" - defaults.variables.PWD = "echo %cd%" - defaults.variables.MKDIR = "md" - defaults.variables.MAKE = os.getenv("MAKE") or "nmake" - defaults.variables.CC = os.getenv("CC") or "cl" - defaults.variables.RC = os.getenv("WINDRES") or "rc" - defaults.variables.LD = os.getenv("LINK") or "link" - defaults.variables.MT = os.getenv("MT") or "mt" - defaults.variables.AR = os.getenv("AR") or "lib" - defaults.variables.CFLAGS = os.getenv("CFLAGS") or "/nologo /MD /O2" - defaults.variables.LDFLAGS = os.getenv("LDFLAGS") - defaults.variables.LIBFLAG = "/nologo /dll" - - defaults.external_deps_patterns = { - bin = { "?.exe", "?.bat" }, - lib = { "?.lib", "lib?.lib", "?.dll", "lib?.dll" }, - include = { "?.h" } - } - defaults.runtime_external_deps_patterns = { - bin = { "?.exe", "?.bat" }, - lib = { "?.dll", "lib?.dll" }, - include = { "?.h" } - } - defaults.export_path_separator = ";" - defaults.wrapper_suffix = ".bat" - - local localappdata = os.getenv("LOCALAPPDATA") - if not localappdata then - -- for Windows versions below Vista - localappdata = dir.path((os.getenv("USERPROFILE") or dir.path("c:", "Users", "All Users")), "Local Settings", "Application Data") - end - defaults.local_cache = dir.path(localappdata, "LuaRocks", "Cache") - defaults.web_browser = "start" - - defaults.external_deps_subdirs.lib = { "lib", "", "bin" } - defaults.runtime_external_deps_subdirs.lib = { "lib", "", "bin" } - defaults.link_lua_explicitly = true - defaults.fs_use_modules = false - end - - if platforms.mingw32 then - defaults.obj_extension = "o" - defaults.static_lib_extension = "a" - defaults.external_deps_dirs = { - dir.path("c:", "external"), - dir.path("c:", "mingw"), - dir.path("c:", "windows", "system32"), - } - defaults.cmake_generator = "MinGW Makefiles" - defaults.variables.MAKE = os.getenv("MAKE") or "mingw32-make" - if target_cpu == "x86_64" then - defaults.variables.CC = os.getenv("CC") or "x86_64-w64-mingw32-gcc" - defaults.variables.LD = os.getenv("CC") or "x86_64-w64-mingw32-gcc" - else - defaults.variables.CC = os.getenv("CC") or "mingw32-gcc" - defaults.variables.LD = os.getenv("CC") or "mingw32-gcc" - end - defaults.variables.AR = os.getenv("AR") or "ar" - defaults.variables.RC = os.getenv("WINDRES") or "windres" - defaults.variables.RANLIB = os.getenv("RANLIB") or "ranlib" - defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2" - defaults.variables.LDFLAGS = os.getenv("LDFLAGS") - defaults.variables.LIBFLAG = "-shared" - defaults.makefile = "Makefile" - defaults.external_deps_patterns = { - bin = { "?.exe", "?.bat" }, - -- mingw lookup list from http://stackoverflow.com/a/15853231/1793220 - -- ...should we keep ?.lib at the end? It's not in the above list. - lib = { "lib?.dll.a", "?.dll.a", "lib?.a", "cyg?.dll", "lib?.dll", "?.dll", "?.lib" }, - include = { "?.h" } - } - defaults.runtime_external_deps_patterns = { - bin = { "?.exe", "?.bat" }, - lib = { "cyg?.dll", "?.dll", "lib?.dll" }, - include = { "?.h" } - } - defaults.link_lua_explicitly = true - end - - if platforms.unix then - defaults.lib_extension = "so" - defaults.static_lib_extension = "a" - defaults.external_lib_extension = "so" - defaults.obj_extension = "o" - defaults.external_deps_dirs = { "/usr/local", "/usr", "/" } - - defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2" - -- we pass -fPIC via CFLAGS because of old Makefile-based Lua projects - -- which didn't have -fPIC in their Makefiles but which honor CFLAGS - if not defaults.variables.CFLAGS:match("-fPIC") then - defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC" - end - - defaults.variables.LDFLAGS = os.getenv("LDFLAGS") - - defaults.cmake_generator = "Unix Makefiles" - defaults.variables.CC = os.getenv("CC") or "gcc" - defaults.variables.LD = os.getenv("CC") or "gcc" - defaults.gcc_rpath = true - defaults.variables.LIBFLAG = "-shared" - defaults.variables.TEST = "test" - - defaults.external_deps_patterns = { - bin = { "?" }, - lib = { "lib?.a", "lib?.so", "lib?.so.*" }, - include = { "?.h" } - } - defaults.runtime_external_deps_patterns = { - bin = { "?" }, - lib = { "lib?.so", "lib?.so.*" }, - include = { "?.h" } - } - defaults.export_path_separator = ":" - defaults.wrapper_suffix = "" - local xdg_cache_home = os.getenv("XDG_CACHE_HOME") or home.."/.cache" - defaults.local_cache = xdg_cache_home.."/luarocks" - defaults.web_browser = "xdg-open" - end - - if platforms.cygwin then - defaults.lib_extension = "so" -- can be overridden in the config file for mingw builds - defaults.arch = "cygwin-"..target_cpu - defaults.cmake_generator = "Unix Makefiles" - defaults.variables.CC = "echo -llua | xargs " .. (os.getenv("CC") or "gcc") - defaults.variables.LD = "echo -llua | xargs " .. (os.getenv("CC") or "gcc") - defaults.variables.LIBFLAG = "-shared" - defaults.link_lua_explicitly = true - end - - if platforms.msys then - -- msys is basically cygwin made out of mingw, meaning the subsytem is unixish - -- enough, yet we can freely mix with native win32 - defaults.external_deps_patterns = { - bin = { "?.exe", "?.bat", "?" }, - lib = { "lib?.so", "lib?.so.*", "lib?.dll.a", "?.dll.a", - "lib?.a", "lib?.dll", "?.dll", "?.lib" }, - include = { "?.h" } - } - defaults.runtime_external_deps_patterns = { - bin = { "?.exe", "?.bat" }, - lib = { "lib?.so", "?.dll", "lib?.dll" }, - include = { "?.h" } - } - if platforms.mingw then - -- MSYS2 can build Windows programs that depend on - -- msys-2.0.dll (based on Cygwin) but MSYS2 is also designed - -- for building native Windows programs by MinGW. These - -- programs don't depend on msys-2.0.dll. - local pipe = io.popen("cygpath --windows %MINGW_PREFIX%") - local mingw_prefix = pipe:read("*l") - pipe:close() - defaults.external_deps_dirs = { - mingw_prefix, - dir.path("c:", "windows", "system32"), - } - defaults.makefile = "Makefile" - defaults.cmake_generator = "MSYS Makefiles" - defaults.local_cache = dir.path(home, ".cache", "luarocks") - defaults.variables.MAKE = os.getenv("MAKE") or "make" - defaults.variables.CC = os.getenv("CC") or "gcc" - defaults.variables.RC = os.getenv("WINDRES") or "windres" - defaults.variables.LD = os.getenv("CC") or "gcc" - defaults.variables.MT = os.getenv("MT") or nil - defaults.variables.AR = os.getenv("AR") or "ar" - defaults.variables.RANLIB = os.getenv("RANLIB") or "ranlib" - - defaults.variables.CFLAGS = os.getenv("CFLAGS") or "-O2 -fPIC" - if not defaults.variables.CFLAGS:match("-fPIC") then - defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC" - end - - defaults.variables.LIBFLAG = "-shared" - end - end - - if platforms.bsd then - defaults.variables.MAKE = "gmake" - defaults.gcc_rpath = false - defaults.variables.CC = os.getenv("CC") or "cc" - defaults.variables.LD = os.getenv("CC") or defaults.variables.CC - end - - if platforms.macosx then - defaults.variables.MAKE = os.getenv("MAKE") or "make" - defaults.external_lib_extension = "dylib" - defaults.arch = "macosx-"..target_cpu - defaults.variables.LIBFLAG = "-bundle -undefined dynamic_lookup -all_load" - local version = util.popen_read("sw_vers -productVersion") - if not (version:match("^%d+%.%d+%.%d+$") or version:match("^%d+%.%d+$")) then - version = "10.3" - end - version = vers.parse_version(version) - if version >= vers.parse_version("11.0") then - version = vers.parse_version("11.0") - elseif version >= vers.parse_version("10.10") then - version = vers.parse_version("10.8") - elseif version >= vers.parse_version("10.5") then - version = vers.parse_version("10.5") - else - defaults.gcc_rpath = false - end - defaults.variables.CC = "env MACOSX_DEPLOYMENT_TARGET="..tostring(version).." gcc" - defaults.variables.LD = "env MACOSX_DEPLOYMENT_TARGET="..tostring(version).." gcc" - defaults.web_browser = "open" - - -- XCode SDK - local sdk_path = util.popen_read("xcrun --show-sdk-path 2>/dev/null") - if sdk_path then - table.insert(defaults.external_deps_dirs, sdk_path .. "/usr") - table.insert(defaults.external_deps_patterns.lib, 1, "lib?.tbd") - table.insert(defaults.runtime_external_deps_patterns.lib, 1, "lib?.tbd") - end - - -- Homebrew - table.insert(defaults.external_deps_dirs, "/usr/local/opt") - defaults.external_deps_subdirs.lib = { "lib", "" } - defaults.runtime_external_deps_subdirs.lib = { "lib", "" } - table.insert(defaults.external_deps_patterns.lib, 1, "/?/lib/lib?.dylib") - table.insert(defaults.runtime_external_deps_patterns.lib, 1, "/?/lib/lib?.dylib") - end - - if platforms.linux then - defaults.arch = "linux-"..target_cpu - - local gcc_arch = util.popen_read("gcc -print-multiarch 2>/dev/null") - if gcc_arch and gcc_arch ~= "" then - defaults.external_deps_subdirs.lib = { "lib/" .. gcc_arch, "lib64", "lib" } - defaults.runtime_external_deps_subdirs.lib = { "lib/" .. gcc_arch, "lib64", "lib" } - else - defaults.external_deps_subdirs.lib = { "lib64", "lib" } - defaults.runtime_external_deps_subdirs.lib = { "lib64", "lib" } - end - end - - if platforms.freebsd then - defaults.arch = "freebsd-"..target_cpu - elseif platforms.dragonfly then - defaults.arch = "dragonfly-"..target_cpu - elseif platforms.openbsd then - defaults.arch = "openbsd-"..target_cpu - elseif platforms.netbsd then - defaults.arch = "netbsd-"..target_cpu - elseif platforms.solaris then - defaults.arch = "solaris-"..target_cpu - defaults.variables.MAKE = "gmake" - end - - -- Expose some more values detected by LuaRocks for use by rockspec authors. - defaults.variables.LIB_EXTENSION = defaults.lib_extension - defaults.variables.OBJ_EXTENSION = defaults.obj_extension - - return defaults -end - -local function use_defaults(cfg, defaults) - - -- Populate variables with values from their 'defaults' counterparts - -- if they were not already set by user. - if not cfg.variables then - cfg.variables = {} - end - for k,v in pairs(defaults.variables) do - if not cfg.variables[k] then - cfg.variables[k] = v - end - end - - util.deep_merge_under(cfg, defaults) - - -- FIXME get rid of this - if not cfg.check_certificates then - cfg.variables.CURLNOCERTFLAG = "-k" - cfg.variables.WGETNOCERTFLAG = "--no-check-certificate" - end -end - --------------------------------------------------------------------------------- - -local cfg = {} - ---- Initializes the LuaRocks configuration for variables, paths --- and OS detection. --- @param detected table containing information detected about the --- environment. All fields below are optional: --- * lua_version (in x.y format, e.g. "5.3") --- * lua_bindir (e.g. "/usr/local/bin") --- * lua_dir (e.g. "/usr/local") --- * lua (e.g. "/usr/local/bin/lua-5.3") --- * project_dir (a string with the path of the project directory --- when using per-project environments, as created with `luarocks init`) --- @param warning a logging function for warnings that takes a string --- @return true on success; nil and an error message on failure. -function cfg.init(detected, warning) - detected = detected or {} - - local exit_ok = true - local exit_err = nil - local exit_what = nil - - local hc_ok, hardcoded = pcall(require, "luarocks.core.hardcoded") - if not hc_ok then - hardcoded = {} - end - - local init = cfg.init - - ---------------------------------------- - -- Reset the cfg table. - ---------------------------------------- - - for k, _ in pairs(cfg) do - cfg[k] = nil - end - - cfg.program_version = program_version - - if hardcoded.IS_BINARY then - cfg.is_binary = true - end - - -- Use detected values as defaults, overridable via config files or CLI args - - local hardcoded_lua = hardcoded.LUA - local hardcoded_lua_dir = hardcoded.LUA_DIR - local hardcoded_lua_bindir = hardcoded.LUA_BINDIR - local hardcoded_lua_incdir = hardcoded.LUA_INCDIR - local hardcoded_lua_libdir = hardcoded.LUA_LIBDIR - local hardcoded_lua_version = hardcoded.LUA_VERSION or _VERSION:sub(5) - - -- if --lua-version or --lua-dir are passed from the CLI, - -- don't use the hardcoded paths at all - if detected.given_lua_version or detected.given_lua_dir then - hardcoded_lua = nil - hardcoded_lua_dir = nil - hardcoded_lua_bindir = nil - hardcoded_lua_incdir = nil - hardcoded_lua_libdir = nil - hardcoded_lua_version = nil - end - - cfg.lua_version = detected.lua_version or hardcoded_lua_version - cfg.project_dir = (not hardcoded.FORCE_CONFIG) and detected.project_dir - - do - local lua = detected.lua or hardcoded_lua - local lua_dir = detected.lua_dir or hardcoded_lua_dir - local lua_bindir = detected.lua_bindir or hardcoded_lua_bindir - cfg.variables = { - LUA = lua, - LUA_DIR = lua_dir, - LUA_BINDIR = lua_bindir, - LUA_LIBDIR = hardcoded_lua_libdir, - LUA_INCDIR = hardcoded_lua_incdir, - } - end - - cfg.init = init - - ---------------------------------------- - -- System detection. - ---------------------------------------- - - -- A proper build of LuaRocks will hardcode the system - -- and proc values with hardcoded.SYSTEM and hardcoded.PROCESSOR. - -- If that is not available, we try to identify the system. - local system, processor = sysdetect.detect() - if hardcoded.SYSTEM then - system = hardcoded.SYSTEM - end - if hardcoded.PROCESSOR then - processor = hardcoded.PROCESSOR - end - - if system == "windows" then - if os.getenv("VCINSTALLDIR") then - -- running from the Development Command prompt for VS 2017 - system = "windows" - else - local msystem = os.getenv("MSYSTEM") - if msystem == nil then - system = "mingw" - elseif msystem == "MSYS" then - system = "msys" - else - -- MINGW32 or MINGW64 - system = "msys2_mingw_w64" - end - end - end - - cfg.target_cpu = processor - - local platforms = make_platforms(system) - - ---------------------------------------- - -- Platform is determined. - -- Let's load the config files. - ---------------------------------------- - - local sys_config_file - local home_config_file - local project_config_file - - local config_file_name = "config-"..cfg.lua_version..".lua" - - do - local sysconfdir = os.getenv("LUAROCKS_SYSCONFDIR") or hardcoded.SYSCONFDIR - if platforms.windows and not platforms.msys2_mingw_w64 then - cfg.home = os.getenv("APPDATA") or "c:" - cfg.home_tree = dir.path(cfg.home, "luarocks") - cfg.sysconfdir = sysconfdir or dir.path((os.getenv("PROGRAMFILES") or "c:"), "luarocks") - else - cfg.home = os.getenv("HOME") or "" - cfg.home_tree = dir.path(cfg.home, ".luarocks") - cfg.sysconfdir = sysconfdir or detect_sysconfdir() or "/etc/luarocks" - end - end - - -- Load system configuration file - sys_config_file = dir.path(cfg.sysconfdir, config_file_name) - local sys_config_ok, err = load_config_file(cfg, platforms, sys_config_file) - if err then - exit_ok, exit_err, exit_what = nil, err, "config" - end - - -- Load user configuration file (if allowed) - local home_config_ok - local project_config_ok - if not hardcoded.FORCE_CONFIG then - local env_var = "LUAROCKS_CONFIG_" .. cfg.lua_version:gsub("%.", "_") - local env_value = os.getenv(env_var) - if not env_value then - env_var = "LUAROCKS_CONFIG" - env_value = os.getenv(env_var) - end - -- first try environment provided file, so we can explicitly warn when it is missing - if env_value then - local env_ok, err = load_config_file(cfg, platforms, env_value) - if err then - exit_ok, exit_err, exit_what = nil, err, "config" - elseif warning and not env_ok then - warning("Warning: could not load configuration file `"..env_value.."` given in environment variable "..env_var.."\n") - end - if env_ok then - home_config_ok = true - home_config_file = env_value - end - end - - -- try XDG config home - if platforms.unix and not home_config_ok then - local xdg_config_home = os.getenv("XDG_CONFIG_HOME") or dir.path(cfg.home, ".config") - cfg.homeconfdir = dir.path(xdg_config_home, "luarocks") - home_config_file = dir.path(cfg.homeconfdir, config_file_name) - home_config_ok, err = load_config_file(cfg, platforms, home_config_file) - if err then - exit_ok, exit_err, exit_what = nil, err, "config" - end - end - - -- try the alternative defaults if there was no environment specified file or it didn't work - if not home_config_ok then - cfg.homeconfdir = cfg.home_tree - home_config_file = dir.path(cfg.homeconfdir, config_file_name) - home_config_ok, err = load_config_file(cfg, platforms, home_config_file) - if err then - exit_ok, exit_err, exit_what = nil, err, "config" - end - end - - -- finally, use the project-specific config file if any - if cfg.project_dir then - project_config_file = dir.path(cfg.project_dir, ".luarocks", config_file_name) - project_config_ok, err = load_config_file(cfg, platforms, project_config_file) - if err then - exit_ok, exit_err, exit_what = nil, err, "config" - end - end - end - - -- backwards compatibility: - if cfg.lua_interpreter and cfg.variables.LUA_BINDIR and not cfg.variables.LUA then - cfg.variables.LUA = dir.path(cfg.variables.LUA_BINDIR, cfg.lua_interpreter) - end - - ---------------------------------------- - -- Config files are loaded. - -- Let's finish up the cfg table. - ---------------------------------------- - - -- Settings given via the CLI (i.e. --lua-dir) take precedence over config files. - cfg.project_dir = detected.given_project_dir or cfg.project_dir - cfg.lua_version = detected.given_lua_version or cfg.lua_version - if detected.given_lua_dir then - cfg.variables.LUA = detected.lua - cfg.variables.LUA_DIR = detected.given_lua_dir - cfg.variables.LUA_BINDIR = detected.lua_bindir - cfg.variables.LUA_LIBDIR = nil - cfg.variables.LUA_INCDIR = nil - end - - -- Build a default list of rocks trees if not given - if cfg.rocks_trees == nil then - cfg.rocks_trees = {} - if cfg.home_tree then - table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree } ) - end - if hardcoded.PREFIX and hardcoded.PREFIX ~= cfg.home_tree then - table.insert(cfg.rocks_trees, { name = "system", root = hardcoded.PREFIX } ) - end - end - - local defaults = make_defaults(cfg.lua_version, processor, platforms, cfg.home) - - if platforms.windows and hardcoded.WIN_TOOLS then - local tools = { "SEVENZ", "CP", "FIND", "LS", "MD5SUM", "WGET", } - for _, tool in ipairs(tools) do - defaults.variables[tool] = '"' .. dir.path(hardcoded.WIN_TOOLS, defaults.variables[tool] .. '.exe') .. '"' - end - else - defaults.fs_use_modules = true - end - - -- if only cfg.variables.LUA is given in config files, - -- derive LUA_BINDIR and LUA_DIR from them. - if cfg.variables.LUA and not cfg.variables.LUA_BINDIR then - cfg.variables.LUA_BINDIR = cfg.variables.LUA:match("^(.*)[\\/][^\\/]*$") - if not cfg.variables.LUA_DIR then - cfg.variables.LUA_DIR = cfg.variables.LUA_BINDIR:gsub("[\\/]bin$", "") or cfg.variables.LUA_BINDIR - end - end - - use_defaults(cfg, defaults) - - cfg.user_agent = "LuaRocks/"..cfg.program_version.." "..cfg.arch - - cfg.config_files = { - project = cfg.project_dir and { - file = project_config_file, - found = not not project_config_ok, - }, - system = { - file = sys_config_file, - found = not not sys_config_ok, - }, - user = { - file = home_config_file, - found = not not home_config_ok, - }, - nearest = project_config_ok - and project_config_file - or (home_config_ok - and home_config_file - or sys_config_file), - } - - cfg.cache = {} - - ---------------------------------------- - -- Attributes of cfg are set. - -- Let's add some methods. - ---------------------------------------- - - do - local function make_paths_from_tree(tree) - local lua_path, lib_path, bin_path - if type(tree) == "string" then - lua_path = dir.path(tree, cfg.lua_modules_path) - lib_path = dir.path(tree, cfg.lib_modules_path) - bin_path = dir.path(tree, "bin") - else - lua_path = tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path) - lib_path = tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path) - bin_path = tree.bin_dir or dir.path(tree.root, "bin") - end - return lua_path, lib_path, bin_path - end - - function cfg.package_paths(current) - local new_path, new_cpath, new_bin = {}, {}, {} - local function add_tree_to_paths(tree) - local lua_path, lib_path, bin_path = make_paths_from_tree(tree) - table.insert(new_path, dir.path(lua_path, "?.lua")) - table.insert(new_path, dir.path(lua_path, "?", "init.lua")) - table.insert(new_cpath, dir.path(lib_path, "?."..cfg.lib_extension)) - table.insert(new_bin, bin_path) - end - if current then - add_tree_to_paths(current) - end - for _,tree in ipairs(cfg.rocks_trees) do - add_tree_to_paths(tree) - end - return table.concat(new_path, ";"), table.concat(new_cpath, ";"), table.concat(new_bin, cfg.export_path_separator) - end - end - - function cfg.init_package_paths() - local lr_path, lr_cpath, lr_bin = cfg.package_paths() - package.path = util.cleanup_path(package.path .. ";" .. lr_path, ";", cfg.lua_version, true) - package.cpath = util.cleanup_path(package.cpath .. ";" .. lr_cpath, ";", cfg.lua_version, true) - end - - --- Check if platform was detected - -- @param name string: The platform name to check. - -- @return boolean: true if LuaRocks is currently running on queried platform. - function cfg.is_platform(name) - assert(type(name) == "string") - return platforms[name] - end - - -- @param direction (optional) "least-specific-first" (default) or "most-specific-first" - function cfg.each_platform(direction) - direction = direction or "least-specific-first" - local i, delta - if direction == "least-specific-first" then - i = 0 - delta = 1 - else - i = #platform_order + 1 - delta = -1 - end - return function() - local p - repeat - i = i + delta - p = platform_order[i] - until (not p) or platforms[p] - return p - end - end - - function cfg.print_platforms() - local platform_keys = {} - for k,_ in pairs(platforms) do - table.insert(platform_keys, k) - end - table.sort(platform_keys) - return table.concat(platform_keys, ", ") - end - - return exit_ok, exit_err, exit_what -end - -return cfg diff --git a/src/luarocks/core/persist.lua b/src/luarocks/core/persist.lua index 1435bd54..258a42c0 100644 --- a/src/luarocks/core/persist.lua +++ b/src/luarocks/core/persist.lua @@ -1,4 +1,4 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local io = _tl_compat and _tl_compat.io or io; local load = _tl_compat and _tl_compat.load or load; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local io = _tl_compat and _tl_compat.io or io; local load = _tl_compat and _tl_compat.load or load; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string local persist = {} @@ -8,6 +8,8 @@ local persist = {} + + function persist.run_file(filename, env) local fd, open_err = io.open(filename) if not fd then @@ -20,17 +22,9 @@ function persist.run_file(filename, env) end str = str:gsub("^#![^\n]*\n", "") local chunk, ran, err - if _VERSION == "Lua 5.1" then - chunk, err = loadstring(str, filename) - if chunk then - setfenv(chunk, env) - ran, err = pcall(chunk) - end - else - chunk, err = load(str, filename, "t", env) - if chunk then - ran, err = pcall(chunk) - end + chunk, err = load(str, filename, "t", env) + if chunk then + ran, err = pcall(chunk) end if not chunk then return nil, "Error loading file: " .. tostring(err), "load" @@ -52,8 +46,6 @@ end function persist.load_into_table(filename, tbl) - assert(type(filename) == "string") - assert(type(tbl) == "table" or not tbl) local result = tbl or {} local globals = {} @@ -70,7 +62,7 @@ function persist.load_into_table(filename, tbl) setmetatable(result, save_mt) if not ok then - return nil, err, errcode + return nil, tostring(err), errcode end return result, globals end diff --git a/src/luarocks/core/persist.tl b/src/luarocks/core/persist.tl index 26eb5dff..f89a95c1 100644 --- a/src/luarocks/core/persist.tl +++ b/src/luarocks/core/persist.tl @@ -1,10 +1,7 @@ local record persist - run_file: function(filename: string, env: {string:any}): boolean, any | nil, string, string - load_into_table: function(filename: string, tbl: {string:any}) : table, table | nil, string, string end -local require = nil --! -------------------------------------------------------------------------------- --- Load and run a Lua file in an environment. @@ -13,7 +10,7 @@ local require = nil --! -- @return (true, any) or (nil, string, string): true and the return value -- of the file, or nil, an error message and an error code ("open", "load" -- or "run") in case of errors. -function persist.run_file(filename: string, env: {string:any}): boolean, any | nil, string, string +function persist.run_file(filename: string, env: {string:any}): boolean, any | string, string local fd, open_err: FILE, any = io.open(filename) if not fd then return nil, open_err, "open" @@ -25,23 +22,15 @@ function persist.run_file(filename: string, env: {string:any}): boolean, any | n end str = str:gsub("^#![^\n]*\n", "") local chunk, ran, err: function(any):(any), boolean, any - if _VERSION == "Lua 5.1" then -- Lua 5.1 - chunk, err = loadstring(str, filename) --! - if chunk then - setfenv(chunk, env) --! - ran, err = pcall(chunk) - end - else -- Lua 5.2 - chunk, err = load(str, filename, "t", env) - if chunk then - ran, err = pcall(chunk) - end + chunk, err = load(str, filename, "t", env) + if chunk then + ran, err = pcall(chunk) end if not chunk then - return nil, "Error loading file: "..tostring(err), "load" --? tostring + return nil, "Error loading file: "..tostring(err), "load" end if not ran then - return nil, "Error running file: "..tostring(err), "run" --? tostring + return nil, "Error running file: "..tostring(err), "run" end return true, err end @@ -56,12 +45,10 @@ end -- or nil, an error message and an error code ("open"; couldn't open the file, -- "load"; compile-time error, or "run"; run-time error) -- in case of errors. -function persist.load_into_table(filename: string, tbl: {string:any}) : table, table | nil, string, string - assert(type(filename) == "string") --? needed - assert(type(tbl) == "table" or not tbl) --? needed +function persist.load_into_table(filename: string, tbl: {string:any}) : table, table | string, string local result: {string:any} = tbl or {} - local globals: table = {} --? {string:boolean} + local globals = {} local globals_mt = { __index = function(_, k: string) globals[k] = true @@ -70,12 +57,12 @@ function persist.load_into_table(filename: string, tbl: {string:any}) : table, t local save_mt = getmetatable(result) setmetatable(result, globals_mt) - local ok, err, errcode = persist.run_file(filename, result) --? boolean, string, string + local ok, err, errcode = persist.run_file(filename, result) setmetatable(result, save_mt) if not ok then - return nil, err, errcode --! table | nil???? + return nil, tostring(err), errcode end return result, globals end diff --git a/src/luarocks/core/sysdetect.tl b/src/luarocks/core/sysdetect.tl index e783a012..a19a5f24 100644 --- a/src/luarocks/core/sysdetect.tl +++ b/src/luarocks/core/sysdetect.tl @@ -46,26 +46,12 @@ local record sysdetect "cygwin" "macosx" end - - hex: function(s: string): string --? - read_int8: function(fd: FILE): integer - bytes2number: function(s: string, endian: Endian): integer - read: function(fd: FILE, bytes: integer, endian: Endian): integer - read_int32le: function(fd: FILE): integer - read_elf_section_headers: function(fd: FILE, hdr: ElfHeader): {string: ElfSection} - detect_elf_system: function(fd: FILE, hdr: ElfHeader, sections: {string:ElfSection}): System - read_elf_header: function(fd: FILE): ElfHeader, Processor - detect_elf: function(fd: FILE): System, Processor - detect_mach: function(magic: string, fd: FILE): System, Processor - detect_pe: function(fd: FILE): System, Processor - detect_file: function(file: string): System, Processor --? systemdetect.detect_file? - detect: function(input_file: string): System, Processor end -local type System = sysdetect.System --? ? -local type Processor = sysdetect.Processor --? ? +local type System = sysdetect.System +local type Processor = sysdetect.Processor -local enum Endian --? in the registry and a line for type ? +local enum Endian "little" "big" end @@ -270,7 +256,7 @@ local function detect_elf_system(fd: FILE, hdr: ElfHeader, sections: {string:Elf local idx = 0 for _ = 0, gnu_version_r.sh_info - 1 do fd:seek("set", gnu_version_r.sh_offset + idx) - assert(read(fd, 2, endian)) -- vn_version --? + assert(read(fd, 2, endian)) -- vn_version local vn_cnt = read(fd, 2, endian) local vn_file = read(fd, 4, endian) local vn_next = read(fd, 2, endian) diff --git a/src/luarocks/core/util.lua b/src/luarocks/core/util.lua index 44c9e63b..f8c7ac71 100644 --- a/src/luarocks/core/util.lua +++ b/src/luarocks/core/util.lua @@ -1,7 +1,7 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local debug = _tl_compat and _tl_compat.debug or debug; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local math = _tl_compat and _tl_compat.math or math; local os = _tl_compat and _tl_compat.os or os; local package = _tl_compat and _tl_compat.package or package; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local debug = _tl_compat and _tl_compat.debug or debug; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local math = _tl_compat and _tl_compat.math or math; local os = _tl_compat and _tl_compat.os or os; local package = _tl_compat and _tl_compat.package or package; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table local util = {} -local require = nil + local dir_sep = package.config:sub(1, 1) @@ -80,7 +80,7 @@ function util.show_table(t, tname, top_indent) cart = cart .. indent .. field - if type(value) ~= "table" then + if not (type(value) == "table") then cart = cart .. " = " .. basic_serialize(value) .. ";\n" else if saved[value] then @@ -156,6 +156,34 @@ end +function util.split_string(str, delim, maxNb) + + if string.find(str, delim) == nil then + return { str } + end + if maxNb == nil or maxNb < 1 then + maxNb = 0 + end + local result = {} + local pat = "(.-)" .. delim .. "()" + local nb = 0 + local lastPos + for part, pos in string.gmatch(str, pat) do + nb = nb + 1 + result[nb] = part + lastPos = tonumber(pos) + if nb == maxNb then break end + end + + if nb ~= maxNb then + result[nb + 1] = string.sub(str, lastPos) + end + return result +end + + + + @@ -164,8 +192,6 @@ end function util.cleanup_path(list, sep, lua_version, keep_first) - assert(type(list) == "string") - assert(type(sep) == "string") list = list:gsub(dir_sep, "/") @@ -200,33 +226,6 @@ end -function util.split_string(str, delim, maxNb) - - if string.find(str, delim) == nil then - return { str } - end - if maxNb == nil or maxNb < 1 then - maxNb = 0 - end - local result = {} - local pat = "(.-)" .. delim .. "()" - local nb = 0 - local lastPos - for part, pos in string.gmatch(str, pat) do - nb = nb + 1 - result[nb] = part - lastPos = pos - if nb == maxNb then break end - end - - if nb ~= maxNb then - result[nb + 1] = string.sub(str, lastPos) - end - return result -end - - - function util.keys(tbl) local ks = {} @@ -278,7 +277,7 @@ function util.sortedpairs(tbl, sort_function) local keys = util.keys(tbl) local sub_orders = {} - if type(sort_function) == "function" then + if (type(sort_function) == "function") and (type(sort_function) == "function") then table.sort(keys, sort_function) else local order = sort_function diff --git a/src/luarocks/core/util.tl b/src/luarocks/core/util.tl index c7f02de2..738bb8a8 100644 --- a/src/luarocks/core/util.tl +++ b/src/luarocks/core/util.tl @@ -1,18 +1,7 @@ local record util - popen_read: function(cmd: string, spec: string): string --? util.popen_read - show_table: function(t: {any:any}, tname: string, top_indent: string): string - deep_merge: function(dst: {integer: any}, src: {integer: any}) - deep_merge_under: function(dst: {integer: any}, src: {integer: any}) - cleanup_path: function(list: string, sep: string, lua_version: string, keep_first: boolean): string - split_string: function(str: string, delim: string, maxNb: number): {string} - keys: function(tbl: {any:any}): {any} - printerr: function(...: string | number) - warning: function(msg: string) - sortedpairs: function(tbl: {any: any}, sort_function: function(any, any): boolean | {any:any} | nil): function end -local require = nil --! -------------------------------------------------------------------------------- local dir_sep = package.config:sub(1, 1) @@ -84,14 +73,14 @@ function util.show_table(t: {any:any}, tname: string, top_indent: string): strin end end - local function add_to_cart (value: {any:any}, name: string, indent: string, saved: {any: string}, field: string) --? value: {A:B} + local function add_to_cart (value: any | {any:any}, name: string, indent: string, saved: {any: string}, field: string) indent = indent or "" saved = saved or {} field = field or name cart = cart .. indent .. field - if type(value) ~= "table" then --? 1 + if not value is {any: any} then cart = cart .. " = " .. basic_serialize(value) .. ";\n" else if saved[value] then @@ -99,7 +88,7 @@ function util.show_table(t: {any:any}, tname: string, top_indent: string): strin autoref = autoref .. name .. " = " .. saved[value] .. ";\n" else saved[value] = name - if is_empty_table(value) then --? 1 + if is_empty_table(value) then cart = cart .. " = {};\n" else cart = cart .. " = {\n" @@ -108,7 +97,7 @@ function util.show_table(t: {any:any}, tname: string, top_indent: string): strin local fname = ("%s[%s]"):format(name, k) field = ("[%s]"):format(k) -- three spaces between levels - add_to_cart(v, fname, indent .. " ", saved, field) --! + add_to_cart(v, fname, indent .. " ", saved, field) end cart = cart .. indent .. "};\n" end @@ -129,14 +118,14 @@ end -- (i.e. if an key from src already exists in dst, replace it). -- @param dst Destination table, which will receive src's contents. -- @param src Table which provides new contents to dst. -function util.deep_merge(dst: {integer: any}, src: {integer: any}) --? {integer: {any:any}}, {integer: {any:any}} +function util.deep_merge(dst: {any: any}, src: {any: any}) for k, v in pairs(src) do - if type(v) == "table" then + if v is table then if dst[k] == nil then dst[k] = {} end - if type(dst[k]) == "table" then - util.deep_merge(dst[k], v) --! + if dst[k] is {any: any} then --! "is" unavailable for table + util.deep_merge(dst[k], v) else dst[k] = v end @@ -150,13 +139,13 @@ end -- (i.e. if an key from src already exists in dst, do not replace it). -- @param dst Destination table, which will receive src's contents. -- @param src Table which provides new contents to dst. -function util.deep_merge_under(dst: {integer: any}, src: {integer: any}) --? {integer: {any:any}}, {integer: {any:any}} +function util.deep_merge_under(dst: {any: any}, src: {any: any}) for k, v in pairs(src) do if type(v) == "table" then if dst[k] == nil then dst[k] = {} end - if type(dst[k]) == "table" then + if dst[k] is {any: any} then --! "is" unavailable for table util.deep_merge_under(dst[k], v) end elseif dst[k] == nil then @@ -165,6 +154,34 @@ function util.deep_merge_under(dst: {integer: any}, src: {integer: any}) --? {in end end +-- from http://lua-users.org/wiki/SplitJoin +-- by Philippe Lhoste +function util.split_string(str: string, delim: string, maxNb: number): {string} + -- Eliminate bad cases... + if string.find(str, delim) == nil then + return { str } + end + if maxNb == nil or maxNb < 1 then + maxNb = 0 -- No limit + end + local result = {} + local pat = "(.-)" .. delim .. "()" + local nb = 0 + local lastPos: number + for part, pos in string.gmatch(str, pat) do + nb = nb + 1 + result[nb] = part + lastPos = tonumber(pos) + if nb == maxNb then break end + end + -- Handle the last field + if nb ~= maxNb then + result[nb + 1] = string.sub(str, lastPos) + end + return result +end + + --- Clean up a path-style string ($PATH, $LUA_PATH/package.path, etc.), -- removing repeated entries and making sure only the relevant -- Lua version is used. @@ -175,14 +192,12 @@ end -- @param keep_first (optional) if true, keep first occurrence in case -- of duplicates; otherwise keep last occurrence. The default is false. function util.cleanup_path(list: string, sep: string, lua_version: string, keep_first: boolean): string - assert(type(list) == "string") --? - assert(type(sep) == "string") --? list = list:gsub(dir_sep, "/") - local parts = util.split_string(list, sep) --! + local parts = util.split_string(list, sep) local final, entries = {}, {} - local start, stop, step: number, number, number + local start, stop, step: integer, integer, integer if keep_first then start, stop, step = 1, #parts, 1 @@ -193,9 +208,9 @@ function util.cleanup_path(list: string, sep: string, lua_version: string, keep_ for i = start, stop, step do local part = parts[i]:gsub("//", "/") if lua_version then - part = part:gsub("/lua/([%d.]+)/", function(part_version) - if part_version:sub(1, #lua_version) ~= lua_version then --! - return "/lua/"..lua_version.."/" --! + part = part:gsub("/lua/([%d.]+)/", function(part_version: string): string + if part_version:sub(1, #lua_version) ~= lua_version then + return "/lua/"..lua_version.."/" end end) end @@ -206,34 +221,7 @@ function util.cleanup_path(list: string, sep: string, lua_version: string, keep_ end end - return (table.concat(final, sep):gsub("/", dir_sep)) --! -end - --- from http://lua-users.org/wiki/SplitJoin --- by Philippe Lhoste -function util.split_string(str: string, delim: string, maxNb: number): {string} - -- Eliminate bad cases... - if string.find(str, delim) == nil then - return { str } - end - if maxNb == nil or maxNb < 1 then - maxNb = 0 -- No limit - end - local result = {} - local pat = "(.-)" .. delim .. "()" - local nb = 0 - local lastPos: any - for part, pos in string.gmatch(str, pat) do --! pos: number or string - nb = nb + 1 - result[nb] = part - lastPos = pos --! number or string - if nb == maxNb then break end - end - -- Handle the last field - if nb ~= maxNb then - result[nb + 1] = string.sub(str, lastPos) - end - return result + return (table.concat(final, sep):gsub("/", dir_sep)) end --- Return an array of keys of a table. @@ -284,24 +272,24 @@ end -- for that key, which is returned by the iterator as the third value after the key -- and the value. -- @return function: the iterator function. -function util.sortedpairs(tbl: {any: any}, sort_function: function(any, any): boolean | {any:any} | nil): function --? function(A, A): boolean | {any:integer} | nil +function util.sortedpairs(tbl: {any: any}, sort_function: function | {any} | nil): function(): any, any, any --! (function(any, any): boolean) | {any} | nil NOT function(any, any): (boolean | {any:any} | nil) sort_function = sort_function or default_sort local keys = util.keys(tbl) local sub_orders = {} - if type(sort_function) == "function" then - table.sort(keys, sort_function) --! + if (sort_function is function ) and (sort_function is function():boolean) then --? How to enforce boolean output? + table.sort(keys, sort_function) else local order = sort_function local ordered_keys = {} local all_keys = keys keys = {} - for _, order_entry in ipairs(order) do --! - local key, sub_order: any, any --! - if type(order_entry) == "table" then - key = order_entry[1] --! - sub_order = order_entry[2] --! + for _, order_entry in ipairs(order) do + local key, sub_order: any, any + if order_entry is {any: any} then + key = order_entry[1] + sub_order = order_entry[2] else key = order_entry end @@ -322,10 +310,10 @@ function util.sortedpairs(tbl: {any: any}, sort_function: function(any, any end local i = 1 - return function() + return function(): any, any, any local key = keys[i] i = i + 1 - return key, tbl[key], sub_orders[key] --! + return key, tbl[key], sub_orders[key] end end -- cgit v1.2.3-55-g6feb