| 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 SCons |
| 39 |
| 40 |
| 41 _bit_descriptions = {} |
| 42 _bits_with_options = set() |
| 43 _bit_exclusive_groups = {} |
| 44 |
| 45 #------------------------------------------------------------------------------ |
| 46 |
| 47 |
| 48 def DeclareBit(bit_name, desc, exclusive_groups=tuple()): |
| 49 """Declares and describes the bit. |
| 50 |
| 51 Args: |
| 52 bit_name: Name of the bit being described. |
| 53 desc: Description of bit. |
| 54 exclusive_groups: Bit groups which this bit belongs to. At most one bit |
| 55 may be set in each exclusive group. |
| 56 |
| 57 Raises: |
| 58 ValueError: The bit has already been defined with a different description, |
| 59 or the description is empty. |
| 60 |
| 61 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(). |
| 63 """ |
| 64 |
| 65 if not desc: |
| 66 raise ValueError('Must supply a description for bit "%s"' % bit_name) |
| 67 |
| 68 existing_desc = _bit_descriptions.get(bit_name) |
| 69 if existing_desc and desc != existing_desc: |
| 70 raise ValueError('Cannot describe bit "%s" as "%s" because it has already' |
| 71 'been described as "%s".' % |
| 72 (bit_name, desc, existing_desc)) |
| 73 |
| 74 _bit_descriptions[bit_name] = desc |
| 75 |
| 76 # Add bit to its exclusive groups |
| 77 for g in exclusive_groups: |
| 78 if g not in _bit_exclusive_groups: |
| 79 _bit_exclusive_groups[g] = set() |
| 80 _bit_exclusive_groups[g].add(bit_name) |
| 81 |
| 82 #------------------------------------------------------------------------------ |
| 83 |
| 84 |
| 85 def Bit(env, bit_name): |
| 86 """Checks if the environment has the bit. |
| 87 |
| 88 Args: |
| 89 env: Environment to check. |
| 90 bit_name: Name of the bit to check. |
| 91 |
| 92 Returns: |
| 93 True if the bit is present in the environment. |
| 94 """ |
| 95 # TODO(rspangler): Add bit sanity checking (description exists, exclusive |
| 96 # groups not violated). |
| 97 return bit_name in env['_BITS'] |
| 98 |
| 99 #------------------------------------------------------------------------------ |
| 100 |
| 101 |
| 102 def AllBits(env, *args): |
| 103 """Checks if the environment has all the bits. |
| 104 |
| 105 Args: |
| 106 env: Environment to check. |
| 107 args: List of bit names to check. |
| 108 |
| 109 Returns: |
| 110 True if every bit listed is present in the environment. |
| 111 """ |
| 112 # TODO(rspangler): Add bit sanity checking |
| 113 return set(args).issubset(env['_BITS']) |
| 114 |
| 115 #------------------------------------------------------------------------------ |
| 116 |
| 117 |
| 118 def AnyBits(env, *args): |
| 119 """Checks if the environment has at least one of the bits. |
| 120 |
| 121 Args: |
| 122 env: Environment to check. |
| 123 args: List of bit names to check. |
| 124 |
| 125 Returns: |
| 126 True if at least one bit listed is present in the environment. |
| 127 """ |
| 128 # TODO(rspangler): Add bit sanity checking |
| 129 return set(args).intersection(env['_BITS']) |
| 130 |
| 131 #------------------------------------------------------------------------------ |
| 132 |
| 133 |
| 134 def SetBits(env, *args): |
| 135 """Sets the bits in the environment. |
| 136 |
| 137 Args: |
| 138 env: Environment to check. |
| 139 args: List of bit names to set. |
| 140 """ |
| 141 # TODO(rspangler): Add bit sanity checking |
| 142 env['_BITS'] = env['_BITS'].union(args) |
| 143 |
| 144 #------------------------------------------------------------------------------ |
| 145 |
| 146 |
| 147 def ClearBits(env, *args): |
| 148 """Sets the bits in the environment. |
| 149 |
| 150 Args: |
| 151 env: Environment to check. |
| 152 args: List of bit names to set. |
| 153 """ |
| 154 # TODO(rspangler): Add bit sanity checking |
| 155 env['_BITS'] = env['_BITS'].difference(args) |
| 156 |
| 157 #------------------------------------------------------------------------------ |
| 158 |
| 159 |
| 160 def SetBitFromOption(env, bit_name, default): |
| 161 """Sets the bit in the environment from a command line option. |
| 162 |
| 163 Args: |
| 164 env: Environment to check. |
| 165 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. |
| 167 """ |
| 168 # TODO(rspangler): Add bit sanity checking |
| 169 |
| 170 # Add the command line option, if not already present |
| 171 if bit_name not in _bits_with_options: |
| 172 _bits_with_options.add(bit_name) |
| 173 SCons.Script.AddOption('--' + bit_name, |
| 174 dest=bit_name, |
| 175 action='store_true', |
| 176 help='set bit:' + _bit_descriptions[bit_name]) |
| 177 SCons.Script.AddOption('--no-' + bit_name, |
| 178 dest=bit_name, |
| 179 action='store_false', |
| 180 help='clear bit:' + _bit_descriptions[bit_name]) |
| 181 |
| 182 bit_set = env.GetOption(bit_name) |
| 183 if bit_set is None: |
| 184 # Not specified on command line, so use default |
| 185 bit_set = default |
| 186 |
| 187 if bit_set: |
| 188 env['_BITS'].add(bit_name) |
| 189 elif bit_name in env['_BITS']: |
| 190 env['_BITS'].remove(bit_name) |
| 191 |
| 192 #------------------------------------------------------------------------------ |
| 193 |
| 194 |
| 195 def generate(env): |
| 196 # NOTE: SCons requires the use of this name, which fails gpylint. |
| 197 """SCons entry point for this tool.""" |
| 198 |
| 199 # Add methods to builtin |
| 200 # TODO(rspangler): These really belong in site_init.py - but if we do that, |
| 201 # what's the right way to access the bit global variables? |
| 202 __builtin__.DeclareBit = DeclareBit |
| 203 |
| 204 # Add methods to environment |
| 205 env.AddMethod(AllBits) |
| 206 env.AddMethod(AnyBits) |
| 207 env.AddMethod(Bit) |
| 208 env.AddMethod(ClearBits) |
| 209 env.AddMethod(SetBitFromOption) |
| 210 env.AddMethod(SetBits) |
| 211 |
| 212 env['_BITS'] = set() |
| OLD | NEW |