diff --git a/.gitmodules b/.gitmodules index 2d406b1017..af800e888b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -115,3 +115,6 @@ [submodule "libmodal-pipe"] path = boards/modalai/voxl2/src/lib/mpa/libmodal-pipe url = https://gitlab.com/voxl-public/voxl-sdk/core-libs/libmodal-pipe.git +[submodule "src/modules/simulation/gz_plugins/optical_flow/PX4-OpticalFlow"] + path = src/modules/simulation/gz_plugins/optical_flow/PX4-OpticalFlow + url = https://github.com/PX4/PX4-OpticalFlow.git diff --git a/src/modules/simulation/gz_plugins/optical_flow/CMakeLists.txt b/src/modules/simulation/gz_plugins/optical_flow/CMakeLists.txt index bdd66e604c..f780f1c986 100644 --- a/src/modules/simulation/gz_plugins/optical_flow/CMakeLists.txt +++ b/src/modules/simulation/gz_plugins/optical_flow/CMakeLists.txt @@ -33,12 +33,37 @@ project(OpticalFlowSystem) -# Include the OpticalFlow external dependency -include(${CMAKE_CURRENT_SOURCE_DIR}/optical_flow.cmake) - -# Find OpenCV find_package(OpenCV REQUIRED COMPONENTS core imgproc) +set(PX4_OPTICAL_FLOW_DIR ${CMAKE_CURRENT_SOURCE_DIR}/PX4-OpticalFlow) + +px4_add_git_submodule(TARGET git_px4_optical_flow PATH "${PX4_OPTICAL_FLOW_DIR}") + +if(NOT TARGET OpticalFlow) + add_subdirectory(${PX4_OPTICAL_FLOW_DIR} ${CMAKE_CURRENT_BINARY_DIR}/PX4-OpticalFlow EXCLUDE_FROM_ALL) + add_dependencies(OpticalFlow git_px4_optical_flow) + add_dependencies(klt_feature_tracker git_px4_optical_flow) + + # PX4-OpticalFlow is an external dependency that predates PX4's strict + # warning policy. Relax -Werror and the few specific warnings it trips so + # upstream source builds cleanly inside our tree. Also restore default + # symbol visibility so the shared library exports its C++ class symbols + # (PX4's global CXX flags set -fvisibility=hidden). + foreach(_tgt OpticalFlow klt_feature_tracker) + target_compile_options(${_tgt} PRIVATE + -Wno-error + -Wno-sign-compare + -Wno-cast-align + -Wno-shadow + -fvisibility=default + ) + set_target_properties(${_tgt} PROPERTIES + CXX_VISIBILITY_PRESET default + VISIBILITY_INLINES_HIDDEN OFF + ) + endforeach() +endif() + add_library(${PROJECT_NAME} SHARED OpticalFlowSensor.cpp OpticalFlowSystem.cpp @@ -51,26 +76,26 @@ target_link_libraries(${PROJECT_NAME} PUBLIC ${GZ_SIM_TARGET} PUBLIC ${GZ_TRANSPORT_TARGET} PUBLIC ${OpenCV_LIBS} - PUBLIC ${OpticalFlow_LIBS} + PUBLIC OpticalFlow ) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} PUBLIC ${CMAKE_CURRENT_BINARY_DIR} PUBLIC ${OpenCV_INCLUDE_DIRS} - PUBLIC ${OpticalFlow_INCLUDE_DIRS} + PUBLIC ${PX4_OPTICAL_FLOW_DIR}/include + PUBLIC ${PX4_OPTICAL_FLOW_DIR}/external/klt_feature_tracker/include PUBLIC px4_gz_msgs ) -add_dependencies(${PROJECT_NAME} OpticalFlow) - -# Add $ORIGIN to RPATH so the plugin finds libOpticalFlow.so in its own -# directory when installed (the packaging copies both .so files together). +# libOpticalFlow.so is built alongside the plugin in the same output directory +# (CMAKE_LIBRARY_OUTPUT_DIRECTORY is set by the parent gz_plugins CMakeLists), +# so $ORIGIN is sufficient at both build and install time. set_target_properties(${PROJECT_NAME} PROPERTIES - BUILD_RPATH "$ORIGIN;${OPTICAL_FLOW_INSTALL_PREFIX}/lib" - INSTALL_RPATH "$ORIGIN" + BUILD_RPATH "$ORIGIN" + INSTALL_RPATH "$ORIGIN" ) if (NOT CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) - install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION lib/px4_gz_plugins) + install(TARGETS ${PROJECT_NAME} OpticalFlow LIBRARY DESTINATION lib/px4_gz_plugins) endif() diff --git a/src/modules/simulation/gz_plugins/optical_flow/PX4-OpticalFlow b/src/modules/simulation/gz_plugins/optical_flow/PX4-OpticalFlow new file mode 160000 index 0000000000..5d60b9eca7 --- /dev/null +++ b/src/modules/simulation/gz_plugins/optical_flow/PX4-OpticalFlow @@ -0,0 +1 @@ +Subproject commit 5d60b9eca7c069868526421e6a126daa8080fd41 diff --git a/src/modules/simulation/gz_plugins/optical_flow/optical_flow.cmake b/src/modules/simulation/gz_plugins/optical_flow/optical_flow.cmake deleted file mode 100644 index d5fa9dd2c7..0000000000 --- a/src/modules/simulation/gz_plugins/optical_flow/optical_flow.cmake +++ /dev/null @@ -1,55 +0,0 @@ -############################################################################ -# -# Copyright (c) 2025 PX4 Development Team. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in -# the documentation and/or other materials provided with the -# distribution. -# 3. Neither the name PX4 nor the names of its contributors may be -# used to endorse or promote products derived from this software -# without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS -# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, -# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS -# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED -# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN -# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -# POSSIBILITY OF SUCH DAMAGE. -# -############################################################################ - -include(ExternalProject) -find_package(OpenCV REQUIRED) - -if(NOT TARGET OpticalFlow) - set(OPTICAL_FLOW_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/OpticalFlow/install") - - ExternalProject_Add(OpticalFlow - GIT_REPOSITORY https://github.com/PX4/PX4-OpticalFlow.git - GIT_TAG master - PREFIX ${CMAKE_BINARY_DIR}/OpticalFlow - INSTALL_DIR ${OPTICAL_FLOW_INSTALL_PREFIX} - CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${OPTICAL_FLOW_INSTALL_PREFIX} - BUILD_BYPRODUCTS - ${OPTICAL_FLOW_INSTALL_PREFIX}/lib/libOpticalFlow${CMAKE_SHARED_LIBRARY_SUFFIX} - UPDATE_DISCONNECTED ON - BUILD_ALWAYS OFF - STEP_TARGETS build - ) - - set(OpticalFlow_INCLUDE_DIRS ${OPTICAL_FLOW_INSTALL_PREFIX}/include CACHE INTERNAL "") - set(OpticalFlow_LIBS ${OPTICAL_FLOW_INSTALL_PREFIX}/lib/libOpticalFlow${CMAKE_SHARED_LIBRARY_SUFFIX} CACHE INTERNAL "") -endif()