]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/build/build.py
BaseTools/build: Expand PREBUILD/POSTBUILD DSC actions
[mirror_edk2.git] / BaseTools / Source / Python / build / build.py
index ae8aa2fa38b655860ee69ef3e8f885aebf2a6aad..743645358205b54c5b23439f2a542d535e913534 100644 (file)
@@ -796,8 +796,6 @@ class Build():
         self.BuildModules = []\r
         self.Db_Flag = False\r
         self.LaunchPrebuildFlag = False\r
-        self.PrebuildScript = ''\r
-        self.PostbuildScript = ''\r
         self.PlatformBuildPath = os.path.join(GlobalData.gConfDirectory,'.cache', '.PlatformBuild')\r
         if BuildOptions.CommandLength:\r
             GlobalData.gCommandMaxLength = BuildOptions.CommandLength\r
@@ -819,11 +817,11 @@ class Build():
         EdkLogger.quiet("%-16s = %s" % ("CONF_PATH", GlobalData.gConfDirectory))\r
         self.InitPreBuild()\r
         self.InitPostBuild()\r
-        if self.PrebuildScript:\r
-            EdkLogger.quiet("%-16s = %s" % ("PREBUILD", self.PrebuildScript))\r
-        if self.PostbuildScript:\r
-            EdkLogger.quiet("%-16s = %s" % ("POSTBUILD", self.PostbuildScript))\r
-        if self.PrebuildScript:\r
+        if self.Prebuild:\r
+            EdkLogger.quiet("%-16s = %s" % ("PREBUILD", self.Prebuild))\r
+        if self.Postbuild:\r
+            EdkLogger.quiet("%-16s = %s" % ("POSTBUILD", self.Postbuild))\r
+        if self.Prebuild:\r
             self.LaunchPrebuild()\r
             self.TargetTxt = TargetTxtClassObject()\r
             self.ToolDef   = ToolDefClassObject()\r
@@ -890,7 +888,7 @@ class Build():
         for Tool in self.ToolChainList:\r
             if TAB_TOD_DEFINES_FAMILY not in ToolDefinition or Tool not in ToolDefinition[TAB_TOD_DEFINES_FAMILY] \\r
                or not ToolDefinition[TAB_TOD_DEFINES_FAMILY][Tool]:\r
-                EdkLogger.warn("No tool chain family found in configuration for %s. Default to MSFT." % Tool)\r
+                EdkLogger.warn("build", "No tool chain family found in configuration for %s. Default to MSFT." % Tool)\r
                 ToolChainFamily.append("MSFT")\r
             else:\r
                 ToolChainFamily.append(ToolDefinition[TAB_TOD_DEFINES_FAMILY][Tool])\r
@@ -964,16 +962,37 @@ class Build():
             Platform = self.Db._MapPlatform(str(self.PlatformFile))\r
             self.Prebuild = str(Platform.Prebuild)\r
         if self.Prebuild:\r
-            PrebuildList = self.Prebuild.split()\r
-            if not os.path.isabs(PrebuildList[0]):\r
-                PrebuildList[0] = mws.join(self.WorkspaceDir, PrebuildList[0])\r
-            if os.path.isfile(PrebuildList[0]):\r
-                self.PrebuildScript = PrebuildList[0]\r
-                self.Prebuild = ' '.join(PrebuildList)\r
-                self.Prebuild += self.PassCommandOption(self.BuildTargetList, self.ArchList, self.ToolChainList)\r
-                #self.LaunchPrebuild()\r
-            else:\r
-                EdkLogger.error("Prebuild", PREBUILD_ERROR, "the prebuild script %s is not exist.\n If you'd like to disable the Prebuild process, please use the format: -D PREBUILD=\"\" " %(PrebuildList[0]))\r
+            PrebuildList = []\r
+            #\r
+            # Evaluate all arguments and convert arguments that are WORKSPACE\r
+            # relative paths to absolute paths.  Filter arguments that look like\r
+            # flags or do not follow the file/dir naming rules to avoid false\r
+            # positives on this conversion.\r
+            #\r
+            for Arg in self.Prebuild.split():\r
+                #\r
+                # Do not modify Arg if it looks like a flag or an absolute file path\r
+                #\r
+                if Arg.startswith('-')  or os.path.isabs(Arg):\r
+                    PrebuildList.append(Arg)\r
+                    continue\r
+                #\r
+                # Do not modify Arg if it does not look like a Workspace relative\r
+                # path that starts with a valid package directory name\r
+                #\r
+                if not Arg[0].isalpha() or os.path.dirname(Arg) == '':\r
+                    PrebuildList.append(Arg)\r
+                    continue\r
+                #\r
+                # If Arg looks like a WORKSPACE relative path, then convert to an\r
+                # absolute path and check to see if the file exists.\r
+                #\r
+                Temp = mws.join(self.WorkspaceDir, Arg)\r
+                if os.path.isfile(Temp):\r
+                    Arg = Temp\r
+                PrebuildList.append(Arg)\r
+            self.Prebuild       = ' '.join(PrebuildList)\r
+            self.Prebuild += self.PassCommandOption(self.BuildTargetList, self.ArchList, self.ToolChainList, self.PlatformFile, self.Target)\r
 \r
     def InitPostBuild(self):\r
         if 'POSTBUILD' in GlobalData.gCommandLineDefines.keys():\r
