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