summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-04-10 21:01:46 +0800
committerruki <[email protected]>2018-04-10 00:10:39 +0800
commit483f101b3fe1502ce4daca8d47058dba4cd27dee (patch)
tree80b24ac81f7b0badf744489c000defe609b09c16
parentc59a2f071f53f88516bb487e5bcef37d027ecc43 (diff)
seperate project and global rules
-rw-r--r--xmake/core/main.lua2
-rw-r--r--xmake/core/project/project.lua43
-rw-r--r--xmake/core/project/rule.lua35
-rw-r--r--xmake/core/project/target.lua2
-rw-r--r--xmake/core/sandbox/modules/import/core/project/project.lua10
-rw-r--r--xmake/core/sandbox/modules/import/core/project/rule.lua10
6 files changed, 73 insertions, 29 deletions
diff --git a/xmake/core/main.lua b/xmake/core/main.lua
index c7a5c86b3..8d10219e5 100644
--- a/xmake/core/main.lua
+++ b/xmake/core/main.lua
@@ -36,7 +36,6 @@ local deprecated = require("base/deprecated")
local privilege = require("base/privilege")
local task = require("base/task")
local colors = require("base/colors")
-local rule = require("project/rule")
local project = require("project/project")
local history = require("project/history")
local package = require("package/package")
@@ -215,7 +214,6 @@ force to build in current directory via run `xmake -P .`]], os.projectdir())
-- define task, rule and package apis first before loading project's xmake.lua
project.define_apis(task.apis())
- project.define_apis(rule.apis())
project.define_apis(package.apis())
end
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua
index 424471854..33774b971 100644
--- a/xmake/core/project/project.lua
+++ b/xmake/core/project/project.lua
@@ -35,6 +35,7 @@ local global = require("base/global")
local process = require("base/process")
local deprecated = require("base/deprecated")
local interpreter = require("base/interpreter")
+local rule = require("project/rule")
local target = require("project/target")
local config = require("project/config")
local option = require("project/option")
@@ -197,6 +198,9 @@ function project.interpreter()
-- set root scope
interp:rootscope_set("target")
+ -- define apis for rule
+ interp:api_define(rule.apis())
+
-- define apis for target
interp:api_define(target.apis())
@@ -393,11 +397,19 @@ function project._load_targets()
targets[targetname] = target.new(targetname, targetinfo)
end
- -- load and attach target deps
+ -- load and attach target deps and rules
for _, target in pairs(targets) do
+
+ -- load deps
target._DEPS = target._DEPS or {}
target._ORDERDEPS = target._ORDERDEPS or {}
project._load_deps(target, targets, target._DEPS, target._ORDERDEPS)
+
+ -- load rules
+ target._RULES = target._RULES or {}
+ for _, rulename in ipairs(table.wrap(target:get("rules"))) do
+ target._RULES[rulename] = project.rule(rulename) or rule.rule(name)
+ end
end
-- enter toolchains environment
@@ -601,22 +613,41 @@ function project.options()
return project._OPTIONS
end
--- get rules
+-- get the given rule
+function project.rule(name)
+ return project.rules()[name]
+end
+
+-- get project rules
function project.rules()
+
+ -- return it directly if exists
+ if project._RULES then
+ return project._RULES
+ end
-- the project file is not found?
if not os.isfile(project.file()) then
- return {}, nil
+ return {}
end
-- load the rules from the the project file
local results, errors = project._load_scope("rule", true, true)
if not results then
- return nil, errors
+ os.raise(errors)
end
- -- ok
- return results
+ -- make rule instances
+ local rules = {}
+ for rulename, ruleinfo in pairs(results) do
+ rules[rulename] = rule.new(rulename, ruleinfo)
+ end
+
+ -- save it
+ project._RULES = rules
+
+ -- ok?
+ return project._RULES
end
-- get tasks
diff --git a/xmake/core/project/rule.lua b/xmake/core/project/rule.lua
index f915ca459..34d9bc843 100644
--- a/xmake/core/project/rule.lua
+++ b/xmake/core/project/rule.lua
@@ -30,7 +30,6 @@ local os = require("base/os")
local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
-local project = require("project/project")
local global = require("base/global")
local interpreter = require("base/interpreter")
local config = require("project/config")
@@ -42,8 +41,7 @@ local sandbox_module = require("sandbox/modules/import/core/sandbox/module")
function rule._directories()
-- the directories
- return { path.join(config.directory(), "rules")
- , path.join(global.directory(), "rules")
+ return { path.join(global.directory(), "rules")
, path.join(os.programdir(), "rules")
}
end
@@ -348,7 +346,12 @@ function rule:script(name, generic)
return result
end
--- get all rules
+-- get the given global rule
+function rule.rule(name)
+ return rule.rules()[name]
+end
+
+-- get global rules
function rule.rules()
-- return it directly if exists
@@ -373,33 +376,23 @@ function rule.rules()
if results then
table.join2(rules, results)
else
- return nil, errors
+ os.raise(errors)
end
end
end
end
- -- merge project rules if exists
- local project_rules, errors = project.rules()
- if project_rules then
-
- -- save rules
- for rulename, ruleinfo in pairs(project_rules) do
- if rules[rulename] == nil then
- rules[rulename] = ruleinfo
- else
- utils.warning("rule(\"%s\") has been defined!", rulename)
- end
- end
- else
- return nil, errors
+ -- make rule instances
+ local instances = {}
+ for rulename, ruleinfo in pairs(rules) do
+ instances[rulename] = rule.new(rulename, ruleinfo)
end
-- save it
- rule._RULES = rules
+ rule._RULES = instances
-- ok?
- return rules
+ return rule._RULES
end
-- return module
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index b9570ce37..0dc8892c1 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -31,6 +31,7 @@ local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local deprecated = require("base/deprecated")
+local rule = require("project/rule")
local option = require("project/option")
local config = require("project/config")
local tool = require("tool/tool")
@@ -51,6 +52,7 @@ function target.apis()
-- target.set_xxx
"target.set_kind"
, "target.set_strip"
+ , "target.set_rules"
, "target.set_default"
, "target.set_options"
, "target.set_symbols"
diff --git a/xmake/core/sandbox/modules/import/core/project/project.lua b/xmake/core/sandbox/modules/import/core/project/project.lua
index 9a48b8cf4..227427c7c 100644
--- a/xmake/core/sandbox/modules/import/core/project/project.lua
+++ b/xmake/core/sandbox/modules/import/core/project/project.lua
@@ -112,6 +112,16 @@ function sandbox_core_project.check()
end
end
+-- get the given project rule
+function sandbox_core_project.rule(name)
+ return project.rule(name)
+end
+
+-- get the all project rules
+function sandbox_core_project.rules()
+ return project.rules()
+end
+
-- get the given target
function sandbox_core_project.target(name)
return project.target(name)
diff --git a/xmake/core/sandbox/modules/import/core/project/rule.lua b/xmake/core/sandbox/modules/import/core/project/rule.lua
index 75a4975dd..0f7766a87 100644
--- a/xmake/core/sandbox/modules/import/core/project/rule.lua
+++ b/xmake/core/sandbox/modules/import/core/project/rule.lua
@@ -32,6 +32,16 @@ local project = require("project/project")
local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
+-- get the given global rule
+function sandbox_core_project_rule.rule(name)
+ return rule.rule(name)
+end
+
+-- get the all global rules
+function sandbox_core_project_rule.rules()
+ return rule.rules()
+end
+
-- build source files
function sandbox_core_project_rule.build(rulename, target, sourcefiles)