diff options
| author | ruki <[email protected]> | 2017-09-18 22:54:13 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-09-18 11:25:24 +0800 |
| commit | 8f55f2e39ae47b7e9bc206d5ddf62a99d25c9d0f (patch) | |
| tree | 18ddc8e1a86fe0c8123be6924ab1d02d1013e177 | |
| parent | 9e730f52fd7a2dfecefc88ca620db855d6aa668b (diff) | |
add add_imports to bulk import modules
| -rw-r--r-- | CHANGELOG.md | 8 | ||||
| -rw-r--r-- | xmake/core/base/interpreter.lua | 4 | ||||
| -rw-r--r-- | xmake/core/package/package.lua | 62 | ||||
| -rw-r--r-- | xmake/core/project/option.lua | 55 | ||||
| -rw-r--r-- | xmake/core/project/project.lua | 2 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 58 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/sandbox/module.lua | 38 | ||||
| -rw-r--r-- | xmake/core/sandbox/sandbox.lua | 6 |
8 files changed, 152 insertions, 81 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index da307a965..31478c741 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### New features + +* Add `add_imports` to bulk import modules for the target, option and package script + ### Changes * Support 24bits truecolors for `cprint()` @@ -357,6 +361,10 @@ ## master (开发中) +### 新特性 + +* 添加`add_imports`去为target,option和package的自定义脚本批量导入模块,简化自定义脚本 + ### 改进 * 改进`cprint()`,支持24位真彩色输出 diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index abd5a3945..8be310ed8 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -1079,8 +1079,8 @@ function interpreter:api_register_set_module(scope_kind, ...) os.raise("set_%s(): %s", name, errors) end - -- import the module - local module, errors = instance:import() + -- load the module + local module, errors = instance:module() if not module then os.raise("set_%s(): %s", name, errors) end diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index fb07bf517..a1f19b8a5 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -27,19 +27,20 @@ local package = package or {} local _instance = _instance or {} -- load modules -local os = require("base/os") -local io = require("base/io") -local path = require("base/path") -local utils = require("base/utils") -local table = require("base/table") -local filter = require("base/filter") -local global = require("base/global") -local interpreter = require("base/interpreter") -local sandbox = require("sandbox/sandbox") -local config = require("project/config") -local project = require("project/project") -local platform = require("platform/platform") -local import = require("sandbox/modules/import") +local os = require("base/os") +local io = require("base/io") +local path = require("base/path") +local utils = require("base/utils") +local table = require("base/table") +local filter = require("base/filter") +local global = require("base/global") +local interpreter = require("base/interpreter") +local sandbox = require("sandbox/sandbox") +local config = require("project/config") +local project = require("project/project") +local platform = require("platform/platform") +local sandbox = require("sandbox/sandbox") +local sandbox_module = require("sandbox/modules/import/core/sandbox/module") -- new an instance function _instance.new(name, info, rootdir) @@ -230,8 +231,9 @@ function _instance:script(name, generic) -- get script local script = self:get(name) + local result = nil if type(script) == "function" then - return script + result = script elseif type(script) == "table" then -- match script for special plat and arch @@ -239,23 +241,40 @@ function _instance:script(name, generic) local pattern = plat .. '|' .. (config.get("arch") or "") for _pattern, _script in pairs(script) do if not _pattern:startswith("__") and pattern:find('^' .. _pattern .. '$') then - return _script + result = _script + break end end -- match script for special plat - for _pattern, _script in pairs(script) do - if not _pattern:startswith("__") and plat:find('^' .. _pattern .. '$') then - return _script + if result == nil then + for _pattern, _script in pairs(script) do + if not _pattern:startswith("__") and plat:find('^' .. _pattern .. '$') then + result = _script + break + end end end -- get generic script - return script["__generic__"] or generic + result = result or script["__generic__"] or generic end -- only generic script - return generic + result = result or generic + + -- imports some modules first + if result then + local scope = getfenv(result) + if scope then + for _, modulename in ipairs(table.wrap(self:get("imports"))) do + scope[sandbox_module.name(modulename)] = sandbox_module.import(modulename, {anonymous = true}) + end + end + end + + -- ok + return result end -- fetch package info from the local packages @@ -265,7 +284,7 @@ end function _instance:fetch() -- import find_package - self._find_package = self._find_package or import("lib.detect.find_package", {anonymous = true}) + self._find_package = self._find_package or sandbox_module.import("lib.detect.find_package", {anonymous = true}) -- fetch it from the package directories first local fetchfrom = self._FETCHFROM @@ -338,6 +357,7 @@ function package.apis() , "package.set_description" -- package.add_xxx , "package.add_deps" + , "package.add_imports" } , script = { diff --git a/xmake/core/project/option.lua b/xmake/core/project/option.lua index b0371903c..238d5f937 100644 --- a/xmake/core/project/option.lua +++ b/xmake/core/project/option.lua @@ -26,19 +26,20 @@ local option = option or {} -- load modules -local io = require("base/io") -local os = require("base/os") -local path = require("base/path") -local table = require("base/table") -local utils = require("base/utils") -local option_ = require("base/option") -local config = require("project/config") -local cache = require("project/cache") -local linker = require("tool/linker") -local compiler = require("tool/compiler") -local sandbox = require("sandbox/sandbox") -local language = require("language/language") -local import = require("sandbox/modules/import") +local io = require("base/io") +local os = require("base/os") +local path = require("base/path") +local table = require("base/table") +local utils = require("base/utils") +local option_ = require("base/option") +local config = require("project/config") +local cache = require("project/cache") +local linker = require("tool/linker") +local compiler = require("tool/compiler") +local sandbox = require("sandbox/sandbox") +local language = require("language/language") +local sandbox = require("sandbox/sandbox") +local sandbox_module = require("sandbox/modules/import/core/sandbox/module") -- get cache function option._cache() @@ -76,7 +77,7 @@ end function option:_cx_check() -- import check_cxsnippets() - self._check_cxsnippets = self._check_cxsnippets or import("lib.detect.check_cxsnippets") + self._check_cxsnippets = self._check_cxsnippets or sandbox_module.import("lib.detect.check_cxsnippets", {anonymous = true}) -- check for c and c++ for _, kind in ipairs({"c", "cxx"}) do @@ -122,7 +123,7 @@ end function option:_on_check() -- get check script - local check = self:get("check") + local check = self:script("check") if check then return sandbox.load(check, self) else @@ -165,8 +166,8 @@ function option:check() end -- before and after check - local check_before = self:get("check_before") - local check_after = self:get("check_after") + local check_before = self:script("check_before") + local check_after = self:script("check_after") if check_before then check_before(self) end @@ -341,5 +342,25 @@ function option.save() option._cache():flush() end +-- get xxx_script +function option:script(name) + + -- get script + local script = self:get(name) + + -- imports some modules first + if script then + local scope = getfenv(script) + if scope then + for _, modulename in ipairs(table.wrap(self:get("imports"))) do + scope[sandbox_module.name(modulename)] = sandbox_module.import(modulename, {anonymous = true}) + end + end + end + + -- ok + return script +end + -- return module return option diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 9dddbe8f8..2d6543fe0 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -225,6 +225,7 @@ function project.interpreter() -- target.add_xxx , "target.add_deps" , "target.add_options" + , "target.add_imports" , "target.add_languages" , "target.add_vectorexts" -- option.set_xxx @@ -237,6 +238,7 @@ function project.interpreter() , "option.set_description" -- option.add_xxx , "option.add_deps" + , "option.add_imports" , "option.add_vectorexts" } , pathes = diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index f2c12bbd2..99649216a 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -26,18 +26,20 @@ local target = target or {} -- load modules -local os = require("base/os") -local path = require("base/path") -local utils = require("base/utils") -local table = require("base/table") -local deprecated = require("base/deprecated") -local option = require("project/option") -local config = require("project/config") -local tool = require("tool/tool") -local linker = require("tool/linker") -local compiler = require("tool/compiler") -local platform = require("platform/platform") -local language = require("language/language") +local os = require("base/os") +local path = require("base/path") +local utils = require("base/utils") +local table = require("base/table") +local deprecated = require("base/deprecated") +local option = require("project/option") +local config = require("project/config") +local tool = require("tool/tool") +local linker = require("tool/linker") +local compiler = require("tool/compiler") +local platform = require("platform/platform") +local language = require("language/language") +local sandbox = require("sandbox/sandbox") +local sandbox_module = require("sandbox/modules/import/core/sandbox/module") -- get the filename from the given target name and kind function target.filename(targetname, targetkind, targetformat) @@ -694,8 +696,9 @@ function target:script(name, generic) -- get script local script = self:get(name) + local result = nil if type(script) == "function" then - return script + result = script elseif type(script) == "table" then -- match script for special plat and arch @@ -703,23 +706,40 @@ function target:script(name, generic) local pattern = plat .. '|' .. (config.get("arch") or "") for _pattern, _script in pairs(script) do if not _pattern:startswith("__") and pattern:find('^' .. _pattern .. '$') then - return _script + result = _script + break end end -- match script for special plat - for _pattern, _script in pairs(script) do - if not _pattern:startswith("__") and plat:find('^' .. _pattern .. '$') then - return _script + if result == nil then + for _pattern, _script in pairs(script) do + if not _pattern:startswith("__") and plat:find('^' .. _pattern .. '$') then + result = _script + break + end end end -- get generic script - return script["__generic__"] or generic + result = result or script["__generic__"] or generic end -- only generic script - return generic + result = result or generic + + -- imports some modules first + if result then + local scope = getfenv(result) + if scope then + for _, modulename in ipairs(table.wrap(self:get("imports"))) do + scope[sandbox_module.name(modulename)] = sandbox_module.import(modulename, {anonymous = true}) + end + end + end + + -- ok + return result end -- get the config header prefix diff --git a/xmake/core/sandbox/modules/import/core/sandbox/module.lua b/xmake/core/sandbox/modules/import/core/sandbox/module.lua index 072b09912..12b2db3f7 100644 --- a/xmake/core/sandbox/modules/import/core/sandbox/module.lua +++ b/xmake/core/sandbox/modules/import/core/sandbox/module.lua @@ -35,22 +35,6 @@ local global = require("base/global") local sandbox = require("sandbox/sandbox") local raise = require("sandbox/modules/raise") --- get module name -function core_sandbox_module._modulename(name) - - -- check - assert(name) - - -- find modulename - local i = name:find_last(".", true) - if i then - name = name:sub(i + 1) - end - - -- get it - return name -end - -- get module path from name function core_sandbox_module._modulepath(name) @@ -98,8 +82,8 @@ function core_sandbox_module._loadfile(filepath, instance) return nil, errors end - -- import module - local result, errors = instance:import() + -- load module + local result, errors = instance:module() if not result then return nil, errors end @@ -237,6 +221,22 @@ function core_sandbox_module._load(dir, name, instance, module) return module, script end +-- get module name +function core_sandbox_module.name(name) + + -- check + assert(name) + + -- find modulename + local i = name:find_last(".", true) + if i then + name = name:sub(i + 1) + end + + -- get it + return name +end + -- get module directories function core_sandbox_module.directories() @@ -326,7 +326,7 @@ function core_sandbox_module.import(name, opt) assert(scope_parent) -- get module name - local modulename = core_sandbox_module._modulename(name) + local modulename = core_sandbox_module.name(name) if not modulename then raise("cannot get module name for %s", name) end diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua index 0be6b604b..85562e908 100644 --- a/xmake/core/sandbox/sandbox.lua +++ b/xmake/core/sandbox/sandbox.lua @@ -238,10 +238,10 @@ function sandbox:fork(script, rootdir) return instance end --- load script and import module -function sandbox:import() +-- load script and module +function sandbox:module() - -- this module has been imported? + -- this module has been loaded? if self._PRIVATE._MODULE then return self._PRIVATE._MODULE end |
