1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
#!/usr/bin/env python
# Copyright (c) 2012 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.
"""Determine OS and various other system properties.
Determine the name of the platform used and other system properties such as
the location of Chrome. This is used, for example, to determine the correct
Toolchain to invoke.
"""
import optparse
import os
import re
import subprocess
import sys
import oshelpers
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
CHROME_DEFAULT_PATH = {
'win': r'c:\Program Files (x86)\Google\Chrome\Application\chrome.exe',
'mac': '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
'linux': '/usr/bin/google-chrome',
}
if sys.version_info < (2, 6, 0):
sys.stderr.write("python 2.6 or later is required run this script\n")
sys.exit(1)
class Error(Exception):
pass
def GetSDKPath():
return os.getenv('NACL_SDK_ROOT', os.path.dirname(SCRIPT_DIR))
def GetPlatform():
if sys.platform.startswith('cygwin') or sys.platform.startswith('win'):
return 'win'
elif sys.platform.startswith('darwin'):
return 'mac'
elif sys.platform.startswith('linux'):
return 'linux'
else:
raise Error("Unknown platform: %s" % sys.platform)
def UseWin64():
arch32 = os.environ.get('PROCESSOR_ARCHITECTURE')
arch64 = os.environ.get('PROCESSOR_ARCHITEW6432')
if arch32 == 'AMD64' or arch64 == 'AMD64':
return True
return False
def GetSDKVersion():
root = GetSDKPath()
readme = os.path.join(root, "README")
if not os.path.exists(readme):
raise Error("README not found in SDK root: %s" % root)
version = None
revision = None
commit_position = None
for line in open(readme):
if ':' in line:
name, value = line.split(':', 1)
if name == "Version":
version = value.strip()
if name == "Chrome Revision":
revision = value.strip()
if name == "Chrome Commit Position":
commit_position = value.strip()
if revision is None or version is None or commit_position is None:
raise Error("error parsing SDK README: %s" % readme)
try:
version = int(version)
except ValueError:
raise Error("error parsing SDK README: %s" % readme)
return (version, revision, commit_position)
def GetSystemArch(platform):
if platform == 'win':
if UseWin64():
return 'x86_64'
return 'x86_32'
if platform in ['mac', 'linux']:
try:
pobj = subprocess.Popen(['uname', '-m'], stdout= subprocess.PIPE)
arch = pobj.communicate()[0]
arch = arch.split()[0]
if arch.startswith('arm'):
arch = 'arm'
except Exception:
arch = None
return arch
def GetChromePath(platform):
# If CHROME_PATH is defined and exists, use that.
chrome_path = os.environ.get('CHROME_PATH')
if chrome_path:
if not os.path.exists(chrome_path):
raise Error('Invalid CHROME_PATH: %s' % chrome_path)
return os.path.realpath(chrome_path)
# Otherwise look in the PATH environment variable.
basename = os.path.basename(CHROME_DEFAULT_PATH[platform])
chrome_path = oshelpers.FindExeInPath(basename)
if chrome_path:
return os.path.realpath(chrome_path)
# Finally, try the default paths to Chrome.
chrome_path = CHROME_DEFAULT_PATH[platform]
if os.path.exists(chrome_path):
return os.path.realpath(chrome_path)
raise Error('CHROME_PATH is undefined, and %s not found in PATH, nor %s.' % (
basename, chrome_path))
def GetNaClArch(platform):
if platform == 'win':
# On windows the nacl arch always matches to system arch
return GetSystemArch(platform)
elif platform == 'mac':
# On Mac the nacl arch is currently always 32-bit.
return 'x86_32'
# On linux the nacl arch matches to chrome arch, so we inspect the chrome
# binary using objdump
chrome_path = GetChromePath(platform)
# If CHROME_PATH is set to point to google-chrome or google-chrome
# was found in the PATH and we are running on UNIX then google-chrome
# is a bash script that points to 'chrome' in the same folder.
#
# When running beta or dev branch, the name is google-chrome-{beta,dev}.
if os.path.basename(chrome_path).startswith('google-chrome'):
chrome_path = os.path.join(os.path.dirname(chrome_path), 'chrome')
if not os.path.exists(chrome_path):
raise Error("File %s does not exist." % chrome_path)
if not os.access(chrome_path, os.X_OK):
raise Error("File %s is not executable" % chrome_path)
try:
pobj = subprocess.Popen(['objdump', '-f', chrome_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, stderr = pobj.communicate()
# error out here if objdump failed
if pobj.returncode:
raise Error(output + stderr.strip())
except OSError as e:
# This will happen if objdump is not installed
raise Error("Error running objdump: %s" % e)
pattern = r'(file format) ([a-zA-Z0-9_\-]+)'
match = re.search(pattern, output)
if not match:
raise Error("Error running objdump on: %s" % chrome_path)
arch = match.group(2)
if 'arm' in arch:
return 'arm'
if '64' in arch:
return 'x86_64'
return 'x86_32'
def ParseVersion(version):
if '.' in version:
version = version.split('.')
else:
version = (version, '0')
try:
return tuple(int(x) for x in version)
except ValueError:
raise Error('error parsing SDK version: %s' % version)
def main(args):
parser = optparse.OptionParser()
parser.add_option('--arch', action='store_true',
help='Print architecture of current machine (x86_32, x86_64 or arm).')
parser.add_option('--chrome', action='store_true',
help='Print the path chrome (by first looking in $CHROME_PATH and '
'then $PATH).')
parser.add_option('--nacl-arch', action='store_true',
help='Print architecture used by NaCl on the current machine.')
parser.add_option('--sdk-version', action='store_true',
help='Print major version of the NaCl SDK.')
parser.add_option('--sdk-revision', action='store_true',
help='Print revision number of the NaCl SDK.')
parser.add_option('--sdk-commit-position', action='store_true',
help='Print commit position of the NaCl SDK.')
parser.add_option('--check-version',
help='Check that the SDK version is at least as great as the '
'version passed in.')
options, _ = parser.parse_args(args)
platform = GetPlatform()
if len(args) > 1:
parser.error('Only one option can be specified at a time.')
if not args:
print platform
return 0
if options.arch:
out = GetSystemArch(platform)
elif options.nacl_arch:
out = GetNaClArch(platform)
elif options.chrome:
out = GetChromePath(platform)
elif options.sdk_version:
out = GetSDKVersion()[0]
elif options.sdk_revision:
out = GetSDKVersion()[1]
elif options.sdk_commit_position:
out = GetSDKVersion()[2]
elif options.check_version:
required_version = ParseVersion(options.check_version)
version = GetSDKVersion()
# We currently ignore the revision and just check the major version number.
# Currently, version[1] is just a Git hash, which cannot be compared.
# TODO(mgiuca): Compare the minor revision numbers (which should be
# Cr-Commit-Position values), when http://crbug.com/406783 is fixed.
# Then Cr-Commit-Position should be available: see http://crbug.com/406993.
if version[0] < required_version[0]:
raise Error("SDK version too old (current: %s, required: %s)"
% (version[0], required_version[0]))
out = None
if out:
print out
return 0
if __name__ == '__main__':
try:
sys.exit(main(sys.argv[1:]))
except Error as e:
sys.stderr.write(str(e) + '\n')
sys.exit(1)
|