blob: 2ce0ee2be60b9e550cbb33acf99a3e70c0cebc2c [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()
Jooyung Hanb531bee2023-03-04 08:28:40 +090031 if args.source:
32 for input in args.source.split(':'):
33 json_content = ''
34 with open(input) as f:
35 for line in f:
36 if not line.lstrip().startswith('//'):
37 json_content += line
38 obj = json.loads(json_content, object_pairs_hook=collections.OrderedDict)
39 ParseDict(obj, pb)
Spandan Das1d4bfd82021-08-25 22:58:46 +000040 with open(args.output, 'wb') as f:
41 f.write(pb.SerializeToString())
Kiyoung Kim62abd122020-10-06 17:16:44 +090042
43
44def Print(args):
Spandan Das1d4bfd82021-08-25 22:58:46 +000045 with open(args.source, 'rb') as f:
46 pb = linker_config_pb2.LinkerConfig()
47 pb.ParseFromString(f.read())
48 print(MessageToString(pb))
Kiyoung Kim62abd122020-10-06 17:16:44 +090049
50
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090051def SystemProvide(args):
Spandan Das1d4bfd82021-08-25 22:58:46 +000052 pb = linker_config_pb2.LinkerConfig()
53 with open(args.source, 'rb') as f:
54 pb.ParseFromString(f.read())
55 libraries = args.value.split()
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090056
Spandan Das1d4bfd82021-08-25 22:58:46 +000057 def IsInLibPath(lib_name):
58 lib_path = os.path.join(args.system, 'lib', lib_name)
59 lib64_path = os.path.join(args.system, 'lib64', lib_name)
60 return os.path.exists(lib_path) or os.path.islink(
61 lib_path) or os.path.exists(lib64_path) or os.path.islink(
62 lib64_path)
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090063
Spandan Das1d4bfd82021-08-25 22:58:46 +000064 installed_libraries = [lib for lib in libraries if IsInLibPath(lib)]
65 for item in installed_libraries:
66 if item not in getattr(pb, 'provideLibs'):
67 getattr(pb, 'provideLibs').append(item)
68 with open(args.output, 'wb') as f:
69 f.write(pb.SerializeToString())
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090070
71
Kiyoung Kim4ee686d2020-12-03 15:20:07 +090072def Append(args):
Spandan Das1d4bfd82021-08-25 22:58:46 +000073 pb = linker_config_pb2.LinkerConfig()
74 with open(args.source, 'rb') as f:
75 pb.ParseFromString(f.read())
Kiyoung Kim4ee686d2020-12-03 15:20:07 +090076
Spandan Das1d4bfd82021-08-25 22:58:46 +000077 if getattr(type(pb),
78 args.key).DESCRIPTOR.label == FieldDescriptor.LABEL_REPEATED:
79 for value in args.value.split():
80 getattr(pb, args.key).append(value)
81 else:
82 setattr(pb, args.key, args.value)
Kiyoung Kim4ee686d2020-12-03 15:20:07 +090083
Spandan Das1d4bfd82021-08-25 22:58:46 +000084 with open(args.output, 'wb') as f:
85 f.write(pb.SerializeToString())
86
Kiyoung Kim4ee686d2020-12-03 15:20:07 +090087
Jooyung Hane134d092021-04-15 05:13:34 +090088def Merge(args):
Spandan Das1d4bfd82021-08-25 22:58:46 +000089 pb = linker_config_pb2.LinkerConfig()
90 for other in args.input:
91 with open(other, 'rb') as f:
92 pb.MergeFromString(f.read())
Jooyung Hane134d092021-04-15 05:13:34 +090093
Spandan Das1d4bfd82021-08-25 22:58:46 +000094 with open(args.out, 'wb') as f:
95 f.write(pb.SerializeToString())
96
Kiyoung Kim4ee686d2020-12-03 15:20:07 +090097
Kiyoung Kim62abd122020-10-06 17:16:44 +090098def GetArgParser():
Spandan Das1d4bfd82021-08-25 22:58:46 +000099 parser = argparse.ArgumentParser()
100 subparsers = parser.add_subparsers()
Kiyoung Kim62abd122020-10-06 17:16:44 +0900101
Spandan Das1d4bfd82021-08-25 22:58:46 +0000102 parser_proto = subparsers.add_parser(
103 'proto',
104 help='Convert the input JSON configuration file into protobuf.')
105 parser_proto.add_argument(
106 '-s',
107 '--source',
Jooyung Hanb531bee2023-03-04 08:28:40 +0900108 nargs='?',
Spandan Das1d4bfd82021-08-25 22:58:46 +0000109 type=str,
Jooyung Han014ccd42023-01-09 16:23:14 +0900110 help='Colon-separated list of linker configuration files in JSON.')
Spandan Das1d4bfd82021-08-25 22:58:46 +0000111 parser_proto.add_argument(
112 '-o',
113 '--output',
114 required=True,
115 type=str,
116 help='Target path to create protobuf file.')
117 parser_proto.set_defaults(func=Proto)
Kiyoung Kim62abd122020-10-06 17:16:44 +0900118
Spandan Das1d4bfd82021-08-25 22:58:46 +0000119 print_proto = subparsers.add_parser(
120 'print', help='Print configuration in human-readable text format.')
121 print_proto.add_argument(
122 '-s',
123 '--source',
124 required=True,
125 type=str,
126 help='Source linker configuration file in protobuf.')
127 print_proto.set_defaults(func=Print)
Kiyoung Kim62abd122020-10-06 17:16:44 +0900128
Spandan Das1d4bfd82021-08-25 22:58:46 +0000129 system_provide_libs = subparsers.add_parser(
130 'systemprovide',
131 help='Append system provide libraries into the configuration.')
132 system_provide_libs.add_argument(
133 '-s',
134 '--source',
135 required=True,
136 type=str,
137 help='Source linker configuration file in protobuf.')
138 system_provide_libs.add_argument(
139 '-o',
140 '--output',
141 required=True,
142 type=str,
143 help='Target linker configuration file to write in protobuf.')
144 system_provide_libs.add_argument(
145 '--value',
146 required=True,
147 type=str,
148 help='Values of the libraries to append. If there are more than one '
149 'it should be separated by empty space'
150 )
151 system_provide_libs.add_argument(
152 '--system', required=True, type=str, help='Path of the system image.')
153 system_provide_libs.set_defaults(func=SystemProvide)
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +0900154
Spandan Das1d4bfd82021-08-25 22:58:46 +0000155 append = subparsers.add_parser(
156 'append', help='Append value(s) to given key.')
157 append.add_argument(
158 '-s',
159 '--source',
160 required=True,
161 type=str,
162 help='Source linker configuration file in protobuf.')
163 append.add_argument(
164 '-o',
165 '--output',
166 required=True,
167 type=str,
168 help='Target linker configuration file to write in protobuf.')
169 append.add_argument('--key', required=True, type=str, help='.')
170 append.add_argument(
171 '--value',
172 required=True,
173 type=str,
174 help='Values of the libraries to append. If there are more than one'
175 'it should be separated by empty space'
176 )
177 append.set_defaults(func=Append)
Kiyoung Kim4ee686d2020-12-03 15:20:07 +0900178
Spandan Das1d4bfd82021-08-25 22:58:46 +0000179 append = subparsers.add_parser('merge', help='Merge configurations')
180 append.add_argument(
181 '-o',
182 '--out',
183 required=True,
184 type=str,
185 help='Output linker configuration file to write in protobuf.')
186 append.add_argument(
187 '-i',
188 '--input',
189 nargs='+',
190 type=str,
191 help='Linker configuration files to merge.')
192 append.set_defaults(func=Merge)
Jooyung Hane134d092021-04-15 05:13:34 +0900193
Spandan Das1d4bfd82021-08-25 22:58:46 +0000194 return parser
Kiyoung Kim62abd122020-10-06 17:16:44 +0900195
196
197def main():
Spandan Das1d4bfd82021-08-25 22:58:46 +0000198 args = GetArgParser().parse_args()
199 args.func(args)
Kiyoung Kim62abd122020-10-06 17:16:44 +0900200
201
202if __name__ == '__main__':
Spandan Das1d4bfd82021-08-25 22:58:46 +0000203 main()