Skip to content

Commit

Permalink
Handle toolset version.
Browse files Browse the repository at this point in the history
Add supports of validator functions to option's field `allowed`.
  • Loading branch information
Jarod42 committed Jul 29, 2023
1 parent 26725da commit c274f8d
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 35 deletions.
26 changes: 12 additions & 14 deletions modules/codelite/codelite_project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,33 @@
m.elements = {}

m.ctools = {
gcc = "gnu gcc",
clang = "clang",
msc = "Visual C++",
[p.tools.gcc] = "gnu gcc",
[p.tools.clang] = "clang",
[p.tools.msc] = "Visual C++",
}
m.cxxtools = {
gcc = "gnu g++",
clang = "clang++",
msc = "Visual C++",
[p.tools.gcc] = "gnu g++",
[p.tools.clang] = "clang++",
[p.tools.msc] = "Visual C++",
}

function m.getcompilername(cfg)
local tool = _OPTIONS.cc or cfg.toolset or p.CLANG

local toolset = p.tools[tool]
local toolset, version = p.tools.canonical(cfg.toolset)
if not toolset then
error("Invalid toolset '" + (_OPTIONS.cc or cfg.toolset) + "'")
error("Invalid toolset '" + cfg.toolset + "'")
end

if p.languages.isc(cfg.language) then
return m.ctools[tool]
return m.ctools[toolset]
elseif p.languages.iscpp(cfg.language) then
return m.cxxtools[tool]
return m.cxxtools[toolset]
end
end

function m.getcompiler(cfg)
local toolset = p.tools[_OPTIONS.cc or cfg.toolset or p.CLANG]
local toolset, version = p.tools.canonical(cfg.toolset)
if not toolset then
error("Invalid toolset '" + (_OPTIONS.cc or cfg.toolset) + "'")
error("Invalid toolset '" + cfg.toolset + "'")
end
return toolset
end
Expand Down
2 changes: 1 addition & 1 deletion modules/gmake/_preload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
trigger = "gmake",
shortname = "GNU Make",
description = "Generate GNU makefiles for POSIX, MinGW, and Cygwin",
toolset = "gcc",
toolset = iif(os.target() == p.MACOSX, "clang", "gcc"),

valid_kinds = { "ConsoleApp", "WindowedApp", "StaticLib", "SharedLib", "Utility", "Makefile", "None" },
valid_languages = { "C", "C++", "C#" },
Expand Down
2 changes: 1 addition & 1 deletion modules/gmake/gmake_cpp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
-- identify the toolset used by this configurations (would be nicer if
-- this were computed and stored with the configuration up front)

local toolset = p.tools[_OPTIONS.cc or cfg.toolset or "gcc"]
local toolset, version = p.tools.canonical(cfg.toolset or p.GCC)
if not toolset then
error("Invalid toolset '" .. cfg.toolset .. "'")
end
Expand Down
2 changes: 1 addition & 1 deletion modules/gmake/gmake_makefile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
-- identify the toolset used by this configurations (would be nicer if
-- this were computed and stored with the configuration up front)

local toolset = p.tools[cfg.toolset or "gcc"]
local toolset, version = p.tools.canonical(cfg.toolset or p.GCC)
if not toolset then
error("Invalid toolset '" .. cfg.toolset .. "'")
end
Expand Down
2 changes: 1 addition & 1 deletion modules/gmake/gmake_utility.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
-- identify the toolset used by this configurations (would be nicer if
-- this were computed and stored with the configuration up front)

local toolset = p.tools[cfg.toolset or "gcc"]
local toolset, version = p.tools.canonical(cfg.toolset or p.GCC)
if not toolset then
error("Invalid toolset '" .. cfg.toolset .. "'")
end
Expand Down
2 changes: 1 addition & 1 deletion modules/gmake2/_preload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
trigger = "gmake2",
shortname = "Alternative GNU Make",
description = "Generate GNU makefiles for POSIX, MinGW, and Cygwin",
toolset = "gcc",
toolset = iif(os.target() == p.MACOSX, "clang", "gcc"),

valid_kinds = { "ConsoleApp", "WindowedApp", "StaticLib", "SharedLib", "Utility", "Makefile", "None" },

Expand Down
2 changes: 1 addition & 1 deletion modules/gmake2/gmake2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@

function gmake2.getToolSet(cfg)
local default = iif(cfg.system == p.MACOSX, "clang", "gcc")
local toolset = p.tools[_OPTIONS.cc or cfg.toolset or default]
local toolset, version = p.tools.canonical(cfg.toolset or default)
if not toolset then
error("Invalid toolset '" .. cfg.toolset .. "'")
end
Expand Down
2 changes: 1 addition & 1 deletion modules/gmake2/gmake2_makefile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
-- identify the toolset used by this configurations (would be nicer if
-- this were computed and stored with the configuration up front)

local toolset = p.tools[cfg.toolset or "gcc"]
local toolset, version = p.tools.canonical(cfg.toolset or p.GCC)
if not toolset then
error("Invalid toolset '" .. cfg.toolset .. "'")
end
Expand Down
2 changes: 1 addition & 1 deletion modules/xcode/_preload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

-- Xcode always uses Mac OS X path and naming conventions

toolset = "clang",
toolset = "clang", -- TODO: os.target() == p.MACOSX and p.checkVersion(minOSVersion, "<10.7") -> gcc

-- The capabilities of this action

Expand Down
10 changes: 1 addition & 9 deletions modules/xcode/xcode_common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,8 @@
--

