From 373ac0e15b800c179896f6e677619553cf426aa0 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 22 Mar 2024 23:34:16 +0800 Subject: [PATCH 01/10] fix check package toolchains --- .../action/require/impl/actions/install.lua | 13 ------------- .../action/require/impl/install_packages.lua | 19 ++++++++++++++++++- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/xmake/modules/private/action/require/impl/actions/install.lua b/xmake/modules/private/action/require/impl/actions/install.lua index bb8b341378..f154962bf9 100644 --- a/xmake/modules/private/action/require/impl/actions/install.lua +++ b/xmake/modules/private/action/require/impl/actions/install.lua @@ -222,16 +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 - if not toolchain_inst:check() then - raise("toolchain(\"%s\"): not found!", toolchain_inst:name()) - end - end -end - -- get failed install directory function _get_installdir_failed(package) return path.join(package:cachedir(), "installdir.failed") @@ -361,9 +351,6 @@ function main(package) dep:envs_enter() end - -- check package toolchains - _check_package_toolchains(package) - -- do install if script ~= nil then filter.call(script, package, {oldenvs = oldenvs}) diff --git a/xmake/modules/private/action/require/impl/install_packages.lua b/xmake/modules/private/action/require/impl/install_packages.lua index 359392f712..82037422ce 100644 --- a/xmake/modules/private/action/require/impl/install_packages.lua +++ b/xmake/modules/private/action/require/impl/install_packages.lua @@ -325,9 +325,12 @@ function _fetch_packages(packages_fetch, installdeps) end end fetching_count = fetching_count + 1 + packages_fetching[index] = instance + + -- check package toolchains, system/find_package maybe will call toolchains + _check_package_toolchains(instance) -- fetch this package - packages_fetching[index] = instance local oldenvs = os.getenvs() instance:envs_enter() instance:fetch() @@ -376,6 +379,15 @@ function _should_install_package(instance) return result end +-- check package toolchains +function _check_package_toolchains(package) + for _, toolchain_inst in pairs(package:toolchains()) do + if not toolchain_inst:check() then + raise("toolchain(\"%s\"): not found!", toolchain_inst:name()) + end + end +end + -- install packages function _install_packages(packages_install, packages_download, installdeps) @@ -484,6 +496,11 @@ function _install_packages(packages_install, packages_download, installdeps) -- install this package packages_installing[index] = instance if downloaded then + + -- check package toolchains + _check_package_toolchains(instance) + + -- do install if not action_install(instance) then assert(instance:is_precompiled(), "package(%s) should be precompiled", instance:name()) -- we need to disable built and re-download and re-install it From a179acd1f474be6821c31cf203964ce41b0177a1 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 22 Mar 2024 23:40:11 +0800 Subject: [PATCH 02/10] add check tips --- xmake/core/tool/toolchain.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua index be4e828109..ea0e9d5805 100644 --- a/xmake/core/tool/toolchain.lua +++ b/xmake/core/tool/toolchain.lua @@ -208,6 +208,9 @@ end -- get the program and name of the given tool kind function _instance:tool(toolkind) + if not self._CHECKED then + os.raise("we cannot get tool(%s) in toolchain(%s), it's not checked", toolkind, self:name()) + end -- ensure to do load for initializing toolset first -- @note we cannot call self:check() here, because it can only be called on config self:_load() From abe7c109927250244ff0294525054401134da79f Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 22 Mar 2024 23:40:31 +0800 Subject: [PATCH 03/10] revert check package toolchains --- .../action/require/impl/actions/install.lua | 12 ++++++++++++ .../action/require/impl/install_packages.lua | 19 +------------------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/xmake/modules/private/action/require/impl/actions/install.lua b/xmake/modules/private/action/require/impl/actions/install.lua index f154962bf9..6d903885a8 100644 --- a/xmake/modules/private/action/require/impl/actions/install.lua +++ b/xmake/modules/private/action/require/impl/actions/install.lua @@ -222,6 +222,15 @@ 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 + if not toolchain_inst:check() then + raise("toolchain(\"%s\"): not found!", toolchain_inst:name()) + end + end +end + -- get failed install directory function _get_installdir_failed(package) return path.join(package:cachedir(), "installdir.failed") @@ -351,6 +360,9 @@ function main(package) dep:envs_enter() end + -- check package toolchains + _check_package_toolchains(package) + -- do install if script ~= nil then filter.call(script, package, {oldenvs = oldenvs}) diff --git a/xmake/modules/private/action/require/impl/install_packages.lua b/xmake/modules/private/action/require/impl/install_packages.lua index 82037422ce..359392f712 100644 --- a/xmake/modules/private/action/require/impl/install_packages.lua +++ b/xmake/modules/private/action/require/impl/install_packages.lua @@ -325,12 +325,9 @@ function _fetch_packages(packages_fetch, installdeps) end end fetching_count = fetching_count + 1 - packages_fetching[index] = instance - - -- check package toolchains, system/find_package maybe will call toolchains - _check_package_toolchains(instance) -- fetch this package + packages_fetching[index] = instance local oldenvs = os.getenvs() instance:envs_enter() instance:fetch() @@ -379,15 +376,6 @@ function _should_install_package(instance) return result end --- check package toolchains -function _check_package_toolchains(package) - for _, toolchain_inst in pairs(package:toolchains()) do - if not toolchain_inst:check() then - raise("toolchain(\"%s\"): not found!", toolchain_inst:name()) - end - end -end - -- install packages function _install_packages(packages_install, packages_download, installdeps) @@ -496,11 +484,6 @@ function _install_packages(packages_install, packages_download, installdeps) -- install this package packages_installing[index] = instance if downloaded then - - -- check package toolchains - _check_package_toolchains(instance) - - -- do install if not action_install(instance) then assert(instance:is_precompiled(), "package(%s) should be precompiled", instance:name()) -- we need to disable built and re-download and re-install it From f259e3c8f7d94f872590f1ca743c47095f4723e8 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 23 Mar 2024 00:41:15 +0800 Subject: [PATCH 04/10] revert check toolchain --- xmake/core/tool/toolchain.lua | 3 --- 1 file changed, 3 deletions(-) diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua index ea0e9d5805..be4e828109 100644 --- a/xmake/core/tool/toolchain.lua +++ b/xmake/core/tool/toolchain.lua @@ -208,9 +208,6 @@ end -- get the program and name of the given tool kind function _instance:tool(toolkind) - if not self._CHECKED then - os.raise("we cannot get tool(%s) in toolchain(%s), it's not checked", toolkind, self:name()) - end -- ensure to do load for initializing toolset first -- @note we cannot call self:check() here, because it can only be called on config self:_load() From 9d0df4ca022d1f2ee74801c84e4930422a681ba9 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 23 Mar 2024 00:42:17 +0800 Subject: [PATCH 05/10] improve system::find_package --- .../package/manager/system/find_package.lua | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/xmake/modules/package/manager/system/find_package.lua b/xmake/modules/package/manager/system/find_package.lua index 862862d5a8..2a7b4727c5 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,12 @@ 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 + if opt.package and not _check_package_toolchains(opt.package) then + return + end + local snippet_opt = { verbose = opt.verbose, target = opt.package, From a3a7f76fe2f10792cedf4198d2b5db51f64cb721 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 23 Mar 2024 00:44:39 +0800 Subject: [PATCH 06/10] improve package:toolchain --- xmake/core/package/package.lua | 3 +++ xmake/modules/package/tools/autoconf.lua | 2 +- xmake/modules/package/tools/cmake.lua | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index c480d9b6c8..d1a082890e 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/tools/autoconf.lua b/xmake/modules/package/tools/autoconf.lua index b59e78cf1c..9f4181b0b0 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 9d8c61b733..ba96251cd9 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 From cffe3388442b419a39daed160c98a7ccff1432bb Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 23 Mar 2024 00:45:38 +0800 Subject: [PATCH 07/10] update comments --- xmake/modules/package/manager/system/find_package.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xmake/modules/package/manager/system/find_package.lua b/xmake/modules/package/manager/system/find_package.lua index 2a7b4727c5..0d0db8a183 100644 --- a/xmake/modules/package/manager/system/find_package.lua +++ b/xmake/modules/package/manager/system/find_package.lua @@ -66,8 +66,11 @@ function main(name, opt) end snippet_configs.links = snippet_configs.links or name - -- we need to check package toolchain first + -- 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 From 77a1eb757584e42371a0ace7ab8741620fb35665 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 23 Mar 2024 00:59:40 +0800 Subject: [PATCH 08/10] improve cmake to support clang-cl --- xmake/modules/package/tools/cmake.lua | 50 +++++++++++++++++++-------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index ba96251cd9..7317f8227e 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -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 From e3c73d10f93a727ab7e8a710c1e91057391315c3 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 23 Mar 2024 00:11:55 +0800 Subject: [PATCH 09/10] improve compatible --- xmake/modules/private/utils/toolchain.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/private/utils/toolchain.lua b/xmake/modules/private/utils/toolchain.lua index dd1b6df0c3..686c06ffd5 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 From a4b0293cb473fb9da151ae1d55588316c69c2bb8 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 23 Mar 2024 00:15:02 +0800 Subject: [PATCH 10/10] improve _get_msvc --- xmake/modules/package/tools/gn.lua | 2 +- xmake/modules/package/tools/meson.lua | 2 +- xmake/modules/package/tools/msbuild.lua | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/xmake/modules/package/tools/gn.lua b/xmake/modules/package/tools/gn.lua index cdcbc298af..9cef0bf7f9 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 d567d9cb58..f037583734 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 5897c357f1..34aa864535 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