| OLD | NEW |
| (Empty) |
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Functions for dealing with determining --tool-prefix.""" | |
| 6 | |
| 7 import distutils.spawn | |
| 8 import logging | |
| 9 import os | |
| 10 | |
| 11 _STATUS_DETECTED = 1 | |
| 12 _STATUS_VERIFIED = 2 | |
| 13 | |
| 14 | |
| 15 def AddOptions(parser): | |
| 16 parser.add_argument('--tool-prefix', default='', | |
| 17 help='Path prefix for c++filt.') | |
| 18 parser.add_argument('--output-directory', | |
| 19 help='Path to the root build directory.') | |
| 20 | |
| 21 | |
| 22 class LazyPaths(object): | |
| 23 def __init__(self, args=None, tool_prefix=None, output_directory=None, | |
| 24 input_file=None): | |
| 25 tool_prefix = tool_prefix or (args and args.tool_prefix) | |
| 26 output_directory = output_directory or (args and args.output_directory) | |
| 27 self._tool_prefix = tool_prefix | |
| 28 self._output_directory = output_directory | |
| 29 self._input_file = input_file | |
| 30 self._output_directory_status = _STATUS_DETECTED if output_directory else 0 | |
| 31 self._tool_prefix_status = _STATUS_DETECTED if tool_prefix else 0 | |
| 32 | |
| 33 @property | |
| 34 def tool_prefix(self): | |
| 35 if self._tool_prefix_status < _STATUS_DETECTED: | |
| 36 self._tool_prefix_status = _STATUS_DETECTED | |
| 37 self._tool_prefix = self._DetectToolPrefix() or '' | |
| 38 logging.debug('Detected --tool-prefix=%s', self._tool_prefix) | |
| 39 return self._tool_prefix | |
| 40 | |
| 41 @property | |
| 42 def output_directory(self): | |
| 43 if self._output_directory_status < _STATUS_DETECTED: | |
| 44 self._output_directory_status = _STATUS_DETECTED | |
| 45 self._output_directory = self._DetectOutputDirectory() | |
| 46 logging.debug('Detected --output-directory=%s', self._output_directory) | |
| 47 return self._output_directory | |
| 48 | |
| 49 def VerifyOutputDirectory(self): | |
| 50 output_directory = self.output_directory | |
| 51 if self._output_directory_status < _STATUS_VERIFIED: | |
| 52 self._output_directory_status = _STATUS_VERIFIED | |
| 53 if not output_directory or not os.path.isdir(output_directory): | |
| 54 raise Exception('Bad --output-directory. Path not found: %s' % | |
| 55 output_directory) | |
| 56 logging.info('Using --output-directory=%s', output_directory) | |
| 57 return output_directory | |
| 58 | |
| 59 def VerifyToolPrefix(self): | |
| 60 tool_prefix = self.tool_prefix | |
| 61 if self._tool_prefix_status < _STATUS_VERIFIED: | |
| 62 self._tool_prefix_status = _STATUS_VERIFIED | |
| 63 if os.path.sep not in tool_prefix: | |
| 64 full_path = distutils.spawn.find_executable(tool_prefix + 'c++filt') | |
| 65 else: | |
| 66 full_path = tool_prefix + 'c++filt' | |
| 67 | |
| 68 if not full_path or not os.path.isfile(full_path): | |
| 69 raise Exception('Bad --tool-prefix. Path not found: %s' % full_path) | |
| 70 logging.info('Using --tool-prefix=%s', self._tool_prefix) | |
| 71 return tool_prefix | |
| 72 | |
| 73 def _DetectOutputDirectory(self): | |
| 74 # See if input file is in out/Release. | |
| 75 abs_path = os.path.abspath(self._input_file) | |
| 76 release_idx = abs_path.find('Release') | |
| 77 if release_idx != -1: | |
| 78 output_directory = abs_path[:release_idx] + 'Release' | |
| 79 output_directory = os.path.relpath(abs_path[:release_idx] + '/Release') | |
| 80 return output_directory | |
| 81 | |
| 82 # See if CWD=output directory. | |
| 83 if os.path.exists('build.ninja'): | |
| 84 return '.' | |
| 85 return None | |
| 86 | |
| 87 def _DetectToolPrefix(self): | |
| 88 output_directory = self.output_directory | |
| 89 if output_directory: | |
| 90 # Auto-detect from build_vars.txt | |
| 91 build_vars_path = os.path.join(output_directory, 'build_vars.txt') | |
| 92 if os.path.exists(build_vars_path): | |
| 93 with open(build_vars_path) as f: | |
| 94 build_vars = dict(l.rstrip().split('=', 1) for l in f if '=' in l) | |
| 95 return os.path.normpath( | |
| 96 os.path.join(output_directory, build_vars['android_tool_prefix'])) | |
| 97 return None | |
| OLD | NEW |