Skip to content

Commit 649420e

Browse files
committed
Prebuild, prelink, postbuild tasks
1 parent bdbdeff commit 649420e

File tree

3 files changed

+270
-106
lines changed

3 files changed

+270
-106
lines changed

modules/ninja/ninja_cpp.lua

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,26 @@ function m.linkTarget(cfg)
12041204
end
12051205
end
12061206

1207+
local function buildcommandstring(cmds, message)
1208+
local shell = os.shell()
1209+
1210+
local allcmds = table.deepcopy(cmds)
1211+
if message then
1212+
table.insert(allcmds, 1, 'echo \"' .. message .. '\"')
1213+
end
1214+
1215+
if shell == "posix" then
1216+
return "sh -c '" .. table.concat(allcmds, " && ") .. "'"
1217+
elseif shell == "cmd" then
1218+
local joined = table.concat(allcmds, " && ")
1219+
-- Escape double quotes
1220+
joined = joined:gsub('"', '\\"')
1221+
return "cmd /C \"" .. joined .. "\""
1222+
else
1223+
return table.concat(allcmds, " && ")
1224+
end
1225+
end
1226+
12071227
function m.buildPreBuildEvents(cfg)
12081228
local hasMessage = cfg.prebuildmessage ~= nil
12091229
local hasCommands = #cfg.prebuildcommands > 0
@@ -1225,14 +1245,15 @@ function m.buildPreBuildEvents(cfg)
12251245
_p(" prebuildmessage = \"%s\"", cfg.prebuildmessage)
12261246
elseif hasCommands and not hasMessage then
12271247
local commands = os.translateCommandsAndPaths(cfg.prebuildcommands, cfg.project.basedir, cfg.project.location)
1228-
local cmdStr = table.concat(commands, " && ")
1248+
local cmdstr = buildcommandstring(commands)
1249+
12291250
_p("build %s: prebuild%s", prebuildTarget, implicitDeps)
1230-
_p(" prebuildcommands = %s", cmdStr)
1251+
_p(" prebuildcommands = %s", cmdstr)
12311252
else
12321253
local commands = os.translateCommandsAndPaths(cfg.prebuildcommands, cfg.project.basedir, cfg.project.location)
1233-
local cmdStr = "echo \"" .. cfg.prebuildmessage .. "\" && " .. table.concat(commands, " && ")
1254+
local cmdstr = buildcommandstring(commands, cfg.prebuildmessage)
12341255
_p("build %s: prebuild%s", prebuildTarget, implicitDeps)
1235-
_p(" prebuildcommands = %s", cmdStr)
1256+
_p(" prebuildcommands = %s", cmdstr)
12361257
end
12371258

12381259
return prebuildTarget
@@ -1259,14 +1280,14 @@ function m.buildPreLinkEvents(cfg, objectFiles)
12591280
_p(" prelinkmessage = \"%s\"", cfg.prelinkmessage)
12601281
elseif hasCommands and not hasMessage then
12611282
local commands = os.translateCommandsAndPaths(cfg.prelinkcommands, cfg.project.basedir, cfg.project.location)
1262-
local cmdStr = table.concat(commands, " && ")
1283+
local cmdstr = buildcommandstring(commands)
12631284
_p("build %s: prelink%s", prelinkTarget, objDeps)
1264-
_p(" prelinkcommands = %s", cmdStr)
1285+
_p(" prelinkcommands = %s", cmdstr)
12651286
else
12661287
local commands = os.translateCommandsAndPaths(cfg.prelinkcommands, cfg.project.basedir, cfg.project.location)
1267-
local cmdStr = "echo \"" .. cfg.prelinkmessage .. "\" && " .. table.concat(commands, " && ")
1288+
local cmdstr = buildcommandstring(commands, cfg.prelinkmessage)
12681289
_p("build %s: prelink%s", prelinkTarget, objDeps)
1269-
_p(" prelinkcommands = %s", cmdStr)
1290+
_p(" prelinkcommands = %s", cmdstr)
12701291
end
12711292

12721293
return prelinkTarget
@@ -1287,14 +1308,14 @@ function m.buildPostBuildEvents(cfg, targetPath)
12871308
_p(" postbuildmessage = \"%s\"", cfg.postbuildmessage)
12881309
elseif hasCommands and not hasMessage then
12891310
local commands = os.translateCommandsAndPaths(cfg.postbuildcommands, cfg.project.basedir, cfg.project.location)
1290-
local cmdStr = table.concat(commands, " && ")
1311+
local cmdstr = buildcommandstring(commands)
12911312
_p("build %s: postbuild | %s", postbuildPhony, targetPath)
1292-
_p(" postbuildcommands = %s", cmdStr)
1313+
_p(" postbuildcommands = %s", cmdstr)
12931314
else
12941315
local commands = os.translateCommandsAndPaths(cfg.postbuildcommands, cfg.project.basedir, cfg.project.location)
1295-
local cmdStr = "echo \"" .. cfg.postbuildmessage .. "\" && " .. table.concat(commands, " && ")
1316+
local cmdstr = buildcommandstring(commands, cfg.postbuildmessage)
12961317
_p("build %s: postbuild | %s", postbuildPhony, targetPath)
1297-
_p(" postbuildcommands = %s", cmdStr)
1318+
_p(" postbuildcommands = %s", cmdstr)
12981319
end
12991320
end
13001321

0 commit comments

Comments
 (0)