blob: fca71ad56c9b88992675b8a24cca0abe331d1b64 [file] [log] [blame]
Kiyoung Kim62abd122020-10-06 17:16:44 +09001#!/usr/bin/env python
2#
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16""" A tool to convert json file into pb with linker config format."""
17
18import argparse
19import collections
20import json
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090021import os
Kiyoung Kim62abd122020-10-06 17:16:44 +090022
23import linker_config_pb2
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090024from google.protobuf.descriptor import FieldDescriptor
Kiyoung Kim62abd122020-10-06 17:16:44 +090025from google.protobuf.json_format import ParseDict
26from google.protobuf.text_format import MessageToString
27
28
29def Proto(args):
Kiyoung Kime52c6652020-10-23 11:00:25 +090030 json_content = ''
Kiyoung Kim62abd122020-10-06 17:16:44 +090031 with open(args.source) as f:
Kiyoung Kime52c6652020-10-23 11:00:25 +090032 for line in f:
33 if not line.lstrip().startswith('//'):
34 json_content += line
35 obj = json.loads(json_content, object_pairs_hook=collections.OrderedDict)
Kiyoung Kim62abd122020-10-06 17:16:44 +090036 pb = ParseDict(obj, linker_config_pb2.LinkerConfig())
37 with open(args.output, 'wb') as f:
38 f.write(pb.SerializeToString())
39
40
41def Print(args):
42 with open(args.source, 'rb') as f:
43 pb = linker_config_pb2.LinkerConfig()
44 pb.ParseFromString(f.read())
45 print(MessageToString(pb))
46
47
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090048def SystemProvide(args):
49 pb = linker_config_pb2.LinkerConfig()
50 with open(args.source, 'rb') as f:
51 pb.ParseFromString(f.read())
52 libraries = args.value.split()
53
54 def IsInLibPath(lib_name):
55 lib_path = os.path.join(args.system, 'lib', lib_name)
56 lib64_path = os.path.join(args.system, 'lib64', lib_name)
57 return os.path.exists(lib_path) or os.path.islink(lib_path) or os.path.exists(lib64_path) or os.path.islink(lib64_path)
58
59 installed_libraries = list(filter(IsInLibPath, libraries))
60 for item in installed_libraries:
61 if item not in getattr(pb, 'provideLibs'):
62 getattr(pb, 'provideLibs').append(item)
63 with open(args.output, 'wb') as f:
64 f.write(pb.SerializeToString())
65
66
Kiyoung Kim62abd122020-10-06 17:16:44 +090067def GetArgParser():
68 parser = argparse.ArgumentParser()
69 subparsers = parser.add_subparsers()
70
71 parser_proto = subparsers.add_parser(
72 'proto', help='Convert the input JSON configuration file into protobuf.')
73 parser_proto.add_argument(
74 '-s',
75 '--source',
76 required=True,
77 type=str,
78 help='Source linker configuration file in JSON.')
79 parser_proto.add_argument(
80 '-o',
81 '--output',
82 required=True,
83 type=str,
84 help='Target path to create protobuf file.')
85 parser_proto.set_defaults(func=Proto)
86
87 print_proto = subparsers.add_parser(
88 'print', help='Print configuration in human-readable text format.')
89 print_proto.add_argument(
90 '-s',
91 '--source',
92 required=True,
93 type=str,
94 help='Source linker configuration file in protobuf.')
95 print_proto.set_defaults(func=Print)
96
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090097 system_provide_libs = subparsers.add_parser(
98 'systemprovide', help='Append system provide libraries into the configuration.')
99 system_provide_libs.add_argument(
100 '-s',
101 '--source',
102 required=True,
103 type=str,
104 help='Source linker configuration file in protobuf.')
105 system_provide_libs.add_argument(
106 '-o',
107 '--output',
108 required=True,
109 type=str,
110 help='Target linker configuration file to write in protobuf.')
111 system_provide_libs.add_argument(
112 '--value',
113 required=True,
114 type=str,
115 help='Values of the libraries to append. If there are more than one it should be separated by empty space')
116 system_provide_libs.add_argument(
117 '--system',
118 required=True,
119 type=str,
120 help='Path of the system image.')
121 system_provide_libs.set_defaults(func=SystemProvide)
122
Kiyoung Kim62abd122020-10-06 17:16:44 +0900123 return parser
124
125
126def main():
127 args = GetArgParser().parse_args()
128 args.func(args)
129
130
131if __name__ == '__main__':
132 main()