]>
Commit | Line | Data |
---|---|---|
1 | # @file Edk2ToolsBuild.py\r | |
2 | # Invocable class that builds the basetool c files.\r | |
3 | #\r | |
4 | # Supports VS2017, VS2019, and GCC5\r | |
5 | ##\r | |
6 | # Copyright (c) Microsoft Corporation\r | |
7 | #\r | |
8 | # SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
9 | ##\r | |
10 | import os\r | |
11 | import sys\r | |
12 | import logging\r | |
13 | import argparse\r | |
14 | import multiprocessing\r | |
15 | from edk2toolext import edk2_logging\r | |
16 | from edk2toolext.environment import self_describing_environment\r | |
17 | from edk2toolext.base_abstract_invocable import BaseAbstractInvocable\r | |
18 | from edk2toollib.utility_functions import RunCmd\r | |
19 | from edk2toollib.windows.locate_tools import QueryVcVariables\r | |
20 | \r | |
21 | \r | |
22 | class Edk2ToolsBuild(BaseAbstractInvocable):\r | |
23 | \r | |
24 | def ParseCommandLineOptions(self):\r | |
25 | ''' parse arguments '''\r | |
26 | ParserObj = argparse.ArgumentParser()\r | |
27 | ParserObj.add_argument("-t", "--tool_chain_tag", dest="tct", default="VS2017",\r | |
28 | help="Set the toolchain used to compile the build tools")\r | |
29 | args = ParserObj.parse_args()\r | |
30 | self.tool_chain_tag = args.tct\r | |
31 | \r | |
32 | def GetWorkspaceRoot(self):\r | |
33 | ''' Return the workspace root for initializing the SDE '''\r | |
34 | \r | |
35 | # this is the bastools dir...not the traditional EDK2 workspace root\r | |
36 | return os.path.dirname(os.path.abspath(__file__))\r | |
37 | \r | |
38 | def GetActiveScopes(self):\r | |
39 | ''' return tuple containing scopes that should be active for this process '''\r | |
40 | \r | |
41 | # for now don't use scopes\r | |
42 | return ('global',)\r | |
43 | \r | |
44 | def GetLoggingLevel(self, loggerType):\r | |
45 | ''' Get the logging level for a given type (return Logging.Level)\r | |
46 | base == lowest logging level supported\r | |
47 | con == Screen logging\r | |
48 | txt == plain text file logging\r | |
49 | md == markdown file logging\r | |
50 | '''\r | |
51 | if(loggerType == "con"):\r | |
52 | return logging.ERROR\r | |
53 | else:\r | |
54 | return logging.DEBUG\r | |
55 | \r | |
56 | def GetLoggingFolderRelativeToRoot(self):\r | |
57 | ''' Return a path to folder for log files '''\r | |
58 | return "BaseToolsBuild"\r | |
59 | \r | |
60 | def GetVerifyCheckRequired(self):\r | |
61 | ''' Will call self_describing_environment.VerifyEnvironment if this returns True '''\r | |
62 | return True\r | |
63 | \r | |
64 | def GetLoggingFileName(self, loggerType):\r | |
65 | ''' Get the logging file name for the type.\r | |
66 | Return None if the logger shouldn't be created\r | |
67 | \r | |
68 | base == lowest logging level supported\r | |
69 | con == Screen logging\r | |
70 | txt == plain text file logging\r | |
71 | md == markdown file logging\r | |
72 | '''\r | |
73 | return "BASETOOLS_BUILD"\r | |
74 | \r | |
75 | def WritePathEnvFile(self, OutputDir):\r | |
76 | ''' Write a PyTool path env file for future PyTool based edk2 builds'''\r | |
77 | content = '''##\r | |
78 | # Set shell variable EDK_TOOLS_BIN to this folder\r | |
79 | #\r | |
80 | # Autogenerated by Edk2ToolsBuild.py\r | |
81 | #\r | |
82 | # Copyright (c), Microsoft Corporation\r | |
83 | # SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
84 | ##\r | |
85 | {\r | |
86 | "id": "You-Built-BaseTools",\r | |
87 | "scope": "edk2-build",\r | |
88 | "flags": ["set_shell_var", "set_path"],\r | |
89 | "var_name": "EDK_TOOLS_BIN"\r | |
90 | }\r | |
91 | '''\r | |
92 | with open(os.path.join(OutputDir, "basetoolsbin_path_env.yaml"), "w") as f:\r | |
93 | f.write(content)\r | |
94 | \r | |
95 | def Go(self):\r | |
96 | logging.info("Running Python version: " + str(sys.version_info))\r | |
97 | \r | |
98 | (build_env, shell_env) = self_describing_environment.BootstrapEnvironment(\r | |
99 | self.GetWorkspaceRoot(), self.GetActiveScopes())\r | |
100 | \r | |
101 | # # Bind our current execution environment into the shell vars.\r | |
102 | ph = os.path.dirname(sys.executable)\r | |
103 | if " " in ph:\r | |
104 | ph = '"' + ph + '"'\r | |
105 | shell_env.set_shell_var("PYTHON_HOME", ph)\r | |
106 | # PYTHON_COMMAND is required to be set for using edk2 python builds.\r | |
107 | pc = sys.executable\r | |
108 | if " " in pc:\r | |
109 | pc = '"' + pc + '"'\r | |
110 | shell_env.set_shell_var("PYTHON_COMMAND", pc)\r | |
111 | \r | |
112 | if self.tool_chain_tag.lower().startswith("vs"):\r | |
113 | \r | |
114 | # # Update environment with required VC vars.\r | |
115 | interesting_keys = ["ExtensionSdkDir", "INCLUDE", "LIB"]\r | |
116 | interesting_keys.extend(\r | |
117 | ["LIBPATH", "Path", "UniversalCRTSdkDir", "UCRTVersion", "WindowsLibPath", "WindowsSdkBinPath"])\r | |
118 | interesting_keys.extend(\r | |
119 | ["WindowsSdkDir", "WindowsSdkVerBinPath", "WindowsSDKVersion", "VCToolsInstallDir"])\r | |
120 | vc_vars = QueryVcVariables(\r | |
121 | interesting_keys, 'x86', vs_version=self.tool_chain_tag.lower())\r | |
122 | for key in vc_vars.keys():\r | |
123 | logging.debug(f"Var - {key} = {vc_vars[key]}")\r | |
124 | if key.lower() == 'path':\r | |
125 | shell_env.insert_path(vc_vars[key])\r | |
126 | else:\r | |
127 | shell_env.set_shell_var(key, vc_vars[key])\r | |
128 | \r | |
129 | self.OutputDir = os.path.join(\r | |
130 | shell_env.get_shell_var("EDK_TOOLS_PATH"), "Bin", "Win32")\r | |
131 | \r | |
132 | # compiled tools need to be added to path because antlr is referenced\r | |
133 | shell_env.insert_path(self.OutputDir)\r | |
134 | \r | |
135 | # Actually build the tools.\r | |
136 | ret = RunCmd('nmake.exe', None,\r | |
137 | workingdir=shell_env.get_shell_var("EDK_TOOLS_PATH"))\r | |
138 | if ret != 0:\r | |
139 | raise Exception("Failed to build.")\r | |
140 | \r | |
141 | self.WritePathEnvFile(self.OutputDir)\r | |
142 | return ret\r | |
143 | \r | |
144 | elif self.tool_chain_tag.lower().startswith("gcc"):\r | |
145 | cpu_count = self.GetCpuThreads()\r | |
146 | ret = RunCmd("make", f"-C . -j {cpu_count}", workingdir=shell_env.get_shell_var("EDK_TOOLS_PATH"))\r | |
147 | if ret != 0:\r | |
148 | raise Exception("Failed to build.")\r | |
149 | \r | |
150 | self.OutputDir = os.path.join(\r | |
151 | shell_env.get_shell_var("EDK_TOOLS_PATH"), "Source", "C", "bin")\r | |
152 | \r | |
153 | self.WritePathEnvFile(self.OutputDir)\r | |
154 | return ret\r | |
155 | \r | |
156 | logging.critical("Tool Chain not supported")\r | |
157 | return -1\r | |
158 | \r | |
159 | def GetCpuThreads(self) -> int:\r | |
160 | ''' Function to return number of cpus. If error return 1'''\r | |
161 | cpus = 1\r | |
162 | try:\r | |
163 | cpus = multiprocessing.cpu_count()\r | |
164 | except:\r | |
165 | # from the internet there are cases where cpu_count is not implemented.\r | |
166 | # will handle error by just doing single proc build\r | |
167 | pass\r | |
168 | return cpus\r | |
169 | \r | |
170 | \r | |
171 | \r | |
172 | def main():\r | |
173 | Edk2ToolsBuild().Invoke()\r | |
174 | \r | |
175 | \r | |
176 | if __name__ == "__main__":\r | |
177 | main()\r |