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