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. |
| 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") ], |
| 26 # "scope", |
| 27 # [ "your_file.gypi" ]) |
| 28 # |
| 29 # Notes: |
| 30 # - The rebase_path call converts the gypi file from being relative to the |
| 31 # current build file to being system absolute for calling the script, which |
| 32 # will have a different current directory than this file. |
| 33 # |
| 34 # - The "scope" parameter tells GN to interpret the result as a series of GN |
| 35 # variable assignments. |
| 36 # |
| 37 # - The last file argument to exec_script tells GN that the given file is a |
| 38 # dependency of the build so Ninja can automatically re-run GN if the file |
| 39 # changes. |
| 40 # |
| 41 # Read the values into a target like this: |
| 42 # component("mycomponent") { |
| 43 # sources = gypi_values.sources |
| 44 # defines = gypi_values.defines |
| 45 # } |
| 46 # |
| 47 # Sometimes your .gypi file will include paths relative to a different |
| 48 # directory than the current .gn file. In this case, you can rebase them to |
| 49 # be relative to the current directory. |
| 50 # sources = rebase_path(gypi_values.sources, ".", |
| 51 # "//path/gypi/input/values/are/relative/to") |
| 52 |
| 53 def LoadPythonDictionary(path): |
| 54 file_string = open(path).read() |
| 55 try: |
| 56 file_data = eval(file_string, {'__builtins__': None}, None) |
| 57 except SyntaxError, e: |
| 58 e.filename = path |
| 59 raise |
| 60 except Exception, e: |
| 61 ExceptionAppend(e, 'while reading ' + path) |
| 62 raise |
| 63 |
| 64 if not isinstance(file_data, dict): |
| 65 raise Error("%s does not evaluate to a dictionary" % path) |
| 66 |
| 67 if 'conditions' in file_data: |
| 68 raise Error("The file %s has conditions in it. These aren't supported." % |
| 69 path) |
| 70 return file_data |
| 71 |
| 72 |
| 73 def main(): |
| 74 parser = OptionParser() |
| 75 (options, args) = parser.parse_args() |
| 76 |
| 77 if len(args) != 1: |
| 78 raise Error("Need one argument which is the .gypi file to read.") |
| 79 |
| 80 data = LoadPythonDictionary(args[0]) |
| 81 gn_helpers.PrintValue(data) |
| 82 |
| 83 main() |
OLD | NEW |