@@ -982,23 +1001,46 @@ class Build():
             Platform = self.Db._MapPlatform(str(self.PlatformFile))\r
             self.Postbuild = str(Platform.Postbuild)\r
         if self.Postbuild:\r
-            PostbuildList = self.Postbuild.split()\r
-            if not os.path.isabs(PostbuildList[0]):\r
-                PostbuildList[0] = mws.join(self.WorkspaceDir, PostbuildList[0])\r
-            if os.path.isfile(PostbuildList[0]):\r
-                self.PostbuildScript = PostbuildList[0]\r
-                self.Postbuild = ' '.join(PostbuildList)\r
-                self.Postbuild += self.PassCommandOption(self.BuildTargetList, self.ArchList, self.ToolChainList)\r
-            else:\r
-                EdkLogger.error("Postbuild", POSTBUILD_ERROR, "the postbuild script %s is not exist.\n If you'd like to disable the Postbuild process, please use the format: -D POSTBUILD=\"\" " %(PostbuildList[0]))\r
-\r
-    def PassCommandOption(self, BuildTarget, TargetArch, ToolChain):\r
+            PostbuildList = []\r
+            #\r
+            # Evaluate all arguments and convert arguments that are WORKSPACE\r
+            # relative paths to absolute paths.  Filter arguments that look like\r
+            # flags or do not follow the file/dir naming rules to avoid false\r
+            # positives on this conversion.\r
+            #\r
+            for Arg in self.Postbuild.split():\r
+                #\r
+                # Do not modify Arg if it looks like a flag or an absolute file path\r
+                #\r
+                if Arg.startswith('-')  or os.path.isabs(Arg):\r
+                    PostbuildList.append(Arg)\r
+                    continue\r
+                #\r
+                # Do not modify Arg if it does not look like a Workspace relative\r
+                # path that starts with a valid package directory name\r
+                #\r
+                if not Arg[0].isalpha() or os.path.dirname(Arg) == '':\r
+                    PostbuildList.append(Arg)\r
+                    continue\r
+                #\r
+                # If Arg looks like a WORKSPACE relative path, then convert to an\r
+                # absolute path and check to see if the file exists.\r
+                #\r
+                Temp = mws.join(self.WorkspaceDir, Arg)\r
+                if os.path.isfile(Temp):\r
+                    Arg = Temp\r
+                PostbuildList.append(Arg)\r
+            self.Postbuild       = ' '.join(PostbuildList)\r
+            self.Postbuild += self.PassCommandOption(self.BuildTargetList, self.ArchList, self.ToolChainList, self.PlatformFile, self.Target)\r
+\r
+    def PassCommandOption(self, BuildTarget, TargetArch, ToolChain, PlatformFile, Target):\r
         BuildStr = ''\r
         if GlobalData.gCommand and isinstance(GlobalData.gCommand, list):\r
             BuildStr += ' ' + ' '.join(GlobalData.gCommand)\r
         TargetFlag = False\r
         ArchFlag = False\r
         ToolChainFlag = False\r
