Skip to content

Commit aa7c679

Browse files
authored
Merge pull request #5582 from mvieth/find_package_behaviour
Make sure that find_package(PCL) does not leak internals or overwrite variables
2 parents b390826 + bbbe9f8 commit aa7c679

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

PCLConfig.cmake.in

+14-3
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ endmacro()
268268
# `--> component is induced ==> warn and remove it
269269
# from the list
270270

271-
macro(find_external_library _component _lib _is_optional)
271+
function(find_external_library _component _lib _is_optional)
272272
if("${_lib}" STREQUAL "boost")
273273
find_boost()
274274
elseif("${_lib}" STREQUAL "eigen")
@@ -299,13 +299,20 @@ macro(find_external_library _component _lib _is_optional)
299299
find_glew()
300300
elseif("${_lib}" STREQUAL "opengl")
301301
find_package(OpenGL)
302+
elseif("${_lib}" STREQUAL "pcap")
303+
find_package(Pcap)
304+
elseif("${_lib}" STREQUAL "png")
305+
find_package(PNG)
306+
else()
307+
message(WARNING "${_lib} is not handled by find_external_library")
302308
endif()
303309

304310
string(TOUPPER "${_component}" COMPONENT)
305311
string(TOUPPER "${_lib}" LIB)
306312
string(REGEX REPLACE "[.-]" "_" LIB ${LIB})
307313
if(${LIB}_FOUND)
308314
list(APPEND PCL_${COMPONENT}_INCLUDE_DIRS ${${LIB}_INCLUDE_DIRS})
315+
set(PCL_${COMPONENT}_INCLUDE_DIRS ${PCL_${COMPONENT}_INCLUDE_DIRS} PARENT_SCOPE)
309316

310317
if(${LIB} MATCHES "VTK")
311318
if(${${LIB}_VERSION_MAJOR} GREATER_EQUAL 9)
@@ -317,12 +324,16 @@ macro(find_external_library _component _lib _is_optional)
317324
include(${${LIB}_USE_FILE})
318325
else()
319326
list(APPEND PCL_${COMPONENT}_LIBRARY_DIRS "${${LIB}_LIBRARY_DIRS}")
327+
set(PCL_${COMPONENT}_LIBRARY_DIRS ${PCL_${COMPONENT}_LIBRARY_DIRS} PARENT_SCOPE)
320328
endif()
321329
if(${LIB}_LIBRARIES)
322330
list(APPEND PCL_${COMPONENT}_LINK_LIBRARIES "${${LIB}_LIBRARIES}")
331+
set(PCL_${COMPONENT}_LINK_LIBRARIES ${PCL_${COMPONENT}_LINK_LIBRARIES} PARENT_SCOPE)
332+
set(PCL_${LIB}_LIBRARIES ${${LIB}_LIBRARIES} PARENT_SCOPE) # Later appended to PCL_LIBRARIES
323333
endif()
324334
if(${LIB}_DEFINITIONS AND NOT ${LIB} STREQUAL "VTK")
325335
list(APPEND PCL_${COMPONENT}_DEFINITIONS ${${LIB}_DEFINITIONS})
336+
set(PCL_${COMPONENT}_DEFINITIONS ${PCL_${COMPONENT}_DEFINITIONS} PARENT_SCOPE)
326337
endif()
327338
else()
328339
if("${_is_optional}" STREQUAL "OPTIONAL")
@@ -339,7 +350,7 @@ macro(find_external_library _component _lib _is_optional)
339350
endif()
340351
endif()
341352
endif()
342-
endmacro()
353+
endfunction()
343354

344355
macro(pcl_check_external_dependency _component)
345356
endmacro()
@@ -658,7 +669,7 @@ endif()
658669
pcl_remove_duplicate_libraries(PCL_COMPONENTS PCL_LIBRARIES)
659670

660671
# Add 3rd party libraries, as user code might include our .HPP implementations
661-
list(APPEND PCL_LIBRARIES ${BOOST_LIBRARIES} ${OPENNI_LIBRARIES} ${OPENNI2_LIBRARIES} ${ENSENSO_LIBRARIES} ${davidSDK_LIBRARIES} ${DSSDK_LIBRARIES} ${RSSDK_LIBRARIES} ${RSSDK2_LIBRARIES} ${VTK_LIBRARIES})
672+
list(APPEND PCL_LIBRARIES ${PCL_BOOST_LIBRARIES} ${PCL_OPENNI_LIBRARIES} ${PCL_OPENNI2_LIBRARIES} ${PCL_ENSENSO_LIBRARIES} ${PCL_davidSDK_LIBRARIES} ${PCL_DSSDK_LIBRARIES} ${PCL_RSSDK_LIBRARIES} ${PCL_RSSDK2_LIBRARIES} ${PCL_VTK_LIBRARIES})
662673
if (TARGET FLANN::FLANN)
663674
list(APPEND PCL_LIBRARIES FLANN::FLANN)
664675
endif()

cmake/Modules/FindFLANN.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
# Early return if FLANN target is already defined. This makes it safe to run
3838
# this script multiple times.
3939
if(TARGET FLANN::FLANN)
40+
set(FLANN_FOUND ON)
4041
return()
4142
endif()
4243

cmake/Modules/Findlibusb.cmake

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
# Early return if libusb target is already defined. This makes it safe to run
3737
# this script multiple times.
3838
if(TARGET libusb::libusb)
39+
set(libusb_FOUND ON)
40+
set(LIBUSB_FOUND ON)
3941
return()
4042
endif()
4143

@@ -67,6 +69,7 @@ find_package_handle_standard_args(libusb DEFAULT_MSG libusb_LIBRARIES libusb_INC
6769
mark_as_advanced(libusb_INCLUDE_DIRS libusb_LIBRARIES)
6870

6971
if(libusb_FOUND)
72+
set(LIBUSB_FOUND ON)
7073
add_library(libusb::libusb UNKNOWN IMPORTED)
7174
set_target_properties(libusb::libusb PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${libusb_INCLUDE_DIR}")
7275
set_target_properties(libusb::libusb PROPERTIES IMPORTED_LOCATION "${libusb_LIBRARIES}")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
2+
3+
# This is not really a tutorial, but instead tests the behaviour of find_package(PCL) when built together with the other tutorials
4+
project(cmake_test)
5+
6+
set(BOOST_LIBRARIES "boost_dont_overwrite")
7+
set(Boost_LIBRARIES "boost_dont_overwrite")
8+
find_package(PCL REQUIRED)
9+
10+
if(NOT "${BOOST_LIBRARIES}" STREQUAL "boost_dont_overwrite")
11+
message(FATAL_ERROR "find_package(PCL) changed the value of BOOST_LIBRARIES")
12+
endif()
13+
if(NOT "${Boost_LIBRARIES}" STREQUAL "boost_dont_overwrite")
14+
message(FATAL_ERROR "find_package(PCL) changed the value of Boost_LIBRARIES")
15+
endif()

0 commit comments

Comments
 (0)