blob: 0605085d7f49528f4483eb761819d7d624696ac9 [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
Mike Frysingeref94e4c2020-02-10 23:59:54 -050014import sys
15
Navil Pereza7823262020-08-07 17:56:57 +000016from chromite.api import api_config
Alex Klein076841b2019-08-29 15:19:39 -060017from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060018from chromite.api import validate
LaMont Jones00b9cff2019-12-12 12:14:07 -070019from chromite.api.controller import controller_util
Navil Pereza7823262020-08-07 17:56:57 +000020from chromite.api.gen.chromite.api import depgraph_pb2
LaMont Jones4cbecba2020-05-12 11:54:27 -060021# TODO(crbug/1081828): stop using build_target and drop it from the proto.
22from chromite.lib import cros_build_lib
Alex Klein18a60af2020-06-11 12:08:47 -060023from chromite.lib.parser import package_info
Alex Kleinb7cdbe62019-02-22 11:41:32 -070024from chromite.service import dependency
Ned Nguyen9a7a9052019-02-05 11:04:03 -070025
Ned Nguyene5d04852019-02-19 16:33:15 -070026
Mike Frysingeref94e4c2020-02-10 23:59:54 -050027assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
28
29
Ned Nguyen8e144bd2019-05-02 09:09:41 -060030def AugmentDepGraphProtoFromJsonMap(json_map, graph):
31 """Augment package deps from |json_map| to graph object.
Ned Nguyene5d04852019-02-19 16:33:15 -070032
33 Args:
34 json_map: the json object that stores the portage package. This is
Alex Kleinb7cdbe62019-02-22 11:41:32 -070035 generated from chromite.lib.service.dependency.GetBuildDependency()
Ned Nguyen8e144bd2019-05-02 09:09:41 -060036 graph: the proto object that represents the dependency graph (see DepGraph
37 message in chromite/api/depgraph.proto)
Ned Nguyene5d04852019-02-19 16:33:15 -070038 """
LaMont Jones4cbecba2020-05-12 11:54:27 -060039 graph.sysroot.build_target.name = json_map['target_board']
40 graph.sysroot.path = json_map['sysroot_path']
41 # TODO(crbug/1081828): Drop this when no longer used.
Ned Nguyene5d04852019-02-19 16:33:15 -070042 graph.build_target.name = json_map['target_board']
43
Mike Frysinger0bdbc102019-06-13 15:27:29 -040044 for data in json_map['package_deps'].values():
Ned Nguyene5d04852019-02-19 16:33:15 -070045 package_dep_info = graph.package_deps.add()
Alex Klein18a60af2020-06-11 12:08:47 -060046 package_info_msg = package_dep_info.package_info
47 package_info_msg.package_name = data['name']
48 package_info_msg.category = data['category']
49 package_info_msg.version = data['version']
Ned Nguyene5d04852019-02-19 16:33:15 -070050 for dep in data['deps']:
Alex Klein18a60af2020-06-11 12:08:47 -060051 cpv = package_info.SplitCPV(dep, strict=False)
Ned Nguyene5d04852019-02-19 16:33:15 -070052 dep_package = package_dep_info.dependency_packages.add()
53 dep_package.package_name = cpv.package
54 dep_package.category = cpv.category
55 if cpv.version:
56 dep_package.version = cpv.version
57
Alex Klein18a60af2020-06-11 12:08:47 -060058 package_CPV = '%s/%s-%s' % (package_info_msg.category,
59 package_info_msg.package_name,
60 package_info_msg.version)
Ned Nguyen8be10052019-02-26 08:58:45 -070061 for path in json_map['source_path_mapping'][package_CPV]:
Ned Nguyene5d04852019-02-19 16:33:15 -070062 source_path = package_dep_info.dependency_source_paths.add()
63 source_path.path = path
64
Ned Nguyenb5d38722019-05-06 10:24:06 -060065
Michael Mortensenaf10cca2019-11-18 20:21:32 -070066def _GetBuildDependencyGraphResponse(_input_proto, output_proto, _config):
67 """Add fake dep_graph data to a successful response."""
68 output_proto.dep_graph.build_target.name = 'target_board'
69
70
71@faux.success(_GetBuildDependencyGraphResponse)
72@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -060073@validate.validation_complete
74def GetBuildDependencyGraph(input_proto, output_proto, _config):
Ned Nguyen9a7a9052019-02-05 11:04:03 -070075 """Create the build dependency graph.
76
77 Args:
78 input_proto (GetBuildDependencyGraphRequest): The input arguments message.
79 output_proto (GetBuildDependencyGraphResponse): The empty output message.
Alex Klein231d2da2019-07-22 16:44:45 -060080 _config (api_config.ApiConfig): The API call config.
Ned Nguyen9a7a9052019-02-05 11:04:03 -070081 """
LaMont Jones4cbecba2020-05-12 11:54:27 -060082 if input_proto.HasField('sysroot'):
83 board = input_proto.sysroot.build_target.name
84 sysroot_path = input_proto.sysroot.path
85 else:
86 # TODO(crbug/1081828): stop using build_target and drop it from the proto.
87 board = input_proto.build_target.name
88 sysroot_path = cros_build_lib.GetSysroot(board or None)
89
Alex Kleind8cd4c62020-09-14 13:37:47 -060090 packages = tuple(
91 controller_util.PackageInfoToCPV(x) for x in input_proto.packages)
Ned Nguyen9a7a9052019-02-05 11:04:03 -070092
LaMont Jones4cbecba2020-05-12 11:54:27 -060093 json_map, sdk_json_map = dependency.GetBuildDependency(sysroot_path, board,
94 packages)
Ned Nguyen8e144bd2019-05-02 09:09:41 -060095 AugmentDepGraphProtoFromJsonMap(json_map, output_proto.dep_graph)
Chris McDonalda22b74f2019-11-22 13:55:06 -070096 AugmentDepGraphProtoFromJsonMap(sdk_json_map, output_proto.sdk_dep_graph)
Chris McDonald360c2da2019-12-18 13:25:16 -070097
98
Navil Pereza7823262020-08-07 17:56:57 +000099def _ListResponse(_input_proto, output_proto, _config):
100 """Add fake dependency data to a successful response."""
101 package_dep = output_proto.package_deps.add()
102 package_dep.category = 'category'
103 package_dep.package_name = 'name'
104
105
106@faux.success(_ListResponse)
107@faux.empty_error
108@validate.require('sysroot.build_target.name')
109@validate.exists('sysroot.path')
110@validate.validation_complete
111def List(input_proto: depgraph_pb2.ListRequest,
112 output_proto: depgraph_pb2.ListResponse,
113 _config: api_config.ApiConfig):
114 """Get a list of package dependencies.
115
116 Args:
117 input_proto: The input arguments message.
118 output_proto: The empty output message.
119 _config: The API call config.
120 """
121 build_target = controller_util.ParseBuildTarget(
122 input_proto.sysroot.build_target)
123 sysroot_path = input_proto.sysroot.path
124 src_paths = [src_path.path for src_path in input_proto.src_paths]
125 packages = [controller_util.PackageInfoToCPV(x) for x in input_proto.packages]
126
127 package_deps = dependency.GetDependencies(
128 sysroot_path,
129 build_target=build_target,
130 src_paths=src_paths,
131 packages=packages)
132 for package in package_deps:
Alex Klein18a60af2020-06-11 12:08:47 -0600133 pkg_info = output_proto.package_deps.add()
134 cpv = package_info.SplitCPV(package, strict=False)
135 controller_util.CPVToPackageInfo(cpv, pkg_info)
Navil Pereza7823262020-08-07 17:56:57 +0000136
137
Chris McDonald360c2da2019-12-18 13:25:16 -0700138def _DummyGetToolchainPathsResponse(_input_proto, output_proto, _config):
139 """Create a fake successful response for GetToolchainPaths."""
140 dummy_entry = output_proto.paths.add()
141 dummy_entry.path = 'src/third_party/dummy-package'
142
143
144@faux.success(_DummyGetToolchainPathsResponse)
145@faux.empty_error
146@validate.validation_complete
147def GetToolchainPaths(_input_proto, output_proto, _config):
148 """Get a list of paths that affect the toolchain."""
149 toolchain_paths = dependency.DetermineToolchainSourcePaths()
150 for p in toolchain_paths:
151 source_path = output_proto.paths.add()
152 source_path.path = p