blob: fd3a4343bd16503130cd4c2429734fd2906dace8 [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
Alex Klein076841b2019-08-29 15:19:39 -060016from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060017from chromite.api import validate
LaMont Jones00b9cff2019-12-12 12:14:07 -070018from chromite.api.controller import controller_util
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
Ned Nguyene5d04852019-02-19 16:33:15 -070021from chromite.lib import portage_util
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
Mike Frysingeref94e4c2020-02-10 23:59:54 -050025assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
26
27
Ned Nguyen8e144bd2019-05-02 09:09:41 -060028def AugmentDepGraphProtoFromJsonMap(json_map, graph):
29 """Augment package deps from |json_map| to graph object.
Ned Nguyene5d04852019-02-19 16:33:15 -070030
31 Args:
32 json_map: the json object that stores the portage package. This is
Alex Kleinb7cdbe62019-02-22 11:41:32 -070033 generated from chromite.lib.service.dependency.GetBuildDependency()
Ned Nguyen8e144bd2019-05-02 09:09:41 -060034 graph: the proto object that represents the dependency graph (see DepGraph
35 message in chromite/api/depgraph.proto)
Ned Nguyene5d04852019-02-19 16:33:15 -070036 """
LaMont Jones4cbecba2020-05-12 11:54:27 -060037 graph.sysroot.build_target.name = json_map['target_board']
38 graph.sysroot.path = json_map['sysroot_path']
39 # TODO(crbug/1081828): Drop this when no longer used.
Ned Nguyene5d04852019-02-19 16:33:15 -070040 graph.build_target.name = json_map['target_board']
41
Mike Frysinger0bdbc102019-06-13 15:27:29 -040042 for data in json_map['package_deps'].values():
Ned Nguyene5d04852019-02-19 16:33:15 -070043 package_dep_info = graph.package_deps.add()
44 package_info = package_dep_info.package_info
45 package_info.package_name = data['name']
46 package_info.category = data['category']
47 package_info.version = data['version']
48 for dep in data['deps']:
49 cpv = portage_util.SplitCPV(dep, strict=False)
50 dep_package = package_dep_info.dependency_packages.add()
51 dep_package.package_name = cpv.package
52 dep_package.category = cpv.category
53 if cpv.version:
54 dep_package.version = cpv.version
55
Chris McDonaldb97c32e2020-01-08 14:26:58 -070056 package_CPV = '%s/%s-%s' % (package_info.category,
57 package_info.package_name, package_info.version)
Ned Nguyen8be10052019-02-26 08:58:45 -070058 for path in json_map['source_path_mapping'][package_CPV]:
Ned Nguyene5d04852019-02-19 16:33:15 -070059 source_path = package_dep_info.dependency_source_paths.add()
60 source_path.path = path
61
Ned Nguyenb5d38722019-05-06 10:24:06 -060062
Michael Mortensenaf10cca2019-11-18 20:21:32 -070063def _GetBuildDependencyGraphResponse(_input_proto, output_proto, _config):
64 """Add fake dep_graph data to a successful response."""
65 output_proto.dep_graph.build_target.name = 'target_board'
66
67
68@faux.success(_GetBuildDependencyGraphResponse)
69@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -060070@validate.validation_complete
71def GetBuildDependencyGraph(input_proto, output_proto, _config):
Ned Nguyen9a7a9052019-02-05 11:04:03 -070072 """Create the build dependency graph.
73
74 Args:
75 input_proto (GetBuildDependencyGraphRequest): The input arguments message.
76 output_proto (GetBuildDependencyGraphResponse): The empty output message.
Alex Klein231d2da2019-07-22 16:44:45 -060077 _config (api_config.ApiConfig): The API call config.
Ned Nguyen9a7a9052019-02-05 11:04:03 -070078 """
LaMont Jones4cbecba2020-05-12 11:54:27 -060079 if input_proto.HasField('sysroot'):
80 board = input_proto.sysroot.build_target.name
81 sysroot_path = input_proto.sysroot.path
82 else:
83 # TODO(crbug/1081828): stop using build_target and drop it from the proto.
84 board = input_proto.build_target.name
85 sysroot_path = cros_build_lib.GetSysroot(board or None)
86
Chris McDonaldb97c32e2020-01-08 14:26:58 -070087 packages = [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
95def _DummyGetToolchainPathsResponse(_input_proto, output_proto, _config):
96 """Create a fake successful response for GetToolchainPaths."""
97 dummy_entry = output_proto.paths.add()
98 dummy_entry.path = 'src/third_party/dummy-package'
99
100
101@faux.success(_DummyGetToolchainPathsResponse)
102@faux.empty_error
103@validate.validation_complete
104def GetToolchainPaths(_input_proto, output_proto, _config):
105 """Get a list of paths that affect the toolchain."""
106 toolchain_paths = dependency.DetermineToolchainSourcePaths()
107 for p in toolchain_paths:
108 source_path = output_proto.paths.add()
109 source_path.path = p