PlatformBuild.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # @file
  2. # Script to Build EmulatorPkg UEFI firmware
  3. #
  4. # Copyright (c) Microsoft Corporation.
  5. # SPDX-License-Identifier: BSD-2-Clause-Patent
  6. ##
  7. import os
  8. import logging
  9. import io
  10. from edk2toolext.environment import shell_environment
  11. from edk2toolext.environment.uefi_build import UefiBuilder
  12. from edk2toolext.invocables.edk2_platform_build import BuildSettingsManager
  13. from edk2toolext.invocables.edk2_setup import SetupSettingsManager, RequiredSubmodule
  14. from edk2toolext.invocables.edk2_update import UpdateSettingsManager
  15. from edk2toolext.invocables.edk2_pr_eval import PrEvalSettingsManager
  16. from edk2toollib.utility_functions import RunCmd
  17. from edk2toollib.utility_functions import GetHostInfo
  18. # ####################################################################################### #
  19. # Common Configuration #
  20. # ####################################################################################### #
  21. class CommonPlatform():
  22. ''' Common settings for this platform. Define static data here and use
  23. for the different parts of stuart
  24. '''
  25. PackagesSupported = ("EmulatorPkg",)
  26. ArchSupported = ("X64", "IA32")
  27. TargetsSupported = ("DEBUG", "RELEASE", "NOOPT")
  28. Scopes = ('emulatorpkg', 'edk2-build')
  29. WorkspaceRoot = os.path.realpath(os.path.join(
  30. os.path.dirname(os.path.abspath(__file__)), "..", ".."))
  31. # ####################################################################################### #
  32. # Configuration for Update & Setup #
  33. # ####################################################################################### #
  34. class SettingsManager(UpdateSettingsManager, SetupSettingsManager, PrEvalSettingsManager):
  35. def GetPackagesSupported(self):
  36. ''' return iterable of edk2 packages supported by this build.
  37. These should be edk2 workspace relative paths '''
  38. return CommonPlatform.PackagesSupported
  39. def GetArchitecturesSupported(self):
  40. ''' return iterable of edk2 architectures supported by this build '''
  41. return CommonPlatform.ArchSupported
  42. def GetTargetsSupported(self):
  43. ''' return iterable of edk2 target tags supported by this build '''
  44. return CommonPlatform.TargetsSupported
  45. def GetRequiredSubmodules(self):
  46. ''' return iterable containing RequiredSubmodule objects.
  47. If no RequiredSubmodules return an empty iterable
  48. '''
  49. rs = []
  50. # intentionally declare this one with recursive false to avoid overhead
  51. rs.append(RequiredSubmodule(
  52. "CryptoPkg/Library/OpensslLib/openssl", False))
  53. # To avoid maintenance of this file for every new submodule
  54. # lets just parse the .gitmodules and add each if not already in list.
  55. # The GetRequiredSubmodules is designed to allow a build to optimize
  56. # the desired submodules but it isn't necessary for this repository.
  57. result = io.StringIO()
  58. ret = RunCmd("git", "config --file .gitmodules --get-regexp path", workingdir=self.GetWorkspaceRoot(), outstream=result)
  59. # Cmd output is expected to look like:
  60. # submodule.CryptoPkg/Library/OpensslLib/openssl.path CryptoPkg/Library/OpensslLib/openssl
  61. # submodule.SoftFloat.path ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3
  62. if ret == 0:
  63. for line in result.getvalue().splitlines():
  64. _, _, path = line.partition(" ")
  65. if path is not None:
  66. if path not in [x.path for x in rs]:
  67. rs.append(RequiredSubmodule(path, True)) # add it with recursive since we don't know
  68. return rs
  69. def SetArchitectures(self, list_of_requested_architectures):
  70. ''' Confirm the requests architecture list is valid and configure SettingsManager
  71. to run only the requested architectures.
  72. Raise Exception if a list_of_requested_architectures is not supported
  73. '''
  74. unsupported = set(list_of_requested_architectures) - \
  75. set(self.GetArchitecturesSupported())
  76. if(len(unsupported) > 0):
  77. errorString = (
  78. "Unsupported Architecture Requested: " + " ".join(unsupported))
  79. logging.critical(errorString)
  80. raise Exception(errorString)
  81. self.ActualArchitectures = list_of_requested_architectures
  82. def GetWorkspaceRoot(self):
  83. ''' get WorkspacePath '''
  84. return CommonPlatform.WorkspaceRoot
  85. def GetActiveScopes(self):
  86. ''' return tuple containing scopes that should be active for this process '''
  87. return CommonPlatform.Scopes
  88. def FilterPackagesToTest(self, changedFilesList: list, potentialPackagesList: list) -> list:
  89. ''' Filter other cases that this package should be built
  90. based on changed files. This should cover things that can't
  91. be detected as dependencies. '''
  92. build_these_packages = []
  93. possible_packages = potentialPackagesList.copy()
  94. for f in changedFilesList:
  95. # BaseTools files that might change the build
  96. if "BaseTools" in f:
  97. if os.path.splitext(f) not in [".txt", ".md"]:
  98. build_these_packages = possible_packages
  99. break
  100. # if the azure pipeline platform template file changed
  101. if "platform-build-run-steps.yml" in f:
  102. build_these_packages = possible_packages
  103. break
  104. return build_these_packages
  105. def GetPlatformDscAndConfig(self) -> tuple:
  106. ''' If a platform desires to provide its DSC then Policy 4 will evaluate if
  107. any of the changes will be built in the dsc.
  108. The tuple should be (<workspace relative path to dsc file>, <input dictionary of dsc key value pairs>)
  109. '''
  110. return (os.path.join("EmulatorPkg", "EmulatorPkg.dsc"), {})
  111. # ####################################################################################### #
  112. # Actual Configuration for Platform Build #
  113. # ####################################################################################### #
  114. class PlatformBuilder(UefiBuilder, BuildSettingsManager):
  115. def __init__(self):
  116. UefiBuilder.__init__(self)
  117. def AddCommandLineOptions(self, parserObj):
  118. ''' Add command line options to the argparser '''
  119. parserObj.add_argument('-a', "--arch", dest="build_arch", type=str, default="X64",
  120. help="Optional - architecture to build. IA32 will use IA32 for Pei & Dxe. "
  121. "X64 will use X64 for both PEI and DXE.")
  122. def RetrieveCommandLineOptions(self, args):
  123. ''' Retrieve command line options from the argparser '''
  124. shell_environment.GetBuildVars().SetValue(
  125. "TARGET_ARCH", args.build_arch.upper(), "From CmdLine")
  126. shell_environment.GetBuildVars().SetValue(
  127. "ACTIVE_PLATFORM", "EmulatorPkg/EmulatorPkg.dsc", "From CmdLine")
  128. def GetWorkspaceRoot(self):
  129. ''' get WorkspacePath '''
  130. return CommonPlatform.WorkspaceRoot
  131. def GetPackagesPath(self):
  132. ''' Return a list of workspace relative paths that should be mapped as edk2 PackagesPath '''
  133. return ()
  134. def GetActiveScopes(self):
  135. ''' return tuple containing scopes that should be active for this process '''
  136. return CommonPlatform.Scopes
  137. def GetName(self):
  138. ''' Get the name of the repo, platform, or product being build '''
  139. ''' Used for naming the log file, among others '''
  140. # check the startup nsh flag and if set then rename the log file.
  141. # this helps in CI so we don't overwrite the build log since running
  142. # uses the stuart_build command.
  143. if(shell_environment.GetBuildVars().GetValue("MAKE_STARTUP_NSH", "FALSE") == "TRUE"):
  144. return "EmulatorPkg_With_Run"
  145. return "EmulatorPkg"
  146. def GetLoggingLevel(self, loggerType):
  147. ''' Get the logging level for a given type
  148. base == lowest logging level supported
  149. con == Screen logging
  150. txt == plain text file logging
  151. md == markdown file logging
  152. '''
  153. return logging.DEBUG
  154. def SetPlatformEnv(self):
  155. logging.debug("PlatformBuilder SetPlatformEnv")
  156. self.env.SetValue("PRODUCT_NAME", "EmulatorPkg", "Platform Hardcoded")
  157. self.env.SetValue("TOOL_CHAIN_TAG", "VS2019", "Default Toolchain")
  158. # Add support for using the correct Platform Headers, tools, and Libs based on emulator architecture
  159. # requested to be built when building VS2019 or VS2017
  160. if self.env.GetValue("TOOL_CHAIN_TAG") == "VS2019" or self.env.GetValue("TOOL_CHAIN_TAG") == "VS2017":
  161. key = self.env.GetValue("TOOL_CHAIN_TAG") + "_HOST"
  162. if self.env.GetValue("TARGET_ARCH") == "IA32":
  163. shell_environment.ShellEnvironment().set_shell_var(key, "x86")
  164. elif self.env.GetValue("TARGET_ARCH") == "X64":
  165. shell_environment.ShellEnvironment().set_shell_var(key, "x64")
  166. # Add support for using the correct Platform Headers, tools, and Libs based on emulator architecture
  167. # requested to be built when building on linux.
  168. if GetHostInfo().os.upper() == "LINUX":
  169. self.ConfigureLinuxDLinkPath()
  170. if GetHostInfo().os.upper() == "WINDOWS":
  171. self.env.SetValue("BLD_*_WIN_HOST_BUILD", "TRUE",
  172. "Trigger Windows host build")
  173. self.env.SetValue("MAKE_STARTUP_NSH", "FALSE", "Default to false")
  174. # I don't see what this does but it is in build.sh
  175. key = "BLD_*_BUILD_" + self.env.GetValue("TARGET_ARCH")
  176. self.env.SetValue(key, "TRUE", "match script in build.sh")
  177. return 0
  178. def PlatformPreBuild(self):
  179. return 0
  180. def PlatformPostBuild(self):
  181. return 0
  182. def FlashRomImage(self):
  183. ''' Use the FlashRom Function to run the emulator. This gives an easy stuart command line to
  184. activate the emulator. '''
  185. OutputPath = os.path.join(self.env.GetValue(
  186. "BUILD_OUTPUT_BASE"), self.env.GetValue("TARGET_ARCH"))
  187. if (self.env.GetValue("MAKE_STARTUP_NSH") == "TRUE"):
  188. f = open(os.path.join(OutputPath, "startup.nsh"), "w")
  189. f.write("BOOT SUCCESS !!! \n")
  190. # add commands here
  191. f.write("reset\n")
  192. f.close()
  193. if GetHostInfo().os.upper() == "WINDOWS":
  194. cmd = "WinHost.exe"
  195. elif GetHostInfo().os.upper() == "LINUX":
  196. cmd = "./Host"
  197. else:
  198. logging.critical("Unsupported Host")
  199. return -1
  200. return RunCmd(cmd, "", workingdir=OutputPath)
  201. def ConfigureLinuxDLinkPath(self):
  202. '''
  203. logic copied from build.sh to setup the correct libraries
  204. '''
  205. if self.env.GetValue("TARGET_ARCH") == "IA32":
  206. LIB_NAMES = ["ld-linux.so.2", "libdl.so.2 crt1.o", "crti.o crtn.o"]
  207. LIB_SEARCH_PATHS = ["/usr/lib/i386-linux-gnu",
  208. "/usr/lib32", "/lib32", "/usr/lib", "/lib"]
  209. elif self.env.GetValue("TARGET_ARCH") == "X64":
  210. LIB_NAMES = ["ld-linux-x86-64.so.2",
  211. "libdl.so.2", "crt1.o", "crti.o", "crtn.o"]
  212. LIB_SEARCH_PATHS = ["/usr/lib/x86_64-linux-gnu",
  213. "/usr/lib64", "/lib64", "/usr/lib", "/lib"]
  214. HOST_DLINK_PATHS = ""
  215. for lname in LIB_NAMES:
  216. logging.debug(f"Looking for {lname}")
  217. for dname in LIB_SEARCH_PATHS:
  218. logging.debug(f"In {dname}")
  219. if os.path.isfile(os.path.join(dname, lname)):
  220. logging.debug(f"Found {lname} in {dname}")
  221. HOST_DLINK_PATHS += os.path.join(
  222. os.path.join(dname, lname)) + os.pathsep
  223. break
  224. HOST_DLINK_PATHS = HOST_DLINK_PATHS.rstrip(os.pathsep)
  225. logging.critical(f"Setting HOST_DLINK_PATHS to {HOST_DLINK_PATHS}")
  226. shell_environment.ShellEnvironment().set_shell_var(
  227. "HOST_DLINK_PATHS", HOST_DLINK_PATHS)