Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for INTERFACE libraries when getting target include directories #121

Merged
merged 3 commits into from
Jan 12, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions ament_cmake_cppcheck/cmake/ament_cmake_cppcheck_lint_hook.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,26 @@ if(_source_files)
if(NOT CMAKE_VERSION VERSION_LESS "3.7.0")
get_directory_property(_build_targets DIRECTORY ${CMAKE_SOURCE_DIR} BUILDSYSTEM_TARGETS)
foreach(_target ${_build_targets})
get_property(_include_dirs
TARGET ${_target}
PROPERTY INCLUDE_DIRECTORIES
)
# Include directories property is different for INTERFACE libraries
get_target_property(_target_type ${_target} TYPE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Glad you found a way to do this.

if(${_target_type} STREQUAL "INTERFACE_LIBRARY")
get_target_property(_include_dirs ${_target} INTERFACE_INCLUDE_DIRECTORIES)
else()
get_target_property(_include_dirs ${_target} INCLUDE_DIRECTORIES)
endif()

# Only append include directories that are from the package being tested
# This accomplishes two things:
# 1. Reduces execution time (less include directories to search)
# 2. cppcheck will not check for errors in external packages
foreach(_include_dir ${_include_dirs})
string(REGEX MATCH "^${CMAKE_SOURCE_DIR}.*" _is_match ${_include_dir})
if(_is_match)
# TODO(jacobperron): Escape special regex characters in CMAKE_SOURCE_DIR
# Related CMake feature request: https://gitlab.kitware.com/cmake/cmake/issues/18409
# Check if include directory is a subdirectory of the source directory
string(REGEX MATCH "^${CMAKE_SOURCE_DIR}/.*" _is_subdirectory ${_include_dir})
# Check if include directory is part of a generator expression (e.g. $<BUILD_INTERFACE:...>)
string(REGEX MATCH "^\\$<.*:${CMAKE_SOURCE_DIR}/.*>$" _is_genexp_subdirectory "${_include_dir}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CMAKE_SOURCE_DIR might contain characters which have special meaning in a regex. Unfortunately afaik there is no function in CMake to escape a string for that use case (see https://gitlab.kitware.com/cmake/cmake/issues/18409).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the real world impact of this issue is narrowly scoped enough that the TODO in 600fa4c covers it.

A robust string library could probably get around the need for regexp here. Comparing the initia substring of the include directory to the CMAKE_SOURCE_DIR might be sufficient in the usual case but I don't have any idea yet what to do about the Generator Expressions.

if(_is_subdirectory OR _is_genexp_subdirectory)
list_append_unique(_all_include_dirs ${_include_dir})
endif()
endforeach()
Expand Down