Skip to content

Commit dc3fa19

Browse files
committed
Fix prepost build step on Windows
1 parent 56552aa commit dc3fa19

File tree

6 files changed

+253
-60
lines changed

6 files changed

+253
-60
lines changed

modules/ninja/ninja_cpp.lua

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,9 +1165,12 @@ function m.linkTarget(cfg)
11651165
end
11661166
end
11671167

1168+
local postbuildStamp = nil
11681169
if hasPostBuild then
1169-
m.buildPostBuildEvents(cfg, targetPath)
1170+
postbuildStamp = m.buildPostBuildEvents(cfg, targetPath)
11701171
end
1172+
1173+
cfg._postbuildStamp = postbuildStamp
11711174
end
11721175

11731176
function m.buildPreBuildEvents(cfg)
@@ -1178,30 +1181,33 @@ function m.buildPreBuildEvents(cfg)
11781181
return nil
11791182
end
11801183

1181-
local targetPath = path.getrelative(cfg.workspace.location, cfg.buildtarget.directory) .. "/" .. cfg.buildtarget.name
1182-
local prebuildTarget = path.getrelative(cfg.workspace.location, cfg.buildtarget.directory) .. "/" .. cfg.project.name .. ".prebuild"
1184+
local stampFile = path.getrelative(cfg.workspace.location, cfg.buildtarget.directory) .. "/" .. cfg.project.name .. ".prebuild.stamp"
1185+
1186+
-- Determine touch command based on target system, not host system
1187+
local shell = _OPTIONS.shell or iif(cfg.system == p.WINDOWS, "cmd", "posix")
1188+
local touchCmd = iif(shell == "cmd", "type nul > \"" .. stampFile .. "\"", "touch \"" .. stampFile .. "\"")
11831189

11841190
local implicitDeps = ""
11851191
if cfg._dependsOnTarget then
11861192
implicitDeps = " | " .. cfg._dependsOnTarget
11871193
end
11881194

11891195
if hasMessage and not hasCommands then
1190-
_p("build %s: prebuildmessage%s", prebuildTarget, implicitDeps)
1191-
_p(" prebuildmessage = \"%s\"", cfg.prebuildmessage)
1196+
_p("build %s: prebuildmessage%s", stampFile, implicitDeps)
1197+
_p(" prebuildmessage = \"%s && %s\"", cfg.prebuildmessage, touchCmd)
11921198
elseif hasCommands and not hasMessage then
11931199
local commands = os.translateCommandsAndPaths(cfg.prebuildcommands, cfg.project.basedir, cfg.project.location)
1194-
local cmdStr = table.concat(commands, " && ")
1195-
_p("build %s: prebuild%s", prebuildTarget, implicitDeps)
1200+
local cmdStr = table.concat(commands, " && ") .. " && " .. touchCmd
1201+
_p("build %s: prebuild%s", stampFile, implicitDeps)
11961202
_p(" prebuildcommands = %s", cmdStr)
11971203
else
11981204
local commands = os.translateCommandsAndPaths(cfg.prebuildcommands, cfg.project.basedir, cfg.project.location)
1199-
local cmdStr = "echo \"" .. cfg.prebuildmessage .. "\" && " .. table.concat(commands, " && ")
1200-
_p("build %s: prebuild%s", prebuildTarget, implicitDeps)
1205+
local cmdStr = "echo \"" .. cfg.prebuildmessage .. "\" && " .. table.concat(commands, " && ") .. " && " .. touchCmd
1206+
_p("build %s: prebuild%s", stampFile, implicitDeps)
12011207
_p(" prebuildcommands = %s", cmdStr)
12021208
end
12031209

1204-
return prebuildTarget
1210+
return stampFile
12051211
end
12061212

12071213
function m.buildPreLinkEvents(cfg, objectFiles)
@@ -1212,30 +1218,33 @@ function m.buildPreLinkEvents(cfg, objectFiles)
12121218
return nil
12131219
end
12141220

1215-
local targetPath = path.getrelative(cfg.workspace.location, cfg.buildtarget.directory) .. "/" .. cfg.buildtarget.name
1216-
local prelinkTarget = path.getrelative(cfg.workspace.location, cfg.buildtarget.directory) .. "/" .. cfg.project.name .. ".prelinkevents"
1221+
local stampFile = path.getrelative(cfg.workspace.location, cfg.buildtarget.directory) .. "/" .. cfg.project.name .. ".prelink.stamp"
1222+
1223+
-- Determine touch command based on target system, not host system
1224+
local shell = _OPTIONS.shell or iif(cfg.system == p.WINDOWS, "cmd", "posix")
1225+
local touchCmd = iif(shell == "cmd", "type nul > \"" .. stampFile .. "\"", "touch \"" .. stampFile .. "\"")
12171226

