aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/NdkHelper.java
blob: 8ad1f24c4a1785cd392d5d12bc1906d8b2cfc7a1 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
 *
 * 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.ide.eclipse.ndk.internal;

import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.ndk.internal.launch.NdkLaunchConstants;

import org.eclipse.cdt.core.CommandLauncher;
import org.eclipse.cdt.core.ICommandLauncher;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.cdt.dsf.gdb.IGDBLaunchConfigurationConstants;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

@SuppressWarnings("restriction")
public class NdkHelper {
    private static final String MAKE = "make";                                      //$NON-NLS-1$
    private static final String CORE_MAKEFILE_PATH = "/build/core/build-local.mk";  //$NON-NLS-1$

    /**
     * Obtain the ABI's the application is compatible with.
     * The ABI's are obtained by reading the result of the following command:
     * make --no-print-dir -f ${NdkRoot}/build/core/build-local.mk -C <project-root> DUMP_APP_ABI
     */
    public static Collection<NativeAbi> getApplicationAbis(IProject project,
                                                                IProgressMonitor monitor) {
        ICommandLauncher launcher = new CommandLauncher();
        launcher.setProject(project);
        String[] args = new String[] {
            "--no-print-dir",                                           //$NON-NLS-1$
            "-f",                                                       //$NON-NLS-1$
            NdkManager.getNdkLocation() + CORE_MAKEFILE_PATH,
            "-C",                                                       //$NON-NLS-1$
            project.getLocation().toOSString(),
            "DUMP_APP_ABI",                                             //$NON-NLS-1$
        };
        try {
            launcher.execute(getPathToMake(), args, null, project.getLocation(), monitor);
        } catch (CoreException e) {
            AdtPlugin.printErrorToConsole(e.getLocalizedMessage());
            return Collections.emptyList();
        }

        ByteArrayOutputStream stdout = new ByteArrayOutputStream();
        ByteArrayOutputStream stderr = new ByteArrayOutputStream();
        launcher.waitAndRead(stdout, stderr, monitor);

        String abis = stdout.toString().trim();
        Set<NativeAbi> nativeAbis = EnumSet.noneOf(NativeAbi.class);
        for (String abi: abis.split(" ")) {                             //$NON-NLS-1$
            if (abi.equals("all")) {                                    //$NON-NLS-1$
                return EnumSet.allOf(NativeAbi.class);
            }

            try {
                nativeAbis.add(NativeAbi.getByString(abi));
            } catch (IllegalArgumentException e) {
                AdtPlugin.printErrorToConsole(project, "Unknown Application ABI: ", abi);
            }
        }

        return nativeAbis;
    }

    /**
     * Obtain the toolchain prefix to use for given project and abi.
     * The prefix is obtained by reading the result of:
     * make --no-print-dir -f ${NdkRoot}/build/core/build-local.mk \
     *      -C <project-root> \
     *      DUMP_TOOLCHAIN_PREFIX APP_ABI=abi
     */
    public static String getToolchainPrefix(IProject project, NativeAbi abi,
            IProgressMonitor monitor) {
        ICommandLauncher launcher = new CommandLauncher();
        launcher.setProject(project);
        String[] args = new String[] {
            "--no-print-dir",                                           //$NON-NLS-1$
            "-f",                                                       //$NON-NLS-1$
            NdkManager.getNdkLocation() + CORE_MAKEFILE_PATH,
            "-C",                                                       //$NON-NLS-1$
            project.getLocation().toOSString(),
            "DUMP_TOOLCHAIN_PREFIX",                                    //$NON-NLS-1$
            "APP_ABI=" + abi.getAbi(),                                  //$NON-NLS-1$
        };
        try {
            launcher.execute(getPathToMake(), args, null, project.getLocation(), monitor);
        } catch (CoreException e) {
            AdtPlugin.printErrorToConsole(e.getLocalizedMessage());
            return null;
        }

        ByteArrayOutputStream stdout = new ByteArrayOutputStream();
        ByteArrayOutputStream stderr = new ByteArrayOutputStream();
        launcher.waitAndRead(stdout, stderr, monitor);
        return stdout.toString().trim();
    }

    private static IPath getPathToMake() {
        return getFullPathTo(MAKE);
    }

    /**
     * Obtain a path to the utilities prebuilt folder in NDK. This is typically
     * "${NdkRoot}/prebuilt/<platform>/bin/". If the executable is not found, it simply returns
     * the name of the executable (which is equal to assuming that it is available on the path).
     */
    private static synchronized IPath getFullPathTo(String executable) {
        if (Platform.getOS().equals(Platform.OS_WIN32)) {
            executable += ".exe";
        }

        IPath ndkRoot = new Path(NdkManager.getNdkLocation());
        IPath prebuilt = ndkRoot.append("prebuilt");                      //$NON-NLS-1$
        if (!prebuilt.toFile().exists() || !prebuilt.toFile().canRead()) {
            return new Path(executable);
        }

        File[] platforms = prebuilt.toFile().listFiles();
        if (platforms != null) {
            for (File p: platforms) {
                IPath exePath = prebuilt.append(p.getName())
                        .append("bin")          //$NON-NLS-1$
                        .append(executable);
                if (exePath.toFile().exists()) {
                    return exePath;
                }
            }
        }

        return new Path(executable);
    }

    public static void setLaunchConfigDefaults(ILaunchConfigurationWorkingCopy config) {
        config.setAttribute(IGDBLaunchConfigurationConstants.ATTR_REMOTE_TCP, true);
        config.setAttribute(NdkLaunchConstants.ATTR_NDK_GDB, NdkLaunchConstants.DEFAULT_GDB);
        config.setAttribute(IGDBLaunchConfigurationConstants.ATTR_GDB_INIT,
                NdkLaunchConstants.DEFAULT_GDBINIT);
        config.setAttribute(IGDBLaunchConfigurationConstants.ATTR_PORT,
                NdkLaunchConstants.DEFAULT_GDB_PORT);
        config.setAttribute(IGDBLaunchConfigurationConstants.ATTR_HOST, "localhost"); //$NON-NLS-1$
        config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN, false);
        config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_START_MODE,
                IGDBLaunchConfigurationConstants.DEBUGGER_MODE_REMOTE_ATTACH);
        config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME,
                NdkLaunchConstants.DEFAULT_PROGRAM);

        config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_START_MODE,
                IGDBLaunchConfigurationConstants.DEBUGGER_MODE_REMOTE);
        config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_ID,
                "gdbserver"); //$NON-NLS-1$

        List<String> solibPaths = new ArrayList<String>(2);
        solibPaths.add(NdkLaunchConstants.DEFAULT_SOLIB_PATH);
        config.setAttribute(NdkLaunchConstants.ATTR_NDK_SOLIB, solibPaths);
    }
}