summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/toolchain/ToolChainId.java
blob: 65e20bd66519dd675993789198d904d22c5aa534 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/** @file

 This file is used to init tool chain and tool preference data
 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.frameworkwizard.toolchain;

import java.io.*;
import java.io.File;
import java.lang.Integer;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import org.tianocore.frameworkwizard.FrameworkWizardUI;
import org.tianocore.frameworkwizard.common.DataType;
import org.tianocore.frameworkwizard.common.Tools;
import org.tianocore.frameworkwizard.common.Log;
import org.tianocore.frameworkwizard.workspace.Workspace;

public class ToolChainId {
    //
    // Define class members
    //
    
    private final boolean Debug = false;
    
    private String toolDefinitionFile = null;

    private final int toolDefFieldCount = 5;

    private String toolsDefIdentifier = null;

    private String toolsDefTargetNames = null;

    private final int toolsDefTargetNameField = 0;

    private String toolsDefTagNames = null;

    private final int toolsDefTagNameField = 1;

    private String toolsDefArchNames = null;

    private final int toolsDefArchNameField = 2;

    private final int toolsDefToolArgumentField = 4;
    
    private String toolCommandCodes = null;
    
    private final int toolsDefCmdCodeArgumentField = 3;
    

    private String activePlatform = null;

    private String targetArchs = null;

    private String tagNames = null;

    private String buildTargets = null;

    private String toolFamilies = null;
    
    private ArrayList<String> toolDefinitionContents = new ArrayList<String>(50);

    private static boolean threadEnabled = false;

    private static int maxThreadCount = 0;

    private String toolsDir = Workspace.getCurrentWorkspace() + System.getProperty("file.separator") + "Tools"
                              + System.getProperty("file.separator") + "Conf";

    private String strTargetFile = toolsDir + DataType.FILE_SEPARATOR + "target.txt";

    private String defaultToolsConf = toolsDir + DataType.FILE_SEPARATOR + "tools_def.txt";

    public void init() {
        readTargetTxtFile();
        readToolDefinitionFile();
    }
    
    public ToolChainId() {
        super();
        init();
    }

    public String getToolDefinitionFile() {
        return toolDefinitionFile;
    }

    public String getActivePlatform() {
        return activePlatform;
    }

    public String getBuildTargets() {
        return buildTargets;
    }

    public String getTagNames() {
        return tagNames;
    }

    public String getTargetArchitectures() {
        return targetArchs;
    }

    public boolean getThreadEnabled() {
        return threadEnabled;
    }

    public int getMaxThreadCount() {
        return maxThreadCount;
    }

    public String getToolFamilies() {
        return toolFamilies;
    }
    
    public String getToolDefinitionIdentifier() {
        return toolsDefIdentifier;
    }
    
    public ArrayList<String> getToolDefinitionStatements() {
        return toolDefinitionContents;
    }

    public String getToolsDefTagNames() {
        return toolsDefTagNames;
    }
    
    public String getToolsDefTargetNames() {
        return toolsDefTargetNames;
    }
    
    public String getToolsDefCommandCodes() {
        return toolCommandCodes;
    }
    
    public String getToolsDefArchNames() {
        return toolsDefArchNames;
    }
    
    private void readTargetTxtFile() {
        File tFile = new File(strTargetFile);

        if (tFile.exists()) {
            try {
                FileReader fileReader = new FileReader(strTargetFile);
                BufferedReader reader = new BufferedReader(fileReader);
                String rLine = null;
                String inLine[] = new String[2];
                while ((rLine = reader.readLine()) != null) {
                	rLine = rLine.trim();
                    if ((rLine.startsWith("ACTIVE_PLATFORM")) && (activePlatform == null)) {
                        // Only one active platform is permitted!
                        inLine = rLine.split("=");
                        if (inLine.length > 1) {
                        	activePlatform = inLine[1].trim();
                        }
                    }
                    if ((rLine.startsWith("TARGET" + " ")) || (rLine.startsWith("TARGET" + "\t"))
                        || (rLine.startsWith("TARGET="))) {
                        // Handle multiple Target Names
                        if (rLine.contains(",")) {
                            inLine = rLine.split("=");
                            if (inLine.length > 1) {
                            	buildTargets = inLine[1].trim().replaceAll(",", " ");
                            }
                        } else {
                            inLine = rLine.trim().split("=");
                            if (inLine.length > 1) {
                            	buildTargets = inLine[1].trim();
                            }
                        }
                    }
                    if (rLine.startsWith("TARGET_ARCH")) {
                        // Handle multiple Target Architectures
                        if (rLine.contains(",")) {
                            inLine = rLine.split("=");
                            if (inLine.length > 1) {
                            	targetArchs = inLine[1].trim().replaceAll(",", " ");
                            }
                        } else {
                            inLine = rLine.split("=");
                            if (inLine.length > 1) {
                            	targetArchs = inLine[1].trim();
                            }
                        }
                    }
                    if (rLine.startsWith("TOOL_CHAIN_CONF")) {
                        // Only one file is permitted
                        inLine = rLine.split("=");
                        if (inLine.length > 1) {
                        	toolDefinitionFile = inLine[1].trim();
                        }
                    }

                    if (rLine.startsWith("TOOL_CHAIN_TAG")) {
                        // Handle multiple Tool TagNames
                        if (rLine.contains(",")) {
                            inLine = rLine.split("=");
                            if (inLine.length > 1) {
                            	tagNames = inLine[1].trim().replaceAll(",", " ");
                            }
                        } else {
                            inLine = rLine.split("=");
                            if (inLine.length > 1) {
                            	tagNames = inLine[1].trim();
                            }
                        }
                    }

                    if (rLine.startsWith("MULTIPLE_THREAD")) {
                        // Handle Thread Enable flag
                        if ((rLine.toLowerCase().contains("enabled"))
                            || (rLine.toLowerCase().contains("true"))) {
                            threadEnabled = true;
                        } else {
                            threadEnabled = false;
                        }
                    }

                    if (rLine.startsWith("MAX_CONCURRENT_THREAD_NUMBER")) {
                        // Handle Thread Enable flag
                        inLine = rLine.split("=");
                        if (inLine.length > 1) {
                        	maxThreadCount = Integer.valueOf(inLine[1].trim());
                        }
                    }
                }
                reader.close();
            } catch (IOException e) {
                Log.log(this.strTargetFile + " Read Error ", e.getMessage());
                e.printStackTrace();
            }
        } else {
            JOptionPane.showMessageDialog(FrameworkWizardUI.getInstance(), "<html>" + "Tool Preferences file: <br>" + strTargetFile
                                                + "<br>does not exist!</html>");
        }
    }

    private void readToolDefinitionFile() {

        // Parse the tool definition file looking for targets and architectures
        String toolsConfFile = null;
        if (toolDefinitionFile != null) {
            String resString = (Tools.convertPathToCurrentOsType(toolDefinitionFile)).trim();
            toolsConfFile = Workspace.getCurrentWorkspace() + System.getProperty("file.separator") + resString.trim();
            File toolsDefFile = new File(toolsConfFile);
            if (!toolsDefFile.exists()) {
                JOptionPane.showMessageDialog(FrameworkWizardUI.getInstance(), "<html>" + "Tool Definition file, " + toolDefinitionFile
                                                    + "<br>specified in the target.txt file does not exist!"
                                                    + "<br>Using the default Tool Definition File:<br>"
                                                    + defaultToolsConf);
                toolsConfFile = defaultToolsConf;
            }
        } else {
            toolsConfFile = defaultToolsConf;
        }
        String[] toolsDefFields = new String[toolDefFieldCount];
        for (int i = 0; i < toolDefFieldCount; i++)
            toolsDefFields[i] = null;
        File toolDefFile = new File(toolsConfFile);
        if (toolDefFile.exists()) {
            try {
                FileReader fileReader = new FileReader(toolDefFile);
                BufferedReader reader = new BufferedReader(fileReader);
                String rLine = null;
                String result[];
                while ((rLine = reader.readLine()) != null) {
                    if ((rLine.startsWith("IDENTIFIER")) && (toolsDefIdentifier == null)) {
                        result = rLine.split("=");
                        toolsDefIdentifier = (result[1]).trim();
                    } else if ((!rLine.startsWith("#")) && (rLine.contains("="))) {
                        result = rLine.split("=");
                        toolsDefFields = ((result[0]).trim()).split("_");
                        if (toolsDefTargetNames == null) {
                            toolsDefTargetNames = (toolsDefFields[toolsDefTargetNameField]).toUpperCase().trim() + " ";
                        } else if (!toolsDefTargetNames.contains((toolsDefFields[toolsDefTargetNameField]).toUpperCase().trim())) {
                            toolsDefTargetNames += (toolsDefFields[toolsDefTargetNameField]).toUpperCase().trim() + " ";
                        }
                        if (toolsDefTagNames == null) {
                            toolsDefTagNames = (toolsDefFields[toolsDefTagNameField]).toUpperCase().toUpperCase().trim() + " ";
                        } else if (!toolsDefTagNames.contains((toolsDefFields[toolsDefTagNameField]).toUpperCase().trim())) {
                            toolsDefTagNames += (toolsDefFields[toolsDefTagNameField]).toUpperCase().trim() + " ";
                        }
                        if (toolsDefArchNames == null) {
                            toolsDefArchNames = (toolsDefFields[toolsDefArchNameField]).toUpperCase().trim() + " ";
                        } else if (!toolsDefArchNames.contains((toolsDefFields[toolsDefArchNameField]).toUpperCase().trim())) {
                            toolsDefArchNames += (toolsDefFields[toolsDefArchNameField]).toUpperCase().trim() + " ";
                        }
                        if ((toolFamilies == null) && (rLine.trim().contains("FAMILY"))) {
                            toolFamilies = (toolsDefFields[toolsDefToolArgumentField]).toUpperCase().trim() + " ";
                        } else if ((rLine.trim().contains("FAMILY"))
                                   && (!toolFamilies.contains((toolsDefFields[toolsDefToolArgumentField]).toUpperCase().trim()))) {
                            toolFamilies += (toolsDefFields[toolsDefToolArgumentField]).toUpperCase().trim() + " ";
                        }
                        if ((toolCommandCodes == null)) {
                            toolCommandCodes = (toolsDefFields[toolsDefCmdCodeArgumentField]).toUpperCase().trim() + " ";
                        } else if ((!toolCommandCodes.contains((toolsDefFields[toolsDefCmdCodeArgumentField]).toUpperCase().trim()))) {
                            toolCommandCodes += (toolsDefFields[toolsDefCmdCodeArgumentField].toUpperCase().trim()) + " ";
                        }
                        
                        toolDefinitionContents.add(rLine.trim().replaceAll(" ", ""));
                    }
                }
                reader.close();
                if (!toolsDefTargetNames.matches("[A-Z]+")) {
                    toolsDefTargetNames = toolsDefTargetNames.replace("* ", "").trim();
                    if (Debug)
                        System.out.println("tools_def file does not define build targets: '" + toolsDefTargetNames
                                           + "'");
                }
            } catch (IOException e) {
                Log.log(toolsConfFile + " Read Error ", e.getMessage());
                e.printStackTrace();
            }
        }
    }

}