From ec8068f4d052e4c4536fd485f9c9eb815e451397 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 14 Jun 2018 22:38:41 +0800 Subject: add before_load and after_load for rule --- scripts/installer.nsi | 3 + tests/projects/wdk/wdm/msdsm/xmake.lua | 7 ++ xmake/core/project/rule.lua | 2 + xmake/core/project/target.lua | 67 +++++++++++++------ xmake/modules/os/winver.lua | 116 +++++++++++++++++++++++++++++++++ xmake/modules/utils/os/winver.lua | 116 --------------------------------- xmake/rules/wdk/env/load.lua | 6 +- xmake/rules/wdk/load.lua | 1 - xmake/rules/wdk/xmake.lua | 8 +-- 9 files changed, 183 insertions(+), 143 deletions(-) create mode 100644 xmake/modules/os/winver.lua delete mode 100644 xmake/modules/utils/os/winver.lua diff --git a/scripts/installer.nsi b/scripts/installer.nsi index e82a93d9d..7a672e55e 100644 --- a/scripts/installer.nsi +++ b/scripts/installer.nsi @@ -77,6 +77,9 @@ Section "xmake (required)" Installer ; Set output path to the installation directory. SetOutPath $INSTDIR + + ; Remove previous directories used + RMDir /r "$INSTDIR" ; Put file there File /r /x ".DS_Store" /x "*.swp" "..\xmake\*.*" diff --git a/tests/projects/wdk/wdm/msdsm/xmake.lua b/tests/projects/wdk/wdm/msdsm/xmake.lua index 069f69767..d7a8804c6 100644 --- a/tests/projects/wdk/wdm/msdsm/xmake.lua +++ b/tests/projects/wdk/wdm/msdsm/xmake.lua @@ -25,3 +25,10 @@ target("sampledsm") -- add links add_links("mpio") + on_load(function (target) + + import("core.project.config") + print(target:values("wdk.env.winver") or config.get("wdk_winver")) + local ntddi_version = import("os.winver").ntddi_version(target:values("wdk.env.winver") or config.get("wdk_winver")) + print(ntddi_version) + end) diff --git a/xmake/core/project/rule.lua b/xmake/core/project/rule.lua index 52530ac5c..5945d91ca 100644 --- a/xmake/core/project/rule.lua +++ b/xmake/core/project/rule.lua @@ -170,6 +170,7 @@ function rule.apis() , "rule.on_uninstall" -- rule.before_xxx , "rule.before_run" + , "rule.before_load" , "rule.before_build" , "rule.before_build_file" , "rule.before_build_files" @@ -179,6 +180,7 @@ function rule.apis() , "rule.before_uninstall" -- rule.after_xxx , "rule.after_run" + , "rule.after_load" , "rule.after_build" , "rule.after_build_file" , "rule.after_build_files" diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index cbe226507..98623895b 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -150,15 +150,15 @@ function target.new(name, info, project) end -- load rule, move cache to target -function target:_load_rule(ruleinst) +function target:_load_rule(ruleinst, suffix) -- init cache - local key = ruleinst:name() + local key = ruleinst:name() .. (suffix and ("_" .. suffix) or "") local cache = self._RULES_LOADED or {} -- do load if cache[key] == nil then - local on_load = ruleinst:script("load") + local on_load = ruleinst:script("load" .. (suffix and ("_" .. suffix) or "")) if on_load then local ok, errors = sandbox.load(on_load, self) cache[key] = {ok, errors} @@ -177,6 +177,51 @@ function target:_load_rule(ruleinst) end end +-- load rules +function target:_load_rules(suffix) + for _, r in pairs(self:orderules()) do + local ok, errors = self:_load_rule(r, suffix) + if not ok then + return false, errors + end + end + return true +end + +-- do load target and rules +function target:_load() + + -- do before_load with target rules + local ok, errors = self:_load_rules("before") + if not ok then + return false, errors + end + + -- do load for target + local on_load = self:script("load") + if on_load then + local ok, errors = sandbox.load(on_load, self) + if not ok then + return false, errors + end + end + + -- do load with target rules + local ok, errors = self:_load_rules() + if not ok then + return false, errors + end + + -- do after_load with target rules + local ok, errors = self:_load_rules("after") + if not ok then + return false, errors + end + + -- ok + return true +end + -- get the target info function target:get(name) return self._INFO[name] @@ -593,22 +638,6 @@ function target:filerules(sourcefile) for _, rulename in ipairs(table.wrap(filerules)) do local r = self._PROJECT.rule(rulename) or rule.rule(rulename) if r then - - -- load dependent rules if these rules have been not loaded - for _, deprule in pairs(r:orderdeps()) do - local ok, errors = self:_load_rule(deprule) - if not ok then - os.raise(errors) - end - end - - -- load file rule if this rule have been not loaded - local ok, errors = self:_load_rule(r) - if not ok then - os.raise(errors) - end - - -- add this rule table.insert(rules, r) end end diff --git a/xmake/modules/os/winver.lua b/xmake/modules/os/winver.lua new file mode 100644 index 000000000..79587c6ed --- /dev/null +++ b/xmake/modules/os/winver.lua @@ -0,0 +1,116 @@ +--!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 winver.lua +-- + +-- get WINVER from name +-- +-- reference: https://msdn.microsoft.com/en-us/library/windows/desktop/aa383745(v=vs.85).aspx +-- +function version(name) + + -- init values + _g.values = _g.values or + { + nt4 = "0x0400" + , win2k = "0x0500" + , winxp = "0x0501" + , ws03 = "0x0502" + , win6 = "0x0600" + , vista = "0x0600" + , ws08 = "0x0600" + , longhorn = "0x0600" + , win7 = "0x0601" + , win8 = "0x0602" + , winblue = "0x0603" + , win81 = "0x0603" + , win10 = "0x0A00" + } + + -- ignore the subname with '_xxx' + name = name:split('_')[1] + + -- get value + return _g.values[name] +end + +-- get NTDDI_VERSION from name +function ntddi_version(name) + + -- init subvalues + _g.subvalues = _g.subvalues or + { + sp1 = "0100" + , sp2 = "0200" + , sp3 = "0300" + , sp4 = "0400" + , th2 = "0001" + , rs1 = "0002" + , rs2 = "0003" + , rs3 = "0004" + } + + -- get subvalue + local subvalue = nil + local subname = name:split('_')[2] + if subname then + subvalue = _g.subvalues[subname] + end + + -- get value + local val = version(name) + if val then + val = val .. (subvalue or "0000") + end + return val +end + +-- get windows system version +-- +-- $ xmake l os.winver +-- +-- - win10: 10.0.14393 +-- - xp: 5.1.2600 +-- +function main() + + -- get it from cache first + if _g.winver ~= nil then + return _g.winver + end + + -- get winver + local winver = nil + local verstr = try { function () return os.iorun("cmd /c ver") end } + if verstr then + winver = verstr:match("%[.-(%d+%.%d+%.%d+)]") + if winver then + winver = winver:trim() + end + end + + -- save to cache + _g.winver = winver or false + + -- done + return winver +end diff --git a/xmake/modules/utils/os/winver.lua b/xmake/modules/utils/os/winver.lua deleted file mode 100644 index 1579b4563..000000000 --- a/xmake/modules/utils/os/winver.lua +++ /dev/null @@ -1,116 +0,0 @@ ---!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 winver.lua --- - --- get windows version value from name --- --- reference: https://msdn.microsoft.com/en-us/library/windows/desktop/aa383745(v=vs.85).aspx --- -function value(name) - - -- init values - _g.values = _g.values or - { - nt4 = "0x0400" - , win2k = "0x0500" - , winxp = "0x0501" - , ws03 = "0x0502" - , win6 = "0x0600" - , vista = "0x0600" - , ws08 = "0x0600" - , longhorn = "0x0600" - , win7 = "0x0601" - , win8 = "0x0602" - , winblue = "0x0603" - , win81 = "0x0603" - , win10 = "0x0A00" - } - - -- ignore the subname with '_xxx' - name = name:split('_')[1] - - -- get value - return _g.values[name] -end - --- get ntddi value from name -function value_ntddi(name) - - -- init subvalues - _g.subvalues = _g.subvalues or - { - sp1 = "0100" - , sp2 = "0200" - , sp3 = "0300" - , sp4 = "0400" - , th2 = "0001" - , rs1 = "0002" - , rs2 = "0003" - , rs3 = "0004" - } - - -- get subvalue - local subvalue = nil - local subname = name:split('_')[2] - if subname then - subvalue = _g.subvalues[subname] - end - - -- get value - local val = value(name) - if val then - val = val .. (subvalue or "0000") - end - return val -end - --- get windows system version --- --- $ xmake l utils.os.winver --- --- - win10: 10.0.14393 --- - xp: 5.1.2600 --- -function main() - - -- get it from cache first - if _g.winver ~= nil then - return _g.winver - end - - -- get winver - local winver = nil - local verstr = try { function () return os.iorun("cmd /c ver") end } - if verstr then - winver = verstr:match("%[.-(%d+%.%d+%.%d+)]") - if winver then - winver = winver:trim() - end - end - - -- save to cache - _g.winver = winver or false - - -- done - return winver -end diff --git a/xmake/rules/wdk/env/load.lua b/xmake/rules/wdk/env/load.lua index d36d5aa49..2f36745f3 100644 --- a/xmake/rules/wdk/env/load.lua +++ b/xmake/rules/wdk/env/load.lua @@ -24,16 +24,16 @@ -- imports import("core.project.config") -import("utils.os.winver", {alias = "os_winver"}) +import("os.winver", {alias = "os_winver"}) -- get windows version value function _winver(winver) - return os_winver.value(winver or "") or "0x0A00" + return os_winver.version(winver or "") or "0x0A00" end -- get windows ntddi version value function _winver_ntddi(winver) - return os_winver.value_ntddi(winver or "") or "0x0A000000" + return os_winver.ntddi_version(winver or "") or "0x0A000000" end -- get version of the library sub-directory diff --git a/xmake/rules/wdk/load.lua b/xmake/rules/wdk/load.lua index cd71a15f7..312fa3454 100644 --- a/xmake/rules/wdk/load.lua +++ b/xmake/rules/wdk/load.lua @@ -24,7 +24,6 @@ -- imports import("core.project.config") -import("utils.os.winver", {alias = "os_winver"}) -- get subsystem version defined value function _winver_subsystem(winver) diff --git a/xmake/rules/wdk/xmake.lua b/xmake/rules/wdk/xmake.lua index 4a9adbc0d..cc6bab715 100644 --- a/xmake/rules/wdk/xmake.lua +++ b/xmake/rules/wdk/xmake.lua @@ -26,7 +26,7 @@ rule("wdk.driver") -- add rules - add_deps("wdk.inf", "wdk.man", "wdk.mc", "wdk.mof", "wdk.sign", "wdk.package.cab") + add_deps("wdk.inf", "wdk.man", "wdk.mc", "wdk.mof", "wdk.tracewpp", "wdk.sign", "wdk.package.cab") -- on load on_load(function (target) @@ -67,7 +67,7 @@ rule("wdk.driver") rule("wdk.binary") -- add rules - add_deps("wdk.inf", "wdk.man", "wdk.mc", "wdk.mof") + add_deps("wdk.inf", "wdk.man", "wdk.mc", "wdk.mof", "wdk.tracewpp") -- on load on_load(function (target) @@ -84,7 +84,7 @@ rule("wdk.binary") rule("wdk.static") -- add rules - add_deps("wdk.inf", "wdk.man", "wdk.mc", "wdk.mof") + add_deps("wdk.inf", "wdk.man", "wdk.mc", "wdk.mof", "wdk.tracewpp") -- on load on_load(function (target) @@ -103,7 +103,7 @@ rule("wdk.static") rule("wdk.shared") -- add rules - add_deps("wdk.inf", "wdk.man", "wdk.mc", "wdk.mof") + add_deps("wdk.inf", "wdk.man", "wdk.mc", "wdk.mof", "wdk.tracewpp") -- on load on_load(function (target) -- cgit v1.3.1