1## @file
2# Generate AutoGen.h, AutoGen.c and *.depex files
3#
4# Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>
5# Copyright (c) 2018, Hewlett Packard Enterprise Development, L.P.<BR>
6# Copyright (c) 2019, American Megatrends, Inc. All rights reserved.<BR>
7#
8# SPDX-License-Identifier: BSD-2-Clause-Patent
9#
10
11## Import Modules
12#
13from __future__ import print_function
14from __future__ import absolute_import
15from Common.DataType import TAB_STAR
16## Base class for AutoGen
17#
18#   This class just implements the cache mechanism of AutoGen objects.
19#
20class AutoGen(object):
21    # database to maintain the objects in each child class
22    __ObjectCache = {}    # (BuildTarget, ToolChain, ARCH, platform file): AutoGen object
23
24    ## Factory method
25    #
26    #   @param  Class           class object of real AutoGen class
27    #                           (WorkspaceAutoGen, ModuleAutoGen or PlatformAutoGen)
28    #   @param  Workspace       Workspace directory or WorkspaceAutoGen object
29    #   @param  MetaFile        The path of meta file
30    #   @param  Target          Build target
31    #   @param  Toolchain       Tool chain name
32    #   @param  Arch            Target arch
33    #   @param  *args           The specific class related parameters
34    #   @param  **kwargs        The specific class related dict parameters
35    #
36
37    def __new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):
38        # check if the object has been created
39        Key = (Target, Toolchain, Arch, MetaFile)
40        if Key in cls.__ObjectCache:
41            # if it exists, just return it directly
42            return cls.__ObjectCache[Key]
43            # it didnt exist. create it, cache it, then return it
44        RetVal = cls.__ObjectCache[Key] = super(AutoGen, cls).__new__(cls)
45        return RetVal
46
47
48    ## hash() operator
49    #
50    #  The file path of platform file will be used to represent hash value of this object
51    #
52    #   @retval int     Hash value of the file path of platform file
53    #
54    def __hash__(self):
55        return hash(self.MetaFile)
56
57    ## str() operator
58    #
59    #  The file path of platform file will be used to represent this object
60    #
61    #   @retval string  String of platform file path
62    #
63    def __str__(self):
64        return str(self.MetaFile)
65
66    ## "==" operator
67    def __eq__(self, Other):
68        return Other and self.MetaFile == Other
69
70    @classmethod
71    def Cache(cls):
72        return cls.__ObjectCache
73
74#
75# The priority list while override build option
76#
77PrioList = {"0x11111"  : 16,     #  TARGET_TOOLCHAIN_ARCH_COMMANDTYPE_ATTRIBUTE (Highest)
78            "0x01111"  : 15,     #  ******_TOOLCHAIN_ARCH_COMMANDTYPE_ATTRIBUTE
79            "0x10111"  : 14,     #  TARGET_*********_ARCH_COMMANDTYPE_ATTRIBUTE
80            "0x00111"  : 13,     #  ******_*********_ARCH_COMMANDTYPE_ATTRIBUTE
81            "0x11011"  : 12,     #  TARGET_TOOLCHAIN_****_COMMANDTYPE_ATTRIBUTE
82            "0x01011"  : 11,     #  ******_TOOLCHAIN_****_COMMANDTYPE_ATTRIBUTE
83            "0x10011"  : 10,     #  TARGET_*********_****_COMMANDTYPE_ATTRIBUTE
84            "0x00011"  : 9,      #  ******_*********_****_COMMANDTYPE_ATTRIBUTE
85            "0x11101"  : 8,      #  TARGET_TOOLCHAIN_ARCH_***********_ATTRIBUTE
86            "0x01101"  : 7,      #  ******_TOOLCHAIN_ARCH_***********_ATTRIBUTE
87            "0x10101"  : 6,      #  TARGET_*********_ARCH_***********_ATTRIBUTE
88            "0x00101"  : 5,      #  ******_*********_ARCH_***********_ATTRIBUTE
89            "0x11001"  : 4,      #  TARGET_TOOLCHAIN_****_***********_ATTRIBUTE
90            "0x01001"  : 3,      #  ******_TOOLCHAIN_****_***********_ATTRIBUTE
91            "0x10001"  : 2,      #  TARGET_*********_****_***********_ATTRIBUTE
92            "0x00001"  : 1}      #  ******_*********_****_***********_ATTRIBUTE (Lowest)
93## Calculate the priority value of the build option
94#
95# @param    Key    Build option definition contain: TARGET_TOOLCHAIN_ARCH_COMMANDTYPE_ATTRIBUTE
96#
97# @retval   Value  Priority value based on the priority list.
98#
99def CalculatePriorityValue(Key):
100    Target, ToolChain, Arch, CommandType, Attr = Key.split('_')
101    PriorityValue = 0x11111
102    if Target == TAB_STAR:
103        PriorityValue &= 0x01111
104    if ToolChain == TAB_STAR:
105        PriorityValue &= 0x10111
106    if Arch == TAB_STAR:
107        PriorityValue &= 0x11011
108    if CommandType == TAB_STAR:
109        PriorityValue &= 0x11101
110    if Attr == TAB_STAR:
111        PriorityValue &= 0x11110
112
113    return PrioList["0x%0.5x" % PriorityValue]
114