summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-01-23 22:49:14 +0800
committerruki <[email protected]>2020-01-23 10:55:24 +0800
commitd989ec1a9903dde07d08bf58386a45dd53651f6c (patch)
tree25dc68e9e4641a53c812d7858a0141f5646cb160
parent17d5e2d5fd3f1d10c0598068cfd58412f848243e (diff)
support to compile *.c as c++
-rw-r--r--CHANGELOG.md8
-rw-r--r--xmake/core/project/rule.lua1
-rw-r--r--xmake/core/project/target.lua41
-rw-r--r--xmake/modules/core/tools/cl.lua9
-rw-r--r--xmake/modules/core/tools/gcc.lua9
-rw-r--r--xmake/rules/asm/xmake.lua2
-rw-r--r--xmake/rules/c++/xmake.lua4
-rw-r--r--xmake/rules/cuda/xmake.lua2
-rw-r--r--xmake/rules/dlang/xmake.lua2
-rw-r--r--xmake/rules/go/xmake.lua2
-rw-r--r--xmake/rules/objc++/xmake.lua4
-rw-r--r--xmake/rules/rust/xmake.lua2
-rw-r--r--xmake/rules/swift/xmake.lua2
13 files changed, 66 insertions, 22 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 773bac62d..899420f76 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
## master (unreleased)
+### New features
+
+* [#675](https://github.com/xmake-io/xmake/issues/675): Support to compile `*.c` as c++, `add_files("*.c", {sourcekind = "cxx"})`.
+
### Change
* [#665](https://github.com/xmake-io/xmake/issues/665): Support to parse *nix style command options, thanks [@OpportunityLiu](https://github.com/OpportunityLiu)
@@ -666,6 +670,10 @@
## master (开发中)
+### 新特性
+
+* [#675](https://github.com/xmake-io/xmake/issues/675): 支持通过设置强制将`*.c`作为c++代码编译, `add_files("*.c", {sourcekind = "cxx"})`。
+
### 改进
* [#665](https://github.com/xmake-io/xmake/issues/665): 支持 *nix style 的参数输入,感谢[@OpportunityLiu](https://github.com/OpportunityLiu)的贡献
diff --git a/xmake/core/project/rule.lua b/xmake/core/project/rule.lua
index 2d5e915a2..d2f630d08 100644
--- a/xmake/core/project/rule.lua
+++ b/xmake/core/project/rule.lua
@@ -154,6 +154,7 @@ function rule.apis()
{
-- rule.set_xxx
"rule.set_extensions"
+ , "rule.set_sourcekinds"
-- rule.add_xxx
, "rule.add_deps"
, "rule.add_imports"
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index a8735e07e..cdcb65f0c 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -876,24 +876,28 @@ function _instance:filerules(sourcefile)
end
end
- -- get target rule from the given source extension
- local extension2rules = self._EXTENSION2RULES
- if not extension2rules then
- extension2rules = {}
+ -- load all rules for this target with sourcekinds and extensions
+ local key2rules = self._KEY2RULES
+ if not key2rules then
+ key2rules = {}
for _, r in pairs(table.wrap(self:rules())) do
+ for _, sourcekind in ipairs(table.wrap(r:get("sourcekinds"))) do
+ key2rules[sourcekind] = key2rules[sourcekind] or {}
+ table.insert(key2rules[sourcekind], r)
+ end
for _, extension in ipairs(table.wrap(r:get("extensions"))) do
extension = extension:lower()
- extension2rules[extension] = extension2rules[extension] or {}
- table.insert(extension2rules[extension], r)
+ key2rules[extension] = key2rules[extension] or {}
+ table.insert(key2rules[extension], r)
end
end
- self._EXTENSION2RULES = extension2rules
+ self._KEY2RULES = key2rules
end
- for _, r in ipairs(table.wrap(extension2rules[path.extension(sourcefile):lower()])) do
+
+ -- get target rules from the given sourcekind or extension
+ for _, r in ipairs(table.wrap(key2rules[self:sourcekind_of(sourcefile)] or key2rules[path.extension(sourcefile):lower()])) do
table.insert(rules, r)
end
-
- -- done
return rules
end
@@ -1284,6 +1288,19 @@ function _instance:dependfiles()
return dependfiles
end
+-- get the sourcekind for the given source file
+function _instance:sourcekind_of(sourcefile)
+
+ -- get the sourcekind of this source file
+ local sourcekind = language.sourcekind_of(sourcefile)
+ local fileconfig = self:fileconfig(sourcefile)
+ if fileconfig and fileconfig.sourcekind then
+ -- we can override the sourcekind, e.g. add_files("*.c", {sourcekind = "cxx"})
+ sourcekind = fileconfig.sourcekind
+ end
+ return sourcekind
+end
+
-- get the kinds of sourcefiles
--
-- e.g. cc cxx mm mxx as ...
@@ -1300,7 +1317,7 @@ function _instance:sourcekinds()
for _, sourcefile in pairs(self:sourcefiles()) do
-- get source kind
- local sourcekind = language.sourcekind_of(sourcefile)
+ local sourcekind = self:sourcekind_of(sourcefile)
if sourcekind then
table.insert(sourcekinds, sourcekind)
end
@@ -1359,7 +1376,7 @@ function _instance:sourcebatches()
table.insert(sourcebatch.sourcefiles, sourcefile)
-- attempt to get source kind from the builtin languages
- local sourcekind = language.sourcekind_of(sourcefile)
+ local sourcekind = self:sourcekind_of(sourcefile)
if sourcekind then
-- save source kind
diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua
index 899258e38..5ce63f6c7 100644
--- a/xmake/modules/core/tools/cl.lua
+++ b/xmake/modules/core/tools/cl.lua
@@ -30,6 +30,15 @@ function init(self)
-- init cxflags
self:set("cxflags", "-nologo")
+ -- set language type explicitly
+ --
+ -- because compiler maybe compile `.c` as c++.
+ -- e.g.
+ -- add_files("*.c", {sourcekind = "cxx"})
+ --
+ self:add("cflags", "-TC")
+ self:add("cxxflags", "-TP")
+
-- init flags map
self:set("mapflags",
{
diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua
index 8fb5a4d81..6daeb2fd6 100644
--- a/xmake/modules/core/tools/gcc.lua
+++ b/xmake/modules/core/tools/gcc.lua
@@ -46,6 +46,15 @@ function init(self)
self:add("shared.cxflags", "-fPIC")
end
+ -- set language type explicitly
+ --
+ -- because compiler maybe compile `.c` as c++.
+ -- e.g.
+ -- add_files("*.c", {sourcekind = "cxx"})
+ --
+ self:add("cflags", "-x c")
+ self:add("cxxflags", "-x c++")
+
-- init flags map
self:set("mapflags",
{
diff --git a/xmake/rules/asm/xmake.lua b/xmake/rules/asm/xmake.lua
index 5cf9c5a63..0c80b2ee5 100644
--- a/xmake/rules/asm/xmake.lua
+++ b/xmake/rules/asm/xmake.lua
@@ -20,7 +20,7 @@
-- define rule: asm.build
rule("asm.build")
- set_extensions(".s", ".asm")
+ set_sourcekinds("as")
on_build_files("private.action.build.object")
-- define rule: asm
diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua
index bb39ee2de..9211db820 100644
--- a/xmake/rules/c++/xmake.lua
+++ b/xmake/rules/c++/xmake.lua
@@ -26,7 +26,7 @@ rule("c.build.pcheader")
-- define rule: c.build
rule("c.build")
- set_extensions(".c")
+ set_sourcekinds("cc")
add_deps("c.build.pcheader")
on_build_files("private.action.build.object")
@@ -38,7 +38,7 @@ rule("c++.build.pcheader")
-- define rule: c++.build
rule("c++.build")
- set_extensions(".cpp", ".cc", ".cxx")
+ set_sourcekinds("cxx")
add_deps("c++.build.pcheader", "c++.build.modules")
on_build_files("private.action.build.object")
diff --git a/xmake/rules/cuda/xmake.lua b/xmake/rules/cuda/xmake.lua
index 739f320ae..220299e55 100644
--- a/xmake/rules/cuda/xmake.lua
+++ b/xmake/rules/cuda/xmake.lua
@@ -20,7 +20,7 @@
-- define rule: cuda.build
rule("cuda.build")
- set_extensions(".cu")
+ set_sourcekinds("cu")
add_deps("cuda.build.devlink")
on_build_files(function (target, sourcebatch, opt)
import("private.action.build.object")(target, sourcebatch, opt)
diff --git a/xmake/rules/dlang/xmake.lua b/xmake/rules/dlang/xmake.lua
index 62ef87442..4e5a957f7 100644
--- a/xmake/rules/dlang/xmake.lua
+++ b/xmake/rules/dlang/xmake.lua
@@ -20,7 +20,7 @@
-- define rule: dlang.build
rule("dlang.build")
- set_extensions(".d")
+ set_sourcekinds("dc")
on_build_files("private.action.build.object")
-- define rule: dlang
diff --git a/xmake/rules/go/xmake.lua b/xmake/rules/go/xmake.lua
index eb9541b78..b7610b0f6 100644
--- a/xmake/rules/go/xmake.lua
+++ b/xmake/rules/go/xmake.lua
@@ -20,7 +20,7 @@
-- define rule: go.build
rule("go.build")
- set_extensions(".go")
+ set_sourcekinds("gc")
on_build_files("build.object")
-- define rule: cpp
diff --git a/xmake/rules/objc++/xmake.lua b/xmake/rules/objc++/xmake.lua
index 55306a934..5235aeff8 100644
--- a/xmake/rules/objc++/xmake.lua
+++ b/xmake/rules/objc++/xmake.lua
@@ -20,13 +20,13 @@
-- define rule: objc.build
rule("objc.build")
- set_extensions(".m")
+ set_sourcekinds("mm")
add_deps("c.build.pcheader")
on_build_files("private.action.build.object")
-- define rule: objc++.build
rule("objc++.build")
- set_extensions(".mm")
+ set_sourcekinds("mxx")
add_deps("c++.build.pcheader")
on_build_files("private.action.build.object")
diff --git a/xmake/rules/rust/xmake.lua b/xmake/rules/rust/xmake.lua
index b0daa2cbe..fc2e0c002 100644
--- a/xmake/rules/rust/xmake.lua
+++ b/xmake/rules/rust/xmake.lua
@@ -20,7 +20,7 @@
-- define rule: rust.build
rule("rust.build")
- set_extensions(".rs")
+ set_sourcekinds("rc")
on_build("build.target")
-- define rule: cpp
diff --git a/xmake/rules/swift/xmake.lua b/xmake/rules/swift/xmake.lua
index ca8ed798d..8207262a4 100644
--- a/xmake/rules/swift/xmake.lua
+++ b/xmake/rules/swift/xmake.lua
@@ -20,7 +20,7 @@
-- define rule: swift.build
rule("swift.build")
- set_extensions(".swift")
+ set_sourcekinds("sc")
on_build_files("private.action.build.object")
-- define rule: swift