aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tradefed/util/AbiFormatter.java
blob: c48879f3b2e178b1ae9ff711b25f7c7abcc4bf47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tradefed.util;

import com.android.ddmlib.IDevice;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.ITestDevice;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utility class for abi.
 */
public class AbiFormatter {

    private static final String PRODUCT_CPU_ABILIST_KEY = "ro.product.cpu.abilist";
    public static final String FORCE_ABI_STRING = "force-abi";
    public static final String FORCE_ABI_DESCRIPTION = "The abi to use, can be either 32 or 64.";

    /**
     * Special marker to be used as a placeholder in strings, that can be then
     * replaced with the help of {@link formatCmdForAbi}.
     */
    static final String ABI_REGEX = "\\|#ABI(\\d*)#\\|";

    /**
     * Helper method that formats a given string to include abi specific
     * values to it by replacing a given marker.
     *
     * @param str {@link String} to format which includes special markers |
     *            {@value #ABI_REGEX} to be replaced
     * @param abi {@link String} of the abi we desire to run on.
     * @return formatted string.
     */
    public static String formatCmdForAbi(String str, String abi) {
        // If the abi is not set or null, do nothing. This is to maintain backward compatibility.
        if (str == null) {
            return null;
        }
        if (abi == null) {
            return str.replaceAll(ABI_REGEX, "");
        }
        StringBuffer sb = new StringBuffer();
        Matcher m = Pattern.compile(ABI_REGEX).matcher(str);
        while (m.find()) {
            if (m.group(1).equals(abi)) {
                m.appendReplacement(sb, "");
            } else {
                m.appendReplacement(sb, abi);
            }
        }
        m.appendTail(sb);
        return sb.toString();
    }

    /**
     * Helper method to get the default abi name for the given abi
     * @param device
     * @param requestedAbi
     * @return the default abi name for the given abi
     * @throws DeviceNotAvailableException
     */
    public static String getDefaultAbi(ITestDevice device, String requestedAbi)
            throws DeviceNotAvailableException {
        String abiList = device.getProperty(PRODUCT_CPU_ABILIST_KEY + requestedAbi);
        if (abiList != null) {
            String []abis = abiList.split(",");
            if (abis.length > 0 && abis[0].length() > 0) {
                return abis[0];
            }
        }
        return null;
    }
}