Add big endian support

Add a script that generates an assembly file from a .dat file. This is
needed for generating big endian assembly file after using icupkg to
convert little endian icudtl.dat to big endian icudtb.dat. Also the
icu.gyp file is modified so big endian architectures use appropriate
files.

Patch by miran.karic@  ( https://codereview.chromium.org/1967523002/)
with a couple of fixes:

1. Two errors mentioned against PS#9 in the above CL.
2. Support copying icu data file for Big Endian targets.

Besides, icudtb.dat was added to common. icudtb.dat was created by
running 'icupkg -tb icudt56l.dat icudt56b.dat' and renaming icudt56b.dat
to icudtb.dat.

BUG=v8:4828
TEST='d8' is built correct with icu_use_data_file set to either 0 or 1.
TEST=run `GYP_DEFINES="target_arch=mips" ./gypfiles/gyp_v8` and make sure
that ninja files use 'b' data/assembly file for Big Endian.

Review URL: https://codereview.chromium.org/2162393003 .

Patch from Miran Karic <miran.karic@imgtec.com>.
diff --git a/common/icudtb.dat b/common/icudtb.dat
new file mode 100644
index 0000000..b2a2959
--- /dev/null
+++ b/common/icudtb.dat
Binary files differ
diff --git a/icu.gyp b/icu.gyp
index b2bf1dd..9806c9b 100644
--- a/icu.gyp
+++ b/icu.gyp
@@ -45,7 +45,7 @@
       ['(OS=="linux" or OS=="freebsd" or OS=="openbsd" or OS=="solaris" \
          or OS=="netbsd" or OS=="mac" or OS=="android" or OS=="qnx") and \
         (target_arch=="arm" or target_arch=="ia32" or \
-         target_arch=="mipsel")', {
+         target_arch=="mipsel" or target_arch=="mips")', {
         'target_conditions': [
           ['_toolset=="host"', {
             'cflags': [ '-m32' ],
@@ -60,7 +60,7 @@
       ['(OS=="linux" or OS=="freebsd" or OS=="openbsd" or OS=="solaris" \
          or OS=="netbsd" or OS=="mac" or OS=="android" or OS=="qnx") and \
         (target_arch=="arm64" or target_arch=="x64" or \
-         target_arch=="mipsel64")', {
+         target_arch=="mips64el" or target_arch=="mips64")', {
         'target_conditions': [
           ['_toolset=="host"', {
             'cflags': [ '-m64' ],
@@ -83,12 +83,12 @@
     ['use_system_icu==0 or want_separate_host_toolset==1', {
       'targets': [
         {
-          'target_name': 'copy_icudtl_dat',
+          'target_name': 'copy_icudt_dat',
           'type': 'none',
           # icudtl.dat is the same for both host/target, so this only supports a
           # single toolset. If a target requires that the .dat file be copied
           # to the output directory, it should explicitly depend on this target
-          # with the host toolset (like copy_icudtl_dat#host).
+          # with the host toolset (like copy_icudt_dat#host).
           'toolsets': [ 'host' ],
           'copies': [{
             'destination': '<(PRODUCT_DIR)',
@@ -98,29 +98,93 @@
                   'android/icudtl.dat',
                 ],
               } , { # else: OS != android
-                'files': [
-                  'common/icudtl.dat',
+                'conditions': [
+                  # Big Endian
+                  [ 'target_arch=="mips" or target_arch=="mips64"', {
+                    'files': [
+                      'common/icudtb.dat',
+                    ],
+                  } , {  # else: ! Big Endian = Little Endian
+                    'files': [
+                      'common/icudtl.dat',
+                    ],
+                  }],
                 ],
               }],
             ],
           }],
         },
         {
+          'target_name': 'data_assembly',
+          'type': 'none',
+          'conditions': [
+            [ 'target_arch=="mips" or target_arch=="mips64"', {
+              'data_assembly_sources': [
+                'common/icudtb.dat',
+              ],
+              'data_assembly_inputs': [
+                'scripts/make_data_assembly.py',
+                'common/icudtb.dat',
+              ],
+              'data_assembly_outputs': [
+                '<(SHARED_INTERMEDIATE_DIR)/third_party/icu/icudtb_dat.S',
+              ],
+            }],
+            [ 'target_arch!="mips" and target_arch!="mips64"', {
+              'data_assembly_sources': [
+                'common/icudtl.dat',
+              ],
+              'data_assembly_inputs': [
+                'scripts/make_data_assembly.py',
+                'common/icudtl.dat',
+              ],
+              'data_assembly_outputs': [
+                '<(SHARED_INTERMEDIATE_DIR)/third_party/icu/icudtl_dat.S',
+              ],
+            }],
+          ],
+          'sources': [
+            '<@(_data_assembly_sources)',
+          ],
+          'actions': [
+            {
+              'action_name': 'make_data_assembly',
+              'inputs': [
+                '<@(_data_assembly_inputs)',
+              ],
+              'outputs': [
+                '<@(_data_assembly_outputs)',
+              ],
+              'action': ['python', '<@(_inputs)', '<@(_outputs)'],
+            },
+          ],
+        },
+        {
           'target_name': 'icudata',
           'type': 'static_library',
           'defines': [
             'U_HIDE_DATA_SYMBOL',
           ],
+          'dependencies': [
+            'data_assembly#target',
+          ],
           'sources': [
              # These are hand-generated, but will do for now.  The linux
              # version is an identical copy of the (mac) icudtl_dat.S file,
              # modulo removal of the .private_extern and .const directives and
              # with no leading underscore on the icudt52_dat symbol.
              'android/icudtl_dat.S',
-             'linux/icudtl_dat.S',
+             '<(SHARED_INTERMEDIATE_DIR)/third_party/icu/icudtl_dat.S',
+             '<(SHARED_INTERMEDIATE_DIR)/third_party/icu/icudtb_dat.S',
              'mac/icudtl_dat.S',
           ],
           'conditions': [
+            [ 'target_arch=="mips" or target_arch=="mips64"', {
+              'sources!': ['<(SHARED_INTERMEDIATE_DIR)/third_party/icu/icudtl_dat.S'],
+            }],
+            [ 'target_arch!="mips" and target_arch!="mips64"', {
+              'sources!': ['<(SHARED_INTERMEDIATE_DIR)/third_party/icu/icudtb_dat.S'],
+            }],
             [ 'use_system_icu==1 and want_separate_host_toolset==1', {
               'toolsets': ['host'],
             }],
@@ -132,6 +196,9 @@
             }],
             [ 'OS == "win" and icu_use_data_file_flag==0', {
               'type': 'none',
+              'dependencies!': [
+                'data_assembly#target',
+              ],
               'copies': [
                 {
                   'destination': '<(PRODUCT_DIR)',
@@ -141,16 +208,25 @@
                 },
               ],
             }],
+            [ '(OS == "mac" or OS == "ios") and icu_use_data_file_flag==0', {
+              'type': 'none',
+              'dependencies!': [
+                'data_assembly#target',
+              ],
+            }],
             [ 'icu_use_data_file_flag==1', {
               'type': 'none',
+              'dependencies!': [
+                'data_assembly#target',
+              ],
               # Remove any assembly data file.
-              'sources/': [['exclude', 'icudtl_dat']],
+              'sources/': [['exclude', 'icudt[lb]_dat']],
 
               # Make sure any binary depending on this gets the data file.
               'conditions': [
                 ['OS != "ios"', {
                   'dependencies': [
-                    'copy_icudtl_dat#host',
+                    'copy_icudt_dat#host',
                   ],
                 } , { # else: OS=="ios"
                   'link_settings': {
@@ -166,7 +242,10 @@
             [ 'OS == "win" or OS == "mac" or OS == "ios" or '
               '(OS == "android" and (_toolset != "host" or host_os != "linux")) or '
               '(OS == "qnx" and (_toolset == "host" and host_os != "linux"))', {
-              'sources!': ['linux/icudtl_dat.S'],
+              'sources!': [
+                '<(SHARED_INTERMEDIATE_DIR)/third_party/icu/icudtl_dat.S',
+                '<(SHARED_INTERMEDIATE_DIR)/third_party/icu/icudtb_dat.S'
+              ],
             }],
             [ 'OS != "android" or _toolset == "host"', {
               'sources!': ['android/icudtl_dat.S'],
diff --git a/scripts/make_data_assembly.py b/scripts/make_data_assembly.py
new file mode 100755
index 0000000..f69a3af
--- /dev/null
+++ b/scripts/make_data_assembly.py
@@ -0,0 +1,73 @@
+#!/usr/bin/python2
+
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import binascii
+import sys
+
+if len(sys.argv) < 2:
+  sys.exit("Usage: %s icu_data_file [output_assembly_file]" % sys.argv[0])
+
+input_file = sys.argv[1]
+n = input_file.find(".dat")
+if n == -1:
+  sys.exit("%s is not an ICU .dat file." % input_file)
+
+if len(sys.argv) < 3:
+  output_file = input_file[0:n] + "_dat.S"
+else:
+  output_file = sys.argv[2]
+
+if input_file.find("l.dat") == -1:
+  if input_file.find("b.dat") == -1:
+    sys.exit("%s has no endianness marker." % input_file)
+  else:
+    step = 1
+else:
+  step = -1
+
+input_data = open(input_file, 'rb').read()
+n = input_data.find("icudt")
+if n == -1:
+  sys.exit("Cannot find a version number in %s." % input_file)
+
+version_number = input_data[n + 5:n + 7]
+
+output = open(output_file, 'w')
+output.write(".globl icudt" + version_number + "_dat\n"
+             "\t.section .note.GNU-stack,\"\",%progbits\n"
+             "\t.section .rodata\n"
+             "\t.balign 16\n"
+             "#ifdef U_HIDE_DATA_SYMBOL\n"
+             "\t.hidden icudt" + version_number + "_dat\n"
+             "#endif\n"
+             "\t.type icudt" + version_number + "_dat,%object\n"
+             "icudt" + version_number + "_dat:\n")
+
+split = [binascii.hexlify(input_data[i:i + 4][::step]).upper().lstrip('0')
+        for i in range(0, len(input_data), 4)]
+
+for i in range(len(split)):
+  if (len(split[i]) == 0):
+    value = '0'
+  elif (len(split[i]) == 1):
+    if not any((c in '123456789') for c in split[i]):
+      value = '0x0' + split[i]
+    else:
+      value = split[i]
+  elif (len(split[i]) % 2 == 1):
+    value = '0x0' + split[i]
+  else:
+    value = '0x' + split[i]
+
+  if (i % 32 == 0):
+    output.write("\n.long ")
+  else:
+    output.write(",")
+  output.write(value)
+
+output.write("\n")
+output.close()
+print "Generated " + output_file