1#!/usr/bin/env python3 2# 3# Copyright (C) 2022 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 run bpmodify for a given module in order to rename it and all references to it""" 17import os 18import subprocess 19import sys 20 21 22def main(): 23 if len(sys.argv) < 2: 24 print("Usage: rename_module_and_deps <pathToModule1,pathToModule2,...>") 25 return 26 27 modulePaths = sys.argv[1].split(",") 28 replacementsList = [] 29 colonReplacementsList = [] 30 31 for modulePath in modulePaths: 32 moduleName = modulePath.split("/")[-1] 33 replacementsList.append(moduleName + "=" + moduleName + "_lib") 34 # add in the colon replacement 35 colonReplaceString = ":" + moduleName + "=" + ":" + moduleName + "_lib" 36 replacementsList.append(colonReplaceString) 37 colonReplacementsList.append(colonReplaceString) 38 39 replacementsString = ",".join(replacementsList) 40 colonReplacementsString = ",".join(colonReplacementsList) 41 buildTop = os.getenv("ANDROID_BUILD_TOP") 42 43 if not buildTop: 44 raise Exception( 45 "$ANDROID_BUILD_TOP not found in environment. Have you run lunch?") 46 47 rename_deps_cmd = f"{buildTop}/prebuilts/go/linux-x86/bin/go run bpmodify.go -w -m=* -property=static_libs,deps,required,test_suites,name,host,libs,data_bins,data_native_bins,tools,shared_libs,file_contexts,target.not_windows.required,target.android.required,target.platform.required -replace-property={replacementsString} {buildTop}" 48 print(rename_deps_cmd) 49 subprocess.check_output(rename_deps_cmd, shell=True) 50 51 # Some properties (for example, data ), refer to files. Such properties may also refer to a filegroup module by prefixing it with a colon. Replacing these module references must thus be done separately. 52 colon_rename_deps_cmd = f"{buildTop}/prebuilts/go/linux-x86/bin/go run bpmodify.go -w -m=* -property=data -replace-property={colonReplacementsString} {buildTop}" 53 print(colon_rename_deps_cmd) 54 subprocess.check_output(colon_rename_deps_cmd, shell=True) 55 56 57if __name__ == "__main__": 58 main() 59