diff options
| author | ruki <[email protected]> | 2019-02-10 20:13:21 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-02-10 20:13:21 +0800 |
| commit | 8be63d51387bc352bddb7b891a231fdecb894344 (patch) | |
| tree | 5a800cd354b6d0dee261f0f2087537faf88fe7b7 | |
| parent | 9a0559206f07c8cea19b63136bf0ad01ea005273 (diff) | |
improve project option
| -rw-r--r-- | xmake/core/package/package.lua | 1 | ||||
| -rw-r--r-- | xmake/core/project/option.lua | 253 | ||||
| -rw-r--r-- | xmake/core/project/project.lua | 8 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/project/option.lua | 6 | ||||
| -rw-r--r-- | xmake/modules/package/manager/xmake/find_package.lua | 22 |
5 files changed, 168 insertions, 122 deletions
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 8567ca95a..cae402f3d 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -48,7 +48,6 @@ function _instance.new(name, info) local instance = table.inherit(_instance) -- init instance - instance._name = name instance._NAME = name instance._INFO = info diff --git a/xmake/core/project/option.lua b/xmake/core/project/option.lua index c6592796c..0acecb711 100644 --- a/xmake/core/project/option.lua +++ b/xmake/core/project/option.lua @@ -24,6 +24,7 @@ -- define module local option = option or {} +local _instance = _instance or {} -- load modules local io = require("base/io") @@ -32,6 +33,8 @@ local path = require("base/path") local table = require("base/table") local utils = require("base/utils") local baseoption = require("base/option") +local global = require("base/global") +local interpreter = require("base/interpreter") local config = require("project/config") local cache = require("project/cache") local linker = require("tool/linker") @@ -41,62 +44,22 @@ 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() +-- new an instance +function _instance.new(name, info) - -- get it from cache first if exists - if option._CACHE then - return option._CACHE - end + -- new an instance + local instance = table.inherit(_instance) - -- init cache - option._CACHE = cache("local.option") + -- init instance + instance._NAME = name + instance._INFO = info -- ok - return option._CACHE -end - --- get option apis -function option.apis() - - return - { - values = - { - -- option.set_xxx - "option.set_values" - , "option.set_default" - , "option.set_showmenu" - , "option.set_category" - , "option.set_warnings" - , "option.set_optimize" - , "option.set_languages" - , "option.set_description" - -- option.add_xxx - , "option.add_deps" - , "option.add_imports" - , "option.add_vectorexts" - , "option.add_features" - } - , keyvalues = - { - -- option.set_xxx - "option.set_configvar" - } - , script = - { - -- option.before_xxx - "option.before_check" - -- option.on_xxx - , "option.on_check" - -- option.after_xxx - , "option.after_check" - } - } + return instance end -- save the option info to the cache -function option:_save() +function _instance:_save() -- clear scripts for caching to file self:set("check", nil) @@ -108,12 +71,12 @@ function option:_save() end -- clear the option info for cache -function option:_clear() +function _instance:_clear() option._cache():set(self:name(), nil) end -- check option conditions -function option:_do_check() +function _instance:_do_check() -- import check_cxsnippets() self._check_cxsnippets = self._check_cxsnippets or sandbox_module.import("lib.detect.check_cxsnippets", {anonymous = true}) @@ -189,7 +152,7 @@ function option:_do_check() end -- on check -function option:_on_check() +function _instance:_on_check() -- get check script local check = self:script("check") @@ -201,7 +164,7 @@ function option:_on_check() end -- check option -function option:_check() +function _instance:_check() -- disable this option first self:enable(false) @@ -226,7 +189,7 @@ function option:_check() end -- attempt to check option -function option:check() +function _instance:check() -- the option name local name = self:name() @@ -270,12 +233,12 @@ function option:check() end -- get the option value -function option:value() +function _instance:value() return config.get(self:name()) end -- set the option value -function option:set_value(value) +function _instance:set_value(value) -- set value to option config.set(self:name(), value) @@ -285,7 +248,7 @@ function option:set_value(value) end -- clear the option status and need recheck it -function option:clear() +function _instance:clear() -- clear config config.set(self:name(), nil) @@ -295,7 +258,7 @@ function option:clear() end -- this option is enabled? -function option:enabled() +function _instance:enabled() return config.get(self:name()) end @@ -304,7 +267,7 @@ end -- @param enabled enable option? -- @param opt the argument options, .e.g {readonly = true, force = false} -- -function option:enable(enabled, opt) +function _instance:enable(enabled, opt) -- init options opt = opt or {} @@ -323,22 +286,22 @@ function option:enable(enabled, opt) end -- dump this option -function option:dump() +function _instance:dump() table.dump(self._INFO) end -- get the type: option -function option:type() +function _instance:type() return "option" end -- get the option info -function option:get(infoname) +function _instance:get(infoname) return self._INFO[infoname] end -- set the value to the option info -function option:set(name_or_info, ...) +function _instance:set(name_or_info, ...) if type(name_or_info) == "string" then local args = ... if args ~= nil then @@ -354,7 +317,7 @@ function option:set(name_or_info, ...) end -- add the value to the option info -function option:add(name_or_info, ...) +function _instance:add(name_or_info, ...) if type(name_or_info) == "string" then local info = table.wrap(self._INFO[name_or_info]) self._INFO[name_or_info] = table.unwrap(table.unique(table.join(info, ...))) @@ -366,7 +329,7 @@ function option:add(name_or_info, ...) end -- get the given dependent option -function option:dep(name) +function _instance:dep(name) local deps = self:deps() if deps then return deps[name] @@ -374,48 +337,22 @@ function option:dep(name) end -- get option deps -function option:deps() +function _instance:deps() return self._DEPS end -- get option order deps -function option:orderdeps() +function _instance:orderdeps() return self._ORDERDEPS end -- get the option name -function option:name() +function _instance:name() return self._NAME end --- load the option info from the cache -function option.load(name) - - -- check - assert(name) - - -- get info - local info = option._cache():get(name) - if info == nil then - return - end - - -- init option instance - local instance = table.inherit(option) - instance._INFO = info - instance._NAME = name - - -- ok - return instance -end - --- save all options to the cache file -function option.save() - option._cache():flush() -end - -- get xxx_script -function option:script(name) +function _instance:script(name) -- get script local script = self:get(name) @@ -434,5 +371,131 @@ function option:script(name) return script end +-- get cache +function option._cache() + + -- get it from cache first if exists + if option._CACHE then + return option._CACHE + end + + -- init cache + option._CACHE = cache("local.option") + + -- ok + return option._CACHE +end + +-- get option apis +function option.apis() + + return + { + values = + { + -- option.set_xxx + "option.set_values" + , "option.set_default" + , "option.set_showmenu" + , "option.set_category" + , "option.set_warnings" + , "option.set_optimize" + , "option.set_languages" + , "option.set_description" + -- option.add_xxx + , "option.add_deps" + , "option.add_imports" + , "option.add_vectorexts" + , "option.add_features" + } + , keyvalues = + { + -- option.set_xxx + "option.set_configvar" + } + , script = + { + -- option.before_xxx + "option.before_check" + -- option.on_xxx + , "option.on_check" + -- option.after_xxx + , "option.after_check" + } + } +end + +-- get interpreter +function option.interpreter() + + -- the interpreter has been initialized? return it directly + if option._INTERPRETER then + return option._INTERPRETER + end + + -- init interpreter + local interp = interpreter.new() + + -- define apis for option + interp:api_define(option.apis()) + + -- define apis for language + interp:api_define(language.apis()) + + -- register filter handler + interp:filter():register("option", function (variable) + + -- init maps + local maps = + { + arch = function() return config.get("arch") or os.arch() end + , plat = function() return config.get("plat") or os.host() end + , mode = function() return config.get("mode") or "release" end + , host = os.host() + , globaldir = global.directory() + , configdir = config.directory() + , projectdir = os.projectdir() + , programdir = os.programdir() + } + + -- map it + local result = maps[variable] + if type(result) == "function" then + result = result() + end + return result + end) + + -- save interpreter + option._INTERPRETER = interp + + -- ok? + return interp +end + +-- new an option instance +function option.new(name, info) + return _instance.new(name, info) +end + +-- load the option info from the cache +function option.load(name) + + -- check + assert(name) + + -- get info + local info = option._cache():get(name) + if info == nil then + return + end + return option.new(name, info) +end + +-- save all options to the cache file +function option.save() + option._cache():flush() +end + -- return module return option diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 88da7811c..e09149a07 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -580,12 +580,8 @@ function project._load_options(disable_filter) local options = {} for optionname, optioninfo in pairs(results) do - -- init a option instance - local instance = table.inherit(option) - - -- save name and info - instance._NAME = optionname - instance._INFO = optioninfo + -- init an option instance + local instance = option.new(optionname, optioninfo) -- save it options[optionname] = instance diff --git a/xmake/core/sandbox/modules/import/core/project/option.lua b/xmake/core/sandbox/modules/import/core/project/option.lua index c8def7922..f2ebc3594 100644 --- a/xmake/core/sandbox/modules/import/core/project/option.lua +++ b/xmake/core/sandbox/modules/import/core/project/option.lua @@ -29,9 +29,9 @@ local sandbox_core_project_option = sandbox_core_project_option or {} local option = require("project/option") local raise = require("sandbox/modules/raise") --- get the option apis -function sandbox_core_project_option.apis() - return option.apis() +-- get the option interpreter +function sandbox_core_project_option.interpreter() + return option.interpreter() end -- return module diff --git a/xmake/modules/package/manager/xmake/find_package.lua b/xmake/modules/package/manager/xmake/find_package.lua index f662a626f..a1a29e9cd 100644 --- a/xmake/modules/package/manager/xmake/find_package.lua +++ b/xmake/modules/package/manager/xmake/find_package.lua @@ -24,7 +24,6 @@ -- imports import("core.base.global") -import("core.base.interpreter") import("core.project.config") import("core.project.option") import("core.project.target") @@ -172,13 +171,7 @@ function _find_package_from_packagedirs(name, opt) end -- init interpreter - local interp = interpreter.new() - - -- define apis for option - interp:api_define(option.apis()) - - -- define apis for language - interp:api_define(language.apis()) + local interp = option.interpreter() -- register filter handler interp:filter():register("find_package", function (variable) @@ -188,15 +181,7 @@ function _find_package_from_packagedirs(name, opt) { arch = opt.arch , plat = opt.plat - , mode = opt.mode or config.get("mode") - , host = os.host() - , tmpdir = function () return os.tmpdir() end - , curdir = function () return os.curdir() end - , scriptdir = function () return os.scriptdir() end - , globaldir = global.directory() - , configdir = config.directory() - , projectdir = os.projectdir() - , programdir = os.programdir() + , mode = opt.mode } -- get variable @@ -215,6 +200,9 @@ function _find_package_from_packagedirs(name, opt) raise(errors) end + -- unregister filter handler + interp:filter():register("find_package", nil) + -- get package info local packageinfo = packageinfos[name] if not packageinfo then |
