Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import gn_helpers | |
| 6 from optparse import OptionParser | |
| 7 | |
| 8 # 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
| |
| 9 # | |
| 10 # It is assumed that the file contains a toplevel dictionary, and this script | |
| 11 # will return that dictionary as a GN "scope" (see example below). This script | |
| 12 # does not know anything about GYP and it will not expand variables or execute | |
| 13 # conditions (it will check for the presence of a "conditions" value in the | |
| 14 # dictionary and will abort if it is present). It also does not support nested | |
| 15 # dictionaries. | |
| 16 # | |
| 17 # Say your_file.gypi looked like this: | |
| 18 # { | |
| 19 # 'sources': [ 'a.cc', 'b.cc' ], | |
| 20 # 'defines': [ 'ENABLE_DOOM_MELON' ], | |
| 21 # } | |
| 22 # | |
| 23 # You would call it like this: | |
| 24 # gypi_values = exec_script("//build/gypi_to_gn.py", | |
| 25 # [ rebase_path("your_file.gypi") ], "scope") | |
| 26 # | |
| 27 # (The rebase_path call converts the gypi file from being relative to the | |
| 28 # current build file to being system absolute, and the "scope" parameter tells | |
| 29 # GN to interpret the result as a series of GN variable assignments.) | |
| 30 # | |
| 31 # Read the values into a target like this: | |
| 32 # component("mycomponent") { | |
| 33 # sources = gypi_values.sources | |
| 34 # defines = gypi_values.defines | |
| 35 # } | |
| 36 # | |
| 37 # Sometimes your .gypi file will include paths relative to a different | |
| 38 # directory than the current .gn file. In this case, you can rebase them to | |
| 39 # be relative to the current directory. | |
| 40 # sources = rebase_path(gypi_values.sources, ".", | |
| 41 # "//path/gypi/input/values/are/relative/to") | |
| 42 | |
| 43 def LoadPythonDictionary(path): | |
| 44 file_string = open(path).read() | |
| 45 try: | |
| 46 file_data = eval(file_string, {'__builtins__': None}, None) | |
| 47 except SyntaxError, e: | |
| 48 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
| |
| 49 raise | |
| 50 except Exception, e: | |
| 51 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
| |
| 52 raise | |
| 53 | |
| 54 if not isinstance(file_data, dict): | |
| 55 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
| |
| 56 | |
| 57 if 'conditions' in file_data: | |
| 58 raise Error("The file %s has conditions in it. These aren't supported." % | |
| 59 path) | |
| 60 return file_data | |
| 61 | |
| 62 | |
| 63 def main(): | |
| 64 parser = OptionParser() | |
| 65 (options, args) = parser.parse_args() | |
| 66 | |
| 67 if len(args) != 1: | |
| 68 raise Error("Need one argument which is the .gypi file to read.") | |
| 69 | |
| 70 data = LoadPythonDictionary(args[0]) | |
| 71 gn_helpers.PrintValue(data) | |
|
Dirk Pranke
2014/04/03 22:01:28
as noted in gn_helpers, I'd probably change this t
| |
| 72 | |
| 73 main() | |
|
Dirk Pranke
2014/04/03 22:01:28
The correct style for this would be to change line
| |
| OLD | NEW |