1import os 2import itertools 3import platform 4import subprocess 5import sys 6 7import lit.util 8from lit.llvm import llvm_config 9from lit.llvm.subst import FindTool 10from lit.llvm.subst import ToolSubst 11 12 13def _get_lldb_init_path(config): 14 return os.path.join(config.test_exec_root, 'Shell', 'lit-lldb-init') 15 16 17def _disallow(config, execName): 18 warning = ''' 19 echo '*** Do not use \'{0}\' in tests; use \'%''{0}\'. ***' && 20 exit 1 && echo 21 ''' 22 config.substitutions.append((' {0} '.format(execName), 23 warning.format(execName))) 24 25 26def use_lldb_substitutions(config): 27 # Set up substitutions for primary tools. These tools must come from config.lldb_tools_dir 28 # which is basically the build output directory. We do not want to find these in path or 29 # anywhere else, since they are specifically the programs which are actually being tested. 30 31 dsname = 'debugserver' if platform.system() in ['Darwin'] else 'lldb-server' 32 dsargs = [] if platform.system() in ['Darwin'] else ['gdbserver'] 33 34 build_script = os.path.dirname(__file__) 35 build_script = os.path.join(build_script, 'build.py') 36 build_script_args = [build_script, 37 '--compiler=any', # Default to best compiler 38 '--arch=' + str(config.lldb_bitness)] 39 if config.lldb_lit_tools_dir: 40 build_script_args.append('--tools-dir={0}'.format(config.lldb_lit_tools_dir)) 41 if config.lldb_tools_dir: 42 build_script_args.append('--tools-dir={0}'.format(config.lldb_tools_dir)) 43 if config.llvm_libs_dir: 44 build_script_args.append('--libs-dir={0}'.format(config.llvm_libs_dir)) 45 46 lldb_init = _get_lldb_init_path(config) 47 48 primary_tools = [ 49 ToolSubst('%lldb', 50 command=FindTool('lldb'), 51 extra_args=['--no-lldbinit', '-S', lldb_init], 52 unresolved='fatal'), 53 ToolSubst('%lldb-init', 54 command=FindTool('lldb'), 55 extra_args=['-S', lldb_init], 56 unresolved='fatal'), 57 ToolSubst('%debugserver', 58 command=FindTool(dsname), 59 extra_args=dsargs, 60 unresolved='ignore'), 61 ToolSubst('%platformserver', 62 command=FindTool('lldb-server'), 63 extra_args=['platform'], 64 unresolved='ignore'), 65 'lldb-test', 66 'lldb-instr', 67 'lldb-vscode', 68 ToolSubst('%build', 69 command="'" + sys.executable + "'", 70 extra_args=build_script_args) 71 ] 72 73 _disallow(config, 'lldb') 74 _disallow(config, 'debugserver') 75 _disallow(config, 'platformserver') 76 77 llvm_config.add_tool_substitutions(primary_tools, [config.lldb_tools_dir]) 78 79def _use_msvc_substitutions(config): 80 # If running from a Visual Studio Command prompt (e.g. vcvars), this will 81 # detect the include and lib paths, and find cl.exe and link.exe and create 82 # substitutions for each of them that explicitly specify /I and /L paths 83 cl = lit.util.which('cl') 84 link = lit.util.which('link') 85 86 if not cl or not link: 87 return 88 89 cl = '"' + cl + '"' 90 link = '"' + link + '"' 91 includes = os.getenv('INCLUDE', '').split(';') 92 libs = os.getenv('LIB', '').split(';') 93 94 config.available_features.add('msvc') 95 compiler_flags = ['"/I{}"'.format(x) for x in includes if os.path.exists(x)] 96 linker_flags = ['"/LIBPATH:{}"'.format(x) for x in libs if os.path.exists(x)] 97 98 tools = [ 99 ToolSubst('%msvc_cl', command=cl, extra_args=compiler_flags), 100 ToolSubst('%msvc_link', command=link, extra_args=linker_flags)] 101 llvm_config.add_tool_substitutions(tools) 102 return 103 104def use_support_substitutions(config): 105 # Set up substitutions for support tools. These tools can be overridden at the CMake 106 # level (by specifying -DLLDB_LIT_TOOLS_DIR), installed, or as a last resort, we can use 107 # the just-built version. 108 host_flags = ['--target=' + config.host_triple] 109 if platform.system() in ['Darwin']: 110 try: 111 out = subprocess.check_output(['xcrun', '--show-sdk-path']).strip() 112 res = 0 113 except OSError: 114 res = -1 115 if res == 0 and out: 116 sdk_path = lit.util.to_string(out) 117 llvm_config.lit_config.note('using SDKROOT: %r' % sdk_path) 118 host_flags += ['-isysroot', sdk_path] 119 elif platform.system() in ['NetBSD', 'OpenBSD', 'Linux']: 120 host_flags += ['-pthread'] 121 122 if sys.platform.startswith('netbsd'): 123 # needed e.g. to use freshly built libc++ 124 host_flags += ['-L' + config.llvm_libs_dir, 125 '-Wl,-rpath,' + config.llvm_libs_dir] 126 127 # The clang module cache is used for building inferiors. 128 host_flags += ['-fmodules-cache-path={}'.format(config.clang_module_cache)] 129 130 host_flags = ' '.join(host_flags) 131 config.substitutions.append(('%clang_host', '%clang ' + host_flags)) 132 config.substitutions.append(('%clangxx_host', '%clangxx ' + host_flags)) 133 config.substitutions.append(('%clang_cl_host', '%clang_cl --target='+config.host_triple)) 134 135 additional_tool_dirs=[] 136 if config.lldb_lit_tools_dir: 137 additional_tool_dirs.append(config.lldb_lit_tools_dir) 138 139 llvm_config.use_clang(additional_flags=['--target=specify-a-target-or-use-a-_host-substitution'], 140 additional_tool_dirs=additional_tool_dirs, 141 required=True) 142 143 144 if sys.platform == 'win32': 145 _use_msvc_substitutions(config) 146 147 have_lld = llvm_config.use_lld(additional_tool_dirs=additional_tool_dirs, 148 required=False) 149 if have_lld: 150 config.available_features.add('lld') 151 152 153 support_tools = ['yaml2obj', 'obj2yaml', 'llvm-dwp', 'llvm-pdbutil', 154 'llvm-mc', 'llvm-readobj', 'llvm-objdump', 155 'llvm-objcopy', 'lli'] 156 additional_tool_dirs += [config.lldb_tools_dir, config.llvm_tools_dir] 157 llvm_config.add_tool_substitutions(support_tools, additional_tool_dirs) 158 159 _disallow(config, 'clang') 160 161def use_lldb_repro_substitutions(config, mode): 162 lldb_init = _get_lldb_init_path(config) 163 substitutions = [ 164 ToolSubst( 165 '%lldb', 166 command=FindTool('lldb-repro'), 167 extra_args=[mode, '--no-lldbinit', '-S', lldb_init]), 168 ToolSubst( 169 '%lldb-init', 170 command=FindTool('lldb-repro'), 171 extra_args=[mode, '-S', lldb_init]), 172 ] 173 llvm_config.add_tool_substitutions(substitutions, [config.lldb_tools_dir]) 174