2 # Copyright (c) 2012 The Native Client 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.
11 sys.path.append(Dir('#/tools').abspath)
15 Import(['pre_base_env'])
17 # Underlay things migrating to ppapi repo.
18 Dir('#/..').addRepository(Dir('#/../ppapi'))
20 # Append a list of files to another, filtering out the files that already exist.
21 # Filtering helps migrate declarations between repos by preventing redundant
22 # declarations from causing an error.
23 def ExtendFileList(existing, additional):
24 # Avoid quadratic behavior by using a set.
26 for file_name in existing + additional:
27 if file_name in combined:
28 print 'WARNING: two references to file %s in the build.' % file_name
29 combined.add(file_name)
30 return sorted(combined)
33 ppapi_scons_files = {}
34 ppapi_scons_files['trusted_scons_files'] = []
35 ppapi_scons_files['untrusted_irt_scons_files'] = []
37 ppapi_scons_files['nonvariant_test_scons_files'] = [
38 'tests/breakpad_crash_test/nacl.scons',
39 'tests/nacl_browser/browser_dynamic_library/nacl.scons',
40 'tests/ppapi_test_lib/nacl.scons',
43 ppapi_scons_files['irt_variant_test_scons_files'] = [
44 # 'inbrowser_test_runner' must be in the irt_variant list
45 # otherwise it will run no tests.
46 'tests/nacl_browser/inbrowser_test_runner/nacl.scons',
47 # Disabled by Brad Chen 4 Sep to try to green Chromium
48 # nacl_integration tests
49 #'tests/nacl_browser/fault_injection/nacl.scons',
52 ppapi_scons_files['untrusted_scons_files'] = [
53 'src/shared/ppapi/nacl.scons',
54 'src/untrusted/irt_stub/nacl.scons',
55 'src/untrusted/nacl_ppapi_util/nacl.scons',
60 'XAUTHORITY', 'HOME', 'DISPLAY', 'SSH_TTY', 'KRB5CCNAME',
61 'CHROME_DEVEL_SANDBOX' ]
63 def SetupBrowserEnv(env):
64 for var_name in EXTRA_ENV:
65 if var_name in os.environ:
66 env['ENV'][var_name] = os.environ[var_name]
68 pre_base_env.AddMethod(SetupBrowserEnv)
71 def GetHeadlessPrefix(env):
72 if env.Bit('browser_headless') and env.Bit('host_linux'):
73 return ['xvfb-run', '--auto-servernum']
75 # Mac and Windows do not seem to have an equivalent.
78 pre_base_env.AddMethod(GetHeadlessPrefix)
81 # A fake file to depend on if a path to Chrome is not specified.
82 no_browser = pre_base_env.File('chrome_browser_path_not_specified')
85 # SCons attempts to run a test that depends on "no_browser", detect this at
86 # runtime and cause a build error.
87 def NoBrowserError(target, source, env):
88 print target, source, env
89 print ("***\nYou need to specificy chrome_browser_path=... on the " +
90 "command line to run these tests.\n***\n")
93 pre_base_env.Append(BUILDERS = {
94 'NoBrowserError': Builder(action=NoBrowserError)
97 pre_base_env.NoBrowserError([no_browser], [])
100 def ChromeBinary(env):
101 if 'chrome_browser_path' in ARGUMENTS:
102 return env.File(env.SConstructAbsPath(ARGUMENTS['chrome_browser_path']))
106 pre_base_env.AddMethod(ChromeBinary)
109 def GetPPAPIPluginPath(env, allow_64bit_redirect=True):
110 if 'force_ppapi_plugin' in ARGUMENTS:
111 return env.SConstructAbsPath(ARGUMENTS['force_ppapi_plugin'])
113 fn = env.File('${STAGING_DIR}/ppNaClPlugin')
115 fn = env.File('${STAGING_DIR}/${SHLIBPREFIX}ppNaClPlugin${SHLIBSUFFIX}')
116 if allow_64bit_redirect and env.Bit('target_x86_64'):
117 # On 64-bit Windows and on Mac, we need the 32-bit plugin because
118 # the browser is 32-bit.
119 # Unfortunately it is tricky to build the 32-bit plugin (and all the
120 # libraries it needs) in a 64-bit build... so we'll assume it has already
121 # been built in a previous invocation.
122 # TODO(ncbray) better 32/64 builds.
123 if env.Bit('windows'):
124 fn = env.subst(fn).abspath.replace('-win-x86-64', '-win-x86-32')
126 fn = env.subst(fn).abspath.replace('-mac-x86-64', '-mac-x86-32')
129 pre_base_env.AddMethod(GetPPAPIPluginPath)
132 # runnable-ld.so log has following format:
133 # lib_name => path_to_lib (0x....address)
134 def ParseLibInfoInRunnableLdLog(line):
135 pos = line.find(' => ')
138 lib_name = line[:pos].strip()
139 lib_path = line[pos+4:]
140 pos1 = lib_path.rfind(' (')
143 lib_path = lib_path[:pos1]
144 return lib_name, lib_path
147 # Expected name of the temporary .libs file which stores glibc library
148 # dependencies in "lib_name => lib_info" format
149 # (see ParseLibInfoInRunnableLdLog)
150 def GlibcManifestLibsListFilename(manifest_base_name):
151 return '${STAGING_DIR}/%s.libs' % manifest_base_name
154 # Copy libs and manifest to the target directory.
155 # source[0] is a manifest file
156 # source[1] is a .libs file with a list of libs generated by runnable-ld.so
157 def CopyLibsForExtensionCommand(target, source, env):
158 source_manifest = str(source[0])
159 target_manifest = str(target[0])
160 shutil.copyfile(source_manifest, target_manifest)
161 target_dir = os.path.dirname(target_manifest)
162 libs_file = open(str(source[1]), 'r')
163 for line in libs_file.readlines():
164 lib_info = ParseLibInfoInRunnableLdLog(line)
166 lib_name, lib_path = lib_info
167 if lib_path == 'NaClMain':
168 # This is a fake file name, which we cannot copy.
170 shutil.copyfile(lib_path, os.path.join(target_dir, lib_name))
171 shutil.copyfile(env.subst('${NACL_SDK_LIB}/runnable-ld.so'),
172 os.path.join(target_dir, 'runnable-ld.so'))
176 # Extensions are loaded from directory on disk and so all dynamic libraries
177 # they use must be copied to extension directory. The option --extra_serving_dir
178 # does not help us in this case.
179 def CopyLibsForExtension(env, target_dir, manifest):
180 if not env.Bit('nacl_glibc'):
181 return env.Install(target_dir, manifest)
182 manifest_base_name = os.path.basename(str(env.subst(manifest)))
183 lib_list_node = env.File(GlibcManifestLibsListFilename(manifest_base_name))
184 nmf_node = env.Command(
185 target_dir + '/' + manifest_base_name,
186 [manifest, lib_list_node],
187 CopyLibsForExtensionCommand)
190 pre_base_env.AddMethod(CopyLibsForExtension)
194 def WhitelistLibsForExtensionCommand(target, source, env):
195 # Load existing extension manifest.
196 src_file = open(source[0].abspath, 'r')
197 src_json = json.load(src_file)
200 # Load existing 'web_accessible_resources' key.
201 if 'web_accessible_resources' not in src_json:
202 src_json['web_accessible_resources'] = []
203 web_accessible = src_json['web_accessible_resources']
205 # Load list of libraries, and add libraries to web_accessible list.
206 libs_file = open(source[1].abspath, 'r')
207 for line in libs_file.readlines():
208 lib_info = ParseLibInfoInRunnableLdLog(line)
210 web_accessible.append(lib_info[0])
211 # Also add the dynamic loader, which won't be in the libs_file.
212 web_accessible.append('runnable-ld.so')
215 # Write out the appended-to extension manifest.
216 target_file = open(target[0].abspath, 'w')
217 json.dump(src_json, target_file, sort_keys=True, indent=2)
221 # Whitelist glibc shared libraries (if necessary), so that they are
222 # 'web_accessible_resources'. This allows the libraries hosted at the origin
223 # chrome-extension://[PACKAGE ID]/
224 # to be made available to webpages that use this NaCl extension,
225 # which are in a different origin.
226 # See: http://code.google.com/chrome/extensions/manifest.html
228 # Alternatively, we could try to use the chrome commandline switch
229 # '--disable-extensions-resource-whitelist', but that would not be what
230 # users will need to do.
231 def WhitelistLibsForExtension(env, target_dir, nmf, extension_manifest):
232 if env.Bit('nacl_static_link'):
233 # For static linking, assume the nexe and nmf files are already
234 # whitelisted, so there is no need to add entries to the extension_manifest.
235 return env.Install(target_dir, extension_manifest)
236 nmf_base_name = os.path.basename(env.File(nmf).abspath)
237 lib_list_node = env.File(GlibcManifestLibsListFilename(nmf_base_name))
238 manifest_base_name = os.path.basename(env.File(extension_manifest).abspath)
239 extension_manifest_node = env.Command(
240 target_dir + '/' + manifest_base_name,
241 [extension_manifest, lib_list_node],
242 WhitelistLibsForExtensionCommand)
243 return extension_manifest_node
245 pre_base_env.AddMethod(WhitelistLibsForExtension)
248 # Generate manifest from newlib manifest and the list of libs generated by
250 def GenerateManifestFunc(target, source, env):
251 # Open the original manifest and parse it.
252 source_file = open(str(source[0]), 'r')
253 obj = json.load(source_file)
255 # Open the file with ldd-format list of NEEDED libs and parse it.
256 libs_file = open(str(source[1]), 'r')
258 arch = env.subst('${TARGET_FULLARCH}')
259 for line in libs_file.readlines():
260 lib_info = ParseLibInfoInRunnableLdLog(line)
262 lib_name, _ = lib_info
263 lib_names.append(lib_name)
265 # Inject the NEEDED libs into the manifest.
266 if 'files' not in obj:
268 for lib_name in lib_names:
269 obj['files'][lib_name] = {}
270 obj['files'][lib_name][arch] = {}
271 obj['files'][lib_name][arch]['url'] = lib_name
272 # Put what used to be specified under 'program' into 'main.nexe'.
273 obj['files']['main.nexe'] = {}
274 for k, v in obj['program'].items():
275 obj['files']['main.nexe'][k] = v.copy()
276 v['url'] = 'runnable-ld.so'
277 # Write the new manifest!
278 target_file = open(str(target[0]), 'w')
279 json.dump(obj, target_file, sort_keys=True, indent=2)
284 def GenerateManifestDynamicLink(env, dest_file, lib_list_file,
286 # Run sel_ldr on the nexe to trace the NEEDED libraries.
287 lib_list_node = env.Command(
290 '${NACL_SDK_LIB}/runnable-ld.so',
292 '${SCONSTRUCT_DIR}/DEPS'],
293 # We ignore the return code using '-' in order to build tests
294 # where binaries do not validate. This is a Scons feature.
295 '-${SOURCES[0]} -a -E LD_TRACE_LOADED_OBJECTS=1 ${SOURCES[1]} '
296 '--library-path ${NACL_SDK_LIB}:${LIB_DIR} ${SOURCES[2].posix} '
298 return env.Command(dest_file,
299 [manifest, lib_list_node],
300 GenerateManifestFunc)[0]
303 def GenerateSimpleManifestStaticLink(env, dest_file, exe_name):
304 def Func(target, source, env):
305 archs = ('x86-32', 'x86-64', 'arm')
306 nmf_data = {'program': dict((arch, {'url': '%s_%s.nexe' % (exe_name, arch)})
308 fh = open(target[0].abspath, 'w')
309 json.dump(nmf_data, fh, sort_keys=True, indent=2)
311 node = env.Command(dest_file, [], Func)[0]
312 # Scons does not track the dependency of dest_file on exe_name or on
313 # the Python code above, so we should always recreate dest_file when
315 env.AlwaysBuild(node)
319 def GenerateSimpleManifest(env, dest_file, exe_name):
320 if env.Bit('nacl_static_link'):
321 return GenerateSimpleManifestStaticLink(env, dest_file, exe_name)
323 static_manifest = GenerateSimpleManifestStaticLink(
324 env, '%s.static' % dest_file, exe_name)
325 return GenerateManifestDynamicLink(
326 env, dest_file, '%s.tmp_lib_list' % dest_file, static_manifest,
327 '${STAGING_DIR}/%s.nexe' % env.ProgramNameForNmf(exe_name))
329 pre_base_env.AddMethod(GenerateSimpleManifest)
332 # Returns a pair (main program, is_portable), based on the program
333 # specified in manifest file.
334 def GetMainProgramFromManifest(env, manifest):
335 obj = json.loads(env.File(manifest).get_contents())
336 program_dict = obj['program']
337 return program_dict[env.subst('${TARGET_FULLARCH}')]['url']
340 # Returns scons node for generated manifest.
341 def GeneratedManifestNode(env, manifest):
342 manifest = env.subst(manifest)
343 manifest_base_name = os.path.basename(manifest)
344 main_program = GetMainProgramFromManifest(env, manifest)
345 result = env.File('${STAGING_DIR}/' + manifest_base_name)
346 # Always generate the manifest for nacl_glibc.
347 # For nacl_glibc, generating the mapping of shared libraries is non-trivial.
348 if not env.Bit('nacl_glibc'):
349 env.Install('${STAGING_DIR}', manifest)
351 return GenerateManifestDynamicLink(
352 env, '${STAGING_DIR}/' + manifest_base_name,
353 # Note that CopyLibsForExtension() and WhitelistLibsForExtension()
354 # assume that it can find the library list file under this filename.
355 GlibcManifestLibsListFilename(manifest_base_name),
357 env.File('${STAGING_DIR}/' + os.path.basename(main_program)))
361 # Compares output_file and golden_file.
362 # If they are different, prints the difference and returns 1.
363 # Otherwise, returns 0.
364 def CheckGoldenFile(golden_file, output_file,
365 filter_regex, filter_inverse, filter_group_only):
366 golden = open(golden_file).read()
367 actual = open(output_file).read()
368 if filter_regex is not None:
369 actual = test_lib.RegexpFilterLines(
374 if command_tester.DifferentFromGolden(actual, golden, output_file):
379 # Returns action that compares output_file and golden_file.
380 # This action can be attached to the node with
381 # env.AddPostAction(target, action)
382 def GoldenFileCheckAction(env, output_file, golden_file,
383 filter_regex=None, filter_inverse=False,
384 filter_group_only=False):
385 def ActionFunc(target, source, env):
386 return CheckGoldenFile(env.subst(golden_file), env.subst(output_file),
387 filter_regex, filter_inverse, filter_group_only)
389 return env.Action(ActionFunc)
392 def PPAPIBrowserTester(env,
397 # List of executable basenames to generate
398 # manifest files for.
406 # list of key/value pairs that are passed to the test
408 # list of "--flag=value" pairs (no spaces!)
410 # redirect streams of NaCl program to files
412 nacl_exe_stdout=None,
413 nacl_exe_stderr=None,
414 python_tester_script=None,
416 if 'TRUSTED_ENV' not in env:
419 # Handle issues with mutating any python default arg lists.
420 if browser_flags is None:
423 # Lint the extra arguments that are being passed to the tester.
424 special_args = ['--ppapi_plugin', '--sel_ldr', '--irt_library', '--file',
425 '--map_file', '--extension', '--mime_type', '--tool',
426 '--browser_flag', '--test_arg']
427 for arg_name in special_args:
429 raise Exception('%s: %r is a test argument provided by the SCons test'
430 ' wrapper, do not specify it as an additional argument' %
434 env.SetupBrowserEnv()
436 if 'scale_timeout' in ARGUMENTS:
437 timeout = timeout * int(ARGUMENTS['scale_timeout'])
439 if python_tester_script is None:
440 python_tester_script = env.File('${SCONSTRUCT_DIR}/tools/browser_tester'
441 '/browser_tester.py')
442 command = env.GetHeadlessPrefix() + [
443 '${PYTHON}', python_tester_script,
444 '--browser_path', env.ChromeBinary(),
446 # Fail if there is no response for X seconds.
447 '--timeout', str(timeout)]
448 for dep_file in files:
449 command.extend(['--file', dep_file])
450 for extension in extensions:
451 command.extend(['--extension', extension])
452 for dest_path, dep_file in map_files:
453 command.extend(['--map_file', dest_path, dep_file])
454 for file_ext, mime_type in mime_types:
455 command.extend(['--mime_type', file_ext, mime_type])
456 command.extend(['--serving_dir', '${NACL_SDK_LIB}'])
457 command.extend(['--serving_dir', '${LIB_DIR}'])
458 if 'browser_tester_bw' in ARGUMENTS:
459 command.extend(['-b', ARGUMENTS['browser_tester_bw']])
461 for nmf_file in nmfs:
462 generated_manifest = GeneratedManifestNode(env, nmf_file)
463 # We need to add generated manifests to the list of default targets.
464 # The manifests should be generated even if the tests are not run -
465 # the manifests may be needed for manual testing.
466 for group in env['COMPONENT_TEST_PROGRAM_GROUPS']:
467 env.Alias(group, generated_manifest)
468 # Generated manifests are served in the root of the HTTP server
469 command.extend(['--file', generated_manifest])
470 for nmf_name in nmf_names:
471 tmp_manifest = '%s.tmp/%s.nmf' % (target, nmf_name)
472 command.extend(['--map_file', '%s.nmf' % nmf_name,
473 env.GenerateSimpleManifest(tmp_manifest, nmf_name)])
474 if 'browser_test_tool' in ARGUMENTS:
475 command.extend(['--tool', ARGUMENTS['browser_test_tool']])
477 # Suppress debugging information on the Chrome waterfall.
478 if env.Bit('disable_flaky_tests') and '--debug' in args:
479 args.remove('--debug')
482 for flag in browser_flags:
483 if flag.find(' ') != -1:
484 raise Exception('Spaces not allowed in browser_flags: '
485 'use --flag=value instead')
486 command.extend(['--browser_flag', flag])
487 for key, value in test_args:
488 command.extend(['--test_arg', str(key), str(value)])
490 # Set a given file to be the nexe's stdin.
491 if nacl_exe_stdin is not None:
492 command.extend(['--nacl_exe_stdin', env.subst(nacl_exe_stdin['file'])])
496 # Set a given file to be the nexe's stdout or stderr. The tester also
497 # compares this output against a golden file.
498 for stream, params in (
499 ('stdout', nacl_exe_stdout),
500 ('stderr', nacl_exe_stderr)):
503 stream_file = env.subst(params['file'])
504 side_effects.append(stream_file)
505 command.extend(['--nacl_exe_' + stream, stream_file])
506 if 'golden' in params:
507 golden_file = env.subst(params['golden'])
508 filter_regex = params.get('filter_regex', None)
509 filter_inverse = params.get('filter_inverse', False)
510 filter_group_only = params.get('filter_group_only', False)
512 GoldenFileCheckAction(
513 env, stream_file, golden_file,
514 filter_regex, filter_inverse, filter_group_only))
516 if env.ShouldUseVerboseOptions(extra):
517 env.MakeVerboseExtraOptions(target, log_verbosity, extra)
518 # Heuristic for when to capture output...
519 capture_output = (extra.pop('capture_output', False)
520 or 'process_output_single' in extra)
521 node = env.CommandTest(target,
523 # Set to 'huge' so that the browser tester's timeout
524 # takes precedence over the default of the test_suite.
526 capture_output=capture_output,
528 for side_effect in side_effects:
529 env.SideEffect(side_effect, node)
530 # We can't check output if the test is not run.
531 if not env.Bit('do_not_run_tests'):
532 for action in post_actions:
533 env.AddPostAction(node, action)
536 pre_base_env.AddMethod(PPAPIBrowserTester)
539 # Disabled for ARM and MIPS because Chrome binaries for ARM and MIPS are not
541 def PPAPIBrowserTesterIsBroken(env):
542 return env.Bit('target_arm') or env.Bit('target_mips32')
544 pre_base_env.AddMethod(PPAPIBrowserTesterIsBroken)
546 # 3D is disabled everywhere
547 def PPAPIGraphics3DIsBroken(env):
550 pre_base_env.AddMethod(PPAPIGraphics3DIsBroken)
553 def AddChromeFilesFromGroup(env, file_group):
554 env['BUILD_SCONSCRIPTS'] = ExtendFileList(
555 env.get('BUILD_SCONSCRIPTS', []),
556 ppapi_scons_files[file_group])
558 pre_base_env.AddMethod(AddChromeFilesFromGroup)