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() |
Jooyung Han | b531bee | 2023-03-04 08:28:40 +0900 | [diff] [blame] | 31 | 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 Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 40 | with open(args.output, 'wb') as f: |
| 41 | f.write(pb.SerializeToString()) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 42 | |
| 43 | |
| 44 | def Print(args): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 45 | with open(args.source, 'rb') as f: |
| 46 | pb = linker_config_pb2.LinkerConfig() |
| 47 | pb.ParseFromString(f.read()) |
| 48 | print(MessageToString(pb)) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 49 | |
| 50 | |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 51 | def SystemProvide(args): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 52 | pb = linker_config_pb2.LinkerConfig() |
| 53 | with open(args.source, 'rb') as f: |
| 54 | pb.ParseFromString(f.read()) |
| 55 | libraries = args.value.split() |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 56 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 57 | 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 Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 63 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 64 | 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 Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 70 | |
| 71 | |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 72 | def Append(args): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 73 | pb = linker_config_pb2.LinkerConfig() |
| 74 | with open(args.source, 'rb') as f: |
| 75 | pb.ParseFromString(f.read()) |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 76 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 77 | 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 Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 83 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 84 | with open(args.output, 'wb') as f: |
| 85 | f.write(pb.SerializeToString()) |
| 86 | |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 87 | |
Jooyung Han | e134d09 | 2021-04-15 05:13:34 +0900 | [diff] [blame] | 88 | def Merge(args): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 89 | pb = linker_config_pb2.LinkerConfig() |
| 90 | for other in args.input: |
| 91 | with open(other, 'rb') as f: |
| 92 | pb.MergeFromString(f.read()) |
Jooyung Han | e134d09 | 2021-04-15 05:13:34 +0900 | [diff] [blame] | 93 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 94 | with open(args.out, 'wb') as f: |
| 95 | f.write(pb.SerializeToString()) |
| 96 | |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 97 | |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 98 | def GetArgParser(): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 99 | parser = argparse.ArgumentParser() |
| 100 | subparsers = parser.add_subparsers() |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 101 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 102 | 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 Han | b531bee | 2023-03-04 08:28:40 +0900 | [diff] [blame] | 108 | nargs='?', |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 109 | type=str, |
Jooyung Han | 014ccd4 | 2023-01-09 16:23:14 +0900 | [diff] [blame] | 110 | help='Colon-separated list of linker configuration files in JSON.') |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 111 | 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 Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 118 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 119 | 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 Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 128 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 129 | 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 Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 154 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 155 | 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 Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 178 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 179 | 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 Han | e134d09 | 2021-04-15 05:13:34 +0900 | [diff] [blame] | 193 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 194 | return parser |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 195 | |
| 196 | |
| 197 | def main(): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 198 | args = GetArgParser().parse_args() |
| 199 | args.func(args) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 200 | |
| 201 | |
| 202 | if __name__ == '__main__': |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 203 | main() |