blob: 659139c0ce4347b8dab652f36717505feb58efe6 [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
62def GetBuildDependencyGraph(input_proto, output_proto, _config):
Ned Nguyen9a7a9052019-02-05 11:04:03 -070063 """Create the build dependency graph.
64
65 Args:
66 input_proto (GetBuildDependencyGraphRequest): The input arguments message.
67 output_proto (GetBuildDependencyGraphResponse): The empty output message.
Alex Klein231d2da2019-07-22 16:44:45 -060068 _config (api_config.ApiConfig): The API call config.
Ned Nguyen9a7a9052019-02-05 11:04:03 -070069 """
LaMont Jones4cbecba2020-05-12 11:54:27 -060070 if input_proto.HasField('sysroot'):
71 board = input_proto.sysroot.build_target.name
72 sysroot_path = input_proto.sysroot.path
73 else:
74 # TODO(crbug/1081828): stop using build_target and drop it from the proto.
75 board = input_proto.build_target.name
Mike Frysinger06a51c82021-04-06 11:39:17 -040076 sysroot_path = build_target_lib.get_default_sysroot_path(board or None)
LaMont Jones4cbecba2020-05-12 11:54:27 -060077
Alex Kleind8cd4c62020-09-14 13:37:47 -060078 packages = tuple(
79 controller_util.PackageInfoToCPV(x) for x in input_proto.packages)
Ned Nguyen9a7a9052019-02-05 11:04:03 -070080
LaMont Jones4cbecba2020-05-12 11:54:27 -060081 json_map, sdk_json_map = dependency.GetBuildDependency(sysroot_path, board,
82 packages)
Ned Nguyen8e144bd2019-05-02 09:09:41 -060083 AugmentDepGraphProtoFromJsonMap(json_map, output_proto.dep_graph)
Chris McDonalda22b74f2019-11-22 13:55:06 -070084 AugmentDepGraphProtoFromJsonMap(sdk_json_map, output_proto.sdk_dep_graph)
Chris McDonald360c2da2019-12-18 13:25:16 -070085
86
Navil Pereza7823262020-08-07 17:56:57 +000087def _ListResponse(_input_proto, output_proto, _config):
88 """Add fake dependency data to a successful response."""
89 package_dep = output_proto.package_deps.add()
90 package_dep.category = 'category'
91 package_dep.package_name = 'name'
92
93
94@faux.success(_ListResponse)
95@faux.empty_error
96@validate.require('sysroot.build_target.name')
97@validate.exists('sysroot.path')
Alex Klein45b73432020-09-23 13:51:20 -060098@validate.require_each('src_paths', ['path'])
99@validate.require_each('packages', ['category', 'package_name'])
Navil Pereza7823262020-08-07 17:56:57 +0000100@validate.validation_complete
101def List(input_proto: depgraph_pb2.ListRequest,
102 output_proto: depgraph_pb2.ListResponse,
103 _config: api_config.ApiConfig):
104 """Get a list of package dependencies.
105
106 Args:
107 input_proto: The input arguments message.
108 output_proto: The empty output message.
109 _config: The API call config.
110 """
Navil Pereza7823262020-08-07 17:56:57 +0000111 sysroot_path = input_proto.sysroot.path
112 src_paths = [src_path.path for src_path in input_proto.src_paths]
Navil Pereza7823262020-08-07 17:56:57 +0000113 package_deps = dependency.GetDependencies(
114 sysroot_path,
Navil Pereza7823262020-08-07 17:56:57 +0000115 src_paths=src_paths,
Navil Perez2c238b52021-03-17 16:33:12 +0000116 packages=[
117 controller_util.deserialize_package_info(package)
118 for package in input_proto.packages
Navil Perez3fa1ca82021-03-19 13:53:20 +0000119 ],
Navil Perez2c108432021-04-09 04:17:30 +0000120 include_rev_dependencies=input_proto.include_rev_deps)
Navil Pereza7823262020-08-07 17:56:57 +0000121 for package in package_deps:
Alex Klein5c4625a2020-10-06 17:40:22 -0600122 pkg_info_msg = output_proto.package_deps.add()
Navil Perez2c238b52021-03-17 16:33:12 +0000123 controller_util.serialize_package_info(package, pkg_info_msg)
Navil Pereza7823262020-08-07 17:56:57 +0000124
125
Chris McDonald360c2da2019-12-18 13:25:16 -0700126def _DummyGetToolchainPathsResponse(_input_proto, output_proto, _config):
127 """Create a fake successful response for GetToolchainPaths."""
128 dummy_entry = output_proto.paths.add()
129 dummy_entry.path = 'src/third_party/dummy-package'
130
131
132@faux.success(_DummyGetToolchainPathsResponse)
133@faux.empty_error
134@validate.validation_complete
135def GetToolchainPaths(_input_proto, output_proto, _config):
136 """Get a list of paths that affect the toolchain."""
137 toolchain_paths = dependency.DetermineToolchainSourcePaths()
138 for p in toolchain_paths:
139 source_path = output_proto.paths.add()
140 source_path.path = p