android: Support host multilib builds.

The Android build system now supports multilib 32/64-bit host builds as
well as target builds, and Chromium needs to make use of this to
properly support compiling V8 for host. Introduce a GYP_HOST_VAR_PREFIX
variable that works equivalently to GYP_VAR_PREFIX and use it in all the
places where the first/second host architecture must be selected, and
also introduce GYP_HOST_MULTILIB to enable the top level makefile to
specify whether it wants 32-bit or 64-bit host binaries.

BUG=chromium:358141
Review URL: https://codereview.chromium.org/301373002

Patch from Torne <torne@chromium.org>!


git-svn-id: http://gyp.googlecode.com/svn/trunk@1970 78cadc50-ecff-11dd-a971-7dbc132099af
diff --git a/pylib/gyp/generator/android.py b/pylib/gyp/generator/android.py
index 5d8c74e..d7aa902 100644
--- a/pylib/gyp/generator/android.py
+++ b/pylib/gyp/generator/android.py
@@ -188,6 +188,7 @@
     self.WriteLn('LOCAL_MODULE_TAGS := optional')
     if self.toolset == 'host':
       self.WriteLn('LOCAL_IS_HOST_MODULE := true')
+      self.WriteLn('LOCAL_MULTILIB := $(GYP_HOST_MULTILIB)')
     else:
       self.WriteLn('LOCAL_MODULE_TARGET_ARCH := '
                    '$(TARGET_$(GYP_VAR_PREFIX)ARCH)')
@@ -195,7 +196,7 @@
     # Grab output directories; needed for Actions and Rules.
     if self.toolset == 'host':
       self.WriteLn('gyp_intermediate_dir := '
-                   '$(call local-intermediates-dir)')
+                   '$(call local-intermediates-dir,,$(GYP_HOST_VAR_PREFIX))')
     else:
       self.WriteLn('gyp_intermediate_dir := '
                    '$(call local-intermediates-dir,,$(GYP_VAR_PREFIX))')
@@ -694,14 +695,15 @@
       path = '$(gyp_shared_intermediate_dir)'
     elif self.type == 'shared_library':
       if self.toolset == 'host':
-        path = '$(HOST_OUT_INTERMEDIATE_LIBRARIES)'
+        path = '$($(GYP_HOST_VAR_PREFIX)HOST_OUT_INTERMEDIATE_LIBRARIES)'
       else:
         path = '$($(GYP_VAR_PREFIX)TARGET_OUT_INTERMEDIATE_LIBRARIES)'
     else:
       # Other targets just get built into their intermediate dir.
       if self.toolset == 'host':
-        path = '$(call intermediates-dir-for,%s,%s,true)' % (self.android_class,
-                                                            self.android_module)
+        path = ('$(call intermediates-dir-for,%s,%s,true,,'
+                '$(GYP_HOST_VAR_PREFIX))' % (self.android_class,
+                                             self.android_module))
       else:
         path = ('$(call intermediates-dir-for,%s,%s,,,$(GYP_VAR_PREFIX))'
                 % (self.android_class, self.android_module))
@@ -887,6 +889,8 @@
       self.WriteLn('LOCAL_UNINSTALLABLE_MODULE := true')
       if self.toolset == 'target':
         self.WriteLn('LOCAL_2ND_ARCH_VAR_PREFIX := $(GYP_VAR_PREFIX)')
+      else:
+        self.WriteLn('LOCAL_2ND_ARCH_VAR_PREFIX := $(GYP_HOST_VAR_PREFIX)')
       self.WriteLn()
       self.WriteLn('include $(BUILD_SYSTEM)/base_rules.mk')
       self.WriteLn()
@@ -894,9 +898,8 @@
       self.WriteLn('\t$(hide) echo "Gyp timestamp: $@"')
       self.WriteLn('\t$(hide) mkdir -p $(dir $@)')
       self.WriteLn('\t$(hide) touch $@')
