From 757ad890028fddcdea24159634d83bbfc41073ef Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 16 Dec 2025 00:30:52 +0800 Subject: improve clang-cl depfiles #7070 --- xmake/modules/core/tools/clang_cl.lua | 38 ++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/xmake/modules/core/tools/clang_cl.lua b/xmake/modules/core/tools/clang_cl.lua index bafbc58a7..fb3333530 100644 --- a/xmake/modules/core/tools/clang_cl.lua +++ b/xmake/modules/core/tools/clang_cl.lua @@ -143,6 +143,20 @@ function nf_pcxxheader(self, pcheaderfile, opt) end end +-- has /clang:-MMD -MF depfile? +function _has_clang_mmd(self) + local has_clang_mmd = _g._HAS_CLANG_MMD + if has_clang_mmd == nil then + local depfile = os.tmpfile() + if self:has_flags({"/clang:-MMD", "/clang:-MF", "/clang:" .. depfile}, "cxflags", {flagskey = "clang_mmd"}) then + has_clang_mmd = true + end + os.tryrm(depfile) + _g._HAS_CLANG_MMD = has_clang_mmd or false + end + return has_clang_mmd +end + -- compile the source file function compile(self, sourcefile, objectfile, dependinfo, flags, opt) @@ -150,6 +164,8 @@ function compile(self, sourcefile, objectfile, dependinfo, flags, opt) os.mkdir(path.directory(objectfile)) -- compile it + local depfile + local depfile_format local outdata = try { function () @@ -157,7 +173,14 @@ function compile(self, sourcefile, objectfile, dependinfo, flags, opt) -- generate includes file local compflags = flags if dependinfo then - compflags = table.join(flags, "-showIncludes") + if _has_clang_mmd(self) then + depfile = os.tmpfile() + compflags = table.join(flags, "/clang:-MMD", "/clang:-MF", "/clang:" .. depfile) + depfile_format = "gcc" + else + compflags = table.join(flags, "-showIncludes") + depfile_format = "cl" + end end -- has color diagnostics? enable it @@ -176,6 +199,9 @@ function compile(self, sourcefile, objectfile, dependinfo, flags, opt) -- try removing the old object file for forcing to rebuild this source file os.tryrm(objectfile) + if depfile then + os.tryrm(depfile) + end -- parse and strip errors local lines = errors and tostring(errors):split('\n', {plain = true}) or {} @@ -217,7 +243,13 @@ function compile(self, sourcefile, objectfile, dependinfo, flags, opt) -- generate the dependent includes if dependinfo and outdata then - dependinfo.depfiles_format = "cl" - dependinfo.depfiles = outdata + if depfile and os.isfile(depfile) then + dependinfo.depfiles_format = depfile_format + dependinfo.depfiles = io.readfile(depfile) + os.tryrm(depfile) + elseif depfile_format == "cl" then + dependinfo.depfiles_format = "cl" + dependinfo.depfiles = outdata + end end end -- cgit v1.3.1 From bbea1373c5cfe37a6a6670be9ea9ba37f366e617 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 16 Dec 2025 00:34:40 +0800 Subject: improve depfiles --- xmake/modules/core/tools/clang_cl.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/xmake/modules/core/tools/clang_cl.lua b/xmake/modules/core/tools/clang_cl.lua index fb3333530..35c58edf3 100644 --- a/xmake/modules/core/tools/clang_cl.lua +++ b/xmake/modules/core/tools/clang_cl.lua @@ -243,10 +243,12 @@ function compile(self, sourcefile, objectfile, dependinfo, flags, opt) -- generate the dependent includes if dependinfo and outdata then - if depfile and os.isfile(depfile) then - dependinfo.depfiles_format = depfile_format - dependinfo.depfiles = io.readfile(depfile) - os.tryrm(depfile) + if depfile then + if os.isfile(depfile) then + dependinfo.depfiles_format = depfile_format + dependinfo.depfiles = io.readfile(depfile) + os.tryrm(depfile) + end elseif depfile_format == "cl" then dependinfo.depfiles_format = "cl" dependinfo.depfiles = outdata -- cgit v1.3.1 From ba89fa5f31026ef3cd95fb32257d4b26491682e3 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 16 Dec 2025 00:36:13 +0800 Subject: improve check clang /mmd --- xmake/modules/core/tools/clang_cl.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xmake/modules/core/tools/clang_cl.lua b/xmake/modules/core/tools/clang_cl.lua index 35c58edf3..53f98843b 100644 --- a/xmake/modules/core/tools/clang_cl.lua +++ b/xmake/modules/core/tools/clang_cl.lua @@ -151,8 +151,9 @@ function _has_clang_mmd(self) if self:has_flags({"/clang:-MMD", "/clang:-MF", "/clang:" .. depfile}, "cxflags", {flagskey = "clang_mmd"}) then has_clang_mmd = true end + has_clang_mmd = has_clang_mmd or false + _g._HAS_CLANG_MMD = has_clang_mmd os.tryrm(depfile) - _g._HAS_CLANG_MMD = has_clang_mmd or false end return has_clang_mmd end -- cgit v1.3.1