Skip to content

Commit be0da31

Browse files
committed
check vs for ifort/icx
1 parent 537c5aa commit be0da31

File tree

4 files changed

+241
-2
lines changed

4 files changed

+241
-2
lines changed

xmake/toolchains/icx/check.lua

+84-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,91 @@
2222
import("core.base.option")
2323
import("core.project.config")
2424
import("detect.sdks.find_icxenv")
25+
import("detect.sdks.find_vstudio")
2526
import("lib.detect.find_tool")
2627

28+
-- attempt to check vs environment
29+
function _check_vsenv(toolchain)
30+
31+
-- has been checked?
32+
local vs = toolchain:config("vs") or config.get("vs")
33+
if vs then
34+
vs = tostring(vs)
35+
end
36+
local vcvars = toolchain:config("vcvars")
37+
if vs and vcvars then
38+
return vs
39+
end
40+
41+
-- find vstudio
42+
local vs_toolset = toolchain:config("vs_toolset") or config.get("vs_toolset")
43+
local vs_sdkver = toolchain:config("vs_sdkver") or config.get("vs_sdkver")
44+
local vstudio = find_vstudio({vcvars_ver = vs_toolset, sdkver = vs_sdkver})
45+
if vstudio then
46+
47+
-- make order vsver
48+
local vsvers = {}
49+
for vsver, _ in pairs(vstudio) do
50+
if not vs or vs ~= vsver then
51+
table.insert(vsvers, vsver)
52+
end
53+
end
54+
table.sort(vsvers, function (a, b) return tonumber(a) > tonumber(b) end)
55+
if vs then
56+
table.insert(vsvers, 1, vs)
57+
end
58+
59+
-- get vcvarsall
60+
for _, vsver in ipairs(vsvers) do
61+
local vcvarsall = (vstudio[vsver] or {}).vcvarsall or {}
62+
local vcvars = vcvarsall[toolchain:arch()]
63+
if vcvars and vcvars.PATH and vcvars.INCLUDE and vcvars.LIB then
64+
65+
-- save vcvars
66+
toolchain:config_set("vcvars", vcvars)
67+
toolchain:config_set("vcarchs", table.orderkeys(vcvarsall))
68+
toolchain:config_set("vs_toolset", vcvars.VCToolsVersion)
69+
toolchain:config_set("vs_sdkver", vcvars.WindowsSDKVersion)
70+
71+
-- check compiler
72+
local program
73+
local paths
74+
local pathenv = os.getenv("PATH")
75+
if pathenv then
76+
paths = path.splitenv(pathenv)
77+
end
78+
local tool = find_tool("cl.exe", {version = true, force = true, paths = paths, envs = vcvars})
79+
if tool then
80+
program = tool.program
81+
end
82+
if program then
83+
return vsver, tool
84+
end
85+
end
86+
end
87+
end
88+
end
89+
90+
-- check the visual studio
91+
function _check_vstudio(toolchain)
92+
local vs, cl = _check_vsenv(toolchain)
93+
if vs then
94+
if toolchain:is_global() then
95+
config.set("vs", vs, {force = true, readonly = true})
96+
end
97+
toolchain:config_set("vs", vs)
98+
toolchain:configs_save()
99+
cprint("checking for Microsoft Visual Studio (%s) version ... ${color.success}%s", toolchain:arch(), vs)
100+
if cl and cl.version then
101+
cprint("checking for LLVM Clang C/C++ Compiler (%s) version ... ${color.success}%s", toolchain:arch(), cl.version)
102+
end
103+
else
104+
cprint("checking for Microsoft Visual Studio (%s) version ... ${color.nothing}${text.nothing}", toolchain:arch())
105+
end
106+
return vs
107+
end
108+
109+
27110
-- check intel on windows
28111
function _check_intel_on_windows(toolchain)
29112

@@ -44,7 +127,7 @@ function _check_intel_on_windows(toolchain)
44127
cprint("checking for Intel LLVM C/C++ Compiler (%s) ... ${color.success}${text.success}", toolchain:arch())
45128
toolchain:config_set("varsall", icxvarsall)
46129
toolchain:configs_save()
47-
return true
130+
return _check_vstudio(toolchain)
48131
end
49132
end
50133
end

