llvm.org GIT mirror llvm / 9eddd8e
[lit] Raise the logic for enabling clang & lld substitutions to llvm. This paves the way for other projects which might /use/ clang or lld but not necessarily need to the full set of functionality available to clang and lld tests to be able to have a basic set of substitutions that allow a project to run the clang or lld executables. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315627 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 2 years ago
1 changed file(s) with 171 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
204204 def get_clang_has_lsan(self, clang, triple):
205205 if not clang:
206206 self.lit_config.warning(
207 "config.host_cxx is unset but test suite is configured to use sanitizers.")
207 'config.host_cxx is unset but test suite is configured to use sanitizers.')
208208 return False
209209
210210 clang_binary = clang.split()[0]
211 version_string, _ = self.get_process_output([clang_binary, '--version'])
211 version_string, _ = self.get_process_output(
212 [clang_binary, '--version'])
212213 if not 'clang' in version_string:
213214 self.lit_config.warning(
214215 "compiler '%s' does not appear to be clang, " % clang_binary +
215 "but test suite is configured to use sanitizers.")
216 'but test suite is configured to use sanitizers.')
216217 return False
217218
218219 if re.match(r'.*-linux', triple):
219220 return True
220221
221222 if re.match(r'^x86_64.*-apple', triple):
222 version_number = int(re.search(r'version ([0-9]+)\.', version_string).group(1))
223 version_number = int(
224 re.search(r'version ([0-9]+)\.', version_string).group(1))
223225 if 'Apple LLVM' in version_string:
224226 return version_number >= 9
225227 else:
298300 self.config.substitutions.append(('%python', sys.executable))
299301 self.add_tool_substitutions(
300302 tool_patterns, [self.config.llvm_tools_dir])
303
304 def use_llvm_tool(self, name, search_env=None, required=False, quiet=False):
305 """Find the executable program 'name', optionally using the specified
306 environment variable as an override before searching the
307 configuration's PATH."""
308 # If the override is specified in the environment, use it without
309 # validation.
310 if search_env:
311 tool = self.config.environment.get(search_env)
312 if tool:
313 return tool
314
315 # Otherwise look in the path.
316 tool = lit.util.which(name, self.config.llvm_tools_dir)
317
318 if required and not tool:
319 message = "couldn't find '{}' program".format(name)
320 if search_env:
321 message = message + \
322 ', try setting {} in your environment'.format(search_env)
323 self.lit_config.fatal(message)
324
325 if tool:
326 tool = os.path.normpath(tool)
327 if not self.lit_config.quiet and not quiet:
328 self.lit_config.note('using {}: {}'.format(name, tool))
329 return tool
330
331 def use_clang(self, required=True):
332 """Configure the test suite to be able to invoke clang.
333
334 Sets up some environment variables important to clang, locates a
335 just-built or installed clang, and add a set of standard
336 substitutions useful to any test suite that makes use of clang.
337
338 """
339 # Clear some environment variables that might affect Clang.
340 #
341 # This first set of vars are read by Clang, but shouldn't affect tests
342 # that aren't specifically looking for these features, or are required
343 # simply to run the tests at all.
344 #
345 # FIXME: Should we have a tool that enforces this?
346
347 # safe_env_vars = ('TMPDIR', 'TEMP', 'TMP', 'USERPROFILE', 'PWD',
348 # 'MACOSX_DEPLOYMENT_TARGET', 'IPHONEOS_DEPLOYMENT_TARGET',
349 # 'VCINSTALLDIR', 'VC100COMNTOOLS', 'VC90COMNTOOLS',
350 # 'VC80COMNTOOLS')
351 possibly_dangerous_env_vars = ['COMPILER_PATH', 'RC_DEBUG_OPTIONS',
352 'CINDEXTEST_PREAMBLE_FILE', 'LIBRARY_PATH',
353 'CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH',
354 'OBJC_INCLUDE_PATH', 'OBJCPLUS_INCLUDE_PATH',
355 'LIBCLANG_TIMING', 'LIBCLANG_OBJTRACKING',
356 'LIBCLANG_LOGGING', 'LIBCLANG_BGPRIO_INDEX',
357 'LIBCLANG_BGPRIO_EDIT', 'LIBCLANG_NOTHREADS',
358 'LIBCLANG_RESOURCE_USAGE',
359 'LIBCLANG_CODE_COMPLETION_LOGGING']
360 # Clang/Win32 may refer to %INCLUDE%. vsvarsall.bat sets it.
361 if platform.system() != 'Windows':
362 possibly_dangerous_env_vars.append('INCLUDE')
363
364 self.clear_environment(possibly_dangerous_env_vars)
365
366 # Tweak the PATH to include the tools dir and the scripts dir.
367 paths = [self.config.llvm_tools_dir]
368 tools = getattr(self.config, 'clang_tools_dir', None)
369 if tools:
370 paths = paths + [tools]
371 self.with_environment('PATH', paths, append_path=True)
372
373 paths = [self.config.llvm_shlib_dir, self.config.llvm_libs_dir]
374 self.with_environment('LD_LIBRARY_PATH', paths, append_path=True)
375
376 # Discover the 'clang' and 'clangcc' to use.
377
378 self.config.clang = self.use_llvm_tool(
379 'clang', search_env='CLANG', required=required)
380
381 self.config.substitutions.append(
382 ('%llvmshlibdir', self.config.llvm_shlib_dir))
383 self.config.substitutions.append(
384 ('%pluginext', self.config.llvm_plugin_ext))
385
386 builtin_include_dir = self.get_clang_builtin_include_dir(self.config.clang)
387 tool_substitutions = [
388 ToolSubst('%clang', command=self.config.clang),
389 ToolSubst('%clang_analyze_cc1', command='%clang_cc1', extra_args=['-analyze']),
390 ToolSubst('%clang_cc1', command=self.config.clang, extra_args=['-cc1', '-internal-isystem', builtin_include_dir, '-nostdsysteminc']),
391 ToolSubst('%clang_cpp', command=self.config.clang, extra_args=['--driver-mode=cpp']),
392 ToolSubst('%clang_cl', command=self.config.clang, extra_args=['--driver-mode=cl']),
393 ToolSubst('%clangxx', command=self.config.clang, extra_args=['--driver-mode=g++']),
394 ]
395 self.add_tool_substitutions(tool_substitutions)
396
397 self.config.substitutions.append(('%itanium_abi_triple',
398 self.make_itanium_abi_triple(self.config.target_triple)))
399 self.config.substitutions.append(('%ms_abi_triple',
400 self.make_msabi_triple(self.config.target_triple)))
401 self.config.substitutions.append(
402 ('%resource_dir', builtin_include_dir))
403
404 # The host triple might not be set, at least if we're compiling clang from
405 # an already installed llvm.
406 if self.config.host_triple and self.config.host_triple != '@LLVM_HOST_TRIPLE@':
407 self.config.substitutions.append(('%target_itanium_abi_host_triple',
408 '--target=%s' % self.make_itanium_abi_triple(self.config.host_triple)))
409 else:
410 self.config.substitutions.append(
411 ('%target_itanium_abi_host_triple', ''))
412
413 self.config.substitutions.append(
414 ('%src_include_dir', self.config.clang_src_dir + '/include'))
415
416 # FIXME: Find nicer way to prohibit this.
417 self.config.substitutions.append(
418 (' clang ', """*** Do not use 'clang' in tests, use '%clang'. ***"""))
419 self.config.substitutions.append(
420 (' clang\+\+ ', """*** Do not use 'clang++' in tests, use '%clangxx'. ***"""))
421 self.config.substitutions.append(
422 (' clang-cc ',
423 """*** Do not use 'clang-cc' in tests, use '%clang_cc1'. ***"""))
424 self.config.substitutions.append(
425 (' clang -cc1 -analyze ',
426 """*** Do not use 'clang -cc1 -analyze' in tests, use '%clang_analyze_cc1'. ***"""))
427 self.config.substitutions.append(
428 (' clang -cc1 ',
429 """*** Do not use 'clang -cc1' in tests, use '%clang_cc1'. ***"""))
430 self.config.substitutions.append(
431 (' %clang-cc1 ',
432 """*** invalid substitution, use '%clang_cc1'. ***"""))
433 self.config.substitutions.append(
434 (' %clang-cpp ',
435 """*** invalid substitution, use '%clang_cpp'. ***"""))
436 self.config.substitutions.append(
437 (' %clang-cl ',
438 """*** invalid substitution, use '%clang_cl'. ***"""))
439
440 def use_lld(self, required=True):
441 """Configure the test suite to be able to invoke lld.
442
443 Sets up some environment variables important to lld, locates a
444 just-built or installed lld, and add a set of standard
445 substitutions useful to any test suite that makes use of lld.
446
447 """
448 # Tweak the PATH to include the tools dir
449 tool_dirs = [self.config.llvm_tools_dir]
450 lib_dirs = [self.config.llvm_libs_dir]
451 lld_tools_dir = getattr(self.config, 'lld_tools_dir', None)
452 lld_libs_dir = getattr(self.config, 'lld_libs_dir', None)
453
454 if lld_tools_dir:
455 tool_dirs = tool_dirs + [lld_tools_dir]
456 if lld_libs_dir:
457 lib_dirs = lib_dirs + [lld_libs_dir]
458
459 self.with_environment('PATH', tool_dirs, append_path=True)
460 self.with_environment('LD_LIBRARY_PATH', lib_dirs, append_path=True)
461
462 self.config.substitutions.append(
463 (r"\bld.lld\b", 'ld.lld --full-shutdown'))
464
465 tool_patterns = ['ld.lld', 'lld-link', 'lld']
466
467 self.add_tool_substitutions(tool_patterns, tool_dirs)