diff --git a/xmake/rules/c++/modules/modules_support/clang/compiler_support.lua b/xmake/rules/c++/modules/modules_support/clang/compiler_support.lua index 6e4b3664596..6cd5270dcaa 100644 --- a/xmake/rules/c++/modules/modules_support/clang/compiler_support.lua +++ b/xmake/rules/c++/modules/modules_support/clang/compiler_support.lua @@ -372,6 +372,20 @@ function has_clangscandepssupport(target) return support_clangscandeps or nil end +function get_keepsystemincludesflag(target) + local keepsystemincludesflag = _g.keepsystemincludesflag + if keepsystemincludesflag == nil then + local compinst = target:compiler("cxx") + local clang_version = get_clang_version(target) + if compinst:has_flags("-fkeep-system-includes", "cxxflags", {flagskey = "clang_keep_system_includes", tryrun = true}) and + semver.compare(clang_version, "18.0") >= 0 then + keepsystemincludesflag = "-fkeep-system-includes" + end + _g.keepsystemincludesflag = keepsystemincludesflag or false + end + return keepsystemincludesflag or nil +end + function get_moduleoutputflag(target) local moduleoutputflag = _g.moduleoutputflag if moduleoutputflag == nil then diff --git a/xmake/rules/c++/modules/modules_support/clang/dependency_scanner.lua b/xmake/rules/c++/modules/modules_support/clang/dependency_scanner.lua index 71ccabb7243..6f78b3a4175 100644 --- a/xmake/rules/c++/modules/modules_support/clang/dependency_scanner.lua +++ b/xmake/rules/c++/modules/modules_support/clang/dependency_scanner.lua @@ -57,6 +57,7 @@ function generate_dependencies(target, sourcebatch, opt) io.writefile(jsonfile, outdata) else fallback_generate_dependencies(target, jsonfile, sourcefile, function(file) + local keepsystemincludesflag = compiler_support.get_keepsystemincludesflag(target) local compflags = compinst:compflags({sourcefile = file, target = target}) -- exclude -fmodule* and -std=c++/gnu++* flags because, -- when they are set clang try to find bmi of imported modules but they don't exists a this point of compilation @@ -64,7 +65,7 @@ function generate_dependencies(target, sourcebatch, opt) return flag:startswith("-fmodule") or flag:startswith("-std=c++") or flag:startswith("-std=gnu++") end) local ifile = path.translate(path.join(outputdir, path.filename(file) .. ".i")) - local flags = table.join(compflags or {}, {"-E", runtime_flag, "-fkeep-system-includes", "-x", "c++", file, "-o", ifile}) + local flags = table.join(compflags or {}, keepsystemincludesflag or {}, {"-E", "-x", "c++", file, "-o", ifile}) os.vrunv(compinst:program(), flags) local content = io.readfile(ifile) os.rm(ifile)