From d3ded5d78ea15d2f2299cab3d03165ab8d07951b Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Dec 2025 00:45:39 +0800 Subject: add vsenvs in toolchain utils --- xmake/modules/private/utils/toolchain.lua | 44 +++++++++++++++++++++++++++++-- xmake/toolchains/clang-cl/load.lua | 37 ++------------------------ xmake/toolchains/clang/load.lua | 40 +--------------------------- xmake/toolchains/icc/load.lua | 37 ++++---------------------- xmake/toolchains/icx/load.lua | 37 ++++---------------------- xmake/toolchains/ifort/load.lua | 37 ++++---------------------- xmake/toolchains/ifx/load.lua | 37 ++++---------------------- xmake/toolchains/msvc/load.lua | 37 ++------------------------ 8 files changed, 67 insertions(+), 239 deletions(-) diff --git a/xmake/modules/private/utils/toolchain.lua b/xmake/modules/private/utils/toolchain.lua index 2d6e63b15..f2b4ca593 100644 --- a/xmake/modules/private/utils/toolchain.lua +++ b/xmake/modules/private/utils/toolchain.lua @@ -91,6 +91,48 @@ function check_vstudio(toolchain, check) return vs end +-- add the given vs environment +function _add_vsenv(toolchain, name, curenvs) + + -- get vcvars + local vcvars = toolchain:config("vcvars") + if not vcvars then + return + end + + -- get the paths for the vs environment + local new = vcvars[name] + if new then + -- fix case naming conflict for cmake/msbuild between the new msvc envs and current environment, if we are running xmake in vs prompt. + -- @see https://github.com/xmake-io/xmake/issues/4751 + for k, c in pairs(curenvs) do + if name:lower() == k:lower() and name ~= k then + name = k + break + end + end + if name == "INCLUDE" or name == "LIB" then + toolchain:add("runenvs", name, table.concat(path.splitenv(new), ";")) + else + toolchain:add("runenvs", name, table.unwrap(path.splitenv(new))) + end + end +end + +-- add vs environments +function add_vsenvs(toolchain, expect_vars) + local curenvs = os.getenvs() + expect_vars = expect_vars or {"PATH", "LIB", "INCLUDE", "LIBPATH"} + for _, name in ipairs(expect_vars) do + _add_vsenv(toolchain, name, curenvs) + end + for _, name in ipairs(find_vstudio.get_vcvars()) do + if not table.contains(expect_vars, name:upper()) then + _add_vsenv(toolchain, name, curenvs) + end + end +end + -- set llvm runtimes function set_llvm_runtimes(toolchain) -- We should set them up uniformly here, because runtimes will be accessed early (in sanitizer), @@ -381,5 +423,3 @@ function add_llvm_runenvs(toolchain) end end end - - diff --git a/xmake/toolchains/clang-cl/load.lua b/xmake/toolchains/clang-cl/load.lua index a8e3ed199..f445c0739 100644 --- a/xmake/toolchains/clang-cl/load.lua +++ b/xmake/toolchains/clang-cl/load.lua @@ -21,32 +21,8 @@ -- imports import("core.base.option") import("core.project.config") -import("detect.sdks.find_vstudio") import("core.project.project") - --- add the given vs environment -function _add_vsenv(toolchain, name, curenvs) - - -- get vcvars - local vcvars = toolchain:config("vcvars") - if not vcvars then - return - end - - -- get the paths for the vs environment - local new = vcvars[name] - if new then - -- fix case naming conflict for cmake/msbuild between the new msvc envs and current environment, if we are running xmake in vs prompt. - -- @see https://github.com/xmake-io/xmake/issues/4751 - for k, c in pairs(curenvs) do - if name:lower() == k:lower() and name ~= k then - name = k - break - end - end - toolchain:add("runenvs", name, table.unwrap(path.splitenv(new))) - end -end +import("private.utils.toolchain", {alias = "toolchain_utils"}) -- main entry function main(toolchain) @@ -76,16 +52,7 @@ function main(toolchain) end -- add vs environments - local expect_vars = {"PATH", "LIB", "INCLUDE", "LIBPATH"} - local curenvs = os.getenvs() - for _, name in ipairs(expect_vars) do - _add_vsenv(toolchain, name, curenvs) - end - for _, name in ipairs(find_vstudio.get_vcvars()) do - if not table.contains(expect_vars, name:upper()) then - _add_vsenv(toolchain, name, curenvs) - end - end + toolchain_utils.add_vsenvs(toolchain) local target if toolchain:is_arch("x86_64", "x64") then diff --git a/xmake/toolchains/clang/load.lua b/xmake/toolchains/clang/load.lua index 3a15f3268..ba4dbfe94 100644 --- a/xmake/toolchains/clang/load.lua +++ b/xmake/toolchains/clang/load.lua @@ -18,53 +18,15 @@ -- @file xmake.lua -- -import("detect.sdks.find_vstudio") import("detect.sdks.find_mingw") import("core.project.config") import("core.project.project") import("private.utils.toolchain", {alias = "toolchain_utils"}) --- add the given vs environment -function _add_vsenv(toolchain, name, curenvs) - - -- get vcvars - local vcvars = toolchain:config("vcvars") - if not vcvars then - return - end - - -- get the paths for the vs environment - local new = vcvars[name] - if new then - -- fix case naming conflict for cmake/msbuild between the new msvc envs and current environment, if we are running xmake in vs prompt. - -- @see https://github.com/xmake-io/xmake/issues/4751 - for k, c in pairs(curenvs) do - if name:lower() == k:lower() and name ~= k then - name = k - break - end - end - if name == "INCLUDE" or name == "LIB" then - toolchain:add("runenvs", name, table.concat(path.splitenv(new), ";")) - else - toolchain:add("runenvs", name, table.unwrap(path.splitenv(new))) - end - end -end - function _load_windows(toolchain, suffix) -- add vs environments - local expect_vars = {"PATH", "LIB", "INCLUDE", "LIBPATH"} - local curenvs = os.getenvs() - for _, name in ipairs(expect_vars) do - _add_vsenv(toolchain, name, curenvs) - end - for _, name in ipairs(find_vstudio.get_vcvars()) do - if not table.contains(expect_vars, name:upper()) then - _add_vsenv(toolchain, name, curenvs) - end - end + toolchain_utils.add_vsenvs(toolchain) if is_host("linux") or project.policy("build.optimization.lto") then toolchain:add("ldflags", "-fuse-ld=lld-link" .. suffix) diff --git a/xmake/toolchains/icc/load.lua b/xmake/toolchains/icc/load.lua index 7bae8a9d1..20b7daa82 100644 --- a/xmake/toolchains/icc/load.lua +++ b/xmake/toolchains/icc/load.lua @@ -21,31 +21,7 @@ -- imports import("core.base.option") import("core.project.config") -import("detect.sdks.find_vstudio") - --- add the given vs environment -function _add_vsenv(toolchain, name, curenvs) - - -- get vcvars - local vcvars = toolchain:config("vcvars") - if not vcvars then - return - end - - -- get the paths for the vs environment - local new = vcvars[name] - if new then - -- fix case naming conflict for cmake/msbuild between the new msvc envs and current environment, if we are running xmake in vs prompt. - -- @see https://github.com/xmake-io/xmake/issues/4751 - for k, c in pairs(curenvs) do - if name:lower() == k:lower() and name ~= k then - name = k - break - end - end - toolchain:add("runenvs", name, table.unpack(path.splitenv(new))) - end -end +import("private.utils.toolchain", {alias = "toolchain_utils"}) -- add the given icl environment function _add_iclenv(toolchain, name, curenvs) @@ -101,18 +77,15 @@ function _load_intel_on_windows(toolchain) toolchain:set("toolset", "as", "icc") end - -- add vs/icl environments + -- add vs environments + toolchain_utils.add_vsenvs(toolchain) + + -- add icl environments local expect_vars = {"PATH", "LIB", "INCLUDE", "LIBPATH"} local curenvs = os.getenvs() for _, name in ipairs(expect_vars) do - _add_vsenv(toolchain, name, curenvs) _add_iclenv(toolchain, name, curenvs) end - for _, name in ipairs(find_vstudio.get_vcvars()) do - if not table.contains(expect_vars, name:upper()) then - _add_vsenv(toolchain, name, curenvs) - end - end end -- load intel on linux diff --git a/xmake/toolchains/icx/load.lua b/xmake/toolchains/icx/load.lua index cd3064676..f91bd5348 100644 --- a/xmake/toolchains/icx/load.lua +++ b/xmake/toolchains/icx/load.lua @@ -21,31 +21,7 @@ -- imports import("core.base.option") import("core.project.config") -import("detect.sdks.find_vstudio") - --- add the given vs environment -function _add_vsenv(toolchain, name, curenvs) - - -- get vcvars - local vcvars = toolchain:config("vcvars") - if not vcvars then - return - end - - -- get the paths for the vs environment - local new = vcvars[name] - if new then - -- fix case naming conflict for cmake/msbuild between the new msvc envs and current environment, if we are running xmake in vs prompt. - -- @see https://github.com/xmake-io/xmake/issues/4751 - for k, c in pairs(curenvs) do - if name:lower() == k:lower() and name ~= k then - name = k - break - end - end - toolchain:add("runenvs", name, table.unpack(path.splitenv(new))) - end -end +import("private.utils.toolchain", {alias = "toolchain_utils"}) -- add the given icx environment function _add_icxenv(toolchain, name, curenvs) @@ -101,18 +77,15 @@ function _load_intel_on_windows(toolchain) toolchain:set("toolset", "as", "icx") end - -- add vs/icx environments + -- add vs environments + toolchain_utils.add_vsenvs(toolchain) + + -- add icx environments local expect_vars = {"PATH", "LIB", "INCLUDE", "LIBPATH"} local curenvs = os.getenvs() for _, name in ipairs(expect_vars) do - _add_vsenv(toolchain, name, curenvs) _add_icxenv(toolchain, name, curenvs) end - for _, name in ipairs(find_vstudio.get_vcvars()) do - if not table.contains(expect_vars, name:upper()) then - _add_vsenv(toolchain, name, curenvs) - end - end end -- load intel on linux diff --git a/xmake/toolchains/ifort/load.lua b/xmake/toolchains/ifort/load.lua index 8593b2399..602e0d3b0 100644 --- a/xmake/toolchains/ifort/load.lua +++ b/xmake/toolchains/ifort/load.lua @@ -21,31 +21,7 @@ -- imports import("core.base.option") import("core.project.config") -import("detect.sdks.find_vstudio") - --- add the given vs environment -function _add_vsenv(toolchain, name, curenvs) - - -- get vcvars - local vcvars = toolchain:config("vcvars") - if not vcvars then - return - end - - -- get the paths for the vs environment - local new = vcvars[name] - if new then - -- fix case naming conflict for cmake/msbuild between the new msvc envs and current environment, if we are running xmake in vs prompt. - -- @see https://github.com/xmake-io/xmake/issues/4751 - for k, c in pairs(curenvs) do - if name:lower() == k:lower() and name ~= k then - name = k - break - end - end - toolchain:add("runenvs", name, table.unpack(path.splitenv(new))) - end -end +import("private.utils.toolchain", {alias = "toolchain_utils"}) -- add the given ifort environment function _add_ifortenv(toolchain, name, curenvs) @@ -90,18 +66,15 @@ function _load_intel_on_windows(toolchain) toolchain:set("toolset", "fcsh", "ifort.exe") toolchain:set("toolset", "ar", "link.exe") - -- add ifort and vs environments + -- add vs environments + toolchain_utils.add_vsenvs(toolchain) + + -- add ifort environments local expect_vars = {"PATH", "LIB", "INCLUDE", "LIBPATH"} local curenvs = os.getenvs() for _, name in ipairs(expect_vars) do - _add_vsenv(toolchain, name, curenvs) _add_ifortenv(toolchain, name, curenvs) end - for _, name in ipairs(find_vstudio.get_vcvars()) do - if not table.contains(expect_vars, name:upper()) then - _add_vsenv(toolchain, name, curenvs) - end - end end -- load intel on linux diff --git a/xmake/toolchains/ifx/load.lua b/xmake/toolchains/ifx/load.lua index 31fbd3ae5..be2a48129 100644 --- a/xmake/toolchains/ifx/load.lua +++ b/xmake/toolchains/ifx/load.lua @@ -21,31 +21,7 @@ -- imports import("core.base.option") import("core.project.config") -import("detect.sdks.find_vstudio") - --- add the given vs environment -function _add_vsenv(toolchain, name, curenvs) - - -- get vcvars - local vcvars = toolchain:config("vcvars") - if not vcvars then - return - end - - -- get the paths for the vs environment - local new = vcvars[name] - if new then - -- fix case naming conflict for cmake/msbuild between the new msvc envs and current environment, if we are running xmake in vs prompt. - -- @see https://github.com/xmake-io/xmake/issues/4751 - for k, c in pairs(curenvs) do - if name:lower() == k:lower() and name ~= k then - name = k - break - end - end - toolchain:add("runenvs", name, table.unpack(path.splitenv(new))) - end -end +import("private.utils.toolchain", {alias = "toolchain_utils"}) -- add the given ifx environment function _add_ifxenv(toolchain, name, curenvs) @@ -90,18 +66,15 @@ function _load_intel_on_windows(toolchain) toolchain:set("toolset", "fcsh", "ifx.exe") toolchain:set("toolset", "ar", "link.exe") - -- add vs/ifx environments + -- add vs environments + toolchain_utils.add_vsenvs(toolchain) + + -- add ifx environments local expect_vars = {"PATH", "LIB", "INCLUDE", "LIBPATH"} local curenvs = os.getenvs() for _, name in ipairs(expect_vars) do - _add_vsenv(toolchain, name, curenvs) _add_ifxenv(toolchain, name, curenvs) end - for _, name in ipairs(find_vstudio.get_vcvars()) do - if not table.contains(expect_vars, name:upper()) then - _add_vsenv(toolchain, name, curenvs) - end - end end -- load intel on linux diff --git a/xmake/toolchains/msvc/load.lua b/xmake/toolchains/msvc/load.lua index 4f6aab45d..c606a0edc 100644 --- a/xmake/toolchains/msvc/load.lua +++ b/xmake/toolchains/msvc/load.lua @@ -22,31 +22,7 @@ import("core.base.option") import("core.base.semver") import("core.project.config") -import("detect.sdks.find_vstudio") - --- add the given vs environment -function _add_vsenv(toolchain, name, curenvs) - - -- get vcvars - local vcvars = toolchain:config("vcvars") - if not vcvars then - return - end - - -- get the paths for the vs environment - local new = vcvars[name] - if new then - -- fix case naming conflict for cmake/msbuild between the new msvc envs and current environment, if we are running xmake in vs prompt. - -- @see https://github.com/xmake-io/xmake/issues/4751 - for k, c in pairs(curenvs) do - if name:lower() == k:lower() and name ~= k then - name = k - break - end - end - toolchain:add("runenvs", name, table.unwrap(path.splitenv(new))) - end -end +import("private.utils.toolchain", {alias = "toolchain_utils"}) -- main entry function main(toolchain) @@ -74,16 +50,7 @@ function main(toolchain) end -- add vs environments - local expect_vars = {"PATH", "LIB", "INCLUDE", "LIBPATH"} - local curenvs = os.getenvs() - for _, name in ipairs(expect_vars) do - _add_vsenv(toolchain, name, curenvs) - end - for _, name in ipairs(find_vstudio.get_vcvars()) do - if not table.contains(expect_vars, name:upper()) then - _add_vsenv(toolchain, name, curenvs) - end - end + toolchain_utils.add_vsenvs(toolchain) -- check and add vs_binary_output env local vs = toolchain:config("vs") -- cgit v1.3.1