summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-07-22 09:47:57 +0800
committerGitHub <[email protected]>2024-07-22 09:47:57 +0800
commite6ba4e15203cf1825efa7fc1f457eff271386ac9 (patch)
treedc5e758ca4ea54ddc99e3702257b8b7cbf456f8e
parent4c1f120cacbbc3043e7b9cbf71a34d2186064958 (diff)
parent7db73f18851c801d8eee42d5191de89e1b3f13af (diff)
Merge pull request #5369 from Arthapz/add-culling-policy
add a policy to disable module culling
-rw-r--r--tests/projects/c++/modules/culling/src/hello.mpp10
-rw-r--r--tests/projects/c++/modules/culling/test.lua1
-rw-r--r--tests/projects/c++/modules/culling/xmake.lua7
-rw-r--r--tests/projects/c++/modules/culling2/src/hello.mpp10
-rw-r--r--tests/projects/c++/modules/culling2/test.lua1
-rw-r--r--tests/projects/c++/modules/culling2/xmake.lua6
-rw-r--r--xmake/core/project/policy.lua2
-rw-r--r--xmake/rules/c++/modules/modules_support/dependency_scanner.lua17
8 files changed, 49 insertions, 5 deletions
diff --git a/tests/projects/c++/modules/culling/src/hello.mpp b/tests/projects/c++/modules/culling/src/hello.mpp
new file mode 100644
index 000000000..124bd72bc
--- /dev/null
+++ b/tests/projects/c++/modules/culling/src/hello.mpp
@@ -0,0 +1,10 @@
+module;
+#include <cstdio>
+
+export module hello;
+
+export namespace hello {
+ void say(const char* str) {
+ printf("%s\n", str);
+ }
+}
diff --git a/tests/projects/c++/modules/culling/test.lua b/tests/projects/c++/modules/culling/test.lua
new file mode 100644
index 000000000..7717f8049
--- /dev/null
+++ b/tests/projects/c++/modules/culling/test.lua
@@ -0,0 +1 @@
+inherit(".test_base")
diff --git a/tests/projects/c++/modules/culling/xmake.lua b/tests/projects/c++/modules/culling/xmake.lua
new file mode 100644
index 000000000..33d3934b9
--- /dev/null
+++ b/tests/projects/c++/modules/culling/xmake.lua
@@ -0,0 +1,7 @@
+add_rules("mode.release", "mode.debug")
+set_languages("c++20")
+
+target("culling")
+ set_kind("static")
+ add_files("src/*.mpp")
+ set_policy("build.c++.modules.culling", false)
diff --git a/tests/projects/c++/modules/culling2/src/hello.mpp b/tests/projects/c++/modules/culling2/src/hello.mpp
new file mode 100644
index 000000000..124bd72bc
--- /dev/null
+++ b/tests/projects/c++/modules/culling2/src/hello.mpp
@@ -0,0 +1,10 @@
+module;
+#include <cstdio>
+
+export module hello;
+
+export namespace hello {
+ void say(const char* str) {
+ printf("%s\n", str);
+ }
+}
diff --git a/tests/projects/c++/modules/culling2/test.lua b/tests/projects/c++/modules/culling2/test.lua
new file mode 100644
index 000000000..7717f8049
--- /dev/null
+++ b/tests/projects/c++/modules/culling2/test.lua
@@ -0,0 +1 @@
+inherit(".test_base")
diff --git a/tests/projects/c++/modules/culling2/xmake.lua b/tests/projects/c++/modules/culling2/xmake.lua
new file mode 100644
index 000000000..9a4d682bd
--- /dev/null
+++ b/tests/projects/c++/modules/culling2/xmake.lua
@@ -0,0 +1,6 @@
+add_rules("mode.release", "mode.debug")
+set_languages("c++20")
+
+target("culling")
+ set_kind("static")
+ add_files("src/*.mpp", {cull = false})
diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua
index 4b5ef9dae..2eb2c1193 100644
--- a/xmake/core/project/policy.lua
+++ b/xmake/core/project/policy.lua
@@ -72,6 +72,8 @@ function policy.policies()
["build.c++.modules"] = {description = "Enable C++ modules for C++ building.", type = "boolean"},
-- Enable std module
["build.c++.modules.std"] = {description = "Enable std modules.", default = true, type = "boolean"},
+ -- Enable unreferenced and non-public named module culling
+ ["build.c++.modules.culling"] = {description = "Enable unrefereced and non-public named module culling.", default = true, type = "boolean"},
-- Try to reuse compiled module bmi file if targets flags permit it
["build.c++.modules.tryreuse"] = {description = "Try to reuse compiled module if possible.", default = true, type = "boolean"},
-- Enable module taking defines acbount for bmi reuse discrimination
diff --git a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua
index d59a4436a..66b00f421 100644
--- a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua
+++ b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua
@@ -422,11 +422,18 @@ function sort_modules_by_dependencies(target, objectfiles, modules)
local objectfiles_sorted_set = hashset.from(objectfiles_sorted)
for _, objectfile in ipairs(objectfiles) do
if not objectfiles_sorted_set:has(objectfile) then
- -- cull unreferenced non-public named module but add non-module files and implementation modules
- local _, provide, cppfile = compiler_support.get_provided_module(modules[objectfile])
- local fileconfig = target:fileconfig(cppfile)
- local public = fileconfig and fileconfig.public
- if not provide or public then
+ if target:policy("build.c++.modules.culling") then
+ -- cull unreferenced non-public named module but add non-module files and implementation modules
+ local _, provide, cppfile = compiler_support.get_provided_module(modules[objectfile])
+ local fileconfig = target:fileconfig(cppfile)
+ local public = fileconfig and fileconfig.public
+ local dont_cull = fileconfig and fileconfig.cull ~= nil and not fileconfig.cull
+ if not provide or public or dont_cull then
+ table.insert(result, objectfile)
+ else
+ wprint("%s has been culled because it's not consumed by its target nor flagged as a public module (add_files(\"xxx.cppm\", {public = true}))", cppfile)
+ end
+ else
table.insert(result, objectfile)
end
end