blob: dc723748453fb9818d79414a72de7dcfcc7d8b98 [file] [log] [blame]
Ned Nguyen9a7a9052019-02-05 11:04:03 -07001# -*- 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
8This service handles the creation of the portage build dependency graphs and the
9graphs mapping from portage packages to the dependency source.
10"""
11
12from __future__ import print_function
13
Navil Pereza7823262020-08-07 17:56:57 +000014from chromite.api import api_config
Alex Klein076841b2019-08-29 15:19:39 -060015from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060016from chromite.api import validate
LaMont Jones00b9cff2019-12-12 12:14:07 -070017from chromite.api.controller import controller_util
Navil Pereza7823262020-08-07 17:56:57 +000018from chromite.api.gen.chromite.api import depgraph_pb2
LaMont Jones4cbecba2020-05-12 11:54:27 -060019# TODO(crbug/1081828): stop using build_target and drop it from the proto.
20from chromite.lib import cros_build_lib
Alex Klein18a60af2020-06-11 12:08:47 -060021from chromite.lib.parser import package_info
Alex Kleinb7cdbe62019-02-22 11:41:32 -070022from chromite.service import dependency
Ned Nguyen9a7a9052019-02-05 11:04:03 -070023
Ned Nguyene5d04852019-02-19 16:33:15 -070024
Ned Nguyen8e144bd2019-05-02 09:09:41 -060025def AugmentDepGraphProtoFromJsonMap(json_map, graph):
26 """Augment package deps from |json_map| to graph object.
Ned Nguyene5d04852019-02-19 16:33:15 -070027
28 Args:
29 json_map: the json object that stores the portage package. This is
Alex Kleinb7cdbe62019-02-22 11:41:32 -070030 generated from chromite.lib.service.dependency.GetBuildDependency()
Ned Nguyen8e144bd2019-05-02 09:09:41 -060031 graph: the proto object that represents the dependency graph (see DepGraph
32 message in chromite/api/depgraph.proto)
Ned Nguyene5d04852019-02-19 16:33:15 -070033 """
LaMont Jones4cbecba2020-05-12 11:54:27 -060034 graph.sysroot.build_target.name = json_map['target_board']
35 graph.sysroot.path = json_map['sysroot_path']
36 # TODO(crbug/1081828): Drop this when no longer used.
Ned Nguyene5d04852019-02-19 16:33:15 -070037 graph.build_target.name = json_map['target_board']
38
Mike Frysinger0bdbc102019-06-13 15:27:29 -040039 for data in json_map['package_deps'].values():
Ned Nguyene5d04852019-02-19 16:33:15 -070040 package_dep_info = graph.package_deps.add()
Alex Klein18a60af2020-06-11 12:08:47 -060041 package_info_msg = package_dep_info.package_info
42 package_info_msg.package_name = data['name']
43 package_info_msg.category = data['category']
44 package_info_msg.version = data['version']
Ned Nguyene5d04852019-02-19 16:33:15 -070045 for dep in data['deps']:
Alex Klein5c4625a2020-10-06 17:40:22 -060046 cpv = package_info.parse(dep)
Ned Nguyene5d04852019-02-19 16:33:15 -070047 dep_package = package_dep_info.dependency_packages.add()
Alex Klein5c4625a2020-10-06 17:40:22 -060048 controller_util.serialize_package_info(cpv, dep_package)
Ned Nguyene5d04852019-02-19 16:33:15 -070049
Alex Klein5c4625a2020-10-06 17:40:22 -060050 package_CPV = controller_util.PackageInfoToString(package_info_msg)
Ned Nguyen8be10052019-02-26 08:58:45 -070051 for path in json_map['source_path_mapping'][package_CPV]:
Ned Nguyene5d04852019-02-19 16:33:15 -070052 source_path = package_dep_info.dependency_source_paths.add()
53 source_path.path = path
54
Ned Nguyenb5d38722019-05-06 10:24:06 -060055
Michael Mortensenaf10cca2019-11-18 20:21:32 -070056def _GetBuildDependencyGraphResponse(_input_proto, output_proto, _config):
57 """Add fake dep_graph data to a successful response."""
58 output_proto.dep_graph.build_target.name = 'target_board'
59
60
61@faux.success(_GetBuildDependencyGraphResponse)
62@faux.empty_error
Alex Klein45b73432020-09-23 13:51:20 -060063@validate.require_each('packages', ['category', 'package_name'])
Alex Klein231d2da2019-07-22 16:44:45 -060064@validate.validation_complete
65def GetBuildDependencyGraph(input_proto, output_proto, _config):
Ned Nguyen9a7a9052019-02-05 11:04:03 -070066 """Create the build dependency graph.
67
68 Args:
69 input_proto (GetBuildDependencyGraphRequest): The input arguments message.
70 output_proto (GetBuildDependencyGraphResponse): The empty output message.
Alex Klein231d2da2019-07-22 16:44:45 -060071 _config (api_config.ApiConfig): 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
79 sysroot_path = cros_build_lib.GetSysroot(board or None)
80
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
122 ])
Navil Pereza7823262020-08-07 17:56:57 +0000123 for package in package_deps:
Alex Klein5c4625a2020-10-06 17:40:22 -0600124 pkg_info_msg = output_proto.package_deps.add()
Navil Perez2c238b52021-03-17 16:33:12 +0000125 controller_util.serialize_package_info(package, pkg_info_msg)
Navil Pereza7823262020-08-07 17:56:57 +0000126
127
Chris McDonald360c2da2019-12-18 13:25:16 -0700128def _DummyGetToolchainPathsResponse(_input_proto, output_proto, _config):
129 """Create a fake successful response for GetToolchainPaths."""
130 dummy_entry = output_proto.paths.add()
131 dummy_entry.path = 'src/third_party/dummy-package'
132
133
134@faux.success(_DummyGetToolchainPathsResponse)
135@faux.empty_error
136@validate.validation_complete
137def GetToolchainPaths(_input_proto, output_proto, _config):
138 """Get a list of paths that affect the toolchain."""
139 toolchain_paths = dependency.DetermineToolchainSourcePaths()
140 for p in toolchain_paths:
141 source_path = output_proto.paths.add()
142 source_path.path = p