summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-03-23 00:29:44 +0800
committerGitHub <[email protected]>2024-03-23 00:29:44 +0800
commit7ada7b72ade732b361dd98b979354ad6bc2b4596 (patch)
treee20be8e33e80af31277dda55010d704eace939ed
parent6c78298c58fd710a28a42ea4ad77d34a41d5a655 (diff)
parenta4b0293cb473fb9da151ae1d55588316c69c2bb8 (diff)
Merge pull request #4866 from xmake-io/clangcl
Improve clang-cl toolchain support
-rw-r--r--xmake/core/package/package.lua3
-rw-r--r--xmake/modules/package/manager/system/find_package.lua20
-rw-r--r--xmake/modules/package/tools/autoconf.lua2
-rw-r--r--xmake/modules/package/tools/cmake.lua52
-rw-r--r--xmake/modules/package/tools/gn.lua2
-rw-r--r--xmake/modules/package/tools/meson.lua2
-rw-r--r--xmake/modules/package/tools/msbuild.lua2
-rw-r--r--xmake/modules/private/action/require/impl/actions/install.lua1
-rw-r--r--xmake/modules/private/utils/toolchain.lua2
9 files changed, 64 insertions, 22 deletions
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index c480d9b6c..d1a082890 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -1165,6 +1165,9 @@ function _instance:toolchain(name)
end
self:_memcache():set("toolchains_map", toolchains_map)
end
+ if not toolchains_map[name] then
+ toolchains_map[name] = toolchain.load(name, {plat = self:plat(), arch = self:arch()})
+ end
return toolchains_map[name]
end
diff --git a/xmake/modules/package/manager/system/find_package.lua b/xmake/modules/package/manager/system/find_package.lua
index 862862d5a..0d0db8a18 100644
--- a/xmake/modules/package/manager/system/find_package.lua
+++ b/xmake/modules/package/manager/system/find_package.lua
@@ -36,6 +36,17 @@ function _get_package_items()
return items
end
+-- check package toolchains
+function _check_package_toolchains(package)
+ local has_standalone
+ for _, toolchain_inst in pairs(package:toolchains()) do
+ if toolchain_inst:check() and toolchain_inst:is_standalone() then
+ has_standalone = true
+ end
+ end
+ return has_standalone
+end
+
-- find package from system and compiler
-- @see https://github.com/xmake-io/xmake/issues/4596
--
@@ -55,6 +66,15 @@ function main(name, opt)
end
snippet_configs.links = snippet_configs.links or name
+ -- We need to check package toolchain first
+ -- https://github.com/xmake-io/xmake/issues/4596#issuecomment-2014528801
+ --
+ -- But if it depends on some toolchain packages,
+ -- then they can't be detected early in the fetch and we have to disable system.find_package
+ if opt.package and not _check_package_toolchains(opt.package) then
+ return
+ end
+
local snippet_opt = {
verbose = opt.verbose,
target = opt.package,
diff --git a/xmake/modules/package/tools/autoconf.lua b/xmake/modules/package/tools/autoconf.lua
index b59e78cf1..9f4181b0b 100644
--- a/xmake/modules/package/tools/autoconf.lua
+++ b/xmake/modules/package/tools/autoconf.lua
@@ -68,7 +68,7 @@ end
-- get msvc
function _get_msvc(package)
- local msvc = package:toolchain("msvc") or toolchain.load("msvc", {plat = package:plat(), arch = package:arch()})
+ local msvc = package:toolchain("msvc")
assert(msvc:check(), "vs not found!") -- we need to check vs envs if it has been not checked yet
return msvc
end
diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua
index 9d8c61b73..7317f8227 100644
--- a/xmake/modules/package/tools/cmake.lua
+++ b/xmake/modules/package/tools/cmake.lua
@@ -87,7 +87,7 @@ end
-- get msvc
function _get_msvc(package)
- local msvc = package:toolchain("msvc") or toolchain.load("msvc", {plat = package:plat(), arch = package:arch()})
+ local msvc = package:toolchain("msvc")
assert(msvc:check(), "vs not found!") -- we need to check vs envs if it has been not checked yet
return msvc
end
@@ -371,6 +371,15 @@ function _get_configs_for_windows(package, configs, opt)
table.insert(configs, "-DCMAKE_GENERATOR_TOOLSET=" .. vs_toolset)
end
end
+
+ -- use clang-cl
+ if package:has_tool("cc", "clang_cl") then
+ table.insert(configs, "-DCMAKE_C_COMPILER=" .. _translate_bin_path(package:build_getenv("cc")))
+ end
+ if package:has_tool("cxx", "clang_cl") then
+ table.insert(configs, "-DCMAKE_CXX_COMPILER=" .. _translate_bin_path(package:build_getenv("cxx")))
+ end
+
-- we maybe need patch `cmake_policy(SET CMP0091 NEW)` to enable this argument for some packages
-- @see https://cmake.org/cmake/help/latest/policy/CMP0091.html#policy:CMP0091
-- https://github.com/xmake-io/xmake-repo/pull/303
@@ -964,23 +973,40 @@ function _install_for_cmakebuild(package, configs, opt)
os.vrunv(cmake.program, {"--install", os.curdir()})
end
+-- get cmake generator
+function _get_cmake_generator(package, opt)
+ opt = opt or {}
+ local cmake_generator = opt.cmake_generator
+ if not cmake_generator then
+ if project.policy("package.cmake_generator.ninja") then
+ cmake_generator = "Ninja"
+ end
+ if not cmake_generator then
+ if package:has_tool("cc", "clang_cl") or package:has_tool("cxx", "clang_cl") then
+ cmake_generator = "Ninja"
+ end
+ end
+ local cmake_generator_env = os.getenv("CMAKE_GENERATOR")
+ if not cmake_generator and cmake_generator_env then
+ cmake_generator = cmake_generator_env
+ end
+ if cmake_generator then
+ opt.cmake_generator = cmake_generator
+ end
+ end
+ return cmake_generator
+end
+
-- build package
function build(package, configs, opt)
-
- -- init options
opt = opt or {}
+ local cmake_generator = _get_cmake_generator(package, opt)
-- enter build directory
local buildir = opt.buildir or package:buildir()
os.mkdir(path.join(buildir, "install"))
local oldir = os.cd(buildir)
- -- exists $CMAKE_GENERATOR? use it
- local cmake_generator_env = os.getenv("CMAKE_GENERATOR")
- if not opt.cmake_generator and cmake_generator_env then
- opt.cmake_generator = cmake_generator_env
- end
-
-- pass configurations
local argv = {}
for name, value in pairs(_get_configs(package, configs, opt)) do
@@ -1000,7 +1026,6 @@ function build(package, configs, opt)
os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package, opt)})
-- do build
- local cmake_generator = opt.cmake_generator
if opt.cmake_build then
_build_for_cmakebuild(package, configs, opt)
elseif cmake_generator then
@@ -1025,12 +1050,8 @@ end
-- install package
function install(package, configs, opt)
-
- -- init options
opt = opt or {}
- if (not opt.cmake_generator) and project.policy("package.cmake_generator.ninja") then
- opt.cmake_generator = "Ninja"
- end
+ local cmake_generator = _get_cmake_generator(package, opt)
-- enter build directory
local buildir = opt.buildir or package:buildir()
@@ -1056,7 +1077,6 @@ function install(package, configs, opt)
os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package, opt)})
-- do build and install
- local cmake_generator = opt.cmake_generator
if opt.cmake_build then
_install_for_cmakebuild(package, configs, opt)
elseif cmake_generator then
diff --git a/xmake/modules/package/tools/gn.lua b/xmake/modules/package/tools/gn.lua
index cdcbc298a..9cef0bf7f 100644
--- a/xmake/modules/package/tools/gn.lua
+++ b/xmake/modules/package/tools/gn.lua
@@ -72,7 +72,7 @@ end
-- get msvc
function _get_msvc(package)
- local msvc = toolchain.load("msvc", {plat = package:plat(), arch = package:arch()})
+ local msvc = package:toolchain("msvc")
assert(msvc:check(), "vs not found!") -- we need to check vs envs if it has been not checked yet
return msvc
end
diff --git a/xmake/modules/package/tools/meson.lua b/xmake/modules/package/tools/meson.lua
index d567d9cb5..f03758373 100644
--- a/xmake/modules/package/tools/meson.lua
+++ b/xmake/modules/package/tools/meson.lua
@@ -347,7 +347,7 @@ end
-- get msvc
function _get_msvc(package)
- local msvc = toolchain.load("msvc", {plat = package:plat(), arch = package:arch()})
+ local msvc = package:toolchain("msvc")
assert(msvc:check(), "vs not found!") -- we need to check vs envs if it has been not checked yet
return msvc
end
diff --git a/xmake/modules/package/tools/msbuild.lua b/xmake/modules/package/tools/msbuild.lua
index 5897c357f..34aa86453 100644
--- a/xmake/modules/package/tools/msbuild.lua
+++ b/xmake/modules/package/tools/msbuild.lua
@@ -31,7 +31,7 @@ end
-- get msvc
function _get_msvc(package)
- local msvc = toolchain.load("msvc", {plat = package:plat(), arch = package:arch()})
+ local msvc = package:toolchain("msvc")
assert(msvc:check(), "vs not found!") -- we need to check vs envs if it has been not checked yet
return msvc
end
diff --git a/xmake/modules/private/action/require/impl/actions/install.lua b/xmake/modules/private/action/require/impl/actions/install.lua
index bb8b34137..6d903885a 100644
--- a/xmake/modules/private/action/require/impl/actions/install.lua
+++ b/xmake/modules/private/action/require/impl/actions/install.lua
@@ -222,7 +222,6 @@ function _fix_paths_for_precompiled_package(package)
end
end
-
-- check package toolchains
function _check_package_toolchains(package)
for _, toolchain_inst in pairs(package:toolchains()) do
diff --git a/xmake/modules/private/utils/toolchain.lua b/xmake/modules/private/utils/toolchain.lua
index dd1b6df0c..686c06ffd 100644
--- a/xmake/modules/private/utils/toolchain.lua
+++ b/xmake/modules/private/utils/toolchain.lua
@@ -25,7 +25,7 @@ function is_compatible_with_host(name)
return true
end
elseif is_host("windows") then
- if name == "msvc" or name == "llvm" then
+ if name == "msvc" or name == "llvm" or name == "clang-cl" then
return true
end
end