blob: 3d7c0faa49b80317df6d6cbd9963147bfdc9a225 [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
Spandan Das1d4bfd82021-08-25 22:58:46 +000023import linker_config_pb2 #pylint: disable=import-error
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):
Jooyung Han014ccd42023-01-09 16:23:14 +090030 pb = linker_config_pb2.LinkerConfig()
31 for input in args.source.split(':'):
32 json_content = ''
33 with open(input) as f:
34 for line in f:
35 if not line.lstrip().startswith('//'):
36 json_content += line
37 obj = json.loads(json_content, object_pairs_hook=collections.OrderedDict)
38 ParseDict(obj, pb)
Spandan Das1d4bfd82021-08-25 22:58:46 +000039 with open(args.output, 'wb') as f:
40 f.write(pb.SerializeToString())
Kiyoung Kim62abd122020-10-06 17:16:44 +090041
42
43def Print(args):
Spandan Das1d4bfd82021-08-25 22:58:46 +000044 with open(args.source, 'rb') as f:
45 pb = linker_config_pb2.LinkerConfig()
46 pb.ParseFromString(f.read())
47 print(MessageToString(pb))
Kiyoung Kim62abd122020-10-06 17:16:44 +090048
49
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090050def SystemProvide(args):
Spandan Das1d4bfd82021-08-25 22:58:46 +000051 pb = linker_config_pb2.LinkerConfig()
52 with open(args.source, 'rb') as f:
53 pb.ParseFromString(f.read())
54 libraries = args.value.split()
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090055
Spandan Das1d4bfd82021-08-25 22:58:46 +000056 def IsInLibPath(lib_name):
57 lib_path = os.path.join(args.system, 'lib', lib_name)
58 lib64_path = os.path.join(args.system, 'lib64', lib_name)
59 return os.path.exists(lib_path) or os.path.islink(
60 lib_path) or os.path.exists(lib64_path) or os.path.islink(
61 lib64_path)
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090062
Spandan Das1d4bfd82021-08-25 22:58:46 +000063 installed_libraries = [lib for lib in libraries if IsInLibPath(lib)]
64 for item in installed_libraries:
65 if item not in getattr(pb, 'provideLibs'):
66 getattr(pb, 'provideLibs').append(item)
67 with open(args.output, 'wb') as f:
68 f.write(pb.SerializeToString())
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090069
70
Kiyoung Kim4ee686d2020-12-03 15:20:07 +090071def Append(args):
Spandan Das1d4bfd82021-08-25 22:58:46 +000072 pb = linker_config_pb2.LinkerConfig()
73 with open(args.source, 'rb') as f:
74 pb.ParseFromString(f.read())
Kiyoung Kim4ee686d2020-12-03 15:20:07 +090075
Spandan Das1d4bfd82021-08-25 22:58:46 +000076 if getattr(type(pb),
77 args.key).DESCRIPTOR.label == FieldDescriptor.LABEL_REPEATED:
78 for value in args.value.split():
79 getattr(pb, args.key).append(value)
80 else:
81 setattr(pb, args.key, args.value)
Kiyoung Kim4ee686d2020-12-03 15:20:07 +090082
Spandan Das1d4bfd82021-08-25 22:58:46 +000083 with open(args.output, 'wb') as f:
84 f.write(pb.SerializeToString())
85
Kiyoung Kim4ee686d2020-12-03 15:20:07 +090086
Jooyung Hane134d092021-04-15 05:13:34 +090087def Merge(args):
Spandan Das1d4bfd82021-08-25 22:58:46 +000088 pb = linker_config_pb2.LinkerConfig()
89 for other in args.input:
90 with open(other, 'rb') as f:
91 pb.MergeFromString(f.read())
Jooyung Hane134d092021-04-15 05:13:34 +090092
Spandan Das1d4bfd82021-08-25 22:58:46 +000093 with open(args.out, 'wb') as f:
94 f.write(pb.SerializeToString())
95
Kiyoung Kim4ee686d2020-12-03 15:20:07 +090096
Kiyoung Kim62abd122020-10-06 17:16:44 +090097def GetArgParser():
Spandan Das1d4bfd82021-08-25 22:58:46 +000098 parser = argparse.ArgumentParser()
99 subparsers = parser.add_subparsers()
Kiyoung Kim62abd122020-10-06 17:16:44 +0900100
Spandan Das1d4bfd82021-08-25 22:58:46 +0000101 parser_proto = subparsers.add_parser(
102 'proto',
103 help='Convert the input JSON configuration file into protobuf.')
104 parser_proto.add_argument(
105 '-s',
106 '--source',
107 required=True,
108 type=str,
Jooyung Han014ccd42023-01-09 16:23:14 +0900109 help='Colon-separated list of linker configuration files in JSON.')
Spandan Das1d4bfd82021-08-25 22:58:46 +0000110 parser_proto.add_argument(
111 '-o',
112 '--output',
113 required=True,
114 type=str,
115 help='Target path to create protobuf file.')
116 parser_proto.set_defaults(func=Proto)
Kiyoung Kim62abd122020-10-06 17:16:44 +0900117
Spandan Das1d4bfd82021-08-25 22:58:46 +0000118 print_proto = subparsers.add_parser(
119 'print', help='Print configuration in human-readable text format.')
120 print_proto.add_argument(
121 '-s',
122 '--source',
123 required=True,
124 type=str,
125 help='Source linker configuration file in protobuf.')
126 print_proto.set_defaults(func=Print)
Kiyoung Kim62abd122020-10-06 17:16:44 +0900127
Spandan Das1d4bfd82021-08-25 22:58:46 +0000128 system_provide_libs = subparsers.add_parser(
129 'systemprovide',
130 help='Append system provide libraries into the configuration.')
131 system_provide_libs.add_argument(
132 '-s',
133 '--source',
134 required=True,
135 type=str,
136 help='Source linker configuration file in protobuf.')
137 system_provide_libs.add_argument(
138 '-o',
139 '--output',
140 required=True,
141 type=str,
142 help='Target linker configuration file to write in protobuf.')
143 system_provide_libs.add_argument(
144 '--value',
145 required=True,
146 type=str,
147 help='Values of the libraries to append. If there are more than one '
148 'it should be separated by empty space'
149 )
150 system_provide_libs.add_argument(
151 '--system', required=True, type=str, help='Path of the system image.')
152 system_provide_libs.set_defaults(func=SystemProvide)
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +0900153
Spandan Das1d4bfd82021-08-25 22:58:46 +0000154 append = subparsers.add_parser(
155 'append', help='Append value(s) to given key.')
156 append.add_argument(
157 '-s',
158 '--source',
159 required=True,
160 type=str,
161 help='Source linker configuration file in protobuf.')
162 append.add_argument(
163 '-o',
164 '--output',
165 required=True,
166 type=str,
167 help='Target linker configuration file to write in protobuf.')
168 append.add_argument('--key', required=True, type=str, help='.')
169 append.add_argument(
170 '--value',
171 required=True,
172 type=str,
173 help='Values of the libraries to append. If there are more than one'
174 'it should be separated by empty space'
175 )
176 append.set_defaults(func=Append)
Kiyoung Kim4ee686d2020-12-03 15:20:07 +0900177
Spandan Das1d4bfd82021-08-25 22:58:46 +0000178 append = subparsers.add_parser('merge', help='Merge configurations')
179 append.add_argument(
180 '-o',
181 '--out',
182 required=True,
183 type=str,
184 help='Output linker configuration file to write in protobuf.')
185 append.add_argument(
186 '-i',
187 '--input',
188 nargs='+',
189 type=str,
190 help='Linker configuration files to merge.')
191 append.set_defaults(func=Merge)
Jooyung Hane134d092021-04-15 05:13:34 +0900192
Spandan Das1d4bfd82021-08-25 22:58:46 +0000193 return parser
Kiyoung Kim62abd122020-10-06 17:16:44 +0900194
195
196def main():
Spandan Das1d4bfd82021-08-25 22:58:46 +0000197 args = GetArgParser().parse_args()
198 args.func(args)
Kiyoung Kim62abd122020-10-06 17:16:44 +0900199
200
201if __name__ == '__main__':
Spandan Das1d4bfd82021-08-25 22:58:46 +0000202 main()