Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 1 | #!/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 | |
| 18 | import argparse |
| 19 | import collections |
| 20 | import json |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 21 | import os |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 22 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 23 | import linker_config_pb2 #pylint: disable=import-error |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 24 | from google.protobuf.descriptor import FieldDescriptor |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 25 | from google.protobuf.json_format import ParseDict |
| 26 | from google.protobuf.text_format import MessageToString |
| 27 | |
| 28 | |
| 29 | def Proto(args): |
Jooyung Han | 014ccd4 | 2023-01-09 16:23:14 +0900 | [diff] [blame^] | 30 | 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 Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 39 | with open(args.output, 'wb') as f: |
| 40 | f.write(pb.SerializeToString()) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def Print(args): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 44 | with open(args.source, 'rb') as f: |
| 45 | pb = linker_config_pb2.LinkerConfig() |
| 46 | pb.ParseFromString(f.read()) |
| 47 | print(MessageToString(pb)) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 48 | |
| 49 | |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 50 | def SystemProvide(args): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 51 | pb = linker_config_pb2.LinkerConfig() |
| 52 | with open(args.source, 'rb') as f: |
| 53 | pb.ParseFromString(f.read()) |
| 54 | libraries = args.value.split() |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 55 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 56 | 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 Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 62 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 63 | 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 Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 69 | |
| 70 | |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 71 | def Append(args): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 72 | pb = linker_config_pb2.LinkerConfig() |
| 73 | with open(args.source, 'rb') as f: |
| 74 | pb.ParseFromString(f.read()) |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 75 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 76 | 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 Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 82 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 83 | with open(args.output, 'wb') as f: |
| 84 | f.write(pb.SerializeToString()) |
| 85 | |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 86 | |
Jooyung Han | e134d09 | 2021-04-15 05:13:34 +0900 | [diff] [blame] | 87 | def Merge(args): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 88 | pb = linker_config_pb2.LinkerConfig() |
| 89 | for other in args.input: |
| 90 | with open(other, 'rb') as f: |
| 91 | pb.MergeFromString(f.read()) |
Jooyung Han | e134d09 | 2021-04-15 05:13:34 +0900 | [diff] [blame] | 92 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 93 | with open(args.out, 'wb') as f: |
| 94 | f.write(pb.SerializeToString()) |
| 95 | |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 96 | |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 97 | def GetArgParser(): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 98 | parser = argparse.ArgumentParser() |
| 99 | subparsers = parser.add_subparsers() |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 100 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 101 | 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 Han | 014ccd4 | 2023-01-09 16:23:14 +0900 | [diff] [blame^] | 109 | help='Colon-separated list of linker configuration files in JSON.') |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 110 | 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 Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 117 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 118 | 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 Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 127 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 128 | 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 Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 153 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 154 | 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 Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 177 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 178 | 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 Han | e134d09 | 2021-04-15 05:13:34 +0900 | [diff] [blame] | 192 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 193 | return parser |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 194 | |
| 195 | |
| 196 | def main(): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 197 | args = GetArgParser().parse_args() |
| 198 | args.func(args) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 199 | |
| 200 | |
| 201 | if __name__ == '__main__': |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 202 | main() |