| OLD | NEW |
| 1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
| 2 # Copyright 2008, Google Inc. | 2 # Copyright 2008, Google Inc. |
| 3 # All rights reserved. | 3 # All rights reserved. |
| 4 # | 4 # |
| 5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
| 7 # met: | 7 # met: |
| 8 # | 8 # |
| 9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 """Environment bit support for software construction toolkit. | 31 """Environment bit support for software construction toolkit. |
| 32 | 32 |
| 33 This module is automatically included by the component_setup tool. | 33 This module is automatically included by the component_setup tool. |
| 34 """ | 34 """ |
| 35 | 35 |
| 36 | 36 |
| 37 import __builtin__ | 37 import __builtin__ |
| 38 import types |
| 38 import SCons | 39 import SCons |
| 39 | 40 |
| 40 | 41 |
| 41 _bit_descriptions = {} | 42 _bit_descriptions = {} |
| 42 _bits_with_options = set() | 43 _bits_with_options = set() |
| 43 _bit_exclusive_groups = {} | 44 _bit_exclusive_groups = {} |
| 44 | 45 |
| 45 #------------------------------------------------------------------------------ | 46 #------------------------------------------------------------------------------ |
| 46 | 47 |
| 47 | 48 |
| 48 def DeclareBit(bit_name, desc, exclusive_groups=tuple()): | 49 def _CheckDeclared(bits): |
| 50 """Checks each of the bits to make sure it's been declared. |
| 51 |
| 52 Args: |
| 53 bits: List of bits to check. |
| 54 |
| 55 Raises: |
| 56 ValueError: A bit has not been declared. |
| 57 """ |
| 58 for bit in bits: |
| 59 if bit not in _bit_descriptions: |
| 60 raise ValueError('Bit "%s" used before DeclareBit()' % |
| 61 bit) |
| 62 |
| 63 |
| 64 def _CheckExclusive(already_set, proposed): |
| 65 """Checks if setting proposed bits would violate any exclusive groups. |
| 66 |
| 67 Args: |
| 68 already_set: List of bits already set. |
| 69 proposed: List of bits attempting to be set. |
| 70 |
| 71 Raises: |
| 72 ValueError: A proposed bit belongs to an exclusive group which already has |
| 73 a bit set. |
| 74 """ |
| 75 # Remove any already-set bits from proposed (note this makes a copy of |
| 76 # proposed so we don't alter the passed list). |
| 77 proposed = [bit for bit in proposed if bit not in already_set] |
| 78 |
| 79 for group_name, group_bits in _bit_exclusive_groups.items(): |
| 80 set_match = group_bits.intersection(already_set) |
| 81 proposed_match = group_bits.intersection(proposed) |
| 82 if set_match and proposed_match: |
| 83 raise ValueError('Unable to set bit "%s" because it belongs to the same ' |
| 84 'exclusive group "%s" as already-set bit "%s"' % ( |
| 85 proposed_match.pop(), group_name, set_match.pop())) |
| 86 |
| 87 |
| 88 #------------------------------------------------------------------------------ |
| 89 |
| 90 |
| 91 def DeclareBit(bit_name, desc, exclusive_groups=None): |
| 49 """Declares and describes the bit. | 92 """Declares and describes the bit. |
| 50 | 93 |
| 51 Args: | 94 Args: |
| 52 bit_name: Name of the bit being described. | 95 bit_name: Name of the bit being described. |
| 53 desc: Description of bit. | 96 desc: Description of bit. |
| 54 exclusive_groups: Bit groups which this bit belongs to. At most one bit | 97 exclusive_groups: Bit groups which this bit belongs to. At most one bit |
| 55 may be set in each exclusive group. | 98 may be set in each exclusive group. May be a string, list of string, |
| 99 or None. |
| 56 | 100 |
| 57 Raises: | 101 Raises: |
| 58 ValueError: The bit has already been defined with a different description, | 102 ValueError: The bit has already been defined with a different description, |
| 59 or the description is empty. | 103 or the description is empty. |
| 60 | 104 |
| 61 Adds a description for the bit in the global dictionary of bit names. All | 105 Adds a description for the bit in the global dictionary of bit names. All |
| 62 bits should be described before being used in Bit()/AllBits()/AnyBits(). | 106 bits should be described before being used in Bit()/AllBits()/AnyBits(). |
| 63 """ | 107 """ |
| 64 | 108 |
| 65 if not desc: | 109 if not desc: |
| 66 raise ValueError('Must supply a description for bit "%s"' % bit_name) | 110 raise ValueError('Must supply a description for bit "%s"' % bit_name) |
| 67 | 111 |
| 68 existing_desc = _bit_descriptions.get(bit_name) | 112 existing_desc = _bit_descriptions.get(bit_name) |
| 69 if existing_desc and desc != existing_desc: | 113 if existing_desc and desc != existing_desc: |
| 70 raise ValueError('Cannot describe bit "%s" as "%s" because it has already' | 114 raise ValueError('Cannot describe bit "%s" as "%s" because it has already' |
| 71 'been described as "%s".' % | 115 'been described as "%s".' % |
| 72 (bit_name, desc, existing_desc)) | 116 (bit_name, desc, existing_desc)) |
| 73 | 117 |
| 74 _bit_descriptions[bit_name] = desc | 118 _bit_descriptions[bit_name] = desc |
| 75 | 119 |
| 76 # Add bit to its exclusive groups | 120 # Add bit to its exclusive groups |
| 77 for g in exclusive_groups: | 121 if exclusive_groups: |
| 78 if g not in _bit_exclusive_groups: | 122 if type(exclusive_groups) == types.StringType: |
| 79 _bit_exclusive_groups[g] = set() | 123 exclusive_groups = [exclusive_groups] |
| 80 _bit_exclusive_groups[g].add(bit_name) | 124 for g in exclusive_groups: |
| 125 if g not in _bit_exclusive_groups: |
| 126 _bit_exclusive_groups[g] = set() |
| 127 _bit_exclusive_groups[g].add(bit_name) |
| 81 | 128 |
| 82 #------------------------------------------------------------------------------ | 129 #------------------------------------------------------------------------------ |
| 83 | 130 |
| 84 | |
| 85 def Bit(env, bit_name): | 131 def Bit(env, bit_name): |
| 86 """Checks if the environment has the bit. | 132 """Checks if the environment has the bit. |
| 87 | 133 |
| 88 Args: | 134 Args: |
| 89 env: Environment to check. | 135 env: Environment to check. |
| 90 bit_name: Name of the bit to check. | 136 bit_name: Name of the bit to check. |
| 91 | 137 |
| 92 Returns: | 138 Returns: |
| 93 True if the bit is present in the environment. | 139 True if the bit is present in the environment. |
| 94 """ | 140 """ |
| 95 # TODO(rspangler): Add bit sanity checking (description exists, exclusive | 141 _CheckDeclared([bit_name]) |
| 96 # groups not violated). | |
| 97 return bit_name in env['_BITS'] | 142 return bit_name in env['_BITS'] |
| 98 | 143 |
| 99 #------------------------------------------------------------------------------ | 144 #------------------------------------------------------------------------------ |
| 100 | 145 |
| 101 | 146 |
| 102 def AllBits(env, *args): | 147 def AllBits(env, *args): |
| 103 """Checks if the environment has all the bits. | 148 """Checks if the environment has all the bits. |
| 104 | 149 |
| 105 Args: | 150 Args: |
| 106 env: Environment to check. | 151 env: Environment to check. |
| 107 args: List of bit names to check. | 152 args: List of bit names to check. |
| 108 | 153 |
| 109 Returns: | 154 Returns: |
| 110 True if every bit listed is present in the environment. | 155 True if every bit listed is present in the environment. |
| 111 """ | 156 """ |
| 112 # TODO(rspangler): Add bit sanity checking | 157 _CheckDeclared(args) |
| 113 return set(args).issubset(env['_BITS']) | 158 return set(args).issubset(env['_BITS']) |
| 114 | 159 |
| 115 #------------------------------------------------------------------------------ | 160 #------------------------------------------------------------------------------ |
| 116 | 161 |
| 117 | 162 |
| 118 def AnyBits(env, *args): | 163 def AnyBits(env, *args): |
| 119 """Checks if the environment has at least one of the bits. | 164 """Checks if the environment has at least one of the bits. |
| 120 | 165 |
| 121 Args: | 166 Args: |
| 122 env: Environment to check. | 167 env: Environment to check. |
| 123 args: List of bit names to check. | 168 args: List of bit names to check. |
| 124 | 169 |
| 125 Returns: | 170 Returns: |
| 126 True if at least one bit listed is present in the environment. | 171 True if at least one bit listed is present in the environment. |
| 127 """ | 172 """ |
| 128 # TODO(rspangler): Add bit sanity checking | 173 _CheckDeclared(args) |
| 129 return set(args).intersection(env['_BITS']) | 174 return set(args).intersection(env['_BITS']) |
| 130 | 175 |
| 131 #------------------------------------------------------------------------------ | 176 #------------------------------------------------------------------------------ |
| 132 | 177 |
| 133 | 178 |
| 134 def SetBits(env, *args): | 179 def SetBits(env, *args): |
| 135 """Sets the bits in the environment. | 180 """Sets the bits in the environment. |
| 136 | 181 |
| 137 Args: | 182 Args: |
| 138 env: Environment to check. | 183 env: Environment to check. |
| 139 args: List of bit names to set. | 184 args: List of bit names to set. |
| 140 """ | 185 """ |
| 141 # TODO(rspangler): Add bit sanity checking | 186 _CheckDeclared(args) |
| 187 _CheckExclusive(env['_BITS'], args) |
| 142 env['_BITS'] = env['_BITS'].union(args) | 188 env['_BITS'] = env['_BITS'].union(args) |
| 143 | 189 |
| 144 #------------------------------------------------------------------------------ | 190 #------------------------------------------------------------------------------ |
| 145 | 191 |
| 146 | 192 |
| 147 def ClearBits(env, *args): | 193 def ClearBits(env, *args): |
| 148 """Sets the bits in the environment. | 194 """Sets the bits in the environment. |
| 149 | 195 |
| 150 Args: | 196 Args: |
| 151 env: Environment to check. | 197 env: Environment to check. |
| 152 args: List of bit names to set. | 198 args: List of bit names to set. |
| 153 """ | 199 """ |
| 154 # TODO(rspangler): Add bit sanity checking | 200 _CheckDeclared(args) |
| 155 env['_BITS'] = env['_BITS'].difference(args) | 201 env['_BITS'] = env['_BITS'].difference(args) |
| 156 | 202 |
| 157 #------------------------------------------------------------------------------ | 203 #------------------------------------------------------------------------------ |
| 158 | 204 |
| 159 | 205 |
| 160 def SetBitFromOption(env, bit_name, default): | 206 def SetBitFromOption(env, bit_name, default): |
| 161 """Sets the bit in the environment from a command line option. | 207 """Sets the bit in the environment from a command line option. |
| 162 | 208 |
| 163 Args: | 209 Args: |
| 164 env: Environment to check. | 210 env: Environment to check. |
| 165 bit_name: Name of the bit to set from a command line option. | 211 bit_name: Name of the bit to set from a command line option. |
| 166 default: Default value for bit if command line option is not present. | 212 default: Default value for bit if command line option is not present. |
| 167 """ | 213 """ |
| 168 # TODO(rspangler): Add bit sanity checking | 214 _CheckDeclared([bit_name]) |
| 169 | 215 |
| 170 # Add the command line option, if not already present | 216 # Add the command line option, if not already present |
| 171 if bit_name not in _bits_with_options: | 217 if bit_name not in _bits_with_options: |
| 172 _bits_with_options.add(bit_name) | 218 _bits_with_options.add(bit_name) |
| 173 SCons.Script.AddOption('--' + bit_name, | 219 SCons.Script.AddOption('--' + bit_name, |
| 174 dest=bit_name, | 220 dest=bit_name, |
| 175 action='store_true', | 221 action='store_true', |
| 176 help='set bit:' + _bit_descriptions[bit_name]) | 222 help='set bit:' + _bit_descriptions[bit_name]) |
| 177 SCons.Script.AddOption('--no-' + bit_name, | 223 SCons.Script.AddOption('--no-' + bit_name, |
| 178 dest=bit_name, | 224 dest=bit_name, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 203 | 249 |
| 204 # Add methods to environment | 250 # Add methods to environment |
| 205 env.AddMethod(AllBits) | 251 env.AddMethod(AllBits) |
| 206 env.AddMethod(AnyBits) | 252 env.AddMethod(AnyBits) |
| 207 env.AddMethod(Bit) | 253 env.AddMethod(Bit) |
| 208 env.AddMethod(ClearBits) | 254 env.AddMethod(ClearBits) |
| 209 env.AddMethod(SetBitFromOption) | 255 env.AddMethod(SetBitFromOption) |
| 210 env.AddMethod(SetBits) | 256 env.AddMethod(SetBits) |
| 211 | 257 |
| 212 env['_BITS'] = set() | 258 env['_BITS'] = set() |
| OLD | NEW |