1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tradefed.util; 18 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.device.ITestDevice; 21 22 import java.util.regex.Matcher; 23 import java.util.regex.Pattern; 24 25 /** 26 * Utility class for abi. 27 */ 28 public class AbiFormatter { 29 30 private static final String PRODUCT_CPU_ABILIST_KEY = "ro.product.cpu.abilist"; 31 private static final String PRODUCT_CPU_ABI_KEY = "ro.product.cpu.abi"; 32 public static final String FORCE_ABI_STRING = "force-abi"; 33 public static final String FORCE_ABI_DESCRIPTION = 34 "The abi bitness to use, can be either 32 or 64."; 35 36 /** 37 * Special marker to be used as a placeholder in strings, that can be then 38 * replaced with the help of {@link #formatCmdForAbi}. 39 */ 40 static final String ABI_REGEX = "\\|#ABI(\\d*)#\\|"; 41 42 /** 43 * Helper method that formats a given string to include abi specific 44 * values to it by replacing a given marker. 45 * 46 * @param str {@link String} to format which includes special markers | 47 * {@value #ABI_REGEX} to be replaced 48 * @param abi {@link String} of the abi we desire to run on. 49 * @return formatted string. 50 */ formatCmdForAbi(String str, String abi)51 public static String formatCmdForAbi(String str, String abi) { 52 // If the abi is not set or null, do nothing. This is to maintain backward compatibility. 53 if (str == null) { 54 return null; 55 } 56 if (abi == null) { 57 return str.replaceAll(ABI_REGEX, ""); 58 } 59 StringBuffer sb = new StringBuffer(); 60 Matcher m = Pattern.compile(ABI_REGEX).matcher(str); 61 while (m.find()) { 62 if (m.group(1).equals(abi)) { 63 m.appendReplacement(sb, ""); 64 } else { 65 m.appendReplacement(sb, abi); 66 } 67 } 68 m.appendTail(sb); 69 return sb.toString(); 70 } 71 72 /** 73 * Helper method to get the default abi name for the given bitness 74 * 75 * @param device 76 * @param bitness 77 * @return the default abi name for the given abi. Returns null if something went wrong. 78 * @throws DeviceNotAvailableException 79 */ getDefaultAbi(ITestDevice device, String bitness)80 public static String getDefaultAbi(ITestDevice device, String bitness) 81 throws DeviceNotAvailableException { 82 String []abis = getSupportedAbis(device, bitness); 83 if (abis != null && abis.length > 0 && abis[0] != null && abis[0].length() > 0) { 84 return abis[0]; 85 } 86 return null; 87 } 88 89 /** 90 * Helper method to get the list of supported abis for the given bitness 91 * 92 * @param device 93 * @param bitness 32 or 64 or empty string 94 * @return the supported abi list of that bitness 95 * @throws DeviceNotAvailableException 96 */ getSupportedAbis(ITestDevice device, String bitness)97 public static String[] getSupportedAbis(ITestDevice device, String bitness) 98 throws DeviceNotAvailableException { 99 String abiList = device.getProperty(PRODUCT_CPU_ABILIST_KEY + bitness); 100 if (abiList != null && !abiList.isEmpty()) { 101 String []abis = abiList.split(","); 102 if (abis.length > 0) { 103 return abis; 104 } 105 } 106 // fallback plan for before lmp, the bitness is ignored 107 String legacyProperty = device.getProperty(PRODUCT_CPU_ABI_KEY); 108 if (legacyProperty != null) { 109 return new String[] {legacyProperty}; 110 } 111 return new String[] {}; 112 } 113 } 114