From 861003a8cacd0663f56f8bdbca431c9aa27d3d40 Mon Sep 17 00:00:00 2001 From: Manu Evans Date: Wed, 1 Nov 2017 11:16:15 -0700 Subject: [PATCH] Resolve the rule properties for gmake (#162) --- modules/gmake2/gmake2_cpp.lua | 47 ++++++++--------- modules/gmake2/gmake2_utility.lua | 48 ++++++++--------- .../gmake2/tests/test_gmake2_file_rules.lua | 52 +++++++++++++++++++ src/base/rule.lua | 9 ++++ 4 files changed, 105 insertions(+), 51 deletions(-) diff --git a/modules/gmake2/gmake2_cpp.lua b/modules/gmake2/gmake2_cpp.lua index 76599ce9f6..21653c376c 100644 --- a/modules/gmake2/gmake2_cpp.lua +++ b/modules/gmake2/gmake2_cpp.lua @@ -237,6 +237,26 @@ cpp.addRuleFile(cfg, node) end + function cpp.prepareEnvironment(rule, environ, cfg) + for _, prop in ipairs(rule.propertydefinition) do + local fld = p.rule.getPropertyField(rule, prop) + local value = cfg[fld.name] + if value ~= nil then + + if fld.kind == "path" then + value = gmake2.path(cfg, value) + elseif fld.kind == "list:path" then + value = gmake2.path(cfg, value) + end + + value = p.rule.expandString(rule, prop, value) + if value ~= nil and #value > 0 then + environ[prop.name] = p.esc(value) + end + end + end + end + function cpp.addRuleFile(cfg, node) local rules = cfg.project._gmake.rules local rule = rules[path.getextension(node.abspath):lower()] @@ -246,7 +266,8 @@ local environ = table.shallowcopy(filecfg.environ) if rule.propertydefinition then - p.rule.prepareEnvironment(rule, environ, "$(%s)") + cpp.prepareEnvironment(rule, environ, cfg) + cpp.prepareEnvironment(rule, environ, filecfg) end local shadowContext = p.context.extent(rule, environ) @@ -298,7 +319,6 @@ cpp.linkCmd, cpp.bindirs, cpp.exepaths, - cpp.ruleProperties, gmake2.settings, gmake2.preBuildCmds, gmake2.preLinkCmds, @@ -491,29 +511,6 @@ end - function cpp.ruleProperties(cfg, toolset) - for i = 1, #cfg.rules do - local rule = p.global.getRule(cfg.rules[i]) - - for prop in p.rule.eachProperty(rule) do - local fld = p.rule.getPropertyField(rule, prop) - local value = cfg[fld.name] - if value ~= nil then - if fld.kind == "path" then - value = gmake2.path(cfg, value) - elseif fld.kind == "list:path" then - value = gmake2.path(cfg, value) - end - - value = p.rule.expandString(rule, prop, value) - if value ~= nil and #value > 0 then - p.outln(prop.name .. ' = ' .. p.esc(value)) - end - end - end - end - end - -- -- Write out the per file configurations. -- diff --git a/modules/gmake2/gmake2_utility.lua b/modules/gmake2/gmake2_utility.lua index 8cf430cb64..1ef65fcfc4 100644 --- a/modules/gmake2/gmake2_utility.lua +++ b/modules/gmake2/gmake2_utility.lua @@ -165,6 +165,26 @@ end + function utility.prepareEnvironment(rule, environ, cfg) + for _, prop in ipairs(rule.propertydefinition) do + local fld = p.rule.getPropertyField(rule, prop) + local value = cfg[fld.name] + if value ~= nil then + + if fld.kind == "path" then + value = gmake2.path(cfg, value) + elseif fld.kind == "list:path" then + value = gmake2.path(cfg, value) + end + + value = p.rule.expandString(rule, prop, value) + if value ~= nil and #value > 0 then + environ[prop.name] = p.esc(value) + end + end + end + end + function utility.addRuleFile(cfg, node) local rules = cfg.project._gmake.rules local rule = rules[path.getextension(node.abspath):lower()] @@ -174,7 +194,8 @@ local environ = table.shallowcopy(filecfg.environ) if rule.propertydefinition then - p.rule.prepareEnvironment(rule, environ, "$(%s)") + utility.prepareEnvironment(rule, environ, cfg) + utility.prepareEnvironment(rule, environ, filecfg) end local shadowContext = p.context.extent(rule, environ) @@ -209,7 +230,6 @@ utility.elements.configuration = function(cfg) return { - utility.ruleProperties, gmake2.settings, gmake2.preBuildCmds, gmake2.preLinkCmds, @@ -218,30 +238,6 @@ end - function utility.ruleProperties(cfg, toolset) - for i = 1, #cfg.rules do - local rule = p.global.getRule(cfg.rules[i]) - - for prop in p.rule.eachProperty(rule) do - local fld = p.rule.getPropertyField(rule, prop) - local value = cfg[fld.name] - if value ~= nil then - if fld.kind == "path" then - value = gmake2.path(cfg, value) - elseif fld.kind == "list:path" then - value = gmake2.path(cfg, value) - end - - value = p.rule.expandString(rule, prop, value) - if value ~= nil and #value > 0 then - p.outln(prop.name .. ' = ' .. p.esc(value)) - end - end - end - end - end - - -- -- Write out the file sets. diff --git a/modules/gmake2/tests/test_gmake2_file_rules.lua b/modules/gmake2/tests/test_gmake2_file_rules.lua index 947cd859ca..fb23af32e2 100644 --- a/modules/gmake2/tests/test_gmake2_file_rules.lua +++ b/modules/gmake2/tests/test_gmake2_file_rules.lua @@ -18,6 +18,29 @@ function suite.setup() p.escaper(gmake2.esc) gmake2.cpp.initialize() + + rule "TestRule" + display "Test Rule" + fileextension ".rule" + + propertydefinition { + name = "TestProperty", + kind = "boolean", + value = false, + switch = "-p" + } + + propertydefinition { + name = "TestProperty2", + kind = "boolean", + value = false, + switch = "-p2" + } + + buildmessage 'Rule-ing %{file.name}' + buildcommands 'dorule %{TestProperty} %{TestProperty2} "%{file.path}"' + buildoutputs { "%{file.basename}.obj" } + wks = test.createWorkspace() end @@ -139,3 +162,32 @@ obj/Release/hello.obj: hello.x hello.x.inc hello.x.inc2 endif ]] end + + function suite.customRuleWithProps() + + rules { "TestRule" } + + files { "test.rule", "test2.rule" } + + testRuleVars { + TestProperty = true + } + + filter "files:test2.rule" + testRuleVars { + TestProperty2 = true + } + + prepare() + test.capture [[ +# File Rules +# ############################################# + +test.obj: test.rule + @echo Rule-ing test.rule + $(SILENT) dorule -p "test.rule" +test2.obj: test2.rule + @echo Rule-ing test2.rule + $(SILENT) dorule -p -p2 "test2.rule" + ]] + end diff --git a/src/base/rule.lua b/src/base/rule.lua index 345b1d3010..8e3b178a93 100644 --- a/src/base/rule.lua +++ b/src/base/rule.lua @@ -174,6 +174,15 @@ end end + -- bool just emits the switch + if type(value) == "boolean" then + if value then + return prop.switch + else + return nil + end + end + -- enum? if prop.values then local i = table.indexof(prop.values, value)