| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python2.4 | |
| 2 # Copyright 2008, Google Inc. | |
| 3 # All rights reserved. | |
| 4 # | |
| 5 # Redistribution and use in source and binary forms, with or without | |
| 6 # modification, are permitted provided that the following conditions are | |
| 7 # met: | |
| 8 # | |
| 9 # * Redistributions of source code must retain the above copyright | |
| 10 # notice, this list of conditions and the following disclaimer. | |
| 11 # * Redistributions in binary form must reproduce the above | |
| 12 # copyright notice, this list of conditions and the following disclaimer | |
| 13 # in the documentation and/or other materials provided with the | |
| 14 # distribution. | |
| 15 # * Neither the name of Google Inc. nor the names of its | |
| 16 # contributors may be used to endorse or promote products derived from | |
| 17 # this software without specific prior written permission. | |
| 18 # | |
| 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 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. | |
| 30 | |
| 31 """Environment bit support for software construction toolkit. | |
| 32 | |
| 33 This module is automatically included by the component_setup tool. | |
| 34 """ | |
| 35 | |
| 36 | |
| 37 import __builtin__ | |
| 38 import types | |
| 39 import SCons | |
| 40 | |
| 41 | |
| 42 _bit_descriptions = {} | |
| 43 _bits_with_options = set() | |
| 44 _bit_exclusive_groups = {} | |
| 45 | |
| 46 #------------------------------------------------------------------------------ | |
| 47 | |
| 48 | |
| 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): | |
| 92 """Declares and describes the bit. | |
| 93 | |
| 94 Args: | |
| 95 bit_name: Name of the bit being described. | |
| 96 desc: Description of bit. | |
| 97 exclusive_groups: Bit groups which this bit belongs to. At most one bit | |
| 98 may be set in each exclusive group. May be a string, list of string, | |
| 99 or None. | |
| 100 | |
| 101 Raises: | |
| 102 ValueError: The bit has already been defined with a different description, | |
| 103 or the description is empty. | |
| 104 | |
| 105 Adds a description for the bit in the global dictionary of bit names. All | |
| 106 bits should be described before being used in Bit()/AllBits()/AnyBits(). | |
| 107 """ | |
| 108 | |
| 109 if not desc: | |
| 110 raise ValueError('Must supply a description for bit "%s"' % bit_name) | |
| 111 | |
| 112 existing_desc = _bit_descriptions.get(bit_name) | |
| 113 if existing_desc and desc != existing_desc: | |
| 114 raise ValueError('Cannot describe bit "%s" as "%s" because it has already' | |
| 115 'been described as "%s".' % | |
| 116 (bit_name, desc, existing_desc)) | |
| 117 | |
| 118 _bit_descriptions[bit_name] = desc | |
| 119 | |
| 120 # Add bit to its exclusive groups | |
| 121 if exclusive_groups: | |
| 122 if type(exclusive_groups) == types.StringType: | |
| 123 exclusive_groups = [exclusive_groups] | |
| 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) | |
| 128 | |
| 129 #------------------------------------------------------------------------------ | |
| 130 | |
| 131 def Bit(env, bit_name): | |
| 132 """Checks if the environment has the bit. | |
| 133 | |
| 134 Args: | |
| 135 env: Environment to check. | |
| 136 bit_name: Name of the bit to check. | |
| 137 | |
| 138 Returns: | |
| 139 True if the bit is present in the environment. | |
| 140 """ | |
| 141 _CheckDeclared([bit_name]) | |
| 142 return bit_name in env['_BITS'] | |
| 143 | |
| 144 #------------------------------------------------------------------------------ | |
| 145 | |
| 146 | |
| 147 def AllBits(env, *args): | |
| 148 """Checks if the environment has all the bits. | |
| 149 | |
| 150 Args: | |
| 151 env: Environment to check. | |
| 152 args: List of bit names to check. | |
| 153 | |
| 154 Returns: | |
| 155 True if every bit listed is present in the environment. | |
| 156 """ | |
| 157 _CheckDeclared(args) | |
| 158 return set(args).issubset(env['_BITS']) | |
| 159 | |
| 160 #------------------------------------------------------------------------------ | |
| 161 | |
| 162 | |
| 163 def AnyBits(env, *args): | |
| 164 """Checks if the environment has at least one of the bits. | |
| 165 | |
| 166 Args: | |
| 167 env: Environment to check. | |
| 168 args: List of bit names to check. | |
| 169 | |
| 170 Returns: | |
| 171 True if at least one bit listed is present in the environment. | |
| 172 """ | |
| 173 _CheckDeclared(args) | |
| 174 return set(args).intersection(env['_BITS']) | |
| 175 | |
| 176 #------------------------------------------------------------------------------ | |
| 177 | |
| 178 | |
| 179 def SetBits(env, *args): | |
| 180 """Sets the bits in the environment. | |
| 181 | |
| 182 Args: | |
| 183 env: Environment to check. | |
| 184 args: List of bit names to set. | |
| 185 """ | |
| 186 _CheckDeclared(args) | |
| 187 _CheckExclusive(env['_BITS'], args) | |
| 188 env['_BITS'] = env['_BITS'].union(args) | |
| 189 | |
| 190 #------------------------------------------------------------------------------ | |
| 191 | |
| 192 | |
| 193 def ClearBits(env, *args): | |
| 194 """Clears the bits in the environment. | |
| 195 | |
| 196 Args: | |
| 197 env: Environment to check. | |
| 198 args: List of bit names to clear (remove). | |
| 199 """ | |
| 200 _CheckDeclared(args) | |
| 201 env['_BITS'] = env['_BITS'].difference(args) | |
| 202 | |
| 203 #------------------------------------------------------------------------------ | |
| 204 | |
| 205 | |
| 206 def SetBitFromOption(env, bit_name, default): | |
| 207 """Sets the bit in the environment from a command line option. | |
| 208 | |
| 209 Args: | |
| 210 env: Environment to check. | |
| 211 bit_name: Name of the bit to set from a command line option. | |
| 212 default: Default value for bit if command line option is not present. | |
| 213 """ | |
| 214 _CheckDeclared([bit_name]) | |
| 215 | |
| 216 # Add the command line option, if not already present | |
| 217 if bit_name not in _bits_with_options: | |
| 218 _bits_with_options.add(bit_name) | |
| 219 SCons.Script.AddOption('--' + bit_name, | |
| 220 dest=bit_name, | |
| 221 action='store_true', | |
| 222 help='set bit:' + _bit_descriptions[bit_name]) | |
| 223 SCons.Script.AddOption('--no-' + bit_name, | |
| 224 dest=bit_name, | |
| 225 action='store_false', | |
| 226 help='clear bit:' + _bit_descriptions[bit_name]) | |
| 227 | |
| 228 bit_set = env.GetOption(bit_name) | |
| 229 if bit_set is None: | |
| 230 # Not specified on command line, so use default | |
| 231 bit_set = default | |
| 232 | |
| 233 if bit_set: | |
| 234 env['_BITS'].add(bit_name) | |
| 235 elif bit_name in env['_BITS']: | |
| 236 env['_BITS'].remove(bit_name) | |
| 237 | |
| 238 #------------------------------------------------------------------------------ | |
| 239 | |
| 240 | |
| 241 def generate(env): | |
| 242 # NOTE: SCons requires the use of this name, which fails gpylint. | |
| 243 """SCons entry point for this tool.""" | |
| 244 | |
| 245 # Add methods to builtin | |
| 246 # TODO(rspangler): These really belong in site_init.py - but if we do that, | |
| 247 # what's the right way to access the bit global variables? | |
| 248 __builtin__.DeclareBit = DeclareBit | |
| 249 | |
| 250 # Add methods to environment | |
| 251 env.AddMethod(AllBits) | |
| 252 env.AddMethod(AnyBits) | |
| 253 env.AddMethod(Bit) | |
| 254 env.AddMethod(ClearBits) | |
| 255 env.AddMethod(SetBitFromOption) | |
| 256 env.AddMethod(SetBits) | |
| 257 | |
| 258 env['_BITS'] = set() | |
| OLD | NEW |