diff options
| author | ruki <[email protected]> | 2024-05-30 11:08:16 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-05-30 11:08:16 +0800 |
| commit | e13085ea9f4e9f45cb1a96555921ffce0076b4a3 (patch) | |
| tree | 2f07c54cc1c078ab00cfde1a47c978e2ab6b4df8 | |
| parent | 02ea6962aeabced2117938181db5e8f48185cb05 (diff) | |
| parent | 6e957e6a5ac524d86d2b300b8c4249c07608c7ce (diff) | |
Merge pull request #5169 from xmake-io/mapflags
Improve mapflags
| -rw-r--r-- | xmake/core/tool/builder.lua | 28 | ||||
| -rw-r--r-- | xmake/plugins/project/cmake/cmakelists.lua | 107 |
2 files changed, 50 insertions, 85 deletions
diff --git a/xmake/core/tool/builder.lua b/xmake/core/tool/builder.lua index 4ef3e8576..92a17b68b 100644 --- a/xmake/core/tool/builder.lua +++ b/xmake/core/tool/builder.lua @@ -773,12 +773,32 @@ end function builder:map_flags(name, values, opt) local flags = {} local mapper = self:_tool()["nf_" .. name] + local multival = false + if name:endswith("s") then + multival = true + elseif not mapper then + mapper = self:_tool()["nf_" .. name .. "s"] + if mapper then + multival = true + end + end if mapper then opt = opt or {} - for _, value in ipairs(table.wrap(values)) do - local flag = mapper(self:_tool(), value, opt.target, opt.targetkind) - if flag and flag ~= "" and (not opt.check or self:has_flags(flag)) then - table.join2(flags, flag) + if multival then + local extra = self:_extraconf(opt.extras, values) + local results = mapper(self:_tool(), values, {target = opt.target, targetkind = opt.targetkind, extra = extra}) + for _, flag in ipairs(table.wrap(results)) do + if flag and flag ~= "" and (not opt.check or self:has_flags(flag)) then + table.insert(flags, flag) + end + end + else + for _, value in ipairs(table.wrap(values)) do + local extra = self:_extraconf(opt.extras, value) + local flag = mapper(self:_tool(), value, {target = opt.target, targetkind = opt.targetkind, extra = extra}) + if flag and flag ~= "" and (not opt.check or self:has_flags(flag)) then + table.join2(flags, flag) + end end end end diff --git a/xmake/plugins/project/cmake/cmakelists.lua b/xmake/plugins/project/cmake/cmakelists.lua index 3d89c7cb6..805212d53 100644 --- a/xmake/plugins/project/cmake/cmakelists.lua +++ b/xmake/plugins/project/cmake/cmakelists.lua @@ -243,6 +243,8 @@ function _map_compflags(toolname, langkind, name, values) program = "cl.exe" elseif toolname == "gcc" then program = "gcc" + elseif toolname == "clang" then + program = "clang" end return program, toolname end, @@ -661,60 +663,25 @@ function _add_target_compile_options(cmakelists, target, outputdir) end end --- add target warnings -function _add_target_warnings(cmakelists, target) - local flags_gcc = - { - none = "-w" - , less = "-Wall" - , more = "-Wall" - , all = "-Wall" - , allextra = "-Wall -Wextra" - , pedantic = "-Wpedantic" - , everything = "-Wall -Wextra" - , error = "-Werror" - } - local flags_msvc = - { - none = "-W0" - , less = "-W1" - , more = "-W3" - , all = "-W3" -- = "-Wall" will enable too more warnings - , allextra = "-W4" - , everything = "-Wall" - , error = "-WX" - } - local warnings = target:get("warnings") - if warnings then - cmakelists:print("if(MSVC)") - for _, warn in ipairs(warnings) do - local flag = flags_msvc[warn] - if flag then - cmakelists:print(" target_compile_options(%s PRIVATE %s)", target:name(), flag) - end - end - cmakelists:print("else()") - for _, warn in ipairs(warnings) do - local flag = flags_gcc[warn] - if flag then - cmakelists:print(" target_compile_options(%s PRIVATE %s)", target:name(), flag) - end +-- add target values +function _add_target_values(cmakelists, target, name) + local values = target:get(name) + if values then + if name:endswith("s") then + name = name:sub(1, #name - 1) end - cmakelists:print("endif()") - end -end - --- add target encodings -function _add_target_encodings(cmakelists, target) - local encodings = target:get("encodings") - if encodings then cmakelists:print("if(MSVC)") - local flags_cl = _map_compflags("cl", "c", "encoding", encodings) + local flags_cl = _map_compflags("cl", "c", name, values) for _, flag in ipairs(flags_cl) do cmakelists:print(" target_compile_options(%s PRIVATE %s)", target:name(), flag) end + cmakelists:print("elseif(Clang)") + local flags_clang = _map_compflags("clang", "c", name, values) + for _, flag in ipairs(flags_clang) do + cmakelists:print(" target_compile_options(%s PRIVATE %s)", target:name(), flag) + end cmakelists:print("elseif(Gcc)") - local flags_gcc = _map_compflags("gcc", "c", "encoding", encodings) + local flags_gcc = _map_compflags("gcc", "c", name, values) for _, flag in ipairs(flags_gcc) do cmakelists:print(" target_compile_options(%s PRIVATE %s)", target:name(), flag) end @@ -722,46 +689,24 @@ function _add_target_encodings(cmakelists, target) end end +-- add target warnings +function _add_target_warnings(cmakelists, target) + _add_target_values(cmakelists, target, "warnings") +end + +-- add target encodings +function _add_target_encodings(cmakelists, target) + _add_target_values(cmakelists, target, "encodings") +end + -- add target exceptions function _add_target_exceptions(cmakelists, target) - local flags_gcc = - { - cxx = "-fexceptions", - ["no-cxx"] = "-fno-exceptions", - objc = "-fobjc-exceptions", - ["no-objc"] = "-fno-objc-exceptions" - } - local flags_clang = - { - cxx = "-fcxx-exceptions", - ["no-cxx"] = "-fno-cxx-exceptions", - objc = "-fobjc-exceptions", - ["no-objc"] = "-fno-objc-exceptions" - } - local flags_msvc = - { - cxx = "/EHsc", - ["no-cxx"] = "/EHsc-" - } local exceptions = target:get("exceptions") if exceptions then if exceptions == "none" then cmakelists:print("string(REPLACE \"/EHsc\" \"\" CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS}\")") else - cmakelists:print("if(MSVC)") - -- msvc or clang-cl - for _, exception in ipairs(exceptions) do - cmakelists:print(" target_compile_options(%s PRIVATE %s)", target:name(), flags_msvc[exception]) - end - cmakelists:print("elseif(Clang)") - for _, exception in ipairs(exceptions) do - cmakelists:print(" target_compile_options(%s PRIVATE %s)", target:name(), flags_clang[exception]) - end - cmakelists:print("else()") - for _, exception in ipairs(exceptions) do - cmakelists:print(" target_compile_options(%s PRIVATE %s)", target:name(), flags_gcc[exception]) - end - cmakelists:print("endif()") + _add_target_values(cmakelists, target, "exceptions") end end end |
