blob: ea243c1b166054189edeb07048129e635573bb4e [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
Ned Nguyene5d04852019-02-19 16:33:15 -070019from chromite.lib import portage_util
Alex Kleinb7cdbe62019-02-22 11:41:32 -070020from chromite.service import dependency
Ned Nguyen9a7a9052019-02-05 11:04:03 -070021
Ned Nguyene5d04852019-02-19 16:33:15 -070022
Mike Frysingeref94e4c2020-02-10 23:59:54 -050023assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
24
25
Ned Nguyen8e144bd2019-05-02 09:09:41 -060026def AugmentDepGraphProtoFromJsonMap(json_map, graph):
27 """Augment package deps from |json_map| to graph object.
Ned Nguyene5d04852019-02-19 16:33:15 -070028
29 Args:
30 json_map: the json object that stores the portage package. This is
Alex Kleinb7cdbe62019-02-22 11:41:32 -070031 generated from chromite.lib.service.dependency.GetBuildDependency()
Ned Nguyen8e144bd2019-05-02 09:09:41 -060032 graph: the proto object that represents the dependency graph (see DepGraph
33 message in chromite/api/depgraph.proto)
Ned Nguyene5d04852019-02-19 16:33:15 -070034 """
Ned Nguyene5d04852019-02-19 16:33:15 -070035 graph.build_target.name = json_map['target_board']
36
Mike Frysinger0bdbc102019-06-13 15:27:29 -040037 for data in json_map['package_deps'].values():
Ned Nguyene5d04852019-02-19 16:33:15 -070038 package_dep_info = graph.package_deps.add()
39 package_info = package_dep_info.package_info
40 package_info.package_name = data['name']
41 package_info.category = data['category']
42 package_info.version = data['version']
43 for dep in data['deps']:
44 cpv = portage_util.SplitCPV(dep, strict=False)
45 dep_package = package_dep_info.dependency_packages.add()
46 dep_package.package_name = cpv.package
47 dep_package.category = cpv.category
48 if cpv.version:
49 dep_package.version = cpv.version
50
Chris McDonaldb97c32e2020-01-08 14:26:58 -070051 package_CPV = '%s/%s-%s' % (package_info.category,
52 package_info.package_name, package_info.version)
Ned Nguyen8be10052019-02-26 08:58:45 -070053 for path in json_map['source_path_mapping'][package_CPV]:
Ned Nguyene5d04852019-02-19 16:33:15 -070054 source_path = package_dep_info.dependency_source_paths.add()
55 source_path.path = path
56
Ned Nguyenb5d38722019-05-06 10:24:06 -060057
Michael Mortensenaf10cca2019-11-18 20:21:32 -070058def _GetBuildDependencyGraphResponse(_input_proto, output_proto, _config):
59 """Add fake dep_graph data to a successful response."""
60 output_proto.dep_graph.build_target.name = 'target_board'
61
62
63@faux.success(_GetBuildDependencyGraphResponse)
64@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -060065@validate.require('build_target.name')
66@validate.validation_complete
67def GetBuildDependencyGraph(input_proto, output_proto, _config):
Ned Nguyen9a7a9052019-02-05 11:04:03 -070068 """Create the build dependency graph.
69
70 Args:
71 input_proto (GetBuildDependencyGraphRequest): The input arguments message.
72 output_proto (GetBuildDependencyGraphResponse): The empty output message.
Alex Klein231d2da2019-07-22 16:44:45 -060073 _config (api_config.ApiConfig): The API call config.
Ned Nguyen9a7a9052019-02-05 11:04:03 -070074 """
75 board = input_proto.build_target.name
Chris McDonaldb97c32e2020-01-08 14:26:58 -070076 packages = [controller_util.PackageInfoToCPV(x) for x in input_proto.packages]
Ned Nguyen9a7a9052019-02-05 11:04:03 -070077
LaMont Jones00b9cff2019-12-12 12:14:07 -070078 json_map, sdk_json_map = dependency.GetBuildDependency(board, packages)
Ned Nguyen8e144bd2019-05-02 09:09:41 -060079 AugmentDepGraphProtoFromJsonMap(json_map, output_proto.dep_graph)
Chris McDonalda22b74f2019-11-22 13:55:06 -070080 AugmentDepGraphProtoFromJsonMap(sdk_json_map, output_proto.sdk_dep_graph)
Chris McDonald360c2da2019-12-18 13:25:16 -070081
82
83def _DummyGetToolchainPathsResponse(_input_proto, output_proto, _config):
84 """Create a fake successful response for GetToolchainPaths."""
85 dummy_entry = output_proto.paths.add()
86 dummy_entry.path = 'src/third_party/dummy-package'
87
88
89@faux.success(_DummyGetToolchainPathsResponse)
90@faux.empty_error
91@validate.validation_complete
92def GetToolchainPaths(_input_proto, output_proto, _config):
93 """Get a list of paths that affect the toolchain."""
94 toolchain_paths = dependency.DetermineToolchainSourcePaths()
95 for p in toolchain_paths:
96 source_path = output_proto.paths.add()
97 source_path.path = p