| """ |
| Build step class for generating compilation database from build directory, |
| it is useful for analysis tools that require actual compilation command, |
| e.g. clang-tidy and iwyu |
| """ |
| |
| from __future__ import absolute_import |
| import logging |
| import os |
| |
| from helpers import commit_message_utils |
| from helpers import diff_utils |
| from helpers import git_utils |
| from slave import base_step |
| |
| BUILD_SCRIPT_PROJECT = 'chromecast/internal' |
| BUILD_SCRIPT_REL_PATH = 'build/build_chromecast.py' |
| |
| |
| class CompdbGenStep(base_step.BaseStep): |
| """Build step class for generating compilation database""" |
| |
| def __init__(self, build_dir, **kwargs): |
| """Creates a CompdbGenStep instance. |
| |
| Args: |
| build_dir: The relative path that contains the build. (relative to |
| chromium/src project path) |
| **kwargs: Any additional args to pass to BaseStep. |
| """ |
| base_step.BaseStep.__init__(self, name='compdb_gen', **kwargs) |
| self._build_dir = build_dir |
| |
| def run(self): |
| """ Use gn to generate compilation database |
| |
| Returns: |
| True iff success. |
| """ |
| |
| # Make sure build directory exists before using it to generate |
| # compilation database. (because the next call to exec_subprocess_chain |
| # can fail silently if the first command files) |
| returncode, _, _ = self.exec_subprocess( |
| ['ls', self._build_dir], cwd=self.get_project_path('chromium/src')) |
| |
| if returncode != 0: |
| logging.info( |
| 'Build directory does not exist. May need to update the build_dir ' |
| 'argument passed into this step.') |
| return False |
| |
| compdb_gen_command = ([ |
| 'ninja', '-C', self._build_dir, '-t', 'compdb', 'cxx', 'cc' |
| ], { |
| 'cwd': self.get_project_path('chromium/src') |
| }) |
| |
| write_to_file = (['cp', '/dev/stdin', 'compile_commands.json'], { |
| 'cwd': self.get_project_path('chromium/src') |
| }) |
| |
| _, _, _ = self.exec_subprocess_chain([compdb_gen_command, write_to_file]) |
| |
| # Remove rbe specific parameters from compilation database, which |
| # present before the path of clang++ compiler. This is not required but |
| # can reduce unnecessary warnings output by clang_tidy/iwyu |
| # pylint: disable=line-too-long |
| patch_rbe_command = [ |
| 'sed', '-i', |
| 's|"command":.*\.\./\.\./third_party/llvm-build/Release+Asserts/bin/clang++' |
| ' |"command": ' |
| '"../../third_party/llvm-build/Release+Asserts/bin/clang++ |g', |
| 'compile_commands.json' |
| ] |
| |
| _, _, _ = self.exec_subprocess( |
| patch_rbe_command, cwd=self.get_project_path('chromium/src')) |
| |
| # Do the same for clang |
| # pylint: disable=line-too-long |
| patch_rbe_command = [ |
| 'sed', '-i', |
| 's|"command":.*\.\./\.\./third_party/llvm-build/Release+Asserts/bin/clang' |
| ' |"command": "../../third_party/llvm-build/Release+Asserts/bin/clang ' |
| '|g', 'compile_commands.json' |
| ] |
| |
| _, _, _ = self.exec_subprocess( |
| patch_rbe_command, cwd=self.get_project_path('chromium/src')) |
| |
| return True |