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