blob: ae19a60bd4103a2cc1bfbc88db3c74a8954601d1 [file] [log] [blame]
Yuheng Longccfaf2f2013-08-02 14:27:45 -07001# 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):
Yuheng Longe896dfd2013-08-13 20:47:45 -070018 """Get the flags that are different from |flags_dict| by |climb_spec|.
19
20 Given a set of flags, |flags_dict|, return a new set of flags that are
21 adjacent along the flag spec |climb_spec|.
Yuheng Longccfaf2f2013-08-02 14:27:45 -070022
Yuheng Long2b514c22013-08-08 21:07:24 -070023 An example flags_dict is {foo=[1-9]:foo=5, bar=[1-5]:bar=2} and climb_spec is
24 bar=[1-5]. This method changes the flag that contains the spec bar=[1-5]. The
25 results are its neighbors dictionaries, i.e., {foo=[1-9]:foo=5,
26 bar=[1-5]:bar=1} and {foo=[1-9]:foo=5, bar=[1-5]:bar=3}.
27
Yuheng Longccfaf2f2013-08-02 14:27:45 -070028 Args:
29 flags_dict: The dictionary containing the original flags whose neighbors are
30 to be explored.
Yuheng Long2b514c22013-08-08 21:07:24 -070031 climb_spec: The spec in the flags_dict is to be changed. The spec is a
32 definition in the little language, a string with escaped sequences of the
33 form [<start>-<end>] where start and end is an positive integer for a
34 fillable value. An example of a spec is "foo[0-9]".
Yuheng Longccfaf2f2013-08-02 14:27:45 -070035
36 Returns:
Yuheng Longe896dfd2013-08-13 20:47:45 -070037 List of dictionaries of neighbor flags.
Yuheng Longccfaf2f2013-08-02 14:27:45 -070038 """
39
Yuheng Long2b514c22013-08-08 21:07:24 -070040 # This method searches for a pattern [start-end] in the spec. If the spec
41 # contains this pattern, it is a numeric flag. Otherwise it is a boolean flag.
42 # For example, -finline-limit=[1-1000] is a numeric flag and -falign-jumps is
43 # a boolean flag.
44 numeric_flag_match = flags.Search(climb_spec)
Yuheng Longccfaf2f2013-08-02 14:27:45 -070045
46 # If the flags do not contain the spec.
47 if climb_spec not in flags_dict:
48 results = flags_dict.copy()
49
Yuheng Long2b514c22013-08-08 21:07:24 -070050 if numeric_flag_match:
Yuheng Longccfaf2f2013-08-02 14:27:45 -070051 # Numeric flags.
Yuheng Long2b514c22013-08-08 21:07:24 -070052 results[climb_spec] = Flag(climb_spec,
53 int(numeric_flag_match.group('start')))
Yuheng Longccfaf2f2013-08-02 14:27:45 -070054 else:
55 # Boolean flags.
56 results[climb_spec] = Flag(climb_spec)
57
58 return [results]
59
60 # The flags contain the spec.
Yuheng Long2b514c22013-08-08 21:07:24 -070061 if not numeric_flag_match:
Yuheng Longccfaf2f2013-08-02 14:27:45 -070062 # Boolean flags.
63 results = flags_dict.copy()
Yuheng Long2b514c22013-08-08 21:07:24 -070064
65 # Turn off the flag. A flag is turned off if it is not presented in the
66 # flags_dict.
Yuheng Longccfaf2f2013-08-02 14:27:45 -070067 del results[climb_spec]
68 return [results]
69
70 # Numeric flags.
71 flag = flags_dict[climb_spec]
72
73 # The value of the flag having spec.
74 value = flag.GetValue()
75 results = []
76
Yuheng Long2b514c22013-08-08 21:07:24 -070077 if value + 1 < int(numeric_flag_match.group('end')):
Yuheng Longccfaf2f2013-08-02 14:27:45 -070078 # If the value is not the end value, explore the value that is 1 larger than
79 # the current value.
80 neighbor = flags_dict.copy()
81 neighbor[climb_spec] = Flag(climb_spec, value + 1)
82 results.append(neighbor)
83
Yuheng Long2b514c22013-08-08 21:07:24 -070084 if value > int(numeric_flag_match.group('start')):
Yuheng Longccfaf2f2013-08-02 14:27:45 -070085 # If the value is not the start value, explore the value that is 1 lesser
86 # than the current value.
87 neighbor = flags_dict.copy()
88 neighbor[climb_spec] = Flag(climb_spec, value - 1)
89 results.append(neighbor)
90 else:
Yuheng Long2b514c22013-08-08 21:07:24 -070091 # Delete the value, i.e., turn off the flag. A flag is turned off if it is
92 # not presented in the flags_dict.
Yuheng Longccfaf2f2013-08-02 14:27:45 -070093 neighbor = flags_dict.copy()
94 del neighbor[climb_spec]
95 results.append(neighbor)
96
97 return results