Skip to content

Commit 3773922

Browse files
committed
Tweak MSC defines creation
1 parent fce9cc6 commit 3773922

File tree

6 files changed

+184
-44
lines changed

6 files changed

+184
-44
lines changed

modules/ninja/ninja_cpp.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ function m.getCFlags(cfg, toolset)
361361
local toolFlags = toolset.getcflags(cfg)
362362
flags = table.join(flags, toolFlags)
363363

364-
local defines = toolset.getdefines(cfg.defines)
364+
local defines = toolset.getdefines(cfg.defines, cfg)
365365
flags = table.join(flags, defines)
366366

367367
local undefines = toolset.getundefines(cfg.undefines)
@@ -391,7 +391,7 @@ function m.getFileCFlags(cfg, filecfg, toolset)
391391
end
392392

393393
local allDefines = table.join(cfg.defines or {}, filecfg.defines or {})
394-
local defines = toolset.getdefines(allDefines)
394+
local defines = toolset.getdefines(allDefines, cfg)
395395
flags = table.join(flags, defines)
396396

397397
local allUndefines = table.join(cfg.undefines or {}, filecfg.undefines or {})
@@ -421,7 +421,7 @@ function m.getCxxFlags(cfg, toolset)
421421
local toolFlags = toolset.getcxxflags(cfg)
422422
flags = table.join(flags, toolFlags)
423423

424-
local defines = toolset.getdefines(cfg.defines)
424+
local defines = toolset.getdefines(cfg.defines, cfg)
425425
flags = table.join(flags, defines)
426426

427427
local undefines = toolset.getundefines(cfg.undefines)
@@ -451,7 +451,7 @@ function m.getFileCxxFlags(cfg, filecfg, toolset)
451451
end
452452

453453
local allDefines = table.join(cfg.defines or {}, filecfg.defines or {})
454-
local defines = toolset.getdefines(allDefines)
454+
local defines = toolset.getdefines(allDefines, cfg)
455455
flags = table.join(flags, defines)
456456

457457
local allUndefines = table.join(cfg.undefines or {}, filecfg.undefines or {})

modules/ninja/tests/test_ninja_config.lua

Lines changed: 126 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ target_MyProject_Debug = MyProject
6666
cpp.configurationVariables(cfg)
6767

