]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/UPT/Parser/InfSourceSectionParser.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Parser / InfSourceSectionParser.py
1 ## @file
2 # This file contained the parser for [Sources] sections in INF file
3 #
4 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
5 #
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
7 #
8 '''
9 InfSourceSectionParser
10 '''
11 ##
12 # Import Modules
13 #
14
15 import Logger.Log as Logger
16 from Logger import StringTable as ST
17 from Logger.ToolError import FORMAT_INVALID
18 from Parser.InfParserMisc import InfExpandMacro
19 from Library import DataType as DT
20 from Library.Parsing import MacroParser
21 from Library.Misc import GetSplitValueList
22 from Object.Parser.InfCommonObject import InfLineCommentObject
23 from Parser.InfParserMisc import InfParserSectionRoot
24
25 class InfSourceSectionParser(InfParserSectionRoot):
26 ## InfSourceParser
27 #
28 #
29 def InfSourceParser(self, SectionString, InfSectionObject, FileName):
30 SectionMacros = {}
31 ValueList = []
32 SourceList = []
33 StillCommentFalg = False
34 HeaderComments = []
35 LineComment = None
36 SectionContent = ''
37 for Line in SectionString:
38 SrcLineContent = Line[0]
39 SrcLineNo = Line[1]
40
41 if SrcLineContent.strip() == '':
42 continue
43
44 #
45 # Found Header Comments
46 #
47 if SrcLineContent.strip().startswith(DT.TAB_COMMENT_SPLIT):
48 #
49 # Last line is comments, and this line go on.
50 #
51 if StillCommentFalg:
52 HeaderComments.append(Line)
53 SectionContent += SrcLineContent + DT.END_OF_LINE
54 continue
55 #
56 # First time encounter comment
57 #
58 else:
59 #
60 # Clear original data
61 #
62 HeaderComments = []
63 HeaderComments.append(Line)
64 StillCommentFalg = True
65 SectionContent += SrcLineContent + DT.END_OF_LINE
66 continue
67 else:
68 StillCommentFalg = False
69
70 if len(HeaderComments) >= 1:
71 LineComment = InfLineCommentObject()
72 LineCommentContent = ''
73 for Item in HeaderComments:
74 LineCommentContent += Item[0] + DT.END_OF_LINE
75 LineComment.SetHeaderComments(LineCommentContent)
76
77 #
78 # Find Tail comment.
79 #
80 if SrcLineContent.find(DT.TAB_COMMENT_SPLIT) > -1:
81 TailComments = SrcLineContent[SrcLineContent.find(DT.TAB_COMMENT_SPLIT):]
82 SrcLineContent = SrcLineContent[:SrcLineContent.find(DT.TAB_COMMENT_SPLIT)]
83 if LineComment is None:
84 LineComment = InfLineCommentObject()
85 LineComment.SetTailComments(TailComments)
86
87 #
88 # Find Macro
89 #
90 Name, Value = MacroParser((SrcLineContent, SrcLineNo),
91 FileName,
92 DT.MODEL_EFI_SOURCE_FILE,
93 self.FileLocalMacros)
94 if Name is not None:
95 SectionMacros[Name] = Value
96 LineComment = None
97 HeaderComments = []
98 continue
99
100 #
101 # Replace with Local section Macro and [Defines] section Macro.
102 #
103 SrcLineContent = InfExpandMacro(SrcLineContent,
104 (FileName, SrcLineContent, SrcLineNo),
105 self.FileLocalMacros,
106 SectionMacros)
107
108 TokenList = GetSplitValueList(SrcLineContent, DT.TAB_VALUE_SPLIT, 4)
109 ValueList[0:len(TokenList)] = TokenList
110
111 #
112 # Store section content string after MACRO replaced.
113 #
114 SectionContent += SrcLineContent + DT.END_OF_LINE
115
116 SourceList.append((ValueList, LineComment,
117 (SrcLineContent, SrcLineNo, FileName)))
118 ValueList = []
119 LineComment = None
120 TailComments = ''
121 HeaderComments = []
122 continue
123
124 #
125 # Current section archs
126 #
127 ArchList = []
128 for Item in self.LastSectionHeaderContent:
129 if Item[1] not in ArchList:
130 ArchList.append(Item[1])
131 InfSectionObject.SetSupArchList(Item[1])
132
133 InfSectionObject.SetAllContent(SectionContent)
134 if not InfSectionObject.SetSources(SourceList, Arch = ArchList):
135 Logger.Error('InfParser',
136 FORMAT_INVALID,
137 ST.ERR_INF_PARSER_MODULE_SECTION_TYPE_ERROR % ("[Sources]"),
138 File=FileName,
139 Line=Item[3])