OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Converts a given gypi file to a python scope and writes the result to stdout. | 5 """Converts a given gypi file to a python scope and writes the result to stdout. |
6 | 6 |
7 It is assumed that the file contains a toplevel dictionary, and this script | 7 It is assumed that the file contains a toplevel dictionary, and this script |
8 will return that dictionary as a GN "scope" (see example below). This script | 8 will return that dictionary as a GN "scope" (see example below). This script |
9 does not know anything about GYP and it will not expand variables or execute | 9 does not know anything about GYP and it will not expand variables or execute |
10 conditions (it will check for the presence of a "conditions" value in the | 10 conditions. |
11 dictionary and will abort if it is present). It also does not support nested | 11 |
12 dictionaries. | 12 It will strip conditions blocks. |
| 13 |
| 14 A variables block at the top level will be flattened so that the variables |
| 15 appear in the root dictionary. This way they can be returned to the GN code. |
13 | 16 |
14 Say your_file.gypi looked like this: | 17 Say your_file.gypi looked like this: |
15 { | 18 { |
16 'sources': [ 'a.cc', 'b.cc' ], | 19 'sources': [ 'a.cc', 'b.cc' ], |
17 'defines': [ 'ENABLE_DOOM_MELON' ], | 20 'defines': [ 'ENABLE_DOOM_MELON' ], |
18 } | 21 } |
19 | 22 |
20 You would call it like this: | 23 You would call it like this: |
21 gypi_values = exec_script("//build/gypi_to_gn.py", | 24 gypi_values = exec_script("//build/gypi_to_gn.py", |
22 [ rebase_path("your_file.gypi") ], | 25 [ rebase_path("your_file.gypi") ], |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 file_string = open(path).read() | 77 file_string = open(path).read() |
75 try: | 78 try: |
76 file_data = eval(file_string, {'__builtins__': None}, None) | 79 file_data = eval(file_string, {'__builtins__': None}, None) |
77 except SyntaxError, e: | 80 except SyntaxError, e: |
78 e.filename = path | 81 e.filename = path |
79 raise | 82 raise |
80 except Exception, e: | 83 except Exception, e: |
81 raise Exception("Unexpected error while reading %s: %s" % (path, str(e))) | 84 raise Exception("Unexpected error while reading %s: %s" % (path, str(e))) |
82 | 85 |
83 assert isinstance(file_data, dict), "%s does not eval to a dictionary" % path | 86 assert isinstance(file_data, dict), "%s does not eval to a dictionary" % path |
84 assert 'conditions' not in file_data, \ | 87 |
85 "The file %s has conditions in it, these aren't supported." % path | 88 # Strip any conditions. |
| 89 if 'conditions' in file_data: |
| 90 del file_data['conditions'] |
| 91 if 'target_conditions' in file_data: |
| 92 del file_data['target_conditions'] |
| 93 |
| 94 # Flatten any varaiables to the top level. |
| 95 if 'variables' in file_data: |
| 96 file_data.update(file_data['variables']) |
| 97 del file_data['variables'] |
86 | 98 |
87 # If the contents of the root is a dictionary with exactly one kee | 99 # If the contents of the root is a dictionary with exactly one kee |
88 # "variables", promote the contents of that to the top level. Some .gypi | 100 # "variables", promote the contents of that to the top level. Some .gypi |
89 # files contain this and some don't depending on how they expect to be | 101 # files contain this and some don't depending on how they expect to be |
90 # embedded in a .gyp file. We don't actually care either way so collapse it | 102 # embedded in a .gyp file. We don't actually care either way so collapse it |
91 # away. | 103 # away. |
92 if len(file_data) == 1 and 'variables' in file_data: | 104 if len(file_data) == 1 and 'variables' in file_data: |
93 return file_data['variables'] | 105 return file_data['variables'] |
94 | 106 |
95 return file_data | 107 return file_data |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 data = ReplaceSubstrings(data, split[0], split[1]) | 152 data = ReplaceSubstrings(data, split[0], split[1]) |
141 | 153 |
142 print gn_helpers.ToGNString(data) | 154 print gn_helpers.ToGNString(data) |
143 | 155 |
144 if __name__ == '__main__': | 156 if __name__ == '__main__': |
145 try: | 157 try: |
146 main() | 158 main() |
147 except Exception, e: | 159 except Exception, e: |
148 print str(e) | 160 print str(e) |
149 sys.exit(1) | 161 sys.exit(1) |
OLD | NEW |