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