build(sim): convert PX4-OpticalFlow to git submodule (#27184)

Replace the ExternalProject_Add-based fetch of PX4/PX4-OpticalFlow with a
proper git submodule at src/modules/simulation/gz_plugins/optical_flow/PX4-OpticalFlow,
matching how every other external C++ dependency is integrated in PX4 (mavlink,
gps/devices, Micro-XRCE-DDS-Client, libevents, heatshrink, etc.).

The previous approach cloned the repo over the network on every clean build,
pinned to GIT_TAG master (unreproducible), and had generated a string of
follow-up PRs over the last year chasing ExternalProject quirks (install
paths, macOS .dylib vs .so, permissions).

The plugin now consumes the upstream OpticalFlow target directly via
add_subdirectory and px4_add_git_submodule. The upstream repo predates PX4's
strict warning policy, so the OpticalFlow and klt_feature_tracker targets get
-Wno-error plus a few specific -Wno-* flags and -fvisibility=default to build
cleanly inside the PX4 tree.

Signed-off-by: Ramon Roche <mrpollo@gmail.com>
This commit is contained in:
Ramon Roche
2026-04-23 12:57:02 -07:00
committed by GitHub
parent 4760327fa8
commit b911d4414e
4 changed files with 42 additions and 68 deletions
+3
View File
@@ -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
@@ -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()
@@ -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()