Skip to content

Commit

Permalink
Merge pull request #2081 from Jarod42/msc_flags
Browse files Browse the repository at this point in the history
Add some missing flags for msc toolset
  • Loading branch information
nickclark2016 authored Aug 9, 2023
2 parents 056af27 + e525296 commit c135248
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/tools/msc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,20 @@
local msc = p.tools.msc
local project = p.project
local config = p.config
local string = require("string")

-- string comparison `toolset >= "msc-v142"` won't work with "msc-v80"
local function isVersionGreaterOrEqualTo(lhs, rhs)
if lhs == nil or rhs == nil then
return false
end
lhs = _G.tonumber(string.match(lhs, "^msc%-v([0-9]+)$"))
rhs = _G.tonumber(string.match(rhs, "^msc%-v([0-9]+)$"))
if lhs == nil or rhs == nil then
return false
end
return lhs >= rhs
end

--
-- Returns list of C preprocessor flags for a configuration.
Expand Down Expand Up @@ -44,6 +57,10 @@
Pure = "/clr:pure",
Safe = "/clr:safe",
},
compileas = {
["C"] = "/TC",
["C++"] = "/TP",
},
flags = {
FatalCompileWarnings = "/WX",
LinkTimeOptimization = "/GL",
Expand Down Expand Up @@ -142,11 +159,28 @@

}

function msc.getsharedflags(cfg)
local shared = config.mapFlags(cfg, msc.shared)

-- D9007: '/external:I' requires '/external:W'
if (#cfg.externalincludedirs > 0 or #cfg.includedirsafter > 0)
and cfg.externalwarnings == nil
and isVersionGreaterOrEqualTo(cfg.toolset, "msc-v142")
then
table.insert(shared, msc.shared.externalwarnings.Default)
end
return shared
end

msc.cflags = {
cdialect = {
["C11"] = "/std:c11",
["C17"] = "/std:c17"
}
}

function msc.getcflags(cfg)
local shared = config.mapFlags(cfg, msc.shared)
local shared = msc.getsharedflags(cfg)
local cflags = config.mapFlags(cfg, msc.cflags)
local flags = table.join(shared, cflags, msc.getwarnings(cfg))
return flags
Expand All @@ -158,6 +192,12 @@
--

msc.cxxflags = {
cppdialect = {
["C++14"] = "/std:c++14",
["C++17"] = "/std:c++17",
["C++20"] = "/std:c++20",
["C++latest"] = "/std:c++latest"
},
exceptionhandling = {
Default = "/EHsc",
On = "/EHsc",
Expand All @@ -173,7 +213,7 @@
}

function msc.getcxxflags(cfg)
local shared = config.mapFlags(cfg, msc.shared)
local shared = msc.getsharedflags(cfg)
local cxxflags = config.mapFlags(cfg, msc.cxxflags)
local flags = table.join(shared, cxxflags, msc.getwarnings(cfg))
return flags
Expand Down Expand Up @@ -264,7 +304,7 @@

for _, dir in ipairs(extdirs or {}) do
dir = project.getrelative(cfg.project, dir)
if cfg.toolset and cfg.toolset >= "msc-v142" then
if isVersionGreaterOrEqualTo(cfg.toolset, "msc-v142") then
table.insert(result, '/external:I' .. p.quoted(dir))
else
table.insert(result, '-I' .. p.quoted(dir))
Expand All @@ -273,7 +313,7 @@

for _, dir in ipairs(includedirsafter or {}) do
dir = project.getrelative(cfg.project, dir)
if cfg.toolset and cfg.toolset >= "msc-v142" then
if isVersionGreaterOrEqualTo(cfg.toolset, "msc-v142") then
table.insert(result, '/external:I' .. p.quoted(dir))
else
table.insert(result, '-I' .. p.quoted(dir))
Expand Down
75 changes: 75 additions & 0 deletions tests/tools/test_msc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,18 @@
test.contains("-I/usr/local/include", msc.getincludedirs(cfg, cfg.includedirs, cfg.externalincludedirs))
end

function suite.cflags_onVs2008ExternalIncludeDirs()
p.action.set("vs2008")
externalincludedirs { "/usr/local/include" }
prepare()
test.contains("-I/usr/local/include", msc.getincludedirs(cfg, cfg.includedirs, cfg.externalincludedirs))
end

function suite.cflags_onVs2022ExternalIncludeDirs()
p.action.set("vs2022")
externalincludedirs { "/usr/local/include" }
prepare()
test.contains("/external:W3", msc.getsharedflags(cfg))
test.contains("/external:I/usr/local/include", msc.getincludedirs(cfg, cfg.includedirs, cfg.externalincludedirs))
end

Expand All @@ -318,10 +326,18 @@ function suite.cflags_onIncludeDirsAfter()
test.contains("-I/usr/local/include", msc.getincludedirs(cfg, cfg.includedirs, cfg.externalincludedirs, cfg.frameworkdirs, cfg.includedirsafter))
end

function suite.cflags_onVs2008IncludeDirsAfter()
p.action.set("vs2008")
includedirsafter { "/usr/local/include" }
prepare()
test.contains("-I/usr/local/include", msc.getincludedirs(cfg, cfg.includedirs, cfg.externalincludedirs, cfg.frameworkdirs, cfg.includedirsafter))
end

function suite.cflags_onVs2022IncludeDirsAfter()
p.action.set("vs2022")
includedirsafter { "/usr/local/include" }
prepare()
test.contains("/external:W3", msc.getsharedflags(cfg))
test.contains("/external:I/usr/local/include", msc.getincludedirs(cfg, cfg.includedirs, cfg.externalincludedirs, cfg.frameworkdirs, cfg.includedirsafter))
end

Expand All @@ -347,6 +363,65 @@ end
test.contains('/FIinclude/sys.h', msc.getforceincludes(cfg))
end

--
-- Check handling of cdialect.
--

function suite.cdialectC11()
cdialect "C11"
prepare()
test.contains('/std:c11', msc.getcflags(cfg))
end

function suite.cdialectC17()
cdialect "C17"
prepare()
test.contains('/std:c17', msc.getcflags(cfg))
end

--
-- Check handling of cppdialect.
--

function suite.cppdialectCpp14()
cppdialect "C++14"
prepare()
test.contains('/std:c++14', msc.getcxxflags(cfg))
end

function suite.cppdialectCpp17()
cppdialect "C++17"
prepare()
test.contains('/std:c++17', msc.getcxxflags(cfg))
end

function suite.cppdialectCpp20()
cppdialect "C++20"
prepare()
test.contains('/std:c++20', msc.getcxxflags(cfg))
end

function suite.cppdialectCppLatest()
cppdialect "C++latest"
prepare()
test.contains('/std:c++latest', msc.getcxxflags(cfg))
end

--
-- Check handling of compileas.
--

function suite.compileasC()
compileas "C"
prepare()
test.contains('/TC', msc.getsharedflags(cfg))
end

function suite.compileasCPP()
compileas "C++"
prepare()
test.contains('/TP', msc.getsharedflags(cfg))
end

--
-- Check handling of floating point modifiers.
Expand Down

0 comments on commit c135248

Please sign in to comment.