summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-02 22:49:56 +0800
committerruki <[email protected]>2025-12-02 22:49:56 +0800
commit5c4b98ecca5a0c437a1bb5679e4046f752ed1a42 (patch)
tree1e877302612037e625928a2238f5711e05b952ea
parent92b61ab2648cc37da47874a4bab4d487a9a01202 (diff)
improve c++/objc++ rules
-rw-r--r--xmake/rules/c++/config/basic.lua38
-rw-r--r--xmake/rules/c++/config/main.lua (renamed from xmake/rules/c++/build_sanitizer/xmake.lua)36
-rw-r--r--xmake/rules/c++/config/optimization.lua (renamed from xmake/rules/c++/build_optimization/config.lua)13
-rw-r--r--xmake/rules/c++/config/sanitizer.lua (renamed from xmake/rules/c++/build_sanitizer/config.lua)7
-rw-r--r--xmake/rules/c++/openmp/load.lua1
-rw-r--r--xmake/rules/c++/openmp/xmake.lua5
-rw-r--r--xmake/rules/c++/xmake.lua24
-rw-r--r--xmake/rules/objc++/config/basic.lua42
-rw-r--r--xmake/rules/objc++/config/main.lua (renamed from xmake/rules/c++/build_optimization/xmake.lua)23
-rw-r--r--xmake/rules/objc++/xmake.lua26
10 files changed, 133 insertions, 82 deletions
diff --git a/xmake/rules/c++/config/basic.lua b/xmake/rules/c++/config/basic.lua
new file mode 100644
index 000000000..e9c810417
--- /dev/null
+++ b/xmake/rules/c++/config/basic.lua
@@ -0,0 +1,38 @@
+--!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, Xmake Open Source Community.
+--
+-- @author ruki
+-- @file basic.lua
+--
+
+-- main entry
+function main(target, sourcekind)
+ -- enable c++ exceptions by default on Windows
+ if sourcekind == "cxx" and target:is_plat("windows") and not target:get("exceptions") then
+ target:set("exceptions", "cxx")
+ end
+
+ -- https://github.com/xmake-io/xmake/issues/4621
+ -- tcc on Windows static library needs special handling
+ if target:is_plat("windows") and target:is_static() then
+ local toolname = sourcekind == "cxx" and "cxx" or "cc"
+ if target:has_tool(toolname, "tcc") then
+ target:set("extension", ".a")
+ target:set("prefixname", "lib")
+ end
+ end
+end
+
diff --git a/xmake/rules/c++/build_sanitizer/xmake.lua b/xmake/rules/c++/config/main.lua
index 89149f414..78d1362af 100644
--- a/xmake/rules/c++/build_sanitizer/xmake.lua
+++ b/xmake/rules/c++/config/main.lua
@@ -15,30 +15,24 @@
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
--- @file xmake.lua
+-- @file main.lua
--
--- define rule: c.build.sanitizer
-rule("c.build.sanitizer")
- on_config(function (target)
- import("config")(target, "cc")
- end)
+-- imports
+import("basic", {alias = "config_basic"})
+import("optimization", {alias = "config_optimization"})
+import("sanitizer", {alias = "config_sanitizer"})
--- define rule: c++.build.sanitizer
-rule("c++.build.sanitizer")
- on_config(function (target)
- import("config")(target, "cxx")
- end)
+-- main entry
+function main(target, sourcekind)
--- define rule: objc.build.sanitizer
-rule("objc.build.sanitizer")
- on_config(function (target)
- import("config")(target, "mm")
- end)
+ -- config basic configs
+ config_basic(target, sourcekind)
--- define rule: objc++.build.sanitizer
-rule("objc++.build.sanitizer")
- on_config(function (target)
- import("config")(target, "mxx")
- end)
+ -- config optimization configs
+ config_optimization(target, sourcekind)
+
+ -- config sanitizer configs
+ config_sanitizer(target, sourcekind)
+end
diff --git a/xmake/rules/c++/build_optimization/config.lua b/xmake/rules/c++/config/optimization.lua
index 686ef76c5..44b6ce755 100644
--- a/xmake/rules/c++/build_optimization/config.lua
+++ b/xmake/rules/c++/config/optimization.lua
@@ -15,7 +15,7 @@
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
--- @file config.lua
+-- @file optimization.lua
--
-- imports
@@ -23,7 +23,10 @@ import("core.tool.compiler")
import("core.project.project")
-- add lto optimization
-function _add_lto_optimization(target, sourcekind)
+function main(target, sourcekind)
+ if not (target:policy("build.optimization.lto") or project.policy("build.optimization.lto")) then
+ return
+ end
-- add cflags
local _, cc = target:tool(sourcekind)
@@ -87,9 +90,3 @@ function _add_lto_optimization(target, sourcekind)
end
end
-function main(target, sourcekind)
- if target:policy("build.optimization.lto") or
- project.policy("build.optimization.lto") then
- _add_lto_optimization(target, sourcekind)
- end
-end
diff --git a/xmake/rules/c++/build_sanitizer/config.lua b/xmake/rules/c++/config/sanitizer.lua
index be5ba7225..b6fe0df7e 100644
--- a/xmake/rules/c++/build_sanitizer/config.lua
+++ b/xmake/rules/c++/config/sanitizer.lua
@@ -15,18 +15,17 @@
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
--- @file config.lua
+-- @file sanitizer.lua
--
-- imports
-import("core.tool.compiler")
import("core.project.project")
import("lib.detect.find_tool")
import("core.base.semver")
+import("core.base.path")
-- add build sanitizer
function _add_build_sanitizer(target, sourcekind, checkmode)
-
-- add cflags
local _, cc = target:tool(sourcekind)
local flagnames = {
@@ -48,6 +47,7 @@ function _add_build_sanitizer(target, sourcekind, checkmode)
end
end
+-- main entry
function main(target, sourcekind)
local sanitizer = false
for _, checkmode in ipairs({"address", "thread", "memory", "leak", "undefined"}) do
@@ -62,7 +62,6 @@ function main(target, sourcekind)
end
if sanitizer then
-
-- enable the debug symbols for sanitizer
if not target:get("symbols") then
target:set("symbols", "debug")
diff --git a/xmake/rules/c++/openmp/load.lua b/xmake/rules/c++/openmp/load.lua
index a1ced6cac..7e4969ff0 100644
--- a/xmake/rules/c++/openmp/load.lua
+++ b/xmake/rules/c++/openmp/load.lua
@@ -39,3 +39,4 @@ function main(target, sourcekind)
target:add(flag_name, "-Qopenmp")
end
end
+
diff --git a/xmake/rules/c++/openmp/xmake.lua b/xmake/rules/c++/openmp/xmake.lua
index 052ad5370..e7a554c5d 100644
--- a/xmake/rules/c++/openmp/xmake.lua
+++ b/xmake/rules/c++/openmp/xmake.lua
@@ -20,12 +20,13 @@
-- define rule: c.openmp
rule("c.openmp")
- on_config(function (target)
+ on_load(function (target)
import("load")(target, "cc")
end)
-- define rule: c++.openmp
rule("c++.openmp")
- on_config(function (target)
+ on_load(function (target)
import("load")(target, "cxx")
end)
+
diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua
index e0673837d..23e5f27bc 100644
--- a/xmake/rules/c++/xmake.lua
+++ b/xmake/rules/c++/xmake.lua
@@ -20,31 +20,19 @@
rule("c.build")
set_sourcekinds("cc")
- add_deps("c.build.pcheader", "c.build.optimization", "c.build.sanitizer")
- on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true})
+ add_deps("c.build.pcheader")
on_config(function (target)
- -- https://github.com/xmake-io/xmake/issues/4621
- if target:is_plat("windows") and target:is_static() and target:has_tool("cc", "tcc") then
- target:set("extension", ".a")
- target:set("prefixname", "lib")
- end
+ import("rules.c++.config")(target, "cc")
end)
+ on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true})
rule("c++.build")
set_sourcekinds("cxx")
- add_deps("c++.build.pcheader", "c++.build.modules", "c++.build.optimization", "c++.build.sanitizer")
- on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true})
+ add_deps("c++.build.pcheader", "c++.build.modules")
on_config(function (target)
- -- enable c++ exceptions by default
- if target:is_plat("windows") and not target:get("exceptions") then
- target:set("exceptions", "cxx")
- end
- -- https://github.com/xmake-io/xmake/issues/4621
- if target:is_plat("windows") and target:is_static() and target:has_tool("cxx", "tcc") then
- target:set("extension", ".a")
- target:set("prefixname", "lib")
- end
+ import("rules.c++.config")(target, "cxx")
end)
+ on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true})
rule("c")
diff --git a/xmake/rules/objc++/config/basic.lua b/xmake/rules/objc++/config/basic.lua
new file mode 100644
index 000000000..cf1da1e12
--- /dev/null
+++ b/xmake/rules/objc++/config/basic.lua
@@ -0,0 +1,42 @@
+--!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, Xmake Open Source Community.
+--
+-- @author ruki
+-- @file basic.lua
+--
+
+-- main entry
+function main(target, sourcekind)
+ -- objc basic configs
+ if sourcekind == "mm" then
+ -- deprecated, we only need to use `add_mflags("-fno-objc-arc")` to override it
+ if target:values("objc.build.arc") == false then
+ target:add("mflags", "-fno-objc-arc")
+ end
+ if target:is_plat("macosx", "iphoneos", "watchos") then
+ target:add("frameworks", "Foundation", "CoreFoundation")
+ end
+ elseif sourcekind == "mxx" then
+ -- deprecated, we only need to use `add_mxxflags("-fno-objc-arc")` to override it
+ if target:values("objc++.build.arc") == false then
+ target:add("mxxflags", "-fno-objc-arc")
+ end
+ if target:is_plat("macosx", "iphoneos", "watchos") then
+ target:add("frameworks", "Foundation", "CoreFoundation")
+ end
+ end
+end
+
diff --git a/xmake/rules/c++/build_optimization/xmake.lua b/xmake/rules/objc++/config/main.lua
index 82693035e..45755edb8 100644
--- a/xmake/rules/c++/build_optimization/xmake.lua
+++ b/xmake/rules/objc++/config/main.lua
@@ -15,18 +15,19 @@
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
--- @file xmake.lua
+-- @file main.lua
--
--- define rule: c.build.optimization
-rule("c.build.optimization")
- on_config(function (target)
- import("config")(target, "cc")
- end)
+-- imports
+import("rules.objc++.config.basic", {rootdir = os.programdir(), alias = "config_basic"})
+import("rules.c++.config", {rootdir = os.programdir(), alias = "config_cxx"})
--- define rule: c++.build.optimization
-rule("c++.build.optimization")
- on_config(function (target)
- import("config")(target, "cxx")
- end)
+-- main entry
+function main(target, sourcekind)
+ -- handle objc++ basic configs
+ config_basic(target, sourcekind)
+
+ -- handle c++ configs (optimization, sanitizer, etc.)
+ config_cxx(target, sourcekind)
+end
diff --git a/xmake/rules/objc++/xmake.lua b/xmake/rules/objc++/xmake.lua
index fe857deef..2ff0ecb8c 100644
--- a/xmake/rules/objc++/xmake.lua
+++ b/xmake/rules/objc++/xmake.lua
@@ -21,30 +21,20 @@
-- define rule: objc.build
rule("objc.build")
set_sourcekinds("mm")
- add_deps("objc.build.pcheader", "c.build.optimization", "objc.build.sanitizer")
- after_load(function (target)
- -- deprecated, we only need to use `add_mflags("-fno-objc-arc")` to override it
- if target:values("objc.build.arc") == false then
- target:add("mflags", "-fno-objc-arc")
- end
- if target:is_plat("macosx", "iphoneos", "watchos") then
- target:add("frameworks", "Foundation", "CoreFoundation")
- end
+ add_deps("objc.build.pcheader")
+ on_config(function (target)
+ -- handle all configs
+ import("rules.objc++.config")(target, "mm")
end)
on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true})
-- define rule: objc++.build
rule("objc++.build")
set_sourcekinds("mxx")
- add_deps("objc++.build.pcheader", "c++.build.optimization", "objc++.build.sanitizer")
- after_load(function (target)
- -- deprecated, we only need to use `add_mxxflags("-fno-objc-arc")` to override it
- if target:values("objc++.build.arc") == false then
- target:add("mxxflags", "-fno-objc-arc")
- end
- if target:is_plat("macosx", "iphoneos", "watchos") then
- target:add("frameworks", "Foundation", "CoreFoundation")
- end
+ add_deps("objc++.build.pcheader")
+ on_config(function (target)
+ -- handle all configs
+ import("rules.objc++.config")(target, "mxx")
end)
on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true})