blob: 1c7bfa695286d5f4df79098091608d36bb259219 [file] [log] [blame]
Ned Nguyen9a7a9052019-02-05 11:04:03 -07001# 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
7This service handles the creation of the portage build dependency graphs and the
8graphs mapping from portage packages to the dependency source.
9"""
10
Navil Pereza7823262020-08-07 17:56:57 +000011from chromite.api import api_config
Alex Klein076841b2019-08-29 15:19:39 -060012from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060013from chromite.api import validate
LaMont Jones00b9cff2019-12-12 12:14:07 -070014from chromite.api.controller import controller_util
Navil Pereza7823262020-08-07 17:56:57 +000015from chromite.api.gen.chromite.api import depgraph_pb2
LaMont Jones4cbecba2020-05-12 11:54:27 -060016# TODO(crbug/1081828): stop using build_target and drop it from the proto.
Mike Frysinger06a51c82021-04-06 11:39:17 -040017from chromite.lib import build_target_lib
Alex Klein18a60af2020-06-11 12:08:47 -060018from chromite.lib.parser import package_info
Alex Kleinb7cdbe62019-02-22 11:41:32 -070019from chromite.service import dependency
Ned Nguyen9a7a9052019-02-05 11:04:03 -070020
Ned Nguyene5d04852019-02-19 16:33:15 -070021
Ned Nguyen8e144bd2019-05-02 09:09:41 -060022def AugmentDepGraphProtoFromJsonMap(json_map, graph):
23 """Augment package deps from |json_map| to graph object.
Ned Nguyene5d04852019-02-19 16:33:15 -070024
25 Args:
26 json_map: the json object that stores the portage package. This is
Alex Kleinb7cdbe62019-02-22 11:41:32 -070027 generated from chromite.lib.service.dependency.GetBuildDependency()
Ned Nguyen8e144bd2019-05-02 09:09:41 -060028 graph: the proto object that represents the dependency graph (see DepGraph
29 message in chromite/api/depgraph.proto)
Ned Nguyene5d04852019-02-19 16:33:15 -070030 """
LaMont Jones4cbecba2020-05-12 11:54:27 -060031 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 Nguyene5d04852019-02-19 16:33:15 -070034 graph.build_target.name = json_map['target_board']
35
Mike Frysinger0bdbc102019-06-13 15:27:29 -040036 for data in json_map['package_deps'].values():
Ned Nguyene5d04852019-02-19 16:33:15 -070037 package_dep_info = graph.package_deps.add()
Alex Klein18a60af2020-06-11 12:08:47 -060038 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 Nguyene5d04852019-02-19 16:33:15 -070042 for dep in data['deps']:
Alex Klein5c4625a2020-10-06 17:40:22 -060043 cpv = package_info.parse(dep)
Ned Nguyene5d04852019-02-19 16:33:15 -070044 dep_package = package_dep_info.dependency_packages.add()
Alex Klein5c4625a2020-10-06 17:40:22 -060045 controller_util.serialize_package_info(cpv, dep_package)
Ned Nguyene5d04852019-02-19 16:33:15 -070046
Alex Klein5c4625a2020-10-06 17:40:22 -060047 package_CPV = controller_util.PackageInfoToString(package_info_msg)
Ned Nguyen8be10052019-02-26 08:58:45 -070048 for path in json_map['source_path_mapping'][package_CPV]:
Ned Nguyene5d04852019-02-19 16:33:15 -070049 source_path = package_dep_info.dependency_source_paths.add()
50 source_path.path = path
51
Ned Nguyenb5d38722019-05-06 10:24:06 -060052
Michael Mortensenaf10cca2019-11-18 20:21:32 -070053def _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 Klein45b73432020-09-23 13:51:20 -060060@validate.require_each('packages', ['category', 'package_name'])
Alex Klein231d2da2019-07-22 16:44:45 -060061@validate.validation_complete
Eric Linaafedbf2021-08-19 19:43:47 +080062def GetBuildDependencyGraph(
63 input_proto: depgraph_pb2.GetBuildDependencyGraphRequest,
64 output_proto: depgraph_pb2.GetBuildDependencyGraphResponse,
65 _config: api_config.ApiConfig) -> None:
Ned Nguyen9a7a9052019-02-05 11:04:03 -070066 """Create the build dependency graph.
67
68 Args:
Eric Linaafedbf2021-08-19 19:43:47 +080069 input_proto: The input arguments message.
70 output_proto: The empty output message.
71 _config: The API call config.
Ned Nguyen9a7a9052019-02-05 11:04:03 -070072 """
LaMont Jones4cbecba2020-05-12 11:54:27 -060073 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 Frysinger06a51c82021-04-06 11:39:17 -040079 sysroot_path = build_target_lib.get_default_sysroot_path(board or None)
LaMont Jones4cbecba2020-05-12 11:54:27 -060080
Alex Kleind8cd4c62020-09-14 13:37:47 -060081 packages = tuple(
82 controller_util.PackageInfoToCPV(x) for x in input_proto.packages)
Ned Nguyen9a7a9052019-02-05 11:04:03 -070083
LaMont Jones4cbecba2020-05-12 11:54:27 -060084 json_map, sdk_json_map = dependency.GetBuildDependency(sysroot_path, board,
85 packages)
Ned Nguyen8e144bd2019-05-02 09:09:41 -060086 AugmentDepGraphProtoFromJsonMap(json_map, output_proto.dep_graph)
Chris McDonalda22b74f2019-11-22 13:55:06 -070087 AugmentDepGraphProtoFromJsonMap(sdk_json_map, output_proto.sdk_dep_graph)
Chris McDonald360c2da2019-12-18 13:25:16 -070088
89
Navil Pereza7823262020-08-07 17:56:57 +000090def _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 Klein45b73432020-09-23 13:51:20 -0600101@validate.require_each('src_paths', ['path'])
102@validate.require_each('packages', ['category', 'package_name'])
Navil Pereza7823262020-08-07 17:56:57 +0000103@validate.validation_complete
104def 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 Pereza7823262020-08-07 17:56:57 +0000114 sysroot_path = input_proto.sysroot.path
115 src_paths = [src_path.path for src_path in input_proto.src_paths]
Navil Pereza7823262020-08-07 17:56:57 +0000116 package_deps = dependency.GetDependencies(
117 sysroot_path,
Navil Pereza7823262020-08-07 17:56:57 +0000118 src_paths=src_paths,
Navil Perez2c238b52021-03-17 16:33:12 +0000119 packages=[
120 controller_util.deserialize_package_info(package)
121 for package in input_proto.packages
Navil Perez3fa1ca82021-03-19 13:53:20 +0000122 ],
Navil Perez2c108432021-04-09 04:17:30 +0000123 include_rev_dependencies=input_proto.include_rev_deps)
Navil Pereza7823262020-08-07 17:56:57 +0000124 for package in package_deps:
Alex Klein5c4625a2020-10-06 17:40:22 -0600125 pkg_info_msg = output_proto.package_deps.add()
Navil Perez2c238b52021-03-17 16:33:12 +0000126 controller_util.serialize_package_info(package, pkg_info_msg)
Navil Pereza7823262020-08-07 17:56:57 +0000127
128
Mike Frysingera5c6e792022-03-15 23:42:12 -0400129def _StubGetToolchainPathsResponse(_input_proto, output_proto, _config):
Chris McDonald360c2da2019-12-18 13:25:16 -0700130 """Create a fake successful response for GetToolchainPaths."""
Mike Frysingera5c6e792022-03-15 23:42:12 -0400131 stub_entry = output_proto.paths.add()
132 stub_entry.path = 'src/third_party/stub-package'
Chris McDonald360c2da2019-12-18 13:25:16 -0700133
134
Mike Frysingera5c6e792022-03-15 23:42:12 -0400135@faux.success(_StubGetToolchainPathsResponse)
Chris McDonald360c2da2019-12-18 13:25:16 -0700136@faux.empty_error
137@validate.validation_complete
138def 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