]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/WorkspaceDatabase.py
2 # This file is used to create a database used by build tool
4 # Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
5 # (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 # This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 from __future__
import absolute_import
20 from Common
.StringUtils
import *
21 from Common
.DataType
import *
22 from Common
.Misc
import *
25 from .MetaDataTable
import *
26 from .MetaFileTable
import *
27 from .MetaFileParser
import *
29 from Workspace
.DecBuildData
import DecBuildData
30 from Workspace
.DscBuildData
import DscBuildData
31 from Workspace
.InfBuildData
import InfBuildData
35 # This class defined the build database for all modules, packages and platform.
36 # It will call corresponding parser for the given file if it cannot find it in
39 # @param DbPath Path of database file
40 # @param GlobalMacros Global macros used for replacement during file parsing
41 # @prarm RenewDb=False Create new database file if it's already there
43 class WorkspaceDatabase(object):
46 # internal class used for call corresponding file parser and caching the result
47 # to avoid unnecessary re-parsing
49 class BuildObjectFactory(object):
52 ".inf" : MODEL_FILE_INF
,
53 ".dec" : MODEL_FILE_DEC
,
54 ".dsc" : MODEL_FILE_DSC
,
59 MODEL_FILE_INF
: InfParser
,
60 MODEL_FILE_DEC
: DecParser
,
61 MODEL_FILE_DSC
: DscParser
,
64 # convert to xxxBuildData object
66 MODEL_FILE_INF
: InfBuildData
,
67 MODEL_FILE_DEC
: DecBuildData
,
68 MODEL_FILE_DSC
: DscBuildData
,
71 _CACHE_
= {} # (FilePath, Arch) : <object>
74 def __init__(self
, WorkspaceDb
):
75 self
.WorkspaceDb
= WorkspaceDb
77 # key = (FilePath, Arch=None)
78 def __contains__(self
, Key
):
84 return (FilePath
, Arch
) in self
._CACHE
_
86 # key = (FilePath, Arch=None, Target=None, Toochain=None)
87 def __getitem__(self
, Key
):
103 # if it's generated before, just return the cached one
104 Key
= (FilePath
, Arch
, Target
, Toolchain
)
105 if Key
in self
._CACHE
_:
106 return self
._CACHE
_[Key
]
110 if Ext
not in self
._FILE
_TYPE
_:
112 FileType
= self
._FILE
_TYPE
_[Ext
]
113 if FileType
not in self
._GENERATOR
_:
116 # get the parser ready for this file
117 MetaFile
= self
._FILE
_PARSER
_[FileType
](
121 MetaFileStorage(self
.WorkspaceDb
.Cur
, FilePath
, FileType
)
123 # alwasy do post-process, in case of macros change
124 MetaFile
.DoPostProcess()
125 # object the build is based on
126 BuildObject
= self
._GENERATOR
_[FileType
](
134 self
._CACHE
_[Key
] = BuildObject
137 # placeholder for file format conversion
138 class TransformObjectFactory
:
139 def __init__(self
, WorkspaceDb
):
140 self
.WorkspaceDb
= WorkspaceDb
142 # key = FilePath, Arch
143 def __getitem__(self
, Key
):
146 ## Constructor of WorkspaceDatabase
148 # @param DbPath Path of database file
149 # @param GlobalMacros Global macros used for replacement during file parsing
150 # @prarm RenewDb=False Create new database file if it's already there
152 def __init__(self
, DbPath
, RenewDb
=False):
153 self
._DbClosedFlag
= False
155 DbPath
= os
.path
.normpath(mws
.join(GlobalData
.gWorkspace
, 'Conf', GlobalData
.gDatabasePath
))
157 # don't create necessary path for db in memory
158 if DbPath
!= ':memory:':
159 DbDir
= os
.path
.split(DbPath
)[0]
160 if not os
.path
.exists(DbDir
):
163 # remove db file in case inconsistency between db and file in file system
164 if self
._CheckWhetherDbNeedRenew
(RenewDb
, DbPath
):
167 # create db with optimized parameters
168 self
.Conn
= sqlite3
.connect(DbPath
, isolation_level
='DEFERRED')
169 self
.Conn
.execute("PRAGMA synchronous=OFF")
170 self
.Conn
.execute("PRAGMA temp_store=MEMORY")
171 self
.Conn
.execute("PRAGMA count_changes=OFF")
172 self
.Conn
.execute("PRAGMA cache_size=8192")
173 #self.Conn.execute("PRAGMA page_size=8192")
175 # to avoid non-ascii character conversion issue
176 self
.Conn
.text_factory
= str
177 self
.Cur
= self
.Conn
.cursor()
179 # create table for internal uses
180 self
.TblDataModel
= TableDataModel(self
.Cur
)
181 self
.TblFile
= TableFile(self
.Cur
)
184 # conversion object for build or file format conversion purpose
185 self
.BuildObject
= WorkspaceDatabase
.BuildObjectFactory(self
)
186 self
.TransformObject
= WorkspaceDatabase
.TransformObjectFactory(self
)
188 ## Check whether workspace database need to be renew.
189 # The renew reason maybe:
190 # 1) If user force to renew;
191 # 2) If user do not force renew, and
192 # a) If the time of last modified python source is newer than database file;
193 # b) If the time of last modified frozen executable file is newer than database file;
195 # @param force User force renew database
196 # @param DbPath The absolute path of workspace database file
198 # @return Bool value for whether need renew workspace databse
200 def _CheckWhetherDbNeedRenew (self
, force
, DbPath
):
201 # if database does not exist, we need do nothing
202 if not os
.path
.exists(DbPath
): return False
204 # if user force to renew database, then not check whether database is out of date
205 if force
: return True
208 # Check the time of last modified source file or build.exe
209 # if is newer than time of database, then database need to be re-created.
211 timeOfToolModified
= 0
212 if hasattr(sys
, "frozen"):
213 exePath
= os
.path
.abspath(sys
.executable
)
214 timeOfToolModified
= os
.stat(exePath
).st_mtime
216 curPath
= os
.path
.dirname(__file__
) # curPath is the path of WorkspaceDatabase.py
217 rootPath
= os
.path
.split(curPath
)[0] # rootPath is root path of python source, such as /BaseTools/Source/Python
218 if rootPath
== "" or rootPath
is None:
219 EdkLogger
.verbose("\nFail to find the root path of build.exe or python sources, so can not \
220 determine whether database file is out of date!\n")
222 # walk the root path of source or build's binary to get the time last modified.
224 for root
, dirs
, files
in os
.walk (rootPath
):
226 # bypass source control folder
227 if dir.lower() in [".svn", "_svn", "cvs"]:
231 ext
= os
.path
.splitext(file)[1]
232 if ext
.lower() == ".py": # only check .py files
233 fd
= os
.stat(os
.path
.join(root
, file))
234 if timeOfToolModified
< fd
.st_mtime
:
235 timeOfToolModified
= fd
.st_mtime
236 if timeOfToolModified
> os
.stat(DbPath
).st_mtime
:
237 EdkLogger
.verbose("\nWorkspace database is out of data!")
242 ## Initialize build database
243 def InitDatabase(self
):
244 EdkLogger
.verbose("\nInitialize build database started ...")
249 self
.TblDataModel
.Create(False)
250 self
.TblFile
.Create(False)
253 # Initialize table DataModel
255 self
.TblDataModel
.InitTable()
256 EdkLogger
.verbose("Initialize build database ... DONE!")
260 # @param Table: The instance of the table to be queried
262 def QueryTable(self
, Table
):
268 ## Close entire database
271 # Close the connection and cursor
274 if not self
._DbClosedFlag
:
278 self
._DbClosedFlag
= True
280 ## Summarize all packages in the database
281 def GetPackageList(self
, Platform
, Arch
, TargetName
, ToolChainTag
):
282 self
.Platform
= Platform
284 Pa
= self
.BuildObject
[self
.Platform
, Arch
, TargetName
, ToolChainTag
]
286 # Get Package related to Modules
288 for Module
in Pa
.Modules
:
289 ModuleObj
= self
.BuildObject
[Module
, Arch
, TargetName
, ToolChainTag
]
290 for Package
in ModuleObj
.Packages
:
291 if Package
not in PackageList
:
292 PackageList
.append(Package
)
294 # Get Packages related to Libraries
296 for Lib
in Pa
.LibraryInstances
:
297 LibObj
= self
.BuildObject
[Lib
, Arch
, TargetName
, ToolChainTag
]
298 for Package
in LibObj
.Packages
:
299 if Package
not in PackageList
:
300 PackageList
.append(Package
)
304 ## Summarize all platforms in the database
305 def _GetPlatformList(self
):
307 for PlatformFile
in self
.TblFile
.GetFileList(MODEL_FILE_DSC
):
309 Platform
= self
.BuildObject
[PathClass(PlatformFile
), TAB_COMMON
]
312 if Platform
is not None:
313 PlatformList
.append(Platform
)
316 def _MapPlatform(self
, Dscfile
):
317 Platform
= self
.BuildObject
[PathClass(Dscfile
), TAB_COMMON
]
319 EdkLogger
.error('build', PARSER_ERROR
, "Failed to parser DSC file: %s" % Dscfile
)
322 PlatformList
= property(_GetPlatformList
)
326 # This acts like the main() function for the script, unless it is 'import'ed into another
329 if __name__
== '__main__':