summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/GenBuild/org/tianocore/build/toolchain/ToolChainInfo.java
blob: 9952c0beda25cbdeb11e353095e0d77e27fca9ba (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/** @file
ToolChainInfo class

This file is to define ToolChainInfo class.

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.build.toolchain;

import java.util.LinkedHashSet;
import java.util.Set;

/**
   ToolChainInfo collects valid build targets, tool chain tag, ARCHs and commands 
   information for real build use.
 **/
public class ToolChainInfo {
    //
    // build target set
    // 
    private Set<String> targets = new LinkedHashSet<String>();
    //
    // tool chain tag name set
    // 
    private Set<String> tagnames = new LinkedHashSet<String>();
    //
    // build archs set
    // 
    private Set<String> archs = new LinkedHashSet<String>();
    //
    // build commands set
    // 
    private Set<String> commands = new LinkedHashSet<String>();

    /**
     Add a list of targets in the form of string separated by space

     @param targetList target list string
     **/
    public void addTargets(String targetList) {
        //
        // targetList some targets separated by space " "
        //
        if (targetList == null || targetList.length() == 0) {
            targets.add("*");
        } else {
            addTargets(targetList.split(" "));
        }
    }

    /**
     Add a list of targets in the form of string array
     
     @param targetArray target string array
     **/
    public void addTargets(String[] targetArray) {
        if (targetArray != null ) {
            for (int i = 0; i < targetArray.length; i++) {
                targets.add(targetArray[i]);
            }
        }
    }

    /**
     Add a list of target in the form of set
     
     @param targetSet target string set
     **/
    public void addTargets(Set<String> targetSet) {
        if (targetSet != null) {
            targets.addAll(targetSet);
        }
    }

    /**
       Add a list of tool chain tag name in the form of string separated by space

       @param tagnameList   Tool chain tag name list string
     **/
    public void addTagnames(String tagnameList) {
        //
        // tagnameList some tagnames separated by space " "
        //
        if (tagnameList == null || tagnameList.length() == 0) {
            tagnames.add("*");
        } else {
            addTagnames(tagnameList.split(" "));
        }
    }

    /**
       Add a list of tool chain tag name in the form of string array
       
       @param tagnameArray  Tool chain tag names array
     **/
    public void addTagnames(String[] tagnameArray) {
        if (tagnameArray != null ) {
            for (int i = 0; i < tagnameArray.length; i++) {
                tagnames.add(tagnameArray[i]);
            }
        }
    }

    /**
       Add a list of tool chain tag name in the form of Set
       
       @param tagnameSet    Tool chain tag names set
     **/
    public void addTagnames(Set<String> tagnameSet) {
        if (tagnameSet != null) {
            tagnames.addAll(tagnameSet);
        }
    }

    /**
       Add a list of ARCH in the form of string
       
       @param archList  ARCH string
     **/
    public void addArchs(String archList) {
        //
        // archList some archs separated by space " "
        //
        if (archList == null || archList.length() == 0) {
            archs.add("*");
        } else {
            addArchs(archList.split(" "));
        }
    }

    /**
       Add a list of ARCH in the form of string array
       
       @param archArray ARCH array
     **/
    public void addArchs(String[] archArray) {
        if (archArray != null ) {
            for (int i = 0; i < archArray.length; i++) {
                archs.add(archArray[i]);
            }
        }
    }

    /**
       Add a list of ARCH in the form of set
       
       @param archSet   ARCH set
     **/
    public void addArchs(Set<String> archSet) {
        if (archSet != null) {
            archs.addAll(archSet);
        }
    }

    /**
       Add a list of command in the form of string
       
       @param commandList   Command list string
     **/
    public void addCommands(String commandList) {
        //
        // archList some archs separated by space " "
        //
        if (commandList == null || commandList.length() == 0) {
            commands.add("*");
        } else {
            addCommands(commandList.split(" "));
        }
    }

    /**
       Add a list of ARCH in the form of array
       
       @param commandArray  Commands array
     **/
    public void addCommands(String[] commandArray) {
        if (commandArray != null ) {
            for (int i = 0; i < commandArray.length; i++) {
                commands.add(commandArray[i]);
            }
        }
    }

    /**
       Add a list of ARCH in the form of set
       
       @param commandSet    Commands set
     **/
    public void addCommands(Set<String> commandSet) {
        if (commandSet != null) {
            commands.addAll(commandSet);
        }
    }

    /**
       Make a union operation on this ToolChainInfo and the given one.

       @param info  Another ToolChainInfo object to merge with
       
       @return ToolChainInfo    Merged ToolChainInfo object
     **/
    public ToolChainInfo union(ToolChainInfo info) {
        ToolChainInfo result = new ToolChainInfo();
        result.addTargets(union(this.targets, info.targets));
        result.addTagnames(union(this.tagnames, info.tagnames));
        result.addArchs(union(this.archs, info.archs));
        return result;
    }

    /**
       Make a intersection operation on this ToolChainInfo and the given one

       @param info  Another ToolChainInfo object to intersect with
       
       @return ToolChainInfo    Intersected ToolChainInfo object
     **/
    public ToolChainInfo intersection(ToolChainInfo info) {
        ToolChainInfo result = new ToolChainInfo();
        result.addTargets(intersection(this.targets, info.targets));
        result.addTagnames(intersection(this.tagnames, info.tagnames));
        result.addArchs(intersection(this.archs, info.archs));
        return result;
    }

    /**
       Make a union operation on two Sets

       @param set1  One Set
       @param set2  Another Set
       
       @return Set<String>  Merged Set object
     **/
    private Set<String> union(Set<String> set1, Set<String> set2) {
        Set<String> result = new LinkedHashSet<String>();
        result.addAll(set1);
        result.addAll(set2);
        result.remove("*");
        return result;
    }

    /**
       Make a intersection operation on two Sets with the consideration of wildcard.

       @param set1  One Set
       @param set2  Another Set
       
       @return Set<String>  The intersected Set object
     **/
    private Set<String> intersection(Set<String> set1, Set<String> set2) {
        Set<String> result = new LinkedHashSet<String>();
        boolean set1HasWildcard = set1.contains("*");
        boolean set2HasWildcard = set2.contains("*");

        if (set1HasWildcard && set2HasWildcard) {
            //
            // Both Sets have wildcard, the result will have all elements in them
            // 
            result.addAll(set1);
            result.addAll(set2);
        } else if (set1HasWildcard) {
            //
            // Only set1 has wildcard, then result will have only set2 elements.
            // 
            result.addAll(set2);
        } else if (set2HasWildcard) {
            //
            // Only set2 has wildcard, then result will have only set1 elements.
            // 
            result.addAll(set1);
        } else {
            //
            // No wildcard in both Sets, the result will have the elements in both Sets.
            // 
            result.addAll(set1);
            result.retainAll(set2);
        }

        return result;
    }

    /**
       Get target array.

       @return String[]
     **/
    public String[] getTargets() {
        return (String[])targets.toArray(new String[targets.size()]);
    }

    /**
       Get tool chain tag name array.

       @return String[]
     **/
    public String[] getTagnames() {
        return (String[])tagnames.toArray(new String[tagnames.size()]);
    }

    /**
       Get ARCH array.

       @return String[]
     **/
    public String[] getArchs() {
        return (String[])archs.toArray(new String[archs.size()]);
    }

    /**
       Get command name array.

       @return String[]
     **/
    public String[] getCommands() {
        return (String[])commands.toArray(new String[commands.size()]);
    }

    /**
       Override the Object's toString().

       @return String
     **/
    public String toString() {
        return  "  TARGET :" + targets + "\n" + 
                "  TAGNAME:" + tagnames + "\n" + 
                "  ARCH   :" + archs + "\n" + 
                "  COMMAND:" + commands;
    }

    /**
       Remove the wildcard element in the tool chain information because they
       are useless when retrieved.
     **/
    public void normalize() {
        targets.remove("*");
        tagnames.remove("*");
        archs.remove("*");
        commands.remove("*");
    }
}