6868
test.capture [[
69-
cflags_MyProject_Debug = /MD
70-
cxxflags_MyProject_Debug = /MD /EHsc
69+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE
70+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE
7171
ldflags_MyProject_Debug = /NOLOGO
7272
objdir_MyProject_Debug = obj/Debug
7373
targetdir_MyProject_Debug = bin/Debug
@@ -134,8 +134,8 @@ target_MyProject_Debug = MyProject
134134
cpp.configurationVariables(cfg)
135135

136136
test.capture [[
137-
cflags_MyProject_Debug = /MD /DDEBUG /DPLATFORM_WINDOWS
138-
cxxflags_MyProject_Debug = /MD /EHsc /DDEBUG /DPLATFORM_WINDOWS
137+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE /DDEBUG /DPLATFORM_WINDOWS
138+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE /DDEBUG /DPLATFORM_WINDOWS
139139
ldflags_MyProject_Debug = /NOLOGO
140140
objdir_MyProject_Debug = obj/Debug
141141
targetdir_MyProject_Debug = bin/Debug
@@ -204,8 +204,8 @@ target_MyProject_Debug = MyProject
204204
cpp.configurationVariables(cfg)
205205

206206
test.capture [[
207-
cflags_MyProject_Debug = /MD /Iinclude /Iexternal
208-
cxxflags_MyProject_Debug = /MD /EHsc /Iinclude /Iexternal
207+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE /Iinclude /Iexternal
208+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE /Iinclude /Iexternal
209209
ldflags_MyProject_Debug = /NOLOGO
210210
objdir_MyProject_Debug = obj/Debug
211211
targetdir_MyProject_Debug = bin/Debug
@@ -272,8 +272,8 @@ target_MyProject_Debug = MyProject
272272
cpp.configurationVariables(cfg)
273273

274274
test.capture [[
275-
cflags_MyProject_Debug = /MD
276-
cxxflags_MyProject_Debug = /MD /EHsc
275+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE
276+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE
277277
ldflags_MyProject_Debug = /NOLOGO /LIBPATH:"lib" /LIBPATH:"external/lib"
278278
objdir_MyProject_Debug = obj/Debug
279279
targetdir_MyProject_Debug = bin/Debug
@@ -339,8 +339,8 @@ target_MyProject_Debug = MyProject
339339
cpp.configurationVariables(cfg)
340340

341341
test.capture [[
342-
cflags_MyProject_Debug = /MD
343-
cxxflags_MyProject_Debug = /MD /EHsc
342+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE
343+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE
344344
ldflags_MyProject_Debug = /NOLOGO
345345
links_MyProject_Debug = User32.lib Gdi32.lib
346346
objdir_MyProject_Debug = obj/Debug
@@ -409,8 +409,8 @@ target_MyProject_Debug = MyProject
409409
cpp.configurationVariables(cfg)
410410

411411
test.capture [[
412-
cflags_MyProject_Debug = /MD /W4 /WX
413-
cxxflags_MyProject_Debug = /MD /EHsc /W4 /WX
412+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE /W4 /WX
413+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE /W4 /WX
414414
ldflags_MyProject_Debug = /NOLOGO
415415
objdir_MyProject_Debug = obj/Debug
416416
targetdir_MyProject_Debug = bin/Debug
@@ -479,8 +479,8 @@ target_MyProject_Debug = MyProject
479479
cpp.configurationVariables(cfg)
480480

481481
test.capture [[
482-
cflags_MyProject_Debug = /MD /UNDEBUG /UOLD_PLATFORM
483-
cxxflags_MyProject_Debug = /MD /EHsc /UNDEBUG /UOLD_PLATFORM
482+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE /UNDEBUG /UOLD_PLATFORM
483+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE /UNDEBUG /UOLD_PLATFORM
484484
ldflags_MyProject_Debug = /NOLOGO
485485
objdir_MyProject_Debug = obj/Debug
486486
targetdir_MyProject_Debug = bin/Debug
@@ -834,3 +834,115 @@ build bin/Debug/MyProject.postbuild: postbuild | bin/Debug/MyProject
834834
postbuildcommands = echo "Finishing build" && cp bin/Debug/MyProject /usr/local/bin/ && chmod +x /usr/local/bin/MyProject
835835
]]
836836
end
837+
838+
839+
--
840+
-- Check getCxxFlags includes characterset defines for MSC with default character set.
841+
--
842+
843+
function suite.getCxxFlags_withMSCCharactersetDefault()
844+
toolset "msc"
845+
_OS = "Windows"
846+
files { "main.cpp" }
847+
characterset "Default"
848+
849+
local cfg = prepare()
850+
local toolset = p.tools.msc
851+
local flags = table.concat(cpp.getCxxFlags(cfg, toolset), " ")
852+
853+
test.istrue(flags:find("/D_UNICODE") ~= nil)
854+
test.istrue(flags:find("/DUNICODE") ~= nil)
855+
end
856+
857+
--
858+
-- Check getCxxFlags includes characterset defines for MSC with Unicode character set.
859+
--
860+
861+
function suite.getCxxFlags_withMSCCharactersetUnicode()
862+
toolset "msc"
863+
_OS = "Windows"
864+
files { "main.cpp" }
865+
characterset "Unicode"
866+
867+
local cfg = prepare()
868+
local toolset = p.tools.msc
869+
local flags = table.concat(cpp.getCxxFlags(cfg, toolset), " ")
870+
871+
test.istrue(flags:find("/D_UNICODE") ~= nil)
872+
test.istrue(flags:find("/DUNICODE") ~= nil)
873+
end
874+
875+
--
876+
-- Check getCxxFlags includes characterset defines for MSC with MBCS character set.
877+
--
878+
879+
function suite.getCxxFlags_withMSCCharactersetMBCS()
880+
toolset "msc"
881+
_OS = "Windows"
882+
files { "main.cpp" }
883+
characterset "MBCS"
884+
885+
local cfg = prepare()
886+
local toolset = p.tools.msc
887+
local flags = table.concat(cpp.getCxxFlags(cfg, toolset), " ")
888+
889+
test.istrue(flags:find("/D_MBCS") ~= nil)
890+
test.isfalse(flags:find("/D_UNICODE") ~= nil)
891+
test.isfalse(flags:find("/DUNICODE") ~= nil)
892+
end
893+
894+
--
895+
-- Check getCxxFlags excludes characterset defines for MSC with ASCII character set.
896+
--
897+
898+
function suite.getCxxFlags_withMSCCharactersetASCII()
899+
toolset "msc"
900+
_OS = "Windows"
901+
files { "main.cpp" }
902+
characterset "ASCII"
903+
904+
local cfg = prepare()
905+
local toolset = p.tools.msc
906+
local flags = table.concat(cpp.getCxxFlags(cfg, toolset), " ")
907+
908+
test.isfalse(flags:find("/D_UNICODE") ~= nil)
909+
test.isfalse(flags:find("/DUNICODE") ~= nil)
910+
test.isfalse(flags:find("/D_MBCS") ~= nil)
911+
end
912+
913+
--
914+
-- Check getCFlags includes characterset defines for MSC with default character set.
915+
--
916+
917+
function suite.getCFlags_withMSCCharactersetDefault()
918+
toolset "msc"
919+
_OS = "Windows"
920+
files { "main.c" }
921+
characterset "Default"
922+
923+
local cfg = prepare()
924+
local toolset = p.tools.msc
925+
local flags = table.concat(cpp.getCFlags(cfg, toolset), " ")
926+
927+
test.istrue(flags:find("/D_UNICODE") ~= nil)
928+
test.istrue(flags:find("/DUNICODE") ~= nil)
929+
end
930+
931+
--
932+
-- Check getCFlags includes characterset defines for MSC with MBCS character set.
933+
--
934+
935+
function suite.getCFlags_withMSCCharactersetMBCS()
936+
toolset "msc"
937+
_OS = "Windows"
938+
files { "main.c" }
939+
characterset "MBCS"
940+
941+
local cfg = prepare()
942+
local toolset = p.tools.msc
943+
local flags = table.concat(cpp.getCFlags(cfg, toolset), " ")
944+
945+
test.istrue(flags:find("/D_MBCS") ~= nil)
946+
test.isfalse(flags:find("/D_UNICODE") ~= nil)
947+
test.isfalse(flags:find("/DUNICODE") ~= nil)
948+
end

modules/ninja/tests/test_ninja_implib.lua

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ function suite.ldflags_includesIMPLIB_onWindowsMSVCSharedLib()
4747
cpp.configurationVariables(cfg)
4848

4949
test.capture [[
50-
cflags_MyProject_Debug = /MD
51-
cxxflags_MyProject_Debug = /MD /EHsc
50+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE
51+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE
5252
ldflags_MyProject_Debug = /NOLOGO /DLL /IMPLIB:bin/Debug/MyProject.lib
5353
objdir_MyProject_Debug = obj/Debug
5454
targetdir_MyProject_Debug = bin/Debug
@@ -72,8 +72,8 @@ function suite.ldflags_noIMPLIB_onStaticLib()
7272
cpp.configurationVariables(cfg)
7373

7474
test.capture [[
75-
cflags_MyProject_Debug = /MD
76-
cxxflags_MyProject_Debug = /MD /EHsc
75+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE
76+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE
7777
ldflags_MyProject_Debug = /NOLOGO
7878
objdir_MyProject_Debug = obj/Debug
7979
targetdir_MyProject_Debug = bin/Debug
@@ -97,8 +97,8 @@ function suite.ldflags_noIMPLIB_onConsoleApp()
9797
cpp.configurationVariables(cfg)
9898

9999
test.capture [[
100-
cflags_MyProject_Debug = /MD
101-
cxxflags_MyProject_Debug = /MD /EHsc
100+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE
101+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE
102102
ldflags_MyProject_Debug = /NOLOGO
103103
objdir_MyProject_Debug = obj/Debug
104104
targetdir_MyProject_Debug = bin/Debug
@@ -148,8 +148,8 @@ function suite.ldflags_IMPLIB_respectsTargetDir()
148148
cpp.configurationVariables(cfg)
149149

150150
test.capture [[
151-
cflags_MyProject_Debug = /MD
152-
cxxflags_MyProject_Debug = /MD /EHsc
151+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE
152+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE
153153
ldflags_MyProject_Debug = /NOLOGO /DLL /IMPLIB:custom/output/MyProject.lib
154154
objdir_MyProject_Debug = obj/Debug
155155
targetdir_MyProject_Debug = custom/output
@@ -174,8 +174,8 @@ function suite.ldflags_IMPLIB_respectsTargetName()
174174
cpp.configurationVariables(cfg)
175175

176176
test.capture [[
177-
cflags_MyProject_Debug = /MD
178-
cxxflags_MyProject_Debug = /MD /EHsc
177+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE
178+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE
179179
ldflags_MyProject_Debug = /NOLOGO /DLL /IMPLIB:bin/Debug/CustomName.lib
180180
objdir_MyProject_Debug = obj/Debug
181181
targetdir_MyProject_Debug = bin/Debug
@@ -262,8 +262,8 @@ function suite.ldflags_noIMPLIB_whenNoImportLibFlag()
262262
cpp.configurationVariables(cfg)
263263

264264
test.capture [[
265-
cflags_MyProject_Debug = /MD
266-
cxxflags_MyProject_Debug = /MD /EHsc
265+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE
266+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE
267267
ldflags_MyProject_Debug = /NOLOGO /DLL
268268
objdir_MyProject_Debug = obj/Debug
269269
targetdir_MyProject_Debug = bin/Debug
@@ -312,8 +312,8 @@ function suite.ldflags_IMPLIB_usesLinkTarget()
312312
cpp.configurationVariables(cfg)
313313

314314
test.capture [[
315-
cflags_MyProject_Debug = /MD
316-
cxxflags_MyProject_Debug = /MD /EHsc
315+
cflags_MyProject_Debug = /MD /D_UNICODE /DUNICODE
316+
cxxflags_MyProject_Debug = /MD /EHsc /D_UNICODE /DUNICODE
317317
ldflags_MyProject_Debug = /NOLOGO /DLL /IMPLIB:lib/implibs/MyDLL.lib
318318
objdir_MyProject_Debug = obj/Debug
319319
targetdir_MyProject_Debug = bin/output

modules/ninja/tests/test_ninja_perfile_config.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ build obj/Debug/special.o: cxx special.cpp
8282
build obj/Debug/main.obj: cxx main.cpp
8383
cxxflags = $cxxflags_MyProject_Debug
8484
build obj/Debug/special.obj: cxx special.cpp
85-
cxxflags = /MD /EHsc /DMAIN_DEFINE /DSPECIAL_DEFINE
85+
cxxflags = /MD /EHsc /D_UNICODE /DUNICODE /DMAIN_DEFINE /DSPECIAL_DEFINE
8686
]]
8787
end
8888

@@ -260,7 +260,7 @@ build obj/Debug/special.o: cxx special.cpp
260260
build obj/Debug/main.obj: cxx main.cpp
261261
cxxflags = $cxxflags_MyProject_Debug
262262
build obj/Debug/special.obj: cxx special.cpp
263-
cxxflags = /MD /EHsc /DMAIN_DEFINE /DSPECIAL_DEFINE /Iinclude /Ispecial/include /O2
263+
cxxflags = /MD /EHsc /D_UNICODE /DUNICODE /DMAIN_DEFINE /DSPECIAL_DEFINE /Iinclude /Ispecial/include /O2
264264
]]
265265
end
266266

src/tools/msc.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,17 @@
260260
end
261261

262262
for _, define in ipairs(defines) do
263-
table.insert(result, '/D' .. p.esc(define))
263+
-- Check if the define contains special characters that need quoting
264+
-- MSVC requires quotes around defines with spaces, parentheses, or quotes
265+
if define:find('[%s%(%)"]') then
266+
-- Quote the entire define value
267+
-- For embedded quotes, we need to escape them with backslashes for MSVC
268+
local escaped_define = define:gsub('"', '\\"')
269+
table.insert(result, '/D"' .. escaped_define .. '"')
270+
else
271+
-- No special characters, use as-is
272+
table.insert(result, '/D' .. define)
273+
end
264274
end
265275

266276
if cfg and cfg.exceptionhandling == p.OFF then

0 commit comments

Comments
 (0)