]>
Commit | Line | Data |
---|---|---|
d14feb6c | 1 | # @file WindowsVsToolChain.py\r |
de4ce46d SB |
2 | # Plugin to configures paths for the VS2017 and VS2019 tool chain\r |
3 | ##\r | |
4 | # This plugin works in conjuncture with the tools_def\r | |
5 | #\r | |
6 | # Copyright (c) Microsoft Corporation\r | |
7 | # SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
8 | ##\r | |
9 | import os\r | |
10 | import logging\r | |
11 | from edk2toolext.environment.plugintypes.uefi_build_plugin import IUefiBuildPlugin\r | |
12 | import edk2toollib.windows.locate_tools as locate_tools\r | |
13 | from edk2toollib.windows.locate_tools import FindWithVsWhere\r | |
14 | from edk2toolext.environment import shell_environment\r | |
15 | from edk2toolext.environment import version_aggregator\r | |
16 | \r | |
d14feb6c | 17 | \r |
de4ce46d SB |
18 | class WindowsVsToolChain(IUefiBuildPlugin):\r |
19 | \r | |
20 | def do_post_build(self, thebuilder):\r | |
21 | return 0\r | |
22 | \r | |
23 | def do_pre_build(self, thebuilder):\r | |
24 | self.Logger = logging.getLogger("WindowsVsToolChain")\r | |
3b4ad37e SB |
25 | interesting_keys = ["ExtensionSdkDir", "INCLUDE", "LIB", "LIBPATH", "UniversalCRTSdkDir",\r |
26 | "UCRTVersion", "WindowsLibPath", "WindowsSdkBinPath", "WindowsSdkDir", "WindowsSdkVerBinPath",\r | |
27 | "WindowsSDKVersion", "VCToolsInstallDir", "Path"]\r | |
de4ce46d SB |
28 | \r |
29 | #\r | |
30 | # VS2017 - Follow VS2017 where there is potential for many versions of the tools.\r | |
31 | # If a specific version is required then the user must set both env variables:\r | |
d14feb6c SB |
32 | # VS150INSTALLPATH: base install path on system to VC install dir. Here you will find the VC folder, etc\r |
33 | # VS150TOOLVER: version number for the VC compiler tools\r | |
34 | # VS2017_PREFIX: path to MSVC compiler folder with trailing slash (can be used instead of two vars above)\r | |
de4ce46d SB |
35 | if thebuilder.env.GetValue("TOOL_CHAIN_TAG") == "VS2017":\r |
36 | \r | |
37 | # check to see if full path already configured\r | |
38 | if shell_environment.GetEnvironment().get_shell_var("VS2017_PREFIX") != None:\r | |
39 | self.Logger.info("VS2017_PREFIX is already set.")\r | |
40 | \r | |
41 | else:\r | |
d14feb6c SB |
42 | install_path = self._get_vs_install_path(\r |
43 | "VS2017".lower(), "VS150INSTALLPATH")\r | |
de4ce46d SB |
44 | vc_ver = self._get_vc_version(install_path, "VS150TOOLVER")\r |
45 | \r | |
46 | if install_path is None or vc_ver is None:\r | |
d14feb6c SB |
47 | self.Logger.error(\r |
48 | "Failed to configure environment for VS2017")\r | |
de4ce46d SB |
49 | return -1\r |
50 | \r | |
51 | version_aggregator.GetVersionAggregator().ReportVersion(\r | |
52 | "Visual Studio Install Path", install_path, version_aggregator.VersionTypes.INFO)\r | |
53 | version_aggregator.GetVersionAggregator().ReportVersion(\r | |
54 | "VC Version", vc_ver, version_aggregator.VersionTypes.TOOL)\r | |
55 | \r | |
d14feb6c SB |
56 | # make VS2017_PREFIX to align with tools_def.txt\r |
57 | prefix = os.path.join(install_path, "VC",\r | |
58 | "Tools", "MSVC", vc_ver)\r | |
de4ce46d SB |
59 | prefix = prefix + os.path.sep\r |
60 | shell_environment.GetEnvironment().set_shell_var("VS2017_PREFIX", prefix)\r | |
61 | \r | |
3b4ad37e SB |
62 | shell_env = shell_environment.GetEnvironment()\r |
63 | # Use the tools lib to determine the correct values for the vars that interest us.\r | |
64 | vs_vars = locate_tools.QueryVcVariables(\r | |
65 | interesting_keys, "amd64", vs_version="vs2017")\r | |
66 | for (k, v) in vs_vars.items():\r | |
67 | if k.upper() == "PATH":\r | |
68 | shell_env.insert_path(v)\r | |
69 | else:\r | |
70 | shell_env.set_shell_var(k, v)\r | |
71 | \r | |
de4ce46d SB |
72 | # now confirm it exists\r |
73 | if not os.path.exists(shell_environment.GetEnvironment().get_shell_var("VS2017_PREFIX")):\r | |
74 | self.Logger.error("Path for VS2017 toolchain is invalid")\r | |
75 | return -2\r | |
76 | \r | |
77 | #\r | |
78 | # VS2019 - Follow VS2019 where there is potential for many versions of the tools.\r | |
79 | # If a specific version is required then the user must set both env variables:\r | |
d14feb6c SB |
80 | # VS160INSTALLPATH: base install path on system to VC install dir. Here you will find the VC folder, etc\r |
81 | # VS160TOOLVER: version number for the VC compiler tools\r | |
82 | # VS2019_PREFIX: path to MSVC compiler folder with trailing slash (can be used instead of two vars above)\r | |
de4ce46d SB |
83 | elif thebuilder.env.GetValue("TOOL_CHAIN_TAG") == "VS2019":\r |
84 | \r | |
85 | # check to see if full path already configured\r | |
86 | if shell_environment.GetEnvironment().get_shell_var("VS2019_PREFIX") != None:\r | |
87 | self.Logger.info("VS2019_PREFIX is already set.")\r | |
88 | \r | |
89 | else:\r | |
d14feb6c SB |
90 | install_path = self._get_vs_install_path(\r |
91 | "VS2019".lower(), "VS160INSTALLPATH")\r | |
de4ce46d SB |
92 | vc_ver = self._get_vc_version(install_path, "VS160TOOLVER")\r |
93 | \r | |
94 | if install_path is None or vc_ver is None:\r | |
d14feb6c SB |
95 | self.Logger.error(\r |
96 | "Failed to configure environment for VS2019")\r | |
de4ce46d SB |
97 | return -1\r |
98 | \r | |
99 | version_aggregator.GetVersionAggregator().ReportVersion(\r | |
100 | "Visual Studio Install Path", install_path, version_aggregator.VersionTypes.INFO)\r | |
101 | version_aggregator.GetVersionAggregator().ReportVersion(\r | |
102 | "VC Version", vc_ver, version_aggregator.VersionTypes.TOOL)\r | |
103 | \r | |
d14feb6c SB |
104 | # make VS2019_PREFIX to align with tools_def.txt\r |
105 | prefix = os.path.join(install_path, "VC",\r | |
106 | "Tools", "MSVC", vc_ver)\r | |
de4ce46d SB |
107 | prefix = prefix + os.path.sep\r |
108 | shell_environment.GetEnvironment().set_shell_var("VS2019_PREFIX", prefix)\r | |
109 | \r | |
3b4ad37e SB |
110 | shell_env = shell_environment.GetEnvironment()\r |
111 | # Use the tools lib to determine the correct values for the vars that interest us.\r | |
112 | vs_vars = locate_tools.QueryVcVariables(\r | |
113 | interesting_keys, "amd64", vs_version="vs2019")\r | |
114 | for (k, v) in vs_vars.items():\r | |
115 | if k.upper() == "PATH":\r | |
116 | shell_env.insert_path(v)\r | |
117 | else:\r | |
118 | shell_env.set_shell_var(k, v)\r | |
119 | \r | |
de4ce46d SB |
120 | # now confirm it exists\r |
121 | if not os.path.exists(shell_environment.GetEnvironment().get_shell_var("VS2019_PREFIX")):\r | |
122 | self.Logger.error("Path for VS2019 toolchain is invalid")\r | |
123 | return -2\r | |
124 | \r | |
125 | return 0\r | |
126 | \r | |
127 | def _get_vs_install_path(self, vs_version, varname):\r | |
128 | # check if already specified\r | |
129 | path = shell_environment.GetEnvironment().get_shell_var(varname)\r | |
130 | if(path is None):\r | |
131 | # Not specified...find latest\r | |
132 | (rc, path) = FindWithVsWhere(vs_version=vs_version)\r | |
133 | if rc == 0 and path is not None and os.path.exists(path):\r | |
134 | self.Logger.debug("Found VS instance for %s", vs_version)\r | |
135 | else:\r | |
d14feb6c SB |
136 | self.Logger.error(\r |
137 | "Failed to find VS instance with VsWhere (%d)" % rc)\r | |
de4ce46d SB |
138 | return path\r |
139 | \r | |
140 | def _get_vc_version(self, path, varname):\r | |
141 | # check if already specified\r | |
142 | vc_ver = shell_environment.GetEnvironment().get_shell_var(varname)\r | |
143 | if (path is None):\r | |
d14feb6c SB |
144 | self.Logger.critical(\r |
145 | "Failed to find Visual Studio tools. Might need to check for VS install")\r | |
de4ce46d SB |
146 | return vc_ver\r |
147 | if(vc_ver is None):\r | |
148 | # Not specified...find latest\r | |
149 | p2 = os.path.join(path, "VC", "Tools", "MSVC")\r | |
150 | if not os.path.isdir(p2):\r | |
151 | self.Logger.critical(\r | |
152 | "Failed to find VC tools. Might need to check for VS install")\r | |
153 | return vc_ver\r | |
154 | vc_ver = os.listdir(p2)[-1].strip() # get last in list\r | |
155 | self.Logger.debug("Found VC Tool version is %s" % vc_ver)\r | |
156 | return vc_ver\r |