From a6fb275feee99cecc5cd79f974fab786400fc88d Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Fri, 2 Feb 2024 22:03:04 +0100 Subject: fix libc++ support on windows --- xmake/modules/core/tools/clang.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/xmake/modules/core/tools/clang.lua b/xmake/modules/core/tools/clang.lua index 763a87f41..c7a2646c5 100644 --- a/xmake/modules/core/tools/clang.lua +++ b/xmake/modules/core/tools/clang.lua @@ -191,6 +191,15 @@ function _has_static_libstdcxx(self) return has_static_libstdcxx end +function _get_llvm_path() + local llvm_path = _g._LLVM_PATH + if llvm_path == nil then + local out, _ = os.iorun("clang -print-resource-dir") + llvm_path = path.normalize(path.join(out, "..", "..", "..")) + _g._LLVM_PATH = llvm_path + end + return llvm_path +end -- make the runtime flag -- @see https://github.com/xmake-io/xmake/issues/3546 @@ -198,6 +207,7 @@ function nf_runtime(self, runtime, opt) opt = opt or {} local maps local kind = self:kind() + local clang_path = _get_llvm_path() if self:is_plat("windows") and runtime then if not _has_ms_runtime_lib(self) then if runtime:startswith("MD") then @@ -228,6 +238,12 @@ function nf_runtime(self, runtime, opt) maps["c++_shared"] = "-stdlib=libc++" maps["stdc++_static"] = "-stdlib=libstdc++" maps["stdc++_shared"] = "-stdlib=libstdc++" + -- clang on windows fail to add libc++ includepath when using -stdlib=libc++ so we manually add it + -- @see https://github.com/llvm/llvm-project/issues/79647 + if is_plat("windows") then + maps["c++_static"] = table.join(maps["c++_static"], "-cxx-isystem" .. path.join(clang_path, "include", "c++", "v1")) + maps["c++_shared"] = table.join(maps["c++_shared"], "-cxx-isystem" .. path.join(clang_path, "include", "c++", "v1")) + end elseif kind == "ld" or kind == "sh" then local target = opt.target if target and target.sourcekinds and table.contains(table.wrap(target:sourcekinds()), "cxx") then @@ -235,6 +251,12 @@ function nf_runtime(self, runtime, opt) maps["c++_shared"] = "-stdlib=libc++" maps["stdc++_static"] = "-stdlib=libstdc++" maps["stdc++_shared"] = "-stdlib=libstdc++" + -- clang on windows fail to add libc++ librarypath when using -stdlib=libc++ so we manually add it + -- @see https://github.com/llvm/llvm-project/issues/79647 + if is_plat("windows") then + maps["c++_static"] = table.join(maps["c++_static"], "-L" .. path.join(clang_path, "lib")) + maps["c++_shared"] = table.join(maps["c++_shared"], "-L" .. path.join(clang_path, "lib")) + end if runtime:endswith("_static") and _has_static_libstdcxx(self) then maps["c++_static"] = table.join(maps["c++_static"], "-static-libstdc++") maps["stdc++_static"] = table.join(maps["stdc++_static"], "-static-libstdc++") -- cgit v1.3.1 From 3872b4d2b245a0283db617821348fda1504f1322 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Sat, 3 Feb 2024 16:46:50 +0100 Subject: apply PR suggestion --- xmake/modules/core/tools/clang.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/xmake/modules/core/tools/clang.lua b/xmake/modules/core/tools/clang.lua index c7a2646c5..7915af499 100644 --- a/xmake/modules/core/tools/clang.lua +++ b/xmake/modules/core/tools/clang.lua @@ -191,12 +191,14 @@ function _has_static_libstdcxx(self) return has_static_libstdcxx end -function _get_llvm_path() +function _get_llvm_path(self) local llvm_path = _g._LLVM_PATH if llvm_path == nil then - local out, _ = os.iorun("clang -print-resource-dir") - llvm_path = path.normalize(path.join(out, "..", "..", "..")) - _g._LLVM_PATH = llvm_path + local out, _ = try { function() return os.iorun(self:program() .. " -print-resource-dir") end } + if out then + llvm_path = path.normalize(path.join(out, "..", "..", "..")) + end + _g._LLVM_PATH = llvm_path or false end return llvm_path end @@ -207,7 +209,7 @@ function nf_runtime(self, runtime, opt) opt = opt or {} local maps local kind = self:kind() - local clang_path = _get_llvm_path() + local clang_path = _get_llvm_path(self) if self:is_plat("windows") and runtime then if not _has_ms_runtime_lib(self) then if runtime:startswith("MD") then @@ -240,7 +242,7 @@ function nf_runtime(self, runtime, opt) maps["stdc++_shared"] = "-stdlib=libstdc++" -- clang on windows fail to add libc++ includepath when using -stdlib=libc++ so we manually add it -- @see https://github.com/llvm/llvm-project/issues/79647 - if is_plat("windows") then + if self:is_plat("windows") then maps["c++_static"] = table.join(maps["c++_static"], "-cxx-isystem" .. path.join(clang_path, "include", "c++", "v1")) maps["c++_shared"] = table.join(maps["c++_shared"], "-cxx-isystem" .. path.join(clang_path, "include", "c++", "v1")) end @@ -253,7 +255,7 @@ function nf_runtime(self, runtime, opt) maps["stdc++_shared"] = "-stdlib=libstdc++" -- clang on windows fail to add libc++ librarypath when using -stdlib=libc++ so we manually add it -- @see https://github.com/llvm/llvm-project/issues/79647 - if is_plat("windows") then + if self:is_plat("windows") then maps["c++_static"] = table.join(maps["c++_static"], "-L" .. path.join(clang_path, "lib")) maps["c++_shared"] = table.join(maps["c++_shared"], "-L" .. path.join(clang_path, "lib")) end -- cgit v1.3.1 From d4514c86f937cda6d4ba19c661d7136ce4c3f5ee Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 4 Feb 2024 00:28:45 +0800 Subject: Update clang.lua --- xmake/modules/core/tools/clang.lua | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/xmake/modules/core/tools/clang.lua b/xmake/modules/core/tools/clang.lua index 7915af499..a62da204e 100644 --- a/xmake/modules/core/tools/clang.lua +++ b/xmake/modules/core/tools/clang.lua @@ -1,3 +1,4 @@ + --!A cross-platform build utility based on Lua -- -- Licensed under the Apache License, Version 2.0 (the "License"); @@ -191,16 +192,17 @@ function _has_static_libstdcxx(self) return has_static_libstdcxx end -function _get_llvm_path(self) - local llvm_path = _g._LLVM_PATH - if llvm_path == nil then - local out, _ = try { function() return os.iorun(self:program() .. " -print-resource-dir") end } - if out then - llvm_path = path.normalize(path.join(out, "..", "..", "..")) +-- get llvm sdk root directory +function _get_llvm_rootdir(self) + local llvm_rootdir = _g._LLVM_ROOTDIR + if llvm_rootdir == nil then + local outdata = try { function() return os.iorun(self:program() .. " -print-resource-dir") end } + if outdata then + llvm_rootdir = path.normalize(path.join(outdata:trim(), "..", "..", "..")) end - _g._LLVM_PATH = llvm_path or false + _g._LLVM_ROOTDIR = llvm_rootdir or false end - return llvm_path + return llvm_rootdir or nil end -- make the runtime flag @@ -209,7 +211,6 @@ function nf_runtime(self, runtime, opt) opt = opt or {} local maps local kind = self:kind() - local clang_path = _get_llvm_path(self) if self:is_plat("windows") and runtime then if not _has_ms_runtime_lib(self) then if runtime:startswith("MD") then @@ -243,8 +244,9 @@ function nf_runtime(self, runtime, opt) -- clang on windows fail to add libc++ includepath when using -stdlib=libc++ so we manually add it -- @see https://github.com/llvm/llvm-project/issues/79647 if self:is_plat("windows") then - maps["c++_static"] = table.join(maps["c++_static"], "-cxx-isystem" .. path.join(clang_path, "include", "c++", "v1")) - maps["c++_shared"] = table.join(maps["c++_shared"], "-cxx-isystem" .. path.join(clang_path, "include", "c++", "v1")) + local llvm_rootdir = _get_llvm_rootdir(self) + maps["c++_static"] = table.join(maps["c++_static"], "-cxx-isystem" .. path.join(llvm_rootdir, "include", "c++", "v1")) + maps["c++_shared"] = table.join(maps["c++_shared"], "-cxx-isystem" .. path.join(llvm_rootdir, "include", "c++", "v1")) end elseif kind == "ld" or kind == "sh" then local target = opt.target @@ -256,8 +258,9 @@ function nf_runtime(self, runtime, opt) -- clang on windows fail to add libc++ librarypath when using -stdlib=libc++ so we manually add it -- @see https://github.com/llvm/llvm-project/issues/79647 if self:is_plat("windows") then - maps["c++_static"] = table.join(maps["c++_static"], "-L" .. path.join(clang_path, "lib")) - maps["c++_shared"] = table.join(maps["c++_shared"], "-L" .. path.join(clang_path, "lib")) + local llvm_rootdir = _get_llvm_rootdir(self) + maps["c++_static"] = table.join(maps["c++_static"], "-L" .. path.join(llvm_rootdir, "lib")) + maps["c++_shared"] = table.join(maps["c++_shared"], "-L" .. path.join(llvm_rootdir, "lib")) end if runtime:endswith("_static") and _has_static_libstdcxx(self) then maps["c++_static"] = table.join(maps["c++_static"], "-static-libstdc++") @@ -268,4 +271,3 @@ function nf_runtime(self, runtime, opt) end return maps and maps[runtime] end - -- cgit v1.3.1 From 1838c52949a1befdc179a95e9a94357680f45e5c Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 4 Feb 2024 00:29:06 +0800 Subject: Update clang.lua --- xmake/modules/core/tools/clang.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/xmake/modules/core/tools/clang.lua b/xmake/modules/core/tools/clang.lua index a62da204e..f052e67db 100644 --- a/xmake/modules/core/tools/clang.lua +++ b/xmake/modules/core/tools/clang.lua @@ -1,4 +1,3 @@ - --!A cross-platform build utility based on Lua -- -- Licensed under the Apache License, Version 2.0 (the "License"); -- cgit v1.3.1 From 9f47d4543b03127c08923568a37e0f1b3f2ed44c Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 4 Feb 2024 00:32:04 +0800 Subject: Update clang.lua --- xmake/modules/core/tools/clang.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/xmake/modules/core/tools/clang.lua b/xmake/modules/core/tools/clang.lua index f052e67db..1a7f25d33 100644 --- a/xmake/modules/core/tools/clang.lua +++ b/xmake/modules/core/tools/clang.lua @@ -198,6 +198,9 @@ function _get_llvm_rootdir(self) local outdata = try { function() return os.iorun(self:program() .. " -print-resource-dir") end } if outdata then llvm_rootdir = path.normalize(path.join(outdata:trim(), "..", "..", "..")) + if not os.isdir(llvm_rootdir) then + llvm_rootdir = nil + end end _g._LLVM_ROOTDIR = llvm_rootdir or false end @@ -244,8 +247,10 @@ function nf_runtime(self, runtime, opt) -- @see https://github.com/llvm/llvm-project/issues/79647 if self:is_plat("windows") then local llvm_rootdir = _get_llvm_rootdir(self) - maps["c++_static"] = table.join(maps["c++_static"], "-cxx-isystem" .. path.join(llvm_rootdir, "include", "c++", "v1")) - maps["c++_shared"] = table.join(maps["c++_shared"], "-cxx-isystem" .. path.join(llvm_rootdir, "include", "c++", "v1")) + if llvm_rootdir then + maps["c++_static"] = table.join(maps["c++_static"], "-cxx-isystem" .. path.join(llvm_rootdir, "include", "c++", "v1")) + maps["c++_shared"] = table.join(maps["c++_shared"], "-cxx-isystem" .. path.join(llvm_rootdir, "include", "c++", "v1")) + end end elseif kind == "ld" or kind == "sh" then local target = opt.target @@ -258,8 +263,10 @@ function nf_runtime(self, runtime, opt) -- @see https://github.com/llvm/llvm-project/issues/79647 if self:is_plat("windows") then local llvm_rootdir = _get_llvm_rootdir(self) - maps["c++_static"] = table.join(maps["c++_static"], "-L" .. path.join(llvm_rootdir, "lib")) - maps["c++_shared"] = table.join(maps["c++_shared"], "-L" .. path.join(llvm_rootdir, "lib")) + if llvm_rootdir then + maps["c++_static"] = table.join(maps["c++_static"], "-L" .. path.join(llvm_rootdir, "lib")) + maps["c++_shared"] = table.join(maps["c++_shared"], "-L" .. path.join(llvm_rootdir, "lib")) + end end if runtime:endswith("_static") and _has_static_libstdcxx(self) then maps["c++_static"] = table.join(maps["c++_static"], "-static-libstdc++") -- cgit v1.3.1