blob: 51a731b405a7bfe50a279d22e032d47dee0dbfd1 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Ned Nguyen9a7a9052019-02-05 11:04:03 -07002# 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
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040016
LaMont Jones4cbecba2020-05-12 11:54:27 -060017# TODO(crbug/1081828): stop using build_target and drop it from the proto.
Mike Frysinger06a51c82021-04-06 11:39:17 -040018from chromite.lib import build_target_lib
Alex Klein18a60af2020-06-11 12:08:47 -060019from chromite.lib.parser import package_info
Alex Kleinb7cdbe62019-02-22 11:41:32 -070020from chromite.service import dependency
Ned Nguyen9a7a9052019-02-05 11:04:03 -070021
Ned Nguyene5d04852019-02-19 16:33:15 -070022
Ned Nguyen8e144bd2019-05-02 09:09:41 -060023def AugmentDepGraphProtoFromJsonMap(json_map, graph):
Alex Klein1699fab2022-09-08 08:46:06 -060024 """Augment package deps from |json_map| to graph object.
Ned Nguyene5d04852019-02-19 16:33:15 -070025
Alex Klein1699fab2022-09-08 08:46:06 -060026 Args:
Alex Klein611dddd2022-10-11 17:02:01 -060027 json_map: the json object that stores the portage package. This is
28 generated from chromite.lib.service.dependency.GetBuildDependency()
29 graph: the proto object that represents the dependency graph (see DepGraph
30 message in chromite/api/depgraph.proto)
Alex Klein1699fab2022-09-08 08:46:06 -060031 """
32 graph.sysroot.build_target.name = json_map["target_board"]
33 graph.sysroot.path = json_map["sysroot_path"]
34 # TODO(crbug/1081828): Drop this when no longer used.
35 graph.build_target.name = json_map["target_board"]
Ned Nguyene5d04852019-02-19 16:33:15 -070036
Alex Klein1699fab2022-09-08 08:46:06 -060037 for data in json_map["package_deps"].values():
38 package_dep_info = graph.package_deps.add()
39 package_info_msg = package_dep_info.package_info
40 package_info_msg.package_name = data["name"]
41 package_info_msg.category = data["category"]
42 package_info_msg.version = data["version"]
43 for dep in data["deps"]:
44 cpv = package_info.parse(dep)
45 dep_package = package_dep_info.dependency_packages.add()
46 controller_util.serialize_package_info(cpv, dep_package)
Ned Nguyene5d04852019-02-19 16:33:15 -070047
Alex Klein1699fab2022-09-08 08:46:06 -060048 package_CPV = controller_util.PackageInfoToString(package_info_msg)
49 for path in json_map["source_path_mapping"][package_CPV]:
50 source_path = package_dep_info.dependency_source_paths.add()
51 source_path.path = path
Ned Nguyene5d04852019-02-19 16:33:15 -070052
Ned Nguyenb5d38722019-05-06 10:24:06 -060053
Michael Mortensenaf10cca2019-11-18 20:21:32 -070054def _GetBuildDependencyGraphResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060055 """Add fake dep_graph data to a successful response."""
56 output_proto.dep_graph.build_target.name = "target_board"
Michael Mortensenaf10cca2019-11-18 20:21:32 -070057
58
59@faux.success(_GetBuildDependencyGraphResponse)
60@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060061@validate.require_each("packages", ["category", "package_name"])
Alex Klein231d2da2019-07-22 16:44:45 -060062@validate.validation_complete
Eric Linaafedbf2021-08-19 19:43:47 +080063def GetBuildDependencyGraph(
64 input_proto: depgraph_pb2.GetBuildDependencyGraphRequest,
65 output_proto: depgraph_pb2.GetBuildDependencyGraphResponse,
Alex Klein1699fab2022-09-08 08:46:06 -060066 _config: api_config.ApiConfig,
67) -> None:
68 """Create the build dependency graph.
Ned Nguyen9a7a9052019-02-05 11:04:03 -070069
Alex Klein1699fab2022-09-08 08:46:06 -060070 Args:
Alex Klein611dddd2022-10-11 17:02:01 -060071 input_proto: The input arguments message.
72 output_proto: The empty output message.
73 _config: The API call config.
Alex Klein1699fab2022-09-08 08:46:06 -060074 """
75 if input_proto.HasField("sysroot"):
76 board = input_proto.sysroot.build_target.name
77 sysroot_path = input_proto.sysroot.path
78 else:
79 # TODO(crbug/1081828): stop using build_target and drop it from the proto.
80 board = input_proto.build_target.name
81 sysroot_path = build_target_lib.get_default_sysroot_path(board or None)
LaMont Jones4cbecba2020-05-12 11:54:27 -060082
Alex Klein1699fab2022-09-08 08:46:06 -060083 packages = tuple(
84 controller_util.PackageInfoToCPV(x) for x in input_proto.packages
85 )
Ned Nguyen9a7a9052019-02-05 11:04:03 -070086
Alex Klein1699fab2022-09-08 08:46:06 -060087 json_map, sdk_json_map = dependency.GetBuildDependency(
88 sysroot_path, board, packages
89 )
90 AugmentDepGraphProtoFromJsonMap(json_map, output_proto.dep_graph)
91 AugmentDepGraphProtoFromJsonMap(sdk_json_map, output_proto.sdk_dep_graph)
Chris McDonald360c2da2019-12-18 13:25:16 -070092
93
Navil Pereza7823262020-08-07 17:56:57 +000094def _ListResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060095 """Add fake dependency data to a successful response."""
96 package_dep = output_proto.package_deps.add()
97 package_dep.category = "category"
98 package_dep.package_name = "name"
Navil Pereza7823262020-08-07 17:56:57 +000099
100
101@faux.success(_ListResponse)
102@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600103@validate.require("sysroot.build_target.name")
104@validate.exists("sysroot.path")
105@validate.require_each("src_paths", ["path"])
106@validate.require_each("packages", ["category", "package_name"])
Navil Pereza7823262020-08-07 17:56:57 +0000107@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600108def List(
109 input_proto: depgraph_pb2.ListRequest,
110 output_proto: depgraph_pb2.ListResponse,
111 _config: api_config.ApiConfig,
112):
113 """Get a list of package dependencies.
Navil Pereza7823262020-08-07 17:56:57 +0000114
Alex Klein1699fab2022-09-08 08:46:06 -0600115 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600116 input_proto: The input arguments message.
117 output_proto: The empty output message.
118 _config: The API call config.
Alex Klein1699fab2022-09-08 08:46:06 -0600119 """
120 sysroot_path = input_proto.sysroot.path
121 src_paths = [src_path.path for src_path in input_proto.src_paths]
122 package_deps = dependency.GetDependencies(
123 sysroot_path,
124 src_paths=src_paths,
125 packages=[
126 controller_util.deserialize_package_info(package)
127 for package in input_proto.packages
128 ],
129 include_rev_dependencies=input_proto.include_rev_deps,
130 )
131 for package in package_deps:
132 pkg_info_msg = output_proto.package_deps.add()
133 controller_util.serialize_package_info(package, pkg_info_msg)
Navil Pereza7823262020-08-07 17:56:57 +0000134
135
Mike Frysingera5c6e792022-03-15 23:42:12 -0400136def _StubGetToolchainPathsResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600137 """Create a fake successful response for GetToolchainPaths."""
138 stub_entry = output_proto.paths.add()
139 stub_entry.path = "src/third_party/stub-package"
Chris McDonald360c2da2019-12-18 13:25:16 -0700140
141
Mike Frysingera5c6e792022-03-15 23:42:12 -0400142@faux.success(_StubGetToolchainPathsResponse)
Chris McDonald360c2da2019-12-18 13:25:16 -0700143@faux.empty_error
144@validate.validation_complete
145def GetToolchainPaths(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600146 """Get a list of paths that affect the toolchain."""
147 toolchain_paths = dependency.DetermineToolchainSourcePaths()
148 for p in toolchain_paths:
149 source_path = output_proto.paths.add()
150 source_path.path = p