| 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 """Software construction toolkit target management for SCons.""" |
| 32 |
| 33 |
| 34 import __builtin__ |
| 35 import SCons.Script |
| 36 |
| 37 |
| 38 # Dict of target groups (TargetGroup indexed by group name) |
| 39 __target_groups = {} |
| 40 |
| 41 # Dict of targets (Target indexed by target name) |
| 42 __targets = {} |
| 43 |
| 44 # Dict of target modes (TargetMode indexed by mode name) |
| 45 __target_modes = {} |
| 46 |
| 47 #------------------------------------------------------------------------------ |
| 48 |
| 49 |
| 50 class TargetGroup(object): |
| 51 """Target group, as used by AddTargetGroup() and GetTargetGroups().""" |
| 52 |
| 53 def __init__(self, name, description): |
| 54 """Initializes the target group. |
| 55 |
| 56 Args: |
| 57 name: Name of the target group. |
| 58 description: Description of group. |
| 59 """ |
| 60 self.name = name |
| 61 self.description = description |
| 62 |
| 63 def GetTargetNames(self): |
| 64 """Returns a list of target name strings for the group.""" |
| 65 items = map(str, SCons.Script.Alias(self.name)[0].sources) |
| 66 # Remove duplicates from multiple environments |
| 67 return list(set(items)) |
| 68 |
| 69 #------------------------------------------------------------------------------ |
| 70 |
| 71 |
| 72 class TargetMode(object): |
| 73 """Target mode, as used by GetTargetModes().""" |
| 74 |
| 75 def __init__(self, name, description): |
| 76 """Initializes the target mode. |
| 77 |
| 78 Args: |
| 79 name: Name of the target mode. |
| 80 description: Description of mode. |
| 81 """ |
| 82 self.name = name |
| 83 self.description = description |
| 84 |
| 85 def GetTargetNames(self): |
| 86 """Returns a list of target name strings for the group.""" |
| 87 items = map(str, SCons.Script.Alias(self.name)[0].sources) |
| 88 # Remove duplicates from multiple environments |
| 89 return list(set(items)) |
| 90 |
| 91 #------------------------------------------------------------------------------ |
| 92 |
| 93 |
| 94 class Target(object): |
| 95 """Target object.""" |
| 96 |
| 97 def __init__(self, name): |
| 98 """Initializes the target. |
| 99 |
| 100 Args: |
| 101 name: Name of the target. |
| 102 """ |
| 103 self.name = name |
| 104 self.properties = {} # Global properties |
| 105 self.mode_properties = {} # Dict of modes to mode-specific properties |
| 106 |
| 107 #------------------------------------------------------------------------------ |
| 108 |
| 109 |
| 110 def AddTargetGroup(name, description): |
| 111 """Adds a target group, used for printing help. |
| 112 |
| 113 Args: |
| 114 name: Name of target group. This should be the name of an alias which |
| 115 points to other aliases for the specific targets. |
| 116 description: Description of the target group. |
| 117 """ |
| 118 |
| 119 # Warn if the target group already exists with a different description |
| 120 if (name in __target_groups |
| 121 and __target_groups[name].description != description): |
| 122 print ('Warning: Changing description of target group "%s" from "%s" to ' |
| 123 '"%s"' % (name, __target_groups[name].description, description)) |
| 124 __target_groups[name].description = description |
| 125 else: |
| 126 __target_groups[name] = TargetGroup(name, description) |
| 127 |
| 128 |
| 129 def GetTargetGroups(): |
| 130 """Gets the dict of target groups. |
| 131 |
| 132 Returns: |
| 133 The dict of target groups, indexed by group name. |
| 134 |
| 135 This dict is not fully populated until after BuildEnvironments() has been |
| 136 called. |
| 137 """ |
| 138 return __target_groups |
| 139 |
| 140 |
| 141 def GetTargetModes(): |
| 142 """Gets the dict of target modes. |
| 143 |
| 144 Returns: |
| 145 The dict of target modes, indexed by mode name. |
| 146 |
| 147 This dict is not fully populated until after BuildEnvironments() has been |
| 148 called. |
| 149 """ |
| 150 return __target_modes |
| 151 |
| 152 |
| 153 def GetTargets(): |
| 154 """Gets the dict of targets. |
| 155 |
| 156 Returns: |
| 157 The dict of targets, indexed by target name. |
| 158 |
| 159 This dict is not fully populated until after BuildEnvironments() has been |
| 160 called. |
| 161 """ |
| 162 return __targets |
| 163 |
| 164 |
| 165 def SetTargetProperty(self, target_name, all_modes=False, **kwargs): |
| 166 """Sets one or more properties for a target. |
| 167 |
| 168 Args: |
| 169 self: Environment context. |
| 170 target_name: Name of the target. |
| 171 all_modes: If True, property applies to all modes. If false, it applies |
| 172 only to the current mode (determined by self['BUILD_TYPE']). |
| 173 kwargs: Keyword args are used to set properties. Properties will be |
| 174 converted to strings via env.subst(). |
| 175 |
| 176 For example: |
| 177 foo_test = env.Program(...)[0] |
| 178 env.SetTargetProperty('foo_test', global=True, DESCRIPTION='Foo test') |
| 179 env.SetTargetProperty('foo_test', EXE=foo_test) |
| 180 """ |
| 181 # Get the target |
| 182 if target_name not in __targets: |
| 183 __targets[target_name] = Target(target_name) |
| 184 target = __targets[target_name] |
| 185 |
| 186 if all_modes: |
| 187 add_to_dict = target.properties |
| 188 else: |
| 189 mode = self.get('BUILD_TYPE') |
| 190 if mode not in target.mode_properties: |
| 191 target.mode_properties[mode] = {} |
| 192 add_to_dict = target.mode_properties[mode] |
| 193 |
| 194 # Add values |
| 195 for k, v in kwargs.items(): |
| 196 add_to_dict[k] = self.subst(str(v)) |
| 197 |
| 198 |
| 199 def AddTargetHelp(): |
| 200 """Adds help for the targets, groups, and modes.""" |
| 201 help_text = '' |
| 202 |
| 203 for group in GetTargetGroups().values(): |
| 204 items = group.GetTargetNames() |
| 205 items.sort() |
| 206 if items: |
| 207 help_text += '\nThe following %s:' % group.description |
| 208 colwidth = max(map(len, items)) + 2 |
| 209 cols = 77 / colwidth |
| 210 if cols < 1: |
| 211 cols = 1 # If target names are really long, one per line |
| 212 rows = (len(items) + cols - 1) / cols |
| 213 for row in range(0, rows): |
| 214 help_text += '\n ' |
| 215 for i in range(row, len(items), rows): |
| 216 help_text += '%-*s' % (colwidth, items[i]) |
| 217 help_text += '\n %s (do all of the above)\n' % group.name |
| 218 |
| 219 SCons.Script.Help(help_text) |
| 220 |
| 221 |
| 222 def SetTargetDescription(self, target_name, description): |
| 223 """Convenience function to set a target's global DESCRIPTION property. |
| 224 |
| 225 Args: |
| 226 self: Environment context. |
| 227 target_name: Name of the target. |
| 228 description: Description of the target. |
| 229 """ |
| 230 self.SetTargetProperty(target_name, all_modes=True, DESCRIPTION=description) |
| 231 |
| 232 |
| 233 def AddTargetMode(env): |
| 234 """Adds the environment as a target mode. |
| 235 |
| 236 Args: |
| 237 env: Environment context. |
| 238 |
| 239 Called via env.Defer() for each build mode. |
| 240 """ |
| 241 # Save the build mode and description |
| 242 mode = env.get('BUILD_TYPE') |
| 243 __target_modes[mode] = TargetMode(mode, env.get('BUILD_TYPE_DESCRIPTION')) |
| 244 |
| 245 |
| 246 #------------------------------------------------------------------------------ |
| 247 |
| 248 |
| 249 def generate(env): |
| 250 # NOTE: SCons requires the use of this name, which fails gpylint. |
| 251 """SCons entry point for this tool.""" |
| 252 env = env # Silence gpylint |
| 253 |
| 254 __builtin__.AddTargetGroup = AddTargetGroup |
| 255 __builtin__.AddTargetHelp = AddTargetHelp |
| 256 __builtin__.GetTargetGroups = GetTargetGroups |
| 257 __builtin__.GetTargetModes = GetTargetModes |
| 258 __builtin__.GetTargets = GetTargets |
| 259 |
| 260 env.AddMethod(SetTargetDescription) |
| 261 env.AddMethod(SetTargetProperty) |
| 262 |
| 263 # Defer per-mode setup |
| 264 env.Defer(AddTargetMode) |
| OLD | NEW |