Merge "resolve merge conflicts of c5f5903 to master."
diff --git a/Makefile.common b/Makefile.common
index 12fe3bd..2a077bf 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -136,6 +136,7 @@
 	android/utils/eintr_wrapper.c \
 	android/utils/filelock.c \
 	android/utils/file_data.c \
+	android/utils/host_bitness.c \
 	android/utils/ini.c \
 	android/utils/intmap.c \
 	android/utils/lineinput.c \
diff --git a/Makefile.tests b/Makefile.tests
index a122684..5ff4c56 100644
--- a/Makefile.tests
+++ b/Makefile.tests
@@ -6,6 +6,7 @@
   android/utils/bufprint_unittest.cpp \
   android/utils/eintr_wrapper_unittest.cpp \
   android/utils/file_data_unittest.cpp \
+  android/utils/host_bitness_unittest.cpp \
   android/utils/property_file_unittest.cpp \
   android/utils/win32_cmdline_quote_unittest.cpp \
   android/base/containers/PodVector_unittest.cpp \
diff --git a/android/main-emulator.c b/android/main-emulator.c
index 26d8ae6..ef2b4b0 100644
--- a/android/main-emulator.c
+++ b/android/main-emulator.c
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <android/utils/compiler.h>
+#include <android/utils/host_bitness.h>
 #include <android/utils/panic.h>
 #include <android/utils/path.h>
 #include <android/utils/bufprint.h>
@@ -200,32 +201,6 @@
     return errno;
 }
 
-#ifndef _WIN32
-static int
-getHostOSBitness()
-{
-  /*
-     This function returns 64 if host is running 64-bit OS, or 32 otherwise.
-
-     It uses the same technique in ndk/build/core/ndk-common.sh.
-     Here are comments from there:
-
-  ## On Linux or Darwin, a 64-bit kernel (*) doesn't mean that the user-land
-  ## is always 32-bit, so use "file" to determine the bitness of the shell
-  ## that invoked us. The -L option is used to de-reference symlinks.
-  ##
-  ## Note that on Darwin, a single executable can contain both x86 and
-  ## x86_64 machine code, so just look for x86_64 (darwin) or x86-64 (Linux)
-  ## in the output.
-
-    (*) ie. The following code doesn't always work:
-        struct utsname u;
-        int host_runs_64bit_OS = (uname(&u) == 0 && strcmp(u.machine, "x86_64") == 0);
-  */
-    return system("file -L \"$SHELL\" | grep -q \"x86[_-]64\"") == 0 ? 64 : 32;
-}
-#endif  // !_WIN32
-
 /* Find the target-specific emulator binary. This will be something
  * like  <programDir>/emulator-<targetArch>, where <programDir> is
  * the directory of the current program.
@@ -244,7 +219,8 @@
     int search_for_64bit_emulator = 0;
 #else
     const char* exeExt = "";
-    int search_for_64bit_emulator = !force_32bit && getHostOSBitness() == 64;
+    int search_for_64bit_emulator =
+            !force_32bit && android_getHostBitness() == 64;
 #endif
 
     const char* emulatorSuffix = emulator_getBackendSuffix(avdArch);
diff --git a/android/utils/host_bitness.c b/android/utils/host_bitness.c
new file mode 100644
index 0000000..b48848f
--- /dev/null
+++ b/android/utils/host_bitness.c
@@ -0,0 +1,53 @@
+// Copyright 2014 The Android Open Source Project
+//
+// This software is licensed under the terms of the GNU General Public
+// License version 2, as published by the Free Software Foundation, and
+// may be copied, distributed, and modified under those terms.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+#include "android/utils/host_bitness.h"
+
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <stdlib.h>
+#endif
+
+int android_getHostBitness(void) {
+#ifdef _WIN32
+    char directory[900];
+
+    // Retrieves the path of the WOW64 system directory, which doesn't
+    // exist on 32-bit systems.
+    unsigned len = GetSystemWow64Directory(directory, sizeof(directory));
+    if (len == 0) {
+        return 32;
+    } else {
+        return 64;
+    }
+#else // !_WIN32
+  /*
+     This function returns 64 if host is running 64-bit OS, or 32 otherwise.
+
+     It uses the same technique in ndk/build/core/ndk-common.sh.
+     Here are comments from there:
+
+  ## On Linux or Darwin, a 64-bit kernel (*) doesn't mean that the user-land
+  ## is always 32-bit, so use "file" to determine the bitness of the shell
+  ## that invoked us. The -L option is used to de-reference symlinks.
+  ##
+  ## Note that on Darwin, a single executable can contain both x86 and
+  ## x86_64 machine code, so just look for x86_64 (darwin) or x86-64 (Linux)
+  ## in the output.
+
+    (*) ie. The following code doesn't always work:
+        struct utsname u;
+        int host_runs_64bit_OS = (uname(&u) == 0 && strcmp(u.machine, "x86_64") == 0);
+  */
+    return system("file -L \"$SHELL\" | grep -q \"x86[_-]64\"") == 0 ? 64 : 32;
+#endif // !_WIN32
+}
diff --git a/android/utils/host_bitness.h b/android/utils/host_bitness.h
new file mode 100644
index 0000000..f239109
--- /dev/null
+++ b/android/utils/host_bitness.h
@@ -0,0 +1,26 @@
+// Copyright 2014 The Android Open Source Project
+//
+// This software is licensed under the terms of the GNU General Public
+// License version 2, as published by the Free Software Foundation, and
+// may be copied, distributed, and modified under those terms.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+#ifndef ANDROID_UTILS_HOST_BITNESS_H
+#define ANDROID_UTILS_HOST_BITNESS_H
+
+#include "android/utils/compiler.h"
+
+ANDROID_BEGIN_HEADER
+
+// Return the bitness of the host machine this program is running on.
+// This is independent of whether the current executable is a 32 or 64
+// bit binary. Returns 32 or 64.
+int android_getHostBitness(void);
+
+ANDROID_END_HEADER
+
+#endif  // ANDROID_UTILS_HOST_BITNESS_H
\ No newline at end of file
diff --git a/android/utils/host_bitness_unittest.cpp b/android/utils/host_bitness_unittest.cpp
new file mode 100644
index 0000000..32f4715
--- /dev/null
+++ b/android/utils/host_bitness_unittest.cpp
@@ -0,0 +1,23 @@
+// Copyright 2014 The Android Open Source Project
+//
+// This software is licensed under the terms of the GNU General Public
+// License version 2, as published by the Free Software Foundation, and
+// may be copied, distributed, and modified under those terms.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+#include "android/utils/host_bitness.h"
+
+#include <gtest/gtest.h>
+
+#include <stdio.h>
+
+TEST(HostBitness, android_getHostBitness) {
+    int host_bitness = android_getHostBitness();
+    printf("Found host bitness: %d\n", host_bitness);
+    EXPECT_TRUE(host_bitness == 32 || host_bitness == 64) <<
+        "Invalid host bitness: " << host_bitness;
+}