blob: 3dee99652c615ff8dc378c006d7c5560926c39c4 [file] [log] [blame]
Daniel Dunbar3667b992009-07-31 05:54:17 +00001# -*- Python -*-
2
Daniel Dunbarb5cbf772009-09-22 05:16:02 +00003import os
Daniel Dunbar79327b62009-09-22 10:08:03 +00004import platform
Chandler Carruth2837f662011-11-05 20:55:50 +00005import re
6import subprocess
7
Daniel Dunbarbe4253a2009-09-08 16:39:23 +00008
Daniel Dunbar3667b992009-07-31 05:54:17 +00009# Configuration file for the 'lit' test runner.
10
Daniel Dunbarb5cbf772009-09-22 05:16:02 +000011# name: The name of this test suite.
12config.name = 'Clang'
Daniel Dunbarbe4253a2009-09-08 16:39:23 +000013
NAKAMURA Takumi1e8200d2011-02-09 04:19:57 +000014# Tweak PATH for Win32
15if platform.system() == 'Windows':
16 # Seek sane tools in directories and set to $PATH.
17 path = getattr(config, 'lit_tools_dir', None)
18 path = lit.getToolsPath(path,
19 config.environment['PATH'],
20 ['cmp.exe', 'grep.exe', 'sed.exe'])
21 if path is not None:
22 path = os.path.pathsep.join((path,
23 config.environment['PATH']))
24 config.environment['PATH'] = path
25
Daniel Dunbarb5cbf772009-09-22 05:16:02 +000026# testFormat: The test format to use to interpret tests.
Daniel Dunbar3667b992009-07-31 05:54:17 +000027#
Daniel Dunbarb5cbf772009-09-22 05:16:02 +000028# For now we require '&&' between commands, until they get globally killed and
29# the test runner updated.
NAKAMURA Takumi70f5be62011-03-05 11:16:06 +000030execute_external = (platform.system() != 'Windows'
31 or lit.getBashPath() not in [None, ""])
Daniel Dunbard90e0a12009-11-08 01:47:35 +000032config.test_format = lit.formats.ShTest(execute_external)
Daniel Dunbarf87be552009-09-06 01:31:12 +000033
Daniel Dunbarb5cbf772009-09-22 05:16:02 +000034# suffixes: A list of file extensions to treat as test files.
Jim Grosbach576452b2012-02-10 20:37:10 +000035config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s']
Daniel Dunbarb5cbf772009-09-22 05:16:02 +000036
37# test_source_root: The root path where tests are located.
38config.test_source_root = os.path.dirname(__file__)
39
40# test_exec_root: The root path where tests should be run.
41clang_obj_root = getattr(config, 'clang_obj_root', None)
42if clang_obj_root is not None:
43 config.test_exec_root = os.path.join(clang_obj_root, 'test')
44
45# Set llvm_{src,obj}_root for use by others.
46config.llvm_src_root = getattr(config, 'llvm_src_root', None)
47config.llvm_obj_root = getattr(config, 'llvm_obj_root', None)
48
Jordy Rose0e09fac2012-04-06 18:14:01 +000049# Clear some environment variables that might affect Clang.
50#
51# This first set of vars are read by Clang, but shouldn't affect tests
52# that aren't specifically looking for these features, or are required
53# simply to run the tests at all.
54#
55# FIXME: Should we have a tool that enforces this?
56
57# safe_env_vars = ('TMPDIR', 'TEMP', 'TMP', 'USERPROFILE', 'PWD',
58# 'MACOSX_DEPLOYMENT_TARGET', 'IPHONEOS_DEPLOYMENT_TARGET',
59# 'IOS_SIMULATOR_DEPLOYMENT_TARGET',
60# 'VCINSTALLDIR', 'VC100COMNTOOLS', 'VC90COMNTOOLS',
61# 'VC80COMNTOOLS')
62possibly_dangerous_env_vars = ('COMPILER_PATH', 'RC_DEBUG_OPTIONS', 'INCLUDE',
63 'CINDEXTEST_PREAMBLE_FILE', 'LIBRARY_PATH',
64 'CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH',
65 'OBJC_INCLUDE_PATH', 'OBJCPLUS_INCLUDE_PATH',
66 'LIBCLANG_TIMING', 'LIBCLANG_OBJTRACKING',
67 'LIBCLANG_LOGGING', 'LIBCLANG_BGPRIO_INDEX',
68 'LIBCLANG_BGPRIO_EDIT', 'LIBCLANG_NOTHREADS',
69 'LIBCLANG_RESOURCE_USAGE',
70 'LIBCLANG_CODE_COMPLETION_LOGGING')
71for name in possibly_dangerous_env_vars:
72 if name in config.environment:
73 del config.environment[name]
74
Daniel Dunbarb5cbf772009-09-22 05:16:02 +000075# Tweak the PATH to include the tools dir and the scripts dir.
76if clang_obj_root is not None:
77 llvm_tools_dir = getattr(config, 'llvm_tools_dir', None)
78 if not llvm_tools_dir:
79 lit.fatal('No LLVM tools dir set!')
Daniel Dunbarc437eb82009-09-24 06:31:08 +000080 path = os.path.pathsep.join((llvm_tools_dir, config.environment['PATH']))
Daniel Dunbarb5cbf772009-09-22 05:16:02 +000081 config.environment['PATH'] = path
82
Daniel Dunbara87097a2009-09-26 07:36:09 +000083 llvm_libs_dir = getattr(config, 'llvm_libs_dir', None)
84 if not llvm_libs_dir:
85 lit.fatal('No LLVM libs dir set!')
86 path = os.path.pathsep.join((llvm_libs_dir,
87 config.environment.get('LD_LIBRARY_PATH','')))
88 config.environment['LD_LIBRARY_PATH'] = path
89
Daniel Dunbarb5cbf772009-09-22 05:16:02 +000090###
91
92# Check that the object root is known.
93if config.test_exec_root is None:
94 # Otherwise, we haven't loaded the site specific configuration (the user is
95 # probably trying to run on a test file directly, and either the site
96 # configuration hasn't been created by the build system, or we are in an
97 # out-of-tree build situation).
98
Daniel Dunbard3f630f2009-11-05 16:36:19 +000099 # Check for 'clang_site_config' user parameter, and use that if available.
100 site_cfg = lit.params.get('clang_site_config', None)
101 if site_cfg and os.path.exists(site_cfg):
102 lit.load_config(config, site_cfg)
103 raise SystemExit
104
Daniel Dunbarb5cbf772009-09-22 05:16:02 +0000105 # Try to detect the situation where we are using an out-of-tree build by
106 # looking for 'llvm-config'.
107 #
108 # FIXME: I debated (i.e., wrote and threw away) adding logic to
109 # automagically generate the lit.site.cfg if we are in some kind of fresh
Daniel Dunbar8466a0d2009-11-07 23:53:17 +0000110 # build situation. This means knowing how to invoke the build system though,
111 # and I decided it was too much magic. We should solve this by just having
112 # the .cfg files generated during the configuration step.
Daniel Dunbarb5cbf772009-09-22 05:16:02 +0000113
114 llvm_config = lit.util.which('llvm-config', config.environment['PATH'])
115 if not llvm_config:
116 lit.fatal('No site specific configuration available!')
117
118 # Get the source and object roots.
119 llvm_src_root = lit.util.capture(['llvm-config', '--src-root']).strip()
120 llvm_obj_root = lit.util.capture(['llvm-config', '--obj-root']).strip()
121 clang_src_root = os.path.join(llvm_src_root, "tools", "clang")
122 clang_obj_root = os.path.join(llvm_obj_root, "tools", "clang")
123
124 # Validate that we got a tree which points to here, using the standard
125 # tools/clang layout.
126 this_src_root = os.path.dirname(config.test_source_root)
127 if os.path.realpath(clang_src_root) != os.path.realpath(this_src_root):
128 lit.fatal('No site specific configuration available!')
129
130 # Check that the site specific configuration exists.
131 site_cfg = os.path.join(clang_obj_root, 'test', 'lit.site.cfg')
132 if not os.path.exists(site_cfg):
Nico Webera384e9a2010-09-27 20:40:32 +0000133 lit.fatal('No site specific configuration available! You may need to '
134 'run "make test" in your Clang build directory.')
Daniel Dunbarb5cbf772009-09-22 05:16:02 +0000135
136 # Okay, that worked. Notify the user of the automagic, and reconfigure.
137 lit.note('using out-of-tree build at %r' % clang_obj_root)
138 lit.load_config(config, site_cfg)
139 raise SystemExit
140
141###
142
143# Discover the 'clang' and 'clangcc' to use.
144
145import os
146
147def inferClang(PATH):
148 # Determine which clang to use.
149 clang = os.getenv('CLANG')
150
151 # If the user set clang in the environment, definitely use that and don't
152 # try to validate.
153 if clang:
154 return clang
155
156 # Otherwise look in the path.
157 clang = lit.util.which('clang', PATH)
158
159 if not clang:
160 lit.fatal("couldn't find 'clang' program, try setting "
161 "CLANG in your environment")
162
163 return clang
164
Daniel Dunbar9b9a46c2010-03-20 21:13:08 +0000165# When running under valgrind, we mangle '-vg' onto the end of the triple so we
166# can check it with XFAIL and XTARGET.
167if lit.useValgrind:
168 config.target_triple += '-vg'
169
NAKAMURA Takumi70f5be62011-03-05 11:16:06 +0000170config.clang = inferClang(config.environment['PATH']).replace('\\', '/')
Daniel Dunbarb5cbf772009-09-22 05:16:02 +0000171if not lit.quiet:
172 lit.note('using clang: %r' % config.clang)
Chandler Carruthc0b1b862011-11-05 10:15:27 +0000173
174# Note that when substituting %clang_cc1 also fill in the include directory of
175# the builtin headers. Those are part of even a freestanding environment, but
176# Clang relies on the driver to locate them.
Chandler Carruth34146d82011-11-05 23:29:28 +0000177def getClangBuiltinIncludeDir(clang):
Chandler Carruth2837f662011-11-05 20:55:50 +0000178 # FIXME: Rather than just getting the version, we should have clang print
179 # out its resource dir here in an easy to scrape form.
Chandler Carruth34146d82011-11-05 23:29:28 +0000180 cmd = subprocess.Popen([clang, '-print-file-name=include'],
181 stdout=subprocess.PIPE)
182 if not cmd.stdout:
183 lit.fatal("Couldn't find the include dir for Clang ('%s')" % clang)
184 return cmd.stdout.read().strip()
Chandler Carruth2837f662011-11-05 20:55:50 +0000185
Chandler Carrutha62ba812011-11-07 09:17:31 +0000186config.substitutions.append( ('%clang_cc1', '%s -cc1 -internal-isystem %s'
Chandler Carruth34146d82011-11-05 23:29:28 +0000187 % (config.clang,
188 getClangBuiltinIncludeDir(config.clang))) )
Chandler Carruthc0b1b862011-11-05 10:15:27 +0000189
Daniel Dunbar8452ef02010-06-29 16:52:24 +0000190config.substitutions.append( ('%clangxx', ' ' + config.clang +
191 ' -ccc-clang-cxx -ccc-cxx '))
Daniel Dunbar5618e982009-12-15 22:01:24 +0000192config.substitutions.append( ('%clang', ' ' + config.clang + ' ') )
Devang Patel068b5b32010-09-13 20:46:23 +0000193config.substitutions.append( ('%test_debuginfo', ' ' + config.llvm_src_root + '/utils/test_debuginfo.pl ') )
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +0000194
Daniel Dunbar5618e982009-12-15 22:01:24 +0000195# FIXME: Find nicer way to prohibit this.
196config.substitutions.append(
197 (' clang ', """*** Do not use 'clang' in tests, use '%clang'. ***""") )
198config.substitutions.append(
David Greene7b293452011-01-03 17:28:52 +0000199 (' clang\+\+ ', """*** Do not use 'clang++' in tests, use '%clangxx'. ***"""))
Daniel Dunbar78c974f2010-02-17 20:31:01 +0000200config.substitutions.append(
Daniel Dunbar5618e982009-12-15 22:01:24 +0000201 (' clang-cc ',
202 """*** Do not use 'clang-cc' in tests, use '%clang_cc1'. ***""") )
203config.substitutions.append(
204 (' clang -cc1 ',
205 """*** Do not use 'clang -cc1' in tests, use '%clang_cc1'. ***""") )
Daniel Dunbar8452ef02010-06-29 16:52:24 +0000206config.substitutions.append(
207 (' %clang-cc1 ',
208 """*** invalid substitution, use '%clang_cc1'. ***""") )
Daniel Dunbarb44eb0b2010-08-24 21:39:55 +0000209
210###
211
212# Set available features we allow tests to conditionalize on.
Andrew Trick3df28242011-08-26 22:46:31 +0000213#
214# As of 2011.08, crash-recovery tests still do not pass on FreeBSD.
215if platform.system() not in ['FreeBSD']:
216 config.available_features.add('crash-recovery')
NAKAMURA Takumif5ea88b2011-02-28 09:41:07 +0000217
218# Shell execution
219if platform.system() not in ['Windows'] or lit.getBashPath() != '':
220 config.available_features.add('shell')
Galina Kistanovab38fd262011-06-03 18:36:30 +0000221
222# Registered Targets
NAKAMURA Takumicd9c3d62011-11-28 05:09:42 +0000223def get_llc_props(tool):
Galina Kistanovab38fd262011-06-03 18:36:30 +0000224 set_of_targets = set()
NAKAMURA Takumicd9c3d62011-11-28 05:09:42 +0000225 enable_assertions = False
Galina Kistanovab38fd262011-06-03 18:36:30 +0000226
227 cmd = subprocess.Popen([tool, '-version'], stdout=subprocess.PIPE)
228
229 # Parse the stdout to get the list of registered targets.
230 parse_targets = False
231 for line in cmd.stdout:
232 if parse_targets:
233 m = re.match( r'(.*) - ', line)
234 if m is not None:
235 set_of_targets.add(m.group(1).strip() + '-registered-target')
236 else:
237 break
238 elif "Registered Targets:" in line:
239 parse_targets = True
240
NAKAMURA Takumicd9c3d62011-11-28 05:09:42 +0000241 if re.search(r'with assertions', line):
242 enable_assertions = True
Galina Kistanovab38fd262011-06-03 18:36:30 +0000243
NAKAMURA Takumicd9c3d62011-11-28 05:09:42 +0000244 return {"set_of_targets": set_of_targets,
245 "enable_assertions": enable_assertions}
246
247llc_props = get_llc_props(os.path.join(llvm_tools_dir, 'llc'))
248if len(llc_props['set_of_targets']) > 0:
249 config.available_features.update(llc_props['set_of_targets'])
Galina Kistanovab38fd262011-06-03 18:36:30 +0000250else:
251 lit.fatal('No Targets Registered with the LLVM Tools!')
NAKAMURA Takumicd9c3d62011-11-28 05:09:42 +0000252
253if llc_props['enable_assertions']:
254 config.available_features.add('asserts')