xmake/toolchains/icx/load.lua

+37
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@
2121
-- imports
2222
import("core.base.option")
2323
import("core.project.config")
24+
import("detect.sdks.find_vstudio")
25+
26+
-- add the given vs environment
27+
function _add_vsenv(toolchain, name, curenvs)
28+
29+
-- get vcvars
30+
local vcvars = toolchain:config("vcvars")
31+
if not vcvars then
32+
return
33+
end
34+
35+
-- get the paths for the vs environment
36+
local new = vcvars[name]
37+
if new then
38+
-- fix case naming conflict for cmake/msbuild between the new msvc envs and current environment, if we are running xmake in vs prompt.
39+
-- @see https://github.com/xmake-io/xmake/issues/4751
40+
for k, c in pairs(curenvs) do
41+
if name:lower() == k:lower() and name ~= k then
42+
name = k
43+
break
44+
end
45+
end
46+
toolchain:add("runenvs", name, table.unwrap(path.splitenv(new)))
47+
end
48+
end
2449

2550
-- add the given icx environment
2651
function _add_icxenv(toolchain, name)
@@ -73,6 +98,18 @@ function _load_intel_on_windows(toolchain)
7398
_add_icxenv(toolchain, "LIB")
7499
_add_icxenv(toolchain, "INCLUDE")
75100
_add_icxenv(toolchain, "LIBPATH")
101+
102+
-- add vs environments
103+
local expect_vars = {"PATH", "LIB", "INCLUDE", "LIBPATH"}
104+
local curenvs = os.getenvs()
105+
for _, name in ipairs(expect_vars) do
106+
_add_vsenv(toolchain, name, curenvs)
107+
end
108+
for _, name in ipairs(find_vstudio.get_vcvars()) do
109+
if not table.contains(expect_vars, name:upper()) then
110+
_add_vsenv(toolchain, name, curenvs)
111+
end
112+
end
76113
end
77114

78115
-- load intel on linux

xmake/toolchains/ifort/check.lua

+83-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,90 @@
2222
import("core.base.option")
2323
import("core.project.config")
2424
import("detect.sdks.find_ifortenv")
25+
import("detect.sdks.find_vstudio")
2526
import("lib.detect.find_tool")
2627

