188cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea
288cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea# Locate and load the lldb python module
388cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea
488cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Maleaimport os, sys
588cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea
688cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Maleadef import_lldb():
788cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  """ Find and import the lldb modules. This function tries to find the lldb module by:
888cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea      1. Simply by doing "import lldb" in case the system python installation is aware of lldb. If that fails,
988cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea      2. Executes the lldb executable pointed to by the LLDB environment variable (or if unset, the first lldb
1088cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea         on PATH") with the -P flag to determine the PYTHONPATH to set. If the lldb executable returns a valid
1188cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea         path, it is added to sys.path and the import is attempted again. If that fails, 3. On Mac OS X the
1288cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea         default Xcode 4.5 installation path.
1388cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  """
1488cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea
1588cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  # Try simple 'import lldb', in case of a system-wide install or a pre-configured PYTHONPATH
1688cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  try:
1788cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    import lldb
1888cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    return True
1988cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  except ImportError:
2088cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    pass
2188cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea
2288cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  # Allow overriding default path to lldb executable with the LLDB environment variable
2388cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  lldb_executable = 'lldb'
2488cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  if 'LLDB' in os.environ and os.path.exists(os.environ['LLDB']):
2588cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    lldb_executable = os.environ['LLDB']
2688cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea
2788cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  # Try using builtin module location support ('lldb -P')
2888cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  from subprocess import check_output, CalledProcessError
2988cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  try:
3088cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    with open(os.devnull, 'w') as fnull:
3188cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea      lldb_minus_p_path = check_output("%s -P" % lldb_executable, shell=True, stderr=fnull).strip()
3288cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    if not os.path.exists(lldb_minus_p_path):
3388cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea      #lldb -P returned invalid path, probably too old
3488cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea      pass
3588cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    else:
3688cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea      sys.path.append(lldb_minus_p_path)
3788cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea      import lldb
3888cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea      return True
3988cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  except CalledProcessError:
4088cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    # Cannot run 'lldb -P' to determine location of lldb python module
4188cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    pass
4288cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  except ImportError:
4388cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    # Unable to import lldb module from path returned by `lldb -P`
4488cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    pass
4588cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea
4688cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  # On Mac OS X, use the try the default path to XCode lldb module
4788cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  if "darwin" in sys.platform:
4888cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    xcode_python_path = "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/Current/Resources/Python/"
4988cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    sys.path.append(xcode_python_path)
5088cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    try:
5188cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea      import lldb
5288cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea      return True
5388cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea    except ImportError:
5488cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea      # Unable to import lldb module from default Xcode python path
5588cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea      pass
5688cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea
5788cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  return False
5888cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea
5988cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Maleaif not import_lldb():
6088cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  import vim
6188cd3fd2f91506c5a9efd660fc0fe92f09b08117Daniel Malea  vim.command('redraw | echo "%s"' % " Error loading lldb module; vim-lldb will be disabled. Check LLDB installation or set LLDB environment variable.")
62