diff options
| author | ruki <[email protected]> | 2018-11-21 23:55:49 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-11-21 17:34:57 +0800 |
| commit | e18261cb79944a7797e174816f345e4ed2a20d1d (patch) | |
| tree | ebdb9c9ef321f0bf6641b3aad0c0aa6c67265181 | |
| parent | 6902984e5c200e03f0d9f4a99d0a0801ee1acabc (diff) | |
add requires info for project
| -rw-r--r-- | xmake/actions/require/impl/package.lua | 37 | ||||
| -rw-r--r-- | xmake/actions/require/install.lua | 41 | ||||
| -rw-r--r-- | xmake/actions/require/list.lua | 11 | ||||
| -rw-r--r-- | xmake/core/project/project.lua | 108 | ||||
| -rw-r--r-- | xmake/core/project/requireinfo.lua | 158 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/project/project.lua | 14 |
6 files changed, 259 insertions, 110 deletions
diff --git a/xmake/actions/require/impl/package.lua b/xmake/actions/require/impl/package.lua index 814378276..b4eb3f7d3 100644 --- a/xmake/actions/require/impl/package.lua +++ b/xmake/actions/require/impl/package.lua @@ -243,31 +243,26 @@ function _load_packages(requires, opt) local packages = {} for packagename, requireinfo in pairs(load_requires(requires, opt.requires_extra, opt.parentinfo)) do - -- attempt to get project option about this package - local packageopt = os.isfile(os.projectfile()) and project.option(packagename) or nil - if packageopt == nil or packageopt:enabled() then -- this package is enabled? + -- load package + local package = _load_package(packagename, requireinfo) - -- load package package - local package = _load_package(packagename, requireinfo) - - -- maybe package not found and optional - if package then + -- maybe package not found and optional + if package then - -- load dependent packages and save them first of this package - local deps = package:get("deps") - if deps and opt.nodeps ~= true then - local packagedeps = {} - for _, dep in ipairs(_load_packages(deps, {requires_extra = package:get("__extra_deps"), parentinfo = requireinfo, nodeps = opt.nodeps})) do - table.insert(packages, dep) - packagedeps[dep:name()] = dep - end - package._DEPS = packagedeps - package._ORDERDEPS = table.unique(_sort_packagedeps(package)) + -- load dependent packages and save them first of this package + local deps = package:get("deps") + if deps and opt.nodeps ~= true then + local packagedeps = {} + for _, dep in ipairs(_load_packages(deps, {requires_extra = package:get("__extra_deps"), parentinfo = requireinfo, nodeps = opt.nodeps})) do + table.insert(packages, dep) + packagedeps[dep:name()] = dep end - - -- save this package package - table.insert(packages, package) + package._DEPS = packagedeps + package._ORDERDEPS = table.unique(_sort_packagedeps(package)) end + + -- save this package package + table.insert(packages, package) end end diff --git a/xmake/actions/require/install.lua b/xmake/actions/require/install.lua index f42380d2a..b0ffb4f0b 100644 --- a/xmake/actions/require/install.lua +++ b/xmake/actions/require/install.lua @@ -31,42 +31,43 @@ import("impl.package") import("impl.repository") import("impl.environment") --- attach package to option -function _attach_to_option(instance, opt) +-- register the required local package +function _register_required_package(instance, requireinfo) - -- disable this option if this package is optional and missing + -- disable it if this package is optional and missing if _g.optional_missing[instance:name()] then - opt:enable(false) + requireinfo:enable(false) else + -- add this package info + requireinfo:add(instance:fetch()) - -- add this package info to option - opt:add(instance:fetch()) - - -- add all dependent packages info to option + -- add all dependent packages info local orderdeps = instance:orderdeps() if orderdeps then local total = #orderdeps for idx, _ in ipairs(orderdeps) do local dep = orderdeps[total + 1 - idx] if dep then - opt:add((dep:fetch())) + requireinfo:add((dep:fetch())) end end end + + -- enable this require info + requireinfo:enable(true) end - -- update option info to the cache file - opt:save() + -- save this require info and flush the whole cache file + requireinfo:save() end --- attach all packages to targets -function _attach_to_targets(packages) - +-- register all required local packages +function _register_required_packages(packages) for _, instance in ipairs(packages) do if instance:kind() ~= "binary" then - local opt = project.option(instance:alias() or instance:name()) - if opt and opt:enabled() then - _attach_to_option(instance, opt) + local requireinfo = project.require(instance:alias() or instance:name()) + if requireinfo then + _register_required_package(instance, requireinfo) end end end @@ -107,7 +108,7 @@ function main(requires) -- init requires local requires_extra = nil if not requires then - requires, requires_extra = project.requires() + requires, requires_extra = project.get("requires"), project.get("__extra_requires") end if not requires or #requires == 0 then return @@ -149,8 +150,8 @@ function main(requires) -- check missing packages _check_missing_packages(packages) - -- attach required local package to targets - _attach_to_targets(packages) + -- register all required local packages + _register_required_packages(packages) end -- leave environment diff --git a/xmake/actions/require/list.lua b/xmake/actions/require/list.lua index 066ed2a64..21f979cdb 100644 --- a/xmake/actions/require/list.lua +++ b/xmake/actions/require/list.lua @@ -58,7 +58,7 @@ function main() print("The package dependencies:") -- get requires - local requires, requires_extra = project.requires() + local requires, requires_extra = project.get("requires"), project.get("__extra_requires") if not requires or #requires == 0 then return end @@ -73,12 +73,9 @@ function main() -- list all packages for _, instance in ipairs(package.load_packages(requires, {requires_extra = requires_extra})) do - local packageopt = project.option(instance:alias() or instance:name()) - if packageopt then - cprint(" ${magenta}require${clear}(%s): %s", instance:requireinfo().originstr, _info(instance)) - for _, dep in ipairs(instance:orderdeps()) do - cprint(" -> ${magenta}dep${clear}(%s): %s", dep:requireinfo().originstr, _info(dep)) - end + cprint(" ${magenta}require${clear}(%s): %s", instance:requireinfo().originstr, _info(instance)) + for _, dep in ipairs(instance:orderdeps()) do + cprint(" -> ${magenta}dep${clear}(%s): %s", dep:requireinfo().originstr, _info(dep)) end end diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 1386ebc26..71920be07 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -40,6 +40,7 @@ local rule = require("project/rule") local target = require("project/target") local config = require("project/config") local option = require("project/option") +local requireinfo = require("project/requireinfo") local deprecated_project = require("project/deprecated/project") local package = require("package/package") local platform = require("platform/platform") @@ -446,46 +447,6 @@ function project._load_options(disable_filter) return nil, errors end - -- load the options from the project requires - local requires_extra = project.get("__extra_requires") or {} - for _, require_str in ipairs(table.wrap(project.get("requires"))) do - - -- get the package name - local packagename = require_str:split('%s+')[1] - - -- check - assert(not results[packagename], "requires(\"" .. packagename .. "\") and option(\"" .. packagename .. "\") conflicts!") - - -- define package option - local packageopt = {category = "requires", default = true, showmenu = true, description = "The " .. packagename .. " package"} - - -- inherit extra option settings and override the default values - local alias = nil - local require_extra = requires_extra[require_str] - if require_extra then - - -- override values from the extra option - local interp = project.interpreter() - for name, value in pairs(table.wrap(require_extra.option)) do - if type(value) == "function" then - local maps = {on_check = "check", before_check = "check_before", after_check = "check_after"} - local scriptname = maps[name] - if scriptname then - packageopt[scriptname] = interp:_script(value) - end - else - packageopt[name] = value - end - end - - -- get alias - alias = require_extra.alias - end - - -- add option - results[alias or packagename] = packageopt - end - -- check options local options = {} for optionname, optioninfo in pairs(results) do @@ -521,32 +482,38 @@ function project._load_options(disable_filter) return options end --- get the project file -function project.file() - return os.projectfile() -end +-- load requires +function project._load_requires() --- get the project directory -function project.directory() - return os.projectdir() -end + -- parse requires + local requires = {} + local requires_extra = project.get("__extra_requires") or {} + for _, requirestr in ipairs(table.wrap(project.get("requires"))) do --- get the project info from the given name -function project.get(name) + -- get the package name + local packagename = requirestr:split('%s+')[1] - -- load the global project infos - local infos = project._INFOS - if not infos then + -- init a require info instance + local instance = table.inherit(requireinfo) + assert(instance) - -- load infos - infos = project._load_scope(nil, true, true) - project._INFOS = infos - end + -- get alias + local alias = nil + local extrainfo = requires_extra[requirestr] + if extrainfo then + alias = extrainfo.alias + end - -- get it - if infos then - return infos[name] + -- save name and info + instance._NAME = packagename + instance._INFO = { requirestr = requirestr, extrainfo = extrainfo } + + -- add require info + requires[alias or packagename] = instance end + + -- ok? + return requires end -- clear project cache to reload targets and options @@ -604,6 +571,27 @@ function project.options() return project._OPTIONS end +-- get the given require info +function project.require(name) + return project.requires()[name] +end + +-- get requires info +function project.requires() + + -- load requires + if not project._REQUIRES then + local requires, errors = project._load_requires() + if not requires then + os.raise(errors) + end + project._REQUIRES = requires + end + + -- ok + return project._REQUIRES +end + -- get the given rule function project.rule(name) return project.rules()[name] diff --git a/xmake/core/project/requireinfo.lua b/xmake/core/project/requireinfo.lua new file mode 100644 index 000000000..257de6ffe --- /dev/null +++ b/xmake/core/project/requireinfo.lua @@ -0,0 +1,158 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2018, TBOOX Open Source Group. +-- +-- @author ruki +-- @file requireinfo.lua +-- + +-- define module +local requireinfo = requireinfo 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 cache = require("project/cache") + +-- get cache +function requireinfo._cache() + + -- get it from cache first if exists + if requireinfo._CACHE then + return requireinfo._CACHE + end + + -- init cache + requireinfo._CACHE = cache("local.requires") + + -- ok + return requireinfo._CACHE +end + +-- save the requires info to the cache +function requireinfo:save() + requireinfo._cache():set(self:name(), self._INFO) + requireinfo._cache():flush() +end + +-- clear the requireinfo status and need recheck it +function requireinfo:clear() + requireinfo._cache():set(self:name(), nil) +end + +-- dump this requireinfo +function requireinfo:dump() + table.dump(self._INFO) +end + +-- get the require info +function requireinfo:get(infoname) + return self._INFO[infoname] +end + +-- get the require name +function requireinfo:name() + return self._NAME +end + +-- get the require string +function requireinfo:requirestr() + return self:get("requirestr") +end + +-- get the extra info from the given name +function requireinfo:extra(name) + local extrainfo = self:extrainfo() + if extrainfo then + return extrainfo[name] + end +end + +-- get the extra info +function requireinfo:extrainfo() + return self:get("extrainfo") +end + +-- set the value to the requires info +function requireinfo:set(name_or_info, ...) + if type(name_or_info) == "string" then + local args = ... + if args ~= nil then + self._INFO[name_or_info] = table.unwrap(table.unique(table.join(...))) + else + self._INFO[name_or_info] = nil + end + elseif table.is_dictionary(name_or_info) then + for name, info in pairs(table.join(name_or_info, ...)) do + self:set(name, info) + end + end +end + +-- add the value to the requires info +function requireinfo: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, ...))) + elseif table.is_dictionary(name_or_info) then + for name, info in pairs(table.join(name_or_info, ...)) do + self:add(name, info) + end + end +end + +-- this require info is enabled? +function requireinfo:enabled() + return self:get("enabled") +end + +-- enable or disable this require info +-- +-- @param enabled enable it? +-- +function requireinfo:enable(enabled) + self:set("enabled", enabled) +end + +-- load the requires info from the cache +function requireinfo.load(name) + + -- check + assert(name) + + -- get info + local info = requireinfo._cache():get(name) + if info == nil then + return + end + + -- init requireinfo instance + local instance = table.inherit(requireinfo) + instance._INFO = info + instance._NAME = name + + -- ok + return instance +end + +-- return module +return requireinfo diff --git a/xmake/core/sandbox/modules/import/core/project/project.lua b/xmake/core/sandbox/modules/import/core/project/project.lua index 227427c7c..99ef93fb7 100644 --- a/xmake/core/sandbox/modules/import/core/project/project.lua +++ b/xmake/core/sandbox/modules/import/core/project/project.lua @@ -178,6 +178,11 @@ function sandbox_core_project.version() return version, version_build end +-- get the project info from the given name +function sandbox_core_project.get(name) + return project.get(name) +end + -- get the project name function sandbox_core_project.name() return project.get("project") @@ -188,9 +193,14 @@ function sandbox_core_project.modes() return project.get("modes") end --- get the project requires +-- get the the given require info +function sandbox_core_project.require(name) + return project.require(name) +end + +-- get the all requires function sandbox_core_project.requires() - return project.get("requires"), project.get("__extra_requires") + return project.requires() end -- return module |
