[lit] Forward LD_PRELOAD to tests.
[oota-llvm.git] / utils / lit / lit / TestingConfig.py
1 import os
2 import sys
3
4 OldPy = sys.version_info[0] == 2 and sys.version_info[1] < 7
5
6 class TestingConfig:
7     """"
8     TestingConfig - Information on the tests inside a suite.
9     """
10
11     @staticmethod
12     def fromdefaults(litConfig):
13         """
14         fromdefaults(litConfig) -> TestingConfig
15
16         Create a TestingConfig object with default values.
17         """
18         # Set the environment based on the command line arguments.
19         environment = {
20             'LIBRARY_PATH' : os.environ.get('LIBRARY_PATH',''),
21             'LD_LIBRARY_PATH' : os.environ.get('LD_LIBRARY_PATH',''),
22             'PATH' : os.pathsep.join(litConfig.path +
23                                      [os.environ.get('PATH','')]),
24             'SYSTEMROOT' : os.environ.get('SYSTEMROOT',''),
25             'TERM' : os.environ.get('TERM',''),
26             'LLVM_DISABLE_CRASH_REPORT' : '1',
27             'LD_PRELOAD' : os.environ.get('LD_PRELOAD',''),
28             }
29
30         if sys.platform == 'win32':
31             environment.update({
32                     'INCLUDE' : os.environ.get('INCLUDE',''),
33                     'PATHEXT' : os.environ.get('PATHEXT',''),
34                     'PYTHONUNBUFFERED' : '1',
35                     'TEMP' : os.environ.get('TEMP',''),
36                     'TMP' : os.environ.get('TMP',''),
37                     })
38
39         # The option to preserve TEMP, TMP, and TMPDIR.
40         # This is intended to check how many temporary files would be generated
41         # (and be not cleaned up) in automated builders.
42         if 'LIT_PRESERVES_TMP' in os.environ:
43             environment.update({
44                     'TEMP' : os.environ.get('TEMP',''),
45                     'TMP' : os.environ.get('TMP',''),
46                     'TMPDIR' : os.environ.get('TMPDIR',''),
47                     })
48
49         # Set the default available features based on the LitConfig.
50         available_features = []
51         if litConfig.useValgrind:
52             available_features.append('valgrind')
53             if litConfig.valgrindLeakCheck:
54                 available_features.append('vg_leak')
55
56         return TestingConfig(None,
57                              name = '<unnamed>',
58                              suffixes = set(),
59                              test_format = None,
60                              environment = environment,
61                              substitutions = [],
62                              unsupported = False,
63                              test_exec_root = None,
64                              test_source_root = None,
65                              excludes = [],
66                              available_features = available_features,
67                              pipefail = True)
68
69     def load_from_path(self, path, litConfig):
70         """
71         load_from_path(path, litConfig)
72
73         Load the configuration module at the provided path into the given config
74         object.
75         """
76
77         # Load the config script data.
78         data = None
79         if not OldPy:
80             f = open(path)
81             try:
82                 data = f.read()
83             except:
84                 litConfig.fatal('unable to load config file: %r' % (path,))
85             f.close()
86
87         # Execute the config script to initialize the object.
88         cfg_globals = dict(globals())
89         cfg_globals['config'] = self
90         cfg_globals['lit_config'] = litConfig
91         cfg_globals['__file__'] = path
92         try:
93             if OldPy:
94                 execfile(path, cfg_globals)
95             else:
96                 exec(compile(data, path, 'exec'), cfg_globals, None)
97             if litConfig.debug:
98                 litConfig.note('... loaded config %r' % path)
99         except SystemExit:
100             e = sys.exc_info()[1]
101             # We allow normal system exit inside a config file to just
102             # return control without error.
103             if e.args:
104                 raise
105         except:
106             import traceback
107             litConfig.fatal(
108                 'unable to parse config file %r, traceback: %s' % (
109                     path, traceback.format_exc()))
110
111         self.finish(litConfig)
112
113     def __init__(self, parent, name, suffixes, test_format,
114                  environment, substitutions, unsupported,
115                  test_exec_root, test_source_root, excludes,
116                  available_features, pipefail):
117         self.parent = parent
118         self.name = str(name)
119         self.suffixes = set(suffixes)
120         self.test_format = test_format
121         self.environment = dict(environment)
122         self.substitutions = list(substitutions)
123         self.unsupported = unsupported
124         self.test_exec_root = test_exec_root
125         self.test_source_root = test_source_root
126         self.excludes = set(excludes)
127         self.available_features = set(available_features)
128         self.pipefail = pipefail
129
130     def finish(self, litConfig):
131         """finish() - Finish this config object, after loading is complete."""
132
133         self.name = str(self.name)
134         self.suffixes = set(self.suffixes)
135         self.environment = dict(self.environment)
136         self.substitutions = list(self.substitutions)
137         if self.test_exec_root is not None:
138             # FIXME: This should really only be suite in test suite config
139             # files. Should we distinguish them?
140             self.test_exec_root = str(self.test_exec_root)
141         if self.test_source_root is not None:
142             # FIXME: This should really only be suite in test suite config
143             # files. Should we distinguish them?
144             self.test_source_root = str(self.test_source_root)
145         self.excludes = set(self.excludes)
146
147     @property
148     def root(self):
149         """root attribute - The root configuration for the test suite."""
150         if self.parent is None:
151             return self
152         else:
153             return self.parent.root
154