summaryrefslogtreecommitdiff
path: root/Tools/Source/MigrationTools/org/tianocore/migration/MsaWriter.java
blob: 3db25079057b4d98851bbd705448a9ce6693b4fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/** @file
 
 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
 
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 **/
package org.tianocore.migration;

import java.io.*;
import java.util.*;

import org.tianocore.*;
import org.tianocore.SupportedArchitectures.Enum;
import org.apache.xmlbeans.*;

public class MsaWriter {
    MsaWriter(ModuleInfo moduleinfo) {
        mi = moduleinfo;
    }
    
    private ModuleInfo mi;
    private ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.newInstance();
    
    private ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc.addNewModuleSurfaceArea();
    private MsaHeaderDocument.MsaHeader msaheader = msa.addNewMsaHeader();
    private ModuleDefinitionsDocument.ModuleDefinitions md = msa.addNewModuleDefinitions();
    private SourceFilesDocument.SourceFiles sourcefiles = msa.addNewSourceFiles();    //found local .h files are not written
    private GuidsDocument.Guids guids;
    private ProtocolsDocument.Protocols protocols;
    private PPIsDocument.PPIs ppis;
    private PackageDependenciesDocument.PackageDependencies pd = msa.addNewPackageDependencies();
    private LibraryClassDefinitionsDocument.LibraryClassDefinitions libclassdefs = msa.addNewLibraryClassDefinitions();
    private ExternsDocument.Externs externs = msa.addNewExterns();
    
    private String Query (String requirement) throws Exception {
        String answer;
        BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
        System.out.println(requirement);
        while ((answer = rd.readLine()).length() == 0) ;
        return answer;
    }
    
    private void addSourceFiles (String name) {                        // furthur modification needed
        List<Enum> arch = new ArrayList<Enum>();
        FilenameDocument.Filename filename;
        filename = sourcefiles.addNewFilename();
        filename.setStringValue(name);
        
        if (name.contains("x64" + File.separator)) {                // filename ???
            arch.add(SupportedArchitectures.X_64);
            System.out.println("x64" + File.separator);
            filename.setSupArchList(arch);
        } else if (name.contains("Ia32" + File.separator)) {        // filename ???
            arch.add(SupportedArchitectures.IA_32);
            System.out.println("Ia32" + File.separator);
            filename.setSupArchList(arch);
        } else if (name.contains("Ipf" + File.separator)) {            // filename ???
            arch.add(SupportedArchitectures.IPF);
            System.out.println("Ipf" + File.separator);
            filename.setSupArchList(arch);
        } else if (name.contains("Ebc" + File.separator)) {            // filename ???
            arch.add(SupportedArchitectures.EBC);
            System.out.println("Ebc" + File.separator);
            filename.setSupArchList(arch);
        }
    }
    
