Zachary Turner | a3037bd | 2015-11-21 01:39:04 +0000 | [diff] [blame^] | 1 | """ |
| 2 | The LLVM Compiler Infrastructure |
| 3 | |
| 4 | This file is distributed under the University of Illinois Open Source |
| 5 | License. See LICENSE.TXT for details. |
| 6 | |
| 7 | Prepares language bindings for LLDB build process. Run with --help |
| 8 | to see a description of the supported command line arguments. |
| 9 | """ |
| 10 | |
| 11 | # Python modules: |
| 12 | import os |
| 13 | import platform |
| 14 | import sys |
| 15 | |
| 16 | |
| 17 | def _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 | |
| 34 | def find_executable(executable): |
| 35 | """Finds the specified executable in the PATH or known good locations.""" |
| 36 | |
| 37 | # Figure out what we're looking for. |
| 38 | if platform.system() == "Windows": |
| 39 | executable = executable + ".exe" |
| 40 | extra_dirs = [] |
| 41 | else: |
| 42 | extra_dirs = ["/usr/local/bin"] |
| 43 | |
| 44 | # Figure out what paths to check. |
| 45 | path_env = os.environ.get("PATH", None) |
| 46 | if path_env is not None: |
| 47 | paths_to_check = path_env.split(os.path.pathsep) |
| 48 | else: |
| 49 | paths_to_check = [] |
| 50 | |
| 51 | # Add in the extra dirs |
| 52 | paths_to_check.extend(extra_dirs) |
| 53 | if len(paths_to_check) < 1: |
| 54 | raise os.OSError( |
| 55 | "executable was not specified, PATH has no " |
| 56 | "contents, and there are no extra directories to search") |
| 57 | |
| 58 | result = _find_file_in_paths(paths_to_check, executable) |
| 59 | |
| 60 | if not result or len(result) < 1: |
| 61 | raise os.OSError( |
| 62 | "failed to find exe='%s' in paths='%s'" % (executable, paths_to_check)) |
| 63 | return result |
| 64 | |