1ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner// Copyright 2014 The Android Open Source Project
2ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner//
3ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner// This software is licensed under the terms of the GNU General Public
4ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner// License version 2, as published by the Free Software Foundation, and
5ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner// may be copied, distributed, and modified under those terms.
6ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner//
7ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner// This program is distributed in the hope that it will be useful,
8ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner// but WITHOUT ANY WARRANTY; without even the implied warranty of
9ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner// GNU General Public License for more details.
11ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner
12ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner#include "android/utils/host_bitness.h"
13ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner
14ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner#ifdef _WIN32
15ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner#include <windows.h>
16ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner#else
17ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner#include <stdlib.h>
18ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner#endif
19ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner
20ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turnerint android_getHostBitness(void) {
21ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner#ifdef _WIN32
22ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner    char directory[900];
23ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner
24ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner    // Retrieves the path of the WOW64 system directory, which doesn't
25ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner    // exist on 32-bit systems.
26ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner    unsigned len = GetSystemWow64Directory(directory, sizeof(directory));
27ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner    if (len == 0) {
28ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner        return 32;
29ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner    } else {
30ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner        return 64;
31ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner    }
32ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner#else // !_WIN32
33ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner  /*
34ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner     This function returns 64 if host is running 64-bit OS, or 32 otherwise.
35ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner
36ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner     It uses the same technique in ndk/build/core/ndk-common.sh.
37ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner     Here are comments from there:
38ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner
39ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner  ## On Linux or Darwin, a 64-bit kernel (*) doesn't mean that the user-land
40ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner  ## is always 32-bit, so use "file" to determine the bitness of the shell
41ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner  ## that invoked us. The -L option is used to de-reference symlinks.
42ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner  ##
43ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner  ## Note that on Darwin, a single executable can contain both x86 and
44ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner  ## x86_64 machine code, so just look for x86_64 (darwin) or x86-64 (Linux)
45ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner  ## in the output.
46ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner
47ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner    (*) ie. The following code doesn't always work:
48ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner        struct utsname u;
49ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner        int host_runs_64bit_OS = (uname(&u) == 0 && strcmp(u.machine, "x86_64") == 0);
50ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner  */
51ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner    return system("file -L \"$SHELL\" | grep -q \"x86[_-]64\"") == 0 ? 64 : 32;
52ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner#endif // !_WIN32
53ee57375c96822790cc7f837b4fdf64a9c1d69b3aDavid 'Digit' Turner}
54