]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/workspace/WorkspaceTools.java
Added "/FI", "-include" and/or "/nologo" options to PP (CC) command option to fpd...
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / workspace / WorkspaceTools.java
1 /** @file
2
3 The file is used to init workspace and get basic information of workspace
4
5 Copyright (c) 2006, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15 package org.tianocore.frameworkwizard.workspace;
16
17 import java.io.IOException;
18 import java.util.Vector;
19
20 import org.apache.xmlbeans.XmlException;
21 import org.tianocore.DbPathAndFilename;
22 import org.tianocore.FrameworkDatabaseDocument;
23 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
24 import org.tianocore.MsaFilesDocument.MsaFiles;
25 import org.tianocore.MsaHeaderDocument.MsaHeader;
26 import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;
27 import org.tianocore.PlatformHeaderDocument.PlatformHeader;
28 import org.tianocore.PlatformSurfaceAreaDocument.PlatformSurfaceArea;
29 import org.tianocore.SpdHeaderDocument.SpdHeader;
30 import org.tianocore.frameworkwizard.common.DataType;
31 import org.tianocore.frameworkwizard.common.Identification;
32 import org.tianocore.frameworkwizard.common.Log;
33 import org.tianocore.frameworkwizard.common.OpenFile;
34 import org.tianocore.frameworkwizard.common.SaveFile;
35 import org.tianocore.frameworkwizard.common.Tools;
36 import org.tianocore.frameworkwizard.module.Identification.ModuleIdentification;
37 import org.tianocore.frameworkwizard.packaging.PackageIdentification;
38 import org.tianocore.frameworkwizard.platform.PlatformIdentification;
39
40 public class WorkspaceTools {
41 //
42 // Define class members
43 //
44 private FrameworkDatabaseDocument.FrameworkDatabase fdb = null;
45
46 private Vector<ModuleIdentification> vModuleList = new Vector<ModuleIdentification>();
47
48 private Vector<PackageIdentification> vPackageList = new Vector<PackageIdentification>();
49
50 private Vector<PlatformIdentification> vPlatformList = new Vector<PlatformIdentification>();
51
52 /**
53
54 Open Framework Database file
55
56 */
57 private void openFrameworkDb() {
58 String strFrameworkDbFilePath = Workspace.getCurrentWorkspace() + Workspace.getStrWorkspaceDatabaseFile();
59 strFrameworkDbFilePath = Tools.convertPathToCurrentOsType(strFrameworkDbFilePath);
60 try {
61 fdb = OpenFile.openFrameworkDb(strFrameworkDbFilePath);
62 } catch (XmlException e) {
63 Log.err("Open Framework Database " + strFrameworkDbFilePath, e.getMessage());
64 return;
65 } catch (Exception e) {
66 Log.err("Open Framework Database " + strFrameworkDbFilePath, "Invalid file type");
67 return;
68 }
69 }
70
71 /**
72 Get all modules' paths from one package
73
74 @return a Vector with all modules' path
75
76 **/
77 public Vector<String> getAllModulesOfPackage(String path) {
78 Vector<String> modulePath = new Vector<String>();
79 try {
80 MsaFiles files = OpenFile.openSpdFile(path).getMsaFiles();
81 if (files != null) {
82 for (int index = 0; index < files.getFilenameList().size(); index++) {
83 modulePath.addElement(files.getFilenameList().get(index));
84 }
85 }
86 } catch (IOException e) {
87 e.printStackTrace();
88 } catch (XmlException e) {
89 e.printStackTrace();
90 } catch (Exception e) {
91 e.printStackTrace();
92 }
93 return modulePath;
94 }
95
96 /**
97 Get all package basic information form the FrameworkDatabase.db file
98
99 @return vPackageList A vector includes all packages' basic information
100
101 */
102 public Vector<PackageIdentification> getAllPackages() {
103 Identification id = null;
104 vPackageList.removeAllElements();
105
106 openFrameworkDb();
107
108 for (int index = 0; index < fdb.getPackageList().getFilenameList().size(); index++) {
109 String path = Workspace.getCurrentWorkspace() + DataType.FILE_SEPARATOR
110 + fdb.getPackageList().getFilenameArray(index).getStringValue();
111 path = Tools.convertPathToCurrentOsType(path);
112 try {
113 id = getId(path, OpenFile.openSpdFile(path));
114 vPackageList.addElement(new PackageIdentification(id));
115 } catch (IOException e) {
116 Log.err("Open Package Surface Area " + path, e.getMessage());
117 e.printStackTrace();
118 } catch (XmlException e) {
119 Log.err("Open Package Surface Area " + path, e.getMessage());
120 e.printStackTrace();
121 } catch (Exception e) {
122 Log.err("Open Package Surface Area " + path, "Invalid file type");
123 e.printStackTrace();
124 }
125 }
126 return vPackageList;
127 }
128
129 /**
130 Get all package basic information form the FrameworkDatabase.db file
131
132 @return vPackageList A vector includes all packages' basic information
133
134 */
135 public Vector<ModuleIdentification> getAllModules() {
136 vModuleList.removeAllElements();
137 //
138 // First get all packages
139 //
140 getAllPackages();
141 Vector<String> modulePaths = new Vector<String>();
142 Identification id = null;
143 String packagePath = null;
144 String modulePath = null;
145
146 //
147 // For each package, get all modules list
148 //
149 for (int indexI = 0; indexI < vPackageList.size(); indexI++) {
150 packagePath = vPackageList.elementAt(indexI).getPath();
151 modulePaths = this.getAllModulesOfPackage(packagePath);
152 packagePath = packagePath.substring(0, packagePath.lastIndexOf(DataType.FILE_SEPARATOR)
153 + DataType.FILE_SEPARATOR.length());
154 for (int indexJ = 0; indexJ < modulePaths.size(); indexJ++) {
155 modulePath = Tools.convertPathToCurrentOsType(packagePath + modulePaths.elementAt(indexJ));
156 try {
157 id = getId(modulePath, OpenFile.openMsaFile(modulePath));
158 vModuleList.addElement(new ModuleIdentification(id, vPackageList.elementAt(indexI)));
159 } catch (IOException e) {
160 Log.err("Open Module Surface Area " + modulePath, e.getMessage());
161 e.printStackTrace();
162 } catch (XmlException e) {
163 Log.err("Open Module Surface Area " + modulePath, e.getMessage());
164 e.printStackTrace();
165 } catch (Exception e) {
166 Log.err("Open Module Surface Area " + modulePath, "Invalid file type");
167 e.printStackTrace();
168 }
169 }
170 }
171
172 return vModuleList;
173 }
174
175 /**
176 Get all platform basic information form the FrameworkDatabase.db file
177
178 @return vplatformList A vector includes all platforms' basic information
179
180 */
181 public Vector<PlatformIdentification> getAllPlatforms() {
182 Identification id = null;
183 vPlatformList.removeAllElements();
184
185 openFrameworkDb();
186
187 for (int index = 0; index < fdb.getPlatformList().getFilenameList().size(); index++) {
188 String path = Workspace.getCurrentWorkspace() + DataType.FILE_SEPARATOR
189 + fdb.getPlatformList().getFilenameArray(index).getStringValue();
190 path = Tools.convertPathToCurrentOsType(path);
191 try {
192 id = getId(path, OpenFile.openFpdFile(path));
193 vPlatformList.addElement(new PlatformIdentification(id));
194 } catch (IOException e) {
195 Log.err("Open Platform Surface Area " + path, e.getMessage());
196 e.printStackTrace();
197 } catch (XmlException e) {
198 Log.err("Open Platform Surface Area " + path, e.getMessage());
199 e.printStackTrace();
200 } catch (Exception e) {
201 Log.err("Open Platform Surface Area " + path, "Invalid file type");
202 e.printStackTrace();
203 }
204 }
205 return vPlatformList;
206 }
207
208 /**
209 Get all Library Class Definitions from a package
210
211 @return Vector
212 **/
213 public Vector<String> getAllLibraryClassDefinitionsFromPackage(PackageSurfaceArea spd) {
214 Vector<String> vector = new Vector<String>();
215 if (spd.getLibraryClassDeclarations() != null) {
216 if (spd.getLibraryClassDeclarations().getLibraryClassList().size() > 0) {
217 for (int index = 0; index < spd.getLibraryClassDeclarations().getLibraryClassList().size(); index++) {
218 vector.addElement(spd.getLibraryClassDeclarations().getLibraryClassList().get(index).getName());
219 }
220 }
221 }
222 return vector;
223 }
224
225 /**
226 Get all Protocol Definitions from a package
227
228 @return Vector
229 **/
230 public Vector<String> getAllProtocolDeclarationsFromPackage(PackageSurfaceArea spd) {
231 Vector<String> vector = new Vector<String>();
232 if (spd.getProtocolDeclarations() != null) {
233 if (spd.getProtocolDeclarations().getEntryList().size() > 0) {
234 for (int index = 0; index < spd.getProtocolDeclarations().getEntryList().size(); index++) {
235 vector.addElement(spd.getProtocolDeclarations().getEntryList().get(index).getCName());
236 }
237 }
238 }
239 return vector;
240 }
241
242 /**
243 Get all Ppi Definitions from a package
244
245 @return Vector
246 **/
247 public Vector<String> getAllPpiDeclarationsFromPackage(PackageSurfaceArea spd) {
248 Vector<String> vector = new Vector<String>();
249 if (spd.getPpiDeclarations() != null) {
250 if (spd.getPpiDeclarations().getEntryList().size() > 0) {
251 for (int index = 0; index < spd.getPpiDeclarations().getEntryList().size(); index++) {
252 vector.addElement(spd.getPpiDeclarations().getEntryList().get(index).getCName());
253 }
254 }
255 }
256 return vector;
257 }
258
259 /**
260 Get all Guid Definitions from a package
261
262 @return Vector
263 **/
264 public Vector<String> getAllGuidDeclarationsFromPackage(PackageSurfaceArea spd) {
265 Vector<String> vector = new Vector<String>();
266 if (spd.getGuidDeclarations() != null) {
267 if (spd.getGuidDeclarations().getEntryList().size() > 0) {
268 for (int index = 0; index < spd.getGuidDeclarations().getEntryList().size(); index++) {
269 vector.addElement(spd.getGuidDeclarations().getEntryList().get(index).getCName());
270 }
271 }
272 }
273 return vector;
274 }
275
276 /**
277 Get all Pcd Definitions from a package
278
279 @return Vector
280 **/
281 public Vector<String> getAllPcdDeclarationsFromPackage(PackageSurfaceArea spd) {
282 Vector<String> vector = new Vector<String>();
283 if (spd.getPcdDeclarations() != null) {
284 if (spd.getPcdDeclarations().getPcdEntryList().size() > 0) {
285 for (int index = 0; index < spd.getPcdDeclarations().getPcdEntryList().size(); index++) {
286 vector.addElement(spd.getPcdDeclarations().getPcdEntryList().get(index).getCName());
287 }
288 }
289 }
290 return vector;
291 }
292
293 public Vector<String> getAllLibraryClassDefinitionsFromWorkspace() {
294 //
295 // First get all packages
296 //
297 this.getAllPackages();
298
299 Vector<String> vector = new Vector<String>();
300 for (int index = 0; index < this.vPackageList.size(); index++) {
301 try {
302 Vector<String> v = getAllLibraryClassDefinitionsFromPackage(OpenFile
303 .openSpdFile(vPackageList
304 .get(index)
305 .getPath()));
306 if (v != null && v.size() > 0) {
307 vector.addAll(v);
308 }
309 } catch (IOException e) {
310 // TODO Auto-generated catch block
311 e.printStackTrace();
312 } catch (XmlException e) {
313 // TODO Auto-generated catch block
314 e.printStackTrace();
315 } catch (Exception e) {
316 // TODO Auto-generated catch block
317 e.printStackTrace();
318 }
319 }
320 return vector;
321 }
322
323 public Vector<String> getAllProtocolDeclarationsFromWorkspace() {
324 //
325 // First get all packages
326 //
327 this.getAllPackages();
328
329 Vector<String> vector = new Vector<String>();
330 for (int index = 0; index < this.vPackageList.size(); index++) {
331 try {
332 Vector<String> v = getAllProtocolDeclarationsFromPackage(OpenFile.openSpdFile(vPackageList.get(index)
333 .getPath()));
334 if (v != null && v.size() > 0) {
335 vector.addAll(v);
336 }
337 } catch (IOException e) {
338 // TODO Auto-generated catch block
339 e.printStackTrace();
340 } catch (XmlException e) {
341 // TODO Auto-generated catch block
342 e.printStackTrace();
343 } catch (Exception e) {
344 // TODO Auto-generated catch block
345 e.printStackTrace();
346 }
347 }
348 return vector;
349 }
350
351 public Vector<String> getAllPpiDeclarationsFromWorkspace() {
352 //
353 // First get all packages
354 //
355 this.getAllPackages();
356
357 Vector<String> vector = new Vector<String>();
358 for (int index = 0; index < this.vPackageList.size(); index++) {
359 try {
360 Vector<String> v = getAllPpiDeclarationsFromPackage(OpenFile.openSpdFile(vPackageList.get(index)
361 .getPath()));
362 if (v != null && v.size() > 0) {
363 vector.addAll(v);
364 }
365 } catch (IOException e) {
366 // TODO Auto-generated catch block
367 e.printStackTrace();
368 } catch (XmlException e) {
369 // TODO Auto-generated catch block
370 e.printStackTrace();
371 } catch (Exception e) {
372 // TODO Auto-generated catch block
373 e.printStackTrace();
374 }
375 }
376 return vector;
377 }
378
379 public Vector<String> getAllGuidDeclarationsFromWorkspace() {
380 //
381 // First get all packages
382 //
383 this.getAllPackages();
384
385 Vector<String> vector = new Vector<String>();
386 for (int index = 0; index < this.vPackageList.size(); index++) {
387 try {
388 Vector<String> v = getAllGuidDeclarationsFromPackage(OpenFile.openSpdFile(vPackageList.get(index)
389 .getPath()));
390 if (v != null && v.size() > 0) {
391 vector.addAll(v);
392 }
393 } catch (IOException e) {
394 // TODO Auto-generated catch block
395 e.printStackTrace();
396 } catch (XmlException e) {
397 // TODO Auto-generated catch block
398 e.printStackTrace();
399 } catch (Exception e) {
400 // TODO Auto-generated catch block
401 e.printStackTrace();
402 }
403 }
404 return vector;
405 }
406
407 public Vector<String> getAllPcdDeclarationsFromWorkspace() {
408 //
409 // First get all packages
410 //
411 this.getAllPackages();
412
413 Vector<String> vector = new Vector<String>();
414 for (int index = 0; index < this.vPackageList.size(); index++) {
415 try {
416 Vector<String> v = getAllPcdDeclarationsFromPackage(OpenFile.openSpdFile(vPackageList.get(index)
417 .getPath()));
418 if (v != null && v.size() > 0) {
419 vector.addAll(v);
420 }
421 } catch (IOException e) {
422 // TODO Auto-generated catch block
423 e.printStackTrace();
424 } catch (XmlException e) {
425 // TODO Auto-generated catch block
426 e.printStackTrace();
427 } catch (Exception e) {
428 // TODO Auto-generated catch block
429 e.printStackTrace();
430 }
431 }
432 return vector;
433 }
434
435 /**
436 Find a module's package's id
437
438 @param id input module id
439 @return package id
440
441 **/
442 public PackageIdentification getPackageIdByModuleId(Identification id) {
443 getAllPackages();
444 Vector<String> modulePaths = new Vector<String>();
445 Identification mid = null;
446 String packagePath = null;
447 String modulePath = null;
448
449 //
450 // For each package, get all modules list
451 //
452 for (int indexI = 0; indexI < vPackageList.size(); indexI++) {
453 packagePath = vPackageList.elementAt(indexI).getPath();
454 modulePaths = this.getAllModulesOfPackage(packagePath);
455 packagePath = packagePath.substring(0, packagePath.lastIndexOf(DataType.FILE_SEPARATOR)
456 + DataType.FILE_SEPARATOR.length());
457 for (int indexJ = 0; indexJ < modulePaths.size(); indexJ++) {
458 modulePath = Tools.convertPathToCurrentOsType(packagePath + modulePaths.elementAt(indexJ));
459 try {
460 mid = getId(modulePath, OpenFile.openMsaFile(modulePath));
461 //
462 // Check id
463 //
464 if (mid.equals(id)) {
465 return vPackageList.elementAt(indexI);
466 }
467 } catch (IOException e) {
468 e.printStackTrace();
469 } catch (XmlException e) {
470 e.printStackTrace();
471 } catch (Exception e) {
472 e.printStackTrace();
473 }
474 }
475 }
476
477 return null;
478 }
479
480 public Identification getId(String path, ModuleSurfaceArea msa) {
481 MsaHeader head = msa.getMsaHeader();
482 String name = head.getModuleName();
483 String guid = head.getGuidValue();
484 String version = head.getVersion();
485 Identification id = new Identification(name, guid, version, path);
486 return id;
487 }
488
489 public Identification getId(String path, PackageSurfaceArea spd) {
490 SpdHeader head = spd.getSpdHeader();
491 String name = head.getPackageName();
492 String guid = head.getGuidValue();
493 String version = head.getVersion();
494 Identification id = new Identification(name, guid, version, path);
495 return id;
496 }
497
498 public Identification getId(String path, PlatformSurfaceArea fpd) {
499 PlatformHeader head = fpd.getPlatformHeader();
500 String name = head.getPlatformName();
501 String guid = head.getGuidValue();
502 String version = head.getVersion();
503 Identification id = new Identification(name, guid, version, path);
504 return id;
505 }
506
507 /**
508 Add module information to package surface area
509
510 @param mid
511 @throws IOException
512 @throws XmlException
513 @throws Exception
514
515 **/
516 public void addModuleToPackage(ModuleIdentification mid, PackageSurfaceArea spd) throws IOException, XmlException,
517 Exception {
518 String packagePath = mid.getPackageId().getPath();
519 String modulePath = mid.getPath();
520 if (spd == null) {
521 spd = OpenFile.openSpdFile(packagePath);
522 }
523 MsaFiles msaFile = spd.getMsaFiles();
524 if (msaFile == null) {
525 msaFile = MsaFiles.Factory.newInstance();
526 }
527 packagePath = packagePath.substring(0, packagePath.lastIndexOf(DataType.FILE_SEPARATOR));
528 String fn = Tools.getRelativePath(modulePath, packagePath);
529 msaFile.addNewFilename();
530 msaFile.setFilenameArray(msaFile.getFilenameList().size() - 1, fn);
531 spd.setMsaFiles(msaFile);
532 SaveFile.saveSpdFile(mid.getPackageId().getPath(), spd);
533 }
534
535 /**
536 Add input package into database
537
538 @param id
539 * @throws Exception
540
541 **/
542 public void addPackageToDatabase(PackageIdentification id) throws Exception {
543 String path = id.getPath();
544 path = Tools.getRelativePath(path, Workspace.getCurrentWorkspace());
545 this.openFrameworkDb();
546 DbPathAndFilename filename = DbPathAndFilename.Factory.newInstance();
547 filename.setStringValue(path);
548 fdb.getPackageList().addNewFilename();
549 fdb.getPackageList().setFilenameArray(fdb.getPackageList().sizeOfFilenameArray() - 1, filename);
550 String strFrameworkDbFilePath = Workspace.getCurrentWorkspace() + Workspace.getStrWorkspaceDatabaseFile();
551 strFrameworkDbFilePath = Tools.convertPathToCurrentOsType(strFrameworkDbFilePath);
552 SaveFile.saveDbFile(strFrameworkDbFilePath, fdb);
553 }
554
555 /**
556 Add input package into database
557
558 @param id
559 * @throws Exception
560
561 **/
562 public void addPlatformToDatabase(PlatformIdentification id) throws Exception {
563 String path = id.getPath();
564 path = Tools.getRelativePath(path, Workspace.getCurrentWorkspace());
565 this.openFrameworkDb();
566 DbPathAndFilename filename = DbPathAndFilename.Factory.newInstance();
567 filename.setStringValue(path);
568 fdb.getPlatformList().addNewFilename();
569 fdb.getPlatformList().setFilenameArray(fdb.getPlatformList().sizeOfFilenameArray() - 1, filename);
570 String strFrameworkDbFilePath = Workspace.getCurrentWorkspace() + Workspace.getStrWorkspaceDatabaseFile();
571 strFrameworkDbFilePath = Tools.convertPathToCurrentOsType(strFrameworkDbFilePath);
572 SaveFile.saveDbFile(strFrameworkDbFilePath, fdb);
573 }
574 }