blob: fcf9750b6a902a80f3801daa51d41761846ae873 [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
Chris McDonaldb97c32e2020-01-08 14:26:58 -070089 packages = [controller_util.PackageInfoToCPV(x) for x in input_proto.packages]
Ned Nguyen9a7a9052019-02-05 11:04:03 -070090
LaMont Jones4cbecba2020-05-12 11:54:27 -060091 json_map, sdk_json_map = dependency.GetBuildDependency(sysroot_path, board,
92 packages)
Ned Nguyen8e144bd2019-05-02 09:09:41 -060093 AugmentDepGraphProtoFromJsonMap(json_map, output_proto.dep_graph)
Chris McDonalda22b74f2019-11-22 13:55:06 -070094 AugmentDepGraphProtoFromJsonMap(sdk_json_map, output_proto.sdk_dep_graph)
Chris McDonald360c2da2019-12-18 13:25:16 -070095
96
Navil Pereza7823262020-08-07 17:56:57 +000097def _ListResponse(_input_proto, output_proto, _config):
98 """Add fake dependency data to a successful response."""
99 package_dep = output_proto.package_deps.add()
100 package_dep.category = 'category'
101 package_dep.package_name = 'name'
102
103
104@faux.success(_ListResponse)
105@faux.empty_error
106@validate.require('sysroot.build_target.name')
107@validate.exists('sysroot.path')
108@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:
131 package_info = output_proto.package_deps.add()
132 cpv = portage_util.SplitCPV(package, strict=False)
133 controller_util.CPVToPackageInfo(cpv, package_info)
134
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