pocc / pre-commit-hooks

C/C++ hooks to integrate with pre-commit
Apache License 2.0
323 stars 67 forks source link

Is it possible to generate compile_commands.json as part of the hook? #32

Closed ivaigult closed 3 years ago

ivaigult commented 3 years ago

Usually compile_commands.json is created by the build system at the build or generation time. It is never stored in a repo.

Is it possible to add extra option so the database is generated automatically somehow? I can think of this syntax:

repos:
  - repo: https://github.com/pocc/pre-commit-hooks
    rev: python
    hooks:
      - id: clang-tidy
        args: [-p __compdb/compile_commands.json]
        generate_compile_commands: ["mkdir __compdb", "cd __compdb", "cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=ON"]

Is something like this available? Would it make sense?

pocc commented 3 years ago

It's not possible to change the syntax of pre-commit-hooks with a generate_compile_commands option.

There are a couple ways to go about this:

  1. Add the commands you want to your pre-commit file before the pre-commit framework code
  2. Create a local hook for your commands similar to https://stackoverflow.com/questions/59499061/how-to-run-custom-shell-script-file-before-pre-commit-hook