CMake and PC-Lint Plus - how do I pass CMake's include directory list to Lint?

霸气de小男生 提交于 2020-06-26 12:51:07

问题


I'm trying to incorporate PC-Lint Plus into my cmake project, mainly per PC-Lint needs a list of all include paths for the files to be scanned. How to get a list of all include paths recursively needed by a target in CMake?.

PC-Lint is getting invoked, but ends up dying because not all the includes are in the PC-Lint invocation.

I dumped the above link into a Lint.cmake, and included that in my top level CMake file. In my project file, I added:

if(COMMAND add_pc_lint)
add_pc_lint(moded ${SRC_FILES})
endif(COMMAND add_pc_lint)

So I'd expect the lines:

function(add_pc_lint target)
    get_directory_property(lint_include_directories INCLUDE_DIRECTORIES)
    # let's get those elephants across the alps
    # prepend each include directory with "-i"; also quotes the directory
    set(lint_include_directories_transformed)
    foreach(include_dir ${lint_include_directories})
        list(APPEND lint_include_directories_transformed -i"${include_dir}")
    endforeach(include_dir)

to pull the include directory list out of INCLUDE_DIRECTORIES.

If I add:

message(STATUS "********************************************")
message(STATUS "Include directories - ${lint_include_directories}")
message(STATUS "********************************************")
message(STATUS "********************************************")
message(STATUS "INCLUDE_DIRECTORIES - ${INCLUDE_DIRECTORIES}")
message(STATUS "********************************************")

directly above the foreach loop, lint_include_directories and INCLUDE_DIRECTORIES both appear to be empty.

What do I need to do to get the full list of include directories, including from target_link_libraries and target_include_directories, from CMake to give to PC-Lint Plus?


回答1:


EDIT: I have updated the CMake PC-Lint script to account for the issue described below in my response here.


The documentation on PC-Lint usage appears to be catered for old CMake, not the target-based modern CMake. The add_pc_lint function grabs the include directories and compile definitions from CMake directory properties. These directories properties will be populated if your project has calls such as:

  • include_directories()
  • add_definitions()

Without these commands (which really should not be used in modern CMake), the add_pc_lint function likely won't find all the include directories and compile definitions you expect.

In modern CMake, the include directories and compile definitions are stored as properties of a target instead, when you use commands such as these:

  • target_include_directories()
  • target_compile_definitions()

In this case, you have to modify the add_pc_lint function to grab the properties from the target, not the directory. So, change the top of the add_pc_lint function from this:

function(add_pc_lint target)
    get_directory_property(lint_include_directories INCLUDE_DIRECTORIES)
    get_directory_property(lint_defines COMPILE_DEFINITIONS)

to this:

function(add_pc_lint target)
    get_target_property(lint_include_directories ${target} INCLUDE_DIRECTORIES)
    get_target_property(lint_defines ${target} COMPILE_DEFINITIONS)


来源:https://stackoverflow.com/questions/60441386/cmake-and-pc-lint-plus-how-do-i-pass-cmakes-include-directory-list-to-lint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!