]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Eot/InfParserLite.py
BaseTools/EOT: Change to call a program instead of calling Python API.
[mirror_edk2.git] / BaseTools / Source / Python / Eot / InfParserLite.py
1 ## @file
2 # This file is used to parse INF file of EDK project
3 #
4 # Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
9 #
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 #
13
14 ##
15 # Import Modules
16 #
17 from __future__ import print_function
18 from __future__ import absolute_import
19 import Common.LongFilePathOs as os
20 import Common.EdkLogger as EdkLogger
21 from Common.DataType import *
22 from CommonDataClass.DataClass import *
23 from Common.Identification import *
24 from Common.StringUtils import *
25 from Eot.Parser import *
26 from Eot import Database
27 from Eot import EotGlobalData
28
29 ## EdkInfParser() class
30 #
31 # This class defined basic INF object which is used by inheriting
32 #
33 # @param object: Inherited from object class
34 #
35 class EdkInfParser(object):
36 ## The constructor
37 #
38 # @param self: The object pointer
39 # @param Filename: INF file name
40 # @param Database: Eot database
41 # @param SourceFileList: A list for all source file belonging this INF file
42 # @param SourceOverridePath: Override path for source file
43 # @param Edk_Source: Envirnoment variable EDK_SOURCE
44 # @param Efi_Source: Envirnoment variable EFI_SOURCE
45 #
46 def __init__(self, Filename = None, Database = None, SourceFileList = None, SourceOverridePath = None, Edk_Source = None, Efi_Source = None):
47 self.Identification = Identification()
48 self.Sources = []
49 self.Macros = {}
50
51 self.Cur = Database.Cur
52 self.TblFile = Database.TblFile
53 self.TblInf = Database.TblInf
54 self.FileID = -1
55 self.SourceOverridePath = SourceOverridePath
56
57 # Load Inf file if filename is not None
58 if Filename is not None:
59 self.LoadInfFile(Filename)
60
61 if SourceFileList:
62 for Item in SourceFileList:
63 self.TblInf.Insert(MODEL_EFI_SOURCE_FILE, Item, '', '', '', '', 'COMMON', -1, self.FileID, -1, -1, -1, -1, 0)
64
65
66 ## LoadInffile() method
67 #
68 # Load INF file and insert a record in database
69 #
70 # @param self: The object pointer
71 # @param Filename: Input value for filename of Inf file
72 #
73 def LoadInfFile(self, Filename = None):
74 # Insert a record for file
75 Filename = NormPath(Filename)
76 self.Identification.FileFullPath = Filename
77 (self.Identification.FileRelativePath, self.Identification.FileName) = os.path.split(Filename)
78
79 self.FileID = self.TblFile.InsertFile(Filename, MODEL_FILE_INF)
80
81 self.ParseInf(PreProcess(Filename, False), self.Identification.FileRelativePath, Filename)
82
83 ## ParserSource() method
84 #
85 # Parse Source section and insert records in database
86 #
87 # @param self: The object pointer
88 # @param CurrentSection: current section name
89 # @param SectionItemList: the item belonging current section
90 # @param ArchList: A list for arch for this section
91 # @param ThirdList: A list for third item for this section
92 #
93 def ParserSource(self, CurrentSection, SectionItemList, ArchList, ThirdList):
94 for Index in range(0, len(ArchList)):
95 Arch = ArchList[Index]
96 Third = ThirdList[Index]
97 if Arch == '':
98 Arch = TAB_ARCH_COMMON
99
100 for Item in SectionItemList:
101 if CurrentSection.upper() == 'defines'.upper():
102 (Name, Value) = AddToSelfMacro(self.Macros, Item[0])
103 self.TblInf.Insert(MODEL_META_DATA_HEADER, Name, Value, Third, '', '', Arch, -1, self.FileID, Item[1], -1, Item[1], -1, 0)
104
105 ## ParseInf() method
106 #
107 # Parse INF file and get sections information
108 #
109 # @param self: The object pointer
110 # @param Lines: contents of INF file
111 # @param FileRelativePath: relative path of the file
112 # @param Filename: file name of INF file
113 #
114 def ParseInf(self, Lines = [], FileRelativePath = '', Filename = ''):
115 IfDefList, SectionItemList, CurrentSection, ArchList, ThirdList, IncludeFiles = \
116 [], [], TAB_UNKNOWN, [], [], []
117 LineNo = 0
118
119 for Line in Lines:
120 LineNo = LineNo + 1
121 if Line == '':
122 continue
123 if Line.startswith(TAB_SECTION_START) and Line.endswith(TAB_SECTION_END):
124 self.ParserSource(CurrentSection, SectionItemList, ArchList, ThirdList)
125
126 # Parse the new section
127 SectionItemList = []
128 ArchList = []
129 ThirdList = []
130 # Parse section name
131 CurrentSection = ''
132 LineList = GetSplitValueList(Line[len(TAB_SECTION_START):len(Line) - len(TAB_SECTION_END)], TAB_COMMA_SPLIT)
133 for Item in LineList:
134 ItemList = GetSplitValueList(Item, TAB_SPLIT)
135 if CurrentSection == '':
136 CurrentSection = ItemList[0]
137 else:
138 if CurrentSection != ItemList[0]:
139 EdkLogger.error("Parser", PARSER_ERROR, "Different section names '%s' and '%s' are found in one section definition, this is not allowed." % (CurrentSection, ItemList[0]), File=Filename, Line=LineNo)
140 ItemList.append('')
141 ItemList.append('')
142 if len(ItemList) > 5:
143 RaiseParserError(Line, CurrentSection, Filename, '', LineNo)
144 else:
145 ArchList.append(ItemList[1].upper())
146 ThirdList.append(ItemList[2])
147
148 continue
149
150 # Add a section item
151 SectionItemList.append([Line, LineNo])
152 # End of parse
153
154 self.ParserSource(CurrentSection, SectionItemList, ArchList, ThirdList)
155 #End of For
156
157