12181227
local objDeps = ""
12191228
if objectFiles and #objectFiles > 0 then
12201229
objDeps = " " .. table.concat(objectFiles, " ")
12211230
end
12221231

12231232
if hasMessage and not hasCommands then
1224-
_p("build %s: prelinkmessage%s", prelinkTarget, objDeps)
1225-
_p(" prelinkmessage = \"%s\"", cfg.prelinkmessage)
1233+
_p("build %s: prelinkmessage%s", stampFile, objDeps)
1234+
_p(" prelinkmessage = \"%s && %s\"", cfg.prelinkmessage, touchCmd)
12261235
elseif hasCommands and not hasMessage then
12271236
local commands = os.translateCommandsAndPaths(cfg.prelinkcommands, cfg.project.basedir, cfg.project.location)
1228-
local cmdStr = table.concat(commands, " && ")
1229-
_p("build %s: prelink%s", prelinkTarget, objDeps)
1237+
local cmdStr = table.concat(commands, " && ") .. " && " .. touchCmd
1238+
_p("build %s: prelink%s", stampFile, objDeps)
12301239
_p(" prelinkcommands = %s", cmdStr)
12311240
else
12321241
local commands = os.translateCommandsAndPaths(cfg.prelinkcommands, cfg.project.basedir, cfg.project.location)
1233-
local cmdStr = "echo \"" .. cfg.prelinkmessage .. "\" && " .. table.concat(commands, " && ")
1234-
_p("build %s: prelink%s", prelinkTarget, objDeps)
1242+
local cmdStr = "echo \"" .. cfg.prelinkmessage .. "\" && " .. table.concat(commands, " && ") .. " && " .. touchCmd
1243+
_p("build %s: prelink%s", stampFile, objDeps)
12351244
_p(" prelinkcommands = %s", cmdStr)
12361245
end
12371246

1238-
return prelinkTarget
1247+
return stampFile
12391248
end
12401249

12411250
function m.buildPostBuildEvents(cfg, targetPath)
@@ -1246,22 +1255,28 @@ function m.buildPostBuildEvents(cfg, targetPath)
12461255
return
12471256
end
12481257

1249-
local postbuildPhony = path.getrelative(cfg.workspace.location, cfg.buildtarget.directory) .. "/" .. cfg.project.name .. ".postbuild"
1258+
local stampFile = path.getrelative(cfg.workspace.location, cfg.buildtarget.directory) .. "/" .. cfg.project.name .. ".postbuild.stamp"
1259+
1260+
-- Determine touch command based on target system, not host system
1261+
local shell = _OPTIONS.shell or iif(cfg.system == p.WINDOWS, "cmd", "posix")
1262+
local touchCmd = iif(shell == "cmd", "type nul > \"" .. stampFile .. "\"", "touch \"" .. stampFile .. "\"")
12501263

12511264
if hasMessage and not hasCommands then
1252-
_p("build %s: postbuildmessage | %s", postbuildPhony, targetPath)
1253-
_p(" postbuildmessage = \"%s\"", cfg.postbuildmessage)
1265+
_p("build %s: postbuildmessage | %s", stampFile, targetPath)
1266+
_p(" postbuildmessage = \"%s && %s\"", cfg.postbuildmessage, touchCmd)
12541267
elseif hasCommands and not hasMessage then
12551268
local commands = os.translateCommandsAndPaths(cfg.postbuildcommands, cfg.project.basedir, cfg.project.location)
1256-
local cmdStr = table.concat(commands, " && ")
1257-
_p("build %s: postbuild | %s", postbuildPhony, targetPath)
1269+
local cmdStr = table.concat(commands, " && ") .. " && " .. touchCmd
1270+
_p("build %s: postbuild | %s", stampFile, targetPath)
12581271
_p(" postbuildcommands = %s", cmdStr)
12591272
else
12601273
local commands = os.translateCommandsAndPaths(cfg.postbuildcommands, cfg.project.basedir, cfg.project.location)
1261-
local cmdStr = "echo \"" .. cfg.postbuildmessage .. "\" && " .. table.concat(commands, " && ")
1262-
_p("build %s: postbuild | %s", postbuildPhony, targetPath)
1274+
local cmdStr = "echo \"" .. cfg.postbuildmessage .. "\" && " .. table.concat(commands, " && ") .. " && " .. touchCmd
1275+
_p("build %s: postbuild | %s", stampFile, targetPath)
12631276
_p(" postbuildcommands = %s", cmdStr)
12641277
end
1278+
1279+
return stampFile
12651280
end
12661281

