summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2023-07-28 09:49:43 +0800
committerGitHub <[email protected]>2023-07-28 09:49:43 +0800
commit05b62d2a23b5ad1cc8cf5df357f8c8cd0229e8c2 (patch)
treee92fabc5a9033545d894b49c6c0519755ab728c5
parente14f298f88acfa8417eaa1c26e43ac8f345fd763 (diff)
parent39f255dcdbae7e4d5c8428f1fe109ff42fd18561 (diff)
Merge pull request #4019 from xmake-io/encoding
Add set_encodings api to set source/target encoding
-rw-r--r--CHANGELOG.md2
-rw-r--r--xmake/core/project/target.lua1
-rw-r--r--xmake/languages/c++/xmake.lua1
-rw-r--r--xmake/languages/objc++/xmake.lua2
-rw-r--r--xmake/modules/core/tools/cl.lua39
-rw-r--r--xmake/modules/core/tools/gcc.lua35
-rw-r--r--xmake/modules/private/check/checker.lua1
-rw-r--r--xmake/modules/private/check/checkers/api/target/encodings.lua28
8 files changed, 109 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fee162eea..ac023d43d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@
* [#4002](https://github.com/xmake-io/xmake/issues/4002): Add soname and version support
* [#1613](https://github.com/xmake-io/xmake/issues/1613): Add avx512 and sse4.2 for add_vectorexts
+* [#2471](https://github.com/xmake-io/xmake/issues/2471): Add set_encodings to set source/target encodings
### Changes
@@ -1627,6 +1628,7 @@
* [#4002](https://github.com/xmake-io/xmake/issues/4002): 增加 soname 支持
* [#1613](https://github.com/xmake-io/xmake/issues/1613): 为 add_vectorexts 增加 avx512 和 sse4.2 支持
+* [#2471](https://github.com/xmake-io/xmake/issues/2471): 添加 set_encodings API 去设置源文件和目标文件的编码
### 改进
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index e0b2ebdd2..0a1c68a4a 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -2496,6 +2496,7 @@ function target.apis()
, "target.set_toolchains"
, "target.set_runargs"
, "target.set_exceptions"
+ , "target.set_encodings"
-- target.add_xxx
, "target.add_deps"
, "target.add_rules"
diff --git a/xmake/languages/c++/xmake.lua b/xmake/languages/c++/xmake.lua
index 4109ab50b..2df4ba6c9 100644
--- a/xmake/languages/c++/xmake.lua
+++ b/xmake/languages/c++/xmake.lua
@@ -48,6 +48,7 @@ language("c++")
, "target.frameworkdirs"
, "target.frameworks"
, "target.exceptions"
+ , "target.encodings"
, "target.pcheader"
, "target.pcxxheader"
, "toolchain.includedirs"
diff --git a/xmake/languages/objc++/xmake.lua b/xmake/languages/objc++/xmake.lua
index b40ab52e6..edea6c5bd 100644
--- a/xmake/languages/objc++/xmake.lua
+++ b/xmake/languages/objc++/xmake.lua
@@ -66,6 +66,8 @@ language("objc++")
, "target.undefines"
, "target.frameworkdirs"
, "target.frameworks"
+ , "target.exceptions"
+ , "target.encodings"
, "target.pmheader"
, "target.pmxxheader"
, "toolchain.includedirs"
diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua
index aa8c9aba1..b043370d1 100644
--- a/xmake/modules/core/tools/cl.lua
+++ b/xmake/modules/core/tools/cl.lua
@@ -315,6 +315,45 @@ function nf_exception(self, exp)
return maps[exp]
end
+-- make the encoding flag
+-- @see https://github.com/xmake-io/xmake/issues/2471
+--
+-- e.g.
+-- set_encodings("utf-8")
+-- set_encodings("source:utf-8", "target:utf-8")
+function nf_encoding(self, 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 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
+
-- make the c precompiled header flag
function nf_pcheader(self, pcheaderfile, target)
if self:kind() == "cc" then
diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua
index b45c04119..60bfc8e5e 100644
--- a/xmake/modules/core/tools/gcc.lua
+++ b/xmake/modules/core/tools/gcc.lua
@@ -337,6 +337,41 @@ function nf_exception(self, exp)
return exp:startswith("no-") and "-fno-exceptions" or "-fexceptions"
end
+-- make the encoding flag
+-- @see https://github.com/xmake-io/xmake/issues/2471
+--
+-- e.g.
+-- set_encodings("utf-8")
+-- set_encodings("source:utf-8", "target:utf-8")
+function nf_encoding(self, 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
+
-- make the c precompiled header flag
function nf_pcheader(self, pcheaderfile, target)
if self:kind() == "cc" then
diff --git a/xmake/modules/private/check/checker.lua b/xmake/modules/private/check/checker.lua
index eb48a2029..055e9ec00 100644
--- a/xmake/modules/private/check/checker.lua
+++ b/xmake/modules/private/check/checker.lua
@@ -37,6 +37,7 @@ function checkers()
["api.target.languages"] = {description = "Check languages configuration in target.", build = true},
["api.target.vectorexts"] = {description = "Check vectorexts configuration in target.", build = true},
["api.target.exceptions"] = {description = "Check exceptions configuration in target.", build = true},
+ ["api.target.encodings"] = {description = "Check encodings configuration in target.", build = true},
["api.target.packages"] = {description = "Check packages configuration in target."},
["api.target.files"] = {description = "Check files configuration in target."},
["api.target.headerfiles"] = {description = "Check header files configuration in target."},
diff --git a/xmake/modules/private/check/checkers/api/target/encodings.lua b/xmake/modules/private/check/checkers/api/target/encodings.lua
new file mode 100644
index 000000000..a4ec908f3
--- /dev/null
+++ b/xmake/modules/private/check/checkers/api/target/encodings.lua
@@ -0,0 +1,28 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file encodings.lua
+--
+
+-- imports
+import(".api_checker")
+
+function main(opt)
+ opt = opt or {}
+ api_checker.check_targets("encodings", table.join(opt, {values = {
+ "none", "utf-8", "source:utf-8", "target:utf-8"}}))
+end