From 312dccfb5ffae68ba471d9ebca5569589bb46988 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 4 Aug 2022 22:53:32 +0800 Subject: improve to get toolchain includedirs --- xmake/rules/c++/modules/modules_support/gcc.lua | 33 ++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'xmake/rules/c++/modules/modules_support/gcc.lua') diff --git a/xmake/rules/c++/modules/modules_support/gcc.lua b/xmake/rules/c++/modules/modules_support/gcc.lua index d3a307d1d..100f1e2e6 100644 --- a/xmake/rules/c++/modules/modules_support/gcc.lua +++ b/xmake/rules/c++/modules/modules_support/gcc.lua @@ -60,20 +60,46 @@ function load(target) target:add("cxxflags", modulemapperflag .. _get_module_mapper(), {force = true, expand = false}) end --- provide toolchain include dir for stl headerunit when p1689 is not supported -function toolchain_include_directories(target) +-- get includedirs for stl headers +-- +-- $ echo '#include ' | gcc -x c++ -E - | grep '/vector"' +-- # 1 "/usr/include/c++/11/vector" 1 3 +-- # 58 "/usr/include/c++/11/vector" 3 +-- # 59 "/usr/include/c++/11/vector" 3 +-- +function _get_toolchain_includedirs_for_stlheaders(includedirs, gcc) + local tmpfile = os.tmpfile() .. ".cc" + io.writefile(tmpfile, "#include ") + local result = try {function () return os.iorunv(gcc, {"-E", "-x", "c++", tmpfile}) end} + if result then + for _, line in ipairs(result:split("\n", {plain = true})) do + line = line:trim() + if line:startswith("#") and line:find("/vector\"", 1, true) then + local includedir = line:match("\"(.+)/vector\"") + if includedir and os.isdir(includedir) then + table.insert(includedirs, includedir) + break + end + end + end + end + os.tryrm(tmpfile) +end + +-- provide toolchain include directories for stl headerunit when p1689 is not supported +function toolchain_includedirs(target) local includedirs = _g.includedirs if includedirs == nil then includedirs = {} local gcc, toolname = target:tool("cc") assert(toolname == "gcc") + _get_toolchain_includedirs_for_stlheaders(includedirs, gcc) local _, result = try {function () return os.iorunv(gcc, {"-E", "-Wp,-v", "-xc", os.nuldev()}) end} if result then for _, line in ipairs(result:split("\n", {plain = true})) do line = line:trim() if os.isdir(line) then table.insert(includedirs, line) - break elseif line:startswith("End") then break end @@ -89,7 +115,6 @@ function generate_dependencies(target, sourcebatch, opt) local cachedir = common.modules_cachedir(target) local compinst = target:compiler("cxx") local common_args = {"-E", "-x", "c++"} - local trtbdflag = get_trtbdflag(target) local depfileflag = get_depfileflag(target) local depoutputflag = get_depoutputflag(target) -- cgit v1.3.1