Can I manually use the CMake cpp scanner dependency file in my cmake code?

I am trying to add a custom target using CMake, which executes one command for every given .cpp file. This command should only be overwritten when changing the source file or one of the included source files. AFAIK for this I need a list of all included files and add them to the DEPENDScall parameter add_custom_command()belonging to my custom target.

So, is there a built-in way to get a list of included files?

I know about the IMPLICIT_DEPENDSfunction option add_custom_command(), but it only works for Makefile generators. I would like to do this work for all generators.

thank you for your time

Edit:

As requested, I will send some cmake code to show what I want to achieve. I want to add a custom target that runs clang-tidy for all data in .cpp files. When incrementally creating a custom target, clang-tidy commands must be re-run whenever the .cpp file or one of its directly or indirectly included header files changes. Just as compiler re-starts are handled.

# ----------------------------------------------------------------------------------------
# mainTargetName The name of the target that shall be analyzed
# files A list of all the main targets .cpp files
#
function( addStaticAnalysisTarget mainTargetName files )

    set(targetName runStaticAnalysis_${mainTargetName})
    set(command "clang-tidy-4.0 -checks=* -p ${CMAKE_BINARY_DIR}")

    foreach( file ${files}  )

        get_filename_component( baseName ${file} NAME_WE)
        set(stampFile ${CMAKE_CURRENT_BINARY_DIR}/analyze_${baseName}.stamp )
        set(fullFile ${CMAKE_CURRENT_SOURCE_DIR}/${file})
        set(commandWithFile "${command} ${fullFile}")
        separate_arguments_for_platform( commandList ${commandWithFile})

        add_custom_command(
            OUTPUT ${stampFile}
            DEPENDS "${fullFile}"
            IMPLICIT_DEPENDS CXX "${fullFile}"
            COMMAND ${commandList}
            COMMAND cmake -E touch "${stampFile}"       # without creating a file as a touch-stone the command will always be re-run.
            WORKING_DIRECTORY ${CPPCODEBASE_ROOT_DIR}
            COMMENT "${commandWithFile}"
            VERBATIM
        )

        list(APPEND stampFiles ${stampFile})

    endforeach()
    set_source_files_properties(${stampFiles} PROPERTIES GENERATED TRUE)   # make the stamp files known to cmake as generated files.

    add_custom_target(
        ${targetName}
        DEPENDS ${stampFiles}
    )

endfunction()

The problem is that it does not work. When I modify the included files, clang-tidy does not restart for the affected files. I used the Unix Makefile generator for this example, so it should work, at least with make. Any clues why this is not so?

, , - cmake, '' '' DEPENDS '' ''. , , cmake. , cmake, .

: https://gitlab.kitware.com/cmake/cmake/issues/16830

2: , , IMPLICIT_DEPENDS , , . , , .

+4
1

, ...

, cmakes cmake.

, cmake, .cpp cmake. cmake . IMPLICIT_DEPENDS.

, , , clang-tidy linux . , clang-tidy , .

:

0

Source: https://habr.com/ru/post/1675367/


All Articles