summaryrefslogtreecommitdiff
path: root/xmake/rules/c++/modules
diff options
context:
space:
mode:
authorruki <[email protected]>2022-08-04 22:53:32 +0800
committerruki <[email protected]>2022-08-04 22:53:32 +0800
commit312dccfb5ffae68ba471d9ebca5569589bb46988 (patch)
tree641681c5674cf3a8d60944110c3552ce39afd9cc /xmake/rules/c++/modules
parentfd44ddb961977eeed2b15637562da80174f1a5ee (diff)
improve to get toolchain includedirs
Diffstat (limited to 'xmake/rules/c++/modules')
-rw-r--r--xmake/rules/c++/modules/modules_support/clang.lua32
-rw-r--r--xmake/rules/c++/modules/modules_support/common.lua21
-rw-r--r--xmake/rules/c++/modules/modules_support/gcc.lua33
-rw-r--r--xmake/rules/c++/modules/modules_support/msvc.lua2
4 files changed, 67 insertions, 21 deletions
diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua
index 44150ab21..865b20a75 100644
--- a/xmake/rules/c++/modules/modules_support/clang.lua
+++ b/xmake/rules/c++/modules/modules_support/clang.lua
@@ -54,20 +54,46 @@ function load(target)
end
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 <vector>' | clang -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, clang)
+ local tmpfile = os.tmpfile() .. ".cc"
+ io.writefile(tmpfile, "#include <vector>")
+ local result = try {function () return os.iorunv(clang, {"-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 clang, toolname = target:tool("cc")
assert(toolname == "clang")
+ _get_toolchain_includedirs_for_stlheaders(includedirs, clang)
local _, result = try {function () return os.iorunv(clang, {"-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
diff --git a/xmake/rules/c++/modules/modules_support/common.lua b/xmake/rules/c++/modules/modules_support/common.lua
index 95ddeb65f..7f6f7aa43 100644
--- a/xmake/rules/c++/modules/modules_support/common.lua
+++ b/xmake/rules/c++/modules/modules_support/common.lua
@@ -119,7 +119,7 @@ function load_moduleinfos(target, sourcebatch, opt)
local moduleinfo = json.decode(data.moduleinfo)
moduleinfo.sourcefile = sourcefile
if moduleinfo then
- table.append(moduleinfos, moduleinfo)
+ table.insert(moduleinfos, moduleinfo)
end
end
end
@@ -233,7 +233,7 @@ function sort_modules_by_dependencies(objectfiles, modules)
for _, objectfile in ipairs(objectfiles) do
local m = modules[objectfile]
if m and m.provides then
- table.append(nodes, {marked = false, tempmarked = false, objectfile = objectfile})
+ table.insert(nodes, {marked = false, tempmarked = false, objectfile = objectfile})
end
end
@@ -251,12 +251,10 @@ function find_quote_header_file(target, sourcefile, file)
end
function find_angle_header_file(target, file)
- -- check if the header is in subtarget
- local headerpaths = modules_support(target).toolchain_include_directories(target)
+ local headerpaths = modules_support(target).toolchain_includedirs(target)
for _, dep in ipairs(target:orderdeps()) do
- table.append(headerpaths, dep:scriptdir())
+ table.insert(headerpaths, dep:scriptdir())
end
-
for _, pkg in ipairs(target:pkgs()) do
local includedirs = pkg:get("sysincludedirs") or pkg:get("includedirs")
if includedirs then
@@ -264,11 +262,8 @@ function find_angle_header_file(target, file)
end
end
table.join2(headerpaths, target:get("includedirs"))
-
local p = find_file(file, headerpaths)
- assert(p)
- assert(os.isfile(p))
-
+ assert(p, "find <%s> not found!", file)
return p
end
@@ -320,18 +315,18 @@ function fallback_generate_dependencies(target, jsonfile, sourcefile)
end
if module_name then
- table.append(rule.outputs, module_name .. bmi_extension(target))
+ table.insert(rule.outputs, module_name .. bmi_extension(target))
local provide = {}
provide["logical-name"] = module_name
provide["source-path"] = path.absolute(sourcefile, project.directory())
rule.provides = {}
- table.append(rule.provides, provide)
+ table.insert(rule.provides, provide)
end
rule.requires = module_deps
- table.append(output.rules, rule)
+ table.insert(output.rules, rule)
local jsondata = json.encode(output)
io.writefile(jsonfile, jsondata)
end
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 <vector>' | 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 <vector>")
+ 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)
diff --git a/xmake/rules/c++/modules/modules_support/msvc.lua b/xmake/rules/c++/modules/modules_support/msvc.lua
index 37428de7f..63e445df9 100644
--- a/xmake/rules/c++/modules/modules_support/msvc.lua
+++ b/xmake/rules/c++/modules/modules_support/msvc.lua
@@ -65,7 +65,7 @@ function load(target)
end
-- provide toolchain include dir for stl headerunit when p1689 is not supported
-function toolchain_include_directories(target)
+function toolchain_includedirs(target)
for _, toolchain_inst in ipairs(target:toolchains()) do
if toolchain_inst:name() == "msvc" then
local vcvars = toolchain_inst:config("vcvars")