• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#===----------------------------------------------------------------------===##
2#
3#                     The LLVM Compiler Infrastructure
4#
5# This file is dual licensed under the MIT and the University of Illinois Open
6# Source Licenses. See LICENSE.TXT for details.
7#
8#===----------------------------------------------------------------------===##
9
10import locale
11import os
12import platform
13import pkgutil
14import pipes
15import re
16import shlex
17import shutil
18import sys
19
20from libcxx.compiler import CXXCompiler
21from libcxx.test.target_info import make_target_info
22from libcxx.test.executor import *
23from libcxx.test.tracing import *
24import libcxx.util
25
26def loadSiteConfig(lit_config, config, param_name, env_name):
27    # We haven't loaded the site specific configuration (the user is
28    # probably trying to run on a test file directly, and either the site
29    # configuration hasn't been created by the build system, or we are in an
30    # out-of-tree build situation).
31    site_cfg = lit_config.params.get(param_name,
32                                     os.environ.get(env_name))
33    if not site_cfg:
34        lit_config.warning('No site specific configuration file found!'
35                           ' Running the tests in the default configuration.')
36    elif not os.path.isfile(site_cfg):
37        lit_config.fatal(
38            "Specified site configuration file does not exist: '%s'" %
39            site_cfg)
40    else:
41        lit_config.note('using site specific configuration at %s' % site_cfg)
42        ld_fn = lit_config.load_config
43
44        # Null out the load_config function so that lit.site.cfg doesn't
45        # recursively load a config even if it tries.
46        # TODO: This is one hell of a hack. Fix it.
47        def prevent_reload_fn(*args, **kwargs):
48            pass
49        lit_config.load_config = prevent_reload_fn
50        ld_fn(config, site_cfg)
51        lit_config.load_config = ld_fn
52
53# Extract the value of a numeric macro such as __cplusplus or a feature-test
54# macro.
55def intMacroValue(token):
56    return int(token.rstrip('LlUu'))
57
58class Configuration(object):
59    # pylint: disable=redefined-outer-name
60    def __init__(self, lit_config, config):
61        self.lit_config = lit_config
62        self.config = config
63        self.is_windows = platform.system() == 'Windows'
64        self.cxx = None
65        self.cxx_is_clang_cl = None
66        self.cxx_stdlib_under_test = None
67        self.project_obj_root = None
68        self.libcxx_src_root = None
69        self.libcxx_obj_root = None
70        self.cxx_library_root = None
71        self.cxx_runtime_root = None
72        self.abi_library_root = None
73        self.link_shared = self.get_lit_bool('enable_shared', default=True)
74        self.debug_build = self.get_lit_bool('debug_build',   default=False)
75        self.exec_env = dict(os.environ)
76        self.use_target = False
77        self.use_system_cxx_lib = False
78        self.use_clang_verify = False
79        self.long_tests = None
80        self.execute_external = False
81
82    def get_lit_conf(self, name, default=None):
83        val = self.lit_config.params.get(name, None)
84        if val is None:
85            val = getattr(self.config, name, None)
86            if val is None:
87                val = default
88        return val
89
90    def get_lit_bool(self, name, default=None, env_var=None):
91        def check_value(value, var_name):
92            if value is None:
93                return default
94            if isinstance(value, bool):
95                return value
96            if not isinstance(value, str):
97                raise TypeError('expected bool or string')
98            if value.lower() in ('1', 'true'):
99                return True
100            if value.lower() in ('', '0', 'false'):
101                return False
102            self.lit_config.fatal(
103                "parameter '{}' should be true or false".format(var_name))
104
105        conf_val = self.get_lit_conf(name)
106        if env_var is not None and env_var in os.environ and \
107                os.environ[env_var] is not None:
108            val = os.environ[env_var]
109            if conf_val is not None:
110                self.lit_config.warning(
111                    'Environment variable %s=%s is overriding explicit '
112                    '--param=%s=%s' % (env_var, val, name, conf_val))
113            return check_value(val, env_var)
114        return check_value(conf_val, name)
115
116    def get_modules_enabled(self):
117        return self.get_lit_bool('enable_modules',
118                                default=False,
119                                env_var='LIBCXX_ENABLE_MODULES')
120
121    def make_static_lib_name(self, name):
122        """Return the full filename for the specified library name"""
123        if self.is_windows:
124            assert name == 'c++'  # Only allow libc++ to use this function for now.
125            return 'lib' + name + '.lib'
126        else:
127            return 'lib' + name + '.a'
128
129    def configure(self):
130        self.configure_executor()
131        self.configure_use_system_cxx_lib()
132        self.configure_target_info()
133        self.configure_cxx()
134        self.configure_triple()
135        self.configure_deployment()
136        self.configure_src_root()
137        self.configure_obj_root()
138        self.configure_cxx_stdlib_under_test()
139        self.configure_cxx_library_root()
140        self.configure_use_clang_verify()
141        self.configure_use_thread_safety()
142        self.configure_execute_external()
143        self.configure_ccache()
144        self.configure_compile_flags()
145        self.configure_filesystem_compile_flags()
146        self.configure_link_flags()
147        self.configure_env()
148        self.configure_color_diagnostics()
149        self.configure_debug_mode()
150        self.configure_warnings()
151        self.configure_sanitizer()
152        self.configure_coverage()
153        self.configure_modules()
154        self.configure_coroutines()
155        self.configure_substitutions()
156        self.configure_features()
157
158    def print_config_info(self):
159        # Print the final compile and link flags.
160        self.lit_config.note('Using compiler: %s' % self.cxx.path)
161        self.lit_config.note('Using flags: %s' % self.cxx.flags)
162        if self.cxx.use_modules:
163            self.lit_config.note('Using modules flags: %s' %
164                                 self.cxx.modules_flags)
165        self.lit_config.note('Using compile flags: %s'
166                             % self.cxx.compile_flags)
167        if len(self.cxx.warning_flags):
168            self.lit_config.note('Using warnings: %s' % self.cxx.warning_flags)
169        self.lit_config.note('Using link flags: %s' % self.cxx.link_flags)
170        # Print as list to prevent "set([...])" from being printed.
171        self.lit_config.note('Using available_features: %s' %
172                             list(self.config.available_features))
173        show_env_vars = {}
174        for k,v in self.exec_env.items():
175            if k not in os.environ or os.environ[k] != v:
176                show_env_vars[k] = v
177        self.lit_config.note('Adding environment variables: %r' % show_env_vars)
178        sys.stderr.flush()  # Force flushing to avoid broken output on Windows
179
180    def get_test_format(self):
181        from libcxx.test.format import LibcxxTestFormat
182        return LibcxxTestFormat(
183            self.cxx,
184            self.use_clang_verify,
185            self.execute_external,
186            self.executor,
187            exec_env=self.exec_env)
188
189    def configure_executor(self):
190        exec_str = self.get_lit_conf('executor', "None")
191        te = eval(exec_str)
192        if te:
193            self.lit_config.note("Using executor: %r" % exec_str)
194            if self.lit_config.useValgrind:
195                # We have no way of knowing where in the chain the
196                # ValgrindExecutor is supposed to go. It is likely
197                # that the user wants it at the end, but we have no
198                # way of getting at that easily.
199                selt.lit_config.fatal("Cannot infer how to create a Valgrind "
200                                      " executor.")
201        else:
202            te = LocalExecutor()
203            if self.lit_config.useValgrind:
204                te = ValgrindExecutor(self.lit_config.valgrindArgs, te)
205        self.executor = te
206
207    def configure_target_info(self):
208        self.target_info = make_target_info(self)
209
210    def configure_cxx(self):
211        # Gather various compiler parameters.
212        cxx = self.get_lit_conf('cxx_under_test')
213        self.cxx_is_clang_cl = cxx is not None and \
214                               os.path.basename(cxx) == 'clang-cl.exe'
215        # If no specific cxx_under_test was given, attempt to infer it as
216        # clang++.
217        if cxx is None or self.cxx_is_clang_cl:
218            search_paths = self.config.environment['PATH']
219            if cxx is not None and os.path.isabs(cxx):
220                search_paths = os.path.dirname(cxx)
221            clangxx = libcxx.util.which('clang++', search_paths)
222            if clangxx:
223                cxx = clangxx
224                self.lit_config.note(
225                    "inferred cxx_under_test as: %r" % cxx)
226            elif self.cxx_is_clang_cl:
227                self.lit_config.fatal('Failed to find clang++ substitution for'
228                                      ' clang-cl')
229        if not cxx:
230            self.lit_config.fatal('must specify user parameter cxx_under_test '
231                                  '(e.g., --param=cxx_under_test=clang++)')
232        self.cxx = CXXCompiler(cxx) if not self.cxx_is_clang_cl else \
233                   self._configure_clang_cl(cxx)
234        cxx_type = self.cxx.type
235        if cxx_type is not None:
236            assert self.cxx.version is not None
237            maj_v, min_v, _ = self.cxx.version
238            self.config.available_features.add(cxx_type)
239            self.config.available_features.add('%s-%s' % (cxx_type, maj_v))
240            self.config.available_features.add('%s-%s.%s' % (
241                cxx_type, maj_v, min_v))
242        self.cxx.compile_env = dict(os.environ)
243        # 'CCACHE_CPP2' prevents ccache from stripping comments while
244        # preprocessing. This is required to prevent stripping of '-verify'
245        # comments.
246        self.cxx.compile_env['CCACHE_CPP2'] = '1'
247
248    def _configure_clang_cl(self, clang_path):
249        def _split_env_var(var):
250            return [p.strip() for p in os.environ.get(var, '').split(';') if p.strip()]
251
252        def _prefixed_env_list(var, prefix):
253            from itertools import chain
254            return list(chain.from_iterable((prefix, path) for path in _split_env_var(var)))
255
256        assert self.cxx_is_clang_cl
257        flags = []
258        compile_flags = _prefixed_env_list('INCLUDE', '-isystem')
259        link_flags = _prefixed_env_list('LIB', '-L')
260        for path in _split_env_var('LIB'):
261            self.add_path(self.exec_env, path)
262        return CXXCompiler(clang_path, flags=flags,
263                           compile_flags=compile_flags,
264                           link_flags=link_flags)
265
266    def _dump_macros_verbose(self, *args, **kwargs):
267        macros_or_error = self.cxx.dumpMacros(*args, **kwargs)
268        if isinstance(macros_or_error, tuple):
269            cmd, out, err, rc = macros_or_error
270            report = libcxx.util.makeReport(cmd, out, err, rc)
271            report += "Compiler failed unexpectedly when dumping macros!"
272            self.lit_config.fatal(report)
273            return None
274        assert isinstance(macros_or_error, dict)
275        return macros_or_error
276
277    def configure_src_root(self):
278        self.libcxx_src_root = self.get_lit_conf(
279            'libcxx_src_root', os.path.dirname(self.config.test_source_root))
280
281    def configure_obj_root(self):
282        self.project_obj_root = self.get_lit_conf('project_obj_root')
283        self.libcxx_obj_root = self.get_lit_conf('libcxx_obj_root')
284        if not self.libcxx_obj_root and self.project_obj_root is not None:
285            possible_root = os.path.join(self.project_obj_root, 'projects', 'libcxx')
286            if os.path.isdir(possible_root):
287                self.libcxx_obj_root = possible_root
288            else:
289                self.libcxx_obj_root = self.project_obj_root
290
291    def configure_cxx_library_root(self):
292        self.cxx_library_root = self.get_lit_conf('cxx_library_root',
293                                                  self.libcxx_obj_root)
294        self.cxx_runtime_root = self.get_lit_conf('cxx_runtime_root',
295                                                   self.cxx_library_root)
296
297    def configure_use_system_cxx_lib(self):
298        # This test suite supports testing against either the system library or
299        # the locally built one; the former mode is useful for testing ABI
300        # compatibility between the current headers and a shipping dynamic
301        # library.
302        # Default to testing against the locally built libc++ library.
303        self.use_system_cxx_lib = self.get_lit_conf('use_system_cxx_lib')
304        if self.use_system_cxx_lib == 'true':
305            self.use_system_cxx_lib = True
306        elif self.use_system_cxx_lib == 'false':
307            self.use_system_cxx_lib = False
308        elif self.use_system_cxx_lib:
309            assert os.path.isdir(self.use_system_cxx_lib), "the specified use_system_cxx_lib parameter (%s) is not a valid directory" % self.use_system_cxx_lib
310            self.use_system_cxx_lib = os.path.abspath(self.use_system_cxx_lib)
311        self.lit_config.note(
312            "inferred use_system_cxx_lib as: %r" % self.use_system_cxx_lib)
313
314    def configure_cxx_stdlib_under_test(self):
315        self.cxx_stdlib_under_test = self.get_lit_conf(
316            'cxx_stdlib_under_test', 'libc++')
317        if self.cxx_stdlib_under_test not in \
318                ['libc++', 'libstdc++', 'msvc', 'cxx_default']:
319            self.lit_config.fatal(
320                'unsupported value for "cxx_stdlib_under_test": %s'
321                % self.cxx_stdlib_under_test)
322        self.config.available_features.add(self.cxx_stdlib_under_test)
323        if self.cxx_stdlib_under_test == 'libstdc++':
324            self.config.available_features.add('libstdc++')
325            # Manually enable the experimental and filesystem tests for libstdc++
326            # if the options aren't present.
327            # FIXME this is a hack.
328            if self.get_lit_conf('enable_experimental') is None:
329                self.config.enable_experimental = 'true'
330            if self.get_lit_conf('enable_filesystem') is None:
331                self.config.enable_filesystem = 'true'
332
333    def configure_use_clang_verify(self):
334        '''If set, run clang with -verify on failing tests.'''
335        self.use_clang_verify = self.get_lit_bool('use_clang_verify')
336        if self.use_clang_verify is None:
337            # NOTE: We do not test for the -verify flag directly because
338            #   -verify will always exit with non-zero on an empty file.
339            self.use_clang_verify = self.cxx.isVerifySupported()
340            self.lit_config.note(
341                "inferred use_clang_verify as: %r" % self.use_clang_verify)
342        if self.use_clang_verify:
343                self.config.available_features.add('verify-support')
344
345    def configure_use_thread_safety(self):
346        '''If set, run clang with -verify on failing tests.'''
347        has_thread_safety = self.cxx.hasCompileFlag('-Werror=thread-safety')
348        if has_thread_safety:
349            self.cxx.compile_flags += ['-Werror=thread-safety']
350            self.config.available_features.add('thread-safety')
351            self.lit_config.note("enabling thread-safety annotations")
352
353    def configure_execute_external(self):
354        # Choose between lit's internal shell pipeline runner and a real shell.
355        # If LIT_USE_INTERNAL_SHELL is in the environment, we use that as the
356        # default value. Otherwise we ask the target_info.
357        use_lit_shell_default = os.environ.get('LIT_USE_INTERNAL_SHELL')
358        if use_lit_shell_default is not None:
359            use_lit_shell_default = use_lit_shell_default != '0'
360        else:
361            use_lit_shell_default = self.target_info.use_lit_shell_default()
362        # Check for the command line parameter using the default value if it is
363        # not present.
364        use_lit_shell = self.get_lit_bool('use_lit_shell',
365                                          use_lit_shell_default)
366        self.execute_external = not use_lit_shell
367
368    def configure_ccache(self):
369        use_ccache_default = os.environ.get('LIBCXX_USE_CCACHE') is not None
370        use_ccache = self.get_lit_bool('use_ccache', use_ccache_default)
371        if use_ccache:
372            self.cxx.use_ccache = True
373            self.lit_config.note('enabling ccache')
374
375    def add_deployment_feature(self, feature):
376        (arch, name, version) = self.config.deployment
377        self.config.available_features.add('%s=%s-%s' % (feature, arch, name))
378        self.config.available_features.add('%s=%s' % (feature, name))
379        self.config.available_features.add('%s=%s%s' % (feature, name, version))
380
381    def configure_features(self):
382        additional_features = self.get_lit_conf('additional_features')
383        if additional_features:
384            for f in additional_features.split(','):
385                self.config.available_features.add(f.strip())
386        self.target_info.add_locale_features(self.config.available_features)
387
388        target_platform = self.target_info.platform()
389
390        # Write an "available feature" that combines the triple when
391        # use_system_cxx_lib is enabled. This is so that we can easily write
392        # XFAIL markers for tests that are known to fail with versions of
393        # libc++ as were shipped with a particular triple.
394        if self.use_system_cxx_lib:
395            self.config.available_features.add('with_system_cxx_lib')
396            self.config.available_features.add(
397                'with_system_cxx_lib=%s' % self.config.target_triple)
398
399            # Add subcomponents individually.
400            target_components = self.config.target_triple.split('-')
401            for component in target_components:
402                self.config.available_features.add(
403                    'with_system_cxx_lib=%s' % component)
404
405            # Add available features for more generic versions of the target
406            # triple attached to  with_system_cxx_lib.
407            if self.use_deployment:
408                self.add_deployment_feature('with_system_cxx_lib')
409
410        # Configure the availability feature. Availability is only enabled
411        # with libc++, because other standard libraries do not provide
412        # availability markup.
413        if self.use_deployment and self.cxx_stdlib_under_test == 'libc++':
414            self.config.available_features.add('availability')
415            self.add_deployment_feature('availability')
416
417        if platform.system() == 'Darwin':
418            self.config.available_features.add('apple-darwin')
419
420        # Insert the platform name into the available features as a lower case.
421        self.config.available_features.add(target_platform)
422
423        # Simulator testing can take a really long time for some of these tests
424        # so add a feature check so we can REQUIRES: long_tests in them
425        self.long_tests = self.get_lit_bool('long_tests')
426        if self.long_tests is None:
427            # Default to running long tests.
428            self.long_tests = True
429            self.lit_config.note(
430                "inferred long_tests as: %r" % self.long_tests)
431
432        if self.long_tests:
433            self.config.available_features.add('long_tests')
434
435        # Run a compile test for the -fsized-deallocation flag. This is needed
436        # in test/std/language.support/support.dynamic/new.delete
437        if self.cxx.hasCompileFlag('-fsized-deallocation'):
438            self.config.available_features.add('-fsized-deallocation')
439
440        if self.cxx.hasCompileFlag('-faligned-allocation'):
441            self.config.available_features.add('-faligned-allocation')
442        else:
443            # FIXME remove this once more than just clang-4.0 support
444            # C++17 aligned allocation.
445            self.config.available_features.add('no-aligned-allocation')
446
447        if self.cxx.hasCompileFlag('-fdelayed-template-parsing'):
448            self.config.available_features.add('fdelayed-template-parsing')
449
450        if self.get_lit_bool('has_libatomic', False):
451            self.config.available_features.add('libatomic')
452
453        macros = self._dump_macros_verbose()
454        if '__cpp_if_constexpr' not in macros:
455            self.config.available_features.add('libcpp-no-if-constexpr')
456
457        if '__cpp_structured_bindings' not in macros:
458            self.config.available_features.add('libcpp-no-structured-bindings')
459
460        if '__cpp_deduction_guides' not in macros or \
461                intMacroValue(macros['__cpp_deduction_guides']) < 201611:
462            self.config.available_features.add('libcpp-no-deduction-guides')
463
464        if self.is_windows:
465            self.config.available_features.add('windows')
466            if self.cxx_stdlib_under_test == 'libc++':
467                # LIBCXX-WINDOWS-FIXME is the feature name used to XFAIL the
468                # initial Windows failures until they can be properly diagnosed
469                # and fixed. This allows easier detection of new test failures
470                # and regressions. Note: New failures should not be suppressed
471                # using this feature. (Also see llvm.org/PR32730)
472                self.config.available_features.add('LIBCXX-WINDOWS-FIXME')
473
474        # Attempt to detect the glibc version by querying for __GLIBC__
475        # in 'features.h'.
476        macros = self.cxx.dumpMacros(flags=['-include', 'features.h'])
477        if isinstance(macros, dict) and '__GLIBC__' in macros:
478            maj_v, min_v = (macros['__GLIBC__'], macros['__GLIBC_MINOR__'])
479            self.config.available_features.add('glibc')
480            self.config.available_features.add('glibc-%s' % maj_v)
481            self.config.available_features.add('glibc-%s.%s' % (maj_v, min_v))
482
483        # Support Objective-C++ only on MacOS and if the compiler supports it.
484        if self.target_info.platform() == "darwin" and \
485           self.target_info.is_host_macosx() and \
486           self.cxx.hasCompileFlag(["-x", "objective-c++", "-fobjc-arc"]):
487            self.config.available_features.add("objective-c++")
488
489    def configure_compile_flags(self):
490        self.configure_default_compile_flags()
491        # Configure extra flags
492        compile_flags_str = self.get_lit_conf('compile_flags', '')
493        self.cxx.compile_flags += shlex.split(compile_flags_str)
494        if self.is_windows:
495            # FIXME: Can we remove this?
496            self.cxx.compile_flags += ['-D_CRT_SECURE_NO_WARNINGS']
497            # Required so that tests using min/max don't fail on Windows,
498            # and so that those tests don't have to be changed to tolerate
499            # this insanity.
500            self.cxx.compile_flags += ['-DNOMINMAX']
501        additional_flags = self.get_lit_conf('test_compiler_flags')
502        if additional_flags:
503            self.cxx.compile_flags += shlex.split(additional_flags)
504
505    def configure_default_compile_flags(self):
506        # Try and get the std version from the command line. Fall back to
507        # default given in lit.site.cfg is not present. If default is not
508        # present then force c++11.
509        std = self.get_lit_conf('std')
510        if not std:
511            # Choose the newest possible language dialect if none is given.
512            possible_stds = ['c++2a', 'c++17', 'c++1z', 'c++14', 'c++11',
513                             'c++03']
514            if self.cxx.type == 'gcc':
515                maj_v, _, _ = self.cxx.version
516                maj_v = int(maj_v)
517                if maj_v < 7:
518                    possible_stds.remove('c++1z')
519                # FIXME: How many C++14 tests actually fail under GCC 5 and 6?
520                # Should we XFAIL them individually instead?
521                if maj_v <= 6:
522                    possible_stds.remove('c++14')
523            for s in possible_stds:
524                if self.cxx.hasCompileFlag('-std=%s' % s):
525                    std = s
526                    self.lit_config.note(
527                        'inferred language dialect as: %s' % std)
528                    break
529            if not std:
530                self.lit_config.fatal(
531                    'Failed to infer a supported language dialect from one of %r'
532                    % possible_stds)
533        self.cxx.compile_flags += ['-std={0}'.format(std)]
534        std_feature = std.replace('gnu++', 'c++')
535        std_feature = std.replace('1z', '17')
536        self.config.available_features.add(std_feature)
537        # Configure include paths
538        self.configure_compile_flags_header_includes()
539        self.target_info.add_cxx_compile_flags(self.cxx.compile_flags)
540        # Configure feature flags.
541        self.configure_compile_flags_exceptions()
542        self.configure_compile_flags_rtti()
543        self.configure_compile_flags_abi_version()
544        enable_32bit = self.get_lit_bool('enable_32bit', False)
545        if enable_32bit:
546            self.cxx.flags += ['-m32']
547        # Use verbose output for better errors
548        self.cxx.flags += ['-v']
549        sysroot = self.get_lit_conf('sysroot')
550        if sysroot:
551            self.cxx.flags += ['--sysroot', sysroot]
552        gcc_toolchain = self.get_lit_conf('gcc_toolchain')
553        if gcc_toolchain:
554            self.cxx.flags += ['-gcc-toolchain', gcc_toolchain]
555        # NOTE: the _DEBUG definition must preceed the triple check because for
556        # the Windows build of libc++, the forced inclusion of a header requires
557        # that _DEBUG is defined.  Incorrect ordering will result in -target
558        # being elided.
559        if self.is_windows and self.debug_build:
560            self.cxx.compile_flags += ['-D_DEBUG']
561        if self.use_target:
562            if not self.cxx.addFlagIfSupported(
563                    ['-target', self.config.target_triple]):
564                self.lit_config.warning('use_target is true but -target is '\
565                        'not supported by the compiler')
566        if self.use_deployment:
567            arch, name, version = self.config.deployment
568            self.cxx.flags += ['-arch', arch]
569            self.cxx.flags += ['-m' + name + '-version-min=' + version]
570
571        # Add includes for support headers used in the tests.
572        support_path = os.path.join(self.libcxx_src_root, 'test/support')
573        self.cxx.compile_flags += ['-I' + support_path]
574
575        # FIXME(EricWF): variant_size.pass.cpp requires a slightly larger
576        # template depth with older Clang versions.
577        self.cxx.addFlagIfSupported('-ftemplate-depth=270')
578
579    def configure_compile_flags_header_includes(self):
580        support_path = os.path.join(self.libcxx_src_root, 'test', 'support')
581        self.configure_config_site_header()
582        if self.cxx_stdlib_under_test != 'libstdc++' and \
583           not self.is_windows:
584            self.cxx.compile_flags += [
585                '-include', os.path.join(support_path, 'nasty_macros.hpp')]
586        if self.cxx_stdlib_under_test == 'msvc':
587            self.cxx.compile_flags += [
588                '-include', os.path.join(support_path,
589                                         'msvc_stdlib_force_include.hpp')]
590            pass
591        if self.is_windows and self.debug_build and \
592                self.cxx_stdlib_under_test != 'msvc':
593            self.cxx.compile_flags += [
594                '-include', os.path.join(support_path,
595                                         'set_windows_crt_report_mode.h')
596            ]
597        cxx_headers = self.get_lit_conf('cxx_headers')
598        if cxx_headers == '' or (cxx_headers is None
599                                 and self.cxx_stdlib_under_test != 'libc++'):
600            self.lit_config.note('using the system cxx headers')
601            return
602        self.cxx.compile_flags += ['-nostdinc++']
603        if cxx_headers is None:
604            cxx_headers = os.path.join(self.libcxx_src_root, 'include')
605        if not os.path.isdir(cxx_headers):
606            self.lit_config.fatal("cxx_headers='%s' is not a directory."
607                                  % cxx_headers)
608        self.cxx.compile_flags += ['-I' + cxx_headers]
609        if self.libcxx_obj_root is not None:
610            cxxabi_headers = os.path.join(self.libcxx_obj_root, 'include',
611                                          'c++build')
612            if os.path.isdir(cxxabi_headers):
613                self.cxx.compile_flags += ['-I' + cxxabi_headers]
614
615    def configure_config_site_header(self):
616        # Check for a possible __config_site in the build directory. We
617        # use this if it exists.
618        if self.libcxx_obj_root is None:
619            return
620        config_site_header = os.path.join(self.libcxx_obj_root, '__config_site')
621        if not os.path.isfile(config_site_header):
622            return
623        contained_macros = self.parse_config_site_and_add_features(
624            config_site_header)
625        self.lit_config.note('Using __config_site header %s with macros: %r'
626            % (config_site_header, contained_macros))
627        # FIXME: This must come after the call to
628        # 'parse_config_site_and_add_features(...)' in order for it to work.
629        self.cxx.compile_flags += ['-include', config_site_header]
630
631    def parse_config_site_and_add_features(self, header):
632        """ parse_config_site_and_add_features - Deduce and add the test
633            features that that are implied by the #define's in the __config_site
634            header. Return a dictionary containing the macros found in the
635            '__config_site' header.
636        """
637        # Parse the macro contents of __config_site by dumping the macros
638        # using 'c++ -dM -E' and filtering the predefines.
639        predefines = self._dump_macros_verbose()
640        macros = self._dump_macros_verbose(header)
641        feature_macros_keys = set(macros.keys()) - set(predefines.keys())
642        feature_macros = {}
643        for k in feature_macros_keys:
644            feature_macros[k] = macros[k]
645        # We expect the header guard to be one of the definitions
646        assert '_LIBCPP_CONFIG_SITE' in feature_macros
647        del feature_macros['_LIBCPP_CONFIG_SITE']
648        # The __config_site header should be non-empty. Otherwise it should
649        # have never been emitted by CMake.
650        assert len(feature_macros) > 0
651        # FIXME: This is a hack that should be fixed using module maps (or something)
652        # If modules are enabled then we have to lift all of the definitions
653        # in __config_site onto the command line.
654        modules_enabled = self.get_modules_enabled()
655        self.cxx.compile_flags += ['-Wno-macro-redefined']
656        # Transform each macro name into the feature name used in the tests.
657        # Ex. _LIBCPP_HAS_NO_THREADS -> libcpp-has-no-threads
658        for m in feature_macros:
659            if modules_enabled:
660                define = '-D%s' % m
661                if feature_macros[m]:
662                    define += '=%s' % (feature_macros[m])
663                self.cxx.compile_flags += [define]
664            if m == '_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS' or \
665               m == '_LIBCPP_HIDE_FROM_ABI_PER_TU_BY_DEFAULT':
666                continue
667            if m == '_LIBCPP_ABI_VERSION':
668                self.config.available_features.add('libcpp-abi-version-v%s'
669                    % feature_macros[m])
670                continue
671            if m == '_LIBCPP_NO_VCRUNTIME':
672                self.config.available_features.add('libcpp-no-vcruntime')
673                continue
674            assert m.startswith('_LIBCPP_HAS_') or m.startswith('_LIBCPP_ABI_')
675            m = m.lower()[1:].replace('_', '-')
676            self.config.available_features.add(m)
677        return feature_macros
678
679
680
681    def configure_compile_flags_exceptions(self):
682        enable_exceptions = self.get_lit_bool('enable_exceptions', True)
683        if not enable_exceptions:
684            self.config.available_features.add('libcpp-no-exceptions')
685            self.cxx.compile_flags += ['-fno-exceptions']
686
687    def configure_compile_flags_rtti(self):
688        enable_rtti = self.get_lit_bool('enable_rtti', True)
689        if not enable_rtti:
690            self.config.available_features.add('libcpp-no-rtti')
691            self.cxx.compile_flags += ['-fno-rtti', '-D_LIBCPP_NO_RTTI']
692
693    def configure_compile_flags_abi_version(self):
694        abi_version = self.get_lit_conf('abi_version', '').strip()
695        abi_unstable = self.get_lit_bool('abi_unstable')
696        # Only add the ABI version when it is non-default.
697        # FIXME(EricWF): Get the ABI version from the "__config_site".
698        if abi_version and abi_version != '1':
699          self.cxx.compile_flags += ['-D_LIBCPP_ABI_VERSION=' + abi_version]
700        if abi_unstable:
701          self.config.available_features.add('libcpp-abi-unstable')
702          self.cxx.compile_flags += ['-D_LIBCPP_ABI_UNSTABLE']
703
704    def configure_filesystem_compile_flags(self):
705        enable_fs = self.get_lit_bool('enable_filesystem', default=False)
706        if not enable_fs:
707            return
708        self.config.available_features.add('c++filesystem')
709        static_env = os.path.join(self.libcxx_src_root, 'test', 'std',
710                                  'input.output', 'filesystems', 'Inputs', 'static_test_env')
711        static_env = os.path.realpath(static_env)
712        assert os.path.isdir(static_env)
713        self.cxx.compile_flags += ['-DLIBCXX_FILESYSTEM_STATIC_TEST_ROOT="%s"' % static_env]
714
715        dynamic_env = os.path.join(self.config.test_exec_root,
716                                   'filesystem', 'Output', 'dynamic_env')
717        dynamic_env = os.path.realpath(dynamic_env)
718        if not os.path.isdir(dynamic_env):
719            os.makedirs(dynamic_env)
720        self.cxx.compile_flags += ['-DLIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT="%s"' % dynamic_env]
721        self.exec_env['LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT'] = ("%s" % dynamic_env)
722
723        dynamic_helper = os.path.join(self.libcxx_src_root, 'test', 'support',
724                                      'filesystem_dynamic_test_helper.py')
725        assert os.path.isfile(dynamic_helper)
726
727        self.cxx.compile_flags += ['-DLIBCXX_FILESYSTEM_DYNAMIC_TEST_HELPER="%s %s"'
728                                   % (sys.executable, dynamic_helper)]
729
730
731    def configure_link_flags(self):
732        # Configure library path
733        self.configure_link_flags_cxx_library_path()
734        self.configure_link_flags_abi_library_path()
735
736        # Configure libraries
737        if self.cxx_stdlib_under_test == 'libc++':
738            self.cxx.link_flags += ['-nodefaultlibs']
739            # FIXME: Handle MSVCRT as part of the ABI library handling.
740            if self.is_windows:
741                self.cxx.link_flags += ['-nostdlib']
742            self.configure_link_flags_cxx_library()
743            self.configure_link_flags_abi_library()
744            self.configure_extra_library_flags()
745        elif self.cxx_stdlib_under_test == 'libstdc++':
746            enable_fs = self.get_lit_bool('enable_filesystem',
747                                          default=False)
748            if enable_fs:
749                self.config.available_features.add('c++experimental')
750                self.cxx.link_flags += ['-lstdc++fs']
751            self.cxx.link_flags += ['-lm', '-pthread']
752        elif self.cxx_stdlib_under_test == 'msvc':
753            # FIXME: Correctly setup debug/release flags here.
754            pass
755        elif self.cxx_stdlib_under_test == 'cxx_default':
756            self.cxx.link_flags += ['-pthread']
757        else:
758            self.lit_config.fatal('invalid stdlib under test')
759
760        link_flags_str = self.get_lit_conf('link_flags', '')
761        self.cxx.link_flags += shlex.split(link_flags_str)
762
763    def configure_link_flags_cxx_library_path(self):
764        if not self.use_system_cxx_lib:
765            if self.cxx_library_root:
766                self.cxx.link_flags += ['-L' + self.cxx_library_root]
767                if self.is_windows and self.link_shared:
768                    self.add_path(self.cxx.compile_env, self.cxx_library_root)
769            if self.cxx_runtime_root:
770                if not self.is_windows:
771                    self.cxx.link_flags += ['-Wl,-rpath,' +
772                                            self.cxx_runtime_root]
773                elif self.is_windows and self.link_shared:
774                    self.add_path(self.exec_env, self.cxx_runtime_root)
775        elif os.path.isdir(str(self.use_system_cxx_lib)):
776            self.cxx.link_flags += ['-L' + self.use_system_cxx_lib]
777            if not self.is_windows:
778                self.cxx.link_flags += ['-Wl,-rpath,' +
779                                        self.use_system_cxx_lib]
780            if self.is_windows and self.link_shared:
781                self.add_path(self.cxx.compile_env, self.use_system_cxx_lib)
782        additional_flags = self.get_lit_conf('test_linker_flags')
783        if additional_flags:
784            self.cxx.link_flags += shlex.split(additional_flags)
785
786    def configure_link_flags_abi_library_path(self):
787        # Configure ABI library paths.
788        self.abi_library_root = self.get_lit_conf('abi_library_path')
789        if self.abi_library_root:
790            self.cxx.link_flags += ['-L' + self.abi_library_root]
791            if not self.is_windows:
792                self.cxx.link_flags += ['-Wl,-rpath,' + self.abi_library_root]
793            else:
794                self.add_path(self.exec_env, self.abi_library_root)
795
796    def configure_link_flags_cxx_library(self):
797        libcxx_experimental = self.get_lit_bool('enable_experimental', default=False)
798        if libcxx_experimental:
799            self.config.available_features.add('c++experimental')
800            self.cxx.link_flags += ['-lc++experimental']
801        libcxx_fs = self.get_lit_bool('enable_filesystem', default=False)
802        if libcxx_fs:
803            self.config.available_features.add('c++fs')
804            self.cxx.link_flags += ['-lc++fs']
805        if self.link_shared:
806            self.cxx.link_flags += ['-lc++']
807        else:
808            cxx_library_root = self.get_lit_conf('cxx_library_root')
809            if cxx_library_root:
810                libname = self.make_static_lib_name('c++')
811                abs_path = os.path.join(cxx_library_root, libname)
812                assert os.path.exists(abs_path) and \
813                       "static libc++ library does not exist"
814                self.cxx.link_flags += [abs_path]
815            else:
816                self.cxx.link_flags += ['-lc++']
817
818    def configure_link_flags_abi_library(self):
819        cxx_abi = self.get_lit_conf('cxx_abi', 'libcxxabi')
820        if cxx_abi == 'libstdc++':
821            self.cxx.link_flags += ['-lstdc++']
822        elif cxx_abi == 'libsupc++':
823            self.cxx.link_flags += ['-lsupc++']
824        elif cxx_abi == 'libcxxabi':
825            if self.target_info.allow_cxxabi_link():
826                libcxxabi_shared = self.get_lit_bool('libcxxabi_shared', default=True)
827                if libcxxabi_shared:
828                    self.cxx.link_flags += ['-lc++abi']
829                else:
830                    cxxabi_library_root = self.get_lit_conf('abi_library_path')
831                    if cxxabi_library_root:
832                        libname = self.make_static_lib_name('c++abi')
833                        abs_path = os.path.join(cxxabi_library_root, libname)
834                        self.cxx.link_flags += [abs_path]
835                    else:
836                        self.cxx.link_flags += ['-lc++abi']
837        elif cxx_abi == 'libcxxrt':
838            self.cxx.link_flags += ['-lcxxrt']
839        elif cxx_abi == 'vcruntime':
840            debug_suffix = 'd' if self.debug_build else ''
841            self.cxx.link_flags += ['-l%s%s' % (lib, debug_suffix) for lib in
842                                    ['vcruntime', 'ucrt', 'msvcrt']]
843        elif cxx_abi == 'none' or cxx_abi == 'default':
844            if self.is_windows:
845                debug_suffix = 'd' if self.debug_build else ''
846                self.cxx.link_flags += ['-lmsvcrt%s' % debug_suffix]
847        else:
848            self.lit_config.fatal(
849                'C++ ABI setting %s unsupported for tests' % cxx_abi)
850
851    def configure_extra_library_flags(self):
852        if self.get_lit_bool('cxx_ext_threads', default=False):
853            self.cxx.link_flags += ['-lc++external_threads']
854        self.target_info.add_cxx_link_flags(self.cxx.link_flags)
855
856    def configure_color_diagnostics(self):
857        use_color = self.get_lit_conf('color_diagnostics')
858        if use_color is None:
859            use_color = os.environ.get('LIBCXX_COLOR_DIAGNOSTICS')
860        if use_color is None:
861            return
862        if use_color != '':
863            self.lit_config.fatal('Invalid value for color_diagnostics "%s".'
864                                  % use_color)
865        color_flag = '-fdiagnostics-color=always'
866        # Check if the compiler supports the color diagnostics flag. Issue a
867        # warning if it does not since color diagnostics have been requested.
868        if not self.cxx.hasCompileFlag(color_flag):
869            self.lit_config.warning(
870                'color diagnostics have been requested but are not supported '
871                'by the compiler')
872        else:
873            self.cxx.flags += [color_flag]
874
875    def configure_debug_mode(self):
876        debug_level = self.get_lit_conf('debug_level', None)
877        if not debug_level:
878            return
879        if debug_level not in ['0', '1']:
880            self.lit_config.fatal('Invalid value for debug_level "%s".'
881                                  % debug_level)
882        self.cxx.compile_flags += ['-D_LIBCPP_DEBUG=%s' % debug_level]
883
884    def configure_warnings(self):
885        # Turn on warnings by default for Clang based compilers when C++ >= 11
886        default_enable_warnings = self.cxx.type in ['clang', 'apple-clang'] \
887            and len(self.config.available_features.intersection(
888                ['c++11', 'c++14', 'c++17', 'c++2a'])) != 0
889        enable_warnings = self.get_lit_bool('enable_warnings',
890                                            default_enable_warnings)
891        self.cxx.useWarnings(enable_warnings)
892        self.cxx.warning_flags += [
893            '-D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER',
894            '-Wall', '-Wextra', '-Werror'
895        ]
896        if self.cxx.hasWarningFlag('-Wuser-defined-warnings'):
897            self.cxx.warning_flags += ['-Wuser-defined-warnings']
898            self.config.available_features.add('diagnose-if-support')
899        self.cxx.addWarningFlagIfSupported('-Wshadow')
900        self.cxx.addWarningFlagIfSupported('-Wno-unused-command-line-argument')
901        self.cxx.addWarningFlagIfSupported('-Wno-attributes')
902        self.cxx.addWarningFlagIfSupported('-Wno-pessimizing-move')
903        self.cxx.addWarningFlagIfSupported('-Wno-c++11-extensions')
904        self.cxx.addWarningFlagIfSupported('-Wno-user-defined-literals')
905        self.cxx.addWarningFlagIfSupported('-Wno-noexcept-type')
906        self.cxx.addWarningFlagIfSupported('-Wno-aligned-allocation-unavailable')
907        # These warnings should be enabled in order to support the MSVC
908        # team using the test suite; They enable the warnings below and
909        # expect the test suite to be clean.
910        self.cxx.addWarningFlagIfSupported('-Wsign-compare')
911        self.cxx.addWarningFlagIfSupported('-Wunused-variable')
912        self.cxx.addWarningFlagIfSupported('-Wunused-parameter')
913        self.cxx.addWarningFlagIfSupported('-Wunreachable-code')
914        std = self.get_lit_conf('std', None)
915        if std in ['c++98', 'c++03']:
916            # The '#define static_assert' provided by libc++ in C++03 mode
917            # causes an unused local typedef whenever it is used.
918            self.cxx.addWarningFlagIfSupported('-Wno-unused-local-typedef')
919
920    def configure_sanitizer(self):
921        san = self.get_lit_conf('use_sanitizer', '').strip()
922        if san:
923            self.target_info.add_sanitizer_features(san, self.config.available_features)
924            # Search for llvm-symbolizer along the compiler path first
925            # and then along the PATH env variable.
926            symbolizer_search_paths = os.environ.get('PATH', '')
927            cxx_path = libcxx.util.which(self.cxx.path)
928            if cxx_path is not None:
929                symbolizer_search_paths = (
930                    os.path.dirname(cxx_path) +
931                    os.pathsep + symbolizer_search_paths)
932            llvm_symbolizer = libcxx.util.which('llvm-symbolizer',
933                                                symbolizer_search_paths)
934
935            def add_ubsan():
936                self.cxx.flags += ['-fsanitize=undefined',
937                                   '-fno-sanitize=vptr,function,float-divide-by-zero',
938                                   '-fno-sanitize-recover=all']
939                self.exec_env['UBSAN_OPTIONS'] = 'print_stacktrace=1'
940                self.config.available_features.add('ubsan')
941
942            # Setup the sanitizer compile flags
943            self.cxx.flags += ['-g', '-fno-omit-frame-pointer']
944            if san == 'Address' or san == 'Address;Undefined' or san == 'Undefined;Address':
945                self.cxx.flags += ['-fsanitize=address']
946                if llvm_symbolizer is not None:
947                    self.exec_env['ASAN_SYMBOLIZER_PATH'] = llvm_symbolizer
948                # FIXME: Turn ODR violation back on after PR28391 is resolved
949                # https://bugs.llvm.org/show_bug.cgi?id=28391
950                self.exec_env['ASAN_OPTIONS'] = 'detect_odr_violation=0'
951                self.config.available_features.add('asan')
952                self.config.available_features.add('sanitizer-new-delete')
953                self.cxx.compile_flags += ['-O1']
954                if san == 'Address;Undefined' or san == 'Undefined;Address':
955                    add_ubsan()
956            elif san == 'Memory' or san == 'MemoryWithOrigins':
957                self.cxx.flags += ['-fsanitize=memory']
958                if san == 'MemoryWithOrigins':
959                    self.cxx.compile_flags += [
960                        '-fsanitize-memory-track-origins']
961                if llvm_symbolizer is not None:
962                    self.exec_env['MSAN_SYMBOLIZER_PATH'] = llvm_symbolizer
963                self.config.available_features.add('msan')
964                self.config.available_features.add('sanitizer-new-delete')
965                self.cxx.compile_flags += ['-O1']
966            elif san == 'Undefined':
967                add_ubsan()
968                self.cxx.compile_flags += ['-O2']
969            elif san == 'Thread':
970                self.cxx.flags += ['-fsanitize=thread']
971                self.config.available_features.add('tsan')
972                self.config.available_features.add('sanitizer-new-delete')
973            else:
974                self.lit_config.fatal('unsupported value for '
975                                      'use_sanitizer: {0}'.format(san))
976            san_lib = self.get_lit_conf('sanitizer_library')
977            if san_lib:
978                self.cxx.link_flags += [
979                    san_lib, '-Wl,-rpath,%s' % os.path.dirname(san_lib)]
980
981    def configure_coverage(self):
982        self.generate_coverage = self.get_lit_bool('generate_coverage', False)
983        if self.generate_coverage:
984            self.cxx.flags += ['-g', '--coverage']
985            self.cxx.compile_flags += ['-O0']
986
987    def configure_coroutines(self):
988        if self.cxx.hasCompileFlag('-fcoroutines-ts'):
989            macros = self._dump_macros_verbose(flags=['-fcoroutines-ts'])
990            if '__cpp_coroutines' not in macros:
991                self.lit_config.warning('-fcoroutines-ts is supported but '
992                    '__cpp_coroutines is not defined')
993            # Consider coroutines supported only when the feature test macro
994            # reflects a recent value.
995            if intMacroValue(macros['__cpp_coroutines']) >= 201703:
996                self.config.available_features.add('fcoroutines-ts')
997
998    def configure_modules(self):
999        modules_flags = ['-fmodules']
1000        if platform.system() != 'Darwin':
1001            modules_flags += ['-Xclang', '-fmodules-local-submodule-visibility']
1002        supports_modules = self.cxx.hasCompileFlag(modules_flags)
1003        enable_modules = self.get_modules_enabled()
1004        if enable_modules and not supports_modules:
1005            self.lit_config.fatal(
1006                '-fmodules is enabled but not supported by the compiler')
1007        if not supports_modules:
1008            return
1009        self.config.available_features.add('modules-support')
1010        module_cache = os.path.join(self.config.test_exec_root,
1011                                   'modules.cache')
1012        module_cache = os.path.realpath(module_cache)
1013        if os.path.isdir(module_cache):
1014            shutil.rmtree(module_cache)
1015        os.makedirs(module_cache)
1016        self.cxx.modules_flags = modules_flags + \
1017            ['-fmodules-cache-path=' + module_cache]
1018        if enable_modules:
1019            self.config.available_features.add('-fmodules')
1020            self.cxx.useModules()
1021
1022    def configure_substitutions(self):
1023        sub = self.config.substitutions
1024        cxx_path = pipes.quote(self.cxx.path)
1025        # Configure compiler substitutions
1026        sub.append(('%cxx', cxx_path))
1027        sub.append(('%libcxx_src_root', self.libcxx_src_root))
1028        # Configure flags substitutions
1029        flags_str = ' '.join([pipes.quote(f) for f in self.cxx.flags])
1030        compile_flags_str = ' '.join([pipes.quote(f) for f in self.cxx.compile_flags])
1031        link_flags_str = ' '.join([pipes.quote(f) for f in self.cxx.link_flags])
1032        all_flags = '%s %s %s' % (flags_str, compile_flags_str, link_flags_str)
1033        sub.append(('%flags', flags_str))
1034        sub.append(('%compile_flags', compile_flags_str))
1035        sub.append(('%link_flags', link_flags_str))
1036        sub.append(('%all_flags', all_flags))
1037        if self.cxx.isVerifySupported():
1038            verify_str = ' ' + ' '.join(self.cxx.verify_flags) + ' '
1039            sub.append(('%verify', verify_str))
1040        # Add compile and link shortcuts
1041        compile_str = (cxx_path + ' -o %t.o %s -c ' + flags_str
1042                       + ' ' + compile_flags_str)
1043        link_str = (cxx_path + ' -o %t.exe %t.o ' + flags_str + ' '
1044                    + link_flags_str)
1045        assert type(link_str) is str
1046        build_str = cxx_path + ' -o %t.exe %s ' + all_flags
1047        if self.cxx.use_modules:
1048            sub.append(('%compile_module', compile_str))
1049            sub.append(('%build_module', build_str))
1050        elif self.cxx.modules_flags is not None:
1051            modules_str = ' '.join(self.cxx.modules_flags) + ' '
1052            sub.append(('%compile_module', compile_str + ' ' + modules_str))
1053            sub.append(('%build_module', build_str + ' ' + modules_str))
1054        sub.append(('%compile', compile_str))
1055        sub.append(('%link', link_str))
1056        sub.append(('%build', build_str))
1057        # Configure exec prefix substitutions.
1058        # Configure run env substitution.
1059        sub.append(('%run', '%t.exe'))
1060        # Configure not program substitutions
1061        not_py = os.path.join(self.libcxx_src_root, 'utils', 'not.py')
1062        not_str = '%s %s ' % (pipes.quote(sys.executable), pipes.quote(not_py))
1063        sub.append(('not ', not_str))
1064
1065    def can_use_deployment(self):
1066        # Check if the host is on an Apple platform using clang.
1067        if not self.target_info.platform() == "darwin":
1068            return False
1069        if not self.target_info.is_host_macosx():
1070            return False
1071        if not self.cxx.type.endswith('clang'):
1072            return False
1073        return True
1074
1075    def configure_triple(self):
1076        # Get or infer the target triple.
1077        target_triple = self.get_lit_conf('target_triple')
1078        self.use_target = self.get_lit_bool('use_target', False)
1079        if self.use_target and target_triple:
1080            self.lit_config.warning('use_target is true but no triple is specified')
1081
1082        # Use deployment if possible.
1083        self.use_deployment = not self.use_target and self.can_use_deployment()
1084        if self.use_deployment:
1085            return
1086
1087        # Save the triple (and warn on Apple platforms).
1088        self.config.target_triple = target_triple
1089        if self.use_target and 'apple' in target_triple:
1090            self.lit_config.warning('consider using arch and platform instead'
1091                                    ' of target_triple on Apple platforms')
1092
1093        # If no target triple was given, try to infer it from the compiler
1094        # under test.
1095        if not self.config.target_triple:
1096            target_triple = self.cxx.getTriple()
1097            # Drop sub-major version components from the triple, because the
1098            # current XFAIL handling expects exact matches for feature checks.
1099            # Example: x86_64-apple-darwin14.0.0 -> x86_64-apple-darwin14
1100            # The 5th group handles triples greater than 3 parts
1101            # (ex x86_64-pc-linux-gnu).
1102            target_triple = re.sub(r'([^-]+)-([^-]+)-([^.]+)([^-]*)(.*)',
1103                                   r'\1-\2-\3\5', target_triple)
1104            # linux-gnu is needed in the triple to properly identify linuxes
1105            # that use GLIBC. Handle redhat and opensuse triples as special
1106            # cases and append the missing `-gnu` portion.
1107            if (target_triple.endswith('redhat-linux') or
1108                target_triple.endswith('suse-linux')):
1109                target_triple += '-gnu'
1110            self.config.target_triple = target_triple
1111            self.lit_config.note(
1112                "inferred target_triple as: %r" % self.config.target_triple)
1113
1114    def configure_deployment(self):
1115        assert not self.use_deployment is None
1116        assert not self.use_target is None
1117        if not self.use_deployment:
1118            # Warn about ignored parameters.
1119            if self.get_lit_conf('arch'):
1120                self.lit_config.warning('ignoring arch, using target_triple')
1121            if self.get_lit_conf('platform'):
1122                self.lit_config.warning('ignoring platform, using target_triple')
1123            return
1124
1125        assert not self.use_target
1126        assert self.target_info.is_host_macosx()
1127
1128        # Always specify deployment explicitly on Apple platforms, since
1129        # otherwise a platform is picked up from the SDK.  If the SDK version
1130        # doesn't match the system version, tests that use the system library
1131        # may fail spuriously.
1132        arch = self.get_lit_conf('arch')
1133        if not arch:
1134            arch = self.cxx.getTriple().split('-', 1)[0]
1135            self.lit_config.note("inferred arch as: %r" % arch)
1136
1137        inferred_platform, name, version = self.target_info.get_platform()
1138        if inferred_platform:
1139            self.lit_config.note("inferred platform as: %r" % (name + version))
1140        self.config.deployment = (arch, name, version)
1141
1142        # Set the target triple for use by lit.
1143        self.config.target_triple = arch + '-apple-' + name + version
1144        self.lit_config.note(
1145            "computed target_triple as: %r" % self.config.target_triple)
1146
1147    def configure_env(self):
1148        self.target_info.configure_env(self.exec_env)
1149
1150    def add_path(self, dest_env, new_path):
1151        if 'PATH' not in dest_env:
1152            dest_env['PATH'] = new_path
1153        else:
1154            split_char = ';' if self.is_windows else ':'
1155            dest_env['PATH'] = '%s%s%s' % (new_path, split_char,
1156                                           dest_env['PATH'])
1157