LinuxCommandLibrary

bear

Generate compilation database for clang-based tools

TLDR

Generate compile_commands.json by running a build command

$ bear -- [make]
copy

Generate compilation database with a custom output file name
$ bear --output [path/to/compile_commands.json] -- [make]
copy

Append results to an existing compile_commands.json file
$ bear --append -- [make]
copy

Run in verbose mode to get detailed output
$ bear --verbose -- [make]
copy

Force bear to use the preload method for command interception
$ bear --force-preload -- [make]
copy

SYNOPSIS

bear [options] build_command

PARAMETERS

-o <file>
    Specify the output file for the compilation database. Defaults to compile_commands.json in the current working directory.

-a
    Append to existing compilation database, don't overwrite.

--append
    Append to existing compilation database, don't overwrite.

-v
    Verbose output, show intercepted commands.

--verbose
    Verbose output, show intercepted commands.

-n <name>
    Specify custom interceptor command instead of the default.

--name <name>
    Specify custom interceptor command instead of the default.

DESCRIPTION

The bear command helps generate a compilation database, specifically a compile_commands.json file, which is crucial for utilizing clang-based static analysis and refactoring tools on C, C++, Objective-C, and Objective-C++ projects. It works by intercepting compiler invocations during a build process and recording the exact commands used, including all compiler flags, include paths, and source file locations. This compilation database allows tools like clang-tidy, clang-format, and others to accurately understand the project's build environment and perform their analyses effectively.
Bear is especially useful when the project's build system doesn't natively provide a way to generate a compilation database or when the existing build system is complex. Bear simplifies the process, providing a straightforward method to create a reliable and comprehensive compilation database. The generated compile_commands.json file is placed in the project's root directory by default but can be modified using options.

CAVEATS

bear relies on intercepting compiler calls. This means the build_command must actually trigger compilations for the database to be populated. Some build systems or build configurations might not be fully compatible. It also requires that the user has write access to the directory where compilation database is written.

<B>USAGE EXAMPLE</B>

To generate a compilation database for a CMake project, you would typically run:
bear make
This will execute the make command and intercept all compiler invocations, creating the compile_commands.json file.

<B>ENVIRONMENT VARIABLES</B>

bear does not have specific, dedicated environment variables it relies on. It mainly relies on the build_command to trigger the compilations. However, the underlying interceptor may rely on some enviroment variables.

HISTORY

bear was developed to address the need for a simple and reliable way to generate compilation databases for clang-based tools, especially in projects with complex or custom build systems. Before bear, generating these databases often involved complex workarounds or modifications to the build process itself. The tool's development has focused on providing a robust and platform-independent solution for capturing compiler invocations.

SEE ALSO

clang-tidy(1), clang-format(1), compile_commands.json(5)

Copied to clipboard