diff options
| author | ruki <[email protected]> | 2018-04-12 00:49:03 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-04-11 22:54:23 +0800 |
| commit | 67bfa73b2ae4ddfa3f5c001685bdb063a0454ec1 (patch) | |
| tree | e9018f32a1c66073aff796ffc48a010839699929 | |
| parent | 6d36e632a581a487f275cd77cf72c52dd67eedef (diff) | |
add orderules and on_load
| -rw-r--r-- | tests/apis/rules/xmake.lua | 80 | ||||
| -rw-r--r-- | xmake/actions/build/kinds/object.lua | 14 | ||||
| -rw-r--r-- | xmake/actions/require/package.lua | 12 | ||||
| -rw-r--r-- | xmake/core/project/project.lua | 81 | ||||
| -rw-r--r-- | xmake/core/project/rule.lua | 39 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 5 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/project/rule.lua | 2 |
7 files changed, 194 insertions, 39 deletions
diff --git a/tests/apis/rules/xmake.lua b/tests/apis/rules/xmake.lua index cf516686e..cb5179982 100644 --- a/tests/apis/rules/xmake.lua +++ b/tests/apis/rules/xmake.lua @@ -27,6 +27,84 @@ rule("c code") table.insert(target:objectfiles(), objectfile_o) end) +-- define rule: stub3 +rule("stub3") + on_load(function (target) + print("rule(stub3): on_load") + end) + +-- define rule: stub2 +rule("stub2") + on_load(function (target) + print("rule(stub2): on_load") + end) + before_build(function (target) + print("rule(stub2): before_build") + end) + on_build(function (target) + print("rule(stub2): on_build") + end) + after_build(function (target) + print("rule(stub2): after_build") + end) + +-- define rule: stub1 +rule("stub1") + add_deps("stub2") + on_load(function (target) + print("rule(stub1): on_load") + end) + + before_build(function (target) + print("rule(stub1): before_build") + end) + on_build(function (target) + print("rule(stub1): on_build") + end) + after_build(function (target) + print("rule(stub1): after_build") + end) + + before_clean(function (target) + print("rule(stub1): before_build") + end) + on_clean(function (target) + print("rule(stub1): on_build") + end) + after_clean(function (target) + print("rule(stub1): after_build") + end) + + before_install(function (target) + print("rule(stub1): before_install") + end) + on_install(function (target) + print("rule(stub1): on_install") + end) + after_install(function (target) + print("rule(stub1): after_install") + end) + + before_uninstall(function (target) + print("rule(stub1): before_uninstall") + end) + on_uninstall(function (target) + print("rule(stub1): on_uninstall") + end) + after_uninstall(function (target) + print("rule(stub1): after_uninstall") + end) + + before_package(function (target) + print("rule(stub1): before_package") + end) + on_package(function (target) + print("rule(stub1): on_package") + end) + after_package(function (target) + print("rule(stub1): after_package") + end) + -- define target target("test") @@ -34,7 +112,7 @@ target("test") set_kind("binary") -- add rules - add_rules("markdown") + add_rules("markdown", "stub1") -- add files add_files("src/*.c") diff --git a/xmake/actions/build/kinds/object.lua b/xmake/actions/build/kinds/object.lua index 9f8c6ea47..14d30f5bb 100644 --- a/xmake/actions/build/kinds/object.lua +++ b/xmake/actions/build/kinds/object.lua @@ -304,14 +304,14 @@ function _build_files_with_rule(target, buildinfo, sourcebatch, jobs) local ruleinst = project.rule(rulename) or rule.rule(rulename) assert(ruleinst, "unknown rule: %s", rulename) - -- build files? - local build_files = ruleinst:script("build_files") - if build_files then - build_files(target, sourcebatch.sourcefiles) + -- on_build_files? + local on_build_files = ruleinst:script("build_files") + if on_build_files then + on_build_files(target, sourcebatch.sourcefiles) else -- get the build file script - local build_file = ruleinst:script("build_file") - assert(build_file, "rule(%s): on_build_file() script not found!", rulename) + local on_build_file = ruleinst:script("build_file") + assert(on_build_file, "rule(%s): on_build_file() script not found!", rulename) -- run build jobs for each source file local curdir = os.curdir() @@ -334,7 +334,7 @@ function _build_files_with_rule(target, buildinfo, sourcebatch, jobs) end -- do build file - build_file(target, sourcefile) + on_build_file(target, sourcefile) end, #sourcebatch.sourcefiles, jobs) end diff --git a/xmake/actions/require/package.lua b/xmake/actions/require/package.lua index 5446a7e93..e48ce37fc 100644 --- a/xmake/actions/require/package.lua +++ b/xmake/actions/require/package.lua @@ -245,12 +245,20 @@ function _search_package(name) return packages end --- sort package deps +-- sort package deps +-- +-- .e.g +-- +-- a.deps = b +-- b.deps = c +-- +-- orderdeps: c -> b -> a +-- function _sort_packagedeps(package) local orderdeps = {} for _, dep in pairs(package:deps()) do table.join2(orderdeps, _sort_packagedeps(dep)) - table.insert(orderdeps, dep) + table.insert(orderdeps, dep) end return orderdeps end diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 6793f9ab7..5e9fe6895 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -330,17 +330,25 @@ function project.get(name) end end --- load deps for option and target -function project._load_deps(target, targets, deps, orderdeps) +-- load deps for instance: .e.g option, target and rule +-- +-- .e.g +-- +-- a.deps = b +-- b.deps = c +-- +-- orderdeps: c -> b -> a +-- +function project._load_deps(instance, instances, deps, orderdeps) - -- get dep targets - for _, dep in ipairs(table.wrap(target:get("deps"))) do - local deptarget = targets[dep] - if deptarget then - project._load_deps(deptarget, targets, deps, orderdeps) + -- get dep instances + for _, dep in ipairs(table.wrap(instance:get("deps"))) do + local depinst = instances[dep] + if depinst then + project._load_deps(depinst, instances, deps, orderdeps) if not deps[dep] then - deps[dep] = deptarget - table.insert(orderdeps, deptarget) + deps[dep] = depinst + table.insert(orderdeps, depinst) end end end @@ -391,17 +399,37 @@ function project._load_targets() end -- load and attach target deps and rules - for _, target in pairs(targets) do + for _, t 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) + t._DEPS = t._DEPS or {} + t._ORDERDEPS = t._ORDERDEPS or {} + project._load_deps(t, targets, t._DEPS, t._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) + -- + -- .e.g + -- + -- a.deps = b + -- b.deps = c + -- + -- orderules: c -> b -> a + -- + t._RULES = t._RULES or {} + t._ORDERULES = t._ORDERULES or {} + for _, rulename in ipairs(table.wrap(t:get("rules"))) do + local r = project.rule(rulename) or rule.rule(name) + if r then + t._RULES[rulename] = r + for _, deprule in ipairs(r:orderdeps()) do + local name = deprule:name() + if not t._RULES[name] then + t._RULES[name] = deprule + table.insert(t._ORDERULES, deprule) + end + end + table.insert(t._ORDERULES, r) + end end end @@ -410,14 +438,29 @@ function project._load_targets() -- on load for each target local ok = true - for _, target in pairs(targets) do - local on_load = target:script("load") + for _, t in pairs(targets) do + + -- do load for target + local on_load = t:script("load") if on_load then - ok, errors = sandbox.load(on_load, target) + ok, errors = sandbox.load(on_load, t) if not ok then break end end + + -- do load with target rules + if ok then + for _, r in pairs(t:orderules()) do + ok, errors = r:do_load(t) + if not ok then + break + end + end + if not ok then + break + end + end end -- leave toolchains environment diff --git a/xmake/core/project/rule.lua b/xmake/core/project/rule.lua index 83d116cfc..ab9fd09ec 100644 --- a/xmake/core/project/rule.lua +++ b/xmake/core/project/rule.lua @@ -120,6 +120,14 @@ function rule._load(filepath) end -- load deps +-- +-- .e.g +-- +-- a.deps = b +-- b.deps = c +-- +-- orderdeps: c -> b -> a +-- function rule._load_deps(self, rules, deps, orderdeps) -- get dep rules @@ -235,20 +243,33 @@ function rule:orderdeps() return self._ORDERDEPS end --- build source files -function rule:build_files(target, sourcefiles) +-- do load +function rule:do_load(target) + + -- on_load? + local on_load = self:script("load") + if on_load then + return sandbox.load(on_load, target) + end + + -- ok + return true +end + +-- do build source files +function rule:do_build_files(target, sourcefiles) - -- build files? - local build_files = self:script("build_files") - if build_files then - return sandbox.load(build_files, target, sourcefiles) + -- on_build_files? + local on_build_files = self:script("build_files") + if on_build_files then + return sandbox.load(on_build_files, target, sourcefiles) else - local build_file = self:script("build_file") - if not build_file then + local on_build_file = self:script("build_file") + if not on_build_file then return false, string.format("rule(%s): on_build_file() script not found!", self:name()) end for _, sourcefile in ipairs(table.wrap(sourcefiles)) do - local ok, errors = sandbox.load(build_file, target, sourcefile) + local ok, errors = sandbox.load(on_build_file, target, sourcefile) if not ok then return false, errors end diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 0a0731ae9..4ecd7ef24 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -281,6 +281,11 @@ function target:rules() return self._RULES end +-- get target order rules +function target:orderules() + return self._ORDERULES +end + -- get target rule from the given source extension function target:rule(extension) diff --git a/xmake/core/sandbox/modules/import/core/project/rule.lua b/xmake/core/sandbox/modules/import/core/project/rule.lua index 78dbb1908..6cb5c72a2 100644 --- a/xmake/core/sandbox/modules/import/core/project/rule.lua +++ b/xmake/core/sandbox/modules/import/core/project/rule.lua @@ -52,7 +52,7 @@ function sandbox_core_project_rule.build_files(rulename, target, sourcefiles) end -- do build - local ok, errors = rule:build_files(target, sourcefiles) + local ok, errors = rule:do_build_files(target, sourcefiles) if not ok then raise(errors) end |