function xcode.getToolSet(cfg)
local default = "clang"
local minOSVersion = project.systemversion(cfg)
if minOSVersion ~= nil
and cfg.system == p.MACOSX
and p.checkVersion(minOSVersion, "<10.7")
then
default = "gcc"
end

local toolset = p.tools[_OPTIONS.cc or cfg.toolset or default]
local toolset, version = p.tools.canonical(cfg.toolset)
if not toolset then
error("Invalid toolset '" .. cfg.toolset .. "'")
end
Expand Down
4 changes: 4 additions & 0 deletions src/_premake_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,10 @@
{ "msc-v141", "Microsoft compiler (Visual Studio 2017)" },
{ "msc-v142", "Microsoft compiler (Visual Studio 2019)" },
{ "msc-v143", "Microsoft compiler (Visual Studio 2022)" },
function (name)
local toolset, version = p.tools.canonical(name)
return toolset
end
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/base/action.lua
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@
if not valid_tools then
return true
end
toolset = p.tools.normalize(toolset)
toolset = toolset:explode("-", true, 1)[1] -- get rid of version

return table.contains(valid_tools, toolset)
end
Expand Down
6 changes: 5 additions & 1 deletion src/base/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@
printf(" --%-" .. length .. "s %s", trigger, description)
if (option.allowed) then
local function compareValue(a, b)
if type(a) == "function" then return false end
if type(b) == "function" then return true end
return a[1] < b[1]
end
table.sort(option.allowed, compareValue)

for _, value in ipairs(option.allowed) do
printf(" %-" .. length-1 .. "s %s", value[1], value[2])
if type(value) ~= "function" then
printf(" %-" .. length-1 .. "s %s", value[1], value[2])
end
end
printf("")
end
Expand Down
7 changes: 6 additions & 1 deletion src/base/option.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@
if opt.allowed then
local found = false
for _, match in ipairs(opt.allowed) do
if match[1] == value then
if type(match) == "function" then
if match(value) then
found = true
break
end
elseif match[1] == value then
found = true
break
end
Expand Down
4 changes: 4 additions & 0 deletions src/tools/clang.lua
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,13 @@
}

function clang.gettoolname(cfg, tool)
local toolset, version = p.tools.canonical(cfg.toolset or p.CLANG)
local value = clang.tools[tool]
if type(value) == "function" then
value = value(cfg)
end
if toolset == p.tools.clang and version ~= nil then
value = value .. "-" .. version
end
return value
end
10 changes: 8 additions & 2 deletions src/tools/gcc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,14 @@
}

function gcc.gettoolname(cfg, tool)
if (cfg.gccprefix and gcc.tools[tool]) or tool == "rc" then
return (cfg.gccprefix or "") .. gcc.tools[tool]
local toolset, version = p.tools.canonical(cfg.toolset or p.GCC)
if toolset == p.tools.gcc and version ~= nil then
version = "-" .. version
else
version = ""
end
if ((cfg.gccprefix or version ~= "") and gcc.tools[tool]) or tool == "rc" then
return (cfg.gccprefix or "") .. gcc.tools[tool] .. version
end
return nil
end
24 changes: 24 additions & 0 deletions tests/base/test_option.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,27 @@

test.isnotnil(p.option.get("testopt2"))
end

-- Test allowed validators

function suite.OptionValidationText()
newoption {
trigger = "TestOptValidation",
description = "Testing",
allowed = {{"value1", "desc1"}}
}

test.istrue(p.option.validate( {testoptvalidation = "value1"}))
test.isfalse(p.option.validate( {testoptvalidation = "other"}))
end

function suite.OptionValidationFunc()
newoption {
trigger = "TestOptValidation",
description = "Testing",
allowed = {function(text) return text == "OK" end}
}

test.istrue(p.option.validate( {testoptvalidation = "OK"}))
test.isfalse(p.option.validate( {testoptvalidation = "KO"}))
end
21 changes: 21 additions & 0 deletions tests/tools/test_clang.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@
end


--
-- Check the selection of tools based on the target system.
--

function suite.tools_onDefaults()
prepare()
test.isequal("clang", clang.gettoolname(cfg, "cc"))
test.isequal("clang++", clang.gettoolname(cfg, "cxx"))
test.isequal("ar", clang.gettoolname(cfg, "ar"))
test.isequal("windres", clang.gettoolname(cfg, "rc"))
end

function suite.tools_forVersion()
toolset "clang-16"
prepare()
test.isequal("clang-16", clang.gettoolname(cfg, "cc"))
test.isequal("clang++-16", clang.gettoolname(cfg, "cxx"))
test.isequal("ar-16", clang.gettoolname(cfg, "ar"))
test.isequal("windres-16", clang.gettoolname(cfg, "rc"))
end

--
-- Check Mac OS X deployment target flags
--
Expand Down
8 changes: 8 additions & 0 deletions tests/tools/test_gcc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
test.isequal("test-prefix-windres", gcc.gettoolname(cfg, "rc"))
end

function suite.tools_forVersion()
toolset "gcc-16"
prepare()
test.isequal("gcc-16", gcc.gettoolname(cfg, "cc"))
test.isequal("g++-16", gcc.gettoolname(cfg, "cxx"))
test.isequal("ar-16", gcc.gettoolname(cfg, "ar"))
test.isequal("windres-16", gcc.gettoolname(cfg, "rc"))
end

--
-- By default, the -MMD -MP are used to generate dependencies.
Expand Down

0 comments on commit c274f8d

Please sign in to comment.