]> git.proxmox.com Git - mirror_edk2.git/blame - ArmVirtPkg/PlatformCI/PlatformBuildLib.py
ArmVirtPkg/PlatformCI: factor out reusable PlatformBuildLib.py
[mirror_edk2.git] / ArmVirtPkg / PlatformCI / PlatformBuildLib.py
CommitLineData
0c7f189e
SB
1# @file\r
2# Script to Build ArmVirtPkg UEFI firmware\r
3#\r
4# Copyright (c) Microsoft Corporation.\r
5# SPDX-License-Identifier: BSD-2-Clause-Patent\r
6##\r
7import os\r
8import logging\r
9import io\r
10\r
11from edk2toolext.environment import shell_environment\r
12from edk2toolext.environment.uefi_build import UefiBuilder\r
13from edk2toolext.invocables.edk2_platform_build import BuildSettingsManager\r
14from edk2toolext.invocables.edk2_setup import SetupSettingsManager, RequiredSubmodule\r
15from edk2toolext.invocables.edk2_update import UpdateSettingsManager\r
16from edk2toolext.invocables.edk2_pr_eval import PrEvalSettingsManager\r
17from edk2toollib.utility_functions import RunCmd\r
18from edk2toollib.utility_functions import GetHostInfo\r
19\r
0c7f189e
SB
20\r
21 # ####################################################################################### #\r
22 # Configuration for Update & Setup #\r
23 # ####################################################################################### #\r
24\r
25\r
26class SettingsManager(UpdateSettingsManager, SetupSettingsManager, PrEvalSettingsManager):\r
27\r
28 def GetPackagesSupported(self):\r
29 ''' return iterable of edk2 packages supported by this build.\r
30 These should be edk2 workspace relative paths '''\r
31 return CommonPlatform.PackagesSupported\r
32\r
33 def GetArchitecturesSupported(self):\r
34 ''' return iterable of edk2 architectures supported by this build '''\r
35 return CommonPlatform.ArchSupported\r
36\r
37 def GetTargetsSupported(self):\r
38 ''' return iterable of edk2 target tags supported by this build '''\r
39 return CommonPlatform.TargetsSupported\r
40\r
41 def GetRequiredSubmodules(self):\r
42 ''' return iterable containing RequiredSubmodule objects.\r
43 If no RequiredSubmodules return an empty iterable\r
44 '''\r
45 rs = []\r
46\r
47 # intentionally declare this one with recursive false to avoid overhead\r
48 rs.append(RequiredSubmodule(\r
49 "CryptoPkg/Library/OpensslLib/openssl", False))\r
50\r
51 # To avoid maintenance of this file for every new submodule\r
52 # lets just parse the .gitmodules and add each if not already in list.\r
53 # The GetRequiredSubmodules is designed to allow a build to optimize\r
54 # the desired submodules but it isn't necessary for this repository.\r
55 result = io.StringIO()\r
56 ret = RunCmd("git", "config --file .gitmodules --get-regexp path", workingdir=self.GetWorkspaceRoot(), outstream=result)\r
57 # Cmd output is expected to look like:\r
58 # submodule.CryptoPkg/Library/OpensslLib/openssl.path CryptoPkg/Library/OpensslLib/openssl\r
59 # submodule.SoftFloat.path ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3\r
60 if ret == 0:\r
61 for line in result.getvalue().splitlines():\r
62 _, _, path = line.partition(" ")\r
63 if path is not None:\r
64 if path not in [x.path for x in rs]:\r
65 rs.append(RequiredSubmodule(path, True)) # add it with recursive since we don't know\r
66 return rs\r
67\r
68 def SetArchitectures(self, list_of_requested_architectures):\r
69 ''' Confirm the requests architecture list is valid and configure SettingsManager\r
70 to run only the requested architectures.\r
71\r
72 Raise Exception if a list_of_requested_architectures is not supported\r
73 '''\r
74 unsupported = set(list_of_requested_architectures) - \\r
75 set(self.GetArchitecturesSupported())\r
76 if(len(unsupported) > 0):\r
77 errorString = (\r
78 "Unsupported Architecture Requested: " + " ".join(unsupported))\r
79 logging.critical(errorString)\r
80 raise Exception(errorString)\r
81 self.ActualArchitectures = list_of_requested_architectures\r
82\r
83 def GetWorkspaceRoot(self):\r
84 ''' get WorkspacePath '''\r
85 return CommonPlatform.WorkspaceRoot\r
86\r
87 def GetActiveScopes(self):\r
88 ''' return tuple containing scopes that should be active for this process '''\r
89\r
90 scopes = CommonPlatform.Scopes\r
91 ActualToolChainTag = shell_environment.GetBuildVars().GetValue("TOOL_CHAIN_TAG", "")\r
92\r
93 if GetHostInfo().os.upper() == "LINUX" and ActualToolChainTag.upper().startswith("GCC"):\r
94 if "AARCH64" in self.ActualArchitectures:\r
95 scopes += ("gcc_aarch64_linux",)\r
96 if "ARM" in self.ActualArchitectures:\r
97 scopes += ("gcc_arm_linux",)\r
98 return scopes\r
99\r
100 def FilterPackagesToTest(self, changedFilesList: list, potentialPackagesList: list) -> list:\r
101 ''' Filter other cases that this package should be built\r
102 based on changed files. This should cover things that can't\r
103 be detected as dependencies. '''\r
104 build_these_packages = []\r
105 possible_packages = potentialPackagesList.copy()\r
106 for f in changedFilesList:\r
107 # BaseTools files that might change the build\r
108 if "BaseTools" in f:\r
109 if os.path.splitext(f) not in [".txt", ".md"]:\r
110 build_these_packages = possible_packages\r
111 break\r
112\r
113 # if the azure pipeline platform template file changed\r
114 if "platform-build-run-steps.yml" in f:\r
115 build_these_packages = possible_packages\r
116 break\r
117\r
118\r
119 return build_these_packages\r
120\r
121 def GetPlatformDscAndConfig(self) -> tuple:\r
122 ''' If a platform desires to provide its DSC then Policy 4 will evaluate if\r
123 any of the changes will be built in the dsc.\r
124\r
125 The tuple should be (<workspace relative path to dsc file>, <input dictionary of dsc key value pairs>)\r
126 '''\r
01a06884 127 return (CommonPlatform.DscName, {})\r
0c7f189e
SB
128\r
129\r
130 # ####################################################################################### #\r
131 # Actual Configuration for Platform Build #\r
132 # ####################################################################################### #\r
133\r
134\r
135class PlatformBuilder(UefiBuilder, BuildSettingsManager):\r
136 def __init__(self):\r
137 UefiBuilder.__init__(self)\r
138\r
139 def AddCommandLineOptions(self, parserObj):\r
140 ''' Add command line options to the argparser '''\r
141 parserObj.add_argument('-a', "--arch", dest="build_arch", type=str, default="AARCH64",\r
142 help="Optional - Architecture to build. Default = AARCH64")\r
143\r
144 def RetrieveCommandLineOptions(self, args):\r
145 ''' Retrieve command line options from the argparser '''\r
146\r
147 shell_environment.GetBuildVars().SetValue(\r
148 "TARGET_ARCH", args.build_arch.upper(), "From CmdLine")\r
149\r
150 shell_environment.GetBuildVars().SetValue(\r
01a06884 151 "ACTIVE_PLATFORM", CommonPlatform.DscName, "From CmdLine")\r
0c7f189e
SB
152\r
153 def GetWorkspaceRoot(self):\r
154 ''' get WorkspacePath '''\r
155 return CommonPlatform.WorkspaceRoot\r
156\r
157 def GetPackagesPath(self):\r
158 ''' Return a list of workspace relative paths that should be mapped as edk2 PackagesPath '''\r
159 return ()\r
160\r
161 def GetActiveScopes(self):\r
162 ''' return tuple containing scopes that should be active for this process '''\r
163 scopes = CommonPlatform.Scopes\r
164 ActualToolChainTag = shell_environment.GetBuildVars().GetValue("TOOL_CHAIN_TAG", "")\r
165 Arch = shell_environment.GetBuildVars().GetValue("TARGET_ARCH", "")\r
166\r
167 if GetHostInfo().os.upper() == "LINUX" and ActualToolChainTag.upper().startswith("GCC"):\r
168 if "AARCH64" == Arch:\r
169 scopes += ("gcc_aarch64_linux",)\r
170 elif "ARM" == Arch:\r
171 scopes += ("gcc_arm_linux",)\r
172 return scopes\r
173\r
174 def GetName(self):\r
175 ''' Get the name of the repo, platform, or product being build '''\r
176 ''' Used for naming the log file, among others '''\r
177 # check the startup nsh flag and if set then rename the log file.\r
178 # this helps in CI so we don't overwrite the build log since running\r
179 # uses the stuart_build command.\r
180 if(shell_environment.GetBuildVars().GetValue("MAKE_STARTUP_NSH", "FALSE") == "TRUE"):\r
181 return "ArmVirtPkg_With_Run"\r
182 return "ArmVirtPkg"\r
183\r
184 def GetLoggingLevel(self, loggerType):\r
185 ''' Get the logging level for a given type\r
186 base == lowest logging level supported\r
187 con == Screen logging\r
188 txt == plain text file logging\r
189 md == markdown file logging\r
190 '''\r
191 return logging.DEBUG\r
192\r
193 def SetPlatformEnv(self):\r
194 logging.debug("PlatformBuilder SetPlatformEnv")\r
195 self.env.SetValue("PRODUCT_NAME", "ArmVirtQemu", "Platform Hardcoded")\r
196 self.env.SetValue("MAKE_STARTUP_NSH", "FALSE", "Default to false")\r
197 self.env.SetValue("QEMU_HEADLESS", "FALSE", "Default to false")\r
198 return 0\r
199\r
200 def PlatformPreBuild(self):\r
201 return 0\r
202\r
203 def PlatformPostBuild(self):\r
204 return 0\r
205\r
206 def FlashRomImage(self):\r
207 VirtualDrive = os.path.join(self.env.GetValue(\r
208 "BUILD_OUTPUT_BASE"), "VirtualDrive")\r
209 os.makedirs(VirtualDrive, exist_ok=True)\r
210 OutputPath_FV = os.path.join(\r
211 self.env.GetValue("BUILD_OUTPUT_BASE"), "FV")\r
212 Built_FV = os.path.join(OutputPath_FV, "QEMU_EFI.fd")\r
213\r
214 # pad fd to 64mb\r
215 with open(Built_FV, "ab") as fvfile:\r
216 fvfile.seek(0, os.SEEK_END)\r
217 additional = b'\0' * ((64 * 1024 * 1024)-fvfile.tell())\r
218 fvfile.write(additional)\r
219\r
220 # QEMU must be on that path\r
221\r
222 # Unique Command and Args parameters per ARCH\r
223 if (self.env.GetValue("TARGET_ARCH").upper() == "AARCH64"):\r
224 cmd = "qemu-system-aarch64"\r
225 args = "-M virt"\r
226 args += " -cpu cortex-a57" # emulate cpu\r
227 elif(self.env.GetValue("TARGET_ARCH").upper() == "ARM"):\r
228 cmd = "qemu-system-arm"\r
229 args = "-M virt"\r
230 args += " -cpu cortex-a15" # emulate cpu\r
231 else:\r
232 raise NotImplementedError()\r
233\r
234 # Common Args\r
235 args += " -pflash " + Built_FV # path to fw\r
236 args += " -m 1024" # 1gb memory\r
237 # turn off network\r
238 args += " -net none"\r
239 # Serial messages out\r
240 args += " -serial stdio"\r
241 # Mount disk with startup.nsh\r
242 args += f" -drive file=fat:rw:{VirtualDrive},format=raw,media=disk"\r
243\r
244 # Conditional Args\r
245 if (self.env.GetValue("QEMU_HEADLESS").upper() == "TRUE"):\r
246 args += " -display none" # no graphics\r
247\r
248 if (self.env.GetValue("MAKE_STARTUP_NSH").upper() == "TRUE"):\r
249 f = open(os.path.join(VirtualDrive, "startup.nsh"), "w")\r
250 f.write("BOOT SUCCESS !!! \n")\r
251 # add commands here\r
252 f.write("reset -s\n")\r
253 f.close()\r
254\r
255 ret = RunCmd(cmd, args)\r
256\r
257 if ret == 0xc0000005:\r
258 # for some reason getting a c0000005 on successful return\r
259 return 0\r
260\r
261 return ret\r