diff options
| author | huarkiou <[email protected]> | 2024-05-27 11:16:45 +0800 |
|---|---|---|
| committer | huarkiou <[email protected]> | 2024-05-27 11:16:45 +0800 |
| commit | 0033f9ea8567815f93e7a3bbc8caf4b239b02286 (patch) | |
| tree | b8c5477b881b0e02800562f56f60b727afa40dbf | |
| parent | d58352a36f0ee20e302e09fb9b0400695ff665b8 (diff) | |
add encoding flags when generate cmakelists
| -rw-r--r-- | xmake/plugins/project/cmake/cmakelists.lua | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/xmake/plugins/project/cmake/cmakelists.lua b/xmake/plugins/project/cmake/cmakelists.lua index 9670972c9..8055d04a8 100644 --- a/xmake/plugins/project/cmake/cmakelists.lua +++ b/xmake/plugins/project/cmake/cmakelists.lua @@ -684,6 +684,90 @@ function _add_target_warnings(cmakelists, target) end end +-- copy from core.tools.cl +function nf_encoding_cl(encoding) + local kind + local charset + local splitinfo = encoding:split(":") + if #splitinfo > 1 then + kind = splitinfo[1] + charset = splitinfo[2] + else + charset = encoding + end + local charsets = { + ["utf-8"] = "utf-8", + utf8 = "utf-8", + gb2312 = "gb2312" + } + local flags = {} + charset = charsets[charset:lower()] + if charset then + if not kind and charset == "utf-8" then + table.insert(flags, "/utf-8") + else + if kind == "source" or not kind then + table.insert(flags, "-source-charset:" .. charset) + end + if kind == "target" or not kind then + table.insert(flags, "-execution-charset:" .. charset) + end + end + end + if #flags > 0 then + return flags + end +end + +-- copy from core.tools.gcc +function nf_encoding_gcc(encoding) + local kind + local charset + local splitinfo = encoding:split(":") + if #splitinfo > 1 then + kind = splitinfo[1] + charset = splitinfo[2] + else + charset = encoding + end + local charsets = { + ["utf-8"] = "UTF-8", + utf8 = "UTF-8" + } + local flags = {} + charset = charsets[charset:lower()] + if charset then + if kind == "source" or not kind then + table.insert(flags, "-finput-charset=" .. charset) + end + if kind == "target" or not kind then + table.insert(flags, "-fexec-charset=" .. charset) + end + end + if #flags > 0 then + return flags + end +end + +-- add target encodings +function _add_target_encodings(cmakelists, target) + local encodings = target:get("encodings") + if encodings then + cmakelists:print("if(MSVC)") + -- msvc or clang-cl + for _, encoding in ipairs(encodings) do + local flags_msvc = nf_encoding_cl(encoding) + cmakelists:print(" target_compile_options(%s PRIVATE %s)", target:name(), flags_msvc[1]) + end + cmakelists:print("else()") + for _, encoding in ipairs(encodings) do + local flags_gcc = nf_encoding_gcc(encoding) + cmakelists:print(" target_compile_options(%s PRIVATE %s)", target:name(), flags_gcc[1]) + end + cmakelists:print("endif()") + end +end + -- add target exceptions function _add_target_exceptions(cmakelists, target) local flags_gcc = @@ -1122,6 +1206,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 |
