diff options
Diffstat (limited to 'chromium/third_party/blink/renderer/bindings/scripts/bind_gen/path_manager.py')
-rw-r--r-- | chromium/third_party/blink/renderer/bindings/scripts/bind_gen/path_manager.py | 180 |
1 files changed, 133 insertions, 47 deletions
diff --git a/chromium/third_party/blink/renderer/bindings/scripts/bind_gen/path_manager.py b/chromium/third_party/blink/renderer/bindings/scripts/bind_gen/path_manager.py index b6997d71e65..47d6c08b8ee 100644 --- a/chromium/third_party/blink/renderer/bindings/scripts/bind_gen/path_manager.py +++ b/chromium/third_party/blink/renderer/bindings/scripts/bind_gen/path_manager.py @@ -2,6 +2,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import os.path import posixpath import web_idl @@ -12,7 +13,16 @@ from .blink_v8_bridge import blink_class_name class PathManager(object): """ - Provides a variety of paths such as Blink headers and output files. + Provides a variety of paths such as Blink headers and output files. Unless + explicitly specified, returned paths are relative to the project's root + directory or the root directory of generated files, e.g. + "third_party/blink/renderer/..." + + Relative paths are represented in POSIX style so that it fits nicely in + generated code, e.g. #include "third_party/blink/renderer/...", while + absolute paths are represented in a platform-specific style so that it works + well with a platform-specific notion, e.g. a drive letter in Windows path + such as "C:\\chromium\\src\\...". About output files, there are two cases. - cross-components case: @@ -26,36 +36,72 @@ class PathManager(object): _is_initialized = False @classmethod - def init(cls, output_dirs): + def init(cls, root_src_dir, root_gen_dir, component_reldirs): """ Args: - output_dirs: Pairs of component and output directory. + root_src_dir: Project's root directory, which corresponds to "//" + in GN. + root_gen_dir: Root directory of generated files, which corresponds + to "//out/Default/gen" in GN. + component_reldirs: Pairs of component and output directory relative + to |root_gen_dir|. """ assert not cls._is_initialized - assert isinstance(output_dirs, dict) - cls._output_dirs = output_dirs + assert isinstance(root_src_dir, str) + assert isinstance(root_gen_dir, str) + assert isinstance(component_reldirs, dict) + cls._blink_path_prefix = posixpath.sep + posixpath.join( "third_party", "blink", "renderer", "") + + cls._root_src_dir = os.path.abspath(root_src_dir) + cls._root_gen_dir = os.path.abspath(root_gen_dir) + cls._component_reldirs = { + component: posixpath.normpath(rel_dir) + for component, rel_dir in component_reldirs.items() + } cls._is_initialized = True @classmethod - def relpath_to_project_root(cls, path): - index = path.find(cls._blink_path_prefix) - if index < 0: - assert path.startswith(cls._blink_path_prefix[1:]) - return path - return path[index + 1:] + def component_path(cls, component, filepath): + """ + Returns the relative path to |filepath| in |component|'s directory. + """ + assert cls._is_initialized, cls._REQUIRE_INIT_MESSAGE + return posixpath.join(cls._component_reldirs[component], filepath) + + @classmethod + def gen_path_to(cls, path): + """ + Returns the absolute path of |path| that must be relative to the root + directory of generated files. + """ + assert cls._is_initialized, cls._REQUIRE_INIT_MESSAGE + return os.path.abspath(os.path.join(cls._root_gen_dir, path)) + + @classmethod + def src_path_to(cls, path): + """ + Returns the absolute path of |path| that must be relative to the + project root directory. + """ + assert cls._is_initialized, cls._REQUIRE_INIT_MESSAGE + return os.path.abspath(os.path.join(cls._root_src_dir, path)) def __init__(self, idl_definition): assert self._is_initialized, self._REQUIRE_INIT_MESSAGE - idl_path = idl_definition.debug_info.location.filepath - self._idl_basepath, _ = posixpath.splitext(idl_path) - self._idl_dir, self._idl_basename = posixpath.split(self._idl_basepath) - - components = sorted(idl_definition.components) + components = sorted(idl_definition.components) # "core" < "modules" - if len(components) == 1: + if len(components) == 0: + assert isinstance(idl_definition, web_idl.Union) + # Unions of built-in types, e.g. DoubleOrString, do not have a + # component. + self._is_cross_components = False + default_component = web_idl.Component("core") + self._api_component = default_component + self._impl_component = default_component + elif len(components) == 1: component = components[0] self._is_cross_components = False self._api_component = component @@ -64,37 +110,36 @@ class PathManager(object): assert components[0] == "core" assert components[1] == "modules" self._is_cross_components = True - self._api_component = "core" - self._impl_component = "modules" + # Union does not have to support cross-component code generation + # because clients of IDL union must be on an upper or same layer to + # any of union members. + if isinstance(idl_definition, web_idl.Union): + self._api_component = components[1] + else: + self._api_component = components[0] + self._impl_component = components[1] else: assert False - self._api_dir = self._output_dirs[self._api_component] - self._impl_dir = self._output_dirs[self._impl_component] - self._out_basename = name_style.file("v8", idl_definition.identifier) - - if isinstance(idl_definition, - (web_idl.CallbackFunction, web_idl.CallbackInterface)): - self._blink_dir = self._api_dir - else: - self._blink_dir = self._idl_dir - self._blink_basename = name_style.file( - blink_class_name(idl_definition)) - - @property - def idl_dir(self): - return self._idl_dir - - def blink_path(self, filename=None, ext=None): - """ - Returns a path to a Blink implementation file relative to the project - root directory, e.g. "third_party/blink/renderer/..." - """ - return self.relpath_to_project_root( - self._join( - dirpath=self._blink_dir, - filename=(filename or self._blink_basename), - ext=ext)) + self._api_dir = self._component_reldirs[self._api_component] + self._impl_dir = self._component_reldirs[self._impl_component] + self._api_basename = name_style.file("v8", idl_definition.identifier) + self._impl_basename = name_style.file("v8", idl_definition.identifier) + # TODO(peria, yukishiino): Add "v8" prefix to union's files. Trying to + # produce the same filepaths with the old bindings generator for the + # time being. + if isinstance(idl_definition, web_idl.Union): + union_class_name = idl_definition.identifier + union_filepath = _BACKWARD_COMPATIBLE_UNION_FILEPATHS.get( + union_class_name, union_class_name) + self._api_basename = name_style.file(union_filepath) + self._impl_basename = name_style.file(union_filepath) + + if not isinstance(idl_definition, web_idl.Union): + idl_path = idl_definition.debug_info.location.filepath + self._blink_dir = posixpath.dirname(idl_path) + self._blink_basename = name_style.file( + blink_class_name(idl_definition)) @property def is_cross_components(self): @@ -111,7 +156,7 @@ class PathManager(object): def api_path(self, filename=None, ext=None): return self._join( dirpath=self.api_dir, - filename=(filename or self._out_basename), + filename=(filename or self._api_basename), ext=ext) @property @@ -125,7 +170,17 @@ class PathManager(object): def impl_path(self, filename=None, ext=None): return self._join( dirpath=self.impl_dir, - filename=(filename or self._out_basename), + filename=(filename or self._impl_basename), + ext=ext) + + @property + def blink_dir(self): + return self._blink_dir + + def blink_path(self, filename=None, ext=None): + return self._join( + dirpath=self.blink_dir, + filename=(filename or self._blink_basename), ext=ext) @staticmethod @@ -133,3 +188,34 @@ class PathManager(object): if ext is not None: filename = posixpath.extsep.join([filename, ext]) return posixpath.join(dirpath, filename) + + +# A hack to make the filepaths to generated IDL unions compatible with the old +# bindings generator. +# +# Copied from |shorten_union_name| defined in +# //third_party/blink/renderer/bindings/scripts/utilities.py +_BACKWARD_COMPATIBLE_UNION_FILEPATHS = { + # modules/canvas2d/CanvasRenderingContext2D.idl + "CSSImageValueOrHTMLImageElementOrSVGImageElementOrHTMLVideoElementOrHTMLCanvasElementOrImageBitmapOrOffscreenCanvas": + "CanvasImageSource", + # modules/canvas/htmlcanvas/html_canvas_element_module_support_webgl2_compute.idl + "CanvasRenderingContext2DOrWebGLRenderingContextOrWebGL2RenderingContextOrWebGL2ComputeRenderingContextOrImageBitmapRenderingContextOrGPUCanvasContext": + "RenderingContext", + # modules/canvas/htmlcanvas/html_canvas_element_module.idl + "CanvasRenderingContext2DOrWebGLRenderingContextOrWebGL2RenderingContextOrImageBitmapRenderingContextOrGPUCanvasContext": + "RenderingContext", + # core/frame/window_or_worker_global_scope.idl + "HTMLImageElementOrSVGImageElementOrHTMLVideoElementOrHTMLCanvasElementOrBlobOrImageDataOrImageBitmapOrOffscreenCanvas": + "ImageBitmapSource", + # bindings/tests/idls/core/TestTypedefs.idl + "NodeOrLongSequenceOrEventOrXMLHttpRequestOrStringOrStringByteStringOrNodeListRecord": + "NestedUnionType", + # modules/canvas/offscreencanvas/offscreen_canvas_module_support_webgl2_compute.idl. + # Due to offscreen_canvas_module_support_webgl2_compute.idl and offscreen_canvas_module.idl are exclusive in modules_idl_files.gni, they have same shorten name. + "OffscreenCanvasRenderingContext2DOrWebGLRenderingContextOrWebGL2RenderingContextOrWebGL2ComputeRenderingContextOrImageBitmapRenderingContext": + "OffscreenRenderingContext", + # modules/canvas/offscreencanvas/offscreen_canvas_module.idl + "OffscreenCanvasRenderingContext2DOrWebGLRenderingContextOrWebGL2RenderingContextOrImageBitmapRenderingContext": + "OffscreenRenderingContext", +} |