blob: 93f35dc88283a2bcee186d6af3c59666afa30bf2 [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
45 package_CP = '%s/%s' % (package_info.category, package_info.package_name)
46 for path in json_map['source_path_mapping'][package_CP]:
47 source_path = package_dep_info.dependency_source_paths.add()
48 source_path.path = path
49
50 return graph
Ned Nguyen9a7a9052019-02-05 11:04:03 -070051
52def GetBuildDependencyGraph(input_proto, output_proto):
53 """Create the build dependency graph.
54
55 Args:
56 input_proto (GetBuildDependencyGraphRequest): The input arguments message.
57 output_proto (GetBuildDependencyGraphResponse): The empty output message.
58 """
59 board = input_proto.build_target.name
60 output_path = input_proto.output_path
61
62 assert board, 'Missing build target name'
63 assert output_path, 'Missing output file'
64
Ned Nguyene5d04852019-02-19 16:33:15 -070065
Ned Nguyen9a7a9052019-02-05 11:04:03 -070066 json_map = dependency.GetBuildDependency(board)
Ned Nguyene5d04852019-02-19 16:33:15 -070067 graph = CreateDepGraphProtoFromJsonMap(json_map)
68
Ned Nguyen9a7a9052019-02-05 11:04:03 -070069 with open(output_path, 'w') as f:
Ned Nguyene5d04852019-02-19 16:33:15 -070070 f.write(
71 json_format.MessageToJson(graph, including_default_value_fields=True))
Ned Nguyen9a7a9052019-02-05 11:04:03 -070072
73 output_proto.build_dependency_graph_file = output_path