Chromium Code Reviews| Index: build/gypi_to_gn.py |
| diff --git a/build/gypi_to_gn.py b/build/gypi_to_gn.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..94f75969a5723f955d542a994316ecc5f984f468 |
| --- /dev/null |
| +++ b/build/gypi_to_gn.py |
| @@ -0,0 +1,73 @@ |
| +# Copyright 2014 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import gn_helpers |
| +from optparse import OptionParser |
| + |
| +# Converts a given gypi file to a python scope and writes the result to stdout. |
|
Dirk Pranke
2014/04/03 22:01:28
It would probably be more Pythonic to make this al
|
| +# |
| +# It is assumed that the file contains a toplevel dictionary, and this script |
| +# will return that dictionary as a GN "scope" (see example below). This script |
| +# does not know anything about GYP and it will not expand variables or execute |
| +# conditions (it will check for the presence of a "conditions" value in the |
| +# dictionary and will abort if it is present). It also does not support nested |
| +# dictionaries. |
| +# |
| +# Say your_file.gypi looked like this: |
| +# { |
| +# 'sources': [ 'a.cc', 'b.cc' ], |
| +# 'defines': [ 'ENABLE_DOOM_MELON' ], |
| +# } |
| +# |
| +# You would call it like this: |
| +# gypi_values = exec_script("//build/gypi_to_gn.py", |
| +# [ rebase_path("your_file.gypi") ], "scope") |
| +# |
| +# (The rebase_path call converts the gypi file from being relative to the |
| +# current build file to being system absolute, and the "scope" parameter tells |
| +# GN to interpret the result as a series of GN variable assignments.) |
| +# |
| +# Read the values into a target like this: |
| +# component("mycomponent") { |
| +# sources = gypi_values.sources |
| +# defines = gypi_values.defines |
| +# } |
| +# |
| +# Sometimes your .gypi file will include paths relative to a different |
| +# 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") |
| + |
| +def LoadPythonDictionary(path): |
| + file_string = open(path).read() |
| + try: |
| + file_data = eval(file_string, {'__builtins__': None}, None) |
| + except SyntaxError, e: |
| + e.filename = path |
|
Dirk Pranke
2014/04/03 22:01:28
overriding the filename here in the exception migh
brettw
2014/04/04 20:32:28
Dunnow, I copied this from GYP and it seems to mak
|
| + raise |
| + except Exception, e: |
| + ExceptionAppend(e, 'while reading ' + path) |
|
scottmg
2014/04/03 20:44:40
this isn't imported from gn_helpers
Dirk Pranke
2014/04/03 22:01:28
Typically I would raise a new exception and includ
Dirk Pranke
2014/04/03 22:01:28
Right, "import gn_helpers" doesn't bring names int
|
| + raise |
| + |
| + if not isinstance(file_data, dict): |
| + raise Error("%s does not evaluate to a dictionary" % path) |
|
Dirk Pranke
2014/04/03 22:01:28
Depending on how you except to handle these except
|
| + |
| + if 'conditions' in file_data: |
| + raise Error("The file %s has conditions in it. These aren't supported." % |
| + path) |
| + return file_data |
| + |
| + |
| +def main(): |
| + parser = OptionParser() |
| + (options, args) = parser.parse_args() |
| + |
| + if len(args) != 1: |
| + raise Error("Need one argument which is the .gypi file to read.") |
| + |
| + data = LoadPythonDictionary(args[0]) |
| + gn_helpers.PrintValue(data) |
|
Dirk Pranke
2014/04/03 22:01:28
as noted in gn_helpers, I'd probably change this t
|
| + |
| +main() |
|
Dirk Pranke
2014/04/03 22:01:28
The correct style for this would be to change line
|