]>
Commit | Line | Data |
---|---|---|
b0282412 | 1 | /** @file\r |
2 | \r | |
3 | Copyright (c) 2006, Intel Corporation\r | |
4 | All rights reserved. This program and the accompanying materials\r | |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | **/\r | |
0dc8c589 | 13 | package org.tianocore.migration;\r |
14 | \r | |
71f30e51 | 15 | import java.io.BufferedReader;\r |
16 | import java.io.BufferedWriter;\r | |
17 | import java.io.File;\r | |
18 | import java.io.FileWriter;\r | |
19 | import java.io.InputStreamReader;\r | |
20 | import java.util.ArrayList;\r | |
21 | import java.util.Iterator;\r | |
22 | import java.util.List;\r | |
23 | import java.util.UUID;\r | |
0dc8c589 | 24 | \r |
71f30e51 | 25 | import org.apache.xmlbeans.XmlCursor;\r |
26 | import org.apache.xmlbeans.XmlOptions;\r | |
27 | import org.tianocore.ExternsDocument;\r | |
28 | import org.tianocore.FilenameDocument;\r | |
29 | import org.tianocore.GuidsDocument;\r | |
30 | import org.tianocore.LibraryClassDefinitionsDocument;\r | |
31 | import org.tianocore.LibraryClassDocument;\r | |
32 | import org.tianocore.ModuleDefinitionsDocument;\r | |
33 | import org.tianocore.ModuleSurfaceAreaDocument;\r | |
34 | import org.tianocore.ModuleTypeDef;\r | |
35 | import org.tianocore.MsaHeaderDocument;\r | |
36 | import org.tianocore.PPIsDocument;\r | |
37 | import org.tianocore.PackageDependenciesDocument;\r | |
38 | import org.tianocore.ProtocolsDocument;\r | |
39 | import org.tianocore.SourceFilesDocument;\r | |
40 | import org.tianocore.SupportedArchitectures;\r | |
41 | import org.tianocore.UsageTypes;\r | |
0dc8c589 | 42 | import org.tianocore.SupportedArchitectures.Enum;\r |
0dc8c589 | 43 | \r |
5c55f71c | 44 | public class MsaWriter {\r |
71f30e51 | 45 | MsaWriter(ModuleInfo moduleinfo) {\r |
46 | mi = moduleinfo;\r | |
47 | }\r | |
48 | \r | |
49 | private ModuleInfo mi;\r | |
50 | \r | |
51 | private ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory\r | |
52 | .newInstance();\r | |
53 | \r | |
54 | private ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc\r | |
55 | .addNewModuleSurfaceArea();\r | |
56 | \r | |
57 | private MsaHeaderDocument.MsaHeader msaheader = msa.addNewMsaHeader();\r | |
58 | \r | |
59 | private ModuleDefinitionsDocument.ModuleDefinitions md = msa\r | |
60 | .addNewModuleDefinitions();\r | |
61 | \r | |
62 | private SourceFilesDocument.SourceFiles sourcefiles = msa\r | |
63 | .addNewSourceFiles(); // found local .h files are not written\r | |
64 | \r | |
65 | private GuidsDocument.Guids guids;\r | |
66 | \r | |
67 | private ProtocolsDocument.Protocols protocols;\r | |
68 | \r | |
69 | private PPIsDocument.PPIs ppis;\r | |
70 | \r | |
71 | private PackageDependenciesDocument.PackageDependencies pd = msa\r | |
72 | .addNewPackageDependencies();\r | |
73 | \r | |
74 | private LibraryClassDefinitionsDocument.LibraryClassDefinitions libclassdefs = msa\r | |
75 | .addNewLibraryClassDefinitions();\r | |
76 | \r | |
77 | private ExternsDocument.Externs externs = msa.addNewExterns();\r | |
78 | \r | |
79 | private String Query(String requirement) throws Exception {\r | |
80 | String answer;\r | |
81 | BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));\r | |
82 | System.out.println(requirement);\r | |
83 | while ((answer = rd.readLine()).length() == 0)\r | |
84 | ;\r | |
85 | return answer;\r | |
86 | }\r | |
87 | \r | |
88 | private void addSourceFiles(String name) { // furthur modification needed\r | |
89 | List<Enum> arch = new ArrayList<Enum>();\r | |
90 | FilenameDocument.Filename filename;\r | |
91 | filename = sourcefiles.addNewFilename();\r | |
92 | filename.setStringValue(name);\r | |
93 | \r | |
94 | if (name.contains("x64" + File.separator)) { // filename ???\r | |
95 | arch.add(SupportedArchitectures.X_64);\r | |
96 | System.out.println("x64" + File.separator);\r | |
97 | filename.setSupArchList(arch);\r | |
98 | } else if (name.contains("Ia32" + File.separator)) { // filename ???\r | |
99 | arch.add(SupportedArchitectures.IA_32);\r | |
100 | System.out.println("Ia32" + File.separator);\r | |
101 | filename.setSupArchList(arch);\r | |
102 | } else if (name.contains("Ipf" + File.separator)) { // filename ???\r | |
103 | arch.add(SupportedArchitectures.IPF);\r | |
104 | System.out.println("Ipf" + File.separator);\r | |
105 | filename.setSupArchList(arch);\r | |
106 | } else if (name.contains("Ebc" + File.separator)) { // filename ???\r | |
107 | arch.add(SupportedArchitectures.EBC);\r | |
108 | System.out.println("Ebc" + File.separator);\r | |
109 | filename.setSupArchList(arch);\r | |
110 | }\r | |
111 | }\r | |
112 | \r | |
113 | private void addWrapper() {\r | |
114 | XmlCursor cursor = msa.newCursor();\r | |
115 | String uri = "http://www.TianoCore.org/2006/Edk2.0";\r | |
116 | cursor.push();\r | |
117 | cursor.toNextToken();\r | |
118 | cursor.insertNamespace("", uri);\r | |
119 | cursor.insertNamespace("xsi",\r | |
120 | "http://www.w3.org/2001/XMLSchema-instance");\r | |
121 | cursor.pop();\r | |
122 | msa = (ModuleSurfaceAreaDocument.ModuleSurfaceArea) cursor.getObject();\r | |
123 | }\r | |
124 | \r | |
125 | private ModuleSurfaceAreaDocument fulfillMsadoc() throws Exception {\r | |
126 | Iterator<String> it;\r | |
127 | String temp;\r | |
128 | \r | |
129 | if (mi.modulename != null) {\r | |
130 | msaheader.setModuleName(mi.modulename);\r | |
131 | } else {\r | |
132 | msaheader\r | |
133 | .setModuleName(mi.modulename = Query("Module Name Not Found! Please Input ModuleName"));\r | |
134 | }\r | |
135 | if (mi.guidvalue == null) {\r | |
136 | mi.guidvalue = UUID.randomUUID().toString();\r | |
137 | MigrationTool.ui\r | |
138 | .println("Guid value can not be retrieved from inf file. Generate "\r | |
139 | + mi.guidvalue + " at random!");\r | |
140 | }\r | |
141 | msaheader.setGuidValue(mi.guidvalue);\r | |
142 | if (mi.moduletype != null) {\r | |
143 | msaheader.setModuleType(ModuleTypeDef.Enum.forString(mi\r | |
144 | .getModuleType()));\r | |
145 | } else {\r | |
146 | msaheader\r | |
147 | .setModuleType(ModuleTypeDef.Enum\r | |
148 | .forString(mi.moduletype = Query("Guid Value Not Found! Please Input Guid Value")));\r | |
149 | }\r | |
150 | \r | |
151 | msaheader\r | |
630bc731 | 152 | .setCopyright("Copyright (c) 2007, Intel Corporation. All rights reserved.");\r |
71f30e51 | 153 | msaheader.setVersion("1.0");\r |
154 | msaheader.setAbstract("Component name for module " + mi.modulename);\r | |
155 | msaheader.setDescription("FIX ME!");\r | |
156 | \r | |
157 | if (mi.license == null) {\r | |
158 | mi.license = "FIX ME!";\r | |
159 | MigrationTool.ui\r | |
160 | .println("Fail to extract license info in inf file");\r | |
161 | }\r | |
162 | msaheader.addNewLicense().setStringValue(mi.license);\r | |
163 | msaheader\r | |
164 | .setSpecification("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052");\r | |
165 | \r | |
166 | List<Enum> arch = new ArrayList<Enum>();\r | |
167 | arch.add(SupportedArchitectures.IA_32);\r | |
168 | arch.add(SupportedArchitectures.X_64);\r | |
169 | arch.add(SupportedArchitectures.IPF);\r | |
170 | arch.add(SupportedArchitectures.EBC);\r | |
171 | md.setSupportedArchitectures(arch);\r | |
172 | md.setBinaryModule(false);\r | |
173 | md.setOutputFileBasename(mi.modulename);\r | |
174 | //\r | |
175 | // For now, simply add all package guids in the database.\r | |
176 | // \r | |
177 | it = MigrationTool.db.dumpAllPkgGuid();\r | |
178 | while (it.hasNext()) {\r | |
179 | pd.addNewPackage().setPackageGuid(it.next());\r | |
180 | }\r | |
181 | externs.addNewSpecification().setStringValue(\r | |
182 | "EFI_SPECIFICATION_VERSION 0x00020000");\r | |
183 | externs.addNewSpecification().setStringValue(\r | |
184 | "EDK_RELEASE_VERSION 0x00020000");\r | |
185 | if (mi.entrypoint != null) {\r | |
186 | externs.addNewExtern().setModuleEntryPoint(mi.entrypoint);\r | |
187 | org.tianocore.ModuleTypeDef.Enum moduleType = msaheader\r | |
188 | .getModuleType();\r | |
189 | if (moduleType == ModuleTypeDef.PEIM) {\r | |
190 | mi.hashrequiredr9libs.add("PeimEntryPoint");\r | |
191 | } else {\r | |
192 | mi.hashrequiredr9libs.add("UefiDriverEntryPoint");\r | |
193 | }\r | |
194 | }\r | |
195 | \r | |
196 | it = mi.localmodulesources.iterator();\r | |
197 | while (it.hasNext()) {\r | |
198 | addSourceFiles(it.next());\r | |
199 | }\r | |
200 | if (!mi.protocols.isEmpty()) {\r | |
201 | protocols = msa.addNewProtocols();\r | |
202 | it = mi.protocols.iterator();\r | |
203 | while (it.hasNext()) {\r | |
204 | if ((temp = it.next()) != null) {\r | |
205 | ProtocolsDocument.Protocols.Protocol pr = protocols\r | |
206 | .addNewProtocol();\r | |
207 | pr.setProtocolCName(temp);\r | |
208 | pr.setUsage(UsageTypes.ALWAYS_CONSUMED);\r | |
209 | }\r | |
210 | }\r | |
211 | }\r | |
212 | if (!mi.ppis.isEmpty()) {\r | |
213 | ppis = msa.addNewPPIs();\r | |
214 | it = mi.ppis.iterator();\r | |
215 | while (it.hasNext()) {\r | |
216 | if ((temp = it.next()) != null) {\r | |
217 | PPIsDocument.PPIs.Ppi pp = ppis.addNewPpi();\r | |
218 | pp.setPpiCName(temp);\r | |
219 | pp.setUsage(UsageTypes.ALWAYS_CONSUMED);\r | |
220 | }\r | |
221 | }\r | |
222 | }\r | |
223 | if (!mi.guids.isEmpty()) {\r | |
224 | guids = msa.addNewGuids();\r | |
225 | it = mi.guids.iterator();\r | |
226 | while (it.hasNext()) {\r | |
227 | if ((temp = it.next()) != null) {\r | |
228 | GuidsDocument.Guids.GuidCNames gcn = guids\r | |
229 | .addNewGuidCNames();\r | |
230 | gcn.setGuidCName(temp);\r | |
231 | gcn.setUsage(UsageTypes.ALWAYS_CONSUMED);\r | |
232 | }\r | |
233 | }\r | |
234 | }\r | |
a09aa7e2 | 235 | if (mi.isLibrary) {\r |
236 | LibraryClassDocument.LibraryClass lc = libclassdefs\r | |
237 | .addNewLibraryClass();\r | |
238 | lc.setKeyword(mi.modulename);\r | |
239 | lc.setUsage(UsageTypes.ALWAYS_PRODUCED);\r | |
240 | }\r | |
71f30e51 | 241 | it = mi.hashrequiredr9libs.iterator();\r |
242 | while (it.hasNext()) {\r | |
243 | if ((temp = it.next()) != null && !temp.matches("%")\r | |
244 | && !temp.matches("n/a")) {\r | |
245 | LibraryClassDocument.LibraryClass lc = libclassdefs\r | |
246 | .addNewLibraryClass();\r | |
247 | lc.setKeyword(temp);\r | |
248 | lc.setUsage(UsageTypes.ALWAYS_CONSUMED);\r | |
249 | }\r | |
250 | }\r | |
251 | addWrapper();\r | |
252 | msadoc.setModuleSurfaceArea(msa);\r | |
253 | return msadoc;\r | |
254 | }\r | |
255 | \r | |
256 | public void flush() throws Exception {\r | |
257 | XmlOptions options = new XmlOptions();\r | |
258 | \r | |
259 | options.setCharacterEncoding("UTF-8");\r | |
260 | options.setSavePrettyPrint();\r | |
261 | options.setSavePrettyPrintIndent(2);\r | |
262 | options.setUseDefaultNamespace();\r | |
263 | \r | |
264 | BufferedWriter bw = new BufferedWriter(new FileWriter(\r | |
265 | MigrationTool.ModuleInfoMap.get(mi) + File.separator\r | |
266 | + "Migration_" + mi.modulename + File.separator\r | |
267 | + mi.modulename + ".msa"));\r | |
268 | fulfillMsadoc().save(bw, options);\r | |
269 | // MsaTreeEditor.init(mi, ui, msadoc);\r | |
270 | bw.flush();\r | |
271 | bw.close();\r | |
272 | }\r | |
273 | \r | |
274 | private static void flush(String path, ModuleSurfaceAreaDocument msadoc)\r | |
275 | throws Exception {\r | |
276 | XmlOptions options = new XmlOptions();\r | |
277 | \r | |
278 | options.setCharacterEncoding("UTF-8");\r | |
279 | options.setSavePrettyPrint();\r | |
280 | options.setSavePrettyPrintIndent(2);\r | |
281 | options.setUseDefaultNamespace();\r | |
282 | \r | |
283 | BufferedWriter bw = new BufferedWriter(new FileWriter(path));\r | |
284 | msadoc.save(bw, options);\r | |
285 | bw.flush();\r | |
286 | bw.close();\r | |
287 | }\r | |
288 | \r | |
289 | public static final void parse(String msafile) throws Exception {\r | |
290 | ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory\r | |
291 | .parse(msafile);\r | |
292 | flush("c:\\temp.msa", msadoc);\r | |
293 | }\r | |
0dc8c589 | 294 | }\r |