Unnamed: 0
int64 0
0
| repo_id
stringlengths 5
186
| file_path
stringlengths 15
223
| content
stringlengths 1
32.8M
⌀ |
---|---|---|---|
0 | repos/xmake/xmake/languages | repos/xmake/xmake/languages/msrc/xmake.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file xmake.lua
--
language("msrc")
add_rules("win.sdk.resource")
set_sourcekinds {mrc = {".rc", ".rc2"}}
set_sourceflags {mrc = "mrcflags"}
set_langkinds {msrc = "mrc"}
set_mixingkinds("mrc")
on_load("load")
set_nameflags {
object = {
"config.includedirs"
, "target.defines"
, "target.undefines"
, "target.includedirs"
, "toolchain.includedirs"
, "toolchain.defines"
, "toolchain.undefines"
, "target.sysincludedirs"
, "toolchain.sysincludedirs"
}
}
set_menu {
config =
{
{category = "Cross Complation Configuration/Compiler Configuration" }
, {nil, "mrc", "kv", nil, "The Microsoft Resource Compiler" }
, {category = "Cross Complation Configuration/Compiler Flags Configuration" }
, {nil, "mrcflags", "kv", nil, "The Microsoft Resource Flags" }
, {category = "Cross Complation Configuration/Builti Flags Configuration" }
, {nil, "includedirs","kv", nil, "The Include Search Directories" }
}
}
|
0 | repos/xmake/xmake | repos/xmake/xmake/core/main.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file main.lua
--
-- define module: main
local main = main or {}
-- load modules
local env = require("base/compat/env")
local os = require("base/os")
local log = require("base/log")
local path = require("base/path")
local utils = require("base/utils")
local option = require("base/option")
local table = require("base/table")
local global = require("base/global")
local privilege = require("base/privilege")
local task = require("base/task")
local colors = require("base/colors")
local process = require("base/process")
local scheduler = require("base/scheduler")
local theme = require("theme/theme")
local config = require("project/config")
local project = require("project/project")
local localcache = require("cache/localcache")
local profiler = require("base/profiler")
local debugger = require("base/debugger")
-- init the option menu
local menu =
{
title = "${bright}xmake v" .. _VERSION .. ", A cross-platform build utility based on " .. (xmake._LUAJIT and "LuaJIT" or "Lua") .. "${clear}"
, copyright = "Copyright (C) 2015-present Ruki Wang, ${underline}tboox.org${clear}, ${underline}xmake.io${clear}"
-- the tasks: xmake [task]
, function ()
local tasks = task.tasks() or {}
local ok, project_tasks = pcall(project.tasks)
if ok then
table.join2(tasks, project_tasks)
end
return task.menu(tasks)
end
}
-- show help and version info
function main._show_help()
if option.get("help") then
option.show_menu(option.taskname())
return true
elseif option.get("version") and not option.taskname() then
if menu.title then
utils.cprint(menu.title)
end
if menu.copyright then
utils.cprint(menu.copyright)
end
option.show_logo()
return true
end
end
-- find the root project file
function main._find_root(projectfile)
-- make all parent directories
local dirs = {}
local dir = path.directory(projectfile)
while os.isdir(dir) do
table.insert(dirs, 1, dir)
local parentdir = path.directory(dir)
if parentdir and parentdir ~= dir and parentdir ~= '.' then
dir = parentdir
else
break
end
end
-- find the first `xmake.lua` from it's parent directory
for _, dir in ipairs(dirs) do
local file = path.join(dir, "xmake.lua")
if os.isfile(file) then
return file
end
end
return projectfile
end
-- get project directory and project file from the argument option
--
-- @note we need to put `-P` in the first argument avoid option.parse() parsing errors
-- e.g. `xmake f -c -P xxx` will be parsed as `-c=-P`, it's incorrect.
--
-- @see https://github.com/xmake-io/xmake/issues/4857
--
function main._basicparse()
-- check command
if xmake._ARGV[1] and not xmake._ARGV[1]:startswith('-') then
-- regard it as command name
xmake._COMMAND = xmake._ARGV[1]
xmake._COMMAND_ARGV = table.move(xmake._ARGV, 2, #xmake._ARGV, 1, table.new(#xmake._ARGV - 1, 0))
else
xmake._COMMAND_ARGV = xmake._ARGV
end
-- parse options, only parse -P xxx/-F xxx/--project=xxx/--file=xxx
local options = {}
local argv = xmake._COMMAND_ARGV
local idx = 1
while idx <= #argv do
local arg = argv[idx]
if arg == "-P" and idx < #argv then
options.project = argv[idx + 1]
idx = idx + 1
elseif arg == "-F" and idx < #argv then
options.file = argv[idx + 1]
idx = idx + 1
elseif arg:startswith("--project=") then
options.project = arg:sub(11)
elseif arg:startswith("--file=") then
options.file = arg:sub(8)
end
idx = idx + 1
if options.project and options.file then
break
end
end
return options
end
-- get the project configuration from cache if we are in the independent working directory
-- @see https://github.com/xmake-io/xmake/issues/3342
--
function main._projectconf(name)
local rootdir = os.getenv("XMAKE_CONFIGDIR")
-- we switch to independent working directory
-- @see https://github.com/xmake-io/xmake/issues/820
if not rootdir and os.isdir(path.join(os.workingdir(), "." .. xmake._NAME)) then
rootdir = os.workingdir()
end
local cachefile = path.join(rootdir, "." .. xmake._NAME, os.host(), os.arch(), "cache", "project")
if os.isfile(cachefile) then
local cacheinfo = io.load(cachefile)
if cacheinfo then
return cacheinfo[name]
end
end
end
-- the init function for main
function main._init()
-- start debugger
if debugger:enabled() then
local ok, errors = debugger:start()
if not ok then
return false, errors
end
end
-- disable scheduler first
scheduler:enable(false)
-- get project directory and project file from the argument option
local options, errors = main._basicparse()
if not options then
return false, errors
end
-- init project paths only for xmake engine
if xmake._NAME == "xmake" then
local opt_projectdir, opt_projectfile = options.project, options.file
-- init the project directory
local projectdir = opt_projectdir or main._projectconf("projectdir") or xmake._PROJECT_DIR
if projectdir and not path.is_absolute(projectdir) then
projectdir = path.absolute(projectdir)
elseif projectdir then
projectdir = path.translate(projectdir)
end
xmake._PROJECT_DIR = projectdir
assert(projectdir)
-- init the xmake.lua file path
local projectfile = opt_projectfile or main._projectconf("projectfile") or xmake._PROJECT_FILE
if projectfile and not path.is_absolute(projectfile) then
projectfile = path.absolute(projectfile, projectdir)
end
xmake._PROJECT_FILE = projectfile
assert(projectfile)
-- find the root project file
if not os.isfile(projectfile) or (not opt_projectdir and not opt_projectfile) then
projectfile = main._find_root(projectfile)
end
-- update and enter project
xmake._PROJECT_DIR = path.directory(projectfile)
xmake._PROJECT_FILE = projectfile
-- enter the project directory
if os.isdir(os.projectdir()) then
os.cd(os.projectdir())
end
else
-- patch a fake project file and directory for other lua programs with xmake/engine
xmake._PROJECT_DIR = path.join(os.tmpdir(), "local")
xmake._PROJECT_FILE = path.join(xmake._PROJECT_DIR, xmake._NAME .. ".lua")
end
return true
end
-- exit main program
function main._exit(ok, errors)
-- run all exit callbacks
os._run_exit_cbs(ok, errors)
-- show errors
local retval = 0
if not ok then
retval = -1
if errors then
utils.error(errors)
end
end
-- show warnings
utils.show_warnings()
-- close log
log:close()
-- return exit code
return retval
end
-- limit root? @see https://github.com/xmake-io/xmake/pull/4513
function main._limit_root()
return not option.get("root") and os.getenv("XMAKE_ROOT") ~= 'y' and os.host() ~= 'haiku'
end
-- the main entry function
function main.entry()
-- init
local ok, errors = main._init()
if not ok then
return main._exit(ok, errors)
end
-- load global configuration
ok, errors = global.load()
if not ok then
return main._exit(ok, errors)
end
-- load theme
local theme_inst = theme.load(os.getenv("XMAKE_THEME") or global.get("theme")) or theme.load("default")
if theme_inst then
colors.theme_set(theme_inst)
end
-- init option
ok, errors = option.init(menu)
if not ok then
return main._exit(ok, errors)
end
-- check run command as root
if main._limit_root() then
if os.isroot() then
errors = [[Running xmake as root is extremely dangerous and no longer supported.
As xmake does not drop privileges on installation you would be giving all
build scripts full access to your system.
Or you can add `--root` option or XMAKE_ROOT=y to allow run as root temporarily.
]]
return main._exit(false, errors)
end
end
-- show help?
if main._show_help() then
return main._exit(true)
end
-- save command lines to history and we need to make sure that the .xmake directory is not generated everywhere
local skip_history = (os.getenv('XMAKE_SKIP_HISTORY') or ''):trim()
if os.projectfile() and os.isfile(os.projectfile()) and os.isdir(config.directory()) and skip_history == '' then
local cmdlines = table.wrap(localcache.get("history", "cmdlines"))
if #cmdlines > 64 then
table.remove(cmdlines, 1)
end
table.insert(cmdlines, option.cmdline())
localcache.set("history", "cmdlines", cmdlines)
localcache.save("history")
end
-- get task instance
local taskname = option.taskname() or "build"
local taskinst = task.task(taskname) or project.task(taskname)
if not taskinst then
return main._exit(false, string.format("do unknown task(%s)!", taskname))
end
-- run task
scheduler:enable(true)
scheduler:co_start_named("xmake " .. taskname, function ()
local ok, errors = taskinst:run()
if not ok then
os.raise(errors)
end
end)
ok, errors = scheduler:runloop()
if not ok then
return main._exit(ok, errors)
end
-- stop profiling
if profiler:enabled() then
profiler:stop()
end
-- exit normally
return main._exit(true)
end
-- return module: main
return main
|
0 | repos/xmake/xmake | repos/xmake/xmake/core/_xmake_main.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file _xmake_main.lua
--
-- init namespace: xmake
xmake = xmake or {}
xmake._NAME = _NAME or "xmake"
xmake._ARGV = _ARGV
xmake._HOST = _HOST
xmake._ARCH = _ARCH
xmake._SUBHOST = _SUBHOST
xmake._SUBARCH = _SUBARCH
xmake._XMAKE_ARCH = _XMAKE_ARCH
xmake._VERSION = _VERSION
xmake._VERSION_SHORT = _VERSION_SHORT
xmake._PROGRAM_DIR = _PROGRAM_DIR
xmake._PROGRAM_FILE = _PROGRAM_FILE
xmake._PROJECT_DIR = _PROJECT_DIR
xmake._PROJECT_FILE = "xmake.lua"
xmake._WORKING_DIR = os.curdir()
xmake._FEATURES = _FEATURES
xmake._LUAJIT = _LUAJIT
-- In order to be compatible with updates from lower versions of engine core
-- @see https://github.com/xmake-io/xmake/issues/1694#issuecomment-925507210
if xmake._LUAJIT == nil then
xmake._LUAJIT = true
end
-- has debugger?
if xmake._HAS_DEBUGGER == nil then
if os.getenv("EMMYLUA_DEBUGGER") then
xmake._HAS_DEBUGGER = true
else
xmake._HAS_DEBUGGER = false
end
end
-- load the given lua file
function _loadfile_impl(filepath, mode, opt)
-- init options
opt = opt or {}
-- init displaypath
local binary = false
local displaypath = opt.displaypath
if displaypath == nil then
displaypath = filepath
if filepath:startswith(xmake._WORKING_DIR) then
displaypath = path.translate("./" .. path.relative(filepath, xmake._WORKING_DIR))
elseif filepath:startswith(xmake._PROGRAM_DIR) then
binary = true -- read file by binary mode, will be faster
displaypath = path.translate("@programdir/" .. path.relative(filepath, xmake._PROGRAM_DIR))
elseif filepath:startswith(xmake._PROJECT_DIR) then
local projectname = path.filename(xmake._PROJECT_DIR)
displaypath = path.translate("@projectdir(" .. projectname .. ")/" .. path.relative(filepath, xmake._PROJECT_DIR))
end
end
-- we use raw file path if debugger is enabled
if xmake._HAS_DEBUGGER then
displaypath = filepath
end
-- load script data from file
local file, ferrors = io.file_open(filepath, binary and "rb" or "r")
if not file then
ferrors = string.format("file(%s): %s", filepath, ferrors or "open failed!")
return nil, ferrors
end
local data, rerrors = io.file_read(file, "a")
if not data then
rerrors = string.format("file(%s): %s", filepath, rerrors or "read failed!")
return nil, rerrors
end
io.file_close(file)
-- do on_load()
if opt.on_load then
data = opt.on_load(data) or data
end
-- load script from string
return load(data, "@" .. displaypath, mode)
end
-- init loadfile
--
-- @param filepath the lua file path
-- @param mode the load mode, e.g 't', 'b' or 'bt' (default)
-- @param opt the arguments option, e.g. {displaypath = "", nocache = true}
--
local _loadcache = {}
function loadfile(filepath, mode, opt)
-- init options
opt = opt or {}
-- get absolute path
filepath = path.absolute(filepath)
-- attempt to load script from cache first
local mtime = nil
local cache = (not opt.nocache) and _loadcache[filepath] or nil
if cache and cache.script then
mtime = os.mtime(filepath)
if mtime > 0 and cache.mtime == mtime then
return cache.script, nil
end
end
-- load file
local script, errors = _loadfile_impl(filepath, mode, opt)
if script then
_loadcache[filepath] = {script = script, mtime = mtime or os.mtime(filepath)}
end
return script, errors
end
-- init package path, package.searchers for lua5.4
table.insert(package.loaders or package.searchers, 2, function(v)
local filepath = xmake._PROGRAM_DIR .. "/core/" .. v .. ".lua"
local script, serr = _loadfile_impl(filepath)
if not script then
return "\n\tfailed to load " .. filepath .. " : " .. serr
end
return script
end)
-- load modules
local main = require("main")
-- the main function
function _xmake_main()
return main.entry()
end
|
0 | repos/xmake/xmake/core | repos/xmake/xmake/core/tool/builder.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file builder.lua
--
-- define module
local builder = builder or {}
-- load modules
local io = require("base/io")
local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local string = require("base/string")
local option = require("base/option")
local hashset = require("base/hashset")
local graph = require("base/graph")
local tool = require("tool/tool")
local config = require("project/config")
local sandbox = require("sandbox/sandbox")
local language = require("language/language")
local platform = require("platform/platform")
-- get the tool of builder
function builder:_tool()
return self._TOOL
end
-- get the name flags
function builder:_nameflags()
return self._NAMEFLAGS
end
-- get the target kind
function builder:_targetkind()
return self._TARGETKIND
end
-- map flag implementation
function builder:_mapflag_impl(flag, flagkind, mapflags, auto_ignore_flags)
-- attempt to map it directly
local flag_mapped = mapflags[flag]
if flag_mapped then
return flag_mapped
end
-- find and replace it using pattern, maybe flag is table, e.g. {"-I", "/xxx"}
if type(flag) == "string" then
for k, v in pairs(mapflags) do
local flag_mapped, count = flag:gsub("^" .. k .. "$", function (w) return v end)
if flag_mapped and count ~= 0 then
return #flag_mapped ~= 0 and flag_mapped
end
end
end
-- has this flag?
if auto_ignore_flags == false or self:has_flags(flag, flagkind) then
return flag
else
utils.warning("add_%s(\"%s\") is ignored, please pass `{force = true}` or call `set_policy(\"check.auto_ignore_flags\", false)` if you want to set it.", flagkind, os.args(flag))
end
end
-- map flag
function builder:_mapflag(flag, flagkind, target)
local mapflags = self:get("mapflags")
local auto_map_flags = target and target.policy and target:policy("check.auto_map_flags")
local auto_ignore_flags = target and target.policy and target:policy("check.auto_ignore_flags")
if mapflags and (auto_map_flags ~= false) then
return self:_mapflag_impl(flag, flagkind, mapflags, auto_ignore_flags)
else
if auto_ignore_flags == false or self:has_flags(flag, flagkind) then
return flag
else
utils.warning("add_%s(\"%s\") is ignored, please pass `{force = true}` or call `set_policy(\"check.auto_ignore_flags\", false)` if you want to set it.", flagkind, flag)
end
end
end
-- map flags
function builder:_mapflags(flags, flagkind, target)
local results = {}
local mapflags = self:get("mapflags")
local auto_map_flags = target and target.policy and target:policy("check.auto_map_flags")
local auto_ignore_flags = target and target.policy and target:policy("check.auto_ignore_flags")
flags = table.wrap(flags)
if mapflags and (auto_map_flags ~= false) then
for _, flag in pairs(flags) do
local flag_mapped = self:_mapflag_impl(flag, flagkind, mapflags, auto_ignore_flags)
if flag_mapped then
table.insert(results, flag_mapped)
end
end
else
for _, flag in pairs(flags) do
if auto_ignore_flags == false or self:has_flags(flag, flagkind) then
table.insert(results, flag)
else
utils.warning("add_%s(\"%s\") is ignored, please pass `{force = true}` or call `set_policy(\"check.auto_ignore_flags\", false)` if you want to set it.", flagkind, flag)
end
end
end
return results
end
-- get the flag kinds
function builder:_flagkinds()
return self._FLAGKINDS
end
-- get the extra configuration from value
function builder:_extraconf(extras, value)
local extra = extras
if extra then
if type(value) == "table" then
extra = extra[table.concat(value, "_")]
else
extra = extra[value]
end
end
return extra
end
-- inherit flags (only for public/interface) from target deps
--
-- e.g.
-- add_cflags("", {public = true})
-- add_cflags("", {interface = true})
--
function builder:_inherit_flags_from_targetdeps(flags, target)
local orderdeps = target:orderdeps({inherit = true})
local total = #orderdeps
for idx, _ in ipairs(orderdeps) do
local dep = orderdeps[total + 1 - idx]
for _, flagkind in ipairs(self:_flagkinds()) do
self:_add_flags_from_flagkind(flags, dep, flagkind, {interface = true})
end
end
end
-- add flags from the flagkind
function builder:_add_flags_from_flagkind(flags, target, flagkind, opt)
local targetflags = target:get(flagkind, opt)
local extraconf = target:extraconf(flagkind)
for _, flag in ipairs(table.wrap(targetflags)) do
-- does this flag belong to this tool?
-- @see https://github.com/xmake-io/xmake/issues/3022
--
-- e.g.
-- for all: add_cxxflags("-g")
-- only for clang: add_cxxflags("clang::-stdlib=libc++")
-- only for clang and multiple flags: add_cxxflags("-stdlib=libc++", "-DFOO", {tools = "clang"})
--
local for_this_tool = true
local flagconf = extraconf and extraconf[flag]
if type(flag) == "string" and flag:find("::", 1, true) then
for_this_tool = false
local splitinfo = flag:split("::", {plain = true})
local toolname = splitinfo[1]
if toolname == self:name() then
flag = splitinfo[2]
for_this_tool = true
end
elseif flagconf and flagconf.tools then
for_this_tool = table.contains(table.wrap(flagconf.tools), self:name())
end
if for_this_tool then
if extraconf then
-- @note we need join the single flag with shallow mode, aboid expand table values
-- e.g. add_cflags({"-I", "/tmp/xxx foo"}, {force = true, expand = false})
if flagconf and flagconf.force then
table.shallow_join2(flags, flag)
else
table.shallow_join2(flags, self:_mapflag(flag, flagkind, target))
end
else
table.shallow_join2(flags, self:_mapflag(flag, flagkind, target))
end
end
end
end
-- add flags from the configure
function builder:_add_flags_from_config(flags)
for _, flagkind in ipairs(self:_flagkinds()) do
local values = config.get(flagkind)
if values then
table.join2(flags, os.argv(values))
end
end
end
-- add flags from the target options
function builder:_add_flags_from_targetopts(flags, target)
for _, flagkind in ipairs(self:_flagkinds()) do
local result = target:get_from(flagkind, "option::*")
if result then
for _, values in ipairs(table.wrap(result)) do
table.join2(flags, self:_mapflags(values, flagkind, target))
end
end
end
end
-- add flags from the target packages
function builder:_add_flags_from_targetpkgs(flags, target)
for _, flagkind in ipairs(self:_flagkinds()) do
local result = target:get_from(flagkind, "package::*")
if result then
for _, values in ipairs(table.wrap(result)) do
table.join2(flags, self:_mapflags(values, flagkind, target))
end
end
end
end
-- add flags from the target
function builder:_add_flags_from_target(flags, target)
-- no target?
if not target then
return
end
-- only for target and option
local target_type = target:type()
if target_type ~= "target" and target_type ~= "option" then
return
end
-- init cache
self._TARGETFLAGS = self._TARGETFLAGS or {}
local cache = self._TARGETFLAGS
-- get flags from cache first
local key = target:cachekey()
local targetflags = cache[key]
if not targetflags then
-- add flags from language
targetflags = {}
self:_add_flags_from_language(targetflags, {target = target})
-- add flags for the target
if target_type == "target" then
-- add flags from options
self:_add_flags_from_targetopts(targetflags, target)
-- add flags from packages
self:_add_flags_from_targetpkgs(targetflags, target)
-- inherit flags (public/interface) from all dependent targets
self:_inherit_flags_from_targetdeps(targetflags, target)
end
-- add the target flags
for _, flagkind in ipairs(self:_flagkinds()) do
self:_add_flags_from_flagkind(targetflags, target, flagkind)
end
cache[key] = targetflags
end
table.join2(flags, targetflags)
end
-- add flags from the argument option
function builder:_add_flags_from_argument(flags, target, args)
-- add flags from the flag kinds (cxflags, ..)
for _, flagkind in ipairs(self:_flagkinds()) do
table.join2(flags, self:_mapflags(args[flagkind], flagkind, target))
local original_flags = (args.force or {})[flagkind]
if original_flags then
table.join2(flags, original_flags)
end
end
-- add flags (named) from the language
self:_add_flags_from_language(flags, {linkorders = args.linkorders, linkgroups = args.linkgroups, getters = {
target = function (name)
-- we need also to get extra from arguments
-- @see https://github.com/xmake-io/xmake/issues/4274
--
-- e.g.
-- package/add_linkgroups("xxx", {group = true})
-- {linkgroups = , extras = {
-- linkgroups = {z = {group = true}}
-- }}
local values = args[name]
local extras = args.extras and args.extras[name]
return values, extras
end,
toolchain = function (name)
if target and target.toolconfig then
return target:toolconfig(name)
end
local plat, arch
if target and target.plat then
plat = target:plat()
end
if target and target.arch then
arch = target:arch()
end
return platform.toolconfig(name, plat, arch)
end}})
end
-- add items from getter
function builder:_add_items_from_getter(items, name, opt)
local values, extras = opt.getter(name)
if values then
table.insert(items, {
name = name,
values = table.wrap(values),
check = opt.check,
multival = opt.multival,
mapper = opt.mapper,
extras = extras})
end
end
-- add items from config
function builder:_add_items_from_config(items, name, opt)
local values = config.get(name)
if values and name:endswith("dirs") then
values = path.splitenv(values)
end
if values then
table.insert(items, {
name = name,
values = table.wrap(values),
check = opt.check,
multival = opt.multival,
mapper = opt.mapper})
end
end
-- add items from toolchain
function builder:_add_items_from_toolchain(items, name, opt)
local values
local target = opt.target
if target and target:type() == "target" then
values = target:toolconfig(name)
else
values = platform.toolconfig(name)
end
if values then
table.insert(items, {
name = name,
values = table.wrap(values),
check = opt.check,
multival = opt.multival,
mapper = opt.mapper})
end
end
-- add items from option
function builder:_add_items_from_option(items, name, opt)
local values
local target = opt.target
if target then
values = target:get(name)
end
if values then
table.insert(items, {
name = name,
values = table.wrap(values),
check = opt.check,
multival = opt.multival,
mapper = opt.mapper})
end
end
-- add items from target
function builder:_add_items_from_target(items, name, opt)
local target = opt.target
if target then
local result, sources = target:get_from(name, "*")
if result then
for idx, values in ipairs(result) do
local source = sources[idx]
local extras = target:extraconf_from(name, source)
values = table.wrap(values)
if values and #values > 0 then
table.insert(items, {
name = name,
values = values,
extras = extras,
check = opt.check,
multival = opt.multival,
mapper = opt.mapper})
end
end
end
end
end
-- add flags from the language
function builder:_add_flags_from_language(flags, opt)
opt = opt or {}
-- get order named items
local items = {}
local target = opt.target
for _, flaginfo in ipairs(self:_nameflags()) do
-- get flag info
local flagscope = flaginfo[1]
local flagname = flaginfo[2]
local checkstate = flaginfo[3]
if checkstate then
local auto_ignore_flags = target and target.policy and target:policy("check.auto_ignore_flags")
if auto_ignore_flags == false then
checkstate = false
end
end
-- get api name of tool
local apiname = flagname:gsub("^nf_", "")
-- use multiple values mapper if be defined in tool module
local multival = false
if apiname:endswith("s") then
if self:_tool()["nf_" .. apiname] then
multival = true
else
apiname = apiname:sub(1, #apiname - 1)
end
end
-- map named flags to real flags
local mapper = self:_tool()["nf_" .. apiname]
if mapper then
local opt_ = {target = target, check = checkstate, multival = multival, mapper = mapper}
if opt.getters then
local getter = opt.getters[flagscope]
if getter then
opt_.getter = getter
self:_add_items_from_getter(items, flagname, opt_)
end
elseif flagscope == "target" and target and target:type() == "target" then
self:_add_items_from_target(items, flagname, opt_)
elseif flagscope == "target" and target and target:type() == "option" then
self:_add_items_from_option(items, flagname, opt_)
elseif flagscope == "config" then
self:_add_items_from_config(items, flagname, opt_)
elseif flagscope == "toolchain" then
self:_add_items_from_toolchain(items, flagname, opt_)
end
end
end
-- sort links
local kind = self:kind()
if kind == "ld" or kind == "sh" then
local linkorders = table.wrap(opt.linkorders)
local linkgroups = table.wrap(opt.linkgroups)
if target and target:type() == "target" then
local values = target:get_from("linkorders", "*")
if values then
for _, value in ipairs(values) do
table.join2(linkorders, value)
end
end
values = target:get_from("linkgroups", "*")
if values then
for _, value in ipairs(values) do
table.join2(linkgroups, value)
end
end
end
if #linkorders > 0 or #linkgroups > 0 then
self:_sort_links_of_items(items, {linkorders = linkorders, linkgroups = linkgroups})
end
end
-- get flags from the items
for _, item in ipairs(items) do
local check = item.check
local mapper = item.mapper
local extras = item.extras
if item.multival then
local extra = self:_extraconf(extras, item.values)
local results = mapper(self:_tool(), item.values, {target = target, targetkind = self:_targetkind(), extra = extra})
for _, flag in ipairs(table.wrap(results)) do
if flag and flag ~= "" and (not check or self:has_flags(flag)) then
table.insert(flags, flag)
end
end
else
for _, flagvalue in ipairs(item.values) do
local extra = self:_extraconf(extras, flagvalue)
local flag = mapper(self:_tool(), flagvalue, {target = target, targetkind = self:_targetkind(), extra = extra})
if flag and flag ~= "" and (not check or self:has_flags(flag)) then
table.insert(flags, flag)
end
end
end
end
end
-- sort links of items
function builder:_sort_links_of_items(items, opt)
opt = opt or {}
local sortlinks = false
local makegroups = false
local linkorders = table.wrap(opt.linkorders)
if #linkorders > 0 then
sortlinks = true
end
local linkgroups = table.wrap(opt.linkgroups)
local linkgroups_set = hashset.new()
if #linkgroups > 0 then
makegroups = true
for _, linkgroup in ipairs(linkgroups) do
for _, link in ipairs(linkgroup) do
linkgroups_set:insert(link)
end
end
end
-- get all links
local links = {}
local linkgroups_map = {}
local extras_map = {}
local link_mapper
local framework_mapper
local linkgroup_mapper
if sortlinks or makegroups then
local linkitems = {}
table.remove_if(items, function (_, item)
local name = item.name
local removed = false
if name == "links" or name == "syslinks" then
link_mapper = item.mapper
removed = true
table.insert(linkitems, item)
elseif name == "frameworks" then
framework_mapper = item.mapper
removed = true
table.insert(linkitems, item)
elseif name == "linkgroups" then
linkgroup_mapper = item.mapper
removed = true
table.insert(linkitems, item)
end
return removed
end)
-- @note table.remove_if will traverse backwards,
-- we need to fix the initial link order first to make sure the syslinks are in the correct order
linkitems = table.reverse(linkitems)
for _, item in ipairs(linkitems) do
local name = item.name
for _, value in ipairs(item.values) do
if name == "links" or name == "syslinks" then
if not linkgroups_set:has(value) then
table.insert(links, value)
end
elseif name == "frameworks" then
table.insert(links, "framework::" .. value)
elseif name == "linkgroups" then
local extras = item.extras
local extra = self:_extraconf(extras, value)
local key = extra and extra.name or tostring(value)
table.insert(links, "linkgroup::" .. key)
linkgroups_map[key] = value
extras_map[key] = extras
end
end
end
links = table.reverse_unique(links)
end
-- sort sublinks
if sortlinks then
local gh = graph.new(true)
local from
local original_deps = {}
for _, link in ipairs(links) do
local to = link
if from and to then
original_deps[from] = to
end
from = to
end
-- we need remove cycle in original links
-- e.g.
--
-- case1:
-- original_deps: a -> b -> c -> d -> e
-- new deps: e -> b
-- graph: a -> b -> c -> d e (remove d -> e, add d -> nil)
-- /|\ |
-- --------------
--
-- case2:
-- original_deps: a -> b -> c -> d -> e
-- new deps: b -> a
--
-- ---------
-- | \|/
-- graph: a b -> c -> d -> e (remove a -> b, add a -> c)
-- /|\ |
-- ----
--
local function remove_cycle_in_original_deps(f, t)
local k
local v = t
while v ~= f do
k = v
v = original_deps[v]
if v == nil then
break
end
end
if v == f and k ~= nil then
-- break the original from node, link to next node
-- e.g.
-- case1: d -x-> e, d -> nil, k: d, f: e
-- case2: a -x-> b, a -> c, k: a, f: b
original_deps[k] = original_deps[f]
end
end
local links_set = hashset.from(links)
for _, linkorder in ipairs(linkorders) do
local from
for _, link in ipairs(linkorder) do
if links_set:has(link) then
local to = link
if from and to then
remove_cycle_in_original_deps(from, to)
gh:add_edge(from, to)
end
from = to
end
end
end
for k, v in pairs(original_deps) do
gh:add_edge(k, v)
end
if not gh:empty() then
local cycle = gh:find_cycle()
if cycle then
utils.warning("cycle links found in add_linkorders(): %s", table.concat(cycle, " -> "))
end
links = gh:topological_sort()
end
end
-- re-generate links to items list
if sortlinks or makegroups then
for _, link in ipairs(links) do
if link:startswith("framework::") then
link = link:sub(12)
table.insert(items, {name = "frameworks", values = table.wrap(link), check = false, multival = false, mapper = framework_mapper})
elseif link:startswith("linkgroup::") then
local key = link:sub(12)
local values = linkgroups_map[key]
local extras = extras_map[key]
table.insert(items, {name = "linkgroups", values = table.wrap(values), extras = extras, check = false, multival = false, mapper = linkgroup_mapper})
else
table.insert(items, {name = "links", values = table.wrap(link), check = false, multival = false, mapper = link_mapper})
end
end
end
end
-- preprocess flags
function builder:_preprocess_flags(flags)
-- remove repeat by right direction, because we need to consider links/deps order
-- @note https://github.com/xmake-io/xmake/issues/1240
local unique = {}
local count = #flags
if count > 1 then
local flags_new = {}
for idx = count, 1, -1 do
local flag = flags[idx]
local flagkey = type(flag) == "table" and table.concat(flag, "") or flag
if flag and not unique[flagkey] then
table.insert(flags_new, flag)
unique[flagkey] = true
end
end
flags = flags_new
count = #flags_new
end
-- remove repeat first and split flags group, e.g. "-I /xxx" => {"-I", "/xxx"}
local results = {}
if count > 0 then
for idx = count, 1, -1 do
local flag = flags[idx]
if type(flag) == "string" then
flag = flag:trim()
if #flag > 0 then
if flag:find(" ", 1, true) then
table.join2(results, os.argv(flag, {splitonly = true}))
else
table.insert(results, flag)
end
end
else
-- may be a table group? e.g. {"-I", "/xxx"}
if #flag > 0 then
table.wrap_unlock(flag)
table.join2(results, flag)
end
end
end
end
return results
end
-- get the target
function builder:target()
return self._TARGET
end
-- get tool name
function builder:name()
return self:_tool():name()
end
-- get tool kind
function builder:kind()
return self:_tool():kind()
end
-- get tool program
function builder:program()
return self:_tool():program()
end
-- get toolchain of this tool
function builder:toolchain()
return self:_tool():toolchain()
end
-- get the run environments
function builder:runenvs()
return self:_tool():runenvs()
end
-- get properties of the tool
function builder:get(name)
return self:_tool():get(name)
end
-- has flags?
function builder:has_flags(flags, flagkind, opt)
return self:_tool():has_flags(flags, flagkind, opt)
end
-- map flags from name and values, e.g. linkdirs, links, defines
function builder:map_flags(name, values, opt)
local flags = {}
local mapper = self:_tool()["nf_" .. name]
local multival = false
if name:endswith("s") then
multival = true
elseif not mapper then
mapper = self:_tool()["nf_" .. name .. "s"]
if mapper then
multival = true
end
end
if mapper then
opt = opt or {}
if multival then
local extra = self:_extraconf(opt.extras, values)
local results = mapper(self:_tool(), values, {target = opt.target, targetkind = opt.targetkind, extra = extra})
for _, flag in ipairs(table.wrap(results)) do
if flag and flag ~= "" and (not opt.check or self:has_flags(flag)) then
table.insert(flags, flag)
end
end
else
for _, value in ipairs(table.wrap(values)) do
local extra = self:_extraconf(opt.extras, value)
local flag = mapper(self:_tool(), value, {target = opt.target, targetkind = opt.targetkind, extra = extra})
if flag and flag ~= "" and (not opt.check or self:has_flags(flag)) then
table.join2(flags, flag)
end
end
end
end
if #flags > 0 then
return flags
end
end
-- return module
return builder
|
0 | repos/xmake/xmake/core | repos/xmake/xmake/core/tool/compiler.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file compiler.lua
--
-- define module
local compiler = compiler or {}
-- load modules
local io = require("base/io")
local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local string = require("base/string")
local option = require("base/option")
local profiler = require("base/profiler")
local tool = require("tool/tool")
local builder = require("tool/builder")
local config = require("project/config")
local sandbox = require("sandbox/sandbox")
local language = require("language/language")
local platform = require("platform/platform")
-- get the language of compiler
function compiler:_language()
return self._LANGUAGE
end
-- add flags from the toolchains
function compiler:_add_flags_from_toolchains(flags, targetkind, target)
-- add flags for platform with the given target kind, e.g. binary.gcc.cxflags or binary.cxflags
if targetkind then
local toolname = self:name()
if target and target.toolconfig then
for _, flagkind in ipairs(self:_flagkinds()) do
local toolflags = target:toolconfig(targetkind .. '.' .. toolname .. '.' .. flagkind)
table.join2(flags, toolflags or target:toolconfig(targetkind .. '.' .. flagkind))
end
else
for _, flagkind in ipairs(self:_flagkinds()) do
local toolflags = platform.toolconfig(targetkind .. '.' .. toolname .. '.' .. flagkind)
table.join2(flags, toolflags or platform.toolconfig(targetkind .. '.' .. flagkind))
end
end
end
end
-- add flags from the compiler
function compiler:_add_flags_from_compiler(flags, targetkind)
for _, flagkind in ipairs(self:_flagkinds()) do
-- add compiler, e.g. cxflags
table.join2(flags, self:get(flagkind))
-- add compiler, e.g. targetkind.cxflags
if targetkind then
table.join2(flags, self:get(targetkind .. '.' .. flagkind))
end
end
end
-- add flags from the sourcefile config
function compiler:_add_flags_from_fileconfig(flags, target, sourcefile, fileconfig)
-- add flags from the current compiler
local add_sourceflags = self:_tool().add_sourceflags
if add_sourceflags then
local flag = add_sourceflags(self:_tool(), sourcefile, fileconfig, target, self:_targetkind())
if flag and flag ~= "" then
table.join2(flags, flag)
end
end
-- add flags from the common argument option
self:_add_flags_from_argument(flags, target, fileconfig)
end
-- load compiler tool
function compiler._load_tool(sourcekind, target)
-- get program from target
local program, toolname, toolchain_info
if target and target.tool then
program, toolname, toolchain_info = target:tool(sourcekind)
end
-- load the compiler tool from the source kind
local result, errors = tool.load(sourcekind, {program = program, toolname = toolname, toolchain_info = toolchain_info})
if not result then
return nil, errors
end
return result, program
end
-- load the compiler from the given source kind
function compiler.load(sourcekind, target)
if not sourcekind then
return nil, "unknown source kind!"
end
-- load compiler tool first (with cache)
local compiler_tool, program_or_errors = compiler._load_tool(sourcekind, target)
if not compiler_tool then
return nil, program_or_errors
end
-- init cache key
-- @note we need plat/arch,
-- because it is possible for the compiler to do cross-compilation with the -target parameter
local plat = compiler_tool:plat() or config.plat() or os.host()
local arch = compiler_tool:arch() or config.arch() or os.arch()
local cachekey = sourcekind .. (program_or_errors or "") .. plat .. arch
-- get it directly from cache dirst
compiler._INSTANCES = compiler._INSTANCES or {}
local instance = compiler._INSTANCES[cachekey]
if not instance then
-- new instance
instance = table.inherit(compiler, builder)
-- save the compiler tool
instance._TOOL = compiler_tool
-- load the compiler language from the source kind
local result, errors = language.load_sk(sourcekind)
if not result then
return nil, errors
end
instance._LANGUAGE = result
-- init target (optional)
instance._TARGET = target
-- init target kind
instance._TARGETKIND = "object"
-- init name flags
instance._NAMEFLAGS = result:nameflags()[instance:_targetkind()]
-- init flag kinds
instance._FLAGKINDS = table.wrap(result:sourceflags()[sourcekind])
-- add toolchains flags to the compiler tool, e.g. gcc.cxflags or cxflags
local toolname = compiler_tool:name()
if target and target.toolconfig then
for _, flagkind in ipairs(instance:_flagkinds()) do
compiler_tool:add(flagkind, target:toolconfig(toolname .. '.' .. flagkind) or target:toolconfig(flagkind))
end
else
for _, flagkind in ipairs(instance:_flagkinds()) do
compiler_tool:add(flagkind, platform.toolconfig(toolname .. '.' .. flagkind) or platform.toolconfig(flagkind))
end
end
-- @note we can't call _load_once before caching the instance,
-- it may call has_flags to trigger the concurrent scheduling.
--
-- this will result in more compiler/linker instances being created at the same time,
-- and they will access the same tool instance at the same time.
--
-- @see https://github.com/xmake-io/xmake/issues/3429
compiler._INSTANCES[cachekey] = instance
end
-- we need to load it at the end because in tool.load().
-- because we may need to call has_flags, which requires the full platform toolchain flags
local ok, errors = compiler_tool:_load_once()
if not ok then
return nil, errors
end
return instance
end
-- build the source files (compile and link)
function compiler:build(sourcefiles, targetfile, opt)
-- init options
opt = opt or {}
-- get compile flags
local compflags = opt.compflags
if not compflags then
-- patch sourcefile to get flags of the given source file
if type(sourcefiles) == "string" then
opt.sourcefile = sourcefiles
end
compflags = self:compflags(opt)
end
-- make flags
local flags = compflags
if opt.target then
flags = table.join(flags, opt.target:linkflags())
end
-- get target kind
local targetkind = opt.targetkind
if not targetkind and opt.target and opt.target.targetkind then
targetkind = opt.target:kind()
end
-- get it
return sandbox.load(self:_tool().build, self:_tool(), sourcefiles, targetkind or "binary", targetfile, flags)
end
-- get the build arguments list (compile and link)
function compiler:buildargv(sourcefiles, targetfile, opt)
-- init options
opt = opt or {}
-- get compile flags
local compflags = opt.compflags
if not compflags then
-- patch sourcefile to get flags of the given source file
if type(sourcefiles) == "string" then
opt.sourcefile = sourcefiles
end
compflags = self:compflags(opt)
end
-- make flags
local flags = compflags
if opt.target then
flags = table.join(flags, opt.target:linkflags())
end
-- get target kind
local targetkind = opt.targetkind
if not targetkind and opt.target and opt.target.targetkind then
targetkind = opt.target:kind()
end
-- get it
return self:_tool():buildargv(sourcefiles, targetkind or "binary", targetfile, flags)
end
-- get the build command
function compiler:buildcmd(sourcefiles, targetfile, opt)
return os.args(table.join(self:buildargv(sourcefiles, targetfile, opt)))
end
-- compile the source files
function compiler:compile(sourcefiles, objectfile, opt)
-- get compile flags
opt = opt or {}
local compflags = opt.compflags
if not compflags then
-- patch sourcefile to get flags of the given source file
if type(sourcefiles) == "string" then
opt.sourcefile = sourcefiles
end
compflags = self:compflags(opt)
end
-- compile it
opt = table.copy(opt)
opt.target = self:target()
profiler:enter(self:name(), "compile", sourcefiles)
local ok, errors = sandbox.load(self:_tool().compile, self:_tool(), sourcefiles, objectfile, opt.dependinfo, compflags, opt)
profiler:leave(self:name(), "compile", sourcefiles)
return ok, errors
end
-- get the compile arguments list
function compiler:compargv(sourcefiles, objectfile, opt)
-- init options
opt = opt or {}
-- get compile flags
local compflags = opt.compflags
if not compflags then
-- patch sourcefile to get flags of the given source file
if type(sourcefiles) == "string" then
opt.sourcefile = sourcefiles
end
compflags = self:compflags(opt)
end
return self:_tool():compargv(sourcefiles, objectfile, compflags, opt)
end
-- get the compile command
function compiler:compcmd(sourcefiles, objectfile, opt)
return os.args(table.join(self:compargv(sourcefiles, objectfile, opt)))
end
-- get the compling flags
--
-- @param opt the argument options (contain all the compiler attributes of target),
-- e.g.
-- {target = ..., targetkind = "static", configs = {defines = "", cxflags = "", includedirs = ""}}
--
-- @return flags list
--
function compiler:compflags(opt)
-- init options
opt = opt or {}
-- get target
local target = opt.target
-- get target kind
local targetkind = opt.targetkind
if not targetkind and target and target:type() == "target" then
targetkind = target:kind()
end
-- add flags from compiler/toolchains
--
-- we need to add toolchain flags at the beginning to allow users to override them.
-- but includedirs/links/syslinks/linkdirs will still be placed last, they are in the order defined in languages/xmake.lua
--
-- @see https://github.com/xmake-io/xmake/issues/978
--
local flags = {}
self:_add_flags_from_compiler(flags, targetkind)
self:_add_flags_from_toolchains(flags, targetkind, target)
-- add flags from user configuration
self:_add_flags_from_config(flags)
-- add flags from target
self:_add_flags_from_target(flags, target)
-- add flags from source file configuration
if opt.sourcefile and target and target.fileconfig then
local fileconfig = target:fileconfig(opt.sourcefile)
if fileconfig then
self:_add_flags_from_fileconfig(flags, target, opt.sourcefile, fileconfig)
end
end
-- add flags from argument
local configs = opt.configs or opt.config
if configs then
self:_add_flags_from_argument(flags, target, configs)
end
-- preprocess flags
return self:_preprocess_flags(flags)
end
-- return module
return compiler
|
0 | repos/xmake/xmake/core | repos/xmake/xmake/core/tool/toolchain.lua | --!A cross-toolchain build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file toolchain.lua
--
-- define module
local toolchain = toolchain or {}
local _instance = _instance or {}
-- load modules
local os = require("base/os")
local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local global = require("base/global")
local option = require("base/option")
local hashset = require("base/hashset")
local scopeinfo = require("base/scopeinfo")
local interpreter = require("base/interpreter")
local config = require("project/config")
local memcache = require("cache/memcache")
local localcache = require("cache/localcache")
local language = require("language/language")
local sandbox = require("sandbox/sandbox")
local sandbox_module = require("sandbox/modules/import/core/sandbox/module")
-- new an instance
function _instance.new(name, info, cachekey, is_builtin, configs)
local instance = table.inherit(_instance)
instance._NAME = name
instance._INFO = info
instance._IS_BUILTIN = is_builtin
instance._CACHE = toolchain._localcache()
instance._CACHEKEY = cachekey
instance._CONFIGS = instance._CACHE:get(cachekey) or {}
for k, v in pairs(configs) do
instance._CONFIGS[k] = v
end
-- is global toolchain for the whole platform?
configs.plat = nil
configs.arch = nil
configs.cachekey = nil
local plat = config.get("plat") or os.host()
local arch = config.get("arch") or os.arch()
if instance:is_plat(plat) and instance:is_arch(arch) and #table.keys(configs) == 0 then
instance._CONFIGS.__global = true
end
return instance
end
-- get toolchain name
function _instance:name()
return self._NAME
end
-- get toolchain platform
function _instance:plat()
return self._PLAT or self:config("plat")
end
-- set toolchain platform
function _instance:plat_set(plat)
self._PLAT = plat
end
-- get toolchain architecture
function _instance:arch()
return self._ARCH or self:config("arch")
end
-- set toolchain architecture
function _instance:arch_set(arch)
self._ARCH = arch
end
-- the current platform is belong to the given platforms?
function _instance:is_plat(...)
local plat = self:plat()
for _, v in ipairs(table.join(...)) do
if v and plat == v then
return true
end
end
end
-- the current architecture is belong to the given architectures?
function _instance:is_arch(...)
local arch = self:arch()
for _, v in ipairs(table.join(...)) do
if v and arch:find("^" .. v:gsub("%-", "%%-") .. "$") then
return true
end
end
end
-- get toolchain info
function _instance:info()
local arch = self:arch()
local infos = self._INFOS
if not infos then
infos = {}
self._INFOS = infos
end
local info = infos[arch]
if not info then
-- we need multiple info objects for different architectures
info = self._INFO:clone()
infos[arch] = info
end
return info
end
-- set the value to the toolchain configuration
function _instance:set(name, ...)
self:info():apival_set(name, ...)
end
-- add the value to the toolchain configuration
function _instance:add(name, ...)
self:info():apival_add(name, ...)
end
-- get the toolchain configuration
function _instance:get(name)
-- attempt to get the static configure value
local value = self:info():get(name)
if value ~= nil then
return value
end
-- lazy loading toolchain
self:_load()
-- get other platform info
return self:info():get(name)
end
-- get toolchain kind
function _instance:kind()
return self:info():get("kind")
end
-- get toolchain formats, we must set it in description scope
-- @see https://github.com/xmake-io/xmake/issues/4769
function _instance:formats()
return self:info():get("formats")
end
-- is cross-compilation toolchain?
function _instance:is_cross()
if self:kind() == "cross" then
return true
elseif self:kind() == "standalone" and (self:cross() or self:sdkdir()) then
return true
end
end
-- is standalone toolchain?
function _instance:is_standalone()
return self:kind() == "standalone" or self:kind() == "cross"
end
-- is global toolchain for whole platform
function _instance:is_global()
return self:config("__global")
end
-- is builtin toolchain? it's not from local project
function _instance:is_builtin()
return self._IS_BUILTIN
end
-- get the run environments
function _instance:runenvs()
local runenvs = self._RUNENVS
if runenvs == nil then
local toolchain_runenvs = self:get("runenvs")
if toolchain_runenvs then
runenvs = {}
for name, values in pairs(toolchain_runenvs) do
if type(values) == "table" then
values = path.joinenv(values)
end
runenvs[name] = values
end
end
runenvs = runenvs or false
self._RUNENVS = runenvs
end
return runenvs or nil
end
-- get the program and name of the given tool kind
function _instance:tool(toolkind)
if not self:_is_checked() then
utils.warning("we cannot get tool(%s) in toolchain(%s) with %s/%s, because it has been not checked yet!", toolkind, self:name(), self:plat(), self:arch())
end
-- ensure to do load for initializing toolset first
-- @note we cannot call self:check() here, because it can only be called on config
self:_load()
local toolpaths = self:get("toolset." .. toolkind)
if toolpaths then
for _, toolpath in ipairs(table.wrap(toolpaths)) do
local program, toolname = self:_checktool(toolkind, toolpath)
if program then
return program, toolname
end
end
end
end
-- get the toolchain script
function _instance:script(name)
return self:info():get(name)
end
-- get the cross
function _instance:cross()
return self:config("cross") or config.get("cross") or self:info():get("cross")
end
-- get the bin directory
function _instance:bindir()
local bindir = self:config("bindir") or config.get("bin") or self:info():get("bindir")
if not bindir and self:is_cross() and self:sdkdir() and os.isdir(path.join(self:sdkdir(), "bin")) then
bindir = path.join(self:sdkdir(), "bin")
end
return bindir
end
-- get the sdk directory
function _instance:sdkdir()
return self:config("sdkdir") or config.get("sdk") or self:info():get("sdkdir")
end
-- get cachekey
function _instance:cachekey()
return self._CACHEKEY
end
-- get user config from `set_toolchains("", {configs = {vs = "2018"}})`
function _instance:config(name)
return self._CONFIGS[name]
end
-- set user config
function _instance:config_set(name, data)
self._CONFIGS[name] = data
end
-- save user configs
function _instance:configs_save()
self._CACHE:set(self:cachekey(), self._CONFIGS)
self._CACHE:save()
end
-- do check, we only check it once for all architectures
function _instance:check()
local checkok = true
local checked = self:_is_checked()
if not checked then
local on_check = self:_on_check()
if on_check then
local ok, results_or_errors = sandbox.load(on_check, self)
if ok then
checkok = results_or_errors
else
os.raise(results_or_errors)
end
end
-- we need to persist this state
self:config_set("__checked", true)
self:configs_save()
end
return checkok
end
-- do load manually, it will call on_load()
function _instance:load()
self:_load()
end
-- check cross toolchain
function _instance:check_cross_toolchain()
return sandbox_module.import("toolchains.cross.check", {rootdir = os.programdir(), anonymous = true})(self)
end
-- load cross toolchain
function _instance:load_cross_toolchain()
return sandbox_module.import("toolchains.cross.load", {rootdir = os.programdir(), anonymous = true})(self)
end
-- get packages
function _instance:packages()
local packages = self._PACKAGES
if packages == nil then
local project = require("project/project")
-- we will get packages from `set_toolchains("foo", {packages})` or `set_toolchains("foo@packages")`
for _, pkgname in ipairs(table.wrap(self:config("packages"))) do
local requires = project.required_packages()
if requires then
local pkginfo = requires[pkgname]
if pkginfo then
packages = packages or {}
table.insert(packages, pkginfo)
end
end
end
self._PACKAGES = packages or false
end
return packages or nil
end
-- save toolchain to file
function _instance:savefile(filepath)
if not self:_is_loaded() then
os.raise("we can only save toolchain(%s) after it has been loaded!", self:name())
end
-- we strip on_load/on_check scripts to solve some issues
-- @see https://github.com/xmake-io/xmake/issues/3774
local info = table.clone(self:info():info())
info.load = nil
info.check = nil
return io.save(filepath, {name = self:name(), info = info, cachekey = self:cachekey(), configs = self._CONFIGS})
end
-- on check (builtin)
function _instance:_on_check()
local on_check = self:info():get("check")
if not on_check and self:is_cross() then
on_check = self.check_cross_toolchain
end
return on_check
end
-- on load (builtin)
function _instance:_on_load()
local on_load = self:info():get("load")
if not on_load and self:is_cross() then
on_load = self.load_cross_toolchain
end
return on_load
end
-- do load, @note we need to load it repeatly for each architectures
function _instance:_load()
if not self:_is_checked() then
utils.warning("we cannot load toolchain(%s), because it has been not checked yet!", self:name(), self:plat(), self:arch())
end
local info = self:info()
if not info:get("__loaded") and not info:get("__loading") then
local on_load = self:_on_load()
if on_load then
info:set("__loading", true)
local ok, errors = sandbox.load(on_load, self)
info:set("__loading", false)
if not ok then
os.raise(errors)
end
end
info:set("__loaded", true)
end
end
-- is loaded?
function _instance:_is_loaded()
return self:info():get("__loaded")
end
-- is checked?
function _instance:_is_checked()
return self:config("__checked") == true or self:_on_check() == nil
end
-- get the tool description from the tool kind
function _instance:_description(toolkind)
local descriptions = self._DESCRIPTIONS
if not descriptions then
descriptions =
{
cc = "the c compiler",
cxx = "the c++ compiler",
cpp = "the c/c++ preprocessor",
ld = "the linker",
sh = "the shared library linker",
ar = "the static library archiver",
mrc = "the windows resource compiler",
strip = "the symbols stripper",
ranlib = "the archive index generator",
objcopy = "the GNU objcopy utility",
dsymutil = "the symbols generator",
mm = "the objc compiler",
mxx = "the objc++ compiler",
as = "the assember",
sc = "the swift compiler",
scld = "the swift linker",
scsh = "the swift shared library linker",
gc = "the golang compiler",
gcld = "the golang linker",
gcar = "the golang static library archiver",
dc = "the dlang compiler",
dcld = "the dlang linker",
dcsh = "the dlang shared library linker",
dcar = "the dlang static library archiver",
rc = "the rust compiler",
rcld = "the rust linker",
rcsh = "the rust shared library linker",
rcar = "the rust static library archiver",
fc = "the fortran compiler",
fcld = "the fortran linker",
fcsh = "the fortran shared library linker",
zc = "the zig compiler",
zcld = "the zig linker",
zcsh = "the zig shared library linker",
zcar = "the zig static library archiver",
cu = "the cuda compiler",
culd = "the cuda linker",
cuccbin = "the cuda host c++ compiler",
nc = "the nim compiler",
ncld = "the nim linker",
ncsh = "the nim shared library linker",
ncar = "the nim static library archiver"
}
self._DESCRIPTIONS = descriptions
end
return descriptions[toolkind]
end
-- check the given tool path
function _instance:_checktool(toolkind, toolpath)
-- get result from cache first
local cachekey = self:cachekey() .. "_checktool" .. toolkind
local result = toolchain._memcache():get3(cachekey, toolkind, toolpath)
if result then
return result[1], result[2]
end
-- get find_tool
local find_tool = self._find_tool
if not find_tool then
find_tool = sandbox_module.import("lib.detect.find_tool", {anonymous = true})
self._find_tool = find_tool
end
-- do filter for toolpath variables, e.g. set_toolset("cc", "$(env CC)")
local sandbox_inst = sandbox.instance()
if sandbox_inst then
local filter = sandbox_inst:filter()
if filter then
local value = filter:handle(toolpath)
if value and value:trim() ~= "" then
toolpath = value
else
return
end
end
end
-- contain toolname? parse it, e.g. '[email protected]'
-- https://github.com/xmake-io/xmake/issues/1361
local program, toolname
if toolpath then
local pos = toolpath:find('@', 1, true)
if pos then
-- we need to ignore valid path with `@`, e.g. /usr/local/opt/[email protected]/bin/go
-- https://github.com/xmake-io/xmake/issues/2853
local prefix = toolpath:sub(1, pos - 1)
if prefix and not prefix:find("[/\\]") then
toolname = prefix
program = toolpath:sub(pos + 1)
end
end
end
-- find tool program
local tool = find_tool(toolpath, {toolchain = self,
cachekey = cachekey,
program = program or toolpath,
paths = self:bindir(),
envs = self:get("runenvs")})
if tool then
program = tool.program
toolname = toolname or tool.name
end
-- get tool description from the tool kind
local description = self:_description(toolkind) or ("unknown toolkind " .. toolkind)
-- trace
if option.get("verbose") then
if program then
utils.cprint("${dim}checking for %s (%s) ... ${color.success}%s", description, toolkind, path.filename(program))
else
utils.cprint("${dim}checking for %s (%s: ${bright}%s${clear}) ... ${color.nothing}${text.nothing}", description, toolkind, toolpath)
end
end
toolchain._memcache():set3(cachekey, toolkind, toolpath, {program, toolname})
return program, toolname
end
-- get memcache
function toolchain._memcache()
return memcache.cache("core.tool.toolchain")
end
-- get local cache
function toolchain._localcache()
return localcache.cache("toolchain")
end
-- the interpreter
function toolchain._interpreter()
-- the interpreter has been initialized? return it directly
if toolchain._INTERPRETER then
return toolchain._INTERPRETER
end
-- init interpreter
local interp = interpreter.new()
assert(interp)
-- define apis
interp:api_define(toolchain.apis())
-- define apis for language
interp:api_define(language.apis())
-- save interpreter
toolchain._INTERPRETER = interp
return interp
end
-- get cache key
function toolchain._cachekey(name, opt)
local cachekey = opt.cachekey
if not cachekey then
cachekey = name
for _, k in ipairs(table.orderkeys(opt)) do
local v = opt[k]
cachekey = cachekey .. "_" .. k .. "_" .. tostring(v)
end
end
return cachekey
end
-- parse toolchain and package name
--
-- format: toolchain@package
-- e.g. "clang@llvm-10", "@muslcc", zig
--
function toolchain.parsename(name)
local splitinfo = name:split('@', {plain = true, strict = true})
local toolchain_name = splitinfo[1]
if toolchain_name == "" then
toolchain_name = nil
end
local packages = splitinfo[2]
if packages == "" then
packages = nil
end
return toolchain_name or packages, packages
end
-- get toolchain apis
function toolchain.apis()
return
{
values =
{
"toolchain.set_kind"
, "toolchain.set_cross"
, "toolchain.set_bindir"
, "toolchain.set_sdkdir"
, "toolchain.set_archs"
, "toolchain.set_runtimes"
, "toolchain.set_homepage"
, "toolchain.set_description"
}
, keyvalues =
{
-- toolchain.set_xxx
"toolchain.set_formats"
, "toolchain.set_toolset"
, "toolchain.add_toolset"
-- toolchain.add_xxx
, "toolchain.add_runenvs"
}
, script =
{
-- toolchain.on_xxx
"toolchain.on_load"
, "toolchain.on_check"
}
}
end
-- get toolchain directories
function toolchain.directories()
local dirs = toolchain._DIRS or { path.join(global.directory(), "toolchains")
, path.join(os.programdir(), "toolchains")
}
toolchain._DIRS = dirs
return dirs
end
-- load toolchain
function toolchain.load(name, opt)
-- get toolchain name and packages
opt = opt or {}
local packages
name, packages = toolchain.parsename(name)
opt.packages = opt.packages or packages
-- get cache
opt.plat = opt.plat or config.get("plat") or os.host()
opt.arch = opt.arch or config.get("arch") or os.arch()
local cache = toolchain._memcache()
local cachekey = toolchain._cachekey(name, opt)
-- get it directly from cache dirst
local instance = cache:get(cachekey)
if instance then
return instance
end
-- find the toolchain script path
local scriptpath = nil
for _, dir in ipairs(toolchain.directories()) do
scriptpath = path.join(dir, name, "xmake.lua")
if os.isfile(scriptpath) then
break
end
end
if not scriptpath or not os.isfile(scriptpath) then
return nil, string.format("the toolchain %s not found!", name)
end
-- get interpreter
local interp = toolchain._interpreter()
-- load script
local ok, errors = interp:load(scriptpath)
if not ok then
return nil, errors
end
-- load toolchain
local results, errors = interp:make("toolchain", true, false)
if not results and os.isfile(scriptpath) then
return nil, errors
end
-- check the toolchain name
local result = results[name]
if not result then
return nil, string.format("the toolchain %s not found!", name)
end
-- save instance to the cache
instance = _instance.new(name, result, cachekey, true, opt)
cache:set(cachekey, instance)
return instance
end
-- load toolchain from the give toolchain info
function toolchain.load_withinfo(name, info, opt)
-- get toolchain name and packages
opt = opt or {}
local packages
name, packages = toolchain.parsename(name)
opt.packages = opt.packages or packages
-- get cache key
opt.plat = opt.plat or config.get("plat") or os.host()
opt.arch = opt.arch or config.get("arch") or os.arch()
local cache = toolchain._memcache()
local cachekey = toolchain._cachekey(name, opt)
-- get it directly from cache dirst
local instance = cache:get(cachekey)
if instance then
return instance
end
-- save instance to the cache
instance = _instance.new(name, info, cachekey, false, opt)
cache:set(cachekey, instance)
return instance
end
-- load toolchain from file
function toolchain.load_fromfile(filepath, opt)
local fileinfo, errors = io.load(filepath)
if not fileinfo then
return nil, errors
end
if not fileinfo.name or not fileinfo.info then
return nil, string.format("%s is invalid toolchain info file!", filepath)
end
opt = table.join(opt or {}, fileinfo.configs)
opt.cachekey = fileinfo.cachekey
local scope_opt = {interpreter = toolchain._interpreter(), deduplicate = true, enable_filter = true}
local info = scopeinfo.new("toolchain", fileinfo.info, scope_opt)
local instance = toolchain.load_withinfo(fileinfo.name, info, opt)
return instance
end
-- get the program and name of the given tool kind
function toolchain.tool(toolchains, toolkind, opt)
-- get plat and arch
opt = opt or {}
local plat = opt.plat or config.get("plat") or os.host()
local arch = opt.arch or config.get("arch") or os.arch()
-- get cache and cachekey
local cache = toolchain._localcache()
local cachekey = "tool_" .. (opt.cachekey or "") .. "_" .. plat .. "_" .. arch .. "_" .. toolkind
local updatecache = false
-- get program from before_script
local program, toolname, toolchain_info
local before_get = opt.before_get
if before_get then
program, toolname, toolchain_info = before_get(toolkind)
if program then
updatecache = true
end
end
-- get program from local cache
if not program then
program = cache:get2(cachekey, "program")
toolname = cache:get2(cachekey, "toolname")
toolchain_info = cache:get2(cachekey, "toolchain_info")
end
-- get program from toolchains
if not program then
for idx, toolchain_inst in ipairs(toolchains) do
program, toolname = toolchain_inst:tool(toolkind)
if program then
toolchain_info = {name = toolchain_inst:name(),
plat = toolchain_inst:plat(),
arch = toolchain_inst:arch(),
cachekey = toolchain_inst:cachekey()}
updatecache = true
break
end
end
end
-- contain toolname? parse it, e.g. '[email protected]'
if program and type(program) == "string" then
local pos = program:find('@', 1, true)
if pos then
-- we need to ignore valid path with `@`, e.g. /usr/local/opt/[email protected]/bin/go
-- https://github.com/xmake-io/xmake/issues/2853
local prefix = program:sub(1, pos - 1)
if prefix and not prefix:find("[/\\]") then
toolname = prefix
program = program:sub(pos + 1)
end
updatecache = true
end
end
-- update cache
if program and updatecache then
cache:set2(cachekey, "program", program)
cache:set2(cachekey, "toolname", toolname)
cache:set2(cachekey, "toolchain_info", toolchain_info)
cache:save()
end
return program, toolname, toolchain_info
end
-- get tool configuration from the toolchains
function toolchain.toolconfig(toolchains, name, opt)
-- get plat and arch
opt = opt or {}
local plat = opt.plat or config.get("plat") or os.host()
local arch = opt.arch or config.get("arch") or os.arch()
-- get cache and cachekey
local cache = toolchain._memcache()
local cachekey = "toolconfig_" .. (opt.cachekey or "") .. "_" .. plat .. "_" .. arch
-- get configuration
local toolconfig = cache:get2(cachekey, name)
if toolconfig == nil then
for _, toolchain_inst in ipairs(toolchains) do
if not toolchain_inst:_is_checked() then
utils.warning("we cannot get toolconfig(%s) in toolchain(%s) with %s/%s, because it has been not checked yet!", name, toolchain_inst:name(), toolchain_inst:plat(), toolchain_inst:arch())
end
local values = toolchain_inst:get(name)
if values then
toolconfig = toolconfig or {}
table.join2(toolconfig, values)
end
local after_get = opt.after_get
if after_get then
values = after_get(toolchain_inst, name)
if values then
toolconfig = toolconfig or {}
table.join2(toolconfig, values)
end
end
end
cache:set2(cachekey, name, toolconfig or false)
end
return toolconfig or nil
end
-- return module
return toolchain
|
0 | repos/xmake/xmake/core | repos/xmake/xmake/core/tool/linker.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file linker.lua
--
-- define module
local linker = linker or {}
-- load modules
local io = require("base/io")
local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local string = require("base/string")
local option = require("base/option")
local profiler = require("base/profiler")
local config = require("project/config")
local sandbox = require("sandbox/sandbox")
local language = require("language/language")
local platform = require("platform/platform")
local tool = require("tool/tool")
local builder = require("tool/builder")
local compiler = require("tool/compiler")
-- add flags from the toolchains
function linker:_add_flags_from_toolchains(flags, targetkind, target)
-- attempt to add special lanugage flags first for target kind, e.g. binary.go.gcldflags, static.dcarflags
if targetkind then
local toolkind = self:kind()
local toolname = self:name()
if target and target.toolconfig then
for _, flagkind in ipairs(self:_flagkinds()) do
local toolflags = target:toolconfig(targetkind .. '.' .. toolname .. '.' .. toolkind .. 'flags') or target:toolconfig(targetkind .. '.' .. toolname .. '.' .. flagkind)
table.join2(flags, toolflags or target:toolconfig(targetkind .. '.' .. toolkind .. 'flags') or target:toolconfig(targetkind .. '.' .. flagkind))
end
else
for _, flagkind in ipairs(self:_flagkinds()) do
local toolflags = platform.toolconfig(targetkind .. '.' .. toolname .. '.' .. toolkind .. 'flags') or platform.toolconfig(targetkind .. '.' .. toolname .. '.' .. flagkind)
table.join2(flags, toolflags or platform.toolconfig(targetkind .. '.' .. toolkind .. 'flags') or platform.toolconfig(targetkind .. '.' .. flagkind))
end
end
end
end
-- add flags from the linker
function linker:_add_flags_from_linker(flags)
local toolkind = self:kind()
for _, flagkind in ipairs(self:_flagkinds()) do
-- attempt to add special lanugage flags first, e.g. gcldflags, dcarflags
table.join2(flags, self:get(toolkind .. 'flags') or self:get(flagkind))
end
end
-- load tool
function linker._load_tool(targetkind, sourcekinds, target)
-- get the linker infos
local linkerinfos, errors = language.linkerinfos_of(targetkind, sourcekinds)
if not linkerinfos then
return nil, errors
end
-- select the linker
local linkerinfo = nil
local linkertool = nil
local firsterror = nil
for _, _linkerinfo in ipairs(linkerinfos) do
-- get program from target
local program, toolname, toolchain_info
if target and target.tool then
program, toolname, toolchain_info = target:tool(_linkerinfo.linkerkind)
end
-- load the linker tool from the linker kind (with cache)
linkertool, errors = tool.load(_linkerinfo.linkerkind, {program = program, toolname = toolname, toolchain_info = toolchain_info})
if linkertool then
linkerinfo = _linkerinfo
linkerinfo.program = program
break
else
firsterror = firsterror or errors
end
end
if not linkerinfo then
return nil, firsterror
end
return linkertool, linkerinfo
end
-- load the linker from the given target kind
function linker.load(targetkind, sourcekinds, target)
assert(sourcekinds)
-- wrap sourcekinds first
sourcekinds = table.wrap(sourcekinds)
if #sourcekinds == 0 then
-- we need to detect the sourcekinds of all deps if the current target has not any source files
for _, dep in ipairs(target:orderdeps()) do
table.join2(sourcekinds, dep:sourcekinds())
end
if #sourcekinds > 0 then
sourcekinds = table.unique(sourcekinds)
end
end
-- load linker tool first (with cache)
local linkertool, linkerinfo_or_errors = linker._load_tool(targetkind, sourcekinds, target)
if not linkertool then
return nil, linkerinfo_or_errors
end
-- get linker info
local linkerinfo = linkerinfo_or_errors
-- init cache key
local plat = linkertool:plat() or config.plat() or os.host()
local arch = linkertool:arch() or config.arch() or os.arch()
local cachekey = targetkind .. "_" .. linkerinfo.linkerkind .. (linkerinfo.program or "") .. plat .. arch
cachekey = cachekey .. table.concat(sourcekinds, "") -- @see https://github.com/xmake-io/xmake/issues/5360
-- get it directly from cache dirst
builder._INSTANCES = builder._INSTANCES or {}
local instance = builder._INSTANCES[cachekey]
if not instance then
-- new instance
instance = table.inherit(linker, builder)
-- save linker tool
instance._TOOL = linkertool
-- load the name flags of archiver
local nameflags = {}
local nameflags_exists = {}
for _, sourcekind in ipairs(sourcekinds) do
-- load language
local result, errors = language.load_sk(sourcekind)
if not result then
return nil, errors
end
-- merge name flags
for _, flaginfo in ipairs(table.wrap(result:nameflags()[targetkind])) do
local key = flaginfo[1] .. flaginfo[2]
if not nameflags_exists[key] then
table.insert(nameflags, flaginfo)
nameflags_exists[key] = flaginfo
end
end
end
instance._NAMEFLAGS = nameflags
-- init target (optional)
instance._TARGET = target
-- init target kind
instance._TARGETKIND = targetkind
-- init flag kinds
instance._FLAGKINDS = {linkerinfo.linkerflag}
-- add toolchains flags to the linker tool
-- add special lanugage flags first, e.g. go.gcldflags or gcc.ldflags or gcldflags or ldflags
local toolkind = linkertool:kind()
local toolname = linkertool:name()
if target and target.toolconfig then
for _, flagkind in ipairs(instance:_flagkinds()) do
linkertool:add(toolkind .. 'flags', target:toolconfig(toolname .. '.' .. toolkind .. 'flags') or target:toolconfig(toolkind .. 'flags'))
linkertool:add(flagkind, target:toolconfig(toolname .. '.' .. flagkind) or target:toolconfig(flagkind))
end
else
for _, flagkind in ipairs(instance:_flagkinds()) do
linkertool:add(toolkind .. 'flags', platform.toolconfig(toolname .. '.' .. toolkind .. 'flags') or platform.toolconfig(toolkind .. 'flags'))
linkertool:add(flagkind, platform.toolconfig(toolname .. '.' .. flagkind) or platform.toolconfig(flagkind))
end
end
-- @note we can't call _load_once before caching the instance,
-- it may call has_flags to trigger the concurrent scheduling.
--
-- this will result in more compiler/linker instances being created at the same time,
-- and they will access the same tool instance at the same time.
--
-- @see https://github.com/xmake-io/xmake/issues/3429
builder._INSTANCES[cachekey] = instance
end
-- we need to load it at the end because in tool.load().
-- because we may need to call has_flags, which requires the full platform toolchain flags
local ok, errors = linkertool:_load_once()
if not ok then
return nil, errors
end
return instance
end
-- link the target file
function linker:link(objectfiles, targetfile, opt)
opt = opt or {}
local linkflags = opt.linkflags or self:linkflags(opt)
opt = table.copy(opt)
opt.target = self:target()
profiler:enter(self:name(), "link", targetfile)
local ok, errors = sandbox.load(self:_tool().link, self:_tool(), table.wrap(objectfiles), self:_targetkind(), targetfile, linkflags, opt)
profiler:leave(self:name(), "link", targetfile)
return ok, errors
end
-- get the link arguments list
function linker:linkargv(objectfiles, targetfile, opt)
return self:_tool():linkargv(table.wrap(objectfiles), self:_targetkind(), targetfile, opt.linkflags or self:linkflags(opt), opt)
end
-- get the link command
function linker:linkcmd(objectfiles, targetfile, opt)
return os.args(table.join(self:linkargv(objectfiles, targetfile, opt)))
end
-- get the link flags
--
-- @param opt the argument options (contain all the linker attributes of target),
-- e.g. {target = ..., targetkind = "static", configs = {ldflags = "", links = "", linkdirs = "", ...}}
--
function linker:linkflags(opt)
-- init options
opt = opt or {}
-- get target
local target = opt.target
-- get target kind
local targetkind = opt.targetkind
if not targetkind and target and target:type() == "target" then
targetkind = target:kind()
end
-- add flags from linker/toolchains
--
-- we need to add toolchain flags at the beginning to allow users to override them.
-- but includedirs/links/syslinks/linkdirs will still be placed last, they are in the order defined in languages/xmake.lua
--
-- @see https://github.com/xmake-io/xmake/issues/978
--
local flags = {}
self:_add_flags_from_linker(flags)
self:_add_flags_from_toolchains(flags, targetkind, target)
-- add flags from user configuration
self:_add_flags_from_config(flags)
-- add flags from target
self:_add_flags_from_target(flags, target)
-- add flags from argument
local configs = opt.configs or opt.config
if configs then
self:_add_flags_from_argument(flags, target, configs)
end
-- preprocess flags
return self:_preprocess_flags(flags)
end
-- return module
return linker
|
0 | repos/xmake/xmake/core | repos/xmake/xmake/core/tool/tool.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific tool governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file tool.lua
--
-- define module
local tool = tool or {}
local _instance = _instance or {}
-- load modules
local os = require("base/os")
local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local string = require("base/string")
local config = require("project/config")
local sandbox = require("sandbox/sandbox")
local toolchain = require("tool/toolchain")
local platform = require("platform/platform")
local language = require("language/language")
local is_cross = require("base/private/is_cross")
local import = require("sandbox/modules/import")
-- new an instance
function _instance.new(kind, name, program, plat, arch, toolchain_inst)
-- import "core.tools.xxx"
local toolclass = nil
if os.isfile(path.join(os.programdir(), "modules", "core", "tools", name .. ".lua")) then
toolclass = import("core.tools." .. name, {nocache = true}) -- @note we need to create a tool instance with unique toolclass context (_g)
end
-- not found?
if not toolclass then
return nil, string.format("cannot import \"core.tool.%s\" module!", name)
end
-- new an instance
local instance = table.inherit(_instance, toolclass)
-- save name, kind and program
instance._NAME = name
instance._KIND = kind
instance._PROGRAM = program
instance._PLAT = plat
instance._ARCH = arch
instance._TOOLCHAIN = toolchain_inst
instance._INFO = {}
-- init instance
if instance.init then
local ok, errors = sandbox.load(instance.init, instance)
if not ok then
return nil, errors
end
end
return instance
end
-- get the tool name
function _instance:name()
return self._NAME
end
-- get the tool kind
function _instance:kind()
return self._KIND
end
-- get the tool platform
function _instance:plat()
return self._PLAT
end
-- get the tool architecture
function _instance:arch()
return self._ARCH
end
-- the current target is belong to the given platforms?
function _instance:is_plat(...)
local plat = self:plat()
for _, v in ipairs(table.join(...)) do
if v and plat == v then
return true
end
end
end
-- the current target is belong to the given architectures?
function _instance:is_arch(...)
local arch = self:arch()
for _, v in ipairs(table.join(...)) do
if v and arch:find("^" .. v:gsub("%-", "%%-") .. "$") then
return true
end
end
end
-- is cross-compilation?
function _instance:is_cross()
return is_cross(self:plat(), self:arch())
end
-- get the tool program
function _instance:program()
return self._PROGRAM
end
-- get the toolchain of this tool
function _instance:toolchain()
return self._TOOLCHAIN
end
-- get run environments
function _instance:runenvs()
return self:toolchain() and self:toolchain():runenvs()
end
-- set the value to the platform info
function _instance:set(name, ...)
self._INFO[name] = table.unwrap({...})
end
-- add the value to the platform info
function _instance:add(name, ...)
local info = table.wrap(self._INFO[name])
self._INFO[name] = table.unwrap(table.join(info, ...))
end
-- get the platform configure
function _instance:get(name)
if self._super_get then
local value = self:_super_get(name)
if value ~= nil then
return value
end
end
return self._INFO[name]
end
-- has the given flag?
function _instance:has_flags(flags, flagkind, opt)
-- init options
opt = opt or {}
opt.program = opt.program or self:program()
opt.toolkind = opt.toolkind or self:kind()
opt.flagkind = opt.flagkind or flagkind
opt.sysflags = opt.sysflags or self:_sysflags(opt.toolkind, opt.flagkind)
-- import has_flags()
self._has_flags = self._has_flags or import("lib.detect.has_flags", {anonymous = true})
-- bind the run environments
opt.envs = self:runenvs()
-- has flags?
return self._has_flags(self:name(), flags, opt)
end
-- load tool only once
function _instance:_load_once()
if not self._LOADED then
if self.load then
local ok, errors = sandbox.load(self.load, self)
if not ok then
return false, errors
end
end
self._LOADED = true
end
return true
end
-- get system flags from toolchains
-- @see https://github.com/xmake-io/xmake/issues/3429
function _instance:_sysflags(toolkind, flagkind)
local sysflags = {}
local sourceflags = language.sourceflags()[toolkind]
if not sourceflags and flagkind then
sourceflags = {}
if flagkind == "cflags" or flagkind == "cxxflags" then
table.insert(sourceflags, flagkind)
table.insert(sourceflags, "cxflags")
elseif flagkind == "cxflags" then
table.insert(sourceflags, flagkind)
table.insert(sourceflags, "cxxflags")
elseif flagkind == "mflags" or flagkind == "mxxflags" then
table.insert(sourceflags, flagkind)
table.insert(sourceflags, "mxflags")
elseif flagkind == "mxflags" then
table.insert(sourceflags, flagkind)
table.insert(sourceflags, "mxxflags")
else
-- flagkind may be ldflags, we need to ignore it
-- and we should use more precise flagkind, e.g. rcldflags instead of ldflags
end
end
if sourceflags then
for _, flagname in ipairs(table.wrap(sourceflags)) do
local flags = self:get(flagname)
if flags then
table.join2(sysflags, flags)
end
end
end
-- maybe it's linker flags, ld -> ldflags, dcld -> dcldflags
if #sysflags == 0 then
table.join2(sysflags, self:get(toolkind .. "flags"))
end
if #sysflags > 0 then
return sysflags
end
end
-- load the given tool from the given kind
--
-- @param kind the tool kind e.g. cc, cxx, mm, mxx, as, ar, ld, sh, ..
-- @param opt.program the tool program, e.g. /xxx/arm-linux-gcc, [email protected], (optional)
-- @param opt.toolname gcc, clang, .. (optional)
-- @param opt.toolchain_info the toolchain info (optional)
--
function tool.load(kind, opt)
-- get tool information
opt = opt or {}
local program = opt.program
local toolname = opt.toolname
local toolchain_info = opt.toolchain_info or {}
-- get platform and architecture
local plat = toolchain_info.plat or config.get("plat") or os.host()
local arch = toolchain_info.arch or config.get("arch") or os.arch()
-- init cachekey
local cachekey = kind .. (program or "") .. plat .. arch
-- get it directly from cache dirst
tool._TOOLS = tool._TOOLS or {}
if tool._TOOLS[cachekey] then
return tool._TOOLS[cachekey]
end
-- contain toolname? parse it, e.g. '[email protected]'
if program then
local pos = program:find('@', 1, true)
if pos then
-- we need to ignore valid path with `@`, e.g. /usr/local/opt/[email protected]/bin/go
-- https://github.com/xmake-io/xmake/issues/2853
local prefix = program:sub(1, pos - 1)
if prefix and not prefix:find("[/\\]") then
toolname = prefix
program = program:sub(pos + 1)
end
end
end
-- get the tool program and name
if not program then
program, toolname, toolchain_info = platform.tool(kind, plat, arch)
if toolchain_info then
assert(toolchain_info.plat == plat)
assert(toolchain_info.arch == arch)
end
end
if not program then
return nil, string.format("cannot get program for %s", kind)
end
-- import find_toolname()
tool._find_toolname = tool._find_toolname or import("lib.detect.find_toolname")
-- get the tool name from the program
local ok, name_or_errors = sandbox.load(tool._find_toolname, toolname or program, {program = program})
if not ok then
return nil, name_or_errors
end
-- get name
local name = name_or_errors
if not name then
return nil, string.format("cannot find known tool script for %s", toolname or program)
end
-- load toolchain instance
local toolchain_inst
if toolchain_info and toolchain_info.name then
toolchain_inst = toolchain.load(toolchain_info.name, {plat = plat, arch = arch, cachekey = toolchain_info.cachekey})
end
-- new an instance
local instance, errors = _instance.new(kind, name, program, plat, arch, toolchain_inst)
if not instance then
return nil, errors
end
tool._TOOLS[cachekey] = instance
return instance
end
-- return module
return tool
|
0 | repos/xmake/xmake/core | repos/xmake/xmake/core/sandbox/sandbox.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file sandbox.lua
--
-- define module
local sandbox = sandbox or {}
-- load modules
local os = require("base/os")
local path = require("base/path")
local table = require("base/table")
local utils = require("base/utils")
local string = require("base/string")
local option = require("base/option")
-- traceback
function sandbox._traceback(errors)
-- no diagnosis info?
if not option.get("diagnosis") then
if errors then
-- remove the prefix info
local _, pos = errors:find(":%d+: ")
if pos then
errors = errors:sub(pos + 1)
end
end
return errors
end
-- traceback exists?
if errors and errors:find("stack traceback:", 1, true) then
return errors
end
-- init results
local results = ""
if errors then
results = errors .. "\n"
end
results = results .. "stack traceback:\n"
-- make results
local level = 2
while true do
-- get debug info
local info = debug.getinfo(level, "Sln")
-- end?
if not info then
break
end
-- function?
if info.what == "C" then
results = results .. string.format(" [C]: in function '%s'\n", info.name)
elseif info.name then
results = results .. string.format(" [%s:%d]: in function '%s'\n", info.short_src, info.currentline, info.name)
elseif info.what == "main" then
results = results .. string.format(" [%s:%d]: in main chunk\n", info.short_src, info.currentline)
break
else
results = results .. string.format(" [%s:%d]:\n", info.short_src, info.currentline)
end
-- next
level = level + 1
end
-- ok?
return results
end
-- register api for builtin
function sandbox._api_register_builtin(self, name, func)
assert(self and self._PUBLIC and func)
self._PUBLIC[name] = func
end
-- new a sandbox instance
function sandbox._new()
-- init an sandbox instance
local instance = {_PUBLIC = {}, _PRIVATE = {}}
-- inherit the interfaces of sandbox
table.inherit2(instance, sandbox)
-- register the builtin modules
instance:_api_register_builtin("_g", {})
for module_name, module in pairs(sandbox.builtin_modules()) do
instance:_api_register_builtin(module_name, module)
end
-- bind instance to the public script envirnoment
instance:bind(instance._PUBLIC)
-- ok?
return instance
end
-- new a sandbox instance with the given script
function sandbox.new(script, filter, rootdir)
-- check
assert(script)
-- new instance
local self = sandbox._new()
-- check
assert(self and self._PUBLIC and self._PRIVATE)
-- save filter
self._PRIVATE._FILTER = filter
-- save root directory
self._PRIVATE._ROOTDIR = rootdir
-- invalid script?
if type(script) ~= "function" then
return nil, "invalid script!"
end
-- bind public scope
setfenv(script, self._PUBLIC)
-- save script
self._PRIVATE._SCRIPT = script
return self
end
-- load script in the sandbox
function sandbox.load(script, ...)
return utils.trycall(script, sandbox._traceback, ...)
end
-- bind self instance to the given script or envirnoment
function sandbox:bind(script_or_env)
-- get envirnoment
local env = script_or_env
if type(script_or_env) == "function" then
env = getfenv(script_or_env)
end
-- bind instance to the script envirnoment
setmetatable(env, { __index = function (tbl, key)
if type(key) == "string" and key == "_SANDBOX" and rawget(tbl, "_SANDBOX_READABLE") then
return self
end
return rawget(tbl, key)
end
, __newindex = function (tbl, key, val)
if type(key) == "string" and (key == "_SANDBOX" or key == "_SANDBOX_READABLE") then
return
end
rawset(tbl, key, val)
end})
-- ok
return script_or_env
end
-- fork a new sandbox from the self sandbox
function sandbox:fork(script, rootdir)
-- invalid script?
if script ~= nil and type(script) ~= "function" then
return nil, "invalid script!"
end
-- init a new sandbox instance
local instance = sandbox._new()
-- check
assert(instance and instance._PUBLIC and instance._PRIVATE)
-- inherit the filter
instance._PRIVATE._FILTER = self:filter()
-- inherit the root directory
instance._PRIVATE._ROOTDIR = rootdir or self:rootdir()
-- bind public scope
if script then
setfenv(script, instance._PUBLIC)
instance._PRIVATE._SCRIPT = script
end
-- ok?
return instance
end
-- load script and module
function sandbox:module()
-- this module has been loaded?
if self._PRIVATE._MODULE then
return self._PRIVATE._MODULE
end
-- backup the scope variables first
local scope_public = getfenv(self:script())
local scope_backup = {}
table.copy2(scope_backup, scope_public)
-- load module with sandbox
local ok, errors = sandbox.load(self:script())
if not ok then
return nil, errors
end
-- only export new public functions
local module = {}
for k, v in pairs(scope_public) do
if type(v) == "function" and not k:startswith("_") and scope_backup[k] == nil then
module[k] = v
end
end
self._PRIVATE._MODULE = module
return module
end
-- get script from the given sandbox
function sandbox:script()
assert(self and self._PRIVATE)
return self._PRIVATE._SCRIPT
end
-- get filter from the given sandbox
function sandbox:filter()
assert(self and self._PRIVATE)
return self._PRIVATE._FILTER
end
-- get root directory from the given sandbox
function sandbox:rootdir()
assert(self and self._PRIVATE)
return self._PRIVATE._ROOTDIR
end
-- get current instance in the sandbox modules
function sandbox.instance(script)
-- get the sandbox instance from the given script
local instance = nil
if script then
local scope = getfenv(script)
if scope then
-- enable to read _SANDBOX
rawset(scope, "_SANDBOX_READABLE", true)
-- attempt to get it
instance = scope._SANDBOX
-- disable to read _SANDBOX
rawset(scope, "_SANDBOX_READABLE", nil)
end
if instance then return instance end
end
-- find self instance for the current sandbox
local level = 2
while level < 32 do
-- get scope
local ok, scope = pcall(getfenv, level)
if not ok then
break;
end
if scope then
-- enable to read _SANDBOX
rawset(scope, "_SANDBOX_READABLE", true)
-- attempt to get it
instance = scope._SANDBOX
-- disable to read _SANDBOX
rawset(scope, "_SANDBOX_READABLE", nil)
end
-- found?
if instance then
break
end
-- next
level = level + 1
end
return instance
end
-- get builtin modules
function sandbox.builtin_modules()
local builtin_modules = sandbox._BUILTIN_MODULES
if builtin_modules == nil then
builtin_modules = {}
local builtin_module_files = os.files(path.join(os.programdir(), "core/sandbox/modules/*.lua"))
if builtin_module_files then
for _, builtin_module_file in ipairs(builtin_module_files) do
local module_name = path.basename(builtin_module_file)
assert(module_name)
local script, errors = loadfile(builtin_module_file)
if script then
local ok, results = utils.trycall(script)
if not ok then
os.raise(results)
end
builtin_modules[module_name] = results
else
os.raise(errors)
end
end
end
sandbox._BUILTIN_MODULES = builtin_modules
end
return builtin_modules
end
-- return module
return sandbox
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/table.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file table.lua
--
-- load module
return require("base/table")
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/linuxos.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file linuxos.lua
--
-- load modules
local linuxos = require("base/linuxos")
local raise = require("sandbox/modules/raise")
-- define module
local sandbox_linuxos = sandbox_linuxos or {}
-- get linux system name
function sandbox_linuxos.name()
local name = linuxos.name()
if not name then
raise("cannot get the system name of the current linux!")
end
return name
end
-- get linux system version
function sandbox_linuxos.version()
local linuxver = linuxos.version()
if not linuxver then
raise("cannot get the system version of the current linux!")
end
return linuxver
end
-- get linux kernel version
function sandbox_linuxos.kernelver()
local kernelver = linuxos.kernelver()
if not kernelver then
raise("cannot get the kernel version of the current linux!")
end
return kernelver
end
-- return module
return sandbox_linuxos
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/ipairs.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file ipairs.lua
--
-- load modules
local table = require("base/table")
-- improve ipairs, wrap nil and single value
function sandbox_ipairs(t)
-- exists the custom ipairs?
local is_table = type(t) == "table"
if is_table and t.ipairs then
return t:ipairs()
end
-- wrap table and return iterator
if not is_table then
t = t ~= nil and {t} or {}
end
return function (t, i)
i = i + 1
local v = t[i]
if v ~= nil then
return i, v
end
end, t, 0
end
-- load module
return sandbox_ipairs
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/unpack.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file table.unpack.lua
--
-- load module
return require("base/table").unpack
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/debug.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file debug.lua
--
-- load modules
local table = require("base/table")
-- define module
local sandbox_debug = sandbox_debug or table.join(debug)
sandbox_debug.rawget = rawget
sandbox_debug.rawset = rawset
sandbox_debug.rawequal = rawequal
sandbox_debug.rawlen = rawlen
sandbox_debug.require = require
sandbox_debug.collectgarbage = collectgarbage
function sandbox_debug.global(key)
if key == nil then
return _G
end
return _G[key]
end
-- return module
return sandbox_debug
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/dprintf.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file dprintf.lua
--
-- load module
return require("sandbox/modules/utils").dprintf
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/string.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file string.lua
--
-- load modules
local utils = require("base/utils")
local string = require("base/string")
local sandbox = require("sandbox/sandbox")
-- define module
local sandbox_string = sandbox_string or {}
-- inherit the public interfaces of string
for k, v in pairs(string) do
if not k:startswith("_") and type(v) == "function" then
sandbox_string[k] = v
end
end
-- format string with the builtin variables
function sandbox_string.vformat(format, ...)
-- check
assert(format)
-- get the current sandbox instance
local instance = sandbox.instance()
assert(instance)
-- format string if exists arguments
local result = format
if #{...} > 0 then
-- escape "%$", "%(", "%)", "%%" to '$', '(', ')', '%%'
format = format:gsub("%%([%$%(%)%%])", function (ch) return ch ~= "%" and ("%%" .. ch) or "%%%%" end)
-- try to format it
result = string.format(format, ...)
end
assert(result)
-- get filter from the current sandbox
local filter = instance:filter()
if filter then
result = filter:handle(result)
end
-- ok?
return result
end
-- return module
return sandbox_string
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/tostring.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file tostring.lua
--
-- load module
return tostring
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/type.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file type.lua
--
-- load module
return type
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/is_arch.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file is_arch.lua
--
-- return module
return require("project/config").is_arch
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/print.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file print.lua
--
-- load module
return require("sandbox/modules/utils").print
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/vprint.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file vprint.lua
--
-- load module
return require("sandbox/modules/utils").vprint
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/utils.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file utils.lua
--
-- load modules
local io = require("base/io")
local os = require("base/os")
local utils = require("base/utils")
local colors = require("base/colors")
local option = require("base/option")
local log = require("base/log")
local deprecated = require("base/deprecated")
local try = require("sandbox/modules/try")
local catch = require("sandbox/modules/catch")
local vformat = require("sandbox/modules/vformat")
-- define module
local sandbox_utils = sandbox_utils or {}
-- inherit some builtin interfaces
sandbox_utils.dump = utils.dump -- do not change to a function call to utils.dump since debug.getinfo is called in utils.dump to get caller info
sandbox_utils.confirm = utils.confirm
sandbox_utils.error = utils.error
sandbox_utils.warning = utils.warning
sandbox_utils.trycall = utils.trycall
-- print each arguments
function sandbox_utils._print(...)
local args = {}
for _, arg in ipairs({...}) do
if type(arg) == "string" then
table.insert(args, vformat(arg))
else
table.insert(args, arg)
end
end
utils._print(table.unpack(args))
log:printv(table.unpack(args))
end
-- print format string with newline
-- print builtin-variables with $(var)
-- print multi-variables with raw lua action
--
function sandbox_utils.print(format, ...)
if type(format) == "string" and format:find("%", 1, true) then
local args = {...}
try {
function ()
local message = vformat(format, table.unpack(args))
utils._print(message)
log:printv(message)
end,
catch {
function (errors)
sandbox_utils._print(format, table.unpack(args))
end
}
}
else
sandbox_utils._print(format, ...)
end
end
-- print format string and the builtin variables without newline
function sandbox_utils.printf(format, ...)
local message = vformat(format, ...)
utils._iowrite(message)
log:write(message)
end
-- print format string, the builtin variables and colors with newline
function sandbox_utils.cprint(format, ...)
local message = vformat(format, ...)
utils._print(colors.translate(message))
if log:file() then
log:printv(colors.ignore(message))
end
end
-- print format string, the builtin variables and colors without newline
function sandbox_utils.cprintf(format, ...)
local message = vformat(format, ...)
utils._iowrite(colors.translate(message))
if log:file() then
log:write(colors.ignore(message))
end
end
-- print the verbose information
function sandbox_utils.vprint(format, ...)
if option.get("verbose") then
sandbox_utils.print(format, ...)
end
end
-- print the verbose information without newline
function sandbox_utils.vprintf(format, ...)
if option.get("verbose") then
sandbox_utils.printf(format, ...)
end
end
-- print the diagnosis information
function sandbox_utils.dprint(format, ...)
if option.get("diagnosis") then
sandbox_utils.print(format, ...)
end
end
-- print the diagnosis information without newline
function sandbox_utils.dprintf(format, ...)
if option.get("diagnosis") then
sandbox_utils.printf(format, ...)
end
end
-- print the warning information
function sandbox_utils.wprint(format, ...)
utils.warning(vformat(format, ...))
end
-- assert
function sandbox_utils.assert(value, format, ...)
if not value then
if format ~= nil then
os.raiselevel(2, format, ...)
else
os.raiselevel(2, "assertion failed!")
end
end
return value
end
-- return module
return sandbox_utils
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/is_plat.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file is_plat.lua
--
-- return module
return require("project/config").is_plat
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/find_packages.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file find_packages.lua
--
-- return module
return function (...)
-- get find_package
local find_package = require("sandbox/modules/import/core/sandbox/module").import("lib.detect.find_package", {anonymous = true})
-- get packages and options
local pkgs = {...}
local opts = pkgs[#pkgs]
if type(opts) == "table" then
pkgs = table.slice(pkgs, 1, #pkgs - 1)
else
opts = {}
end
-- find all packages
local packages = {}
for _, pkgname in ipairs(pkgs) do
local pkg = find_package(pkgname, opts)
if pkg then
table.insert(packages, pkg)
end
end
return packages
end
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/is_config.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file is_config.lua
--
-- return module
return require("project/config").is_value
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/dprint.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file dprint.lua
--
-- load module
return require("sandbox/modules/utils").dprint
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/assert.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file assert.lua
--
-- load module
return require("sandbox/modules/utils").assert
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/printf.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file printf.lua
--
-- load module
return require("sandbox/modules/utils").printf
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/macos.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file macos.lua
--
-- load modules
local macos = require("base/macos")
local raise = require("sandbox/modules/raise")
-- define module
local sandbox_macos = sandbox_macos or {}
-- get system version
function sandbox_macos.version()
local winver = macos.version()
if not winver then
raise("cannot get the version of the current macos!")
end
return winver
end
-- return module
return sandbox_macos
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/pairs.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file pairs.lua
--
-- load modules
local table = require("base/table")
-- improve pairs, wrap nil/single value
function sandbox_pairs(t)
-- exists the custom ipairs?
local is_table = type(t) == "table"
if is_table and t.pairs then
return t:pairs()
end
-- wrap table and return iterator
if not is_table then
t = t ~= nil and {t} or {}
end
return function (t, i)
return next(t, i)
end, t, nil
end
-- load module
return sandbox_pairs
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/find_package.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file find_package.lua
--
-- return module
return function (name, opt)
local find_package = require("sandbox/modules/import/core/sandbox/module").import("lib.detect.find_package", {anonymous = true})
return find_package(name, opt)
end
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/inherit.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file inherit.lua
--
-- load modules
return require("sandbox/modules/import/core/sandbox/module").inherit
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/hash.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file hash.lua
--
-- load modules
local hash = require("base/hash")
local raise = require("sandbox/modules/raise")
-- define module
local sandbox_hash = sandbox_hash or {}
-- make a new uuid
function sandbox_hash.uuid(name)
return sandbox_hash.uuid4(name)
end
-- make a new uuid v4
function sandbox_hash.uuid4(name)
local uuid = hash.uuid4(name)
if not uuid then
raise("cannot make uuid %s", name)
end
return uuid
end
-- make sha1 from the given file or data
function sandbox_hash.sha1(file_or_data)
local sha1, errors = hash.sha1(file_or_data)
if not sha1 then
raise("cannot make sha1 for %s, %s", file_or_data, errors or "unknown errors")
end
return sha1
end
-- make sha256 from the given file or data
function sandbox_hash.sha256(file_or_data)
local sha256, errors = hash.sha256(file_or_data)
if not sha256 then
raise("cannot make sha256 for %s, %s", file_or_data, errors or "unknown errors")
end
return sha256
end
-- make md5 from the given file or data
function sandbox_hash.md5(file_or_data)
local md5, errors = hash.md5(file_or_data)
if not md5 then
raise("cannot make md5 for %s, %s", file_or_data, errors or "unknown errors")
end
return md5
end
-- make xxhash64 from the given file or data
function sandbox_hash.xxhash64(file_or_data)
local xxhash64, errors = hash.xxhash64(file_or_data)
if not xxhash64 then
raise("cannot make xxhash64 for %s, %s", file_or_data, errors or "unknown errors")
end
return xxhash64
end
-- make xxhash128 from the given file or data
function sandbox_hash.xxhash128(file_or_data)
local xxhash128, errors = hash.xxhash128(file_or_data)
if not xxhash128 then
raise("cannot make xxhash128 for %s, %s", file_or_data, errors or "unknown errors")
end
return xxhash128
end
-- return module
return sandbox_hash
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/winos.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file winos.lua
--
-- load modules
local winos = require("base/winos")
local raise = require("sandbox/modules/raise")
-- define module
local sandbox_winos = sandbox_winos or {}
-- inherit some builtin interfaces
sandbox_winos.oem_cp = winos.oem_cp
sandbox_winos.ansi_cp = winos.ansi_cp
sandbox_winos.cp_info = winos.cp_info
sandbox_winos.console_cp = winos.console_cp
sandbox_winos.console_output_cp = winos.console_output_cp
sandbox_winos.logical_drives = winos.logical_drives
sandbox_winos.cmdargv = winos.cmdargv
sandbox_winos.inherit_handles_safely = winos.inherit_handles_safely
-- get windows system version
function sandbox_winos.version()
local winver = winos.version()
if not winver then
raise("cannot get the version of the current winos!")
end
return winver
end
-- query registry value
function sandbox_winos.registry_query(keypath)
local value, errors = winos.registry_query(keypath)
if not value then
raise(errors)
end
return value
end
-- get registry keys
function sandbox_winos.registry_keys(keypath)
local keys, errors = winos.registry_keys(keypath)
if not keys then
raise(errors)
end
return keys
end
-- get registry values
function sandbox_winos.registry_values(keypath)
local values, errors = winos.registry_values(keypath)
if not values then
raise(errors)
end
return values
end
-- get short path
function sandbox_winos.short_path(long_path)
local short_path, errors = winos.short_path(long_path)
if not short_path then
raise(errors)
end
return short_path
end
-- return module
return sandbox_winos
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/os.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file os.lua
--
-- load modules
local io = require("base/io")
local os = require("base/os")
local utils = require("base/utils")
local xmake = require("base/xmake")
local option = require("base/option")
local semver = require("base/semver")
local scheduler = require("base/scheduler")
local sandbox = require("sandbox/sandbox")
local vformat = require("sandbox/modules/vformat")
-- define module
local sandbox_os = sandbox_os or {}
-- inherit some builtin interfaces
sandbox_os.shell = os.shell
sandbox_os.term = os.term
sandbox_os.host = os.host
sandbox_os.arch = os.arch
sandbox_os.subhost = os.subhost
sandbox_os.subarch = os.subarch
sandbox_os.is_host = os.is_host
sandbox_os.is_arch = os.is_arch
sandbox_os.is_subhost = os.is_subhost
sandbox_os.is_subarch = os.is_subarch
sandbox_os.syserror = os.syserror
sandbox_os.strerror = os.strerror
sandbox_os.exit = os.exit
sandbox_os.atexit = os.atexit
sandbox_os.date = os.date
sandbox_os.time = os.time
sandbox_os.args = os.args
sandbox_os.args = os.args
sandbox_os.argv = os.argv
sandbox_os.mtime = os.mtime
sandbox_os.raise = os.raise
sandbox_os.fscase = os.fscase
sandbox_os.isroot = os.isroot
sandbox_os.mclock = os.mclock
sandbox_os.nuldev = os.nuldev
sandbox_os.getenv = os.getenv
sandbox_os.setenv = os.setenv
sandbox_os.addenv = os.addenv
sandbox_os.setenvp = os.setenvp
sandbox_os.addenvp = os.addenvp
sandbox_os.getenvs = os.getenvs
sandbox_os.setenvs = os.setenvs
sandbox_os.addenvs = os.addenvs
sandbox_os.joinenvs = os.joinenvs
sandbox_os.pbpaste = os.pbpaste
sandbox_os.pbcopy = os.pbcopy
sandbox_os.cpuinfo = os.cpuinfo
sandbox_os.meminfo = os.meminfo
sandbox_os.default_njob = os.default_njob
sandbox_os.emptydir = os.emptydir
sandbox_os.filesize = os.filesize
sandbox_os.features = os.features
sandbox_os.workingdir = os.workingdir
sandbox_os.programdir = os.programdir
sandbox_os.programfile = os.programfile
sandbox_os.projectdir = os.projectdir
sandbox_os.projectfile = os.projectfile
sandbox_os.getwinsize = os.getwinsize
sandbox_os.getpid = os.getpid
-- syserror code
sandbox_os.SYSERR_UNKNOWN = os.SYSERR_UNKNOWN
sandbox_os.SYSERR_NONE = os.SYSERR_NONE
sandbox_os.SYSERR_NOT_PERM = os.SYSERR_NOT_PERM
sandbox_os.SYSERR_NOT_FILEDIR = os.SYSERR_NOT_FILEDIR
sandbox_os.SYSERR_NOT_ACCESS = os.SYSERR_NOT_ACCESS
-- copy file or directory
function sandbox_os.cp(srcpath, dstpath, opt)
assert(srcpath and dstpath)
srcpath = tostring(srcpath)
dstpath = tostring(dstpath)
local ok, errors = os.cp(vformat(srcpath), vformat(dstpath), opt)
if not ok then
os.raise(errors)
end
end
-- move file or directory
function sandbox_os.mv(srcpath, dstpath, opt)
assert(srcpath and dstpath)
srcpath = tostring(srcpath)
dstpath = tostring(dstpath)
local ok, errors = os.mv(vformat(srcpath), vformat(dstpath), opt)
if not ok then
os.raise(errors)
end
end
-- remove files or directories
function sandbox_os.rm(filepath, opt)
assert(filepath)
filepath = tostring(filepath)
local ok, errors = os.rm(vformat(filepath), opt)
if not ok then
os.raise(errors)
end
end
-- link file or directory to the new symfile
function sandbox_os.ln(srcpath, dstpath, opt)
assert(srcpath and dstpath)
srcpath = tostring(srcpath)
dstpath = tostring(dstpath)
local ok, errors = os.ln(vformat(srcpath), vformat(dstpath), opt)
if not ok then
os.raise(errors)
end
end
-- copy file or directory with the verbose info
function sandbox_os.vcp(srcpath, dstpath, opt)
assert(srcpath and dstpath)
if option.get("verbose") then
utils.cprint("${dim}> copy %s to %s", srcpath, dstpath)
end
return sandbox_os.cp(srcpath, dstpath, opt)
end
-- move file or directory with the verbose info
function sandbox_os.vmv(srcpath, dstpath, opt)
assert(srcpath and dstpath)
if option.get("verbose") then
utils.cprint("${dim}> move %s to %s", srcpath, dstpath)
end
return sandbox_os.mv(srcpath, dstpath, opt)
end
-- remove file or directory with the verbose info
function sandbox_os.vrm(filepath, opt)
assert(filepath)
if option.get("verbose") then
utils.cprint("${dim}> remove %s", filepath)
end
return sandbox_os.rm(filepath, opt)
end
-- link file or directory with the verbose info
function sandbox_os.vln(srcpath, dstpath, opt)
assert(srcpath and dstpath)
if option.get("verbose") then
utils.cprint("${dim}> link %s to %s", srcpath, dstpath)
end
return sandbox_os.ln(srcpath, dstpath, opt)
end
-- try to copy file or directory
function sandbox_os.trycp(srcpath, dstpath, opt)
assert(srcpath and dstpath)
return os.cp(vformat(srcpath), vformat(dstpath), opt)
end
-- try to move file or directory
function sandbox_os.trymv(srcpath, dstpath, opt)
assert(srcpath and dstpath)
return os.mv(vformat(srcpath), vformat(dstpath), opt)
end
-- try to remove files or directories
function sandbox_os.tryrm(filepath, opt)
assert(filepath)
return os.rm(vformat(filepath), opt)
end
-- change to directory
function sandbox_os.cd(dir)
-- check
assert(dir)
-- format it first
dir = vformat(dir)
-- enter this directory
local oldir, errors = os.cd(dir)
if not oldir then
os.raise(errors)
end
-- ok
return oldir
end
-- touch file or directory
function sandbox_os.touch(filepath, opt)
assert(filepath)
local ok, errors = os.touch(vformat(filepath), opt)
if not ok then
os.raise(errors)
end
end
-- create directories
function sandbox_os.mkdir(dir)
assert(dir)
local ok, errors = os.mkdir(vformat(dir))
if not ok then
os.raise(errors)
end
end
-- remove directories
function sandbox_os.rmdir(dir)
assert(dir)
local ok, errors = os.rmdir(vformat(dir))
if not ok then
os.raise(errors)
end
end
-- get the current directory
function sandbox_os.curdir()
return assert(os.curdir())
end
-- get the temporary directory
function sandbox_os.tmpdir(opt)
return assert(os.tmpdir(opt))
end
-- get the temporary file
function sandbox_os.tmpfile(key, opt)
return assert(os.tmpfile(key, opt))
end
-- get the script directory
function sandbox_os.scriptdir()
local instance = sandbox.instance()
local rootdir = instance:rootdir()
assert(rootdir)
return rootdir
end
-- quietly run command
function sandbox_os.run(cmd, ...)
cmd = vformat(cmd, ...)
local ok, errors = os.run(cmd)
if not ok then
os.raise(errors)
end
end
-- quietly run command with arguments list
function sandbox_os.runv(program, argv, opt)
program = vformat(program)
local ok, errors = os.runv(program, argv, opt)
if not ok then
os.raise(errors)
end
end
-- quietly run command and echo verbose info if [-v|--verbose] option is enabled
function sandbox_os.vrun(cmd, ...)
if option.get("verbose") then
print(vformat(cmd, ...))
end
(option.get("verbose") and sandbox_os.exec or sandbox_os.run)(cmd, ...)
end
-- quietly run command with arguments list and echo verbose info if [-v|--verbose] option is enabled
function sandbox_os.vrunv(program, argv, opt)
if option.get("verbose") then
print(vformat(program) .. " " .. sandbox_os.args(argv))
end
if not (opt and opt.dryrun) then
(option.get("verbose") and sandbox_os.execv or sandbox_os.runv)(program, argv, opt)
end
end
-- run command and return output and error data
function sandbox_os.iorun(cmd, ...)
cmd = vformat(cmd, ...)
local ok, outdata, errdata, errors = os.iorun(cmd)
if not ok then
if not errors then
errors = errdata or ""
if #errors:trim() == 0 then
errors = outdata or ""
end
end
os.raise({errors = errors, stderr = errdata, stdout = outdata})
end
return outdata, errdata
end
-- run command and return output and error data
function sandbox_os.iorunv(program, argv, opt)
program = vformat(program)
local ok, outdata, errdata, errors = os.iorunv(program, argv, opt)
if not ok then
if not errors then
errors = errdata or ""
if #errors:trim() == 0 then
errors = outdata or ""
end
end
os.raise({errors = errors, stderr = errdata, stdout = outdata})
end
return outdata, errdata
end
-- execute command
function sandbox_os.exec(cmd, ...)
cmd = vformat(cmd, ...)
local ok, errors = os.exec(cmd)
if ok ~= 0 then
if ok ~= nil then
errors = string.format("exec(%s) failed(%d)", cmd, ok)
else
errors = string.format("cannot exec(%s), %s", cmd, errors and errors or "unknown reason")
end
os.raise(errors)
end
end
-- execute command with arguments list
function sandbox_os.execv(program, argv, opt)
-- make program
program = vformat(program)
-- flush io buffer first for fixing redirect io output order
--
-- e.g.
--
-- xmake run > /tmp/a
-- print("xxx1")
-- os.exec("echo xxx2")
--
-- cat /tmp/a
-- xxx2
-- xxx1
--
io.flush()
-- run it
opt = opt or {}
local ok, errors = os.execv(program, argv, opt)
if ok ~= 0 and not opt.try then
-- get command
local cmd = program
if argv then
cmd = cmd .. " " .. os.args(argv)
end
-- get errors
if ok ~= nil then
errors = string.format("execv(%s) failed(%d)", cmd, ok)
else
errors = string.format("cannot execv(%s), %s", cmd, errors and errors or "unknown reason")
end
os.raise(errors)
end
-- we need return results if opt.try is enabled
return ok, errors
end
-- execute command and echo verbose info if [-v|--verbose] option is enabled
function sandbox_os.vexec(cmd, ...)
-- echo command
if option.get("verbose") then
utils.cprint("${color.dump.string}" .. vformat(cmd, ...))
end
-- run it
sandbox_os.exec(cmd, ...)
end
-- execute command with arguments list and echo verbose info if [-v|--verbose] option is enabled
function sandbox_os.vexecv(program, argv, opt)
-- echo command
if option.get("verbose") then
utils.cprint("${color.dump.string}" .. vformat(program) .. " " .. sandbox_os.args(argv))
end
-- run it
if not (opt and opt.dryrun) then
return sandbox_os.execv(program, argv, opt)
else
return 0
end
end
-- match files or directories
function sandbox_os.match(pattern, mode, callback)
return os.match(vformat(tostring(pattern)), mode, callback)
end
-- match directories
function sandbox_os.dirs(pattern, callback)
return (sandbox_os.match(pattern, 'd', callback))
end
-- match files
function sandbox_os.files(pattern, callback)
return (sandbox_os.match(pattern, 'f', callback))
end
-- match files and directories
function sandbox_os.filedirs(pattern, callback)
return (sandbox_os.match(pattern, 'a', callback))
end
-- is directory?
function sandbox_os.isdir(dirpath)
assert(dirpath)
dirpath = tostring(dirpath)
return os.isdir(vformat(dirpath))
end
-- is file?
function sandbox_os.isfile(filepath)
assert(filepath)
filepath = tostring(filepath)
return os.isfile(vformat(filepath))
end
-- is symlink?
function sandbox_os.islink(filepath)
assert(filepath)
filepath = tostring(filepath)
return os.islink(vformat(filepath))
end
-- is execute program?
function sandbox_os.isexec(filepath)
assert(filepath)
filepath = tostring(filepath)
return os.isexec(vformat(filepath))
end
-- exists file or directory?
function sandbox_os.exists(filedir)
assert(filedir)
filedir = tostring(filedir)
return os.exists(vformat(filedir))
end
-- read the content of symlink
function sandbox_os.readlink(symlink)
local result = os.readlink(tostring(symlink))
if not result then
os.raise("cannot read link(%s)", symlink)
end
return result
end
-- sleep (support in coroutine)
function sandbox_os.sleep(ms)
if scheduler:co_running() then
local ok, errors = scheduler:co_sleep(ms)
if not ok then
raise(errors)
end
else
os.sleep(ms)
end
end
-- get xmake version
function sandbox_os.xmakever()
-- fill cache
if sandbox_os._XMAKEVER == nil then
-- get xmakever
local xmakever = semver.new(xmake._VERSION_SHORT)
-- save to cache
sandbox_os._XMAKEVER = xmakever or false
end
-- done
return sandbox_os._XMAKEVER or nil
end
-- return module
return sandbox_os
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/has_package.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file has_package.lua
--
-- return module
return function (...)
require("sandbox/modules/import/core/sandbox/module").import("core.project.project")
local requires = project.required_packages()
if requires then
for _, name in ipairs(table.join(...)) do
local pkg = requires[name]
if pkg and pkg:enabled() then
return true
end
end
end
end
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/vprintf.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file vprintf.lua
--
-- load module
return require("sandbox/modules/utils").vprintf
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/import.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file import.lua
--
-- load module
return require("sandbox/modules/import/core/sandbox/module").import
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/raise.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file raise.lua
--
-- load module
return require("sandbox/modules/os").raise
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/cprint.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file cprint.lua
--
-- load module
return require("sandbox/modules/utils").cprint
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/irpairs.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file irpairs.lua
--
-- load modules
local table = require("base/table")
-- irpairs
--
-- e.g.
--
-- @code
--
-- for idx, val in irpairs({"a", "b", "c", "d", "e", "f"}) do
-- print("%d %s", idx, val)
-- end
--
-- for idx, val in irpairs({"a", "b", "c", "d", "e", "f"}, function (v) return v:upper() end) do
-- print("%d %s", idx, val)
-- end
--
-- for idx, val in irpairs({"a", "b", "c", "d", "e", "f"}, function (v, a, b) return v:upper() .. a .. b end, "a", "b") do
-- print("%d %s", idx, val)
-- end
--
-- @endcode
function sandbox_irpairs(t, filter, ...)
-- has filter?
local has_filter = type(filter) == "function"
-- init iterator
local args = table.pack(...)
local iter = function (t, i)
i = i - 1
local v = t[i]
if v ~= nil then
if has_filter then
v = filter(v, table.unpack(args, 1, args.n))
end
return i, v
end
end
-- return iterator and initialized state
t = table.wrap(t)
return iter, t, table.getn(t) + 1
end
-- load module
return sandbox_irpairs
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/has_config.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file has_config.lua
--
-- return module
return require("project/config").has
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/format.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file format.lua
--
-- return module
return require("sandbox/modules/string").format
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/tonumber.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file tonumber.lua
--
-- load module
return tonumber
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/path.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file path.lua
--
-- load module
return require("base/path")
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/todisplay.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file todisplay.lua
--
-- load module
return require("base/todisplay").print
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/io.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file io.lua
--
-- load modules
local io = require("base/io")
local utils = require("base/utils")
local string = require("base/string")
local raise = require("sandbox/modules/raise")
local vformat = require("sandbox/modules/vformat")
-- define module
local sandbox_io = sandbox_io or {}
local sandbox_io_file = sandbox_io_file or {}
local sandbox_io_filelock = sandbox_io_filelock or {}
sandbox_io.lines = io.lines
-- get file size
function sandbox_io_file.size(file)
local result, errors = file:_size()
if not result then
raise(errors)
end
return result
end
-- get file rawfd
function sandbox_io_file.rawfd(file)
local result, errors = file:_rawfd()
if not result then
raise(errors)
end
return result
end
-- close file
function sandbox_io_file.close(file)
local ok, errors = file:_close()
if not ok then
raise(errors)
end
return ok
end
-- flush file
function sandbox_io_file.flush(file)
local ok, errors = file:_flush()
if not ok then
raise(errors)
end
return ok
end
-- this file is a tty?
function sandbox_io_file.isatty(file)
local ok, errors = file:_isatty()
if ok == nil then
raise(errors)
end
return ok
end
-- seek offset at file
function sandbox_io_file.seek(file, whence, offset)
local result, errors = file:_seek(whence, offset)
if not result then
raise(errors)
end
return result
end
-- read data from file
function sandbox_io_file.read(file, fmt, opt)
local result, errors = file:_read(fmt, opt)
if errors then
raise(errors)
end
return result
end
-- readable for file
function sandbox_io_file.readable(file)
local ok, errors = file:_readable()
if errors then
raise(errors)
end
return ok
end
-- write data to file
function sandbox_io_file.write(file, ...)
local ok, errors = file:_write(...)
if not ok then
raise(errors)
end
end
-- print file
function sandbox_io_file.print(file, ...)
sandbox_io_file.write(file, vformat(...), "\n")
end
-- printf file
function sandbox_io_file.printf(file, ...)
sandbox_io_file.write(file, vformat(...))
end
-- writef file (without value filter)
function sandbox_io_file.writef(file, ...)
sandbox_io_file.write(file, string.format(...))
end
-- load object from file
function sandbox_io_file.load(file)
local result, errors = file:_load()
if errors then
raise(errors)
end
return result
end
-- save object to file
function sandbox_io_file.save(file, object, opt)
local ok, errors = file:_save(object, opt)
if not ok then
raise(errors)
end
return ok
end
-- lock filelock
function sandbox_io_filelock.lock(lock, opt)
local ok, errors = lock:_lock(opt)
if not ok then
raise(errors)
end
end
-- unlock filelock
function sandbox_io_filelock.unlock(lock)
local ok, errors = lock:_unlock()
if not ok then
raise(errors)
end
end
-- close filelock
function sandbox_io_filelock.close(lock)
local ok, errors = lock:_close()
if not ok then
raise(errors)
end
end
-- gsub the given file and return replaced data
function sandbox_io.gsub(filepath, pattern, replace, opt)
assert(filepath)
filepath = vformat(filepath)
local data, count, errors = io.gsub(filepath, pattern, replace, opt)
if not data then
raise(errors)
end
return data, count
end
-- replace text of the given file and return new data
function sandbox_io.replace(filepath, pattern, replace, opt)
assert(filepath)
filepath = vformat(filepath)
local data, count, errors = io.replace(filepath, pattern, replace, opt)
if not data then
raise(errors)
end
return data, count
end
-- insert text before line number in the given file and return new data
function sandbox_io.insert(filepath, lineidx, text, opt)
assert(filepath)
filepath = vformat(filepath)
local data, errors = io.insert(filepath, lineidx, text, opt)
if not data then
raise(errors)
end
return data
end
-- get std file
function sandbox_io.stdfile(filepath)
assert(filepath)
local file, errors = io.stdfile(filepath)
if not file then
raise(errors)
end
-- hook file interfaces
local hooked = {}
for name, func in pairs(sandbox_io_file) do
if not name:startswith("_") and type(func) == "function" then
hooked["_" .. name] = file["_" .. name] or file[name]
hooked[name] = func
end
end
for name, func in pairs(hooked) do
file[name] = func
end
return file
end
-- open file
function sandbox_io.open(filepath, mode, opt)
assert(filepath)
filepath = vformat(filepath)
local file, errors = io.open(filepath, mode, opt)
if not file then
raise(errors)
end
-- hook file interfaces
local hooked = {}
for name, func in pairs(sandbox_io_file) do
if not name:startswith("_") and type(func) == "function" then
hooked["_" .. name] = file["_" .. name] or file[name]
hooked[name] = func
end
end
for name, func in pairs(hooked) do
file[name] = func
end
return file
end
-- open file lock
function sandbox_io.openlock(filepath)
assert(filepath)
filepath = vformat(filepath)
local lock, errors = io.openlock(filepath)
if not lock then
raise(errors)
end
-- hook filelock interfaces
local hooked = {}
for name, func in pairs(sandbox_io_filelock) do
if not name:startswith("_") and type(func) == "function" then
hooked["_" .. name] = lock["_" .. name] or lock[name]
hooked[name] = func
end
end
for name, func in pairs(hooked) do
lock[name] = func
end
return lock
end
-- load object from the given file
function sandbox_io.load(filepath, opt)
assert(filepath)
filepath = vformat(filepath)
local result, errors = io.load(filepath, opt)
if errors ~= nil then
raise(errors)
end
return result
end
-- save object the the given filepath
function sandbox_io.save(filepath, object, opt)
assert(filepath)
filepath = vformat(filepath)
local ok, errors = io.save(filepath, object, opt)
if not ok then
raise(errors)
end
end
-- read all data from file
function sandbox_io.readfile(filepath, opt)
assert(filepath)
filepath = vformat(filepath)
local result, errors = io.readfile(filepath, opt)
if not result then
raise(errors)
end
return result
end
-- direct read from stdin
function sandbox_io.read(fmt, opt)
return sandbox_io.stdin:read(fmt, opt)
end
-- has readable for stdin?
function sandbox_io.readable()
return sandbox_io.stdin:readable()
end
-- direct write to stdout
function sandbox_io.write(...)
sandbox_io.stdout:write(...)
end
--- flush file
function sandbox_io.flush(file)
return (file or sandbox_io.stdout):flush()
end
-- isatty
function sandbox_io.isatty(file)
file = file or sandbox_io.stdout
return file:isatty()
end
-- write all data to file
function sandbox_io.writefile(filepath, data, opt)
assert(filepath)
filepath = vformat(filepath)
local ok, errors = io.writefile(filepath, data, opt)
if not ok then
raise(errors)
end
end
-- print line to file
function sandbox_io.print(filepath, ...)
sandbox_io.writefile(filepath, vformat(...) .. "\n")
end
-- print string to file
function sandbox_io.printf(filepath, ...)
sandbox_io.writefile(filepath, vformat(...))
end
-- cat the given file
function sandbox_io.cat(filepath, linecount, opt)
assert(filepath)
filepath = vformat(filepath)
io.cat(filepath, linecount, opt)
end
-- tail the given file
function sandbox_io.tail(filepath, linecount, opt)
assert(filepath)
filepath = vformat(filepath)
io.tail(filepath, linecount, opt)
end
-- lazy loading stdfile
setmetatable(sandbox_io, { __index = function (tbl, key)
local val = rawget(tbl, key)
if val == nil and (key == "stdin" or key == "stdout" or key == "stderr") then
val = sandbox_io.stdfile("/dev/" .. key)
if val ~= nil then
rawset(tbl, key, val)
end
end
return val
end})
-- return module
return sandbox_io
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/finally.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file finally.lua
--
-- finally
function sandbox_finally(block)
-- get the finally block function
return {finally = block[1]}
end
-- return module
return sandbox_finally
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/xmake.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file xmake.lua
--
-- load modules
local xmake = require("base/xmake")
-- define module
local sandbox_xmake = sandbox_xmake or {}
-- inherit some builtin interfaces
sandbox_xmake.arch = xmake.arch
sandbox_xmake.version = xmake.version
sandbox_xmake.branch = xmake.branch
sandbox_xmake.programdir = xmake.programdir
sandbox_xmake.programfile = xmake.programfile
sandbox_xmake.luajit = xmake.luajit
sandbox_xmake.argv = xmake.argv
-- return module
return sandbox_xmake
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/vformat.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file vformat.lua
--
-- return module
return require("sandbox/modules/string").vformat
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/get_config.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file get_config.lua
--
-- return module
return require("project/config").get
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/is_host.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file is_host.lua
--
-- return module
return require("base/os").is_host
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/coroutine.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file coroutine.lua
--
-- define module
local sandbox_coroutine = sandbox_coroutine or {}
-- load modules
local option = require("base/option")
local raise = require("sandbox/modules/raise")
-- inherit some builtin interfaces
sandbox_coroutine.create = coroutine.create
sandbox_coroutine.wrap = coroutine.wrap
sandbox_coroutine.yield = coroutine.yield
sandbox_coroutine.status = coroutine.status
sandbox_coroutine.running = coroutine.running
-- resume coroutine
function sandbox_coroutine.resume(co, ...)
-- resume it
local ok, results = coroutine.resume(co, ...)
if not ok then
-- get errors
local errors = results
if option.get("diagnosis") then
errors = debug.traceback(co, results)
elseif type(results) == "string" then
-- remove the prefix info
local _, pos = results:find(":%d+: ")
if pos then
errors = results:sub(pos + 1)
end
end
-- raise it
raise(errors)
end
-- ok
return results
end
-- load module
return sandbox_coroutine
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/val.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file val.lua
--
-- load modules
local sandbox = require("sandbox/sandbox")
-- get the variable value of filter
--
-- e.g.
--
-- local value = val("host")
-- local value = val("env PATH")
-- local value = val("shell echo hello xmake!")
-- local value = val("reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\XXXX;Name")
--
function val(name)
-- get the current sandbox instance
local instance = sandbox.instance()
assert(instance)
-- get filter from the current sandbox
local filter = instance:filter()
if filter then
return filter:get(name) or ""
end
-- no this variable
return ""
end
-- return module
return val
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/catch.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file catch.lua
--
-- catch
function sandbox_catch(block)
-- get the catch block function
return {catch = block[1]}
end
-- return module
return sandbox_catch
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/try.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file try.lua
--
-- load modules
local utils = require("base/utils")
local table = require("base/table")
local string = require("base/string")
local option = require("base/option")
-- define module
local sandbox_try = sandbox_try or {}
-- traceback
function sandbox_try._traceback(errors)
-- no diagnosis info?
if not option.get("diagnosis") then
if errors then
-- remove the prefix info
local _, pos = errors:find(":%d+: ")
if pos then
errors = errors:sub(pos + 1)
end
end
return errors
end
-- traceback exists?
if errors and errors:find("stack traceback:", 1, true) then
return errors
end
-- init results
local results = ""
if errors then
results = errors .. "\n"
end
results = results .. "stack traceback:\n"
-- make results
local level = 2
while true do
-- get debug info
local info = debug.getinfo(level, "Sln")
-- end?
if not info or (info.name and info.name == "xpcall") then
break
end
-- function?
if info.what == "C" then
results = results .. string.format(" [C]: in function '%s'\n", info.name)
elseif info.name then
results = results .. string.format(" [%s:%d]: in function '%s'\n", info.short_src, info.currentline, info.name)
elseif info.what == "main" then
results = results .. string.format(" [%s:%d]: in main chunk\n", info.short_src, info.currentline)
break
else
results = results .. string.format(" [%s:%d]:\n", info.short_src, info.currentline)
end
level = level + 1
end
return results
end
-- local ok = try
-- {
-- function ()
-- raise("errors")
-- raise({errors = "xxx", xxx = "", yyy = ""})
-- return true
-- end,
-- catch
-- {
-- function (errors)
-- print(errors)
-- if errors then
-- print(errors.xxx)
-- end
-- end
-- },
-- finally
-- {
-- function (ok, result_or_errors)
-- end
-- }
-- }
function sandbox_try.try(block)
-- get the try function
local try = block[1]
assert(try)
-- get catch and finally functions
local funcs = table.join(block[2] or {}, block[3] or {})
-- try to call it
local results = table.pack(utils.trycall(try, sandbox_try._traceback))
local ok = results[1]
if not ok then
-- run the catch function
if funcs and funcs.catch then
funcs.catch(results[2])
end
end
-- run the finally function
if funcs and funcs.finally then
funcs.finally(ok, table.unpack(results, 2, results.n))
end
if ok then
return table.unpack(results, 2, results.n)
end
end
-- return module
return sandbox_try.try
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/is_subhost.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file is_subhost.lua
--
-- return module
return require("base/os").is_subhost
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/wprint.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file wprint.lua
--
-- load module
return require("sandbox/modules/utils").wprint
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/is_mode.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file is_mode.lua
--
-- return module
return require("project/config").is_mode
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/cprintf.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file cprintf.lua
--
-- load module
return require("sandbox/modules/utils").cprintf
|
0 | repos/xmake/xmake/core/sandbox | repos/xmake/xmake/core/sandbox/modules/math.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file math.lua
--
-- load module
return require("base/math")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/private/core | repos/xmake/xmake/core/sandbox/modules/import/private/core/base/is_cross.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file is_cross.lua
--
return require("base/private/is_cross")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/private/core | repos/xmake/xmake/core/sandbox/modules/import/private/core/base/match_copyfiles.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file match_copyfiles.lua
--
return require("base/private/match_copyfiles")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/private/core | repos/xmake/xmake/core/sandbox/modules/import/private/core/base/select_script.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file select_script.lua
--
return require("base/private/select_script")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/tool/compiler.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file compiler.lua
--
-- define module
local sandbox_core_tool_compiler = sandbox_core_tool_compiler or {}
-- load modules
local platform = require("platform/platform")
local language = require("language/language")
local compiler = require("tool/compiler")
local raise = require("sandbox/modules/raise")
local assert = require("sandbox/modules/assert")
local import = require("sandbox/modules/import")
local sandbox = require("sandbox/sandbox")
-- load the compiler from the given source kind
function sandbox_core_tool_compiler.load(sourcekind, opt)
local instance, errors = compiler.load(sourcekind, opt and opt.target or nil)
if not instance then
raise(errors)
end
return instance
end
-- make command for compiling source file
function sandbox_core_tool_compiler.compcmd(sourcefiles, objectfile, opt)
return os.args(table.join(sandbox_core_tool_compiler.compargv(sourcefiles, objectfile, opt)))
end
-- make arguments list for compiling source file
function sandbox_core_tool_compiler.compargv(sourcefiles, objectfile, opt)
-- init options
opt = opt or {}
-- get source kind if only one source file
local sourcekind = opt.sourcekind
if not sourcekind and type(sourcefiles) == "string" then
sourcekind = language.sourcekind_of(sourcefiles)
end
-- get the compiler instance
local instance = sandbox_core_tool_compiler.load(sourcekind, opt)
-- make arguments list
return instance:compargv(sourcefiles, objectfile, opt)
end
-- compile source files
function sandbox_core_tool_compiler.compile(sourcefiles, objectfile, opt)
-- init options
opt = opt or {}
-- get source kind if only one source file
local sourcekind = opt.sourcekind
if not sourcekind and type(sourcefiles) == "string" then
sourcekind = language.sourcekind_of(sourcefiles)
end
-- get the compiler instance
local instance = sandbox_core_tool_compiler.load(sourcekind, opt)
-- compile it
local ok, errors = instance:compile(sourcefiles, objectfile, opt)
if not ok then
raise(errors)
end
end
-- make compiling flags
--
-- @param sourcefiles the source files
-- @param opt the argument options (contain all the compiler attributes of target),
-- e.g. {target = ..., targetkind = "static", configs = {cxflags = "", defines = "", includedirs = "", ...}}
--
-- @return the flags list
--
function sandbox_core_tool_compiler.compflags(sourcefiles, opt)
-- init options
opt = opt or {}
-- patch sourcefile to get flags of the given source file
if type(sourcefiles) == "string" then
opt.sourcefile = sourcefiles
end
-- get source kind if only one source file
local sourcekind = opt.sourcekind
if not sourcekind and type(sourcefiles) == "string" then
sourcekind = language.sourcekind_of(sourcefiles)
end
-- get the compiler instance
local instance = sandbox_core_tool_compiler.load(sourcekind, opt)
-- make flags
return instance:compflags(opt)
end
-- make command for building source file
function sandbox_core_tool_compiler.buildcmd(sourcefiles, targetfile, opt)
return os.args(table.join(sandbox_core_tool_compiler.buildargv(sourcefiles, targetfile, opt)))
end
-- make arguments list for building source file
function sandbox_core_tool_compiler.buildargv(sourcefiles, targetfile, opt)
-- init options
opt = opt or {}
-- get source kind if only one source file
local sourcekind = opt.sourcekind
if not sourcekind and type(sourcefiles) == "string" then
sourcekind = language.sourcekind_of(sourcefiles)
end
-- get the compiler instance
local instance = sandbox_core_tool_compiler.load(sourcekind, opt)
-- make arguments list
return instance:buildargv(sourcefiles, targetfile, opt)
end
-- build source files
function sandbox_core_tool_compiler.build(sourcefiles, targetfile, opt)
-- init options
opt = opt or {}
-- get source kind if only one source file
local sourcekind = opt.sourcekind
if not sourcekind and type(sourcefiles) == "string" then
sourcekind = language.sourcekind_of(sourcefiles)
end
-- get the compiler instance
local instance = sandbox_core_tool_compiler.load(sourcekind, opt)
-- build it
local ok, errors = instance:build(sourcefiles, targetfile, opt)
if not ok then
raise(errors)
end
end
-- get the current compiler name of given language kind
--
-- @param langkind the language kind, e.g. c, cxx, mm, mxx, swift, go, rust, d, as
--
-- @return the compiler name
--
function sandbox_core_tool_compiler.name(langkind, opt)
-- get sourcekind from the language kind
local sourcekind = language.langkinds()[langkind]
assert(sourcekind, "unknown language kind: " .. langkind)
-- get the compiler instance
local instance = sandbox_core_tool_compiler.load(sourcekind, opt)
return instance:name()
end
-- get all compiler features
--
-- @param langkind the language kind, e.g. c, cxx, mm, mxx, swift, go, rust, d, as
-- @param opt the argument options (contain all the compiler attributes of target),
-- e.g. {target = ..., targetkind = "static", cxflags = "", defines = "", includedirs = "", ...}
--
-- @return the features
--
function sandbox_core_tool_compiler.features(langkind, opt)
-- init options
opt = opt or {}
-- get sourcekind from the language kind
local sourcekind = language.langkinds()[langkind]
assert(sourcekind, "unknown language kind: " .. langkind)
-- get the compiler instance
local instance = sandbox_core_tool_compiler.load(sourcekind, opt)
-- import "lib.detect.features"
sandbox_core_tool_compiler._features = sandbox_core_tool_compiler._features or import("lib.detect.features")
if sandbox_core_tool_compiler._features then
-- get flags
local flags = instance:compflags(opt)
-- get features
local ok, results_or_errors = sandbox.load(sandbox_core_tool_compiler._features, instance:name(), {flags = flags, program = instance:program(), envs = instance:runenvs()})
if not ok then
raise(results_or_errors)
end
-- return features
return results_or_errors
end
-- no features
return {}
end
-- has the given features?
--
-- @param features the features, e.g. {"c_static_assert", "cxx_constexpr"}
-- @param opt the argument options (contain all the compiler attributes of target),
-- e.g. {target = ..., targetkind = "static", cxflags = "", defines = "", includedirs = "", ...}
--
-- @return the true or false
--
function sandbox_core_tool_compiler.has_features(features, opt)
-- import "lib.detect.has_features"
sandbox_core_tool_compiler._has_features = sandbox_core_tool_compiler._has_features or import("lib.detect.has_features")
if not sandbox_core_tool_compiler._has_features then
return
end
-- classify features by the source kind
opt = opt or {}
local features_by_kind = {}
local langkinds = language.langkinds()
for _, feature in ipairs(table.wrap(features)) do
-- get language kind
local langkind = feature:match("^(%w-)_")
assert(langkind, "unknown language kind for the feature: %s", feature)
-- get the sourcekind from the language kind
local sourcekind = langkinds[langkind]
assert(sourcekind, "unknown language kind: " .. langkind)
-- add feature
features_by_kind[sourcekind] = features_by_kind[sourcekind] or {}
table.insert(features_by_kind[sourcekind], feature)
end
-- has features for each compiler?
for sourcekind, features in pairs(features_by_kind) do
local instance = sandbox_core_tool_compiler.load(sourcekind, opt)
local flags = instance:compflags(opt)
local ok, results_or_errors = sandbox.load(sandbox_core_tool_compiler._has_features, instance:name(), features, {flags = flags, program = instance:program(), envs = instance:runenvs()})
if not ok then
raise(results_or_errors)
end
if not results_or_errors then
return false
end
end
return true
end
-- has the given flags?
--
-- @param langkind the language kind, e.g. c, cxx, mm, mxx, swift, go, rust, d, as
-- @param flags the flags
-- @param opt the options
--
-- @return the supported flags or nil
--
function sandbox_core_tool_compiler.has_flags(langkind, flags, opt)
-- init options
opt = opt or {}
-- get sourcekind from the language kind
local sourcekind = language.langkinds()[langkind]
assert(sourcekind, "unknown language kind: " .. langkind)
-- get the compiler instance
local instance = sandbox_core_tool_compiler.load(sourcekind, opt)
-- has flags?
return instance:has_flags(flags)
end
-- map flags from name and values
--
-- @param langkind the language kind, e.g. c, cxx, mm, mxx, swift, go, rust, d, as
-- @param name the name, e.g. includedirs, defines, links
-- @param values the values
-- @param opt the options
--
-- @return flags or nil
--
function sandbox_core_tool_compiler.map_flags(langkind, name, values, opt)
-- init options
opt = opt or {}
-- get sourcekind from the language kind
local sourcekind = language.langkinds()[langkind]
assert(sourcekind, "unknown language kind: " .. langkind)
-- get the compiler instance
local instance = sandbox_core_tool_compiler.load(sourcekind, opt)
-- map flags
return instance:map_flags(name, values, opt)
end
-- return module
return sandbox_core_tool_compiler
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/tool/toolchain.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file toolchain.lua
--
-- define module
local sandbox_core_tool_toolchain = sandbox_core_tool_toolchain or {}
-- load modules
local platform = require("platform/platform")
local toolchain = require("tool/toolchain")
local project = require("project/project")
local raise = require("sandbox/modules/raise")
-- inherit some builtin interfaces
sandbox_core_tool_toolchain.apis = toolchain.apis
-- get all toolchains list
function sandbox_core_tool_toolchain.list()
local names = table.copy(platform.toolchains())
if os.isfile(os.projectfile()) then
for _, name in ipairs(project.toolchains()) do
table.insert(names, name)
end
end
return names
end
-- load the toolchain from the given name
function sandbox_core_tool_toolchain.load(name, opt)
local instance, errors = toolchain.load(name, opt)
if not instance then
raise(errors)
end
return instance
end
-- return module
return sandbox_core_tool_toolchain
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/tool/linker.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file linker.lua
--
-- define module
local sandbox_core_tool_linker = sandbox_core_tool_linker or {}
-- load modules
local platform = require("platform/platform")
local linker = require("tool/linker")
local raise = require("sandbox/modules/raise")
-- load the linker from the given target kind
function sandbox_core_tool_linker.load(targetkind, sourcekinds, opt)
local instance, errors = linker.load(targetkind, sourcekinds, opt and opt.target or nil)
if not instance then
raise(errors)
end
return instance
end
-- make command for linking target file
function sandbox_core_tool_linker.linkcmd(targetkind, sourcekinds, objectfiles, targetfile, opt)
return os.args(table.join(sandbox_core_tool_linker.linkargv(targetkind, sourcekinds, objectfiles, targetfile, opt)))
end
-- make arguments list for linking target file
function sandbox_core_tool_linker.linkargv(targetkind, sourcekinds, objectfiles, targetfile, opt)
opt = opt or {}
local instance = sandbox_core_tool_linker.load(targetkind, sourcekinds, opt)
return instance:linkargv(objectfiles, targetfile, opt)
end
-- make link flags for the given target
--
-- @param targetkind the target kind
-- @param sourcekinds the source kinds
-- @param opt the argument options (contain all the linker attributes of target),
-- e.g. {target = ..., targetkind = "static", config = {cxflags = "", defines = "", includedirs = "", ...}}
--
function sandbox_core_tool_linker.linkflags(targetkind, sourcekinds, opt)
-- init options
opt = opt or {}
-- get the linker instance
local instance = sandbox_core_tool_linker.load(targetkind, sourcekinds, opt)
-- make flags
return instance:linkflags(opt)
end
-- link target file
function sandbox_core_tool_linker.link(targetkind, sourcekinds, objectfiles, targetfile, opt)
opt = opt or {}
local instance = sandbox_core_tool_linker.load(targetkind, sourcekinds, opt)
local ok, errors = instance:link(objectfiles, targetfile, opt)
if not ok then
raise(errors)
end
end
-- has the given flags?
--
-- @param targetkind the target kind
-- @param sourcekinds the source kinds
-- @param flags the flags
-- @param opt the options
--
-- @return the supported flags or nil
--
function sandbox_core_tool_linker.has_flags(targetkind, sourcekinds, flags, opt)
opt = opt or {}
local instance = sandbox_core_tool_linker.load(targetkind, sourcekinds, opt)
return instance:has_flags(flags)
end
-- map flags from name and values
--
-- @param targetkind the target kind
-- @param sourcekinds the source kinds
-- @param values the values
-- @param opt the options
--
-- @return flags or nil
--
function sandbox_core_tool_linker.map_flags(targetkind, sourcekinds, name, values, opt)
opt = opt or {}
local instance = sandbox_core_tool_linker.load(targetkind, sourcekinds, opt)
return instance:map_flags(name, values, opt)
end
-- return module
return sandbox_core_tool_linker
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file sandbox.lua
--
-- define module
local sandbox_core_sandbox = sandbox_core_sandbox or {}
-- load modules
local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
local try = require("sandbox/modules/try")
local catch = require("sandbox/modules/catch")
local utils = require("base/utils")
local table = require("base/table")
local colors = require("base/colors")
local dump = require("base/dump")
local option = require("base/option")
local scheduler = require("base/scheduler")
local globalcache = require("cache/globalcache")
-- print variables for interactive mode
function sandbox_core_sandbox._interactive_dump(...)
local diagnosis = option.get("diagnosis")
local values = table.pack(...)
-- do not use #values since nil might be included
local n = values.n
if n <= 1 then
dump(values[1], "< ", diagnosis)
else
local fmt = "< %d: "
if n >= 1000 then
-- try `unpack({}, 1, 5000)`, wish you happy!
fmt = "< %4d: "
elseif n >= 100 then
fmt = "< %3d: "
elseif n >= 10 then
fmt = "< %2d: "
end
for i = 1, n do
dump(values[i], string.format(fmt, i), diagnosis)
end
end
end
-- enter interactive mode
function sandbox_core_sandbox.interactive()
-- get the current sandbox instance
local instance = sandbox.instance()
if not instance then
raise("cannot get sandbox instance!")
end
-- fork a new sandbox
local errors
instance, errors = instance:fork()
if not instance then
raise(errors)
end
-- load repl history
local replhistory = nil
if readline then
-- clear history
readline.clear_history()
-- load history
replhistory = table.wrap(globalcache.get("history", "replhistory"))
for _, ln in ipairs(replhistory) do
readline.add_history(ln)
end
end
-- register dump function for interactive mode
local public_scope = instance._PUBLIC
public_scope["$interactive_dump"] = sandbox_core_sandbox._interactive_dump
public_scope["$interactive_prompt"] = colors.translate("${color.interactive.prompt}${text.interactive.prompt} ")
public_scope["$interactive_prompt2"] = colors.translate("${color.interactive.prompt2}${text.interactive.prompt2} ")
public_scope["$interactive_setfenv"] = setfenv
-- disable scheduler
scheduler:enable(false)
-- enter interactive mode with this new sandbox
sandbox.interactive(public_scope)
-- enable scheduler
scheduler:enable(true)
-- save repl history if readline is enabled
if readline then
-- save to history
local entries = readline.history_list()
if #entries > #replhistory then
for i = #replhistory + 1, #entries do
if #replhistory > 64 then
table.remove(replhistory, 1)
end
table.insert(replhistory, entries[i].line)
globalcache.set("history", "replhistory", replhistory)
end
globalcache.save("history")
end
-- clear history
readline.clear_history()
end
end
-- get the filter of the current sandbox for the given script
function sandbox_core_sandbox.filter(script)
local instance = sandbox.instance(script)
if not instance then
raise("cannot get sandbox instance!")
end
return instance:filter()
end
-- get all builtin modules
function sandbox_core_sandbox.builtin_modules()
return sandbox.builtin_modules()
end
-- return module
return sandbox_core_sandbox
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/sandbox/module.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file module.lua
--
-- define module
local core_sandbox_module = core_sandbox_module or {}
-- load modules
local os = require("base/os")
local path = require("base/path")
local hash = require("base/hash")
local utils = require("base/utils")
local table = require("base/table")
local string = require("base/string")
local option = require("base/option")
local global = require("base/global")
local config = require("project/config")
local memcache = require("cache/memcache")
local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
-- the module kinds
local MODULE_KIND_LUAFILE = 1
local MODULE_KIND_LUADIR = 2
local MODULE_KIND_BINARY = 3
local MODULE_KIND_SHARED = 4
-- get module path from name
function core_sandbox_module._modulepath(name)
-- translate module path
--
-- "package.module" => "package/module"
-- "..package.module" => "../../package/module"
--
local startdots = true
local modulepath = name:gsub(".", function(c)
if c == '.' then
if startdots then
return ".." .. path.sep()
else
return path.sep()
end
else
startdots = false
return c
end
end)
-- return module path
return modulepath
end
-- load module from file
function core_sandbox_module._loadfile(filepath, instance)
assert(filepath)
-- load module script
local script, errors = loadfile(filepath)
if not script then
return nil, errors
end
-- with sandbox?
if instance then
-- fork a new sandbox for this script
instance, errors = instance:fork(script, path.directory(filepath))
if not instance then
return nil, errors
end
-- load module
local result, errors = instance:module()
if not result then
return nil, errors
end
return result, instance:script()
end
-- load module without sandbox
local ok, result = utils.trycall(script)
if not ok then
return nil, result
end
return result, script
end
-- find module
function core_sandbox_module._find(dir, name)
assert(dir and name)
-- get module subpath
local module_subpath = core_sandbox_module._modulepath(name)
assert(module_subpath)
-- get module full path
local module_fullpath = path.join(dir, module_subpath)
-- single lua module?
local modulekey = path.normalize(path.absolute(module_fullpath))
if os.isfile(module_fullpath .. ".lua") then
return modulekey, MODULE_KIND_LUAFILE
elseif os.isdir(module_fullpath) then
local module_projectfile = path.join(module_fullpath, "xmake.lua")
if os.isfile(module_projectfile) then -- native module? e.g. binary/shared modules
local content = io.readfile(module_projectfile)
local kind = content:match("add_rules%(\"module.(.-)\"%)")
if kind == "binary" then
return modulekey, MODULE_KIND_BINARY
elseif kind == "shared" then
return modulekey, MODULE_KIND_SHARED
end
else
-- module directory
return modulekey, MODULE_KIND_LUADIR
end
end
end
-- load module from the single script file
function core_sandbox_module._load_from_scriptfile(module_fullpath, opt)
assert(not opt.module)
return core_sandbox_module._loadfile(module_fullpath .. ".lua", opt.instance)
end
-- load module from the script directory
function core_sandbox_module._load_from_scriptdir(module_fullpath, opt)
local script
local module = opt.module
local modulefiles = os.files(path.join(module_fullpath, "**.lua"))
if modulefiles then
for _, modulefile in ipairs(modulefiles) do
local result, errors = core_sandbox_module._loadfile(modulefile, opt.instance)
if not result then
return nil, errors
end
-- bind main entry
if type(result) == "table" and result.main then
setmetatable(result, { __call = function (_, ...) return result.main(...) end})
end
-- get the module path
local modulepath = path.relative(modulefile, module_fullpath)
if not modulepath then
return nil, string.format("cannot get the path for module: %s", module_subpath)
end
module = module or {}
script = errors
-- save module
local scope = module
for _, modulename in ipairs(path.split(modulepath)) do
local pos = modulename:find(".lua", 1, true)
if pos then
modulename = modulename:sub(1, pos - 1)
assert(modulename)
scope[modulename] = result
else
-- enter submodule
scope[modulename] = scope[modulename] or {}
scope = scope[modulename]
end
end
end
end
return module, script
end
-- add some builtin global options from the parent xmake
function core_sandbox_module._add_builtin_argv(argv, projectdir)
table.insert(argv, "-P")
table.insert(argv, projectdir)
for _, name in ipairs({"diagnosis", "verbose", "quiet", "yes", "confirm", "root"}) do
local value = option.get(name)
if type(value) == "boolean" then
table.insert(argv, "--" .. name)
elseif value ~= nil then
table.insert(argv, "--" .. name .. "=" .. value)
end
end
end
-- get the native module info
function core_sandbox_module._native_moduleinfo(module_fullpath, modulekind)
local projectdir = path.normalize(path.absolute(module_fullpath))
local programdir = path.normalize(os.programdir())
local modulename = path.basename(module_fullpath)
local modulehash = hash.uuid4(projectdir):split("-", {plain = true})[1]:lower()
local buildir
local is_global = false
local modulepath
if projectdir:startswith(programdir) then
is_global = true
buildir = path.join(global.directory(), "cache", "modules", modulehash)
modulepath = path.join("@programdir", path.relative(projectdir, programdir))
elseif os.isfile(os.projectfile()) then
buildir = path.join(config.directory(), "cache", "modules", modulehash)
modulepath = path.relative(projectdir, os.projectdir())
else
buildir = path.join(projectdir, "cache", "modules", modulehash)
modulepath = projectdir
end
return {buildir = buildir, projectdir = projectdir, is_global = is_global, modulename = modulename, modulekind = modulekind, modulepath = modulepath}
end
-- build module
function core_sandbox_module._build_module(moduleinfo)
local modulekind_str
if moduleinfo.modulekind == MODULE_KIND_BINARY then
modulekind_str = "binary"
elseif moduleinfo.modulekind == MODULE_KIND_SHARED then
modulekind_str = "shared"
else
return nil, "unknown module kind!"
end
utils.cprint("${color.build.target}building native %s module(%s) in %s ...", modulekind_str, moduleinfo.modulename, moduleinfo.modulepath)
local buildir = moduleinfo.buildir
local projectdir = moduleinfo.projectdir
local envs = {XMAKE_CONFIGDIR = buildir}
local argv = {"config", "-o", buildir, "-a", xmake.arch()}
core_sandbox_module._add_builtin_argv(argv, projectdir)
local ok, errors = os.execv(os.programfile(), argv, {envs = envs, curdir = projectdir})
if ok ~= 0 then
return nil, errors
end
argv = {}
core_sandbox_module._add_builtin_argv(argv, projectdir)
ok, errors = os.execv(os.programfile(), argv, {envs = envs, curdir = projectdir})
if ok ~= 0 then
return nil, errors
end
return true
end
-- load module from the binary module
function core_sandbox_module._load_from_binary(module_fullpath, opt)
opt = opt or {}
local moduleinfo = core_sandbox_module._native_moduleinfo(module_fullpath, MODULE_KIND_BINARY)
local binaryfiles = os.files(path.join(moduleinfo.buildir, "module_*"))
if #binaryfiles == 0 or (not moduleinfo.is_global and opt.always_build) then
local ok, errors = core_sandbox_module._build_module(moduleinfo)
if not ok then
return nil, errors
end
binaryfiles = {}
end
local module
if #binaryfiles == 0 then
binaryfiles = os.files(path.join(moduleinfo.buildir, "module_*"))
end
if #binaryfiles > 0 then
module = {}
for _, binaryfile in ipairs(binaryfiles) do
local modulename = path.basename(binaryfile):sub(8)
module[modulename] = function (...)
local argv = {}
for _, arg in ipairs(table.pack(...)) do
table.insert(argv, tostring(arg))
end
local ok, outdata, errdata, errors = os.iorunv(binaryfile, argv)
if ok then
return outdata
else
if not errors then
errors = errdata or ""
if #errors:trim() == 0 then
errors = outdata or ""
end
end
os.raise({errors = errors, stderr = errdata, stdout = outdata})
end
end
end
end
return module
end
-- load module from the shared module
function core_sandbox_module._load_from_shared(module_fullpath, opt)
opt = opt or {}
local moduleinfo = core_sandbox_module._native_moduleinfo(module_fullpath, MODULE_KIND_SHARED)
local libraryfiles = os.files(path.join(moduleinfo.buildir, "*module_*"))
if #libraryfiles == 0 or (not moduleinfo.is_global and opt.always_build) then
local ok, errors = core_sandbox_module._build_module(moduleinfo)
if not ok then
return nil, errors
end
libraryfiles = {}
end
local module
if #libraryfiles == 0 then
libraryfiles = os.files(path.join(moduleinfo.buildir, "*module_*"))
end
if #libraryfiles > 0 then
local script, errors1, errors2
for _, libraryfile in ipairs(libraryfiles) do
local modulename = path.basename(libraryfile):match("module_(.+)")
if modulename then
if package.loadxmi then
script, errors1 = package.loadxmi(libraryfile, "xmiopen_" .. modulename)
end
if not script then
script, errors2 = package.loadlib(libraryfile, "luaopen_" .. modulename)
end
if script then
module = script()
else
return nil, errors1 or errors2 or string.format("xmiopen_%s and luaopen_%s not found!", modulename, modulename)
end
break
end
end
end
return module, script
end
-- load module
function core_sandbox_module._load(dir, name, opt)
opt = opt or {}
assert(dir and name)
-- get module subpath
local module_subpath = core_sandbox_module._modulepath(name)
assert(module_subpath)
-- get module full path
local module_fullpath = path.join(dir, module_subpath)
-- load module
local script
local module
local modulekind = opt.modulekind
if modulekind == MODULE_KIND_LUAFILE then
module, script = core_sandbox_module._load_from_scriptfile(module_fullpath, opt)
elseif modulekind == MODULE_KIND_LUADIR then
module, script = core_sandbox_module._load_from_scriptdir(module_fullpath, opt)
elseif modulekind == MODULE_KIND_BINARY then
module, script = core_sandbox_module._load_from_binary(module_fullpath, opt)
elseif modulekind == MODULE_KIND_SHARED then
module, script = core_sandbox_module._load_from_shared(module_fullpath, opt)
end
if not module then
local errors = script
return nil, errors or string.format("module: %s not found!", name)
end
return module, script
end
-- find and load module
function core_sandbox_module._find_and_load(name, opt)
opt = opt or {}
local found = false
local errors = nil
local module = nil
local modulekey = nil
local modulekind = MODULE_KIND_LUAFILE
local modules = opt.modules
local modules_directories = opt.modules_directories
local always_build = opt.always_build
local loadnext = false
for idx, moduledir in ipairs(modules_directories) do
modulekey, modulekind = core_sandbox_module._find(moduledir, name)
if modulekey then
local moduleinfo = modules[modulekey]
if moduleinfo and not opt.nocache and not opt.inherit then
module = moduleinfo[1]
errors = moduleinfo[2]
else
module, errors = core_sandbox_module._load(moduledir, name, {
instance = idx < #modules_directories and opt.instance or nil, -- last modules need not fork sandbox
module = module,
always_build = always_build,
modulekind = modulekind})
if not opt.nocache then
modules[modulekey] = {module, errors}
end
end
if module and modulekind == MODULE_KIND_LUADIR then
loadnext = true
end
found = true
if not loadnext then
break
end
end
end
return found, module, errors
end
-- get module name
function core_sandbox_module.name(name)
local i = name:lastof(".", true)
if i then
name = name:sub(i + 1)
end
return name
end
-- get module directories
function core_sandbox_module.directories()
local moduledirs = memcache.get("core_sandbox_module", "moduledirs")
if not moduledirs then
moduledirs = { path.join(global.directory(), "modules"),
path.join(os.programdir(), "modules"),
path.join(os.programdir(), "core/sandbox/modules/import")}
local modulesdir = os.getenv("XMAKE_MODULES_DIR")
if modulesdir and os.isdir(modulesdir) then
table.insert(moduledirs, 1, modulesdir)
end
memcache.set("core_sandbox_module", "moduledirs", moduledirs)
end
return moduledirs
end
-- add module directories
function core_sandbox_module.add_directories(...)
local moduledirs = core_sandbox_module.directories()
for _, dir in ipairs({...}) do
table.insert(moduledirs, 1, dir)
end
memcache.set("core_sandbox_module", "moduledirs", table.unique(moduledirs))
end
-- find module
function core_sandbox_module.find(name)
for _, moduledir in ipairs(core_sandbox_module.directories()) do
if (core_sandbox_module._find(moduledir, name)) then
return true
end
end
end
-- import module
--
-- @param name the module name, e.g. core.platform
-- @param opt the argument options, e.g. {alias = "", nolocal = true, rootdir = "", try = false, inherit = false, anonymous = false, nocache = false}
--
-- @return the module instance
--
-- e.g.
--
-- import("core.platform")
-- => platform
--
-- import("core.platform", {alias = "p"})
-- => p
--
-- import("core")
-- => core
-- => core.platform
---
-- import("core", {rootdir = "/scripts"})
-- => core
-- => core.platform
--
-- import("core.platform", {inherit = true})
-- => inherit the all interfaces of core.platform to the current scope
--
-- local test = import("test", {rootdir = "/tmp/xxx", anonymous = true})
-- => only return imported module
--
-- import("core.project.config", {try = true})
-- => cannot raise errors if the imported module not found
--
-- import("native.module", {always_build = true})
-- => always build module when calling import
--
-- @note the polymiorphism is not supported for import.inherit mode now.
--
function core_sandbox_module.import(name, opt)
assert(name)
opt = opt or {}
-- init module cache
core_sandbox_module._MODULES = core_sandbox_module._MODULES or {}
local modules = core_sandbox_module._MODULES
-- get the parent scope
local scope_parent = getfenv(2)
assert(scope_parent)
-- get module name
local modulename = core_sandbox_module.name(name)
if not modulename then
raise("cannot get module name for %s", name)
end
-- the imported name
local imported_name = opt.alias or modulename
-- get the current sandbox instance
local instance = sandbox.instance()
assert(instance)
-- the root directory for this sandbox script
local rootdir = opt.rootdir or instance:rootdir()
-- init module directories (disable local packages?)
local modules_directories = (opt.nolocal or not rootdir) and core_sandbox_module.directories() or table.join(rootdir, core_sandbox_module.directories())
-- load module
local loadopt = table.clone(opt) or {}
loadopt.instance = instance
loadopt.modules = modules
loadopt.modules_directories = modules_directories
local found, module, errors = core_sandbox_module._find_and_load(name, loadopt)
-- not found? attempt to load module.interface
if not found and not opt.inherit then
-- get module name
local found2 = false
local errors2 = nil
local module2_name = nil
local interface_name = nil
local pos = name:lastof('.', true)
if pos then
module2_name = name:sub(1, pos - 1)
interface_name = name:sub(pos + 1)
end
-- load module.interface
if module2_name and interface_name then
found2, module2, errors2 = core_sandbox_module._find_and_load(module2_name, loadopt)
if found2 and module2 and module2[interface_name] then
module = module2[interface_name]
found = true
errors = nil
else
errors = errors2
end
end
end
-- not found?
if not found then
if opt.try then
return nil
else
raise("cannot import module: %s, not found!", name)
end
end
-- check
if not module then
raise("cannot import module: %s, %s", name, errors)
end
-- get module script
local script = errors
-- inherit?
if opt.inherit then
-- inherit this module into the parent scope
table.inherit2(scope_parent, module)
-- import as super module
imported_name = "_super"
-- public the script scope for the super module
--
-- we can access the all scope members of _super in the child module
--
-- e.g.
--
-- import("core.platform.xxx", {inherit = true})
--
-- print(_super._g)
--
if script ~= nil then
setmetatable(module, { __index = function (tbl, key)
local val = rawget(tbl, key)
if val == nil then
val = rawget(getfenv(script), key)
end
return val
end})
end
end
-- bind main entry
if type(module) == "table" and module.main then
setmetatable(module, { __call = function (_, ...) return module.main(...) end})
end
-- import this module into the parent scope
if not opt.anonymous then
scope_parent[imported_name] = module
end
-- return it
return module
end
-- inherit module
--
-- we can access all super interfaces by _super
--
-- @note the polymiorphism is not supported for import.inherit mode now.
--
function core_sandbox_module.inherit(name, opt)
-- init opt
opt = opt or {}
-- mark as inherit
opt.inherit = true
-- import and inherit it
return core_sandbox_module.import(name, opt)
end
-- get the public object in the current module
function core_sandbox_module.get(name)
-- is private object?
if name:startswith('_') then
return
end
-- get the parent scope
local scope_parent = getfenv(2)
assert(scope_parent)
-- get it
return scope_parent[name]
end
-- load module
return core_sandbox_module
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/cache/memcache.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file memcache.lua
--
-- return module
return require("cache/memcache")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/cache/globalcache.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file globalcache.lua
--
-- return module
return require("cache/globalcache")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/cache/global_detectcache.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file global_detectcache.lua
--
-- return module
return require("cache/global_detectcache")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/cache/localcache.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file localcache.lua
--
-- return module
return require("cache/localcache")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/cache/detectcache.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file detectcache.lua
--
-- return module
return require("cache/detectcache")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/theme/theme.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file theme.lua
--
-- define module
local sandbox_core_theme = sandbox_core_theme or {}
-- load modules
local theme = require("theme/theme")
local raise = require("sandbox/modules/raise")
-- get the current theme instance
function sandbox_core_theme.instance()
local instance = theme.instance()
if instance ~= nil then
return instance
else
raise("cannot get the current theme")
end
end
-- load the given theme
function sandbox_core_theme.load(name)
local instance, errors = theme.load(name)
if not instance then
raise("load theme(%s) failed, %s", name, errors)
end
return instance
end
-- get the theme configuration
function sandbox_core_theme.get(name)
local value = theme.get(name)
if value ~= nil then
return value
else
local instance = theme.instance()
raise("cannot get %s from the current theme(%s)", name, instance and instance:name() or "unknown")
end
end
-- find all themes
function sandbox_core_theme.names()
return theme.names()
end
-- return module
return sandbox_core_theme
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/project/menu.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file menu.lua
--
-- define module
local sandbox_core_project_menu = sandbox_core_project_menu or {}
-- load modules
local config = require("project/config")
local project = require("project/project")
-- get the menu options
function sandbox_core_project_menu.options()
-- get it
return project.menu()
end
-- return module
return sandbox_core_project_menu
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/project/target.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file target.lua
--
-- define module
local sandbox_core_project_target = sandbox_core_project_target or {}
-- load modules
local target = require("project/target")
local raise = require("sandbox/modules/raise")
-- inherit some builtin interfaces
sandbox_core_project_target.filename = target.filename
sandbox_core_project_target.linkname = target.linkname
sandbox_core_project_target.new = target.new
sandbox_core_project_target.apis = target.apis
-- return module
return sandbox_core_project_target
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/project/config.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file config.lua
--
-- define module
local sandbox_core_project_config = sandbox_core_project_config or {}
-- load modules
local config = require("project/config")
local project = require("project/project")
local platform = require("platform/platform")
local raise = require("sandbox/modules/raise")
-- inherit some builtin interfaces
sandbox_core_project_config.buildir = config.buildir
sandbox_core_project_config.plat = config.plat
sandbox_core_project_config.arch = config.arch
sandbox_core_project_config.mode = config.mode
sandbox_core_project_config.host = config.host
sandbox_core_project_config.get = config.get
sandbox_core_project_config.set = config.set
sandbox_core_project_config.directory = config.directory
sandbox_core_project_config.filepath = config.filepath
sandbox_core_project_config.readonly = config.readonly
sandbox_core_project_config.load = config.load
sandbox_core_project_config.read = config.read
sandbox_core_project_config.clear = config.clear
sandbox_core_project_config.dump = config.dump
-- save the configuration
function sandbox_core_project_config.save(filepath, opt)
local ok, errors = config.save(filepath, opt)
if not ok then
raise(errors)
end
end
-- return module
return sandbox_core_project_config
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/project/project.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file project.lua
--
-- define module
local sandbox_core_project = sandbox_core_project or {}
-- load modules
local table = require("base/table")
local deprecated = require("base/deprecated")
local utils = require("base/utils")
local baseoption = require("base/option")
local config = require("project/config")
local option = require("project/option")
local project = require("project/project")
local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
local environment = require("platform/environment")
local package = require("package/package")
local import = require("sandbox/modules/import")
-- export some readonly interfaces
sandbox_core_project.get = project.get
sandbox_core_project.extraconf = project.extraconf
sandbox_core_project.rule = project.rule
sandbox_core_project.rules = project.rules
sandbox_core_project.toolchain = project.toolchain
sandbox_core_project.toolchains = project.toolchains
sandbox_core_project.target = project.target
sandbox_core_project.target_add = project.target_add
sandbox_core_project.targets = project.targets
sandbox_core_project.ordertargets = project.ordertargets
sandbox_core_project.option = project.option
sandbox_core_project.options = project.options
sandbox_core_project.rootfile = project.rootfile
sandbox_core_project.allfiles = project.allfiles
sandbox_core_project.rcfiles = project.rcfiles
sandbox_core_project.directory = project.directory
sandbox_core_project.name = project.name
sandbox_core_project.modes = project.modes
sandbox_core_project.default_arch = project.default_arch
sandbox_core_project.allowed_modes = project.allowed_modes
sandbox_core_project.allowed_plats = project.allowed_plats
sandbox_core_project.allowed_archs = project.allowed_archs
sandbox_core_project.mtimes = project.mtimes
sandbox_core_project.version = project.version
sandbox_core_project.required_package = project.required_package
sandbox_core_project.required_packages = project.required_packages
sandbox_core_project.requires_str = project.requires_str
sandbox_core_project.requireconfs_str = project.requireconfs_str
sandbox_core_project.requireslock = project.requireslock
sandbox_core_project.requireslock_version = project.requireslock_version
sandbox_core_project.policy = project.policy
sandbox_core_project.tmpdir = project.tmpdir
sandbox_core_project.tmpfile = project.tmpfile
sandbox_core_project.is_loaded = project.is_loaded
sandbox_core_project.apis = project.apis
-- check project options
function sandbox_core_project.check_options()
-- get project options
local options = {}
for _, opt in pairs(project.options()) do
table.insert(options, opt)
end
-- get sandbox instance
local instance = sandbox.instance()
assert(instance)
-- enter the project directory
local oldir, errors = os.cd(os.projectdir())
if not oldir then
raise(errors)
end
-- init check task
local checked = {}
local checktask = function (index)
local opt = options[index]
if opt then
-- check deps of this option first
for _, dep in ipairs(opt:orderdeps()) do
if not checked[dep:name()] then
dep:check()
checked[dep:name()] = true
end
end
-- check this option
if not checked[opt:name()] then
opt:check()
checked[opt:name()] = true
end
end
end
-- check all options
local jobs = baseoption.get("jobs") or os.default_njob()
import("async.runjobs", {anonymous = true})("check_options", instance:fork(checktask):script(), {total = #options, comax = jobs})
-- save all options to the cache file
option.save()
-- leave the project directory
local ok, errors = os.cd(oldir)
if not ok then
raise(errors)
end
end
-- config target
function sandbox_core_project._config_target(target, opt)
for _, rule in ipairs(table.wrap(target:orderules())) do
local on_config = rule:script("config")
if on_config then
on_config(target, opt)
end
end
local on_config = target:script("config")
if on_config then
on_config(target, opt)
end
end
-- config targets
--
-- @param opt the extra option, e.g. {recheck = false}
--
-- on_config(target, opt)
-- -- @see https://github.com/xmake-io/xmake/issues/4173#issuecomment-1712843956
-- if opt.recheck then
-- target:has_cfuncs(...)
-- end
-- end
--
function sandbox_core_project._config_targets(opt)
opt = opt or {}
for _, target in ipairs(table.wrap(project.ordertargets())) do
if target:is_enabled() then
sandbox_core_project._config_target(target, opt)
end
end
end
-- load rules in the required packages for target
function sandbox_core_project._load_package_rules_for_target(target)
for _, rulename in ipairs(table.wrap(target:get("rules"))) do
local packagename = rulename:match("@(.-)/")
if packagename then
local pkginfo = project.required_package(packagename)
if pkginfo then
local r = pkginfo:rule(rulename)
if r then
target:rule_add(r)
for _, dep in pairs(table.wrap(r:deps())) do
target:rule_add(dep)
end
end
end
end
end
end
-- load rules in the required packages for targets
-- @see https://github.com/xmake-io/xmake/issues/2374
--
-- @code
-- add_requires("zlib", {system = false})
-- target("test")
-- set_kind("binary")
-- add_files("src/*.cpp")
-- add_packages("zlib")
-- add_rules("@zlib/test")
-- @endcode
--
function sandbox_core_project._load_package_rules_for_targets()
for _, target in ipairs(table.wrap(project.ordertargets())) do
if target:is_enabled() then
sandbox_core_project._load_package_rules_for_target(target)
end
end
end
-- load project targets
function sandbox_core_project.load_targets(opt)
sandbox_core_project._load_package_rules_for_targets()
sandbox_core_project._config_targets(opt)
end
-- get the filelock of the whole project directory
function sandbox_core_project.filelock()
local filelock, errors = project.filelock()
if not filelock then
raise("cannot create the project lock, %s!", errors or "unknown errors")
end
return filelock
end
-- lock the whole project
function sandbox_core_project.lock(opt)
if sandbox_core_project.trylock(opt) then
return true
elseif baseoption.get("diagnosis") then
utils.cprint("${color.warning}the current project is being accessed by other processes, please wait!")
io.flush()
end
local ok, errors = sandbox_core_project.filelock():lock(opt)
if not ok then
raise(errors)
end
end
-- trylock the whole project
function sandbox_core_project.trylock(opt)
return sandbox_core_project.filelock():trylock(opt)
end
-- unlock the whole project
function sandbox_core_project.unlock()
local ok, errors = sandbox_core_project.filelock():unlock()
if not ok then
raise(errors)
end
end
-- change project file and directory (xmake.lua)
function sandbox_core_project.chdir(projectdir, projectfile)
if not projectfile then
projectfile = path.join(projectdir, "xmake.lua")
end
xmake._PROJECT_FILE = projectfile
xmake._PROJECT_DIR = path.directory(projectfile)
xmake._WORKING_DIR = xmake._PROJECT_DIR
config._DIRECTORY = nil
end
-- get scope
function sandbox_core_project.scope(scopename)
local cachekey = "scope." .. scopename
local scope = project._memcache():get(cachekey)
if not scope then
-- load the project file first if has not been loaded?
local ok, errors = project._load()
if not ok then
raise("load project failed, %s", errors or "unknown")
end
-- load scope
scope, errors = project._load_scope(scopename, true, false)
if not scope then
raise("load scope(%s) failed, %s", scopename, errors or "unknown")
end
project._memcache():set(cachekey, scope)
end
return scope
end
-- return module
return sandbox_core_project
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/project/policy.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file policy.lua
--
-- define module
local sandbox_core_project_policy = sandbox_core_project_policy or {}
-- load modules
local table = require("base/table")
local global = require("base/global")
local option = require("base/option")
local utils = require("base/utils")
local policy = require("project/policy")
local project = require("project/project")
local raise = require("sandbox/modules/raise")
-- export some readonly interfaces
sandbox_core_project_policy.policies = policy.policies
-- has build warnings?
function sandbox_core_project_policy.build_warnings(opt)
opt = opt or {}
if opt.build_warnings == false and not option.get("diagnosis") then
return false
end
local warnings = sandbox_core_project_policy._BUILD_WARNINGS
if warnings == nil then
if option.get("warning") then
utils.warning("\"xmake build -w\" option has been deprecated, the warning output has been enabled by default.")
end
warnings = option.get("diagnosis")
if warnings == nil and os.isfile(os.projectfile()) and project.policy("build.warning") ~= nil then
warnings = project.policy("build.warning")
end
sandbox_core_project_policy._BUILD_WARNINGS = warnings or false
end
return warnings
end
-- return module
return sandbox_core_project_policy
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/project/template.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file template.lua
--
-- define module
local sandbox_core_project_template = sandbox_core_project_template or {}
-- load modules
local template = require("project/template")
local raise = require("sandbox/modules/raise")
-- get all languages
function sandbox_core_project_template.languages()
return assert(template.languages())
end
-- load all templates from the given language
function sandbox_core_project_template.templates(language)
return assert(template.templates(language))
end
-- return module
return sandbox_core_project_template
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/project/rule.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file rule.lua
--
-- define module
local sandbox_core_project_rule = sandbox_core_project_rule or {}
-- load modules
local table = require("base/table")
local rule = require("project/rule")
local project = require("project/project")
local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
-- inherit some builtin interfaces
sandbox_core_project_rule.rule = rule.rule
sandbox_core_project_rule.rules = rule.rules
sandbox_core_project_rule.new = rule.new
sandbox_core_project_rule.apis = rule.apis
-- return module
return sandbox_core_project_rule
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/project/option.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file option.lua
--
-- define module
local sandbox_core_project_option = sandbox_core_project_option or {}
-- load modules
local option = require("project/option")
local raise = require("sandbox/modules/raise")
-- inherit some builtin interfaces
sandbox_core_project_option.interpreter = option.interpreter
sandbox_core_project_option.new = option.new
sandbox_core_project_option.apis = option.apis
-- return module
return sandbox_core_project_option
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/project/task.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file task.lua
--
-- deprecatd: return module
return require("sandbox/modules/import/core/base/task")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/libc.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file libc.lua
--
-- load modules
return require("base/libc")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/process.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file process.lua
--
-- load modules
local process = require("base/process")
local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
local vformat = require("sandbox/modules/vformat")
-- define module
local sandbox_core_base_process = sandbox_core_base_process or {}
local sandbox_core_base_instance = sandbox_core_base_instance or {}
sandbox_core_base_process._subprocess = sandbox_core_base_process._subprocess or process._subprocess
-- wait subprocess
function sandbox_core_base_instance.wait(proc, timeout)
local ok, status_or_errors = proc:_wait(timeout)
if ok < 0 and status_or_errors then
raise(status_or_errors)
end
return ok, status_or_errors
end
-- kill subprocess
function sandbox_core_base_instance.kill(proc)
local ok, errors = proc:_kill()
if not ok then
raise(errors)
end
end
-- close subprocess
function sandbox_core_base_instance.close(proc)
local ok, errors = proc:_close()
if not ok then
raise(errors)
end
end
-- open process
---
-- @param command the command
-- @param opt the arguments option, {outpath = "", errpath = "", envs = {"PATH=xxx", "XXX=yyy"}
--
function sandbox_core_base_process.open(command, opt)
-- check
assert(command)
-- format command first
command = vformat(command)
-- open process
local proc, errors = process.open(command, opt)
if not proc then
raise(errors)
end
-- hook subprocess interfaces
local hooked = {}
for name, func in pairs(sandbox_core_base_instance) do
if not name:startswith("_") and type(func) == "function" then
hooked["_" .. name] = proc["_" .. name] or proc[name]
hooked[name] = func
end
end
for name, func in pairs(hooked) do
proc[name] = func
end
return proc
end
-- open process with arguments
--
-- @param filename the command/file name
-- @param argv the command arguments
-- @param opt the arguments option, {outpath = "", errpath = "", envs = {"PATH=xxx", "XXX=yyy"}
--
function sandbox_core_base_process.openv(filename, argv, opt)
-- check
assert(argv)
-- format filename first
filename = vformat(filename)
-- open process
local proc, errors = process.openv(filename, argv, opt)
if not proc then
raise(errors)
end
-- hook subprocess interfaces
local hooked = {}
for name, func in pairs(sandbox_core_base_instance) do
if not name:startswith("_") and type(func) == "function" then
hooked["_" .. name] = proc["_" .. name] or proc[name]
hooked[name] = func
end
end
for name, func in pairs(hooked) do
proc[name] = func
end
return proc
end
-- return module
return sandbox_core_base_process
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/base64.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file base64.lua
--
-- define module
local sandbox_core_base_base64 = sandbox_core_base_base64 or {}
-- load modules
local base64 = require("base/base64")
local raise = require("sandbox/modules/raise")
-- decode the base64 string to the data
function sandbox_core_base_base64.decode(base64str, opt)
local data, errors = base64.decode(base64str, opt)
if not data and errors then
raise(errors)
end
return data
end
-- encode the data to the base64 string
function sandbox_core_base_base64.encode(data, opt)
local base64str, errors = base64.encode(data, opt)
if not base64str and errors then
raise(errors)
end
return base64str
end
-- return module
return sandbox_core_base_base64
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/bytes.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file bytes.lua
--
-- load modules
return require("base/bytes")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/memory.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file memory.lua
--
-- load modules
return require("base/memory")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/bit.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file bit.lua
--
-- load modules
return require("base/bit")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/singleton.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file singleton.lua
--
-- return module
return require("base/singleton")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/scheduler.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file scheduler.lua
--
-- define module
local sandbox_core_base_scheduler = sandbox_core_base_scheduler or {}
-- load modules
local poller = require("base/poller")
local scheduler = require("base/scheduler")
local raise = require("sandbox/modules/raise")
-- the poller object type
sandbox_core_base_scheduler.OT_SOCK = poller.OT_SOCK
sandbox_core_base_scheduler.OT_PIPE = poller.OT_PIPE
sandbox_core_base_scheduler.OT_PROC = poller.OT_PROC
-- start a new coroutine task
function sandbox_core_base_scheduler.co_start(cotask, ...)
local co, errors = scheduler:co_start(cotask, ...)
if not co then
raise(errors)
end
return co
end
-- start a new named coroutine task
function sandbox_core_base_scheduler.co_start_named(coname, cotask, ...)
local co, errors = scheduler:co_start_named(coname, cotask, ...)
if not co then
raise(errors)
end
return co
end
-- start a new coroutine task with options
function sandbox_core_base_scheduler.co_start_withopt(opt, cotask, ...)
local co, errors = scheduler:co_start_withopt(opt, cotask, ...)
if not co then
raise(errors)
end
return co
end
-- resume the given coroutine
function sandbox_core_base_scheduler.co_resume(co, ...)
return scheduler:resume(co:thread(), ...)
end
-- suspend the current coroutine
function sandbox_core_base_scheduler.co_suspend(...)
return scheduler:co_suspend(...)
end
-- yield the current coroutine
function sandbox_core_base_scheduler.co_yield()
local ok, errors = scheduler:co_yield()
if not ok then
raise(errors)
end
end
-- sleep some times (ms)
function sandbox_core_base_scheduler.co_sleep(ms)
local ok, errors = scheduler:co_sleep(ms)
if not ok then
raise(errors)
end
end
-- lock the current coroutine
function sandbox_core_base_scheduler.co_lock(lockname)
local ok, errors = scheduler:co_lock(lockname)
if not ok then
raise(errors)
end
end
-- unlock the current coroutine
function sandbox_core_base_scheduler.co_unlock(lockname)
local ok, errors = scheduler:co_unlock(lockname)
if not ok then
raise(errors)
end
end
-- get coroutine group with the given name
function sandbox_core_base_scheduler.co_group(name)
return scheduler:co_group(name)
end
-- begin coroutine group with the given name
function sandbox_core_base_scheduler.co_group_begin(name, scopefunc)
local ok, errors = scheduler:co_group_begin(name, scopefunc)
if not ok then
raise(errors)
end
end
-- wait for finishing the given coroutine group
function sandbox_core_base_scheduler.co_group_wait(name, opt)
local ok, errors = scheduler:co_group_wait(name, opt)
if not ok then
raise(errors)
end
end
-- get the waiting poller objects of the given coroutine group
function sandbox_core_base_scheduler.co_group_waitobjs(name)
return scheduler:co_group_waitobjs(name)
end
-- get the current running coroutine
function sandbox_core_base_scheduler.co_running()
return scheduler:co_running()
end
-- get the all coroutine task count
function sandbox_core_base_scheduler.co_count()
return scheduler:co_count()
end
-- return module
return sandbox_core_base_scheduler
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/colors.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file colors.lua
--
-- return module
return require("base/colors")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/signal.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file signal.lua
--
-- load modules
return require("base/signal")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/object.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file object.lua
--
-- return module
return require("base/object")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/cpu.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file cpu.lua
--
-- load modules
return require("base/cpu")
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/pipe.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file pipe.lua
--
-- load modules
local utils = require("base/utils")
local pipe = require("base/pipe")
local string = require("base/string")
local raise = require("sandbox/modules/raise")
-- define module
local sandbox_core_base_pipe = sandbox_core_base_pipe or {}
local sandbox_core_base_pipe_instance = sandbox_core_base_pipe_instance or {}
-- export the pipe events
sandbox_core_base_pipe.EV_READ = pipe.EV_READ
sandbox_core_base_pipe.EV_WRITE = pipe.EV_WRITE
sandbox_core_base_pipe.EV_CONN = pipe.EV_CONN
-- wrap pipe file
function _pipefile_wrap(pipefile)
-- hook pipe interfaces
local hooked = {}
for name, func in pairs(sandbox_core_base_pipe_instance) do
if not name:startswith("_") and type(func) == "function" then
hooked["_" .. name] = pipefile["_" .. name] or pipefile[name]
hooked[name] = func
end
end
for name, func in pairs(hooked) do
pipefile[name] = func
end
return pipefile
end
-- wait pipe events
function sandbox_core_base_pipe_instance.wait(pipefile, events, timeout)
local events, errors = pipefile:_wait(events, timeout)
if events < 0 and errors then
raise(errors)
end
return events
end
-- connect pipe, only for named pipe (server-side)
function sandbox_core_base_pipe_instance.connect(pipefile, opt)
local ok, errors = pipefile:_connect(opt)
if ok < 0 and errors then
raise(errors)
end
return ok
end
-- write data to pipe file
function sandbox_core_base_pipe_instance.write(pipefile, data, opt)
local real, errors = pipefile:_write(data, opt)
if real < 0 and errors then
raise(errors)
end
return real
end
-- read data from pipe file
function sandbox_core_base_pipe_instance.read(pipefile, buff, size, opt)
local real, data_or_errors = pipefile:_read(buff, size, opt)
if real < 0 and data_or_errors then
raise(data_or_errors)
end
return real, data_or_errors
end
-- close pipe file
function sandbox_core_base_pipe_instance.close(pipefile)
local ok, errors = pipefile:_close()
if not ok then
raise(errors)
end
end
-- open a named pipe file
function sandbox_core_base_pipe.open(name, mode, buffsize)
local pipefile, errors = pipe.open(name, mode, buffsize)
if not pipefile then
raise(errors)
end
return _pipefile_wrap(pipefile)
end
-- open a anonymous pipe pair
function sandbox_core_base_pipe.openpair(mode, buffsize)
local rpipefile, wpipefile, errors = pipe.openpair(mode, buffsize)
if not rpipefile or not wpipefile then
raise(errors)
end
return _pipefile_wrap(rpipefile), _pipefile_wrap(wpipefile)
end
-- return module
return sandbox_core_base_pipe
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/bloom_filter.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file bloom_filter.lua
--
-- load modules
local utils = require("base/utils")
local bloom_filter = require("base/bloom_filter")
local string = require("base/string")
local raise = require("sandbox/modules/raise")
-- define module
local sandbox_core_base_bloom_filter = sandbox_core_base_bloom_filter or {}
local sandbox_core_base_bloom_filter_instance = sandbox_core_base_bloom_filter_instance or {}
-- wrap bloom_filter
function _bloom_filter_wrap(filter)
-- hook bloom_filter interfaces
local hooked = {}
for name, func in pairs(sandbox_core_base_bloom_filter_instance) do
if not name:startswith("_") and type(func) == "function" then
hooked["_" .. name] = filter["_" .. name] or filter[name]
hooked[name] = func
end
end
for name, func in pairs(hooked) do
filter[name] = func
end
return filter
end
-- get bloom filter data
function sandbox_core_base_bloom_filter_instance.data(filter)
local data, errors = filter:_data()
if not data and errors then
raise(errors)
end
return data
end
-- set bloom filter data
function sandbox_core_base_bloom_filter_instance.data_set(filter, data)
local ok, errors = filter:_data_set(data)
if not ok and errors then
raise(errors)
end
end
-- set bloom filter item
function sandbox_core_base_bloom_filter_instance.set(filter, item)
local ok, result_or_errors = filter:_set(item)
if not ok then
raise(result_or_errors)
end
return result_or_errors
end
-- get bloom filter item
function sandbox_core_base_bloom_filter_instance.get(filter, item)
local ok, result_or_errors = filter:_get(item)
if not ok then
raise(result_or_errors)
end
return result_or_errors
end
-- clear bloom filter data
function sandbox_core_base_bloom_filter_instance.clear(filter)
local ok, errors = filter:_clear()
if not ok then
raise(errors)
end
end
-- close bloom filter
function sandbox_core_base_bloom_filter_instance.close(filter)
local ok, errors = filter:_close()
if not ok then
raise(errors)
end
end
-- new bloom filter
function sandbox_core_base_bloom_filter.new(opt)
local filter, errors = bloom_filter.new(opt)
if not filter then
raise(errors)
end
return _bloom_filter_wrap(filter)
end
-- return module
return sandbox_core_base_bloom_filter
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/filter.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file filter.lua
--
-- define module
local sandbox_core_base_filter = sandbox_core_base_filter or {}
-- load modules
local filter = require("base/filter")
local raise = require("sandbox/modules/raise")
-- new filter instance
function sandbox_core_base_filter.new()
return filter.new()
end
-- return module
return sandbox_core_base_filter
|
0 | repos/xmake/xmake/core/sandbox/modules/import/core | repos/xmake/xmake/core/sandbox/modules/import/core/base/socket.lua | --!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file socket.lua
--
-- load modules
local utils = require("base/utils")
local socket = require("base/socket")
local string = require("base/string")
local raise = require("sandbox/modules/raise")
-- define module
local sandbox_core_base_socket = sandbox_core_base_socket or {}
local sandbox_core_base_socket_instance = sandbox_core_base_socket_instance or {}
-- export the socket types
sandbox_core_base_socket.TCP = socket.TCP
sandbox_core_base_socket.UDP = socket.UDP
sandbox_core_base_socket.ICMP = socket.ICMP
-- export the socket families
sandbox_core_base_socket.IPV4 = socket.IPV4
sandbox_core_base_socket.IPV6 = socket.IPV6
-- export the socket events
sandbox_core_base_socket.EV_RECV = socket.EV_RECV
sandbox_core_base_socket.EV_SEND = socket.EV_SEND
sandbox_core_base_socket.EV_CONN = socket.EV_CONN
sandbox_core_base_socket.EV_ACPT = socket.EV_ACPT
-- export the socket control code
sandbox_core_base_socket.CTRL_SET_RECVBUFF = socket.CTRL_SET_RECVBUFF
sandbox_core_base_socket.CTRL_SET_SENDBUFF = socket.CTRL_SET_SENDBUFF
-- wrap socket
function _socket_wrap(sock)
-- hook socket interfaces
local hooked = {}
for name, func in pairs(sandbox_core_base_socket_instance) do
if not name:startswith("_") and type(func) == "function" then
hooked["_" .. name] = sock["_" .. name] or sock[name]
hooked[name] = func
end
end
for name, func in pairs(hooked) do
sock[name] = func
end
return sock
end
-- wait socket events
function sandbox_core_base_socket_instance.wait(sock, events, timeout)
local events, errors = sock:_wait(events, timeout)
if events < 0 and errors then
raise(errors)
end
return events
end
-- control socket
function sandbox_core_base_socket_instance.ctrl(sock, code, value)
local ok, errors = sock:_ctrl(code, value)
if not ok and errors then
raise(errors)
end
return ok
end
-- get peer address
function sandbox_core_base_socket_instance.peeraddr(sock)
local result, errors = sock:_peeraddr(data, opt)
if not result and errors then
raise(errors)
end
return result
end
-- bind socket
function sandbox_core_base_socket_instance.bind(sock, addr, port)
local ok, errors = sock:_bind(addr, port)
if not ok and errors then
raise(errors)
end
return ok
end
-- listen socket
function sandbox_core_base_socket_instance.listen(sock, backlog)
local ok, errors = sock:_listen(backlog)
if not ok and errors then
raise(errors)
end
return ok
end
-- accept socket
function sandbox_core_base_socket_instance.accept(sock, opt)
local client_sock, errors = sock:_accept(opt)
if not client_sock and errors then
raise(errors)
end
return client_sock and _socket_wrap(client_sock) or nil
end
-- connect socket
function sandbox_core_base_socket_instance.connect(sock, addr, port, opt)
local ok, errors = sock:_connect(addr, port, opt)
if ok < 0 and errors then
raise(errors)
end
return ok
end
-- send data to socket
function sandbox_core_base_socket_instance.send(sock, data, opt)
local real, errors = sock:_send(data, opt)
if real < 0 and errors then
raise(errors)
end
return real
end
-- send file to socket
function sandbox_core_base_socket_instance.sendfile(sock, file, opt)
local real, errors = sock:_sendfile(file, opt)
if real < 0 and errors then
raise(errors)
end
return real
end
-- recv data from socket
function sandbox_core_base_socket_instance.recv(sock, buff, size, opt)
local real, data_or_errors = sock:_recv(buff, size, opt)
if real < 0 and data_or_errors then
raise(data_or_errors)
end
return real, data_or_errors
end
-- send udp data to peer
function sandbox_core_base_socket_instance.sendto(sock, data, addr, port, opt)
local real, errors = sock:_sendto(data, addr, port, opt)
if real < 0 and errors then
raise(errors)
end
return real
end
-- recv udp data from peer
function sandbox_core_base_socket_instance.recvfrom(sock, buff, size, opt)
local real, data_or_errors, addr, port = sock:_recvfrom(buff, size, opt)
if real < 0 and data_or_errors then
raise(data_or_errors)
end
return real, data_or_errors, addr, port
end
-- get socket rawfd
function sandbox_core_base_socket_instance.rawfd(sock)
local result, errors = sock:_rawfd()
if not result then
raise(errors)
end
return result
end
-- close socket
function sandbox_core_base_socket_instance.close(sock)
local ok, errors = sock:_close()
if not ok then
raise(errors)
end
end
-- open socket
function sandbox_core_base_socket.open(socktype, family)
local sock, errors = socket.open(socktype, family)
if not sock then
raise(errors)
end
return _socket_wrap(sock)
end
-- open tcp socket
function sandbox_core_base_socket.tcp(opt)
local sock, errors = socket.tcp(opt)
if not sock then
raise(errors)
end
return _socket_wrap(sock)
end
-- open udp socket
function sandbox_core_base_socket.udp(opt)
local sock, errors = socket.udp(opt)
if not sock then
raise(errors)
end
return _socket_wrap(sock)
end
-- open unix socket
function sandbox_core_base_socket.unix(opt)
local sock, errors = socket.unix(opt)
if not sock then
raise(errors)
end
return _socket_wrap(sock)
end
-- open and bind tcp socket
function sandbox_core_base_socket.bind(addr, port, opt)
local sock, errors = socket.bind(addr, port, opt)
if not sock then
raise(errors)
end
return _socket_wrap(sock)
end
-- open and bind tcp socket from the unix socket
function sandbox_core_base_socket.bind_unix(addr, opt)
local sock, errors = socket.bind_unix(addr, opt)
if not sock then
raise(errors)
end
return _socket_wrap(sock)
end
-- open and connect tcp socket
function sandbox_core_base_socket.connect(addr, port, opt)
local sock, errors = socket.connect(addr, port, opt)
if not sock and errors then
raise(errors)
end
return sock and _socket_wrap(sock) or nil
end
-- open and connect tcp socket from the unix socket
function sandbox_core_base_socket.connect_unix(addr, opt)
local sock, errors = socket.connect_unix(addr, opt)
if not sock and errors then
raise(errors)
end
return sock and _socket_wrap(sock) or nil
end
-- return module
return sandbox_core_base_socket
|
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.