Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 1 | # Copyright 2019 The Chromium OS 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 | """Build graph dependency creation service. |
| 6 | |
| 7 | This service handles the creation of the portage build dependency graphs and the |
| 8 | graphs mapping from portage packages to the dependency source. |
| 9 | """ |
| 10 | |
Navil Perez | a782326 | 2020-08-07 17:56:57 +0000 | [diff] [blame] | 11 | from chromite.api import api_config |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 12 | from chromite.api import faux |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 13 | from chromite.api import validate |
LaMont Jones | 00b9cff | 2019-12-12 12:14:07 -0700 | [diff] [blame] | 14 | from chromite.api.controller import controller_util |
Navil Perez | a782326 | 2020-08-07 17:56:57 +0000 | [diff] [blame] | 15 | from chromite.api.gen.chromite.api import depgraph_pb2 |
LaMont Jones | 4cbecba | 2020-05-12 11:54:27 -0600 | [diff] [blame] | 16 | # TODO(crbug/1081828): stop using build_target and drop it from the proto. |
Mike Frysinger | 06a51c8 | 2021-04-06 11:39:17 -0400 | [diff] [blame] | 17 | from chromite.lib import build_target_lib |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 18 | from chromite.lib.parser import package_info |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 19 | from chromite.service import dependency |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 20 | |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 21 | |
Ned Nguyen | 8e144bd | 2019-05-02 09:09:41 -0600 | [diff] [blame] | 22 | def AugmentDepGraphProtoFromJsonMap(json_map, graph): |
| 23 | """Augment package deps from |json_map| to graph object. |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 24 | |
| 25 | Args: |
| 26 | json_map: the json object that stores the portage package. This is |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 27 | generated from chromite.lib.service.dependency.GetBuildDependency() |
Ned Nguyen | 8e144bd | 2019-05-02 09:09:41 -0600 | [diff] [blame] | 28 | graph: the proto object that represents the dependency graph (see DepGraph |
| 29 | message in chromite/api/depgraph.proto) |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 30 | """ |
LaMont Jones | 4cbecba | 2020-05-12 11:54:27 -0600 | [diff] [blame] | 31 | graph.sysroot.build_target.name = json_map['target_board'] |
| 32 | graph.sysroot.path = json_map['sysroot_path'] |
| 33 | # TODO(crbug/1081828): Drop this when no longer used. |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 34 | graph.build_target.name = json_map['target_board'] |
| 35 | |
Mike Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 36 | for data in json_map['package_deps'].values(): |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 37 | package_dep_info = graph.package_deps.add() |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 38 | package_info_msg = package_dep_info.package_info |
| 39 | package_info_msg.package_name = data['name'] |
| 40 | package_info_msg.category = data['category'] |
| 41 | package_info_msg.version = data['version'] |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 42 | for dep in data['deps']: |
Alex Klein | 5c4625a | 2020-10-06 17:40:22 -0600 | [diff] [blame] | 43 | cpv = package_info.parse(dep) |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 44 | dep_package = package_dep_info.dependency_packages.add() |
Alex Klein | 5c4625a | 2020-10-06 17:40:22 -0600 | [diff] [blame] | 45 | controller_util.serialize_package_info(cpv, dep_package) |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 46 | |
Alex Klein | 5c4625a | 2020-10-06 17:40:22 -0600 | [diff] [blame] | 47 | package_CPV = controller_util.PackageInfoToString(package_info_msg) |
Ned Nguyen | 8be1005 | 2019-02-26 08:58:45 -0700 | [diff] [blame] | 48 | for path in json_map['source_path_mapping'][package_CPV]: |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 49 | source_path = package_dep_info.dependency_source_paths.add() |
| 50 | source_path.path = path |
| 51 | |
Ned Nguyen | b5d3872 | 2019-05-06 10:24:06 -0600 | [diff] [blame] | 52 | |
Michael Mortensen | af10cca | 2019-11-18 20:21:32 -0700 | [diff] [blame] | 53 | def _GetBuildDependencyGraphResponse(_input_proto, output_proto, _config): |
| 54 | """Add fake dep_graph data to a successful response.""" |
| 55 | output_proto.dep_graph.build_target.name = 'target_board' |
| 56 | |
| 57 | |
| 58 | @faux.success(_GetBuildDependencyGraphResponse) |
| 59 | @faux.empty_error |
Alex Klein | 45b7343 | 2020-09-23 13:51:20 -0600 | [diff] [blame] | 60 | @validate.require_each('packages', ['category', 'package_name']) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 61 | @validate.validation_complete |
Eric Lin | aafedbf | 2021-08-19 19:43:47 +0800 | [diff] [blame] | 62 | def GetBuildDependencyGraph( |
| 63 | input_proto: depgraph_pb2.GetBuildDependencyGraphRequest, |
| 64 | output_proto: depgraph_pb2.GetBuildDependencyGraphResponse, |
| 65 | _config: api_config.ApiConfig) -> None: |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 66 | """Create the build dependency graph. |
| 67 | |
| 68 | Args: |
Eric Lin | aafedbf | 2021-08-19 19:43:47 +0800 | [diff] [blame] | 69 | input_proto: The input arguments message. |
| 70 | output_proto: The empty output message. |
| 71 | _config: The API call config. |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 72 | """ |
LaMont Jones | 4cbecba | 2020-05-12 11:54:27 -0600 | [diff] [blame] | 73 | if input_proto.HasField('sysroot'): |
| 74 | board = input_proto.sysroot.build_target.name |
| 75 | sysroot_path = input_proto.sysroot.path |
| 76 | else: |
| 77 | # TODO(crbug/1081828): stop using build_target and drop it from the proto. |
| 78 | board = input_proto.build_target.name |
Mike Frysinger | 06a51c8 | 2021-04-06 11:39:17 -0400 | [diff] [blame] | 79 | sysroot_path = build_target_lib.get_default_sysroot_path(board or None) |
LaMont Jones | 4cbecba | 2020-05-12 11:54:27 -0600 | [diff] [blame] | 80 | |
Alex Klein | d8cd4c6 | 2020-09-14 13:37:47 -0600 | [diff] [blame] | 81 | packages = tuple( |
| 82 | controller_util.PackageInfoToCPV(x) for x in input_proto.packages) |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 83 | |
LaMont Jones | 4cbecba | 2020-05-12 11:54:27 -0600 | [diff] [blame] | 84 | json_map, sdk_json_map = dependency.GetBuildDependency(sysroot_path, board, |
| 85 | packages) |
Ned Nguyen | 8e144bd | 2019-05-02 09:09:41 -0600 | [diff] [blame] | 86 | AugmentDepGraphProtoFromJsonMap(json_map, output_proto.dep_graph) |
Chris McDonald | a22b74f | 2019-11-22 13:55:06 -0700 | [diff] [blame] | 87 | AugmentDepGraphProtoFromJsonMap(sdk_json_map, output_proto.sdk_dep_graph) |
Chris McDonald | 360c2da | 2019-12-18 13:25:16 -0700 | [diff] [blame] | 88 | |
| 89 | |
Navil Perez | a782326 | 2020-08-07 17:56:57 +0000 | [diff] [blame] | 90 | def _ListResponse(_input_proto, output_proto, _config): |
| 91 | """Add fake dependency data to a successful response.""" |
| 92 | package_dep = output_proto.package_deps.add() |
| 93 | package_dep.category = 'category' |
| 94 | package_dep.package_name = 'name' |
| 95 | |
| 96 | |
| 97 | @faux.success(_ListResponse) |
| 98 | @faux.empty_error |
| 99 | @validate.require('sysroot.build_target.name') |
| 100 | @validate.exists('sysroot.path') |
Alex Klein | 45b7343 | 2020-09-23 13:51:20 -0600 | [diff] [blame] | 101 | @validate.require_each('src_paths', ['path']) |
| 102 | @validate.require_each('packages', ['category', 'package_name']) |
Navil Perez | a782326 | 2020-08-07 17:56:57 +0000 | [diff] [blame] | 103 | @validate.validation_complete |
| 104 | def List(input_proto: depgraph_pb2.ListRequest, |
| 105 | output_proto: depgraph_pb2.ListResponse, |
| 106 | _config: api_config.ApiConfig): |
| 107 | """Get a list of package dependencies. |
| 108 | |
| 109 | Args: |
| 110 | input_proto: The input arguments message. |
| 111 | output_proto: The empty output message. |
| 112 | _config: The API call config. |
| 113 | """ |
Navil Perez | a782326 | 2020-08-07 17:56:57 +0000 | [diff] [blame] | 114 | sysroot_path = input_proto.sysroot.path |
| 115 | src_paths = [src_path.path for src_path in input_proto.src_paths] |
Navil Perez | a782326 | 2020-08-07 17:56:57 +0000 | [diff] [blame] | 116 | package_deps = dependency.GetDependencies( |
| 117 | sysroot_path, |
Navil Perez | a782326 | 2020-08-07 17:56:57 +0000 | [diff] [blame] | 118 | src_paths=src_paths, |
Navil Perez | 2c238b5 | 2021-03-17 16:33:12 +0000 | [diff] [blame] | 119 | packages=[ |
| 120 | controller_util.deserialize_package_info(package) |
| 121 | for package in input_proto.packages |
Navil Perez | 3fa1ca8 | 2021-03-19 13:53:20 +0000 | [diff] [blame] | 122 | ], |
Navil Perez | 2c10843 | 2021-04-09 04:17:30 +0000 | [diff] [blame] | 123 | include_rev_dependencies=input_proto.include_rev_deps) |
Navil Perez | a782326 | 2020-08-07 17:56:57 +0000 | [diff] [blame] | 124 | for package in package_deps: |
Alex Klein | 5c4625a | 2020-10-06 17:40:22 -0600 | [diff] [blame] | 125 | pkg_info_msg = output_proto.package_deps.add() |
Navil Perez | 2c238b5 | 2021-03-17 16:33:12 +0000 | [diff] [blame] | 126 | controller_util.serialize_package_info(package, pkg_info_msg) |
Navil Perez | a782326 | 2020-08-07 17:56:57 +0000 | [diff] [blame] | 127 | |
| 128 | |
Chris McDonald | 360c2da | 2019-12-18 13:25:16 -0700 | [diff] [blame] | 129 | def _DummyGetToolchainPathsResponse(_input_proto, output_proto, _config): |
| 130 | """Create a fake successful response for GetToolchainPaths.""" |
| 131 | dummy_entry = output_proto.paths.add() |
| 132 | dummy_entry.path = 'src/third_party/dummy-package' |
| 133 | |
| 134 | |
| 135 | @faux.success(_DummyGetToolchainPathsResponse) |
| 136 | @faux.empty_error |
| 137 | @validate.validation_complete |
| 138 | def GetToolchainPaths(_input_proto, output_proto, _config): |
| 139 | """Get a list of paths that affect the toolchain.""" |
| 140 | toolchain_paths = dependency.DetermineToolchainSourcePaths() |
| 141 | for p in toolchain_paths: |
| 142 | source_path = output_proto.paths.add() |
| 143 | source_path.path = p |