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