3b910985c831cfe287173431465700a7cd3b56aa
[people/mcb30/basetools.git] / Source / Python / GenFds / FfsInfStatement.py
1 import Rule\r
2 import os\r
3 from GenFdsGlobalVariable import GenFdsGlobalVariable\r
4 import Ffs\r
5 import subprocess\r
6 import sys\r
7 import Section\r
8 import RuleSimpleFile\r
9 import RuleComplexFile\r
10 #from String import *\r
11 \r
12 class FfsInfStatement(Ffs.Ffs):\r
13     def __init__(self):\r
14         Ffs.Ffs.__init__(self)\r
15         self.Rule = None\r
16         self.ver = None\r
17         self.Ui = None\r
18         self.InfFileName = None\r
19         self.BuildNum = ''\r
20         self.KeyStringList = []\r
21         self.RuleOverride = None\r
22 \r
23     def __infParse__(self):\r
24         #\r
25         # Get the InfClass object\r
26         #\r
27 ##        for item in GenFdsGlobalVariable.WorkSpace.InfDatabase:\r
28 ##            print item\r
29         self.InfFileName = os.path.normpath(self.InfFileName)\r
30         Inf = GenFdsGlobalVariable.WorkSpace.InfDatabase[self.InfFileName]\r
31         #\r
32         # Set Ffs BaseName, MdouleGuid, ModuleType, Version, OutputPath\r
33         #\r
34         self.BaseName = Inf.Defines.DefinesDictionary['BASE_NAME'][0]\r
35         self.ModuleGuid = Inf.Defines.DefinesDictionary['FILE_GUID'][0]\r
36         self.ModuleType = Inf.Defines.DefinesDictionary['MODULE_TYPE'][0]\r
37         self.VersionString = Inf.Defines.DefinesDictionary['VERSION_STRING'][0]\r
38         print "BaseName : %s" %self.BaseName\r
39         print "ModuleGuid : %s" %self.ModuleGuid\r
40         print "ModuleType : %s" %self.ModuleType\r
41         print "VersionString : %s" %self.VersionString\r
42         print "InfFileName :" , self.InfFileName\r
43         #\r
44         # Set OutputPath = ${WorkSpace}\Build\Fv\Ffs\${ModuleGuid}+ ${MdouleName}\\r
45         #\r
46 \r
47         self.OutputPath = os.path.join(GenFdsGlobalVariable.FfsDir, \\r
48                                        self.ModuleGuid + self.BaseName)\r
49         if not os.path.exists(self.OutputPath) :\r
50             os.makedirs(self.OutputPath)\r
51             \r
52         self.InfOutputPath = self.__GetEFIOutPutPath__()\r
53         print "ModuelEFIPath: " ,self.InfOutputPath\r
54                              \r
55     def GenFfs(self):\r
56         #\r
57         # Parse Inf file get Module related information\r
58         #\r
59         print " Begion parsing INf file : %s" %self.InfFileName\r
60         \r
61         """ Replace $(WORKSPACE) to None!"""\r
62         self.InfFileName = self.InfFileName.replace('$(WORKSPACE)', '')\r
63         if self.InfFileName[0] == '\\' or self.InfFileName[0] == '/' :\r
64             self.InfFileName = self.InfFileName[1:]\r
65         \r
66  \r
67         self.__infParse__()\r
68         #\r
69         # Get the rule of how to generate Ffs file\r
70         #\r
71         Rule = self.__GetRule__()\r
72                                                        \r
73         FileType = Ffs.Ffs.ModuleTypeToFileType[Rule.ModuleType]\r
74         #\r
75         # For the rule only has simpleFile\r
76         #\r
77         if isinstance (Rule, RuleSimpleFile.RuleSimpleFile) :\r
78             SectionOutput = self.__GenSimpleFileSection__(Rule)\r
79             FfsOutput = self.__GenSimpleFileFfs__(Rule, SectionOutput)\r
80             return FfsOutput\r
81         #\r
82         # For Rule has ComplexFile\r
83         #\r
84         elif isinstance(Rule, RuleComplexFile.RuleComplexFile):\r
85            \r
86             InputSectList = self.__GenComplexFileSection__(Rule)\r
87             FfsOutput = self.__GenComplexFileFfs__(Rule, InputSectList)\r
88             \r
89             return FfsOutput\r
90                 \r
91     def __ExtendMarco__ (self, String):\r
92         MarcoDict = {\r
93             '$(INF_OUTPUT)'  : self.InfOutputPath,\r
94             '$(MODULE_NAME)' : self.BaseName,\r
95             '$(BUILD_NUMBER)': self.BuildNum,\r
96             '$(INF_VERSION)' : self.VersionString,\r
97             '$(NAMED_GUID)'  : self.ModuleGuid,\r
98             '$(WORKSPACE)'   : GenFdsGlobalVariable.WorkSpaceDir\r
99         }\r
100         if String == None :\r
101             return None\r
102         for Marco in MarcoDict.keys():\r
103             if String.find(Marco) >= 0 :\r
104                 String = String.replace (Marco, MarcoDict[Marco])\r
105         return String\r
106 \r
107     def __GetRule__ (self) :\r
108         currentArch = 'IA32'\r
109         #for item in GenFdsGlobalVariable.FdfParser.profile.RuleDict :\r
110         #    print item\r
111         RuleName = 'RULE'      + \\r
112                    '.'         + \\r
113                    currentArch + \\r
114                    '.'         + \\r
115                    self.ModuleType.upper()\r
116         \r
117         Rule = GenFdsGlobalVariable.FdfParser.profile.RuleDict.get(RuleName)\r
118         if Rule == None :\r
119             RuleName = 'RULE'      + \\r
120                        '.'         + \\r
121                        'COMMON'    + \\r
122                        '.'         + \\r
123                        self.ModuleType.upper()\r
124             print "Want To Find Rule Name is : " + RuleName\r
125             Rule = GenFdsGlobalVariable.FdfParser.profile.RuleDict.get(RuleName)\r
126             if Rule == None :\r
127                 print 'Dont Find Related Rule, Using Default Rule !!!'\r
128                 if GenFdsGlobalVariable.DefaultRule == None:\r
129                     raise Exception ("Default Rule doesn't exist!!")\r
130                 return GenFdsGlobalVariable.DefaultRule\r
131         print "Want To Find Rule Name is : " + RuleName\r
132         return Rule\r
133     \r
134     def __GetEFIOutPutPath__(self):\r
135         Flag = False\r
136         Arch = ''\r
137         OutputPath = ''\r
138         (ModulePath, fileName) = os.path.split(self.InfFileName)\r
139         index = fileName.find('.')\r
140         fileName = fileName[0:index]\r
141         Platform = os.path.normpath(GenFdsGlobalVariable.WorkSpace.TargetTxt.TargetTxtDictionary["ACTIVE_PLATFORM"][0])\r
142         targetArchList = GenFdsGlobalVariable.WorkSpace.TargetTxt.TargetTxtDictionary["TARGET_ARCH"]\r
143         if len(targetArchList) == 0:\r
144             myArchList = GenFdsGlobalVariable.WorkSpace.SupArchList\r
145         else:\r
146             myArchList = set(GenFdsGlobalVariable.WorkSpace.SupArchList) & set(targetArchList)\r
147             print "Valid target architecture(s) is", " ".join(myArchList)\r
148 \r
149         if len(myArchList) > 1 :\r
150             for Key in self.KeyStringList:\r
151                 Target, Tag, Arch = Key.split('_')\r
152                 ArchList = set (ArchList) & Arch\r
153                 if ArchList == 1:\r
154                     Arch = ArchList[0]\r
155                 else:\r
156                     raise Exception("Module %s has too many bulid Arch !" %self.InfFileNames)\r
157         elif len(myArchList) == 1 :\r
158             Arch = myArchList.pop()\r
159             \r
160         OutputPath = os.path.join(GenFdsGlobalVariable.OuputDir,\r
161                                   Arch ,\r
162                                   ModulePath,\r
163                                   fileName,\r
164                                   'OUTPUT'\r
165                                   )\r
166         OutputPath = os.path.normcase(OutputPath)\r
167         return OutputPath\r
168         \r
169     def __GenSimpleFileSection__(self, Rule):\r
170         #\r
171         # Prepare the parameter of GenSection\r
172         #\r
173         GenSecInputFile = self.__ExtendMarco__(Rule.FileName)\r
174 \r
175         SectionType     = Rule.SectionType\r
176 \r
177         GenSecOutputFile= self.__ExtendMarco__(Rule.NameGuid) + \\r
178                               Ffs.Ffs.SectionSuffix[SectionType]\r
179                               \r
180         OutputFile = os.path.join(self.OutputPath, GenSecOutputFile)\r
181         \r
182         genSectionCmd = 'GenSec -o '                                   + \\r
183                          OutputFile                                    + \\r
184                          ' -s '                                        + \\r
185                          Section.Section.SectionType[SectionType]      + \\r
186                          ' '                                           + \\r
187                          GenSecInputFile\r
188         #\r
189         # Call GenSection\r
190         #\r
191         print genSectionCmd\r
192         GenFdsGlobalVariable.CallExternalTool(genSectionCmd, "Gensection Failed!")\r
193         return OutputFile\r
194     \r
195     def __GenSimpleFileFfs__(self, Rule, InputFile):\r
196         #\r
197         # Prepare the parameter of GenFfs\r
198         #\r
199         (FileType,Fixed, CheckSum, Alignment) = self.__GetGenFfsComParamter__(Rule)\r
200         \r
201         FfsOutput = self.OutputPath                     + \\r
202                     os.sep                              + \\r
203                     self.__ExtendMarco__(Rule.NameGuid) + \\r
204                     '.ffs'\r
205 \r
206         print self.__ExtendMarco__(Rule.NameGuid)\r
207         InputSection = ' -i '     + \\r
208                        InputFile\r
209 \r
210 \r
211         GenFfsCmd = 'GenFfs '  + \\r
212                      FileType  + \\r
213                      Fixed     + \\r
214                      CheckSum  + \\r
215                      Alignment + \\r
216                      ' -o '    + \\r
217                      FfsOutput + \\r
218                      ' -g '    + \\r
219                      self.NameGuid + \\r
220                      InputSection\r
221         #\r
222         # Call GenSection\r
223         #\r
224         print GenFfsCmd\r
225         GenFdsGlobalVariable.CallExternalTool(GenFfsCmd, "GenFfs Failed!")\r
226         return FfsOutput\r
227     \r
228     def __GenComplexFileSection__(self, Rule):\r
229         SectFiles = ''\r
230         for Sect in Rule.SectionList:\r
231            #print 'GenSection: %s %s :' %(self.OutputPath ,self.ModuleGuid)\r
232            secName = ''\r
233            secName, Align = Sect.GenSection(self.OutputPath , self.ModuleGuid, self.KeyStringList, self)\r
234            if secName != '':\r
235                SectFiles = SectFiles    + \\r
236                            ' -i '       + \\r
237                            secName\r
238                if Align != None:\r
239                    SectFiles = SectFiles + \\r
240                                ' -n '    + \\r
241                                Align\r
242         return SectFiles\r
243 \r
244     def __GenComplexFileFfs__(self, Rule, InputFile):\r
245         \r
246         (FileType,Fixed, CheckSum, Alignment) = self.__GetGenFfsComParamter__(Rule)\r
247         \r
248         FfsOutput = os.path.join( self.OutputPath, self.ModuleGuid + '.ffs')\r
249         GenFfsCmd = 'GenFfs '                                     + \\r
250                      Fixed                                        + \\r
251                      CheckSum                                     + \\r
252                      Alignment                                    + \\r
253                      FileType                                     + \\r
254                      ' -g '                                       + \\r
255                      self.ModuleGuid                              + \\r
256                      ' -o '                                       + \\r
257                      FfsOutput                                    + \\r
258                      InputFile\r
259                      \r
260         print GenFfsCmd\r
261         GenFdsGlobalVariable.CallExternalTool(GenFfsCmd, "GenFfs Failed !")\r
262         return FfsOutput\r
263 \r
264     def __GetGenFfsComParamter__(self, Rule):\r
265         FileType = ' -t ' + \\r
266                    Ffs.Ffs.ModuleTypeToFileType[Rule.ModuleType]\r
267         print "Rule.Fixed = ", Rule.Fixed\r
268         if Rule.Fixed != False:\r
269             Fixed = ' -x '\r
270         else :\r
271             Fixed = ''\r
272         if Rule.CheckSum != False:\r
273             CheckSum = ' -s '\r
274         else :\r
275             CheckSume = ''\r
276             \r
277         if Rule.Alignment != None and Rule.Alignment != '':\r
278             Alignment = ' -a %s' %Rule.Alignment\r
279         else :\r
280             Alignment = ''\r
281             \r
282         return FileType, Fixed, CheckSum, Alignment