diff options
| author | ruki <[email protected]> | 2024-05-29 20:51:28 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-05-29 20:51:28 +0800 |
| commit | 02ea6962aeabced2117938181db5e8f48185cb05 (patch) | |
| tree | cea7270e29a93477014b05d9b7283d1e296d6b32 | |
| parent | 5767107d1e7f66288e9b7eebd4272ba443cbffa0 (diff) | |
| parent | c43e8533e5738fce03a58557c63377f60ac263e4 (diff) | |
Merge pull request #5167 from xmake-io/cmake
add encoding flags when generate cmakelists
| -rw-r--r-- | xmake/modules/package/tools/cmake.lua | 2 | ||||
| -rw-r--r-- | xmake/plugins/project/cmake/cmakelists.lua | 41 |
2 files changed, 42 insertions, 1 deletions
diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index 9c54fddb5..681bbf209 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -824,7 +824,7 @@ function _get_envs_for_runtime_flags(package, configs, opt) local runtimes = package:runtimes() if runtimes then local fake_target = {is_shared = function(_) return false end, - sourcekinds = function(_) return "c" end} + sourcekinds = function(_) return "cc" end} envs[format("CMAKE_C_FLAGS_%s", buildtype)] = _map_compflags(fake_target, "c", "runtime", runtimes) fake_target.sourcekinds = function(_) return "cxx" end envs[format("CMAKE_CXX_FLAGS_%s", buildtype)] = _map_compflags(fake_target, "cxx", "runtime", runtimes) diff --git a/xmake/plugins/project/cmake/cmakelists.lua b/xmake/plugins/project/cmake/cmakelists.lua index 9670972c9..3d89c7cb6 100644 --- a/xmake/plugins/project/cmake/cmakelists.lua +++ b/xmake/plugins/project/cmake/cmakelists.lua @@ -233,6 +233,26 @@ function _translate_flags(flags, outputdir) return result end +-- map compiler flags +function _map_compflags(toolname, langkind, name, values) + local fake_target = { + is_shared = function() return false end, + tool = function() + local program + if toolname == "cl" then + program = "cl.exe" + elseif toolname == "gcc" then + program = "gcc" + end + return program, toolname + end, + sourcekinds = function() + return langkind == "c" and "cc" or "cxx" + end + } + return compiler.map_flags(langkind, name, values, {target = fake_target}) +end + -- get flags from fileconfig function _get_flags_from_fileconfig(fileconfig, outputdir, name) local flags = {} @@ -684,6 +704,24 @@ function _add_target_warnings(cmakelists, target) 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) + for _, flag in ipairs(flags_cl) 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) + for _, flag in ipairs(flags_gcc) do + cmakelists:print(" target_compile_options(%s PRIVATE %s)", target:name(), flag) + end + cmakelists:print("endif()") + end +end + -- add target exceptions function _add_target_exceptions(cmakelists, target) local flags_gcc = @@ -1122,6 +1160,9 @@ function _add_target(cmakelists, target, outputdir) _add_target_warnings(cmakelists, target) -- add target exceptions + _add_target_encodings(cmakelists, target) + + -- add target exceptions _add_target_exceptions(cmakelists, target) -- add target languages |
