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

Find FLANN using both module- and config-based methods #3202

Merged
merged 2 commits into from
Jul 4, 2019
Merged
Changes from all 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
79 changes: 37 additions & 42 deletions cmake/Modules/FindFLANN.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# FindFLANN
# --------
#
# Try to find FLANN library and include files.
# Try to find FLANN library and headers. This module supports both old released versions
# of FLANN ≤ 1.9.1 and newer development versions that ship with a modern config file.
#
# IMPORTED Targets
# ^^^^^^^^^^^^^^^^
Expand All @@ -20,12 +21,6 @@
# ::
#
# FLANN_FOUND True in case FLANN is found, otherwise false
# FLANN_DEFINITIONS Compiler flags for FLANN.
# FLANN_INCLUDE_DIR Location of FLANN header files
# FLANN_INCLUDE_DIRS Location of FLANN header files (including dependencies)
# FLANN_LIBRARY FLANN release library
# FLANN_LIBRARY_DEBUG FLANN debug library
# FLANN_LIBRARIES FLANN release and debug library
#
# Example usage
# ^^^^^^^^^^^^^
Expand All @@ -38,23 +33,29 @@
# target_link_libraries(foo FLANN::FLANN)
#

# First try to locate FLANN using modern config
find_package(flann NO_MODULE ${FLANN_FIND_VERSION} QUIET)
if(flann_FOUND)
unset(flann_FOUND)
set(FLANN_FOUND ON)
# Create interface library that effectively becomes an alias for the appropriate (static/dynamic) imported FLANN target
add_library(FLANN::FLANN INTERFACE IMPORTED)
if(FLANN_USE_STATIC)
set_property(TARGET FLANN::FLANN APPEND PROPERTY INTERFACE_LINK_LIBRARIES flann::flann_cpp_s)
else()
set_property(TARGET FLANN::FLANN APPEND PROPERTY INTERFACE_LINK_LIBRARIES flann::flann_cpp)
endif()
return()
endif()

# Second try to locate FLANN using pkgconfig
find_package(PkgConfig QUIET)
if(FLANN_FIND_VERSION)
pkg_check_modules(PC_FLANN flann>=${FLANN_FIND_VERSION})
else()
pkg_check_modules(PC_FLANN flann)
endif()

set(FLANN_DEFINITIONS ${PC_FLANN_CFLAGS_OTHER})

if(FLANN_USE_STATIC)
set(FLANN_RELEASE_NAME flann_cpp_s)
set(FLANN_DEBUG_NAME flann_cpp_s-gd)
else()
set(FLANN_RELEASE_NAME flann_cpp)
set(FLANN_DEBUG_NAME flann_cpp-gd)
endif()

find_path(FLANN_INCLUDE_DIR
NAMES
flann/flann.hpp
Expand All @@ -69,6 +70,16 @@ find_path(FLANN_INCLUDE_DIR
include
)

if(FLANN_USE_STATIC)
set(FLANN_RELEASE_NAME flann_cpp_s)
set(FLANN_DEBUG_NAME flann_cpp_s-gd)
set(FLANN_LIBRARY_TYPE STATIC)
else()
set(FLANN_RELEASE_NAME flann_cpp)
set(FLANN_DEBUG_NAME flann_cpp-gd)
set(FLANN_LIBRARY_TYPE SHARED)
endif()

find_library(FLANN_LIBRARY
NAMES
${FLANN_RELEASE_NAME}
Expand Down Expand Up @@ -97,39 +108,23 @@ find_library(FLANN_LIBRARY_DEBUG
lib
)

if(FLANN_LIBRARY AND FLANN_LIBRARY_DEBUG)
set(FLANN_LIBRARIES optimized ${FLANN_LIBRARY} debug ${FLANN_LIBRARY_DEBUG})
else()
set(FLANN_LIBRARIES ${FLANN_LIBRARY})
endif()

set(FLANN_INCLUDE_DIRS
${FLANN_INCLUDE_DIR}
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
FLANN DEFAULT_MSG
FLANN_LIBRARIES FLANN_INCLUDE_DIR
FLANN_LIBRARY FLANN_INCLUDE_DIR
)

if(FLANN_FOUND)
if(NOT TARGET FLANN::FLANN)
if (FLANN_USE_STATIC)
add_library(FLANN::FLANN STATIC IMPORTED)
add_library(FLANN::FLANN ${FLANN_LIBRARY_TYPE} IMPORTED)
set_target_properties(FLANN::FLANN PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${FLANN_INCLUDE_DIR}")
set_target_properties(FLANN::FLANN PROPERTIES INTERFACE_COMPILE_DEFINITIONS "${PC_FLANN_CFLAGS_OTHER}")
set_property(TARGET FLANN::FLANN APPEND PROPERTY IMPORTED_CONFIGURATIONS "RELEASE")
set_target_properties(FLANN::FLANN PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX")
if(WIN32 AND NOT FLANN_USE_STATIC)
set_target_properties(FLANN::FLANN PROPERTIES IMPORTED_IMPLIB_RELEASE "${FLANN_LIBRARY}")
else()
add_library(FLANN::FLANN SHARED IMPORTED)
endif()
set_target_properties(FLANN::FLANN PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${FLANN_INCLUDE_DIRS}")
set_target_properties(FLANN::FLANN PROPERTIES INTERFACE_COMPILE_DEFINITIONS "${FLANN_DEFINITIONS}")
if(FLANN_LIBRARY)
set_property(TARGET FLANN::FLANN APPEND PROPERTY IMPORTED_CONFIGURATIONS "RELEASE")
set_target_properties(FLANN::FLANN PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX")
if(WIN32 AND NOT FLANN_USE_STATIC)
set_target_properties(FLANN::FLANN PROPERTIES IMPORTED_IMPLIB_RELEASE "${FLANN_LIBRARY}")
else()
set_target_properties(FLANN::FLANN PROPERTIES IMPORTED_LOCATION_RELEASE "${FLANN_LIBRARY}")
endif()
set_target_properties(FLANN::FLANN PROPERTIES IMPORTED_LOCATION_RELEASE "${FLANN_LIBRARY}")
endif()
if(FLANN_LIBRARY_DEBUG)
set_property(TARGET FLANN::FLANN APPEND PROPERTY IMPORTED_CONFIGURATIONS "DEBUG")
Expand Down