blob: ddadd742e8b37cf96cadb44d5737deeabc490d99 [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
Ned Nguyene5d04852019-02-19 16:33:15 -070014from chromite.api.gen import depgraph_pb2
Ned Nguyen9a7a9052019-02-05 11:04:03 -070015from chromite.lib.api import dependency
Ned Nguyene5d04852019-02-19 16:33:15 -070016from chromite.lib import portage_util
Ned Nguyen9a7a9052019-02-05 11:04:03 -070017
Ned Nguyene5d04852019-02-19 16:33:15 -070018from google.protobuf import json_format
19
20
21def CreateDepGraphProtoFromJsonMap(json_map):
22 """Return the depgraph proto message from package deps json map.
23
24 Args:
25 json_map: the json object that stores the portage package. This is
26 generated from chromite.lib.api.dependency.GetBuildDependency()
27 """
28 graph = depgraph_pb2.DepGraph()
29 graph.build_target.name = json_map['target_board']
30
31 for data in json_map['package_deps'].itervalues():
32 package_dep_info = graph.package_deps.add()
33 package_info = package_dep_info.package_info
34 package_info.package_name = data['name']
35 package_info.category = data['category']
36 package_info.version = data['version']
37 for dep in data['deps']:
38 cpv = portage_util.SplitCPV(dep, strict=False)
39 dep_package = package_dep_info.dependency_packages.add()
40 dep_package.package_name = cpv.package
41 dep_package.category = cpv.category
42 if cpv.version:
43 dep_package.version = cpv.version
44
Ned Nguyen8be10052019-02-26 08:58:45 -070045 package_CPV = '%s/%s-%s' % (
46 package_info.category, package_info.package_name, package_info.version)
47 for path in json_map['source_path_mapping'][package_CPV]:
Ned Nguyene5d04852019-02-19 16:33:15 -070048 source_path = package_dep_info.dependency_source_paths.add()
49 source_path.path = path
50
51 return graph
Ned Nguyen9a7a9052019-02-05 11:04:03 -070052
53def GetBuildDependencyGraph(input_proto, output_proto):
54 """Create the build dependency graph.
55
56 Args:
57 input_proto (GetBuildDependencyGraphRequest): The input arguments message.
58 output_proto (GetBuildDependencyGraphResponse): The empty output message.
59 """
60 board = input_proto.build_target.name
61 output_path = input_proto.output_path
62
63 assert board, 'Missing build target name'
64 assert output_path, 'Missing output file'
65
Ned Nguyene5d04852019-02-19 16:33:15 -070066
Ned Nguyen9a7a9052019-02-05 11:04:03 -070067 json_map = dependency.GetBuildDependency(board)
Ned Nguyene5d04852019-02-19 16:33:15 -070068 graph = CreateDepGraphProtoFromJsonMap(json_map)
69
Ned Nguyen9a7a9052019-02-05 11:04:03 -070070 with open(output_path, 'w') as f:
Ned Nguyene5d04852019-02-19 16:33:15 -070071 f.write(
72 json_format.MessageToJson(graph, including_default_value_fields=True))
Ned Nguyen9a7a9052019-02-05 11:04:03 -070073
74 output_proto.build_dependency_graph_file = output_path