Skip to content

Commit

Permalink
use -fkeep-system-includes only if supported by underlying clang
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthapz committed Jan 30, 2024
1 parent 0ff527a commit a9f3040
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions xmake/rules/c++/modules/modules_support/clang/compiler_support.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ 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
table.remove_if(compflags, function(_, flag)
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)
Expand Down

0 comments on commit a9f3040

Please sign in to comment.