summaryrefslogtreecommitdiff
path: root/utils/lit/lit/LitConfig.py
blob: cb216ea1f44fd5089feb349f9cf485025dbf88f7 (plain)
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
class LitConfig:
    """LitConfig - Configuration data for a 'lit' test runner instance, shared
    across all tests.

    The LitConfig object is also used to communicate with client configuration
    files, it is always passed in as the global variable 'lit' so that
    configuration files can access common functionality and internal components
    easily.
    """

    # Provide access to Test module.
    import Test

    # Provide access to built-in formats.
    import TestFormats as formats

    # Provide access to built-in utility functions.
    import Util as util

    def __init__(self, progname, path, quiet,
                 useValgrind, valgrindLeakCheck, valgrindArgs,
                 noExecute, debug, isWindows,
                 params, config_prefix = None):
        # The name of the test runner.
        self.progname = progname
        # The items to add to the PATH environment variable.
        self.path = list(map(str, path))
        self.quiet = bool(quiet)
        self.useValgrind = bool(useValgrind)
        self.valgrindLeakCheck = bool(valgrindLeakCheck)
        self.valgrindUserArgs = list(valgrindArgs)
        self.noExecute = noExecute
        self.debug = debug
        self.isWindows = bool(isWindows)
        self.params = dict(params)
        self.bashPath = None

        # Configuration files to look for when discovering test suites.
        self.config_prefix = config_prefix or 'lit'
        self.config_name = '%s.cfg' % (self.config_prefix,)
        self.site_config_name = '%s.site.cfg' % (self.config_prefix,)
        self.local_config_name = '%s.local.cfg' % (self.config_prefix,)

        self.numErrors = 0
        self.numWarnings = 0

        self.valgrindArgs = []
        if self.useValgrind:
            self.valgrindArgs = ['valgrind', '-q', '--run-libc-freeres=no',
                                 '--tool=memcheck', '--trace-children=yes',
                                 '--error-exitcode=123']
            if self.valgrindLeakCheck:
                self.valgrindArgs.append('--leak-check=full')
            else:
                # The default is 'summary'.
                self.valgrindArgs.append('--leak-check=no')
            self.valgrindArgs.extend(self.valgrindUserArgs)


    def load_config(self, config, path):
        """load_config(config, path) - Load a config object from an alternate
        path."""
        from TestingConfig import TestingConfig
        if self.debug:
            self.note('load_config from %r' % path)
        return TestingConfig.frompath(path, config.parent, self,
                                      mustExist = True,
                                      config = config)

    def getBashPath(self):
        """getBashPath - Get the path to 'bash'"""
        import os, Util

        if self.bashPath is not None:
            return self.bashPath

        self.bashPath = Util.which('bash', os.pathsep.join(self.path))
        if self.bashPath is None:
            # Check some known paths.
            for path in ('/bin/bash', '/usr/bin/bash', '/usr/local/bin/bash'):
                if os.path.exists(path):
                    self.bashPath = path
                    break

        if self.bashPath is None:
            self.warning("Unable to find 'bash'.")
            self.bashPath = ''

        return self.bashPath

    def getToolsPath(self, dir, paths, tools):
        import os, Util
        if dir is not None and os.path.isabs(dir) and os.path.isdir(dir):
            if not Util.checkToolsPath(dir, tools):
                return None
        else:
            dir = Util.whichTools(tools, paths)

        # bash
        self.bashPath = Util.which('bash', dir)
        if self.bashPath is None:
            self.note("Unable to find 'bash.exe'.")
            self.bashPath = ''

        return dir

    def _write_message(self, kind, message):
        import inspect, os, sys

        # Get the file/line where this message was generated.
        f = inspect.currentframe()
        # Step out of _write_message, and then out of wrapper.
        f = f.f_back.f_back
        file,line,_,_,_ = inspect.getframeinfo(f)
        location = '%s:%d' % (os.path.basename(file), line)

        print >>sys.stderr, '%s: %s: %s: %s' % (self.progname, location,
                                                kind, message)

    def note(self, message):
        self._write_message('note', message)

    def warning(self, message):
        self._write_message('warning', message)
        self.numWarnings += 1

    def error(self, message):
        self._write_message('error', message)
        self.numErrors += 1

    def fatal(self, message):
        import sys
        self._write_message('fatal', message)
        sys.exit(2)