]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EmulatorPkg/PlatformCI/PlatformBuild.py
EmulatorPkg: Add Platform CI and configuration for Core CI
[mirror_edk2.git] / EmulatorPkg / PlatformCI / PlatformBuild.py
diff --git a/EmulatorPkg/PlatformCI/PlatformBuild.py b/EmulatorPkg/PlatformCI/PlatformBuild.py
new file mode 100644 (file)
index 0000000..6defbf6
--- /dev/null
@@ -0,0 +1,272 @@
+# @file\r
+# Script to Build EmulatorPkg UEFI firmware\r
+#\r
+# Copyright (c) Microsoft Corporation.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+import os\r
+import logging\r
+import io\r
+\r
+from edk2toolext.environment import shell_environment\r
+from edk2toolext.environment.uefi_build import UefiBuilder\r
+from edk2toolext.invocables.edk2_platform_build import BuildSettingsManager\r
+from edk2toolext.invocables.edk2_setup import SetupSettingsManager, RequiredSubmodule\r
+from edk2toolext.invocables.edk2_update import UpdateSettingsManager\r
+from edk2toolext.invocables.edk2_pr_eval import PrEvalSettingsManager\r
+from edk2toollib.utility_functions import RunCmd\r
+from edk2toollib.utility_functions import GetHostInfo\r
+\r
+# ####################################################################################### #\r
+#                                Common Configuration                                     #\r
+# ####################################################################################### #\r
+\r
+\r
+class CommonPlatform():\r
+    ''' Common settings for this platform.  Define static data here and use\r
+        for the different parts of stuart\r
+    '''\r
+    PackagesSupported = ("EmulatorPkg",)\r
+    ArchSupported = ("X64", "IA32")\r
+    TargetsSupported = ("DEBUG", "RELEASE", "NOOPT")\r
+    Scopes = ('emulatorpkg', 'edk2-build')\r
+    WorkspaceRoot = os.path.realpath(os.path.join(\r
+        os.path.dirname(os.path.abspath(__file__)), "..", ".."))\r
+\r
+    # ####################################################################################### #\r
+    #                         Configuration for Update & Setup                                #\r
+    # ####################################################################################### #\r
+\r
+\r
+class SettingsManager(UpdateSettingsManager, SetupSettingsManager, PrEvalSettingsManager):\r
+\r
+    def GetPackagesSupported(self):\r
+        ''' return iterable of edk2 packages supported by this build.\r
+        These should be edk2 workspace relative paths '''\r
+        return CommonPlatform.PackagesSupported\r
+\r
+    def GetArchitecturesSupported(self):\r
+        ''' return iterable of edk2 architectures supported by this build '''\r
+        return CommonPlatform.ArchSupported\r
+\r
+    def GetTargetsSupported(self):\r
+        ''' return iterable of edk2 target tags supported by this build '''\r
+        return CommonPlatform.TargetsSupported\r
+\r
+    def GetRequiredSubmodules(self):\r
+        ''' return iterable containing RequiredSubmodule objects.\r
+        If no RequiredSubmodules return an empty iterable\r
+        '''\r
+        rs = []\r
+        # intentionally declare this one with recursive false to avoid overhead\r
+        rs.append(RequiredSubmodule(\r
+            "CryptoPkg/Library/OpensslLib/openssl", False))\r
+\r
+        # To avoid maintenance of this file for every new submodule\r
+        # lets just parse the .gitmodules and add each if not already in list.\r
+        # The GetRequiredSubmodules is designed to allow a build to optimize\r
+        # the desired submodules but it isn't necessary for this repository.\r
+        result = io.StringIO()\r
+        ret = RunCmd("git", "config --file .gitmodules --get-regexp path", workingdir=self.GetWorkspaceRoot(), outstream=result)\r
+        # Cmd output is expected to look like:\r
+        # submodule.CryptoPkg/Library/OpensslLib/openssl.path CryptoPkg/Library/OpensslLib/openssl\r
+        # submodule.SoftFloat.path ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3\r
+        if ret == 0:\r
+            for line in result.getvalue().splitlines():\r
+                _, _, path = line.partition(" ")\r
+                if path is not None:\r
+                    if path not in [x.path for x in rs]:\r
+                        rs.append(RequiredSubmodule(path, True)) # add it with recursive since we don't know\r
+        return rs\r
+\r
+    def SetArchitectures(self, list_of_requested_architectures):\r
+        ''' Confirm the requests architecture list is valid and configure SettingsManager\r
+        to run only the requested architectures.\r
+\r
+        Raise Exception if a list_of_requested_architectures is not supported\r
+        '''\r
+        unsupported = set(list_of_requested_architectures) - \\r
+            set(self.GetArchitecturesSupported())\r
+        if(len(unsupported) > 0):\r
+            errorString = (\r
+                "Unsupported Architecture Requested: " + " ".join(unsupported))\r
+            logging.critical(errorString)\r
+            raise Exception(errorString)\r
+        self.ActualArchitectures = list_of_requested_architectures\r
+\r
+    def GetWorkspaceRoot(self):\r
+        ''' get WorkspacePath '''\r
+        return CommonPlatform.WorkspaceRoot\r
+\r
+    def GetActiveScopes(self):\r
+        ''' return tuple containing scopes that should be active for this process '''\r
+        return CommonPlatform.Scopes\r
+\r
+    def FilterPackagesToTest(self, changedFilesList: list, potentialPackagesList: list) -> list:\r
+        ''' Filter other cases that this package should be built\r
+        based on changed files. This should cover things that can't\r
+        be detected as dependencies. '''\r
+        build_these_packages = []\r
+        possible_packages = potentialPackagesList.copy()\r
+        for f in changedFilesList:\r
+            # BaseTools files that might change the build\r
+            if "BaseTools" in f:\r
+                if os.path.splitext(f) not in [".txt", ".md"]:\r
+                    build_these_packages = possible_packages\r
+                    break\r
+            # if the azure pipeline platform template file changed\r
+            if "platform-build-run-steps.yml" in f:\r
+                build_these_packages = possible_packages\r
+                break\r
+        return build_these_packages\r
+\r
+    def GetPlatformDscAndConfig(self) -> tuple:\r
+        ''' If a platform desires to provide its DSC then Policy 4 will evaluate if\r
+        any of the changes will be built in the dsc.\r
+\r
+        The tuple should be (<workspace relative path to dsc file>, <input dictionary of dsc key value pairs>)\r
+        '''\r
+        return (os.path.join("EmulatorPkg", "EmulatorPkg.dsc"), {})\r
+\r
+    # ####################################################################################### #\r
+    #                         Actual Configuration for Platform Build                         #\r
+    # ####################################################################################### #\r
+\r
+\r
+class PlatformBuilder(UefiBuilder, BuildSettingsManager):\r
+    def __init__(self):\r
+        UefiBuilder.__init__(self)\r
+\r
+    def AddCommandLineOptions(self, parserObj):\r
+        ''' Add command line options to the argparser '''\r
+        parserObj.add_argument('-a', "--arch", dest="build_arch", type=str, default="X64",\r
+                               help="Optional - architecture to build.  IA32 will use IA32 for Pei & Dxe. "\r
+                               "X64 will use X64 for both PEI and DXE.")\r
+\r
+    def RetrieveCommandLineOptions(self, args):\r
+        '''  Retrieve command line options from the argparser '''\r
+\r
+        shell_environment.GetBuildVars().SetValue(\r
+            "TARGET_ARCH", args.build_arch.upper(), "From CmdLine")\r
+        shell_environment.GetBuildVars().SetValue(\r
+            "ACTIVE_PLATFORM", "EmulatorPkg/EmulatorPkg.dsc", "From CmdLine")\r
+\r
+    def GetWorkspaceRoot(self):\r
+        ''' get WorkspacePath '''\r
+        return CommonPlatform.WorkspaceRoot\r
+\r
+    def GetPackagesPath(self):\r
+        ''' Return a list of workspace relative paths that should be mapped as edk2 PackagesPath '''\r
+        return ()\r
+\r
+    def GetActiveScopes(self):\r
+        ''' return tuple containing scopes that should be active for this process '''\r
+        return CommonPlatform.Scopes\r
+\r
+    def GetName(self):\r
+        ''' Get the name of the repo, platform, or product being build '''\r
+        ''' Used for naming the log file, among others '''\r
+\r
+        # check the startup nsh flag and if set then rename the log file.\r
+        # this helps in CI so we don't overwrite the build log since running\r
+        # uses the stuart_build command.\r
+        if(shell_environment.GetBuildVars().GetValue("MAKE_STARTUP_NSH", "FALSE") == "TRUE"):\r
+            return "EmulatorPkg_With_Run"\r
+        return "EmulatorPkg"\r
+\r
+    def GetLoggingLevel(self, loggerType):\r
+        ''' Get the logging level for a given type\r
+        base == lowest logging level supported\r
+        con  == Screen logging\r
+        txt  == plain text file logging\r
+        md   == markdown file logging\r
+        '''\r
+        return logging.DEBUG\r
+\r
+    def SetPlatformEnv(self):\r
+        logging.debug("PlatformBuilder SetPlatformEnv")\r
+        self.env.SetValue("PRODUCT_NAME", "EmulatorPkg", "Platform Hardcoded")\r
+        self.env.SetValue("TOOL_CHAIN_TAG", "VS2019", "Default Toolchain")\r
+\r
+        # Add support for using the correct Platform Headers, tools, and Libs based on emulator architecture\r
+        # requested to be built when building VS2019 or VS2017\r
+        if self.env.GetValue("TOOL_CHAIN_TAG") == "VS2019" or self.env.GetValue("TOOL_CHAIN_TAG") == "VS2017":\r
+            key = self.env.GetValue("TOOL_CHAIN_TAG") + "_HOST"\r
+            if self.env.GetValue("TARGET_ARCH") == "IA32":\r
+                shell_environment.ShellEnvironment().set_shell_var(key, "x86")\r
+            elif self.env.GetValue("TARGET_ARCH") == "X64":\r
+                shell_environment.ShellEnvironment().set_shell_var(key, "x64")\r
+\r
+        # Add support for using the correct Platform Headers, tools, and Libs based on emulator architecture\r
+        # requested to be built when building on linux.\r
+        if GetHostInfo().os.upper() == "LINUX":\r
+            self.ConfigureLinuxDLinkPath()\r
+\r
+        if GetHostInfo().os.upper() == "WINDOWS":\r
+            self.env.SetValue("BLD_*_WIN_HOST_BUILD", "TRUE",\r
+                              "Trigger Windows host build")\r
+\r
+        self.env.SetValue("MAKE_STARTUP_NSH", "FALSE", "Default to false")\r
+\r
+        # I don't see what this does but it is in build.sh\r
+        key = "BLD_*_BUILD_" + self.env.GetValue("TARGET_ARCH")\r
+        self.env.SetValue(key, "TRUE", "match script in build.sh")\r
+        return 0\r
+\r
+    def PlatformPreBuild(self):\r
+        return 0\r
+\r
+    def PlatformPostBuild(self):\r
+        return 0\r
+\r
+    def FlashRomImage(self):\r
+        ''' Use the FlashRom Function to run the emulator.  This gives an easy stuart command line to\r
+        activate the emulator. '''\r
+\r
+        OutputPath = os.path.join(self.env.GetValue(\r
+            "BUILD_OUTPUT_BASE"), self.env.GetValue("TARGET_ARCH"))\r
+\r
+        if (self.env.GetValue("MAKE_STARTUP_NSH") == "TRUE"):\r
+            f = open(os.path.join(OutputPath, "startup.nsh"), "w")\r
+            f.write("BOOT SUCCESS !!! \n")\r
+            # add commands here\r
+            f.write("reset\n")\r
+            f.close()\r
+\r
+        if GetHostInfo().os.upper() == "WINDOWS":\r
+            cmd = "WinHost.exe"\r
+        elif GetHostInfo().os.upper() == "LINUX":\r
+            cmd = "./Host"\r
+        else:\r
+            logging.critical("Unsupported Host")\r
+            return -1\r
+        return RunCmd(cmd, "", workingdir=OutputPath)\r
+\r
+    def ConfigureLinuxDLinkPath(self):\r
+        '''\r
+        logic copied from build.sh to setup the correct libraries\r
+        '''\r
+        if self.env.GetValue("TARGET_ARCH") == "IA32":\r
+            LIB_NAMES = ["ld-linux.so.2", "libdl.so.2 crt1.o", "crti.o crtn.o"]\r
+            LIB_SEARCH_PATHS = ["/usr/lib/i386-linux-gnu",\r
+                                "/usr/lib32", "/lib32", "/usr/lib", "/lib"]\r
+        elif self.env.GetValue("TARGET_ARCH") == "X64":\r
+            LIB_NAMES = ["ld-linux-x86-64.so.2",\r
+                         "libdl.so.2", "crt1.o", "crti.o", "crtn.o"]\r
+            LIB_SEARCH_PATHS = ["/usr/lib/x86_64-linux-gnu",\r
+                                "/usr/lib64", "/lib64", "/usr/lib", "/lib"]\r
+\r
+        HOST_DLINK_PATHS = ""\r
+        for lname in LIB_NAMES:\r
+            logging.debug(f"Looking for {lname}")\r
+            for dname in LIB_SEARCH_PATHS:\r
+                logging.debug(f"In {dname}")\r
+                if os.path.isfile(os.path.join(dname, lname)):\r
+                    logging.debug(f"Found {lname} in {dname}")\r
+                    HOST_DLINK_PATHS += os.path.join(\r
+                        os.path.join(dname, lname)) + os.pathsep\r
+                    break\r
+        HOST_DLINK_PATHS = HOST_DLINK_PATHS.rstrip(os.pathsep)\r
+        logging.critical(f"Setting HOST_DLINK_PATHS to {HOST_DLINK_PATHS}")\r
+        shell_environment.ShellEnvironment().set_shell_var(\r
+            "HOST_DLINK_PATHS", HOST_DLINK_PATHS)\r