summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/GenBuild/org/tianocore/build/toolchain/ToolChainMap.java
blob: 18e664ccf86c75d5e6a200a9b870c99dc348231c (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
/** @file
ToolChainMap class

ToolChainMap class is used for storing tool chain configurations.

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.HashMap;
import java.util.Map;
import java.util.Set;

/**
 ToolChainMap is a wrapper class for a generic Map class which uses ToolChainKey
 class as its key. It's used to store and retrieve tool chain configuration 
 information.
 **/
public class ToolChainMap {
    //
    // From which part of key can be used to match "*"
    // 
    private int matchLevel = ToolChainKey.keyLength - 1;

    //
    // A Map object in which tool chain configuration information will be stored
    // 
    private Map<ToolChainKey, String> map = null;

    /**
       Public constructor. It just initializes the private Map object.
     **/
    public ToolChainMap() {
        this.map = new HashMap<ToolChainKey, String>();
    }

    /**
       Wrapper function for Map.put(). It's used when default delimiter of
       ToolChainKey is not wanted and will be overrided by "delimiter" parameter.

       @param key           Key string which is concatenated with "delimiter"
       @param delimiter     The delimiter string in the key string
       @param value         Value string associated with the "key"
       
       @retval String       The "value" string if the "key" is valid.
       @retval null         if the "key" is invalid
     **/
    public String put(String key, String delimiter, String value) {
        ToolChainKey toolChainKey;

        try {
            toolChainKey = new ToolChainKey(key, delimiter);
        } catch (Exception e) {
            return null;
        }
        return (String)map.put(toolChainKey, value);
    }

    /**
       Wrapper function for Map.put().

       @param key           Key string which is concatenated with default "delimiter"
       @param value         Value string associated with the "key"
       
       @retval String       The "value" string if the "key" is valid.
       @retval null         if the "key" is invalid
     **/
    public String put(String key, String value) {
        ToolChainKey toolChainKey;

        try {
            toolChainKey = new ToolChainKey(key);
        } catch (Exception e) {
            return null;
        }
        return (String)map.put(toolChainKey, value);
    }

    /**
       Wrapper function for Map.put(). The key is given in the form of string
       array.

       @param key           Key string array
       @param value         Value string associated with the "key"
       
       @retval String       The "value" string if the "key" is valid.
       @retval null         if the "key" is invalid
     **/
    public String put(String[] key, String value) {
        ToolChainKey toolChainKey;

        try {
            toolChainKey = new ToolChainKey(key);
        } catch (Exception e) {
            return null;
        }
        return (String)map.put(toolChainKey, value);
    }

    /**
       Wrapper function for Map.put(). The key is given in ToolChainKey class.

       @param key           ToolChainKey class
       @param value         Value string associated with the "key"
       
       @retval String       The "value" string if the "key" is valid.
       @retval null         if the "key" is invalid
     **/
    public String put(ToolChainKey key, String value) {
        return (String)map.put(key, value);
    }

    /**
       Wrapper function for Map.get().

       @param key           Key string which is concatenated with default "delimiter"
       
       @return String
     **/
    public String get(String key) {
        ToolChainKey toolChainKey;

        try {
            toolChainKey = new ToolChainKey(key);
        } catch (Exception e) {
            return null;
        }
        return get(toolChainKey);
    }

    /**
       Wrapper function for Map.get(). It's used when default delimiter of
       ToolChainKey is not wanted and will be overrided by "delimiter" parameter.

       @param key           Key string which is concatenated with "delimiter"
       @param delimiter     The delimiter string in the key string
       
       @return String
     **/
    public String get(String key, String delimiter) {
        ToolChainKey toolChainKey;

        try {
            toolChainKey = new ToolChainKey(key, delimiter);
        } catch (Exception e) {
            return null;
        }
        return get(toolChainKey);
    }

    /**
       Wrapper function for Map.get(). The key is given in the form of string
       array.

       @param key           Key string array
       
       @return String
     **/
    public String get(String[] key) {
        ToolChainKey toolChainKey;

        try {
            toolChainKey = new ToolChainKey(key);
        } catch (Exception e) {
            return null;
        }
        return get(toolChainKey);
    }

    /**
       Wrapper function for Map.get(). The key is given in ToolChainKey class.
       All other form of get() method will eventually call this form of get. It
       will do real job of finding the value associated with the given key. Most
       of the job is to try to match the key with "wildcard".

       @param key           ToolChainKey class
       
       @return String       The value associated with the key
     **/
    public String get(ToolChainKey key) {
        ///
        /// First, we'll try to get the value through the exact given key
        /// 
        String result = map.get(key);
        if (result != null || map.containsKey(key)) {
            return result;
        }

        ///
        /// If nothing is found, then, we'll try all possible keys combined with
        /// wildcard "*". In order not to change the original key value, we have
        /// to clone one for later use. 
        /// 
        String[] keySet = key.getKeySet();
        ToolChainKey tmpKey;
        try {
            tmpKey = new ToolChainKey(keySet);
        } catch (Exception e) {
            return null;
        }

        ///
        /// In the current tool chain definition format (in name/value pair), 
        /// there're five parts in the "name". The last part of the "name" must
        /// not be "wildcard". We should start combining "*" from left to right.
        /// We'll try all the possible combinations until the value can be fetched.
        ///  
        /// The following code implements the logic which will try to use, for example,
        /// following key parts combinations sequentially to get the value.
        /// 
        /// TARGET_TOOLCHAIN_ARCH_TOOLCODE_ATTRIBUTE
        /// ******_TOOLCHAIN_ARCH_TOOLCODE_ATTRIBUTE
        /// TARGET_*********_ARCH_TOOLCODE_ATTRIBUTE
        /// ******_*********_ARCH_TOOLCODE_ATTRIBUTE
        /// TARGET_TOOLCHAIN_****_TOOLCODE_ATTRIBUTE
        /// ******_TOOLCHAIN_****_TOOLCODE_ATTRIBUTE
        /// TARGET_*********_****_TOOLCODE_ATTRIBUTE
        /// ******_*********_****_TOOLCODE_ATTRIBUTE
        /// TARGET_TOOLCHAIN_ARCH_********_ATTRIBUTE
        /// ******_TOOLCHAIN_ARCH_********_ATTRIBUTE
        /// TARGET_*********_ARCH_********_ATTRIBUTE
        /// ******_*********_ARCH_********_ATTRIBUTE
        /// TARGET_TOOLCHAIN_****_********_ATTRIBUTE
        /// ******_TOOLCHAIN_****_********_ATTRIBUTE
        /// TARGET_*********_****_********_ATTRIBUTE
        /// ******_*********_****_********_ATTRIBUTE
        /// 

        //
        // The wildcard "*" appears regularly (2^n). "*" in TARGET appears 2^0 
        // times at every 2^0 TARGET, "*" in TOOLCHAIN appears 2^1 times at 
        // every 2^1 TOOLCHAIN,  and "*" in TOOLCODE appears 2^3 times at every 
        // 2^3 TOOLCODE. We're going to use this to form all the combinations of key.
        // 
        int[] combinations = new int[matchLevel];
        for (int i = 0; i < matchLevel; ++i) {
            //
            // initialize the array with 2^n
            // 
            combinations[i] = 1 << (i + 1);
        }

        //
        // when last part goes down to zero, we tried all combinations of key
        // 
        int lastIndex = matchLevel - 1;
        while (combinations[lastIndex] > 0) {
            //
            // form the key which has "*" in it
            // 
            for (int i = 0; i < matchLevel; ++i) {
                //
                // start again if not finished
                // 
                if (combinations[i] == 0) {
                    combinations[i] = 1 << (i + 1);
                }

                //
                // half of 2^n is "*", the rest is non-*
                // 
                try {
                    if (combinations[i] > (1 << i)) {
                        tmpKey.setKey(keySet[i], i);
                    } else {
                        tmpKey.setKey("*", i);
                    }
                } catch (Exception e) {
                    return null;
                }

                combinations[i] -= 1;
            }

            //
            // Try get the value from the map
            // 
            result = map.get(tmpKey);
            if (result != null) {
                //
                // The map actually has no exact key as the given "key", 
                // putting it back into map can speed up the get() next time
                // 
                map.put(key, result);
                return result;
            }
        }

        //
        // The map actually has no exact key as the given "key", putting it back
        // into map can speed up the get() next time even we got nothing.
        // 
        map.put(key, null);
        return null;
    }

    /**
       Wrapper function for Map.size().

       @return int  The size of map
     **/
    public int size() {
        return map.size();
    }

    /**
       Wrapper function for Map.keySet().

       @return Set<ToolChainKey>    A set of ToolChainKey objects
     */
    public Set<ToolChainKey> keySet() {
        return (Set<ToolChainKey>)map.keySet();
    }
    
    public String toString() {
        return map + "";
    }
}