summaryrefslogtreecommitdiff
path: root/scripts/conv_linker_config.py
blob: 81425fb3516c19c97b0107a09a8f8dfc0e011122 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
#
# Copyright (C) 2020 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" A tool to convert json file into pb with linker config format."""

import argparse
import collections
import json

import linker_config_pb2
from google.protobuf.json_format import ParseDict
from google.protobuf.text_format import MessageToString


def Proto(args):
  json_content = ''
  with open(args.source) as f:
    for line in f:
      if not line.lstrip().startswith('//'):
        json_content += line
  obj = json.loads(json_content, object_pairs_hook=collections.OrderedDict)
  pb = ParseDict(obj, linker_config_pb2.LinkerConfig())
  with open(args.output, 'wb') as f:
    f.write(pb.SerializeToString())


def Print(args):
  with open(args.source, 'rb') as f:
    pb = linker_config_pb2.LinkerConfig()
    pb.ParseFromString(f.read())
  print(MessageToString(pb))


def GetArgParser():
  parser = argparse.ArgumentParser()
  subparsers = parser.add_subparsers()

  parser_proto = subparsers.add_parser(
      'proto', help='Convert the input JSON configuration file into protobuf.')
  parser_proto.add_argument(
      '-s',
      '--source',
      required=True,
      type=str,
      help='Source linker configuration file in JSON.')
  parser_proto.add_argument(
      '-o',
      '--output',
      required=True,
      type=str,
      help='Target path to create protobuf file.')
  parser_proto.set_defaults(func=Proto)

  print_proto = subparsers.add_parser(
      'print', help='Print configuration in human-readable text format.')
  print_proto.add_argument(
      '-s',
      '--source',
      required=True,
      type=str,
      help='Source linker configuration file in protobuf.')
  print_proto.set_defaults(func=Print)

  return parser


def main():
  args = GetArgParser().parse_args()
  args.func(args)


if __name__ == '__main__':
  main()