flags_util.py revision 2b514c28dd26aa02c2ea0f9924be91fb2394c8a0
1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Utility functions to explore the neighbor flags.
6
7Part of the Chrome build flags optimization.
8"""
9
10__author__ = 'yuhenglong@google.com (Yuheng Long)'
11
12
13import flags
14from flags import Flag
15
16
17def ClimbNext(flags_dict, climb_spec):
18  """Get the flags who are different from flags_dict by climb_spec.
19
20  An example flags_dict is {foo=[1-9]:foo=5, bar=[1-5]:bar=2} and climb_spec is
21  bar=[1-5]. This method changes the flag that contains the spec bar=[1-5]. The
22  results are its neighbors dictionaries, i.e., {foo=[1-9]:foo=5,
23  bar=[1-5]:bar=1} and {foo=[1-9]:foo=5, bar=[1-5]:bar=3}.
24
25  Args:
26    flags_dict: The dictionary containing the original flags whose neighbors are
27      to be explored.
28    climb_spec: The spec in the flags_dict is to be changed. The spec is a
29      definition in the little language, a string with escaped sequences of the
30      form [<start>-<end>] where start and end is an positive integer for a
31      fillable value. An example of a spec is "foo[0-9]".
32
33  Returns:
34    Dictionaries of neighbor flags.
35  """
36
37  # This method searches for a pattern [start-end] in the spec. If the spec
38  # contains this pattern, it is a numeric flag. Otherwise it is a boolean flag.
39  # For example, -finline-limit=[1-1000] is a numeric flag and -falign-jumps is
40  # a boolean flag.
41  numeric_flag_match = flags.Search(climb_spec)
42
43  # If the flags do not contain the spec.
44  if climb_spec not in flags_dict:
45    results = flags_dict.copy()
46
47    if numeric_flag_match:
48      # Numeric flags.
49      results[climb_spec] = Flag(climb_spec,
50                                 int(numeric_flag_match.group('start')))
51    else:
52      # Boolean flags.
53      results[climb_spec] = Flag(climb_spec)
54
55    return [results]
56
57  # The flags contain the spec.
58  if not numeric_flag_match:
59    # Boolean flags.
60    results = flags_dict.copy()
61
62    # Turn off the flag. A flag is turned off if it is not presented in the
63    # flags_dict.
64    del results[climb_spec]
65    return [results]
66
67  # Numeric flags.
68  flag = flags_dict[climb_spec]
69
70  # The value of the flag having spec.
71  value = flag.GetValue()
72  results = []
73
74  if value + 1 < int(numeric_flag_match.group('end')):
75    # If the value is not the end value, explore the value that is 1 larger than
76    # the current value.
77    neighbor = flags_dict.copy()
78    neighbor[climb_spec] = Flag(climb_spec, value + 1)
79    results.append(neighbor)
80
81  if value > int(numeric_flag_match.group('start')):
82    # If the value is not the start value, explore the value that is 1 lesser
83    # than the current value.
84    neighbor = flags_dict.copy()
85    neighbor[climb_spec] = Flag(climb_spec, value - 1)
86    results.append(neighbor)
87  else:
88    # Delete the value, i.e., turn off the flag. A flag is turned off if it is
89    # not presented in the flags_dict.
90    neighbor = flags_dict.copy()
91    del neighbor[climb_spec]
92    results.append(neighbor)
93
94  return results
95