From nobody Thu Dec 26 00:31:50 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1505802978707880.6598453607825; Mon, 18 Sep 2017 23:36:18 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 2F4DE21D492FD; Mon, 18 Sep 2017 23:33:13 -0700 (PDT) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id A0C1121CEB115 for ; Mon, 18 Sep 2017 23:33:12 -0700 (PDT) Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga105.fm.intel.com with ESMTP; 18 Sep 2017 23:36:16 -0700 Received: from shwdeopenpsi168.ccr.corp.intel.com ([10.239.158.121]) by fmsmga005.fm.intel.com with ESMTP; 18 Sep 2017 23:36:15 -0700 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.42,416,1500966000"; d="scan'208";a="152903809" From: Yonghong Zhu To: edk2-devel@lists.01.org Date: Tue, 19 Sep 2017 14:36:12 +0800 Message-Id: <1505802972-18168-1-git-send-email-yonghong.zhu@intel.com> X-Mailer: git-send-email 2.6.1.windows.1 Subject: [edk2] [Patch V2] BaseTools: add support for BIOS build with binary cache X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Liming Gao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" V2: change the option name to --binary-destination and --binary-source. Add three new options: --hash enables hash-based caching during build process. when --hash is enabled, build tool will base on the module hash value to do the incremental build, without --hash, build tool will base on the timestamp to do the incremental build. --hash option use md5 method to get every hash value, DSC/FDF, tools_def.txt, build_rule.txt and build command are calculated as global hash value, Package DEC and its include header files are calculated as package hash value, Module source files and its INF file are calculated as module hash value. Library hash value will combine the global hash value and its dependent package hash value. Driver hash value will combine the global hash value, its dependent package hash value and its linked library hash value. When --hash and --binary-destination are specified, build tool will copy generated binary files for each module into the directory specified by binary-destination at the build phase. Binary-destination directory caches all generated binary files. When --hash and --binary-source are specified, build tool will try to get the binary files from the binary source directory at the build phase.If the cached binary has the same hash value, it will be directly used. Otherwise, build tool will compile the source files and generate the binary files. Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Yonghong Zhu --- BaseTools/Source/Python/AutoGen/AutoGen.py | 161 +++++++++++++++++++++++= ++-- BaseTools/Source/Python/Common/GlobalData.py | 7 ++ BaseTools/Source/Python/build/build.py | 30 +++++ 3 files changed, 189 insertions(+), 9 deletions(-) diff --git a/BaseTools/Source/Python/AutoGen/AutoGen.py b/BaseTools/Source/= Python/AutoGen/AutoGen.py index 1a8c0d9..585c382 100644 --- a/BaseTools/Source/Python/AutoGen/AutoGen.py +++ b/BaseTools/Source/Python/AutoGen/AutoGen.py @@ -41,10 +41,11 @@ import Common.VpdInfoFile as VpdInfoFile from GenPcdDb import CreatePcdDatabaseCode from Workspace.MetaFileCommentParser import UsageList from Common.MultipleWorkspace import MultipleWorkspace as mws import InfSectionParser import datetime +import hashlib =20 ## Regular expression for splitting Dependency Expression string into toke= ns gDepexTokenPattern =3D re.compile("(\(|\)|\w+| \S+\.inf)") =20 # @@ -263,10 +264,14 @@ class WorkspaceAutoGen(AutoGen): self.FdfFile =3D FlashDefinitionFile self.FdTargetList =3D Fds self.FvTargetList =3D Fvs self.CapTargetList =3D Caps self.AutoGenObjectList =3D [] + self._BuildDir =3D None + self._FvDir =3D None + self._MakeFileDir =3D None + self._BuildCommand =3D None =20 # there's many relative directory operations, so ... os.chdir(self.WorkspaceDir) =20 # @@ -642,10 +647,18 @@ class WorkspaceAutoGen(AutoGen): # Pa.CollectPlatformDynamicPcds() Pa.CollectFixedAtBuildPcds() self.AutoGenObjectList.append(Pa) =20 + # + # Generate Package level hash value + # + GlobalData.gPackageHash[Arch] =3D {} + if GlobalData.gUseHashCache: + for Pkg in Pkgs: + self._GenPkgLevelHash(Pkg) + # # Check PCDs token value conflict in each DEC file. # self._CheckAllPcdsTokenValueConflict() =20 @@ -655,15 +668,10 @@ class WorkspaceAutoGen(AutoGen): self._CheckPcdDefineAndType() =20 # if self.FdfFile: # self._CheckDuplicateInFV(Fdf) =20 - self._BuildDir =3D None - self._FvDir =3D None - self._MakeFileDir =3D None - self._BuildCommand =3D None - # # Create BuildOptions Macro & PCD metafile, also add the Active Pl= atform and FDF file. # content =3D 'gCommandLineDefines: ' content +=3D str(GlobalData.gCommandLineDefines) @@ -675,10 +683,14 @@ class WorkspaceAutoGen(AutoGen): content +=3D str(self.Platform) content +=3D os.linesep if self.FdfFile: content +=3D 'Flash Image Definition: ' content +=3D str(self.FdfFile) + content +=3D os.linesep + if GlobalData.gBinCacheDest: + content +=3D 'Cache of .efi location: ' + content +=3D str(GlobalData.gBinCacheDest) SaveFileOnChange(os.path.join(self.BuildDir, 'BuildOptions'), cont= ent, False) =20 # # Create PcdToken Number file for Dynamic/DynamicEx Pcd. # @@ -704,10 +716,22 @@ class WorkspaceAutoGen(AutoGen): for f in AllWorkSpaceMetaFiles: if os.stat(f)[8] > SrcTimeStamp: SrcTimeStamp =3D os.stat(f)[8] self._SrcTimeStamp =3D SrcTimeStamp =20 + if GlobalData.gUseHashCache: + m =3D hashlib.md5() + for files in AllWorkSpaceMetaFiles: + if files.endswith('.dec'): + continue + f =3D open(files, 'r') + Content =3D f.read() + f.close() + m.update(Content) + SaveFileOnChange(os.path.join(self.BuildDir, 'AutoGen.hash'), = m.hexdigest(), True) + GlobalData.gPlatformHash =3D m.hexdigest() + # # Write metafile list to build directory # AutoGenFilePath =3D os.path.join(self.BuildDir, 'AutoGen') if os.path.exists (AutoGenFilePath): @@ -717,10 +741,33 @@ class WorkspaceAutoGen(AutoGen): with open(os.path.join(self.BuildDir, 'AutoGen'), 'w+') as file: for f in AllWorkSpaceMetaFiles: print >> file, f return True =20 + def _GenPkgLevelHash(self, Pkg): + PkgDir =3D os.path.join(self.BuildDir, Pkg.Arch, Pkg.PackageName) + CreateDirectory(PkgDir) + HashFile =3D os.path.join(PkgDir, Pkg.PackageName + '.hash') + m =3D hashlib.md5() + # Get .dec file's hash value + f =3D open(Pkg.MetaFile.Path, 'r') + Content =3D f.read() + f.close() + m.update(Content) + # Get include files hash value + if Pkg.Includes: + for inc in Pkg.Includes: + for Root, Dirs, Files in os.walk(str(inc)): + for File in Files: + File_Path =3D os.path.join(Root, File) + f =3D open(File_Path, 'r') + Content =3D f.read() + f.close() + m.update(Content) + SaveFileOnChange(HashFile, m.hexdigest(), True) + if Pkg.PackageName not in GlobalData.gPackageHash[Pkg.Arch]: + GlobalData.gPackageHash[Pkg.Arch][Pkg.PackageName] =3D m.hexdi= gest() =20 def _GetMetaFiles(self, Target, Toolchain, Arch): AllWorkSpaceMetaFiles =3D set() # # add fdf @@ -954,11 +1001,12 @@ class WorkspaceAutoGen(AutoGen): self._FvDir =3D path.join(self.BuildDir, 'FV') return self._FvDir =20 ## Return the directory to store all intermediate and final files built def _GetBuildDir(self): - return self.AutoGenObjectList[0].BuildDir + if self._BuildDir =3D=3D None: + return self.AutoGenObjectList[0].BuildDir =20 ## Return the build output directory platform specifies def _GetOutputDir(self): return self.Platform.OutputDirectory =20 @@ -3908,37 +3956,45 @@ class ModuleAutoGen(AutoGen): AsBuiltInfDict['module_uefi_specification_version'] +=3D [self= .Specification['UEFI_SPECIFICATION_VERSION']] if 'PI_SPECIFICATION_VERSION' in self.Specification: AsBuiltInfDict['module_pi_specification_version'] +=3D [self.S= pecification['PI_SPECIFICATION_VERSION']] =20 OutputDir =3D self.OutputDir.replace('\\', '/').strip('/') - + self.OutputFile =3D [] for Item in self.CodaTargetList: File =3D Item.Target.Path.replace('\\', '/').strip('/').replac= e(OutputDir, '').strip('/') + if File not in self.OutputFile: + self.OutputFile.append(File) if Item.Target.Ext.lower() =3D=3D '.aml': AsBuiltInfDict['binary_item'] +=3D ['ASL|' + File] elif Item.Target.Ext.lower() =3D=3D '.acpi': AsBuiltInfDict['binary_item'] +=3D ['ACPI|' + File] elif Item.Target.Ext.lower() =3D=3D '.efi': AsBuiltInfDict['binary_item'] +=3D ['PE32|' + self.Name + = '.efi'] else: AsBuiltInfDict['binary_item'] +=3D ['BIN|' + File] if self.DepexGenerated: + if self.Name + '.depex' not in self.OutputFile: + self.OutputFile.append(self.Name + '.depex') if self.ModuleType in ['PEIM']: AsBuiltInfDict['binary_item'] +=3D ['PEI_DEPEX|' + self.Na= me + '.depex'] if self.ModuleType in ['DXE_DRIVER', 'DXE_RUNTIME_DRIVER', 'DX= E_SAL_DRIVER', 'UEFI_DRIVER']: AsBuiltInfDict['binary_item'] +=3D ['DXE_DEPEX|' + self.Na= me + '.depex'] if self.ModuleType in ['DXE_SMM_DRIVER']: AsBuiltInfDict['binary_item'] +=3D ['SMM_DEPEX|' + self.Na= me + '.depex'] =20 Bin =3D self._GenOffsetBin() if Bin: AsBuiltInfDict['binary_item'] +=3D ['BIN|%s' % Bin] + if Bin not in self.OutputFile: + self.OutputFile.append(Bin) =20 for Root, Dirs, Files in os.walk(OutputDir): for File in Files: if File.lower().endswith('.pdb'): AsBuiltInfDict['binary_item'] +=3D ['DISPOSABLE|' + Fi= le] + if File not in self.OutputFile: + self.OutputFile.append(File) HeaderComments =3D self.Module.HeaderComments StartPos =3D 0 for Index in range(len(HeaderComments)): if HeaderComments[Index].find('@BinaryHeader') !=3D -1: HeaderComments[Index] =3D HeaderComments[Index].replace('@= BinaryHeader', '@file') @@ -4115,11 +4171,52 @@ class ModuleAutoGen(AutoGen): AsBuiltInf.Append(gAsBuiltInfHeaderString.Replace(AsBuiltInfDict)) =20 SaveFileOnChange(os.path.join(self.OutputDir, self.Name + '.inf'),= str(AsBuiltInf), False) =20 self.IsAsBuiltInfCreated =3D True - =20 + if GlobalData.gBinCacheDest: + self.CopyModuleToCache() + + def CopyModuleToCache(self): + FileDir =3D path.join(GlobalData.gBinCacheDest, self.Arch, self.So= urceDir, self.MetaFile.BaseName) + CreateDirectory (FileDir) + HashFile =3D path.join(self.BuildDir, self.Name + '.hash') + ModuleFile =3D path.join(self.OutputDir, self.Name + '.inf') + if os.path.exists(HashFile): + shutil.copy2(HashFile, FileDir) + if os.path.exists(ModuleFile): + shutil.copy2(ModuleFile, FileDir) + if self.OutputFile: + for File in self.OutputFile: + if not os.path.isabs(File): + File =3D os.path.join(self.OutputDir, File) + if os.path.exists(File): + shutil.copy2(File, FileDir) + + def AttemptModuleCacheCopy(self): + if self.IsBinaryModule: + return False + FileDir =3D path.join(GlobalData.gBinCacheSource, self.Arch, self.= SourceDir, self.MetaFile.BaseName) + HashFile =3D path.join(FileDir, self.Name + '.hash') + if os.path.exists(HashFile): + f =3D open(HashFile, 'r') + CacheHash =3D f.read() + f.close() + if GlobalData.gModuleHash[self.Arch][self.Name]: + if CacheHash =3D=3D GlobalData.gModuleHash[self.Arch][self= .Name]: + for root, dir, files in os.walk(FileDir): + for f in files: + if self.Name + '.hash' in f: + shutil.copy2(HashFile, self.BuildDir) + else: + File =3D path.join(root, f) + shutil.copy2(File, self.OutputDir) + if self.Name =3D=3D "PcdPeim" or self.Name =3D=3D "Pcd= Dxe": + CreatePcdDatabaseCode(self, TemplateString(), Temp= lateString()) + return True + return False + ## Create makefile for the module and its dependent libraries # # @param CreateLibraryMakeFile Flag indicating if or not the = makefiles of # dependent libraries will be cr= eated # @@ -4243,12 +4340,58 @@ class ModuleAutoGen(AutoGen): self._LibraryAutoGenList.append(La) for Lib in La.CodaTargetList: self._ApplyBuildRule(Lib.Target, TAB_UNKNOWN_FILE) return self._LibraryAutoGenList =20 + def GenModuleHash(self): + if self.Arch not in GlobalData.gModuleHash: + GlobalData.gModuleHash[self.Arch] =3D {} + m =3D hashlib.md5() + # Add Platform level hash + m.update(GlobalData.gPlatformHash) + # Add Package level hash + if self.DependentPackageList: + for Pkg in self.DependentPackageList: + if Pkg.PackageName in GlobalData.gPackageHash[self.Arch]: + m.update(GlobalData.gPackageHash[self.Arch][Pkg.Packag= eName]) + + # Add Library hash + if self.LibraryAutoGenList: + for Lib in self.LibraryAutoGenList: + if Lib.Name not in GlobalData.gModuleHash[self.Arch]: + Lib.GenModuleHash() + m.update(GlobalData.gModuleHash[self.Arch][Lib.Name]) + + # Add Module self + f =3D open(str(self.MetaFile), 'r') + Content =3D f.read() + f.close() + m.update(Content) + # Add Module's source files + if self.SourceFileList: + for File in self.SourceFileList: + f =3D open(str(File), 'r') + Content =3D f.read() + f.close() + m.update(Content) + + ModuleHashFile =3D path.join(self.BuildDir, self.Name + ".hash") + if self.Name not in GlobalData.gModuleHash[self.Arch]: + GlobalData.gModuleHash[self.Arch][self.Name] =3D m.hexdigest() + if GlobalData.gBinCacheSource: + CacheValid =3D self.AttemptModuleCacheCopy() + if CacheValid: + return False + return SaveFileOnChange(ModuleHashFile, m.hexdigest(), True) + + ## Decide whether we can skip the ModuleAutoGen process + def CanSkipbyHash(self): + if GlobalData.gUseHashCache: + return not self.GenModuleHash() + ## Decide whether we can skip the ModuleAutoGen process - # If any source file is newer than the modeule than we cannot skip + # If any source file is newer than the module than we cannot skip # def CanSkip(self): if not os.path.exists(self.GetTimeStampPath()): return False #last creation time of the module diff --git a/BaseTools/Source/Python/Common/GlobalData.py b/BaseTools/Sourc= e/Python/Common/GlobalData.py index 667877e..45e7ea0 100644 --- a/BaseTools/Source/Python/Common/GlobalData.py +++ b/BaseTools/Source/Python/Common/GlobalData.py @@ -85,5 +85,12 @@ BuildOptionPcd =3D [] # MixedPcd =3D {} =20 # Pcd name for the Pcd which used in the Conditional directives gConditionalPcds =3D [] + +gUseHashCache =3D None +gBinCacheDest =3D None +gBinCacheSource =3D None +gPlatformHash =3D None +gPackageHash =3D {} +gModuleHash =3D {} diff --git a/BaseTools/Source/Python/build/build.py b/BaseTools/Source/Pyth= on/build/build.py index bb70a58..d9105be 100644 --- a/BaseTools/Source/Python/build/build.py +++ b/BaseTools/Source/Python/build/build.py @@ -764,10 +764,34 @@ class Build(): self.TargetTxt =3D TargetTxtClassObject() self.ToolDef =3D ToolDefClassObject() GlobalData.BuildOptionPcd =3D BuildOptions.OptionPcd #Set global flag for build mode GlobalData.gIgnoreSource =3D BuildOptions.IgnoreSources + GlobalData.gUseHashCache =3D BuildOptions.UseHashCache + GlobalData.gBinCacheDest =3D BuildOptions.BinCacheDest + GlobalData.gBinCacheSource =3D BuildOptions.BinCacheSource + + if GlobalData.gBinCacheDest and not GlobalData.gUseHashCache: + EdkLogger.error("build", OPTION_NOT_SUPPORTED, ExtraData=3D"--= binary-destination must be used together with --hash.") + + if GlobalData.gBinCacheSource and not GlobalData.gUseHashCache: + EdkLogger.error("build", OPTION_NOT_SUPPORTED, ExtraData=3D"--= binary-source must be used together with --hash.") + + if GlobalData.gBinCacheDest and GlobalData.gBinCacheSource: + EdkLogger.error("build", OPTION_NOT_SUPPORTED, ExtraData=3D"--= binary-destination can not be used together with --binary-source.") + + if GlobalData.gBinCacheSource: + BinCacheSource =3D os.path.normpath(GlobalData.gBinCacheSource) + if not os.path.isabs(BinCacheSource): + BinCacheSource =3D mws.join(self.WorkspaceDir, BinCacheSou= rce) + GlobalData.gBinCacheSource =3D BinCacheSource + + if GlobalData.gBinCacheDest: + BinCacheDest =3D os.path.normpath(GlobalData.gBinCacheDest) + if not os.path.isabs(BinCacheDest): + BinCacheDest =3D mws.join(self.WorkspaceDir, BinCacheDest) + GlobalData.gBinCacheDest =3D BinCacheDest =20 if self.ConfDirectory: # Get alternate Conf location, if it is absolute, then just us= e the absolute directory name ConfDirectoryPath =3D os.path.normpath(self.ConfDirectory) =20 @@ -1908,10 +1932,13 @@ class Build(): # Get ModuleAutoGen object to generate C code file= and makefile Ma =3D ModuleAutoGen(Wa, Module, BuildTarget, Tool= Chain, Arch, self.PlatformFile) =20 if Ma =3D=3D None: continue + if Ma.CanSkipbyHash(): + continue + # Not to auto-gen for targets 'clean', 'cleanlib',= 'cleanall', 'run', 'fds' if self.Target not in ['clean', 'cleanlib', 'clean= all', 'run', 'fds']: # for target which must generate AutoGen code = and makefile if not self.SkipAutoGen or self.Target =3D=3D = 'genc': Ma.CreateCodeFile(True) @@ -2213,10 +2240,13 @@ def MyOptionParser(): Parser.add_option("--conf", action=3D"store", type=3D"string", dest=3D= "ConfDirectory", help=3D"Specify the customized Conf directory.") Parser.add_option("--check-usage", action=3D"store_true", dest=3D"Chec= kUsage", default=3DFalse, help=3D"Check usage content of entries listed in = INF file.") Parser.add_option("--ignore-sources", action=3D"store_true", dest=3D"I= gnoreSources", default=3DFalse, help=3D"Focus to a binary build and ignore = all source files") Parser.add_option("--pcd", action=3D"append", dest=3D"OptionPcd", help= =3D"Set PCD value by command line. Format: \"PcdName=3DValue\" ") Parser.add_option("-l", "--cmd-len", action=3D"store", type=3D"int", d= est=3D"CommandLength", help=3D"Specify the maximum line length of build com= mand. Default is 4096.") + Parser.add_option("--hash", action=3D"store_true", dest=3D"UseHashCach= e", default=3DFalse, help=3D"Enable hash-based caching during build process= .") + Parser.add_option("--binary-destination", action=3D"store", type=3D"st= ring", dest=3D"BinCacheDest", help=3D"Generate a cache of binary files in t= he specified directory.") + Parser.add_option("--binary-source", action=3D"store", type=3D"string"= , dest=3D"BinCacheSource", help=3D"Consume a cache of binary files from the= specified directory.") =20 (Opt, Args) =3D Parser.parse_args() return (Opt, Args) =20 ## Tool entrance method --=20 2.6.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel