diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-11 17:23:40 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-11 17:23:40 +0000 |
commit | f2f4d5ebf657ec531d618fc52b39e4743edfe0cc (patch) | |
tree | a16fd6948a446725cc7b4e48fe774edf722477ad /build | |
parent | 7d999d5179ef9eb0cefde962b630b373f3e83e39 (diff) | |
download | chromium_src-f2f4d5ebf657ec531d618fc52b39e4743edfe0cc.zip chromium_src-f2f4d5ebf657ec531d618fc52b39e4743edfe0cc.tar.gz chromium_src-f2f4d5ebf657ec531d618fc52b39e4743edfe0cc.tar.bz2 |
Add support for substring replacement in gypi_to_gn
Use substring replacement in skia source file list, and update the skia BUILD.gn file accordingly. The .gn file gets a bit simpler now: we can do the substring replacement to make absolute paths rather than doing rebase for all the files.
R=djsollen@google.com, dpranke@chromium.org
Review URL: https://codereview.chromium.org/231983002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@263272 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'build')
-rw-r--r-- | build/gypi_to_gn.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/build/gypi_to_gn.py b/build/gypi_to_gn.py index 1446d81..d753d10 100644 --- a/build/gypi_to_gn.py +++ b/build/gypi_to_gn.py @@ -46,6 +46,24 @@ directory than the current .gn file. In this case, you can rebase them to be relative to the current directory. sources = rebase_path(gypi_values.sources, ".", "//path/gypi/input/values/are/relative/to") + +This script will tolerate a 'variables' in the toplevel dictionary or not. If +the toplevel dictionary just contains one item called 'variables', it will be +collapsed away and the result will be the contents of that dictinoary. Some +.gypi files are written with or without this, depending on how they expect to +be embedded into a .gyp file. + +This script also has the ability to replace certain substrings in the input. +Generally this is used to emulate GYP variable expansion. If you passed the +argument "--replace=<(foo)=bar" then all instances of "<(foo)" in strings in +the input will be replaced with "bar": + + gypi_values = exec_script("//build/gypi_to_gn.py", + [ rebase_path("your_file.gypi"), + "--replace=<(foo)=bar"], + "scope", + [ "your_file.gypi" ]) + """ import gn_helpers @@ -66,17 +84,61 @@ def LoadPythonDictionary(path): assert 'conditions' not in file_data, \ "The file %s has conditions in it, these aren't supported." % path + # If the contents of the root is a dictionary with exactly one kee + # "variables", promote the contents of that to the top level. Some .gypi + # files contain this and some don't depending on how they expect to be + # embedded in a .gyp file. We don't actually care either way so collapse it + # away. + if len(file_data) == 1 and 'variables' in file_data: + return file_data['variables'] + return file_data +def ReplaceSubstrings(values, search_for, replace_with): + """Recursively replaces substrings in a value. + + Replaces all substrings of the "search_for" with "repace_with" for all + strings occurring in "values". This is done by recursively iterating into + lists as well as the keys and values of dictionaries.""" + if isinstance(values, str): + return values.replace(search_for, replace_with) + + if isinstance(values, list): + return [ReplaceSubstrings(v, search_for, replace_with) for v in values] + + if isinstance(values, dict): + # For dictionaries, do the search for both the key and values. + result = {} + for key, value in values.items(): + new_key = ReplaceSubstrings(key, search_for, replace_with) + new_value = ReplaceSubstrings(value, search_for, replace_with) + result[new_key] = new_value + return result + + # Assume everything else is unchanged. + return values + def main(): parser = OptionParser() + parser.add_option("-r", "--replace", action="append", + help="Replaces substrings. If passed a=b, replaces all substrs a with b.") (options, args) = parser.parse_args() if len(args) != 1: raise Exception("Need one argument which is the .gypi file to read.") data = LoadPythonDictionary(args[0]) + if options.replace: + # Do replacements for all specified patterns. + for replace in options.replace: + split = replace.split('=') + # Allow "foo=" to replace with nothing. + if len(split) == 1: + split.append('') + assert len(split) == 2, "Replacement must be of the form 'key=value'." + data = ReplaceSubstrings(data, split[0], split[1]) + print gn_helpers.ToGNString(data) if __name__ == '__main__': |