+ if not self.Db_Flag:\r
+ self.Db.InitDatabase()\r
+\r
+ def InitPreBuild(self):\r
+ self.LoadConfiguration()\r
+ ErrorCode, ErrorInfo = self.PlatformFile.Validate(".dsc", False)\r
+ if ErrorCode != 0:\r
+ EdkLogger.error("build", ErrorCode, ExtraData=ErrorInfo)\r
+ if self.BuildTargetList:\r
+ GlobalData.gGlobalDefines['TARGET'] = self.BuildTargetList[0]\r
+ if self.ArchList:\r
+ GlobalData.gGlobalDefines['ARCH'] = self.ArchList[0]\r
+ if self.ToolChainList:\r
+ GlobalData.gGlobalDefines['TOOLCHAIN'] = self.ToolChainList[0]\r
+ GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = self.ToolChainList[0]\r
+ if self.ToolChainFamily:\r
+ GlobalData.gGlobalDefines['FAMILY'] = self.ToolChainFamily[0]\r
+ if 'PREBUILD' in GlobalData.gCommandLineDefines:\r
+ self.Prebuild = GlobalData.gCommandLineDefines.get('PREBUILD')\r
+ else:\r
+ self.Db.InitDatabase()\r
+ self.Db_Flag = True\r
+ Platform = self.Db._MapPlatform(str(self.PlatformFile))\r
+ self.Prebuild = str(Platform.Prebuild)\r
+ if self.Prebuild:\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:\r
+ self.Postbuild = GlobalData.gCommandLineDefines.get('POSTBUILD')\r
+ else:\r
+ Platform = self.Db._MapPlatform(str(self.PlatformFile))\r
+ self.Postbuild = str(Platform.Postbuild)\r
+ if self.Postbuild:\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
+ if GlobalData.gOptions and not GlobalData.gOptions.TargetArch:\r
+ 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
+ BuildStr += ' -b ' + ' -b '.join(BuildTarget)\r
+ elif isinstance(BuildTarget, str):\r
+ BuildStr += ' -b ' + BuildTarget\r
+ if ArchFlag and TargetArch:\r
+ if isinstance(TargetArch, list) or isinstance(TargetArch, tuple):\r
+ BuildStr += ' -a ' + ' -a '.join(TargetArch)\r
+ elif isinstance(TargetArch, str):\r
+ BuildStr += ' -a ' + TargetArch\r
+ if ToolChainFlag and ToolChain:\r
+ if isinstance(ToolChain, list) or isinstance(ToolChain, tuple):\r
+ 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
+ def LaunchPrebuild(self):\r
+ 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
+ if os.path.isfile(self.PlatformBuildPath):\r
+ os.remove(self.PlatformBuildPath)\r
+ if sys.platform == "win32":\r
+ args = ' && '.join((self.Prebuild, 'set > ' + PrebuildEnvFile))\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
+\r
+ # launch two threads to read the STDOUT and STDERR\r
+ EndOfProcedure = Event()\r
+ EndOfProcedure.clear()\r
+ if Process.stdout:\r
+ StdOutThread = Thread(target=ReadMessage, args=(Process.stdout, EdkLogger.info, EndOfProcedure))\r
+ StdOutThread.setName("STDOUT-Redirector")\r
+ StdOutThread.setDaemon(False)\r
+ StdOutThread.start()\r
+\r
+ if Process.stderr:\r
+ StdErrThread = Thread(target=ReadMessage, args=(Process.stderr, EdkLogger.quiet, EndOfProcedure))\r
+ StdErrThread.setName("STDERR-Redirector")\r
+ StdErrThread.setDaemon(False)\r
+ StdErrThread.start()\r
+ # waiting for program exit\r
+ Process.wait()\r
+\r
+ if Process.stdout:\r
+ StdOutThread.join()\r
+ if Process.stderr:\r
+ StdErrThread.join()\r
+ if Process.returncode != 0 :\r
+ EdkLogger.error("Prebuild", PREBUILD_ERROR, 'Prebuild process is not success!')\r
+\r
+ if os.path.exists(PrebuildEnvFile):\r
+ f = open(PrebuildEnvFile)\r
+ envs = f.readlines()\r
+ f.close()\r
+ envs = itertools.imap(lambda l: l.split('=',1), envs)\r
+ envs = itertools.ifilter(lambda l: len(l) == 2, envs)\r
+ envs = itertools.imap(lambda l: [i.strip() for i in l], envs)\r
+ os.environ.update(dict(envs))\r
+ EdkLogger.info("\n- Prebuild Done -\n")\r
+\r
+ def LaunchPostbuild(self):\r
+ if self.Postbuild:\r
+ EdkLogger.info("\n- Postbuild Start -\n")\r
+ if sys.platform == "win32":\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
+ EndOfProcedure = Event()\r
+ EndOfProcedure.clear()\r
+ if Process.stdout:\r
+ StdOutThread = Thread(target=ReadMessage, args=(Process.stdout, EdkLogger.info, EndOfProcedure))\r
+ StdOutThread.setName("STDOUT-Redirector")\r
+ StdOutThread.setDaemon(False)\r
+ StdOutThread.start()\r
+\r
+ if Process.stderr:\r
+ StdErrThread = Thread(target=ReadMessage, args=(Process.stderr, EdkLogger.quiet, EndOfProcedure))\r
+ StdErrThread.setName("STDERR-Redirector")\r
+ StdErrThread.setDaemon(False)\r
+ StdErrThread.start()\r
+ # waiting for program exit\r
+ Process.wait()\r
+\r
+ if Process.stdout:\r
+ StdOutThread.join()\r
+ if Process.stderr:\r
+ StdErrThread.join()\r
+ if Process.returncode != 0 :\r
+ EdkLogger.error("Postbuild", POSTBUILD_ERROR, 'Postbuild process is not success!')\r
+ EdkLogger.info("\n- Postbuild Done -\n")\r
+ ## Build a module or platform\r
+ #\r
+ # Create autogen code and makefile for a module or platform, and the launch\r
+ # "make" command to build it\r
+ #\r
+ # @param Target The target of build command\r
+ # @param Platform The platform file\r
+ # @param Module The module file\r
+ # @param BuildTarget The name of build target, one of "DEBUG", "RELEASE"\r
+ # @param ToolChain The name of toolchain to build\r
+ # @param Arch The arch of the module/platform\r
+ # @param CreateDepModuleCodeFile Flag used to indicate creating code\r
+ # for dependent modules/Libraries\r
+ # @param CreateDepModuleMakeFile Flag used to indicate creating makefile\r
+ # for dependent modules/Libraries\r
+ #\r
+ def _BuildPa(self, Target, AutoGenObject, CreateDepsCodeFile=True, CreateDepsMakeFile=True, BuildModule=False, FfsCommand={}):\r
+ if AutoGenObject is None:\r
+ return False\r
+\r
+ # skip file generation for cleanxxx targets, run and fds target\r
+ if Target not in ['clean', 'cleanlib', 'cleanall', 'run', 'fds']:\r
+ # for target which must generate AutoGen code and makefile\r
+ if not self.SkipAutoGen or Target == 'genc':\r
+ self.Progress.Start("Generating code")\r
+ AutoGenObject.CreateCodeFile(CreateDepsCodeFile)\r
+ self.Progress.Stop("done!")\r
+ if Target == "genc":\r
+ return True\r
+\r
+ if not self.SkipAutoGen or Target == 'genmake':\r
+ self.Progress.Start("Generating makefile")\r
+ AutoGenObject.CreateMakeFile(CreateDepsMakeFile, FfsCommand)\r
+ self.Progress.Stop("done!")\r
+ if Target == "genmake":\r
+ return True\r
+ else:\r
+ # always recreate top/platform makefile when clean, just in case of inconsistency\r
+ AutoGenObject.CreateCodeFile(False)\r
+ AutoGenObject.CreateMakeFile(False)\r
+\r
+ if EdkLogger.GetLevel() == EdkLogger.QUIET:\r
+ EdkLogger.quiet("Building ... %s" % repr(AutoGenObject))\r
+\r
+ BuildCommand = AutoGenObject.BuildCommand\r
+ if BuildCommand is None or len(BuildCommand) == 0:\r
+ EdkLogger.error("build", OPTION_MISSING,\r
+ "No build command found for this module. "\r
+ "Please check your setting of %s_%s_%s_MAKE_PATH in Conf/tools_def.txt file." %\r
+ (AutoGenObject.BuildTarget, AutoGenObject.ToolChain, AutoGenObject.Arch),\r
+ ExtraData=str(AutoGenObject))\r
+\r
+ makefile = GenMake.BuildFile(AutoGenObject)._FILE_NAME_[GenMake.gMakeType]\r
+\r
+ # run\r
+ if Target == 'run':\r
+ RunDir = os.path.normpath(os.path.join(AutoGenObject.BuildDir, GlobalData.gGlobalDefines['ARCH']))\r
+ Command = '.\SecMain'\r
+ os.chdir(RunDir)\r
+ LaunchCommand(Command, RunDir)\r
+ return True\r
+\r
+ # build modules\r
+ if BuildModule:\r
+ BuildCommand = BuildCommand + [Target]\r
+ LaunchCommand(BuildCommand, AutoGenObject.MakeFileDir)\r
+ self.CreateAsBuiltInf()\r
+ return True\r
+\r
+ # build library\r
+ if Target == 'libraries':\r
+ for Lib in AutoGenObject.LibraryBuildDirectoryList:\r
+ NewBuildCommand = BuildCommand + ['-f', os.path.normpath(os.path.join(Lib, makefile)), 'pbuild']\r
+ LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)\r
+ return True\r
+\r
+ # build module\r
+ if Target == 'modules':\r
+ for Lib in AutoGenObject.LibraryBuildDirectoryList:\r
+ NewBuildCommand = BuildCommand + ['-f', os.path.normpath(os.path.join(Lib, makefile)), 'pbuild']\r
+ LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)\r
+ for Mod in AutoGenObject.ModuleBuildDirectoryList:\r
+ NewBuildCommand = BuildCommand + ['-f', os.path.normpath(os.path.join(Mod, makefile)), 'pbuild']\r
+ LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)\r
+ self.CreateAsBuiltInf()\r
+ return True\r
+\r
+ # cleanlib\r
+ if Target == 'cleanlib':\r
+ for Lib in AutoGenObject.LibraryBuildDirectoryList:\r
+ LibMakefile = os.path.normpath(os.path.join(Lib, makefile))\r
+ if os.path.exists(LibMakefile):\r
+ NewBuildCommand = BuildCommand + ['-f', LibMakefile, 'cleanall']\r
+ LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)\r
+ return True\r
+\r
+ # clean\r
+ if Target == 'clean':\r
+ for Mod in AutoGenObject.ModuleBuildDirectoryList:\r
+ ModMakefile = os.path.normpath(os.path.join(Mod, makefile))\r
+ if os.path.exists(ModMakefile):\r
+ NewBuildCommand = BuildCommand + ['-f', ModMakefile, 'cleanall']\r
+ LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)\r
+ for Lib in AutoGenObject.LibraryBuildDirectoryList:\r
+ LibMakefile = os.path.normpath(os.path.join(Lib, makefile))\r
+ if os.path.exists(LibMakefile):\r
+ NewBuildCommand = BuildCommand + ['-f', LibMakefile, 'cleanall']\r
+ LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)\r
+ return True\r
+\r
+ # cleanall\r
+ if Target == 'cleanall':\r
+ try:\r
+ #os.rmdir(AutoGenObject.BuildDir)\r
+ RemoveDirectory(AutoGenObject.BuildDir, True)\r
+ except WindowsError as X:\r
+ EdkLogger.error("build", FILE_DELETE_FAILURE, ExtraData=str(X))\r
+ return True\r