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