summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-04-12 00:50:21 +0800
committerruki <[email protected]>2025-04-12 00:50:21 +0800
commit2d350252b5413a1e3379d4ed88968a984b59c57d (patch)
tree43861d4000f905f9c932ba25a55ad01a0e5619eb
parent4d20b980c0d3826641a7b0481145a5e495417f6e (diff)
improve modules with pch
-rw-r--r--tests/projects/c++/modules/hello_with_pch/test.lua1
-rw-r--r--tests/projects/c++/modules/test_pch.lua31
-rw-r--r--xmake/modules/core/tools/gcc.lua32
-rw-r--r--xmake/rules/c++/modules/modules_support/compiler_support.lua5
4 files changed, 24 insertions, 45 deletions
diff --git a/tests/projects/c++/modules/hello_with_pch/test.lua b/tests/projects/c++/modules/hello_with_pch/test.lua
new file mode 100644
index 000000000..7717f8049
--- /dev/null
+++ b/tests/projects/c++/modules/hello_with_pch/test.lua
@@ -0,0 +1 @@
+inherit(".test_base")
diff --git a/tests/projects/c++/modules/test_pch.lua b/tests/projects/c++/modules/test_pch.lua
deleted file mode 100644
index 98286c229..000000000
--- a/tests/projects/c++/modules/test_pch.lua
+++ /dev/null
@@ -1,31 +0,0 @@
-import("lib.detect.find_tool")
-import("core.base.semver")
-import("detect.sdks.find_vstudio")
-import("utils.ci.is_running", {alias = "ci_is_running"})
-
-function _build()
- if ci_is_running() then
- os.run("xmake -rvD")
- else
- os.run("xmake -r")
- end
- local outdata = os.iorun("xmake")
- if outdata then
- if outdata:find("compiling") or outdata:find("linking") or outdata:find("generating") then
- raise("Modules incremental compilation does not work\n%s", outdata)
- end
- end
-end
-
-function main(t)
- -- TODO c++ modules with pch does not work for gcc now.
- if is_host("linux") then
- local clang = find_tool("clang", {version = true})
- if clang then
- os.exec("xmake f --toolchain=clang -c --yes --policies=build.c++.modules.std:n,build.c++.clang.fallbackscanner")
- _build()
- end
- else
- _build()
- end
-end
diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua
index 059eb5977..53e71f713 100644
--- a/xmake/modules/core/tools/gcc.lua
+++ b/xmake/modules/core/tools/gcc.lua
@@ -31,6 +31,7 @@ import("core.language.language")
import("utils.progress")
import("private.cache.build_cache")
import("private.service.distcc_build.client", {alias = "distcc_build_client"})
+import("rules.c++.modules.modules_support.compiler_support", {rootdir = os.programdir()})
function init(self)
@@ -864,24 +865,26 @@ function _compile(self, sourcefile, objectfile, compflags, opt)
end
end
--- make the compile arguments list for the precompiled header
-function _compargv_pch(self, pcheaderfile, pcoutputfile, flags, opt)
-
- -- remove "-include xxx.h" and "-include-pch xxx.pch"
- local pchflags = {}
+-- remove "-include xxx.h" and "-include-pch xxx.pch"
+function _remove_include_flags_for_pch(self, flags)
+ local result = {}
local include = false
for _, flag in ipairs(flags) do
if not flag:startswith("-include") then
if not include then
- table.insert(pchflags, flag)
+ table.insert(result, flag)
end
include = false
else
include = true
end
end
+ return result
+end
- -- set the language of precompiled header?
+-- make the compile arguments list for the precompiled header
+function _translate_flags_for_pch(self, flags)
+ local pchflags = _remove_include_flags_for_pch(self, flags)
if self:kind() == "cxx" then
table.insert(pchflags, "-x")
table.insert(pchflags, "c++-header")
@@ -895,19 +898,24 @@ function _compargv_pch(self, pcheaderfile, pcoutputfile, flags, opt)
table.insert(pchflags, "-x")
table.insert(pchflags, "objective-c-header")
end
+ return pchflags
+end
- -- make the compile arguments list
- local argv = table.join("-c", pchflags, "-o", pcoutputfile, pcheaderfile)
- return self:program(), argv
+-- remove the force includes for c++modules
+-- @see https://github.com/xmake-io/xmake/issues/4051#issuecomment-2795707800
+function _translate_flags_for_mpp(self, flags)
+ return _remove_include_flags_for_pch(self, flags)
end
-- make the compile arguments list
function compargv(self, sourcefile, objectfile, flags, opt)
- -- precompiled header?
+ -- is precompiled header or module files? remove the force includes.
local extension = path.extension(sourcefile)
if (extension:startswith(".h") or extension == ".inl") then
- return _compargv_pch(self, sourcefile, objectfile, flags, opt)
+ flags = _translate_flags_for_pch(self, flags, opt)
+ elseif compiler_support.has_module_extension(sourcefile, {extension = extension}) then
+ flags = _translate_flags_for_mpp(self, flags, opt)
end
local argv = table.join("-c", flags, "-o", objectfile, sourcefile)
diff --git a/xmake/rules/c++/modules/modules_support/compiler_support.lua b/xmake/rules/c++/modules/modules_support/compiler_support.lua
index eed30a320..554b536cf 100644
--- a/xmake/rules/c++/modules/modules_support/compiler_support.lua
+++ b/xmake/rules/c++/modules/modules_support/compiler_support.lua
@@ -82,13 +82,14 @@ function get_bmi_path(bmifile)
end
-- has module extension? e.g. *.mpp, ...
-function has_module_extension(sourcefile)
+function has_module_extension(sourcefile, opt)
+ opt = opt or {}
local modulexts = _g.modulexts
if modulexts == nil then
modulexts = hashset.of(".mpp", ".mxx", ".cppm", ".ixx")
_g.modulexts = modulexts
end
- local extension = path.extension(sourcefile)
+ local extension = opt.extension or path.extension(sourcefile)
return modulexts:has(extension:lower())
end