12671282
function m.buildTargets(prj)
@@ -1272,10 +1287,8 @@ function m.buildTargets(prj)
12721287
local targetPath = path.getrelative(cfg.workspace.location, cfg.buildtarget.directory) .. "/" .. cfg.buildtarget.name
12731288
local cfgName = ninja.key(cfg)
12741289

1275-
local hasPostBuild = #cfg.postbuildcommands > 0 or cfg.postbuildmessage
1276-
if hasPostBuild then
1277-
local postbuildTarget = path.getrelative(cfg.workspace.location, cfg.buildtarget.directory) .. "/" .. cfg.project.name .. ".postbuild"
1278-
_p("build %s: phony %s", cfgName, postbuildTarget)
1290+
if cfg._postbuildStamp then
1291+
_p("build %s: phony %s", cfgName, cfg._postbuildStamp)
12791292
else
12801293
_p("build %s: phony %s", cfgName, targetPath)
12811294
end
@@ -1290,13 +1303,10 @@ function m.projectPhonies(prj)
12901303

12911304
local firstCfg = project.getfirstconfig(prj)
12921305
if firstCfg then
1293-
local targetPath = path.getrelative(firstCfg.workspace.location, firstCfg.buildtarget.directory) .. "/" .. firstCfg.buildtarget.name
1294-
1295-
local hasPostBuild = #firstCfg.postbuildcommands > 0 or firstCfg.postbuildmessage
1296-
if hasPostBuild then
1297-
local postbuildTarget = path.getrelative(firstCfg.workspace.location, firstCfg.buildtarget.directory) .. "/" .. firstCfg.project.name .. ".postbuild"
1298-
_p("build %s: phony %s", prj.name, postbuildTarget)
1306+
if firstCfg._postbuildStamp then
1307+
_p("build %s: phony %s", prj.name, firstCfg._postbuildStamp)
12991308
else
1309+
local targetPath = path.getrelative(firstCfg.workspace.location, firstCfg.buildtarget.directory) .. "/" .. firstCfg.buildtarget.name
13001310
_p("build %s: phony %s", prj.name, targetPath)
13011311
end
13021312
end