    private ModuleSurfaceAreaDocument fulfillMsadoc() throws Exception {
        Iterator<String> it;
        String temp;
        
        if (mi.modulename != null) {
            msaheader.setModuleName(mi.modulename);
        } else {
            msaheader.setModuleName(mi.modulename = Query("Module Name Not Found!  Please Input ModuleName"));
        }
        if (mi.guidvalue == null) {
          mi.guidvalue = UUID.randomUUID().toString();
          MigrationTool.ui.println ("Guid value can not be retrieved from inf file. Generate " + mi.guidvalue + " at random!"); 
        } 
        msaheader.setGuidValue(mi.guidvalue);
        if (mi.moduletype != null) {
            msaheader.setModuleType(ModuleTypeDef.Enum.forString(mi.getModuleType()));
        } else {
            msaheader.setModuleType(ModuleTypeDef.Enum.forString(mi.moduletype = Query("Guid Value Not Found!  Please Input Guid Value")));
        }

        msaheader.setCopyright("Copyright (c) 2006, Intel Corporation");
        msaheader.setVersion("1.0");
        msaheader.setAbstract("Component name for module " + mi.modulename);
        msaheader.setDescription("FIX ME!");
        msaheader.addNewLicense().setStringValue("All rights reserved.\n" +
                "      This software and associated documentation (if any) is furnished\n" +
                "      under a license and may only be used or copied in accordance\n" +
                "      with the terms of the license. Except as permitted by such\n" +
                "      license, no part of this software or documentation may be\n" +
                "      reproduced, stored in a retrieval system, or transmitted in any\n" +
                "      form or by any means without the express written consent of\n" +
                "      Intel Corporation.");
        msaheader.setSpecification("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION   0x00000052");
        
        List<Enum> arch = new ArrayList<Enum>();
        arch.add(SupportedArchitectures.IA_32);
        arch.add(SupportedArchitectures.X_64);
        arch.add(SupportedArchitectures.IPF);
        arch.add(SupportedArchitectures.EBC);
        md.setSupportedArchitectures(arch);
        md.setBinaryModule(false);
        md.setOutputFileBasename(mi.modulename);
        
        pd.addNewPackage().setPackageGuid("5e0e9358-46b6-4ae2-8218-4ab8b9bbdcec");
        pd.addNewPackage().setPackageGuid("68169ab0-d41b-4009-9060-292c253ac43d");
        externs.addNewSpecification().setStringValue("EFI_SPECIFICATION_VERSION 0x00020000");
        externs.addNewSpecification().setStringValue("EDK_RELEASE_VERSION 0x00020000");
        if (mi.entrypoint != null) {
            externs.addNewExtern().setModuleEntryPoint(mi.entrypoint);
            org.tianocore.ModuleTypeDef.Enum moduleType = msaheader.getModuleType();
            if (moduleType == ModuleTypeDef.PEIM) {
                mi.hashrequiredr9libs.add("PeimEntryPoint");
            } else {
                mi.hashrequiredr9libs.add("UefiDriverEntryPoint");
            }
        }
       
        it = mi.localmodulesources.iterator();
        while (it.hasNext()) {
            addSourceFiles(it.next());
        }
        if (!mi.protocols.isEmpty()) {
            protocols = msa.addNewProtocols();
            it = mi.protocols.iterator();
            while (it.hasNext()) {
                if ((temp = it.next()) != null) {
                    ProtocolsDocument.Protocols.Protocol pr = protocols.addNewProtocol();
                    pr.setProtocolCName(temp);
                    pr.setUsage(UsageTypes.ALWAYS_CONSUMED);
                }
            }
        }
        if (!mi.ppis.isEmpty()) {
            ppis = msa.addNewPPIs();
            it = mi.ppis.iterator();
            while (it.hasNext()) {
                if ((temp = it.next()) != null) {
                    PPIsDocument.PPIs.Ppi pp = ppis.addNewPpi();
                    pp.setPpiCName(temp);
                    pp.setUsage(UsageTypes.ALWAYS_CONSUMED);
                }
            }
        }
        if (!mi.guids.isEmpty()) {
            guids = msa.addNewGuids();
            it = mi.guids.iterator();
            while (it.hasNext()) {
                if ((temp = it.next()) != null) {
                    GuidsDocument.Guids.GuidCNames gcn = guids.addNewGuidCNames();
                    gcn.setGuidCName(temp);
                    gcn.setUsage(UsageTypes.ALWAYS_CONSUMED);
                }
            }
        }
        it = mi.hashrequiredr9libs.iterator();
        while (it.hasNext()) {
            if ((temp = it.next()) != null && !temp.matches("%") && !temp.matches("n/a")) {
                LibraryClassDocument.LibraryClass lc = libclassdefs.addNewLibraryClass();
                lc.setKeyword(temp);
                lc.setUsage(UsageTypes.ALWAYS_CONSUMED);
            }
        }
        
        return msadoc;
    }
    
    public void flush() throws Exception {
        XmlOptions options = new XmlOptions();

        options.setCharacterEncoding("UTF-8");
        options.setSavePrettyPrint();
        options.setSavePrettyPrintIndent(2);
        options.setUseDefaultNamespace();
        
        BufferedWriter bw = new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + mi.modulename + ".msa"));
        fulfillMsadoc().save(bw, options);
        //MsaTreeEditor.init(mi, ui, msadoc);
        bw.flush();
        bw.close();
    }

    private static void flush(String path, ModuleSurfaceAreaDocument msadoc) throws Exception {
        XmlOptions options = new XmlOptions();

        options.setCharacterEncoding("UTF-8");
        options.setSavePrettyPrint();
        options.setSavePrettyPrintIndent(2);
        options.setUseDefaultNamespace();

        BufferedWriter bw = new BufferedWriter(new FileWriter(path));
        msadoc.save(bw, options);
        bw.flush();
        bw.close();
    }

    public static final void parse(String msafile) throws Exception {
        ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.parse(msafile);
        flush("c:\\temp.msa", msadoc);
    }
}