2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Sets environment variables needed to run a chromium unit test."""
14 # This is hardcoded to be src/ relative to this script.
15 ROOT_DIR
= os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
)))
17 CHROME_SANDBOX_ENV
= 'CHROME_DEVEL_SANDBOX'
18 CHROME_SANDBOX_PATH
= '/opt/chromium/chrome_sandbox'
21 def should_enable_sandbox(cmd
, sandbox_path
):
22 """Return a boolean indicating that the current slave is capable of using the
23 sandbox and should enable it. This should return True iff the slave is a
24 Linux host with the sandbox file present and configured correctly."""
25 if not (sys
.platform
.startswith('linux') and
26 os
.path
.exists(sandbox_path
)):
29 # Copy the check in tools/build/scripts/slave/runtest.py.
33 sandbox_stat
= os
.stat(sandbox_path
)
34 if ((sandbox_stat
.st_mode
& stat
.S_ISUID
) and
35 (sandbox_stat
.st_mode
& stat
.S_IRUSR
) and
36 (sandbox_stat
.st_mode
& stat
.S_IXUSR
) and
37 (sandbox_stat
.st_uid
== 0)):
42 def enable_sandbox_if_required(cmd
, env
, verbose
=False):
43 """Checks enables the sandbox if it is required, otherwise it disables it."""
44 chrome_sandbox_path
= env
.get(CHROME_SANDBOX_ENV
, CHROME_SANDBOX_PATH
)
46 if should_enable_sandbox(cmd
, chrome_sandbox_path
):
48 print 'Enabling sandbox. Setting environment variable:'
49 print ' %s="%s"' % (CHROME_SANDBOX_ENV
, chrome_sandbox_path
)
50 env
[CHROME_SANDBOX_ENV
] = chrome_sandbox_path
53 print 'Disabling sandbox. Setting environment variable:'
54 print ' CHROME_DEVEL_SANDBOX=""'
55 env
['CHROME_DEVEL_SANDBOX'] = ''
58 def fix_python_path(cmd
):
59 """Returns the fixed command line to call the right python executable."""
61 if out
[0] == 'python':
62 out
[0] = sys
.executable
63 elif out
[0].endswith('.py'):
64 out
.insert(0, sys
.executable
)
68 def run_executable(cmd
, env
):
69 """Runs an executable with:
70 - environment variable CR_SOURCE_ROOT set to the root directory.
71 - environment variable LANGUAGE to en_US.UTF-8.
72 - environment variable CHROME_DEVEL_SANDBOX set if need
73 - Reuses sys.executable automatically.
75 env
= collections
.defaultdict(str, env
)
76 # Many tests assume a English interface...
77 env
['LANG'] = 'en_US.UTF-8'
78 # Used by base/base_paths_linux.cc as an override. Just make sure the default
80 env
.pop('CR_SOURCE_ROOT', None)
81 enable_sandbox_if_required(cmd
, env
)
83 # Copy logic from tools/build/scripts/slave/runtest.py.
84 asan
= '--asan=1' in cmd
85 lsan
= '--lsan=1' in cmd
86 if lsan
and sys
.platform
== 'linux2':
87 # Use the debug version of libstdc++ under LSan. If we don't, there will
88 # be a lot of incomplete stack traces in the reports.
89 env
['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/debug:'
91 if asan
and sys
.platform
== 'darwin':
92 isolate_output_dir
= os
.path
.abspath(os
.path
.dirname(cmd
[0]))
93 # This is needed because the test binary has @executable_path embedded in it
94 # that the OS tries to resolve to the cache directory and not the mapped
96 env
['DYLD_LIBRARY_PATH'] = str(isolate_output_dir
)
98 # Ensure paths are correctly separated on windows.
99 cmd
[0] = cmd
[0].replace('/', os
.path
.sep
)
100 cmd
= fix_python_path(cmd
)
103 # Need to pipe to the symbolizer script.
104 p1
= subprocess
.Popen(cmd
, env
=env
, stdout
=subprocess
.PIPE
,
106 p2
= subprocess
.Popen(["../tools/valgrind/asan/asan_symbolize.py"],
108 p1
.stdout
.close() # Allow p1 to receive a SIGPIPE if p2 exits.
112 return subprocess
.call(cmd
, env
=env
)
114 print >> sys
.stderr
, 'Failed to start %s' % cmd
119 return run_executable(sys
.argv
[1:], os
.environ
.copy())
122 if __name__
== '__main__':