Skip to content

Commit dbb5f95

Browse files
committed
Add buildoptions, linkoptions support
1 parent 1fc5b66 commit dbb5f95

File tree

6 files changed

+192
-42
lines changed

6 files changed

+192
-42
lines changed

modules/ninja/_preload.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ newaction {
3333
elseif target == p.EMSCRIPTEN then
3434
return "emcc"
3535
elseif target == p.WINDOWS then
36-
return "v143"
36+
return "msc"
3737
else
3838
return "gcc"
3939
end

modules/ninja/ninja.lua

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,14 @@ p.modules.ninja._VERSION = p._VERSION
1212

1313
local ninja = p.modules.ninja
1414

15-
--
16-
-- Escape a string so it can be written to a Ninja build file.
17-
-- Ninja variables are expanded into shell commands, so we need to escape
18-
-- shell special characters as well as Ninja special characters.
19-
--
2015
function ninja.esc(value)
2116
value = value:gsub("%$", "$$")
2217
value = value:gsub(":", "$:")
2318
value = value:gsub("\n", "$\n")
24-
value = value:gsub(" ", "$ ")
2519
value = value:gsub('%(', '\\(')
2620
value = value:gsub('%)', '\\)')
2721
value = value:gsub('"', '\\"')
22+
value = value:gsub(" ", "\\ ")
2823

2924
return value
3025
end

modules/ninja/ninja_cpp.lua

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ function m.generate(prj)
3434
_p("") -- Empty line at end of file
3535
end
3636

37-
-- Generate all ninja rules (compile, link, etc.)
3837
function m.rules(prj)
39-
-- Generate rules for each configuration to handle different toolsets
4038
local rulesDone = {}
4139

4240
for cfg in project.eachconfig(prj) do
@@ -298,12 +296,18 @@ function m.getCFlags(cfg, toolset)
298296

299297
local defines = toolset.getdefines(cfg.defines)
300298
flags = table.join(flags, defines)
299+
300+
local undefines = toolset.getundefines(cfg.undefines)
301+
flags = table.join(flags, undefines)
301302

302303
local includedirs = toolset.getincludedirs(cfg, cfg.includedirs, cfg.externalincludedirs, cfg.frameworkdirs)
303304
flags = table.join(flags, includedirs)
304305

305306
local forceincludes = toolset.getforceincludes(cfg)
306307
flags = table.join(flags, forceincludes)
308+
309+
local buildopts = cfg.buildoptions or {}
310+
flags = table.join(flags, buildopts)
307311

308312
return flags
309313
end
@@ -317,12 +321,18 @@ function m.getCxxFlags(cfg, toolset)
317321

318322
local defines = toolset.getdefines(cfg.defines)
319323
flags = table.join(flags, defines)
324+
325+
local undefines = toolset.getundefines(cfg.undefines)
326+
flags = table.join(flags, undefines)
320327

321328
local includedirs = toolset.getincludedirs(cfg, cfg.includedirs, cfg.externalincludedirs, cfg.frameworkdirs)
322329
flags = table.join(flags, includedirs)
323330

324331
local forceincludes = toolset.getforceincludes(cfg)
325332
flags = table.join(flags, forceincludes)
333+
334+
local buildopts = cfg.buildoptions or {}
335+
flags = table.join(flags, buildopts)
326336

327337
return flags
328338
end
@@ -333,6 +343,9 @@ function m.getLdFlags(cfg, toolset)
333343

334344
local toolFlags = toolset.getldflags(cfg)
335345
flags = table.join(flags, toolFlags)
346+
347+
local linkopts = cfg.linkoptions or {}
348+
flags = table.join(flags, linkopts)
336349

337350
local libdirs = toolset.getLibraryDirectories(cfg)
338351
flags = table.join(flags, libdirs)
@@ -630,15 +643,15 @@ function m.buildPreBuildEvents(cfg)
630643

631644
if hasMessage and not hasCommands then
632645
_p("build %s: prebuildmessage", prebuildTarget)
633-
_p(" prebuildmessage = %s", cfg.prebuildmessage)
646+
_p(" prebuildmessage = \"%s\"", cfg.prebuildmessage)
634647
elseif hasCommands and not hasMessage then
635648
local commands = os.translateCommandsAndPaths(cfg.prebuildcommands, cfg.project.basedir, cfg.project.location)
636649
local cmdStr = table.concat(commands, " && ")
637650
_p("build %s: prebuild", prebuildTarget)
638651
_p(" prebuildcommands = %s", cmdStr)
639652
else
640653
local commands = os.translateCommandsAndPaths(cfg.prebuildcommands, cfg.project.basedir, cfg.project.location)
641-
local cmdStr = "echo " .. ninja.esc(cfg.prebuildmessage) .. " && " .. table.concat(commands, " && ")
654+
local cmdStr = "echo \"" .. cfg.prebuildmessage .. "\" && " .. table.concat(commands, " && ")
642655
_p("build %s: prebuild", prebuildTarget)
643656
_p(" prebuildcommands = %s", cmdStr)
644657
end
@@ -659,15 +672,15 @@ function m.buildPreLinkEvents(cfg)
659672

660673
if hasMessage and not hasCommands then
661674
_p("build %s: prelinkmessage", prelinkTarget)
662-
_p(" prelinkmessage = %s", cfg.prelinkmessage)
675+
_p(" prelinkmessage = \"%s\"", cfg.prelinkmessage)
663676
elseif hasCommands and not hasMessage then
664677
local commands = os.translateCommandsAndPaths(cfg.prelinkcommands, cfg.project.basedir, cfg.project.location)
665678
local cmdStr = table.concat(commands, " && ")
666679
_p("build %s: prelink", prelinkTarget)
667680
_p(" prelinkcommands = %s", cmdStr)
668681
else
669682
local commands = os.translateCommandsAndPaths(cfg.prelinkcommands, cfg.project.basedir, cfg.project.location)
670-
local cmdStr = "echo " .. ninja.esc(cfg.prelinkmessage) .. " && " .. table.concat(commands, " && ")
683+
local cmdStr = "echo \"" .. cfg.prelinkmessage .. "\" && " .. table.concat(commands, " && ")
671684
_p("build %s: prelink", prelinkTarget)
672685
_p(" prelinkcommands = %s", cmdStr)
673686
end
@@ -685,15 +698,15 @@ function m.buildPostBuildEvents(cfg, linkTarget, finalTarget)
685698

686699
if hasMessage and not hasCommands then
687700
_p("build %s: postbuildmessage %s", finalTarget, linkTarget)
688-
_p(" postbuildmessage = %s", cfg.postbuildmessage)
701+
_p(" postbuildmessage = \"%s\"", cfg.postbuildmessage)
689702
elseif hasCommands and not hasMessage then
690703
local commands = os.translateCommandsAndPaths(cfg.postbuildcommands, cfg.project.basedir, cfg.project.location)
691704
local cmdStr = table.concat(commands, " && ")
692705
_p("build %s: postbuild %s", finalTarget, linkTarget)
693706
_p(" postbuildcommands = %s", cmdStr)
694707
else
695708
local commands = os.translateCommandsAndPaths(cfg.postbuildcommands, cfg.project.basedir, cfg.project.location)
696-
local cmdStr = "echo " .. ninja.esc(cfg.postbuildmessage) .. " && " .. table.concat(commands, " && ")
709+
local cmdStr = "echo \"" .. cfg.postbuildmessage .. "\" && " .. table.concat(commands, " && ")
697710
_p("build %s: postbuild %s", finalTarget, linkTarget)
698711
_p(" postbuildcommands = %s", cmdStr)
699712
end

modules/ninja/tests/test_ninja_config.lua

Lines changed: 150 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ target_MyProject_Debug = MyProject
105105
kind "ConsoleApp"
106106
files { "main.cpp" }
107107
defines { "DEBUG", "PLATFORM_LINUX" }
108+
defines { 'HELLO="HELLO WORLD"' }
109+
defines { "VALUE=with_paren()"}
108110

109111
local cfg = prepare()
110112
cpp.configurationVariables(cfg)
111113

112114
test.capture [[
113-
cflags_MyProject_Debug = -DDEBUG -DPLATFORM_LINUX
114-
cxxflags_MyProject_Debug = -DDEBUG -DPLATFORM_LINUX
115+
cflags_MyProject_Debug = -DDEBUG -DPLATFORM_LINUX -DHELLO="HELLO WORLD" -DVALUE=with_paren()
116+
cxxflags_MyProject_Debug = -DDEBUG -DPLATFORM_LINUX -DHELLO="HELLO WORLD" -DVALUE=with_paren()
115117
ldflags_MyProject_Debug = -s
116118
objdir_MyProject_Debug = obj/Debug
117119
targetdir_MyProject_Debug = bin/Debug
@@ -370,6 +372,146 @@ target_MyProject_Debug = MyProject
370372
end
371373

372374

375+
--
376+
-- Check that buildoptions are included in flags.
377+
--
378+
379+
function suite.configVars_withBuildOptions_Linux()
380+
toolset "gcc"
381+
_OS = "Linux"
382+
kind "ConsoleApp"
383+
files { "main.cpp" }
384+
buildoptions { "-Wall", "-Wextra" }
385+
386+
local cfg = prepare()
387+
cpp.configurationVariables(cfg)
388+
389+
test.capture [[
390+
cflags_MyProject_Debug = -Wall -Wextra
391+
cxxflags_MyProject_Debug = -Wall -Wextra
392+
ldflags_MyProject_Debug = -s
393+
objdir_MyProject_Debug = obj/Debug
394+
targetdir_MyProject_Debug = bin/Debug
395+
target_MyProject_Debug = MyProject
396+
397+
]]
398+
end
399+
400+
401+
function suite.configVars_withBuildOptions_Windows()
402+
toolset "msc"
403+
_OS = "Windows"
404+
kind "ConsoleApp"
405+
files { "main.cpp" }
406+
buildoptions { "/W4", "/WX" }
407+
408+
local cfg = prepare()
409+
cpp.configurationVariables(cfg)
410+
411+
test.capture [[
412+
cflags_MyProject_Debug = /MD /W4 /WX
413+
cxxflags_MyProject_Debug = /MD /EHsc /W4 /WX
414+
ldflags_MyProject_Debug = /NOLOGO
415+
objdir_MyProject_Debug = obj/Debug
416+
targetdir_MyProject_Debug = bin/Debug
417+
target_MyProject_Debug = MyProject.exe
418+
419+
]]
420+
end
421+
422+
423+
function suite.configVars_withBuildOptions_Macosx()
424+
toolset "gcc"
425+
_OS = "macosx"
426+
kind "ConsoleApp"
427+
files { "main.cpp" }
428+
buildoptions { "-Wall", "-Wextra" }
429+
430+
local cfg = prepare()
431+
cpp.configurationVariables(cfg)
432+
433+
test.capture [[
434+
cflags_MyProject_Debug = -Wall -Wextra
435+
cxxflags_MyProject_Debug = -Wall -Wextra
436+
ldflags_MyProject_Debug = -Wl,-x
437+
objdir_MyProject_Debug = obj/Debug
438+
targetdir_MyProject_Debug = bin/Debug
439+
target_MyProject_Debug = MyProject
440+
441+
]]
442+
end
443+
444+
445+
--
446+
-- Check that undefines are included in flags.
447+
--
448+
449+
function suite.configVars_withUndefines_Linux()
450+
toolset "gcc"
451+
_OS = "Linux"
452+
kind "ConsoleApp"
453+
files { "main.cpp" }
454+
undefines { "NDEBUG", "OLD_PLATFORM" }
455+
456+
local cfg = prepare()
457+
cpp.configurationVariables(cfg)
458+
459+
test.capture [[
460+
cflags_MyProject_Debug = -UNDEBUG -UOLD_PLATFORM
461+
cxxflags_MyProject_Debug = -UNDEBUG -UOLD_PLATFORM
462+
ldflags_MyProject_Debug = -s
463+
objdir_MyProject_Debug = obj/Debug
464+
targetdir_MyProject_Debug = bin/Debug
465+
target_MyProject_Debug = MyProject
466+
467+
]]
468+
end
469+
470+
471+
function suite.configVars_withUndefines_Windows()
472+
toolset "msc"
473+
_OS = "Windows"
474+
kind "ConsoleApp"
475+
files { "main.cpp" }
476+
undefines { "NDEBUG", "OLD_PLATFORM" }
477+
478+
local cfg = prepare()
479+
cpp.configurationVariables(cfg)
480+
481+
test.capture [[
482+
cflags_MyProject_Debug = /MD /UNDEBUG /UOLD_PLATFORM
483+
cxxflags_MyProject_Debug = /MD /EHsc /UNDEBUG /UOLD_PLATFORM
484+
ldflags_MyProject_Debug = /NOLOGO
485+
objdir_MyProject_Debug = obj/Debug
486+
targetdir_MyProject_Debug = bin/Debug
487+
target_MyProject_Debug = MyProject.exe
488+
489+
]]
490+
end
491+
492+
493+
function suite.configVars_withUndefines_Macosx()
494+
toolset "gcc"
495+
_OS = "macosx"
496+
kind "ConsoleApp"
497+
files { "main.cpp" }
498+
undefines { "NDEBUG", "OLD_PLATFORM" }
499+
500+
local cfg = prepare()
501+
cpp.configurationVariables(cfg)
502+
503+
test.capture [[
504+
cflags_MyProject_Debug = -UNDEBUG -UOLD_PLATFORM
505+
cxxflags_MyProject_Debug = -UNDEBUG -UOLD_PLATFORM
506+
ldflags_MyProject_Debug = -Wl,-x
507+
objdir_MyProject_Debug = obj/Debug
508+
targetdir_MyProject_Debug = bin/Debug
509+
target_MyProject_Debug = MyProject
510+
511+
]]
512+
end
513+
514+
373515
---
374516
-- C/C++ flags function tests
375517
---
@@ -485,7 +627,7 @@ build bin/Debug/MyProject.prebuild: prebuild
485627
test.isnotnil(result)
486628
test.capture [[
487629
build bin/Debug/MyProject.prebuild: prebuildmessage
488-
prebuildmessage = Building project
630+
prebuildmessage = "Building project"
489631
]]
490632
end
491633

@@ -506,7 +648,7 @@ build bin/Debug/MyProject.prebuild: prebuildmessage
506648
test.isnotnil(result)
507649
test.capture [[
508650
build bin/Debug/MyProject.prebuild: prebuild
509-
prebuildcommands = echo Building$ project && mkdir -p build && cp file.txt build/
651+
prebuildcommands = echo "Building project" && mkdir -p build && cp file.txt build/
510652
]]
511653
end
512654

@@ -566,7 +708,7 @@ build bin/Debug/MyProject.prelinkevents: prelink
566708
test.isnotnil(result)
567709
test.capture [[
568710
build bin/Debug/MyProject.prelinkevents: prelinkmessage
569-
prelinkmessage = Linking project
711+
prelinkmessage = "Linking project"
570712
]]
571713
end
572714

@@ -587,7 +729,7 @@ build bin/Debug/MyProject.prelinkevents: prelinkmessage
587729
test.isnotnil(result)
588730
test.capture [[
589731
build bin/Debug/MyProject.prelinkevents: prelink
590-
prelinkcommands = echo Preparing$ link && echo prelinking && ls -la
732+
prelinkcommands = echo "Preparing link" && echo prelinking && ls -la
591733
]]
592734
end
593735

@@ -645,7 +787,7 @@ build bin/Debug/MyProject: postbuild bin/Debug/MyProject.link
645787

646788
test.capture [[
647789
build bin/Debug/MyProject: postbuildmessage bin/Debug/MyProject.link
648-
postbuildmessage = Build complete
790+
postbuildmessage = "Build complete"
649791
]]
650792
end
651793

@@ -665,6 +807,6 @@ build bin/Debug/MyProject: postbuildmessage bin/Debug/MyProject.link
665807

666808
test.capture [[
667809
build bin/Debug/MyProject: postbuild bin/Debug/MyProject.link
668-
postbuildcommands = echo Finishing$ build && cp bin/Debug/MyProject /usr/local/bin/ && chmod +x /usr/local/bin/MyProject
810+
postbuildcommands = echo "Finishing build" && cp bin/Debug/MyProject /usr/local/bin/ && chmod +x /usr/local/bin/MyProject
669811
]]
670812
end

website/docs/Using-Premake.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ premake5 [action]
1616

1717
Premake defines the following list of actions out of the box; projects may also add their own custom actions.
1818

19-
| Action | Description |
20-
|-------------|---------------------------------------------------|
21-
| vs2026 | Generate Visual Studio 2026 project files |
22-
| vs2022 | Generate Visual Studio 2022 project files |
23-
| vs2019 | Generate Visual Studio 2019 project files |
24-
| vs2017 | Generate Visual Studio 2017 project files |
25-
| vs2015 | Generate Visual Studio 2015 project files |
26-
| vs2013 | Generate Visual Studio 2013 project files |
27-
| vs2012 | Generate Visual Studio 2012 project files |
28-
| vs2010 | Generate Visual Studio 2010 project files |
29-
| vs2008 | Generate Visual Studio 2008 project files |
30-
| vs2005 | Generate Visual Studio 2005 project files |
19+
| Action | Description |
20+
|-------------|---------------------------------------------------------------|
21+
| codelite | CodeLite projects |
3122
| gmake | Generate GNU Makefiles (including [Cygwin][1] and [MinGW][2]) |
32-
| gmakelegacy | Generate GNU Makefiles (deprecated exporter) |
33-
| xcode4 | XCode projects |
34-
| codelite | CodeLite projects |
35-
| ninja | Ninja projects |
23+
| gmakelegacy | Generate GNU Makefiles (deprecated exporter) |
24+
| ninja | Ninja projects |
25+
| vs2026 | Generate Visual Studio 2026 project files |
26+
| vs2022 | Generate Visual Studio 2022 project files |
27+
| vs2019 | Generate Visual Studio 2019 project files |
28+
| vs2017 | Generate Visual Studio 2017 project files |
29+
| vs2015 | Generate Visual Studio 2015 project files |
30+
| vs2013 | Generate Visual Studio 2013 project files |
31+
| vs2012 | Generate Visual Studio 2012 project files |
32+
| vs2010 | Generate Visual Studio 2010 project files |
33+
| vs2008 | Generate Visual Studio 2008 project files |
34+
| vs2005 | Generate Visual Studio 2005 project files |
35+
| xcode4 | XCode projects |
3636

3737
(Premake4 supported some additional actions that haven't yet been ported to this new version; see the [Available Feature Matrix](Feature-Matrix.md) for the whole list.)
3838

0 commit comments

Comments
 (0)