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