Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: build/gypi_to_gn.py

Issue 231983002: Add support for substring replacement in gypi_to_gn (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | skia/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 (it will check for the presence of a "conditions" value in the
(...skipping 28 matching lines...) Expand all
39 component("mycomponent") { 39 component("mycomponent") {
40 sources = gypi_values.sources 40 sources = gypi_values.sources
41 defines = gypi_values.defines 41 defines = gypi_values.defines
42 } 42 }
43 43
44 Sometimes your .gypi file will include paths relative to a different 44 Sometimes your .gypi file will include paths relative to a different
45 directory than the current .gn file. In this case, you can rebase them to 45 directory than the current .gn file. In this case, you can rebase them to
46 be relative to the current directory. 46 be relative to the current directory.
47 sources = rebase_path(gypi_values.sources, ".", 47 sources = rebase_path(gypi_values.sources, ".",
48 "//path/gypi/input/values/are/relative/to") 48 "//path/gypi/input/values/are/relative/to")
49
50 This script will tolerate a 'variables' in the toplevel dictionary or not. If
51 the toplevel dictionary just contains one item called 'variables', it will be
52 collapsed away and the result will be the contents of that dictinoary. Some
53 .gypi files are written with or without this, depending on how they expect to
54 be embedded into a .gyp file.
55
56 This script also has the ability to replace certain substrings in the input.
57 Generally this is used to emulate GYP variable expansion. If you passed the
58 argument "--replace=<(foo)=bar" then all instances of "<(foo)" in strings in
59 the input will be replaced with "bar":
60
61 gypi_values = exec_script("//build/gypi_to_gn.py",
62 [ rebase_path("your_file.gypi"),
63 "--replace=<(foo)=bar"],
64 "scope",
65 [ "your_file.gypi" ])
66
49 """ 67 """
50 68
51 import gn_helpers 69 import gn_helpers
52 from optparse import OptionParser 70 from optparse import OptionParser
53 import sys 71 import sys
54 72
55 def LoadPythonDictionary(path): 73 def LoadPythonDictionary(path):
56 file_string = open(path).read() 74 file_string = open(path).read()
57 try: 75 try:
58 file_data = eval(file_string, {'__builtins__': None}, None) 76 file_data = eval(file_string, {'__builtins__': None}, None)
59 except SyntaxError, e: 77 except SyntaxError, e:
60 e.filename = path 78 e.filename = path
61 raise 79 raise
62 except Exception, e: 80 except Exception, e:
63 raise Exception("Unexpected error while reading %s: %s" % (path, str(e))) 81 raise Exception("Unexpected error while reading %s: %s" % (path, str(e)))
64 82
65 assert isinstance(file_data, dict), "%s does not eval to a dictionary" % path 83 assert isinstance(file_data, dict), "%s does not eval to a dictionary" % path
66 assert 'conditions' not in file_data, \ 84 assert 'conditions' not in file_data, \
67 "The file %s has conditions in it, these aren't supported." % path 85 "The file %s has conditions in it, these aren't supported." % path
68 86
87 # 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
89 # 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
91 # away.
92 if len(file_data) == 1 and 'variables' in file_data:
93 return file_data['variables']
94
69 return file_data 95 return file_data
70 96
71 97
98 def ReplaceSubstrings(values, search_for, replace_with):
99 """Recursively replaces substrings in a value.
100
101 Replaces all substrings of the "search_for" with "repace_with" for all
102 strings occurring in "values". This is done by recursively iterating into
103 lists as well as the keys and values of dictionaries."""
104 if isinstance(values, str):
105 return values.replace(search_for, replace_with)
106
107 if isinstance(values, list):
108 return [ReplaceSubstrings(v, search_for, replace_with) for v in values]
109
110 if isinstance(values, dict):
111 # For dictionaries, do the search for both the key and values.
112 result = {}
113 for key, value in values.items():
114 new_key = ReplaceSubstrings(key, search_for, replace_with)
115 new_value = ReplaceSubstrings(value, search_for, replace_with)
116 result[new_key] = new_value
117 return result
118
119 # Assume everything else is unchanged.
120 return values
121
72 def main(): 122 def main():
73 parser = OptionParser() 123 parser = OptionParser()
124 parser.add_option("-r", "--replace", action="append",
125 help="Replaces substrings. If passed a=b, replaces all substrs a with b.")
74 (options, args) = parser.parse_args() 126 (options, args) = parser.parse_args()
75 127
76 if len(args) != 1: 128 if len(args) != 1:
77 raise Exception("Need one argument which is the .gypi file to read.") 129 raise Exception("Need one argument which is the .gypi file to read.")
78 130
79 data = LoadPythonDictionary(args[0]) 131 data = LoadPythonDictionary(args[0])
132 if options.replace:
133 # Do replacements for all specified patterns.
134 for replace in options.replace:
135 split = replace.split('=')
136 # Allow "foo=" to replace with nothing.
137 if len(split) == 1:
138 split.append('')
139 assert len(split) == 2, "Replacement must be of the form 'key=value'."
140 data = ReplaceSubstrings(data, split[0], split[1])
141
80 print gn_helpers.ToGNString(data) 142 print gn_helpers.ToGNString(data)
81 143
82 if __name__ == '__main__': 144 if __name__ == '__main__':
83 try: 145 try:
84 main() 146 main()
85 except Exception, e: 147 except Exception, e:
86 print str(e) 148 print str(e)
87 sys.exit(1) 149 sys.exit(1)
OLDNEW
« no previous file with comments | « no previous file | skia/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698