| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """This script drives the execution of the Mojom parser.""" | |
| 7 | |
| 8 import os | |
| 9 import platform | |
| 10 import subprocess | |
| 11 import sys | |
| 12 | |
| 13 | |
| 14 # We assume this script is located in the Mojo SDK in | |
| 15 # tools/bindings/pylib/mojom/parse. | |
| 16 THIS_DIR = os.path.abspath(os.path.dirname(__file__)) | |
| 17 SDK_ROOT = os.path.abspath(os.path.join(THIS_DIR, os.pardir, os.pardir, | |
| 18 os.pardir, os.pardir, os.pardir)) | |
| 19 PYTHON_SDK_DIR = os.path.abspath(os.path.join(SDK_ROOT, "python")) | |
| 20 sys.path.insert(0, PYTHON_SDK_DIR) | |
| 21 # In order to use mojom_files_mojom we need to make sure the dummy mojo_system | |
| 22 # can be found on the python path. | |
| 23 sys.path.insert(0, os.path.join(PYTHON_SDK_DIR, "dummy_mojo_system")) | |
| 24 | |
| 25 from mojom.generate.generated import mojom_files_mojom | |
| 26 from mojom.generate.generated import mojom_types_mojom | |
| 27 from mojo_bindings import serialization | |
| 28 | |
| 29 def RunParser(sdk_root, file_names, import_directories=None, | |
| 30 meta_data_only=False): | |
| 31 """Runs the mojom parser. Only 64-bit Linux and Mac is supported. | |
| 32 | |
| 33 Args: | |
| 34 sdk_root: {str} Absolute path to the root of the Mojo SDK. The parser | |
| 35 binary is expected to be found in | |
| 36 <sdk_root>/tools/bindings/mojom_tool/bin/<platform> | |
| 37 | |
| 38 file_names {list of str} Paths to mojom files to be parsed. May be either | |
| 39 absolute or relative to the current working directory. | |
| 40 | |
| 41 import_directories: {list of str} Optional specification of import | |
| 42 directories where mojom imports should be searched. The elements of the | |
| 43 list should be absolute paths or paths relative to the current working | |
| 44 directory. | |
| 45 | |
| 46 meta_data_only: {bool} If True then the flag -meta-data-only | |
| 47 will be passed to the parser. | |
| 48 | |
| 49 Returns: | |
| 50 {str} The serialized mojom_files.MojomFileGraph returned by mojom parser, | |
| 51 or None if the mojom parser returned a non-zero error code. | |
| 52 """ | |
| 53 system_dirs = { | |
| 54 ("Linux", "64bit"): "linux64", | |
| 55 ("Darwin", "64bit"): "mac64", | |
| 56 } | |
| 57 system = (platform.system(), platform.architecture()[0]) | |
| 58 if system not in system_dirs: | |
| 59 raise Exception("The mojom parser only supports Linux or Mac 64 bits.") | |
| 60 | |
| 61 mojom_tool = os.path.join(sdk_root, "tools", "bindings", "mojom_tool", | |
| 62 "bin", system_dirs[system], "mojom") | |
| 63 | |
| 64 if not os.path.exists(mojom_tool): | |
| 65 raise Exception( | |
| 66 "The mojom parser could not be found at %s. " | |
| 67 "You may need to run gclient sync." | |
| 68 % mojom_tool) | |
| 69 | |
| 70 cmd = [mojom_tool, "parse"] | |
| 71 if import_directories: | |
| 72 cmd.extend(["-I", ",".join(import_directories)]) | |
| 73 if meta_data_only: | |
| 74 cmd.extend(["-meta-data-only"]) | |
| 75 | |
| 76 cmd.extend(file_names) | |
| 77 | |
| 78 try: | |
| 79 return subprocess.check_output(cmd) | |
| 80 except subprocess.CalledProcessError: | |
| 81 return None | |
| 82 | |
| 83 | |
| 84 def DeserializeMojomFileGraph(serialized_bytes): | |
| 85 """Deserializes a mojom_files.MojomFileGraph. | |
| 86 | |
| 87 Args: | |
| 88 serialized_bytes: {str} The serialized mojom_files.MojomFileGraph returned | |
| 89 by mojom parser | |
| 90 Returns: | |
| 91 {mojom_files.MojomFileGraph} The deserialized MojomFileGraph. | |
| 92 """ | |
| 93 data = bytearray(serialized_bytes) | |
| 94 context = serialization.RootDeserializationContext(data, []) | |
| 95 return mojom_files_mojom.MojomFileGraph.Deserialize(context) | |
| 96 | |
| 97 def ParseToMojomFileGraph(sdk_root, file_names, import_directories=None, | |
| 98 meta_data_only=False): | |
| 99 """Runs the mojom parser and deserializes the result. Only 64-bit Linux and | |
| 100 Mac is supported. | |
| 101 | |
| 102 Args: | |
| 103 sdk_root: {str} Absolute path to the root of the Mojo SDK. The parser | |
| 104 binary is expected to be found in | |
| 105 <sdk_root>/tools/bindings/mojom_tool/bin/<platform> | |
| 106 | |
| 107 file_names {list of str} Paths to mojom files to be parsed. May be either | |
| 108 absolute or relative to the current working directory. | |
| 109 | |
| 110 import_directories: {list of str} Optional specification of import | |
| 111 directories where mojom imports should be searched. The elements of the | |
| 112 list should be absolute paths or paths relative to the current working | |
| 113 directory. | |
| 114 | |
| 115 meta_data_only: {bool} If True then the flag -meta-data-only | |
| 116 will be passed to the parser. | |
| 117 | |
| 118 Returns: | |
| 119 {mojom_files.MojomFileGraph} The deserialized MojomFileGraph obtained by | |
| 120 deserializing the bytes returned by mojom parser, or None if the mojom | |
| 121 parser returned a non-zero error code. | |
| 122 """ | |
| 123 serialized_bytes = RunParser(sdk_root, file_names, import_directories, | |
| 124 meta_data_only) | |
| 125 if serialized_bytes is None: | |
| 126 return None | |
| 127 return DeserializeMojomFileGraph(serialized_bytes) | |
| OLD | NEW |