Skip to content

Commit

Permalink
Resolve the rule properties for gmake (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
TurkeyMan committed Nov 1, 2017
1 parent 66362c0 commit 861003a
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 51 deletions.
47 changes: 22 additions & 25 deletions modules/gmake2/gmake2_cpp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand All @@ -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)
Expand Down Expand Up @@ -298,7 +319,6 @@
cpp.linkCmd,
cpp.bindirs,
cpp.exepaths,
cpp.ruleProperties,
gmake2.settings,
gmake2.preBuildCmds,
gmake2.preLinkCmds,
Expand Down Expand Up @@ -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.
--
Expand Down
48 changes: 22 additions & 26 deletions modules/gmake2/gmake2_utility.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand All @@ -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)
Expand Down Expand Up @@ -209,7 +230,6 @@

utility.elements.configuration = function(cfg)
return {
utility.ruleProperties,
gmake2.settings,
gmake2.preBuildCmds,
gmake2.preLinkCmds,
Expand All @@ -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.
Expand Down
52 changes: 52 additions & 0 deletions modules/gmake2/tests/test_gmake2_file_rules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions src/base/rule.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 861003a

Please sign in to comment.