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 |
Jooyung Han | 3397b6a | 2023-03-08 05:44:17 +0900 | [diff] [blame] | 22 | import sys |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 23 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 24 | import linker_config_pb2 #pylint: disable=import-error |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 25 | from google.protobuf.descriptor import FieldDescriptor |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 26 | from google.protobuf.json_format import ParseDict |
| 27 | from google.protobuf.text_format import MessageToString |
| 28 | |
| 29 | |
Jooyung Han | f6fd4c2 | 2023-03-09 14:50:35 +0900 | [diff] [blame] | 30 | def LoadJsonMessage(path): |
| 31 | """ |
| 32 | Loads a message from a .json file with `//` comments strippedfor convenience. |
| 33 | """ |
| 34 | json_content = '' |
| 35 | with open(path) as f: |
| 36 | for line in f: |
| 37 | if not line.lstrip().startswith('//'): |
| 38 | json_content += line |
| 39 | obj = json.loads(json_content, object_pairs_hook=collections.OrderedDict) |
| 40 | return ParseDict(obj, linker_config_pb2.LinkerConfig()) |
| 41 | |
| 42 | |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 43 | def Proto(args): |
Jooyung Han | 3397b6a | 2023-03-08 05:44:17 +0900 | [diff] [blame] | 44 | """ |
| 45 | Merges input json files (--source) into a protobuf message (--output). |
| 46 | Fails if the output file exists. Set --force or --append to deal with the existing |
| 47 | output file. |
| 48 | --force to overwrite the output file with the input (.json files). |
| 49 | --append to append the input to the output file. |
| 50 | """ |
Jooyung Han | 014ccd4 | 2023-01-09 16:23:14 +0900 | [diff] [blame] | 51 | pb = linker_config_pb2.LinkerConfig() |
Jooyung Han | 3397b6a | 2023-03-08 05:44:17 +0900 | [diff] [blame] | 52 | if os.path.isfile(args.output): |
| 53 | if args.force: |
| 54 | pass |
| 55 | elif args.append: |
| 56 | with open(args.output, 'rb') as f: |
| 57 | pb.ParseFromString(f.read()) |
| 58 | else: |
| 59 | sys.stderr.write(f'Error: {args.output} exists. Use --force or --append.\n') |
| 60 | sys.exit(1) |
| 61 | |
Jooyung Han | b531bee | 2023-03-04 08:28:40 +0900 | [diff] [blame] | 62 | if args.source: |
| 63 | for input in args.source.split(':'): |
Jooyung Han | f6fd4c2 | 2023-03-09 14:50:35 +0900 | [diff] [blame] | 64 | pb.MergeFrom(LoadJsonMessage(input)) |
Kiyoung Kim | 3df5f50 | 2023-09-27 11:26:58 +0900 | [diff] [blame^] | 65 | |
| 66 | ValidateAndWriteAsPbFile(pb, args.output) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 67 | |
| 68 | |
| 69 | def Print(args): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 70 | with open(args.source, 'rb') as f: |
| 71 | pb = linker_config_pb2.LinkerConfig() |
| 72 | pb.ParseFromString(f.read()) |
| 73 | print(MessageToString(pb)) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 74 | |
| 75 | |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 76 | def SystemProvide(args): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 77 | pb = linker_config_pb2.LinkerConfig() |
| 78 | with open(args.source, 'rb') as f: |
| 79 | pb.ParseFromString(f.read()) |
| 80 | libraries = args.value.split() |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 81 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 82 | def IsInLibPath(lib_name): |
| 83 | lib_path = os.path.join(args.system, 'lib', lib_name) |
| 84 | lib64_path = os.path.join(args.system, 'lib64', lib_name) |
| 85 | return os.path.exists(lib_path) or os.path.islink( |
| 86 | lib_path) or os.path.exists(lib64_path) or os.path.islink( |
| 87 | lib64_path) |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 88 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 89 | installed_libraries = [lib for lib in libraries if IsInLibPath(lib)] |
| 90 | for item in installed_libraries: |
| 91 | if item not in getattr(pb, 'provideLibs'): |
| 92 | getattr(pb, 'provideLibs').append(item) |
Kiyoung Kim | 3df5f50 | 2023-09-27 11:26:58 +0900 | [diff] [blame^] | 93 | |
| 94 | ValidateAndWriteAsPbFile(pb, args.output) |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 95 | |
| 96 | |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 97 | def Append(args): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 98 | pb = linker_config_pb2.LinkerConfig() |
| 99 | with open(args.source, 'rb') as f: |
| 100 | pb.ParseFromString(f.read()) |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 101 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 102 | if getattr(type(pb), |
| 103 | args.key).DESCRIPTOR.label == FieldDescriptor.LABEL_REPEATED: |
| 104 | for value in args.value.split(): |
| 105 | getattr(pb, args.key).append(value) |
| 106 | else: |
| 107 | setattr(pb, args.key, args.value) |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 108 | |
Kiyoung Kim | 3df5f50 | 2023-09-27 11:26:58 +0900 | [diff] [blame^] | 109 | ValidateAndWriteAsPbFile(pb, args.output) |
| 110 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 111 | |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 112 | |
Jooyung Han | e134d09 | 2021-04-15 05:13:34 +0900 | [diff] [blame] | 113 | def Merge(args): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 114 | pb = linker_config_pb2.LinkerConfig() |
| 115 | for other in args.input: |
| 116 | with open(other, 'rb') as f: |
| 117 | pb.MergeFromString(f.read()) |
Jooyung Han | e134d09 | 2021-04-15 05:13:34 +0900 | [diff] [blame] | 118 | |
Kiyoung Kim | 3df5f50 | 2023-09-27 11:26:58 +0900 | [diff] [blame^] | 119 | ValidateAndWriteAsPbFile(pb, args.output) |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 120 | |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 121 | |
Jooyung Han | 4bc1026 | 2023-09-08 11:51:45 +0900 | [diff] [blame] | 122 | def Validate(args): |
| 123 | if os.path.isdir(args.input): |
| 124 | config_file = os.path.join(args.input, 'etc/linker.config.pb') |
| 125 | if os.path.exists(config_file): |
| 126 | args.input = config_file |
| 127 | Validate(args) |
| 128 | # OK if there's no linker config file. |
| 129 | return |
| 130 | |
| 131 | if not os.path.isfile(args.input): |
| 132 | sys.exit(f"{args.input} is not a file") |
| 133 | |
| 134 | pb = linker_config_pb2.LinkerConfig() |
| 135 | with open(args.input, 'rb') as f: |
| 136 | pb.ParseFromString(f.read()) |
| 137 | |
| 138 | if args.type == 'apex': |
| 139 | # Shouldn't use provideLibs/requireLibs in APEX linker.config.pb |
| 140 | if getattr(pb, 'provideLibs'): |
| 141 | sys.exit(f'{args.input}: provideLibs is set. Use provideSharedLibs in apex_manifest') |
| 142 | if getattr(pb, 'requireLibs'): |
| 143 | sys.exit(f'{args.input}: requireLibs is set. Use requireSharedLibs in apex_manifest') |
| 144 | elif args.type == 'system': |
| 145 | if getattr(pb, 'visible'): |
| 146 | sys.exit(f'{args.input}: do not use visible, which is for APEX') |
| 147 | if getattr(pb, 'permittedPaths'): |
| 148 | sys.exit(f'{args.input}: do not use permittedPaths, which is for APEX') |
| 149 | else: |
| 150 | sys.exit(f'Unknown type: {args.type}') |
| 151 | |
| 152 | |
Kiyoung Kim | 3df5f50 | 2023-09-27 11:26:58 +0900 | [diff] [blame^] | 153 | def ValidateAndWriteAsPbFile(pb, output_path): |
| 154 | ValidateConfiguration(pb) |
| 155 | with open(output_path, 'wb') as f: |
| 156 | f.write(pb.SerializeToString()) |
| 157 | |
| 158 | |
| 159 | def ValidateConfiguration(pb): |
| 160 | """ |
| 161 | Validate if the configuration is valid to be used as linker configuration |
| 162 | """ |
| 163 | |
| 164 | # Validate if provideLibs and requireLibs have common module |
| 165 | provideLibs = set(getattr(pb, 'provideLibs')) |
| 166 | requireLibs = set(getattr(pb, 'requireLibs')) |
| 167 | |
| 168 | intersectLibs = provideLibs.intersection(requireLibs) |
| 169 | |
| 170 | if intersectLibs: |
| 171 | for lib in intersectLibs: |
| 172 | print(f'{lib} exists both in requireLibs and provideLibs', file=sys.stderr) |
| 173 | sys.exit(1) |
| 174 | |
| 175 | |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 176 | def GetArgParser(): |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 177 | parser = argparse.ArgumentParser() |
| 178 | subparsers = parser.add_subparsers() |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 179 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 180 | parser_proto = subparsers.add_parser( |
| 181 | 'proto', |
| 182 | help='Convert the input JSON configuration file into protobuf.') |
| 183 | parser_proto.add_argument( |
| 184 | '-s', |
| 185 | '--source', |
Jooyung Han | b531bee | 2023-03-04 08:28:40 +0900 | [diff] [blame] | 186 | nargs='?', |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 187 | type=str, |
Jooyung Han | 014ccd4 | 2023-01-09 16:23:14 +0900 | [diff] [blame] | 188 | help='Colon-separated list of linker configuration files in JSON.') |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 189 | parser_proto.add_argument( |
| 190 | '-o', |
| 191 | '--output', |
| 192 | required=True, |
| 193 | type=str, |
| 194 | help='Target path to create protobuf file.') |
Jooyung Han | 3397b6a | 2023-03-08 05:44:17 +0900 | [diff] [blame] | 195 | option_for_existing_output = parser_proto.add_mutually_exclusive_group() |
| 196 | option_for_existing_output.add_argument( |
| 197 | '-f', |
| 198 | '--force', |
| 199 | action='store_true', |
| 200 | help='Overwrite if the output file exists.') |
| 201 | option_for_existing_output.add_argument( |
| 202 | '-a', |
| 203 | '--append', |
| 204 | action='store_true', |
| 205 | help='Append the input to the output file if the output file exists.') |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 206 | parser_proto.set_defaults(func=Proto) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 207 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 208 | print_proto = subparsers.add_parser( |
| 209 | 'print', help='Print configuration in human-readable text format.') |
| 210 | print_proto.add_argument( |
| 211 | '-s', |
| 212 | '--source', |
| 213 | required=True, |
| 214 | type=str, |
| 215 | help='Source linker configuration file in protobuf.') |
| 216 | print_proto.set_defaults(func=Print) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 217 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 218 | system_provide_libs = subparsers.add_parser( |
| 219 | 'systemprovide', |
| 220 | help='Append system provide libraries into the configuration.') |
| 221 | system_provide_libs.add_argument( |
| 222 | '-s', |
| 223 | '--source', |
| 224 | required=True, |
| 225 | type=str, |
| 226 | help='Source linker configuration file in protobuf.') |
| 227 | system_provide_libs.add_argument( |
| 228 | '-o', |
| 229 | '--output', |
| 230 | required=True, |
| 231 | type=str, |
| 232 | help='Target linker configuration file to write in protobuf.') |
| 233 | system_provide_libs.add_argument( |
| 234 | '--value', |
| 235 | required=True, |
| 236 | type=str, |
| 237 | help='Values of the libraries to append. If there are more than one ' |
| 238 | 'it should be separated by empty space' |
| 239 | ) |
| 240 | system_provide_libs.add_argument( |
| 241 | '--system', required=True, type=str, help='Path of the system image.') |
| 242 | system_provide_libs.set_defaults(func=SystemProvide) |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 243 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 244 | append = subparsers.add_parser( |
| 245 | 'append', help='Append value(s) to given key.') |
| 246 | append.add_argument( |
| 247 | '-s', |
| 248 | '--source', |
| 249 | required=True, |
| 250 | type=str, |
| 251 | help='Source linker configuration file in protobuf.') |
| 252 | append.add_argument( |
| 253 | '-o', |
| 254 | '--output', |
| 255 | required=True, |
| 256 | type=str, |
| 257 | help='Target linker configuration file to write in protobuf.') |
| 258 | append.add_argument('--key', required=True, type=str, help='.') |
| 259 | append.add_argument( |
| 260 | '--value', |
| 261 | required=True, |
| 262 | type=str, |
| 263 | help='Values of the libraries to append. If there are more than one' |
| 264 | 'it should be separated by empty space' |
| 265 | ) |
| 266 | append.set_defaults(func=Append) |
Kiyoung Kim | 4ee686d | 2020-12-03 15:20:07 +0900 | [diff] [blame] | 267 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 268 | append = subparsers.add_parser('merge', help='Merge configurations') |
| 269 | append.add_argument( |
| 270 | '-o', |
| 271 | '--out', |
| 272 | required=True, |
| 273 | type=str, |
| 274 | help='Output linker configuration file to write in protobuf.') |
| 275 | append.add_argument( |
| 276 | '-i', |
| 277 | '--input', |
| 278 | nargs='+', |
| 279 | type=str, |
| 280 | help='Linker configuration files to merge.') |
| 281 | append.set_defaults(func=Merge) |
Jooyung Han | e134d09 | 2021-04-15 05:13:34 +0900 | [diff] [blame] | 282 | |
Jooyung Han | 4bc1026 | 2023-09-08 11:51:45 +0900 | [diff] [blame] | 283 | validate = subparsers.add_parser('validate', help='Validate configuration') |
| 284 | validate.add_argument( |
| 285 | '--type', |
| 286 | required=True, |
| 287 | choices=['apex', 'system'], |
| 288 | help='Type of linker configuration') |
| 289 | validate.add_argument( |
| 290 | 'input', |
| 291 | help='Input can be a directory which has etc/linker.config.pb or a path' |
| 292 | ' to the linker config file') |
| 293 | validate.set_defaults(func=Validate) |
| 294 | |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 295 | return parser |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 296 | |
| 297 | |
| 298 | def main(): |
Jooyung Han | 3397b6a | 2023-03-08 05:44:17 +0900 | [diff] [blame] | 299 | parser = GetArgParser() |
| 300 | args = parser.parse_args() |
| 301 | if 'func' in args: |
| 302 | args.func(args) |
| 303 | else: |
| 304 | parser.print_help() |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 305 | |
| 306 | |
| 307 | if __name__ == '__main__': |
Spandan Das | 1d4bfd8 | 2021-08-25 22:58:46 +0000 | [diff] [blame] | 308 | main() |