-      if self.toolset == 'target':
-        self.WriteLn()
-        self.WriteLn('LOCAL_2ND_ARCH_VAR_PREFIX :=')
+      self.WriteLn()
+      self.WriteLn('LOCAL_2ND_ARCH_VAR_PREFIX :=')
 
 
   def WriteList(self, value_list, variable=None, prefix='',
@@ -1080,6 +1083,8 @@
 
   root_makefile.write('GYP_CONFIGURATION ?= %s\n' % default_configuration)
   root_makefile.write('GYP_VAR_PREFIX ?=\n')
+  root_makefile.write('GYP_HOST_VAR_PREFIX ?=\n')
+  root_makefile.write('GYP_HOST_MULTILIB ?=\n')
 
   # Write out the sorted list of includes.
   root_makefile.write('\n')
diff --git a/test/android/32or64.c b/test/android/32or64.c
new file mode 100644
index 0000000..3ea8169
--- /dev/null
+++ b/test/android/32or64.c
@@ -0,0 +1,13 @@
+/* Copyright (c) 2014 Google Inc. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+const char* getString()
+{
+#ifdef __LP64__
+  return "Hello, 64-bit world!\n";
+#else
+  return "Hello, 32-bit world!\n";
+#endif
+}
diff --git a/test/android/gyptest-host-multilib.py b/test/android/gyptest-host-multilib.py
new file mode 100755
index 0000000..4fb7919
--- /dev/null
+++ b/test/android/gyptest-host-multilib.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2014 Google Inc. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""
+Verifies that it's possible to build host targets correctly in a multilib
+configuration, explicitly forcing either 32 or 64 bit.
+"""
+
+import TestGyp
+
+test = TestGyp.TestGyp(formats=['android'])
+
+test.run_gyp('host_32or64.gyp')
+
+# Force building as 32-bit
+test.build('host_32or64.gyp', 'generate_output',
+           arguments=['GYP_HOST_VAR_PREFIX=$(HOST_2ND_ARCH_VAR_PREFIX)',
+                      'GYP_HOST_MULTILIB=32'])
+
+test.built_file_must_match('host_32or64.output', 'Hello, 32-bit world!\n')
+
+# Force building as 64-bit
+test.build('host_32or64.gyp', 'generate_output',
+           arguments=['GYP_HOST_VAR_PREFIX=',
+                      'GYP_HOST_MULTILIB=64'])
+
+test.built_file_must_match('host_32or64.output', 'Hello, 64-bit world!\n')
+
+test.pass_test()
diff --git a/test/android/host_32or64.gyp b/test/android/host_32or64.gyp
new file mode 100644
index 0000000..e8ca79f
--- /dev/null
+++ b/test/android/host_32or64.gyp
@@ -0,0 +1,38 @@
+# Copyright (c) 2014 Google Inc. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+{
+  'targets': [
+    {
+      'target_name': 'lib32or64',
+      'type': 'static_library',
+      'toolsets': [ 'host' ],
+      'sources': [
+        '32or64.c',
+      ],
+    },
+    {
+      'target_name': 'host_32or64',
+      'type': 'executable',
+      'toolsets': [ 'host' ],
+      'dependencies': [ 'lib32or64#host' ],
+      'sources': [
+        'writefile.c',
+      ],
+    },
+    {
+      'target_name': 'generate_output',
+      'type': 'none',
+      'dependencies': [ 'host_32or64#host' ],
+      'actions': [
+        {
+          'action_name': 'generate',
+          'inputs': [ '<(PRODUCT_DIR)/host_32or64' ],
+          'outputs': [ '<(PRODUCT_DIR)/host_32or64.output' ],
+          'action': [ '<@(_inputs)', '<@(_outputs)' ],
+        }
+      ],
+    },
+  ],
+}
diff --git a/test/android/writefile.c b/test/android/writefile.c
new file mode 100644
index 0000000..3f5960f
--- /dev/null
+++ b/test/android/writefile.c
@@ -0,0 +1,18 @@
+/* Copyright (c) 2014 Google Inc. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <stdio.h>
+
+extern const char* getString(void);
+
+int main(int argc, char* argv[])
+{
+  if (argc < 2) return 2;
+  FILE* f = fopen(argv[1], "w");
+  if (f == NULL) return 1;
+  fprintf(f, "%s", getString());
+  fclose(f);
+  return 0;
+}