+        PlatformFileFlag = False\r
 \r
         if GlobalData.gOptions and not GlobalData.gOptions.BuildTarget:\r
             TargetFlag = True\r
@@ -1006,6 +1048,8 @@ class Build():
             ArchFlag = True\r
         if GlobalData.gOptions and not GlobalData.gOptions.ToolChain:\r
             ToolChainFlag = True\r
+        if GlobalData.gOptions and not GlobalData.gOptions.PlatformFile:\r
+            PlatformFileFlag = True\r
 \r
         if TargetFlag and BuildTarget:\r
             if isinstance(BuildTarget, list) or isinstance(BuildTarget, tuple):\r
@@ -1022,6 +1066,14 @@ class Build():
                 BuildStr += ' -t ' + ' -t '.join(ToolChain)\r
             elif isinstance(ToolChain, str):\r
                 BuildStr += ' -t ' + ToolChain\r
+        if PlatformFileFlag and PlatformFile:\r
+            if isinstance(PlatformFile, list) or isinstance(PlatformFile, tuple):\r
+                BuildStr += ' -p ' + ' -p '.join(PlatformFile)\r
+            elif isinstance(PlatformFile, str):\r
+                BuildStr += ' -p' + PlatformFile\r
+        BuildStr += ' --conf=' + GlobalData.gConfDirectory\r
+        if Target:\r
+            BuildStr += ' ' + Target\r
 \r
         return BuildStr\r
 \r
@@ -1029,6 +1081,11 @@ class Build():
         if self.Prebuild:\r
             EdkLogger.info("\n- Prebuild Start -\n")\r
             self.LaunchPrebuildFlag = True\r
+            #\r
+            # The purpose of .PrebuildEnv file is capture environment variable settings set by the prebuild script\r
+            # and preserve them for the rest of the main build step, because the child process environment will\r
+            # evaporate as soon as it exits, we cannot get it in build step.\r
+            #\r
             PrebuildEnvFile = os.path.join(GlobalData.gConfDirectory,'.cache','.PrebuildEnv')\r
             if os.path.isfile(PrebuildEnvFile):\r
                 os.remove(PrebuildEnvFile)\r
@@ -1036,7 +1093,7 @@ class Build():
                 os.remove(self.PlatformBuildPath)\r
             if sys.platform == "win32":\r
                 args = ' && '.join((self.Prebuild, 'set > ' + PrebuildEnvFile))\r
-                Process = Popen(args, stdout=PIPE, stderr=PIPE)\r
+                Process = Popen(args, stdout=PIPE, stderr=PIPE, shell=True)\r
             else:\r
                 args = ' && '.join((self.Prebuild, 'env > ' + PrebuildEnvFile))\r
                 Process = Popen(args, stdout=PIPE, stderr=PIPE, shell=True)\r
@@ -1079,7 +1136,7 @@ class Build():
         if self.Postbuild:\r
             EdkLogger.info("\n- Postbuild Start -\n")\r
             if sys.platform == "win32":\r
-                Process = Popen(self.Postbuild, stdout=PIPE, stderr=PIPE)\r
+                Process = Popen(self.Postbuild, stdout=PIPE, stderr=PIPE, shell=True)\r
             else:\r
                 Process = Popen(self.Postbuild, stdout=PIPE, stderr=PIPE, shell=True)\r
             # launch two threads to read the STDOUT and STDERR\r
@@ -1499,7 +1556,7 @@ class Build():
                         if IsIpfPlatform and ImageInfo.Image.Size % 0x2000 != 0:\r
                             ImageInfo.Image.Size = (ImageInfo.Image.Size / 0x2000 + 1) * 0x2000\r
                         RtSize += ImageInfo.Image.Size\r
-                    elif Module.ModuleType in ['SMM_CORE', 'DXE_SMM_DRIVER']:\r
+                    elif Module.ModuleType in ['SMM_CORE', 'DXE_SMM_DRIVER', 'MM_STANDALONE', 'MM_CORE_STANDALONE']:\r
                         SmmModuleList[Module.MetaFile] = ImageInfo\r
                         SmmSize += ImageInfo.Image.Size\r
                         if Module.ModuleType == 'DXE_SMM_DRIVER':\r