• Masahiro Yamada's avatar
    kbuild: wire up the build rule of compile_commands.json to Makefile · 3d32285f
    Masahiro Yamada authored
    Currently, you need to manually run scripts/gen_compile_commands.py
    to create compile_commands.json. It parses all the .*.cmd files found
    under the specified directory.
    
    If you rebuild the kernel over again without 'make clean',
    .*.cmd files from older builds will create stale entries in
    compile_commands.json.
    
    This commit wires up the compile_commands.json rule to Makefile, and
    makes it parse only the .*.cmd files involved in the current build.
    
    Pass $(KBUILD_VMLINUX_OBJS), $(KBUILD_VMLINUX_LIBS), and modules.order
    to the script. The objects or archives linked to vmlinux are listed in
    $(KBUILD_VMLINUX_OBJS) or $(KBUILD_VMLINUX_LIBS). All the modules are
    listed in modules.order.
    
    You can create compile_commands.json from Make:
    
      $ make -j$(nproc) CC=clang compile_commands.json
    
    You can also build vmlinux, modules, and compile_commands.json all
    together in a single command:
    
      $ make -j$(nproc) CC=clang all compile_commands.json
    
    It works for M= builds as well. In this case, compile_commands.json
    is created in the top directory of the external module.
    
    This is convenient, but it has a drawback; the coverage of the
    compile_commands.json is reduced because only the objects linked to
    vmlinux or modules are handled. For example, the following C files are
    not included in the compile_commands.json:
    
     - Decompressor source files (arch/*/boot/)
     - VDSO source files
     - C files used to generate intermediates (e.g. kernel/bounds.c)
     - Standalone host programs
    
    I think it is fine for most developers because our main interest is
    the kernel-space code.
    
    If you want to cover all the compiled C files, please build the kernel,
    then run the script manually as you did before:
    
      $ make clean    # if you want to remove stale .cmd files [optional]
      $ make -j$(nproc) CC=clang
      $ scripts/gen_compile_commands.py
    
    Here is a note for out-of-tree builds. 'make compile_commands.json'
    works with O= option, but please notice compile_commands.json is
    created in the object tree instead of the source tree.
    
    Some people may want to have compile_commands.json in the source tree
    because Clang Tools searches for it through all parent paths of the
    first input source file.
    
    However, you cannot do this for O= builds. Kbuild should never generate
    any build artifact in the source tree when O= is given because the
    source tree might be read-only. Any write attempt to the source tree
    is monitored and the violation may be reported. See the commit log of
    8ef14c2c.
    
    So, the only possible way is to create compile_commands.json in the
    object tree, then specify '-p <build-path>' when you use clang-check,
    clang-tidy, etc.
    Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
    Acked-by: default avatarNick Desaulniers <ndesaulniers@google.com>
    3d32285f
Makefile 62.6 KB