]>
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 - 2017, 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.
19 from Common
.String
import *
20 from Common
.DataType
import *
21 from Common
.Misc
import *
24 from MetaDataTable
import *
25 from MetaFileTable
import *
26 from MetaFileParser
import *
28 from Workspace
.DecBuildData
import DecBuildData
29 from Workspace
.DscBuildData
import DscBuildData
30 from Workspace
.InfBuildData
import InfBuildData
34 # This class defined the build database for all modules, packages and platform.
35 # It will call corresponding parser for the given file if it cannot find it in
38 # @param DbPath Path of database file
39 # @param GlobalMacros Global macros used for replacement during file parsing
40 # @prarm RenewDb=False Create new database file if it's already there
42 class WorkspaceDatabase(object):
45 # internal class used for call corresponding file parser and caching the result
46 # to avoid unnecessary re-parsing
48 class BuildObjectFactory(object):
51 ".inf" : MODEL_FILE_INF
,
52 ".dec" : MODEL_FILE_DEC
,
53 ".dsc" : MODEL_FILE_DSC
,
58 MODEL_FILE_INF
: InfParser
,
59 MODEL_FILE_DEC
: DecParser
,
60 MODEL_FILE_DSC
: DscParser
,
63 # convert to xxxBuildData object
65 MODEL_FILE_INF
: InfBuildData
,
66 MODEL_FILE_DEC
: DecBuildData
,
67 MODEL_FILE_DSC
: DscBuildData
,
70 _CACHE_
= {} # (FilePath, Arch) : <object>
73 def __init__(self
, WorkspaceDb
):
74 self
.WorkspaceDb
= WorkspaceDb
76 # key = (FilePath, Arch=None)
77 def __contains__(self
, Key
):
83 return (FilePath
, Arch
) in self
._CACHE
_
85 # key = (FilePath, Arch=None, Target=None, Toochain=None)
86 def __getitem__(self
, Key
):
102 # if it's generated before, just return the cached one
103 Key
= (FilePath
, Arch
, Target
, Toolchain
)
104 if Key
in self
._CACHE
_:
105 return self
._CACHE
_[Key
]
109 if Ext
not in self
._FILE
_TYPE
_:
111 FileType
= self
._FILE
_TYPE
_[Ext
]
112 if FileType
not in self
._GENERATOR
_:
115 # get the parser ready for this file
116 MetaFile
= self
._FILE
_PARSER
_[FileType
](
120 MetaFileStorage(self
.WorkspaceDb
.Cur
, FilePath
, FileType
)
122 # alwasy do post-process, in case of macros change
123 MetaFile
.DoPostProcess()
124 # object the build is based on
125 BuildObject
= self
._GENERATOR
_[FileType
](
133 self
._CACHE
_[Key
] = BuildObject
136 # placeholder for file format conversion
137 class TransformObjectFactory
:
138 def __init__(self
, WorkspaceDb
):
139 self
.WorkspaceDb
= WorkspaceDb
141 # key = FilePath, Arch
142 def __getitem__(self
, Key
):
145 ## Constructor of WorkspaceDatabase
147 # @param DbPath Path of database file
148 # @param GlobalMacros Global macros used for replacement during file parsing
149 # @prarm RenewDb=False Create new database file if it's already there
151 def __init__(self
, DbPath
, RenewDb
=False):
152 self
._DbClosedFlag
= False
154 DbPath
= os
.path
.normpath(mws
.join(GlobalData
.gWorkspace
, 'Conf', GlobalData
.gDatabasePath
))
156 # don't create necessary path for db in memory
157 if DbPath
!= ':memory:':
158 DbDir
= os
.path
.split(DbPath
)[0]
159 if not os
.path
.exists(DbDir
):
162 # remove db file in case inconsistency between db and file in file system
163 if self
._CheckWhetherDbNeedRenew
(RenewDb
, DbPath
):
166 # create db with optimized parameters
167 self
.Conn
= sqlite3
.connect(DbPath
, isolation_level
='DEFERRED')
168 self
.Conn
.execute("PRAGMA synchronous=OFF")
169 self
.Conn
.execute("PRAGMA temp_store=MEMORY")
170 self
.Conn
.execute("PRAGMA count_changes=OFF")
171 self
.Conn
.execute("PRAGMA cache_size=8192")
172 #self.Conn.execute("PRAGMA page_size=8192")
174 # to avoid non-ascii character conversion issue
175 self
.Conn
.text_factory
= str
176 self
.Cur
= self
.Conn
.cursor()
178 # create table for internal uses
179 self
.TblDataModel
= TableDataModel(self
.Cur
)
180 self
.TblFile
= TableFile(self
.Cur
)
183 # conversion object for build or file format conversion purpose
184 self
.BuildObject
= WorkspaceDatabase
.BuildObjectFactory(self
)
185 self
.TransformObject
= WorkspaceDatabase
.TransformObjectFactory(self
)
187 ## Check whether workspace database need to be renew.
188 # The renew reason maybe:
189 # 1) If user force to renew;
190 # 2) If user do not force renew, and
191 # a) If the time of last modified python source is newer than database file;
192 # b) If the time of last modified frozen executable file is newer than database file;
194 # @param force User force renew database
195 # @param DbPath The absolute path of workspace database file
197 # @return Bool value for whether need renew workspace databse
199 def _CheckWhetherDbNeedRenew (self
, force
, DbPath
):
200 # if database does not exist, we need do nothing
201 if not os
.path
.exists(DbPath
): return False
203 # if user force to renew database, then not check whether database is out of date
204 if force
: return True
207 # Check the time of last modified source file or build.exe
208 # if is newer than time of database, then database need to be re-created.
210 timeOfToolModified
= 0
211 if hasattr(sys
, "frozen"):
212 exePath
= os
.path
.abspath(sys
.executable
)
213 timeOfToolModified
= os
.stat(exePath
).st_mtime
215 curPath
= os
.path
.dirname(__file__
) # curPath is the path of WorkspaceDatabase.py
216 rootPath
= os
.path
.split(curPath
)[0] # rootPath is root path of python source, such as /BaseTools/Source/Python
217 if rootPath
== "" or rootPath
== None:
218 EdkLogger
.verbose("\nFail to find the root path of build.exe or python sources, so can not \
219 determine whether database file is out of date!\n")
221 # walk the root path of source or build's binary to get the time last modified.
223 for root
, dirs
, files
in os
.walk (rootPath
):
225 # bypass source control folder
226 if dir.lower() in [".svn", "_svn", "cvs"]:
230 ext
= os
.path
.splitext(file)[1]
231 if ext
.lower() == ".py": # only check .py files
232 fd
= os
.stat(os
.path
.join(root
, file))
233 if timeOfToolModified
< fd
.st_mtime
:
234 timeOfToolModified
= fd
.st_mtime
235 if timeOfToolModified
> os
.stat(DbPath
).st_mtime
:
236 EdkLogger
.verbose("\nWorkspace database is out of data!")
241 ## Initialize build database
242 def InitDatabase(self
):
243 EdkLogger
.verbose("\nInitialize build database started ...")
248 self
.TblDataModel
.Create(False)
249 self
.TblFile
.Create(False)
252 # Initialize table DataModel
254 self
.TblDataModel
.InitTable()
255 EdkLogger
.verbose("Initialize build database ... DONE!")
259 # @param Table: The instance of the table to be queried
261 def QueryTable(self
, Table
):
267 ## Close entire database
270 # Close the connection and cursor
273 if not self
._DbClosedFlag
:
277 self
._DbClosedFlag
= True
279 ## Summarize all packages in the database
280 def GetPackageList(self
, Platform
, Arch
, TargetName
, ToolChainTag
):
281 self
.Platform
= Platform
283 Pa
= self
.BuildObject
[self
.Platform
, Arch
]
285 # Get Package related to Modules
287 for Module
in Pa
.Modules
:
288 ModuleObj
= self
.BuildObject
[Module
, Arch
, TargetName
, ToolChainTag
]
289 for Package
in ModuleObj
.Packages
:
290 if Package
not in PackageList
:
291 PackageList
.append(Package
)
293 # Get Packages related to Libraries
295 for Lib
in Pa
.LibraryInstances
:
296 LibObj
= self
.BuildObject
[Lib
, Arch
, TargetName
, ToolChainTag
]
297 for Package
in LibObj
.Packages
:
298 if Package
not in PackageList
:
299 PackageList
.append(Package
)
303 ## Summarize all platforms in the database
304 def _GetPlatformList(self
):
306 for PlatformFile
in self
.TblFile
.GetFileList(MODEL_FILE_DSC
):
308 Platform
= self
.BuildObject
[PathClass(PlatformFile
), 'COMMON']
312 PlatformList
.append(Platform
)
315 def _MapPlatform(self
, Dscfile
):
316 Platform
= self
.BuildObject
[PathClass(Dscfile
), 'COMMON']
318 EdkLogger
.error('build', PARSER_ERROR
, "Failed to parser DSC file: %s" % Dscfile
)
321 PlatformList
= property(_GetPlatformList
)
325 # This acts like the main() function for the script, unless it is 'import'ed into another
328 if __name__
== '__main__':