modules/ninja/tests/_tests.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ return {
1818
"test_ninja_implib.lua",
1919
"test_ninja_pch.lua",
2020
"test_ninja_perfile_config.lua",
21+
"test_ninja_postbuild_stamp.lua",
2122
"test_ninja_project.lua",
2223
"test_ninja_tokens.lua",
2324
"test_ninja_workspace.lua",

modules/ninja/tests/test_ninja_build_commands.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,6 @@ function suite.prebuildCommands_withQuotedPaths_onWindows()
192192
cpp.buildPreBuildEvents(cfg)
193193

194194
-- The command should have quotes around paths
195-
test.string_contains(premake.captured(), 'copy /B /Y')
195+
local captured = premake.captured()
196+
test.istrue(captured:find('copy /B /Y', 1, true) ~= nil)
196197
end

modules/ninja/tests/test_ninja_config.lua

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ target_MyProject_Debug = MyProject
596596
--
597597

598598
function suite.prebuildEvents_onCommands()
599+
system "Linux"
599600
toolset "gcc"
600601
kind "ConsoleApp"
601602
files { "main.cpp" }
@@ -606,8 +607,8 @@ target_MyProject_Debug = MyProject
606607

607608
test.isnotnil(result)
608609
test.capture [[
609-
build bin/Debug/MyProject.prebuild: prebuild
610-
prebuildcommands = echo Building
610+
build bin/Debug/MyProject.prebuild.stamp: prebuild
611+
prebuildcommands = echo Building && touch "bin/Debug/MyProject.prebuild.stamp"
611612
]]
612613
end
613614

@@ -616,6 +617,7 @@ build bin/Debug/MyProject.prebuild: prebuild
616617
--
617618

618619
function suite.prebuildEvents_onMessage()
620+
system "Linux"
619621
toolset "gcc"
620622
kind "ConsoleApp"
621623
files { "main.cpp" }
@@ -626,8 +628,8 @@ build bin/Debug/MyProject.prebuild: prebuild
626628

627629
test.isnotnil(result)
628630
test.capture [[
629-
build bin/Debug/MyProject.prebuild: prebuildmessage
630-
prebuildmessage = "Building project"
631+
build bin/Debug/MyProject.prebuild.stamp: prebuildmessage
632+
prebuildmessage = "Building project && touch "bin/Debug/MyProject.prebuild.stamp""
631633
]]
632634
end
633635

@@ -636,6 +638,7 @@ build bin/Debug/MyProject.prebuild: prebuildmessage
636638
--
637639

638640
function suite.prebuildEvents_onMessageAndCommands()
641+
system "Linux"
639642
toolset "gcc"
640643
kind "ConsoleApp"
641644
files { "main.cpp" }
@@ -647,8 +650,8 @@ build bin/Debug/MyProject.prebuild: prebuildmessage
647650

648651
test.isnotnil(result)
649652
test.capture [[
650-
build bin/Debug/MyProject.prebuild: prebuild
651-
prebuildcommands = echo "Building project" && mkdir -p build && cp file.txt build/
653+
build bin/Debug/MyProject.prebuild.stamp: prebuild
654+
prebuildcommands = echo "Building project" && mkdir -p build && cp file.txt build/ && touch "bin/Debug/MyProject.prebuild.stamp"
652655
]]
653656
end
654657

@@ -677,6 +680,7 @@ build bin/Debug/MyProject.prebuild: prebuild
677680
--
678681

679682
function suite.prelinkEvents_onCommands()
683+
system "Linux"
680684
toolset "gcc"
681685
kind "ConsoleApp"
682686
files { "main.cpp" }
@@ -688,8 +692,8 @@ build bin/Debug/MyProject.prebuild: prebuild
688692

689693
test.isnotnil(result)
690694
test.capture [[
691-
build bin/Debug/MyProject.prelinkevents: prelink obj/Debug/main.o
692-
prelinkcommands = echo Linking
695+
build bin/Debug/MyProject.prelink.stamp: prelink obj/Debug/main.o
696+
prelinkcommands = echo Linking && touch "bin/Debug/MyProject.prelink.stamp"
693697
]]
694698
end
695699

@@ -698,6 +702,7 @@ build bin/Debug/MyProject.prelinkevents: prelink obj/Debug/main.o
698702
--
699703

700704
function suite.prelinkEvents_onMessage()
705+
system "Linux"
701706
toolset "gcc"
702707
kind "ConsoleApp"
703708
files { "main.cpp" }
@@ -709,8 +714,8 @@ build bin/Debug/MyProject.prelinkevents: prelink obj/Debug/main.o
709714

710715
test.isnotnil(result)
711716
test.capture [[
712-
build bin/Debug/MyProject.prelinkevents: prelinkmessage obj/Debug/main.o
713-
prelinkmessage = "Linking project"
717+
build bin/Debug/MyProject.prelink.stamp: prelinkmessage obj/Debug/main.o
718+
prelinkmessage = "Linking project && touch "bin/Debug/MyProject.prelink.stamp""
714719
]]
715720
end
716721

@@ -719,6 +724,7 @@ build bin/Debug/MyProject.prelinkevents: prelinkmessage obj/Debug/main.o
719724
--
720725

721726
function suite.prelinkEvents_onMessageAndCommands()
727+
system "Linux"
722728
toolset "gcc"
723729
kind "ConsoleApp"
724730
files { "main.cpp" }
@@ -731,8 +737,8 @@ build bin/Debug/MyProject.prelinkevents: prelinkmessage obj/Debug/main.o
731737

732738
test.isnotnil(result)
733739
test.capture [[
734-
build bin/Debug/MyProject.prelinkevents: prelink obj/Debug/main.o
735-
prelinkcommands = echo "Preparing link" && echo prelinking && ls -la
740+
build bin/Debug/MyProject.prelink.stamp: prelink obj/Debug/main.o
741+
prelinkcommands = echo "Preparing link" && echo prelinking && ls -la && touch "bin/Debug/MyProject.prelink.stamp"
736742
]]
737743
end
738744

@@ -756,6 +762,7 @@ build bin/Debug/MyProject.prelinkevents: prelink obj/Debug/main.o
756762
--
757763

758764
function suite.prelinkEvents_withMultipleObjectFiles()
765+
system "Linux"
759766
toolset "gcc"
760767
kind "ConsoleApp"
761768
files { "main.cpp", "foo.cpp", "bar.cpp" }
@@ -767,8 +774,8 @@ build bin/Debug/MyProject.prelinkevents: prelink obj/Debug/main.o
767774

768775
test.isnotnil(result)
769776
test.capture [[
770-
build bin/Debug/MyProject.prelinkevents: prelink obj/Debug/main.o obj/Debug/foo.o obj/Debug/bar.o
771-
prelinkcommands = echo Linking
777+
build bin/Debug/MyProject.prelink.stamp: prelink obj/Debug/main.o obj/Debug/foo.o obj/Debug/bar.o
778+
prelinkcommands = echo Linking && touch "bin/Debug/MyProject.prelink.stamp"
772779
]]
773780
end
774781

@@ -782,6 +789,7 @@ build bin/Debug/MyProject.prelinkevents: prelink obj/Debug/main.o obj/Debug/foo.
782789
--
783790

784791
function suite.postbuildEvents_onCommands()
792+
system "Linux"
785793
toolset "gcc"
786794
kind "ConsoleApp"
787795
files { "main.cpp" }
@@ -791,8 +799,8 @@ build bin/Debug/MyProject.prelinkevents: prelink obj/Debug/main.o obj/Debug/foo.
791799
cpp.buildPostBuildEvents(cfg, "bin/Debug/MyProject")
792800

793801
test.capture [[
794-
build bin/Debug/MyProject.postbuild: postbuild | bin/Debug/MyProject
795-
postbuildcommands = echo Done
802+
build bin/Debug/MyProject.postbuild.stamp: postbuild | bin/Debug/MyProject
803+
postbuildcommands = echo Done && touch "bin/Debug/MyProject.postbuild.stamp"
796804
]]
797805
end
798806

@@ -801,6 +809,7 @@ build bin/Debug/MyProject.postbuild: postbuild | bin/Debug/MyProject
801809
--
802810

803811
function suite.postbuildEvents_onMessage()
812+
system "Linux"
804813
toolset "gcc"
805814
kind "ConsoleApp"
806815
files { "main.cpp" }
@@ -810,8 +819,8 @@ build bin/Debug/MyProject.postbuild: postbuild | bin/Debug/MyProject
810819
cpp.buildPostBuildEvents(cfg, "bin/Debug/MyProject")
811820

812821
test.capture [[
813-
build bin/Debug/MyProject.postbuild: postbuildmessage | bin/Debug/MyProject
814-
postbuildmessage = "Build complete"
822+
build bin/Debug/MyProject.postbuild.stamp: postbuildmessage | bin/Debug/MyProject
823+
postbuildmessage = "Build complete && touch "bin/Debug/MyProject.postbuild.stamp""
815824
]]
816825
end
817826

@@ -820,6 +829,7 @@ build bin/Debug/MyProject.postbuild: postbuildmessage | bin/Debug/MyProject
820829
--
821830

822831
function suite.postbuildEvents_onMessageAndCommands()
832+
system "Linux"
823833
toolset "gcc"
824834
kind "ConsoleApp"
825835
files { "main.cpp" }
@@ -830,8 +840,8 @@ build bin/Debug/MyProject.postbuild: postbuildmessage | bin/Debug/MyProject
830840
cpp.buildPostBuildEvents(cfg, "bin/Debug/MyProject")
831841

832842
test.capture [[
833-
build bin/Debug/MyProject.postbuild: postbuild | bin/Debug/MyProject
834-
postbuildcommands = echo "Finishing build" && cp bin/Debug/MyProject /usr/local/bin/ && chmod +x /usr/local/bin/MyProject
843+
build bin/Debug/MyProject.postbuild.stamp: postbuild | bin/Debug/MyProject
844+
postbuildcommands = echo "Finishing build" && cp bin/Debug/MyProject /usr/local/bin/ && chmod +x /usr/local/bin/MyProject && touch "bin/Debug/MyProject.postbuild.stamp"
835845
]]
836846
end
837847

0 commit comments

Comments
 (0)