blob: 30388596ea167cce3228f6d842c6e6644180f8a4 [file] [log] [blame]
Zachary Turnera3037bd2015-11-21 01:39:04 +00001"""
2 The LLVM Compiler Infrastructure
3
4This file is distributed under the University of Illinois Open Source
5License. See LICENSE.TXT for details.
6
7Prepares language bindings for LLDB build process. Run with --help
8to see a description of the supported command line arguments.
9"""
10
11# Python modules:
12import os
13import platform
14import sys
15
16
17def _find_file_in_paths(paths, exe_basename):
18 """Returns the full exe path for the first path match.
19
20 @params paths the list of directories to search for the exe_basename
21 executable
22 @params exe_basename the name of the file for which to search.
23 e.g. "swig" or "swig.exe".
24
25 @return the full path to the executable if found in one of the
26 given paths; otherwise, returns None.
27 """
28 for path in paths:
29 trial_exe_path = os.path.join(path, exe_basename)
30 if os.path.exists(trial_exe_path):
31 return os.path.normcase(trial_exe_path)
32 return None
33
Kate Stoneb9c1b512016-09-06 20:57:50 +000034
Zachary Turnera3037bd2015-11-21 01:39:04 +000035def find_executable(executable):
36 """Finds the specified executable in the PATH or known good locations."""
37
38 # Figure out what we're looking for.
39 if platform.system() == "Windows":
40 executable = executable + ".exe"
41 extra_dirs = []
42 else:
43 extra_dirs = ["/usr/local/bin"]
44
45 # Figure out what paths to check.
46 path_env = os.environ.get("PATH", None)
47 if path_env is not None:
48 paths_to_check = path_env.split(os.path.pathsep)
49 else:
50 paths_to_check = []
51
52 # Add in the extra dirs
53 paths_to_check.extend(extra_dirs)
54 if len(paths_to_check) < 1:
55 raise os.OSError(
56 "executable was not specified, PATH has no "
57 "contents, and there are no extra directories to search")
58
59 result = _find_file_in_paths(paths_to_check, executable)
60
61 if not result or len(result) < 1:
62 raise os.OSError(
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 "failed to find exe='%s' in paths='%s'" %
64 (executable, paths_to_check))
Zachary Turnera3037bd2015-11-21 01:39:04 +000065 return result