+# Provide a command line for mcjit tests
+lli_mcjit = 'lli -use-mcjit'
+# The target triple used by default by lli is the process target triple (some
+# triple appropriate for generating code for the current process) but because
+# we don't support COFF in MCJIT well enough for the tests, force ELF format on
+# Windows. FIXME: the process target triple should be used here, but this is
+# difficult to obtain on Windows.
+if re.search(r'cygwin|mingw32|win32', config.host_triple):
+ lli_mcjit += ' -mtriple='+config.host_triple+'-elf'
+config.substitutions.append( ('%lli_mcjit', lli_mcjit) )
+
+# Provide a substition for those tests that need to run the jit to obtain data
+# but simply want use the currently considered most reliable jit for platform
+# FIXME: ppc32 is not ready for mcjit.
+if 'arm' in config.target_triple \
+ or 'aarch64' in config.target_triple \
+ or 'powerpc64' in config.target_triple \
+ or 's390x' in config.target_triple:
+ defaultIsMCJIT = 'true'
+else:
+ defaultIsMCJIT = 'false'
+config.substitutions.append( ('%defaultjit', '-use-mcjit='+defaultIsMCJIT) )
+
+# Process jit implementation option
+jit_impl_cfg = lit_config.params.get('jit_impl', None)
+if jit_impl_cfg == 'mcjit':
+ # When running with mcjit, mangle -mcjit into target triple
+ # and add -use-mcjit flag to lli invocation
+ if 'i386' in config.target_triple or 'i686' in config.target_triple:
+ config.target_triple += jit_impl_cfg + '-ia32'
+ elif 'x86_64' in config.target_triple:
+ config.target_triple += jit_impl_cfg + '-ia64'
+ else:
+ config.target_triple += jit_impl_cfg
+
+ config.substitutions.append( ('%lli', 'lli -use-mcjit') )
+else:
+ config.substitutions.append( ('%lli', 'lli') )
+
+# Add site-specific substitutions.
+config.substitutions.append( ('%ocamlopt', config.ocamlopt_executable) )
+config.substitutions.append( ('%llvmshlibdir', config.llvm_shlib_dir) )
+config.substitutions.append( ('%shlibext', config.llvm_shlib_ext) )
+
+# For each occurrence of an llvm tool name as its own word, replace it
+# with the full path to the build directory holding that tool. This
+# ensures that we are testing the tools just built and not some random
+# tools that might happen to be in the user's PATH. Thus this list
+# includes every tool placed in $(LLVM_OBJ_ROOT)/$(BuildMode)/bin
+# (llvm_tools_dir in lit parlance).
+ # Don't match 'bugpoint-' or 'clang-'.
+ # Don't match '/clang' or '-clang'.
+if os.pathsep == ';':
+ pathext = os.environ.get('PATHEXT', '').split(';')
+else:
+ pathext = ['']
+# Regex to reject matching a hyphen
+NOHYPHEN = r"(?<!-)"
+
+for pattern in [r"\bbugpoint\b(?!-)",
+ r"(?<!/|-)\bclang\b(?!-)",
+ r"\bgold\b",
+ # Match llc but not -llc
+ NOHYPHEN + r"\bllc\b",
+ r"\blli\b",
+ r"\bllvm-PerfectShuffle\b",
+ r"\bllvm-ar\b",
+ r"\bllvm-as\b",
+ r"\bllvm-bcanalyzer\b",
+ r"\bllvm-config\b",
+ r"\bllvm-cov\b",
+ r"\bllvm-diff\b",
+ r"\bllvm-dis\b",
+ r"\bllvm-dwarfdump\b",
+ r"\bllvm-extract\b",
+ r"\bllvm-jistlistener\b",
+ r"\bllvm-link\b",
+ r"\bllvm-lto\b",
+ r"\bllvm-mc\b",
+ r"\bllvm-mcmarkup\b",
+ r"\bllvm-nm\b",
+ r"\bllvm-objdump\b",
+ r"\bllvm-ranlib\b",
+ r"\bllvm-readobj\b",
+ r"\bllvm-rtdyld\b",
+ r"\bllvm-shlib\b",
+ r"\bllvm-size\b",
+ r"\bllvm-tblgen\b",
+ r"\bllvm-c-test\b",
+ # Match llvmc but not -llvmc
+ NOHYPHEN + r"\bllvmc\b",
+ # Match lto but not -lto
+ NOHYPHEN + r"\blto\b",
+ r"\bmacho-dump\b",
+ # Don't match '.opt', '-opt', '^opt' or '/opt'.
+ r"(?<!\.|-|\^|/)\bopt\b",
+ r"\bFileCheck\b",
+ r"\bFileUpdate\b",
+ r"\bc-index-test\b",
+ r"\bfpcmp\b",
+ r"\bobj2yaml\b",
+ r"\byaml2obj\b",
+ # Handle these specially as they are strings searched
+ # for during testing.
+ r"\| \bcount\b",
+ r"\| \bnot\b"]:
+ # Extract the tool name from the pattern. This relies on the tool
+ # name being surrounded by \b word match operators. If the
+ # pattern starts with "| ", include it in the string to be
+ # substituted.
+ substitution = re.sub(r"^(\\)?((\| )?)\W+b([0-9A-Za-z-_]+)\\b\W*$",
+ r"\2" + llvm_tools_dir + "/" + r"\4",
+ pattern)
+ for ext in pathext:
+ substitution_ext = substitution + ext
+ if os.path.exists(substitution_ext):
+ substitution = substitution_ext
+ break
+ config.substitutions.append((pattern, substitution))
+
+### Features
+
+# Shell execution
+if execute_external:
+ config.available_features.add('shell')
+
+# Others/can-execute.txt
+if sys.platform not in ['win32']:
+ config.available_features.add('can-execute')
+
+# Loadable module
+# FIXME: This should be supplied by Makefile or autoconf.
+if sys.platform in ['win32', 'cygwin']:
+ loadable_module = (config.enable_shared == 1)
+else:
+ loadable_module = True
+
+if loadable_module:
+ config.available_features.add('loadable_module')
+
+# Sanitizers.
+if config.llvm_use_sanitizer == "Address":
+ config.available_features.add("asan")
+if (config.llvm_use_sanitizer == "Memory" or
+ config.llvm_use_sanitizer == "MemoryWithOrigins"):
+ config.available_features.add("msan")
+
+# Direct object generation
+if not 'hexagon' in config.target_triple:
+ config.available_features.add("object-emission")
+
+if config.have_zlib == "1":
+ config.available_features.add("zlib")
+
+# Native compilation: host arch == target arch
+# FIXME: Consider cases that target can be executed
+# even if host_triple were different from target_triple.
+if config.host_triple == config.target_triple:
+ config.available_features.add("native")
+
+# llc knows whether he is compiled with -DNDEBUG.
+import subprocess
+try:
+ llc_cmd = subprocess.Popen([os.path.join(llvm_tools_dir, 'llc'), '-version'],
+ stdout = subprocess.PIPE)
+except OSError:
+ print("Could not find llc in " + llvm_tools_dir)
+ exit(42)
+
+if re.search(r'with assertions', llc_cmd.stdout.read().decode('ascii')):
+ config.available_features.add('asserts')
+llc_cmd.wait()
+
+if 'darwin' == sys.platform:
+ try:
+ sysctl_cmd = subprocess.Popen(['sysctl', 'hw.optional.fma'],
+ stdout = subprocess.PIPE)
+ except OSError:
+ print("Could not exec sysctl")
+ result = sysctl_cmd.stdout.read().decode('ascii')
+ if -1 != result.find("hw.optional.fma: 1"):
+ config.available_features.add('fma3')
+ sysctl_cmd.wait()
+
+# Check if we should use gmalloc.
+use_gmalloc_str = lit_config.params.get('use_gmalloc', None)
+if use_gmalloc_str is not None:
+ if use_gmalloc_str.lower() in ('1', 'true'):
+ use_gmalloc = True
+ elif use_gmalloc_str.lower() in ('', '0', 'false'):
+ use_gmalloc = False