blob: 22fe9f671b300b9db2f296354fe8f8dfbcedc3ec [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 Kim4ee686d2020-12-03 15:20:07 +090067def Append(args):
68 pb = linker_config_pb2.LinkerConfig()
69 with open(args.source, 'rb') as f:
70 pb.ParseFromString(f.read())
71
72 if getattr(type(pb), args.key).DESCRIPTOR.label == FieldDescriptor.LABEL_REPEATED:
73 for value in args.value.split():
74 getattr(pb, args.key).append(value)
75 else:
76 setattr(pb, args.key, args.value)
77
78 with open(args.output, 'wb') as f:
79 f.write(pb.SerializeToString())
80
81
Kiyoung Kim62abd122020-10-06 17:16:44 +090082def GetArgParser():
83 parser = argparse.ArgumentParser()
84 subparsers = parser.add_subparsers()
85
86 parser_proto = subparsers.add_parser(
87 'proto', help='Convert the input JSON configuration file into protobuf.')
88 parser_proto.add_argument(
89 '-s',
90 '--source',
91 required=True,
92 type=str,
93 help='Source linker configuration file in JSON.')
94 parser_proto.add_argument(
95 '-o',
96 '--output',
97 required=True,
98 type=str,
99 help='Target path to create protobuf file.')
100 parser_proto.set_defaults(func=Proto)
101
102 print_proto = subparsers.add_parser(
103 'print', help='Print configuration in human-readable text format.')
104 print_proto.add_argument(
105 '-s',
106 '--source',
107 required=True,
108 type=str,
109 help='Source linker configuration file in protobuf.')
110 print_proto.set_defaults(func=Print)
111
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +0900112 system_provide_libs = subparsers.add_parser(
113 'systemprovide', help='Append system provide libraries into the configuration.')
114 system_provide_libs.add_argument(
115 '-s',
116 '--source',
117 required=True,
118 type=str,
119 help='Source linker configuration file in protobuf.')
120 system_provide_libs.add_argument(
121 '-o',
122 '--output',
123 required=True,
124 type=str,
125 help='Target linker configuration file to write in protobuf.')
126 system_provide_libs.add_argument(
127 '--value',
128 required=True,
129 type=str,
130 help='Values of the libraries to append. If there are more than one it should be separated by empty space')
131 system_provide_libs.add_argument(
132 '--system',
133 required=True,
134 type=str,
135 help='Path of the system image.')
136 system_provide_libs.set_defaults(func=SystemProvide)
137
Kiyoung Kim4ee686d2020-12-03 15:20:07 +0900138 append = subparsers.add_parser(
139 'append', help='Append value(s) to given key.')
140 append.add_argument(
141 '-s',
142 '--source',
143 required=True,
144 type=str,
145 help='Source linker configuration file in protobuf.')
146 append.add_argument(
147 '-o',
148 '--output',
149 required=True,
150 type=str,
151 help='Target linker configuration file to write in protobuf.')
152 append.add_argument(
153 '--key',
154 required=True,
155 type=str,
156 help='.')
157 append.add_argument(
158 '--value',
159 required=True,
160 type=str,
161 help='Values of the libraries to append. If there are more than one it should be separated by empty space')
162 append.set_defaults(func=Append)
163
Kiyoung Kim62abd122020-10-06 17:16:44 +0900164 return parser
165
166
167def main():
168 args = GetArgParser().parse_args()
169 args.func(args)
170
171
172if __name__ == '__main__':
173 main()