blob: 338cfac6c78b4e950a0cd07c980010a4cddfbc21 [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
Ned Nguyene5d04852019-02-19 16:33:15 -070023from chromite.lib import portage_util
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()
46 package_info = package_dep_info.package_info
47 package_info.package_name = data['name']
48 package_info.category = data['category']
49 package_info.version = data['version']
50 for dep in data['deps']:
51 cpv = portage_util.SplitCPV(dep, strict=False)
52 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
Chris McDonaldb97c32e2020-01-08 14:26:58 -070058 package_CPV = '%s/%s-%s' % (package_info.category,
59 package_info.package_name, package_info.version)
Ned Nguyen8be10052019-02-26 08:58:45 -070060 for path in json_map['source_path_mapping'][package_CPV]:
Ned Nguyene5d04852019-02-19 16:33:15 -070061 source_path = package_dep_info.dependency_source_paths.add()
62 source_path.path = path
63
Ned Nguyenb5d38722019-05-06 10:24:06 -060064
Michael Mortensenaf10cca2019-11-18 20:21:32 -070065def _GetBuildDependencyGraphResponse(_input_proto, output_proto, _config):
66 """Add fake dep_graph data to a successful response."""
67 output_proto.dep_graph.build_target.name = 'target_board'
68
69
70@faux.success(_GetBuildDependencyGraphResponse)
71@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -060072@validate.validation_complete
73def GetBuildDependencyGraph(input_proto, output_proto, _config):
Ned Nguyen9a7a9052019-02-05 11:04:03 -070074 """Create the build dependency graph.
75
76 Args:
77 input_proto (GetBuildDependencyGraphRequest): The input arguments message.
78 output_proto (GetBuildDependencyGraphResponse): The empty output message.
Alex Klein231d2da2019-07-22 16:44:45 -060079 _config (api_config.ApiConfig): The API call config.
Ned Nguyen9a7a9052019-02-05 11:04:03 -070080 """
LaMont Jones4cbecba2020-05-12 11:54:27 -060081 if input_proto.HasField('sysroot'):
82 board = input_proto.sysroot.build_target.name
83 sysroot_path = input_proto.sysroot.path
84 else:
85 # TODO(crbug/1081828): stop using build_target and drop it from the proto.
86 board = input_proto.build_target.name
87 sysroot_path = cros_build_lib.GetSysroot(board or None)
88
Alex Kleind8cd4c62020-09-14 13:37:47 -060089 packages = tuple(
90 controller_util.PackageInfoToCPV(x) for x in input_proto.packages)
Ned Nguyen9a7a9052019-02-05 11:04:03 -070091
LaMont Jones4cbecba2020-05-12 11:54:27 -060092 json_map, sdk_json_map = dependency.GetBuildDependency(sysroot_path, board,
93 packages)
Ned Nguyen8e144bd2019-05-02 09:09:41 -060094 AugmentDepGraphProtoFromJsonMap(json_map, output_proto.dep_graph)
Chris McDonalda22b74f2019-11-22 13:55:06 -070095 AugmentDepGraphProtoFromJsonMap(sdk_json_map, output_proto.sdk_dep_graph)
Chris McDonald360c2da2019-12-18 13:25:16 -070096
97
Navil Pereza7823262020-08-07 17:56:57 +000098def _ListResponse(_input_proto, output_proto, _config):
99 """Add fake dependency data to a successful response."""
100 package_dep = output_proto.package_deps.add()
101 package_dep.category = 'category'
102 package_dep.package_name = 'name'
103
104
105@faux.success(_ListResponse)
106@faux.empty_error
107@validate.require('sysroot.build_target.name')
108@validate.exists('sysroot.path')
109@validate.validation_complete
110def List(input_proto: depgraph_pb2.ListRequest,
111 output_proto: depgraph_pb2.ListResponse,
112 _config: api_config.ApiConfig):
113 """Get a list of package dependencies.
114
115 Args:
116 input_proto: The input arguments message.
117 output_proto: The empty output message.
118 _config: The API call config.
119 """
120 build_target = controller_util.ParseBuildTarget(
121 input_proto.sysroot.build_target)
122 sysroot_path = input_proto.sysroot.path
123 src_paths = [src_path.path for src_path in input_proto.src_paths]
124 packages = [controller_util.PackageInfoToCPV(x) for x in input_proto.packages]
125
126 package_deps = dependency.GetDependencies(
127 sysroot_path,
128 build_target=build_target,
129 src_paths=src_paths,
130 packages=packages)
131 for package in package_deps:
132 package_info = output_proto.package_deps.add()
133 cpv = portage_util.SplitCPV(package, strict=False)
134 controller_util.CPVToPackageInfo(cpv, package_info)
135
136
Chris McDonald360c2da2019-12-18 13:25:16 -0700137def _DummyGetToolchainPathsResponse(_input_proto, output_proto, _config):
138 """Create a fake successful response for GetToolchainPaths."""
139 dummy_entry = output_proto.paths.add()
140 dummy_entry.path = 'src/third_party/dummy-package'
141
142
143@faux.success(_DummyGetToolchainPathsResponse)
144@faux.empty_error
145@validate.validation_complete
146def GetToolchainPaths(_input_proto, output_proto, _config):
147 """Get a list of paths that affect the toolchain."""
148 toolchain_paths = dependency.DetermineToolchainSourcePaths()
149 for p in toolchain_paths:
150 source_path = output_proto.paths.add()
151 source_path.path = p