1b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# Copyright 2012 the V8 project authors. All rights reserved.
2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# Redistribution and use in source and binary forms, with or without
3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# modification, are permitted provided that the following conditions are
4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# met:
5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#
6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#     * Redistributions of source code must retain the above copyright
7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#       notice, this list of conditions and the following disclaimer.
8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#     * Redistributions in binary form must reproduce the above
9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#       copyright notice, this list of conditions and the following
10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#       disclaimer in the documentation and/or other materials provided
11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#       with the distribution.
12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#     * Neither the name of Google Inc. nor the names of its
13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#       contributors may be used to endorse or promote products derived
14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#       from this software without specific prior written permission.
15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#
16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
29b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochimport os
30b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochfrom os.path import exists
31b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochfrom os.path import isdir
32b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochfrom os.path import join
33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockimport platform
34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockimport re
35014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochimport subprocess
36b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochimport urllib2
37a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
39b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochdef GetSuitePaths(test_root):
40b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return [ f for f in os.listdir(test_root) if isdir(join(test_root, f)) ]
41b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
42b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
43b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# Reads a file into an array of strings
44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockdef ReadLinesFrom(name):
45b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  lines = []
46b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  with open(name) as f:
47b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    for line in f:
48b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if line.startswith('#'): continue
49b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if '#' in line:
50b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        line = line[:line.find('#')]
51b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      line = line.strip()
52b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if not line: continue
53b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      lines.append(line)
54b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return lines
55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockdef GuessOS():
58b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  system = platform.system()
59b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if system == 'Linux':
60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return 'linux'
61b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  elif system == 'Darwin':
62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return 'macos'
63b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  elif system.find('CYGWIN') >= 0:
64e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    return 'cygwin'
65b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  elif system == 'Windows' or system == 'Microsoft':
66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    # On Windows Vista platform.system() can return 'Microsoft' with some
67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    # versions of Python, see http://bugs.python.org/issue1082
68b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return 'windows'
69b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  elif system == 'FreeBSD':
70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return 'freebsd'
71b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  elif system == 'OpenBSD':
72d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block    return 'openbsd'
73b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  elif system == 'SunOS':
74d91b9f7d46489a9ee00f9cb415630299c76a502bLeon Clarke    return 'solaris'
75b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  elif system == 'NetBSD':
763ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    return 'netbsd'
77014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  elif system == 'AIX':
78014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    return 'aix'
79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  else:
80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return None
81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
83b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochdef UseSimulator(arch):
84b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  machine = platform.machine()
85b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return (machine and
86b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      (arch == "mipsel" or arch == "arm" or arch == "arm64") and
87b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      not arch.startswith(machine))
88b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
89b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
90b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# This will default to building the 32 bit VM even on machines that are
91b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# capable of running the 64 bit VM.
92b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochdef DefaultArch():
93b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  machine = platform.machine()
94b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  machine = machine.lower()  # Windows 7 capitalizes 'AMD64'.
95b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if machine.startswith('arm'):
96a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return 'arm'
97b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  elif (not machine) or (not re.match('(x|i[3-6])86$', machine) is None):
98a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return 'ia32'
99b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  elif machine == 'i86pc':
100d91b9f7d46489a9ee00f9cb415630299c76a502bLeon Clarke    return 'ia32'
101b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  elif machine == 'x86_64':
10280d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen    return 'ia32'
103b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  elif machine == 'amd64':
1046ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    return 'ia32'
1051b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  elif machine == 's390x':
1061b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    return 's390'
107014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  elif machine == 'ppc64':
108014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    return 'ppc'
109a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  else:
110a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return None
111a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
112a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
113a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockdef GuessWordsize():
114a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  if '64' in platform.machine():
115a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return '64'
116a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  else:
117a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return '32'
118a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
119a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
120a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockdef IsWindows():
121b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return GuessOS() == 'windows'
122b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
123b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
124b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochdef URLRetrieve(source, destination):
125b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  """urllib is broken for SSL connections via a proxy therefore we
126b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  can't use urllib.urlretrieve()."""
127014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if IsWindows():
128014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    try:
129014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      # In python 2.7.6 on windows, urlopen has a problem with redirects.
130014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      # Try using curl instead. Note, this is fixed in 2.7.8.
131014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      subprocess.check_call(["curl", source, '-k', '-L', '-o', destination])
132014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return
133014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    except:
134014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      # If there's no curl, fall back to urlopen.
135014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      print "Curl is currently not installed. Falling back to python."
136014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      pass
137b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  with open(destination, 'w') as f:
138b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    f.write(urllib2.urlopen(source).read())
139