blob: 13eece557aaa12676b221282b5861bb803fbad5b [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 Klein18a60af2020-06-11 12:08:47 -060046 cpv = package_info.SplitCPV(dep, strict=False)
Ned Nguyene5d04852019-02-19 16:33:15 -070047 dep_package = package_dep_info.dependency_packages.add()
48 dep_package.package_name = cpv.package
49 dep_package.category = cpv.category
50 if cpv.version:
51 dep_package.version = cpv.version
52
Alex Klein18a60af2020-06-11 12:08:47 -060053 package_CPV = '%s/%s-%s' % (package_info_msg.category,
54 package_info_msg.package_name,
55 package_info_msg.version)
Ned Nguyen8be10052019-02-26 08:58:45 -070056 for path in json_map['source_path_mapping'][package_CPV]:
Ned Nguyene5d04852019-02-19 16:33:15 -070057 source_path = package_dep_info.dependency_source_paths.add()
58 source_path.path = path
59
Ned Nguyenb5d38722019-05-06 10:24:06 -060060
Michael Mortensenaf10cca2019-11-18 20:21:32 -070061def _GetBuildDependencyGraphResponse(_input_proto, output_proto, _config):
62 """Add fake dep_graph data to a successful response."""
63 output_proto.dep_graph.build_target.name = 'target_board'
64
65
66@faux.success(_GetBuildDependencyGraphResponse)
67@faux.empty_error
Alex Klein45b73432020-09-23 13:51:20 -060068@validate.require_each('packages', ['category', 'package_name'])
Alex Klein231d2da2019-07-22 16:44:45 -060069@validate.validation_complete
70def GetBuildDependencyGraph(input_proto, output_proto, _config):
Ned Nguyen9a7a9052019-02-05 11:04:03 -070071 """Create the build dependency graph.
72
73 Args:
74 input_proto (GetBuildDependencyGraphRequest): The input arguments message.
75 output_proto (GetBuildDependencyGraphResponse): The empty output message.
Alex Klein231d2da2019-07-22 16:44:45 -060076 _config (api_config.ApiConfig): The API call config.
Ned Nguyen9a7a9052019-02-05 11:04:03 -070077 """
LaMont Jones4cbecba2020-05-12 11:54:27 -060078 if input_proto.HasField('sysroot'):
79 board = input_proto.sysroot.build_target.name
80 sysroot_path = input_proto.sysroot.path
81 else:
82 # TODO(crbug/1081828): stop using build_target and drop it from the proto.
83 board = input_proto.build_target.name
84 sysroot_path = cros_build_lib.GetSysroot(board or None)
85
Alex Kleind8cd4c62020-09-14 13:37:47 -060086 packages = tuple(
87 controller_util.PackageInfoToCPV(x) for x in input_proto.packages)
Ned Nguyen9a7a9052019-02-05 11:04:03 -070088
LaMont Jones4cbecba2020-05-12 11:54:27 -060089 json_map, sdk_json_map = dependency.GetBuildDependency(sysroot_path, board,
90 packages)
Ned Nguyen8e144bd2019-05-02 09:09:41 -060091 AugmentDepGraphProtoFromJsonMap(json_map, output_proto.dep_graph)
Chris McDonalda22b74f2019-11-22 13:55:06 -070092 AugmentDepGraphProtoFromJsonMap(sdk_json_map, output_proto.sdk_dep_graph)
Chris McDonald360c2da2019-12-18 13:25:16 -070093
94
Navil Pereza7823262020-08-07 17:56:57 +000095def _ListResponse(_input_proto, output_proto, _config):
96 """Add fake dependency data to a successful response."""
97 package_dep = output_proto.package_deps.add()
98 package_dep.category = 'category'
99 package_dep.package_name = 'name'
100
101
102@faux.success(_ListResponse)
103@faux.empty_error
104@validate.require('sysroot.build_target.name')
105@validate.exists('sysroot.path')
Alex Klein45b73432020-09-23 13:51:20 -0600106@validate.require_each('src_paths', ['path'])
107@validate.require_each('packages', ['category', 'package_name'])
Navil Pereza7823262020-08-07 17:56:57 +0000108@validate.validation_complete
109def List(input_proto: depgraph_pb2.ListRequest,
110 output_proto: depgraph_pb2.ListResponse,
111 _config: api_config.ApiConfig):
112 """Get a list of package dependencies.
113
114 Args:
115 input_proto: The input arguments message.
116 output_proto: The empty output message.
117 _config: The API call config.
118 """
119 build_target = controller_util.ParseBuildTarget(
120 input_proto.sysroot.build_target)
121 sysroot_path = input_proto.sysroot.path
122 src_paths = [src_path.path for src_path in input_proto.src_paths]
123 packages = [controller_util.PackageInfoToCPV(x) for x in input_proto.packages]
124
125 package_deps = dependency.GetDependencies(
126 sysroot_path,
127 build_target=build_target,
128 src_paths=src_paths,
129 packages=packages)
130 for package in package_deps:
Alex Klein18a60af2020-06-11 12:08:47 -0600131 pkg_info = output_proto.package_deps.add()
132 cpv = package_info.SplitCPV(package, strict=False)
133 controller_util.CPVToPackageInfo(cpv, pkg_info)
Navil Pereza7823262020-08-07 17:56:57 +0000134
135
Chris McDonald360c2da2019-12-18 13:25:16 -0700136def _DummyGetToolchainPathsResponse(_input_proto, output_proto, _config):
137 """Create a fake successful response for GetToolchainPaths."""
138 dummy_entry = output_proto.paths.add()
139 dummy_entry.path = 'src/third_party/dummy-package'
140
141
142@faux.success(_DummyGetToolchainPathsResponse)
143@faux.empty_error
144@validate.validation_complete
145def GetToolchainPaths(_input_proto, output_proto, _config):
146 """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