28+
-- attempt to check vs environment
29+
function _check_vsenv(toolchain)
30+
31+
-- has been checked?
32+
local vs = toolchain:config("vs") or config.get("vs")
33+
if vs then
34+
vs = tostring(vs)
35+
end
36+
local vcvars = toolchain:config("vcvars")
37+
if vs and vcvars then
38+
return vs
39+
end
40+
41+
-- find vstudio
42+
local vs_toolset = toolchain:config("vs_toolset") or config.get("vs_toolset")
43+
local vs_sdkver = toolchain:config("vs_sdkver") or config.get("vs_sdkver")
44+
local vstudio = find_vstudio({vcvars_ver = vs_toolset, sdkver = vs_sdkver})
45+
if vstudio then
46+
47+
-- make order vsver
48+
local vsvers = {}
49+
for vsver, _ in pairs(vstudio) do
50+
if not vs or vs ~= vsver then
51+
table.insert(vsvers, vsver)
52+
end
53+
end
54+
table.sort(vsvers, function (a, b) return tonumber(a) > tonumber(b) end)
55+
if vs then
56+
table.insert(vsvers, 1, vs)
57+
end
58+
59+
-- get vcvarsall
60+
for _, vsver in ipairs(vsvers) do
61+
local vcvarsall = (vstudio[vsver] or {}).vcvarsall or {}
62+
local vcvars = vcvarsall[toolchain:arch()]
63+
if vcvars and vcvars.PATH and vcvars.INCLUDE and vcvars.LIB then
64+
65+
-- save vcvars
66+
toolchain:config_set("vcvars", vcvars)
67+
toolchain:config_set("vcarchs", table.orderkeys(vcvarsall))
68+
toolchain:config_set("vs_toolset", vcvars.VCToolsVersion)
69+
toolchain:config_set("vs_sdkver", vcvars.WindowsSDKVersion)
70+
71+
-- check compiler
72+
local program
73+
local paths
74+
local pathenv = os.getenv("PATH")
75+
if pathenv then
76+
paths = path.splitenv(pathenv)
77+
end
78+
local tool = find_tool("cl.exe", {version = true, force = true, paths = paths, envs = vcvars})
79+
if tool then
80+
program = tool.program
81+
end
82+
if program then
83+
return vsver, tool
84+
end
85+
end
86+
end
87+
end
88+
end
89+
90+
-- check the visual studio
91+
function _check_vstudio(toolchain)
92+
local vs, cl = _check_vsenv(toolchain)
93+
if vs then
94+
if toolchain:is_global() then
95+
config.set("vs", vs, {force = true, readonly = true})
96+
end
97+
toolchain:config_set("vs", vs)
98+
toolchain:configs_save()
99+
cprint("checking for Microsoft Visual Studio (%s) version ... ${color.success}%s", toolchain:arch(), vs)
100+
if cl and cl.version then
101+
cprint("checking for LLVM Clang C/C++ Compiler (%s) version ... ${color.success}%s", toolchain:arch(), cl.version)
102+
end
103+
else
104+
cprint("checking for Microsoft Visual Studio (%s) version ... ${color.nothing}${text.nothing}", toolchain:arch())
105+
end
106+
return vs
107+
end
108+
27109
-- check intel on windows
28110
function _check_intel_on_windows(toolchain)
29111

@@ -44,7 +126,7 @@ function _check_intel_on_windows(toolchain)
44126
cprint("checking for Intel Fortran Compiler (%s) ... ${color.success}${text.success}", toolchain:arch())
45127
toolchain:config_set("varsall", ifortvarsall)
46128
toolchain:configs_save()
47-
return true
129+
return _check_vstudio(toolchain)
48130
end
49131
end
50132
end

xmake/toolchains/ifort/load.lua

+37
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@
2121
-- imports
2222
import("core.base.option")
2323
import("core.project.config")
24+
import("detect.sdks.find_vstudio")
25+
26+
-- add the given vs environment
27+
function _add_vsenv(toolchain, name, curenvs)
28+
29+
-- get vcvars
30+
local vcvars = toolchain:config("vcvars")
31+
if not vcvars then
32+
return
33+
end
34+
35+
-- get the paths for the vs environment
36+
local new = vcvars[name]
37+
if new then
38+
-- fix case naming conflict for cmake/msbuild between the new msvc envs and current environment, if we are running xmake in vs prompt.
39+
-- @see https://github.com/xmake-io/xmake/issues/4751
40+
for k, c in pairs(curenvs) do
41+
if name:lower() == k:lower() and name ~= k then
42+
name = k
43+
break
44+
end
45+
end
46+
toolchain:add("runenvs", name, table.unwrap(path.splitenv(new)))
47+
end
48+
end
2449

2550
-- add the given ifort environment
2651
function _add_ifortenv(toolchain, name)
@@ -62,6 +87,18 @@ function _load_intel_on_windows(toolchain)
6287
_add_ifortenv(toolchain, "LIB")
6388
_add_ifortenv(toolchain, "INCLUDE")
6489
_add_ifortenv(toolchain, "LIBPATH")
90+
91+
-- add vs environments
92+
local expect_vars = {"PATH", "LIB", "INCLUDE", "LIBPATH"}
93+
local curenvs = os.getenvs()
94+
for _, name in ipairs(expect_vars) do
95+
_add_vsenv(toolchain, name, curenvs)
96+
end
97+
for _, name in ipairs(find_vstudio.get_vcvars()) do
98+
if not table.contains(expect_vars, name:upper()) then
99+
_add_vsenv(toolchain, name, curenvs)
100+
end
101+
end
65102
end
66103

67104
-- load intel on linux

0 commit comments

Comments
 (0)