Changed the import path
[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 Common.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 \r
22     def __infParse__(self):\r
23         #\r
24         # Get the InfClass object\r
25         #\r
26 ##        for item in GenFdsGlobalVariable.WorkSpace.InfDatabase:\r
27 ##            print item\r
28         self.InfFileName = os.path.normpath(self.InfFileName)\r
29         Inf = GenFdsGlobalVariable.WorkSpace.InfDatabase[self.InfFileName]\r
30         #\r
31         # Set Ffs BaseName, MdouleGuid, ModuleType, Version, OutputPath\r
32         #\r
33         self.BaseName = Inf.Defines.DefinesDictionary['BASE_NAME'][0]\r
34         self.ModuleGuid = Inf.Defines.DefinesDictionary['FILE_GUID'][0]\r
35         self.ModuleType = Inf.Defines.DefinesDictionary['MODULE_TYPE'][0]\r
36         self.VersionString = Inf.Defines.DefinesDictionary['VERSION_STRING'][0]\r
37         print "BaseName : %s" %self.BaseName\r
38         print "ModuleGuid : %s" %self.ModuleGuid\r
39         print "ModuleType : %s" %self.ModuleType\r
40         print "VersionString : %s" %self.VersionString\r
41         print "InfFileName :" , self.InfFileName\r
42         #\r
43         # Set OutputPath = ${WorkSpace}\Build\Fv\Ffs\${ModuleGuid}+ ${MdouleName}\\r
44         #\r
45 \r
46         self.OutputPath = os.path.join(GenFdsGlobalVariable.FfsDir, \\r
47                                        self.ModuleGuid + self.BaseName)\r
48         self.OutputPath = os.path.normcase(self.OutputPath)\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 \r
118         Rule = GenFdsGlobalVariable.FdfParser.profile.RuleDict.get(RuleName)\r
119         if Rule == None :\r
120             RuleName = 'RULE'      + \\r
121                        '.'         + \\r
122                        'COMMON'    + \\r
123                        '.'         + \\r
124                        self.ModuleType.upper()\r
125                        \r
126             Rule = GenFdsGlobalVariable.FdfParser.profile.RuleDict.get(RuleName)\r
127             if Rule == None :\r
128                 print 'Dont Find Related Rule, Using Default Rule !!!'\r
129                 if GenFdsGlobalVariable.DefaultRule == None:\r
130                     raise Exception ("Default Rule doesn't exist!!")\r
131                 return GenFdsGlobalVariable.DefaultRule\r
132         print "Want To Find Rule Name is : " + RuleName\r
133         return Rule\r
134     \r
135     def __GetEFIOutPutPath__(self):\r
136         Flag = False\r
137         Arch = ''\r
138         OutputPath = ''\r
139         (ModulePath, fileName) = os.path.split(self.InfFileName)\r
140         index = fileName.find('.')\r
141         fileName = fileName[0:index]\r
142         Platform = os.path.normpath(GenFdsGlobalVariable.WorkSpace.TargetTxt.TargetTxtDictionary["ACTIVE_PLATFORM"][0])\r
143         targetArchList = GenFdsGlobalVariable.WorkSpace.TargetTxt.TargetTxtDictionary["TARGET_ARCH"]\r
144         if len(targetArchList) == 0:\r
145             myArchList = GenFdsGlobalVariable.WorkSpace.SupArchList\r
146         else:\r
147             myArchList = set(GenFdsGlobalVariable.WorkSpace.SupArchList) & set(targetArchList)\r
148             print "Valid target architecture(s) is", " ".join(myArchList)\r
149 \r
150         if len(myArchList) > 1 :\r
151             for Key in self.KeyStringList:\r
152                 Target, Tag, Arch = Key.split('_')\r
153                 ArchList = set (ArchList) & Arch\r
154                 if ArchList == 1:\r
155                     Arch = ArchList[0]\r
156                 else:\r
157                     raise Exception("Module %s has too many bulid Arch !" %self.InfFileNames)\r
158         elif len(myArchList) == 1 :\r
159             Arch = myArchList.pop()\r
160             \r
161         OutputPath = os.path.join(GenFdsGlobalVariable.OuputDir,\r
162                                   Arch ,\r
163                                   ModulePath,\r
164                                   fileName,\r
165                                   'OUTPUT'\r
166                                   )\r
167         OutputPath = os.path.normcase(OutputPath)\r
168         return OutputPath\r
169         \r
170     def __GenSimpleFileSection__(self, Rule):\r
171         #\r
172         # Prepare the parameter of GenSection\r
173         #\r
174         GenSecInputFile = self.__ExtendMarco__(Rule.FileName)\r
175 \r
176         SectionType     = Rule.SectionType\r
177 \r
178         GenSecOutputFile= self.__ExtendMarco__(Rule.NameGuid) + \\r
179                               Ffs.Ffs.SectionSuffix[SectionType]\r
180                               \r
181         OutputFile = os.path.join(self.OutputPath, GenSecOutputFile)\r
182         \r
183         genSectionCmd = 'GenSec -o '                                   + \\r
184                          OutputFile                                    + \\r
185                          ' -s '                                        + \\r
186                          Section.Section.SectionType[SectionType]      + \\r
187                          ' '                                           + \\r
188                          GenSecInputFile\r
189         #\r
190         # Call GenSection\r
191         #\r
192         print genSectionCmd\r
193         GenFdsGlobalVariable.CallExternalTool(genSectionCmd, "Gensection Failed!")\r
194         return OutputFile\r
195     \r
196     def __GenSimpleFileFfs__(self, Rule, InputFile):\r
197         #\r
198         # Prepare the parameter of GenFfs\r
199         #\r
200         (FileType,Fixed, CheckSum, Alignment) = self.__GetGenFfsComParamter__(Rule)\r
201         \r
202         FfsOutput = self.OutputPath                     + \\r
203                     os.sep                              + \\r
204                     self.__ExtendMarco__(Rule.NameGuid) + \\r
205                     '.ffs'\r
206 \r
207         print self.__ExtendMarco__(Rule.NameGuid)\r
208         InputSection = ' -i '     + \\r
209                        InputFile\r
210 \r
211 \r
212         GenFfsCmd = 'GenFfs '  + \\r
213                      FileType  + \\r
214                      Fixed     + \\r
215                      CheckSum  + \\r
216                      Alignment + \\r
217                      ' -o '    + \\r
218                      FfsOutput + \\r
219                      ' -g '    + \\r
220                      self.NameGuid + \\r
221                      InputSection\r
222         #\r
223         # Call GenSection\r
224         #\r
225         print GenFfsCmd\r
226         GenFdsGlobalVariable.CallExternalTool(GenFfsCmd, "GenFfs Failed!")\r
227         return FfsOutput\r
228     \r
229     def __GenComplexFileSection__(self, Rule):\r
230         SectFiles = ''\r
231         for Sect in Rule.SectionList:\r
232            print 'GenSection: %s %s :' %(self.OutputPath ,self.ModuleGuid)\r
233            secName = ''\r
234            secName, Align = Sect.GenSection(self.OutputPath , self.ModuleGuid, self.KeyStringList, self)\r
235            if secName != '':\r
236                SectFiles = SectFiles    + \\r
237                            ' -i '       + \\r
238                            secName\r
239                if Align != None:\r
240                    SectFiles = SectFiles + \\r
241                                ' -n '    + \\r
242                                Align\r
243         return SectFiles\r
244 \r
245     def __GenComplexFileFfs__(self, Rule, InputFile):\r
246         \r
247         (FileType,Fixed, CheckSum, Alignment) = self.__GetGenFfsComParamter__(Rule)\r
248         \r
249         FfsOutput = os.path.join( self.OutputPath, self.ModuleGuid + '.ffs')\r
250         GenFfsCmd = 'GenFfs '                                     + \\r
251                      Fixed                                        + \\r
252                      CheckSum                                     + \\r
253                      Alignment                                    + \\r
254                      FileType                                     + \\r
255                      ' -g '                                       + \\r
256                      self.ModuleGuid                              + \\r
257                      ' -o '                                       + \\r
258                      FfsOutput                                    + \\r
259                      InputFile\r
260                      \r
261         print GenFfsCmd\r
262         GenFdsGlobalVariable.CallExternalTool(GenFfsCmd, "GenFfs Failed !")\r
263         return FfsOutput\r
264 \r
265     def __GetGenFfsComParamter__(self, Rule):\r
266         FileType = ' -t ' + \\r
267                    Ffs.Ffs.ModuleTypeToFileType[Rule.ModuleType]\r
268         print "Rule.Fixed = ", Rule.Fixed\r
269         if Rule.Fixed != False:\r
270             Fixed = ' -x '\r
271         else :\r
272             Fixed = ''\r
273         if Rule.CheckSum != False:\r
274             CheckSum = ' -s '\r
275         else :\r
276             CheckSume = ''\r
277             \r
278         if Rule.Alignment != None and Rule.Alignment != '':\r
279             Alignment = ' -a %s' %Rule.Alignment\r
280         else :\r
281             Alignment = ''\r
282             \r
283         return FileType, Fixed, CheckSum, Alignment\r