diff --git a/xmake/modules/core/tools/tcc.lua b/xmake/modules/core/tools/tcc.lua index 0fbaa202bb..98d81a998d 100644 --- a/xmake/modules/core/tools/tcc.lua +++ b/xmake/modules/core/tools/tcc.lua @@ -144,15 +144,27 @@ end -- compile the source file function compile(self, sourcefile, objectfile, dependinfo, flags, opt) - - -- ensure the object directory + opt = opt or {} os.mkdir(path.directory(objectfile)) - -- compile it + local depfile = dependinfo and os.tmpfile() or nil try { function () - local outdata, errdata = os.iorunv(compargv(self, sourcefile, objectfile, flags)) + + -- support `-MMD -MF depfile.d`? some old gcc does not support it at same time + if depfile and _g._HAS_MMD_MF == nil then + _g._HAS_MMD_MF = self:has_flags({"-MD", "-MF", os.nuldev()}, "cflags", { flagskey = "-MD -MF" }) or false + end + + -- generate includes file + local compflags = flags + if depfile and _g._HAS_MMD_MF then + compflags = table.join(compflags, "-MD", "-MF", depfile) + end + + -- do compile + local outdata, errdata = os.iorunv(compargv(self, sourcefile, objectfile, compflags)) return (outdata or "") .. (errdata or "") end, catch @@ -190,6 +202,16 @@ function compile(self, sourcefile, objectfile, dependinfo, flags, opt) if warnings and #warnings > 0 and policy.build_warnings(opt) then cprint("${color.warning}%s", table.concat(table.slice(warnings:split('\n'), 1, 8), '\n')) end + + -- generate the dependent includes + if depfile and os.isfile(depfile) then + if dependinfo then + dependinfo.depfiles_gcc = io.readfile(depfile, {continuation = "\\"}) + end + + -- remove the temporary dependent file + os.tryrm(depfile) + end end } }