summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-11-11 20:43:53 +0800
committerruki <[email protected]>2017-11-11 20:43:53 +0800
commitbc63208869cc865be6ce6ec60d22c9d128d9d65e (patch)
tree85b45c6533b2789bb42eeeaca2f5b38ca1b745f7
parentfc0ac813b07b28abdbd2b40c5ba78ee76dcc09d7 (diff)
impl build rule
-rw-r--r--tests/apis/rules/xmake.lua42
-rw-r--r--xmake/actions/build/kinds/object.lua55
-rw-r--r--xmake/core/project/rule.lua46
-rw-r--r--xmake/core/project/target.lua14
-rw-r--r--xmake/core/sandbox/modules/import/core/project/rule.lua14
5 files changed, 103 insertions, 68 deletions
diff --git a/tests/apis/rules/xmake.lua b/tests/apis/rules/xmake.lua
index fd25ba94f..d97890552 100644
--- a/tests/apis/rules/xmake.lua
+++ b/tests/apis/rules/xmake.lua
@@ -1,28 +1,21 @@
-- define rule: markdown
rule("markdown")
- on_build(function (target, sourcefiles)
- for _, sourcefile in ipairs(sourcefiles) do
- print("compiling %s", sourcefile)
- os.cp(sourcefile, path.join(target:objectdir(), path.basename(sourcefile) .. ".html"))
- end
+ on_build(function (target, sourcefile)
+ os.cp(sourcefile, path.join(target:targetdir(), path.basename(sourcefile) .. ".html"))
end)
-- define rule: man
rule("man")
add_imports("core.project.rule")
- on_build(function (target, sourcefiles)
- for _, sourcefile in ipairs(sourcefiles) do
- print("generating %s", sourcefile)
- end
- rule.build("markdown", target, sourcefiles)
+ on_build(function (target, sourcefile)
+ rule.build("markdown", target, sourcefile)
end)
-- define rule: c code
rule("c code")
add_imports("core.tool.compiler")
on_build(function (target, sourcefile)
- print("compiling %s", sourcefile)
local objectfile_o = os.tmpfile() .. ".o"
local sourcefile_c = os.tmpfile() .. ".c"
os.cp(sourcefile, sourcefile_c)
@@ -32,45 +25,18 @@ rule("c code")
-- define rule: stub
rule("stub")
- before_build(function (target, sourcefile)
- print("before_build: %s", sourcefile)
- end)
on_build(function (target, sourcefile)
print("on_build: %s", sourcefile)
end)
- after_build(function (target, sourcefile)
- print("after_build: %s", sourcefile)
- end)
-
- before_clean(function (target, sourcefile)
- print("before_clean: %s", sourcefile)
- end)
on_clean(function (target, sourcefile)
print("on_clean: %s", sourcefile)
end)
- after_clean(function (target, sourcefile)
- print("after_clean: %s", sourcefile)
- end)
-
- before_install(function (target, sourcefile)
- print("before_install: %s", sourcefile)
- end)
on_install(function (target, sourcefile)
print("on_install: %s", sourcefile)
end)
- after_install(function (target, sourcefile)
- print("after_install: %s", sourcefile)
- end)
-
- before_package(function (target, sourcefile)
- print("before_package: %s", sourcefile)
- end)
on_package(function (target, sourcefile)
print("on_package: %s", sourcefile)
end)
- after_package(function (target, sourcefile)
- print("after_package: %s", sourcefile)
- end)
-- define target
target("test")
diff --git a/xmake/actions/build/kinds/object.lua b/xmake/actions/build/kinds/object.lua
index a6e8a9050..0510d1d23 100644
--- a/xmake/actions/build/kinds/object.lua
+++ b/xmake/actions/build/kinds/object.lua
@@ -26,6 +26,7 @@
import("core.base.option")
import("core.tool.compiler")
import("core.tool.extractor")
+import("core.project.rule")
import("core.project.config")
import("core.project.project")
import("core.language.language")
@@ -296,24 +297,46 @@ end
-- build source files with the custom rule
function _build_rule(target, buildinfo, sourcebatch, jobs)
+ -- the rule name
+ local rulename = sourcebatch.rulename
+
-- get rule
- local rule = project.rule(sourcebatch.rulename)
- assert(rule, "unknown rule: %s", sourcebatch.rule)
-
- -- the rule scripts
- local scripts =
- {
- rule:script("build_before")
- , rule:script("build")
- , rule:script("build_after")
- }
+ local rule = project.rule(rulename)
+ assert(rule, "unknown rule: %s", rulename)
- -- run the rule scripts
- for i = 1, 3 do
- local script = scripts[i]
- if script ~= nil then
- script(target, table.unwrap(sourcebatch.sourcefiles))
- end
+ -- build all?
+ local build_all = rule:script("build_all")
+ if build_all then
+ build_all(target, sourcebatch.sourcefiles)
+ else
+ -- get the build script
+ local build = rule:script("build")
+ assert(build, "rule(%s): build script not found!", rulename)
+
+ -- run build jobs for each source file
+ local curdir = os.curdir()
+ process.runjobs(function (index)
+
+ -- force to set the current directory first because the other jobs maybe changed it
+ os.cd(curdir)
+
+ -- calculate percent
+ local percent = ((buildinfo.targetindex + (_g.sourceindex + index - 1) / _g.sourcecount) * 100 / buildinfo.targetcount)
+
+ -- the source file
+ local sourcefile = sourcebatch.sourcefiles[index]
+
+ -- trace percent info
+ if option.get("verbose") then
+ cprint("${green}[%02d%%]:${dim} compiling.%s %s", percent, rulename, sourcefile)
+ else
+ cprint("${green}[%02d%%]:${clear} compiling.%s %s", percent, rulename, sourcefile)
+ end
+
+ -- do build
+ build(target, sourcefile)
+
+ end, #sourcebatch.sourcefiles, jobs)
end
-- update object index
diff --git a/xmake/core/project/rule.lua b/xmake/core/project/rule.lua
index 51df6a6c2..4d9a9df7e 100644
--- a/xmake/core/project/rule.lua
+++ b/xmake/core/project/rule.lua
@@ -51,18 +51,11 @@ function rule.apis()
, "rule.on_package"
, "rule.on_install"
, "rule.on_uninstall"
- -- rule.before_xxx
- , "rule.before_build"
- , "rule.before_clean"
- , "rule.before_package"
- , "rule.before_install"
- , "rule.before_uninstall"
- -- rule.after_xxx
- , "rule.after_build"
- , "rule.after_clean"
- , "rule.after_package"
- , "rule.after_install"
- , "rule.after_uninstall"
+ , "rule.on_build_all"
+ , "rule.on_clean_all"
+ , "rule.on_package_all"
+ , "rule.on_install_all"
+ , "rule.on_uninstall_all"
}
}
end
@@ -87,6 +80,35 @@ function rule:get(name)
return self._INFO[name]
end
+-- get the rule name
+function rule:name()
+ return self._NAME
+end
+
+-- build source files
+function rule:build(target, sourcefiles)
+
+ -- build all?
+ local build_all = self:script("build_all")
+ if build_all then
+ return sandbox.load(build_all, target, sourcefiles)
+ else
+ local build = self:script("build")
+ if not build then
+ return false, string.format("rule(%s): build script not found!", self:name())
+ end
+ for _, sourcefile in ipairs(table.wrap(sourcefiles)) do
+ local ok, errors = sandbox.load(build, target, sourcefile)
+ if not ok then
+ return false, errors
+ end
+ end
+ end
+
+ -- ok
+ return true
+end
+
-- get xxx_script
function rule:script(name, generic)
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index 3aaa71f24..49cbcdeae 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -323,13 +323,23 @@ function target:targetkind()
return self:get("kind")
end
--- get the target file
-function target:targetfile()
+-- get the target directory
+function target:targetdir()
-- the target directory
local targetdir = self:get("targetdir") or config.get("buildir")
assert(targetdir and type(targetdir) == "string")
+ -- ok?
+ return targetdir
+end
+
+-- get the target file
+function target:targetfile()
+
+ -- the target directory
+ local targetdir = self:targetdir()
+
-- get target kind
local targetkind = self:targetkind()
diff --git a/xmake/core/sandbox/modules/import/core/project/rule.lua b/xmake/core/sandbox/modules/import/core/project/rule.lua
index 32d5cac03..75d88a26a 100644
--- a/xmake/core/sandbox/modules/import/core/project/rule.lua
+++ b/xmake/core/sandbox/modules/import/core/project/rule.lua
@@ -26,12 +26,26 @@
local sandbox_core_project_rule = sandbox_core_project_rule or {}
-- load modules
+local table = require("base/table")
local rule = require("project/rule")
+local project = require("project/project")
+local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
-- build source files
function sandbox_core_project_rule.build(rulename, target, sourcefiles)
+ -- get rule
+ local rule = project.rule(rulename)
+ if not rule then
+ raise("unknown rule: %s", rulename)
+ end
+
+ -- do build
+ local ok, errors = rule:build(target, sourcefiles)
+ if not ok then
+ raise(errors)
+ end
end
-- return module