summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-11-11 22:14:13 +0800
committerruki <[email protected]>2017-11-11 22:14:13 +0800
commit025ba16c6bb7aca2590d92fde69c5cb4a7414cbc (patch)
tree75a85748c9b7f77891e1f9302837edd4352e4802
parentf50cc8971ad01966cb5bfbcd2bef354fdef42cd8 (diff)
add add_rules api
-rw-r--r--CHANGELOG.md2
-rw-r--r--tests/apis/rules/src/main.c1
-rw-r--r--tests/apis/rules/xmake.lua8
-rw-r--r--xmake/core/project/project.lua10
-rw-r--r--xmake/core/project/rule.lua4
-rw-r--r--xmake/core/project/target.lua58
6 files changed, 71 insertions, 12 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f1a86ffb1..38b694fa4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@
### New features
* Add `del_files()` api to delete files in the files list
+* Add `rule()`, `add_rules()` api to implement the custom build rule and improve `add_files("src/*.md", {rule = "markdown"})`
### Changes
@@ -410,6 +411,7 @@
### 新特性
* 添加`del_files()`接口去从已添加的文件列表中移除一些文件
+* 添加`rule()`, `add_rules()`接口实现自定义构建规则,并且改进`add_files("src/*.md", {rule = "markdown"})`
### 改进
diff --git a/tests/apis/rules/src/main.c b/tests/apis/rules/src/main.c
index f2e46be48..b86c9ad8e 100644
--- a/tests/apis/rules/src/main.c
+++ b/tests/apis/rules/src/main.c
@@ -4,6 +4,5 @@ extern int test(int a, int b);
int main(int argc, char** argv)
{
printf("1 + 1 = %d\n", test(1, 1));
- printf("%s\n", TEST);
return 0;
}
diff --git a/tests/apis/rules/xmake.lua b/tests/apis/rules/xmake.lua
index 545e53938..08b0cd0df 100644
--- a/tests/apis/rules/xmake.lua
+++ b/tests/apis/rules/xmake.lua
@@ -1,6 +1,7 @@
-- define rule: markdown
rule("markdown")
+ set_extensions(".md", ".markdown")
on_build(function (target, sourcefile)
os.cp(sourcefile, path.join(target:targetdir(), path.basename(sourcefile) .. ".html"))
end)
@@ -44,14 +45,15 @@ rule("stub")
-- define target
target("test")
- add_cxflags("-DTEST=\"D:/asdasd/adasd/adsad\"")
-
-- set kind
set_kind("binary")
+ -- add rules
+ add_rules("markdown")
+
-- add files
add_files("src/*.c")
add_files("src/man/*.in", {rule = "man"})
- add_files("src/index.md", {rule = "markdown"})
+ add_files("src/index.md")
add_files("src/test.c.in", {rule = "c code"})
add_files("src/empty.stub", {rule = "stub"})
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua
index 26c986b03..802d4e83d 100644
--- a/xmake/core/project/project.lua
+++ b/xmake/core/project/project.lua
@@ -423,11 +423,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)
+ end
end
-- enter toolchains environment
diff --git a/xmake/core/project/rule.lua b/xmake/core/project/rule.lua
index 31d53d753..31b84e30a 100644
--- a/xmake/core/project/rule.lua
+++ b/xmake/core/project/rule.lua
@@ -40,8 +40,10 @@ function rule.apis()
{
values =
{
+ -- rule.set_xxx
+ "rule.set_extensions"
-- rule.add_xxx
- "rule.add_imports"
+ , "rule.add_imports"
}
, script =
{
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index 23d65f384..87b53c0e7 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -60,6 +60,7 @@ function target.apis()
, "target.set_languages"
-- target.add_xxx
, "target.add_deps"
+ , "target.add_rules"
, "target.add_options"
, "target.add_imports"
, "target.add_languages"
@@ -256,6 +257,34 @@ function target:depconfig(name)
return depsconfig[name]
end
+-- get target rules
+function target:rules()
+ return self._RULES
+end
+
+-- get target rule from the given source extension
+function target:rule(extension)
+
+ -- get it from cache first
+ local extension2rules = self._EXTENSION2RULES
+ if not extension2rules then
+
+ -- make extension to rules
+ extension2rules = {}
+ for _, rule in pairs(table.wrap(self:rules())) do
+ for _, extension in ipairs(table.wrap(rule:get("extensions"))) do
+ extension2rules[extension] = rule
+ end
+ end
+ end
+
+ -- cache it
+ self._EXTENSION2RULES = extension2rules
+
+ -- ok?
+ return extension2rules[extension]
+end
+
-- is phony target?
function target:isphony()
@@ -376,6 +405,23 @@ function target:headerdir()
return self:get("headerdir") or config.get("buildir")
end
+-- get the source file rule name
+function target:filerule(sourcefile)
+
+ -- get file config
+ local fileconfig = self:fileconfig(sourcefile)
+
+ -- get rule name
+ if fileconfig and fileconfig.rule then
+ return fileconfig.rule
+ else
+ local rule = self:rule(path.extension(sourcefile))
+ if rule then
+ return rule:name()
+ end
+ end
+end
+
-- get the config info of the given source file
function target:fileconfig(sourcefile)
@@ -713,13 +759,13 @@ function target:sourcebatches()
local sourcebatches = {}
for _, sourcefile in ipairs(sourcefiles) do
- -- get file config
- local fileconfig = self:fileconfig(sourcefile)
+ -- get file rule
+ local filerule = self:filerule(sourcefile)
-- get source kind
local sourcekind = nil
- if fileconfig and fileconfig.rule then
- sourcekind = "__rule_" .. fileconfig.rule
+ if filerule then
+ sourcekind = "__rule_" .. filerule
end
if not sourcekind then
sourcekind = language.sourcekind_of(sourcefile)
@@ -744,8 +790,8 @@ function target:sourcebatches()
sourcebatch.sourcekind = sourcekind
-- add source rule to this batch
- if fileconfig and fileconfig.rule then
- sourcebatch.rulename = fileconfig.rule
+ if filerule then
+ sourcebatch.rulename = filerule
end
-- add source file to this batch