Skip to content

Commit 416cb3e

Browse files
committed
Updated name of DESTDIR to avoid confusion with cmake variable and fixed code to not use generator expressions in final code where they are not evaluated.
1 parent c80a0c9 commit 416cb3e

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

CMakeLists.txt

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,29 @@ if(${GLOBAL_INSTALL})
168168
message(FATAL_ERROR "Could not find qml plugin dir. Is qml installed?")
169169
endif()
170170
message(STATUS "Plugin will be installed to ${QT_INSTALL_QML}")
171-
set(DESTDIR "${QT_INSTALL_QML}/${TARGETPATH}")
171+
set(QML_INSTALL_DIR "${QT_INSTALL_QML}/${TARGETPATH}")
172172
install(TARGETS ${PROJECT_NAME} EXPORT export_${PROJECT_NAME} DESTINATION lib)
173-
install(FILES ${CMAKE_CURRENT_LIST_DIR}/qmldir DESTINATION ${DESTDIR})
173+
install(FILES ${CMAKE_CURRENT_LIST_DIR}/qmldir DESTINATION ${QML_INSTALL_DIR})
174174
# Create symlink to the library instead of installing it twice
175175
# If code loads the library from two separate inodes (files), the singletons will not be shared.
176-
install(CODE "
177-
file(MAKE_DIRECTORY \"\$ENV{DESTDIR}${DESTDIR}\")
176+
# Compute relative path from QML plugin dir to lib dir
177+
file(RELATIVE_PATH REL_LIB_PATH "${QML_INSTALL_DIR}" "${CMAKE_INSTALL_PREFIX}/lib")
178+
179+
# Generate a script to create the symlink because install(CODE) does not support generator expressions.
180+
set(INSTALL_SYMLINK_CODE "
181+
file(MAKE_DIRECTORY \"\$ENV{DESTDIR}${QML_INSTALL_DIR}\")
178182
file(CREATE_LINK
179-
\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/lib/\$<TARGET_FILE_NAME:${PROJECT_NAME}>\"
180-
\"\$ENV{DESTDIR}${DESTDIR}/\$<TARGET_FILE_NAME:${PROJECT_NAME}>\"
183+
\"${REL_LIB_PATH}/$<TARGET_FILE_NAME:${PROJECT_NAME}>\"
184+
\"\$ENV{DESTDIR}${QML_INSTALL_DIR}/$<TARGET_FILE_NAME:${PROJECT_NAME}>\"
181185
SYMBOLIC
186+
RESULT LINK_RESULT
182187
)
188+
if(NOT LINK_RESULT STREQUAL \"0\")
189+
message(FATAL_ERROR \"Failed to create symlink: \${LINK_RESULT}. Symlink is required to prevent singleton duplication. On Windows, enable Developer Mode or run as administrator.\")
190+
endif()
183191
")
192+
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/install_symlink_global_$<CONFIG>.cmake" CONTENT "${INSTALL_SYMLINK_CODE}")
193+
install(CODE "include(\"${CMAKE_CURRENT_BINARY_DIR}/install_symlink_global_\${CMAKE_INSTALL_CONFIG_NAME}.cmake\")")
184194
else()
185195
message(STATUS "Installing as part of a ROS2 workspace.")
186196
# Register plugin to be found by QML
@@ -190,14 +200,23 @@ else()
190200
install(FILES qmldir DESTINATION lib/qml/Ros2)
191201
# Create symlink to the library instead of installing it twice
192202
# If code loads the library from two separate inodes (files), the singletons will not be shared.
193-
install(CODE "
203+
204+
# Generate a script to create the symlink because install(CODE) does not support generator expressions.
205+
set(INSTALL_SYMLINK_CODE "
194206
file(MAKE_DIRECTORY \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/lib/qml/Ros2\")
195207
file(CREATE_LINK
196-
\"../../\$<TARGET_FILE_NAME:${PROJECT_NAME}>\"
197-
\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/lib/qml/Ros2/\$<TARGET_FILE_NAME:${PROJECT_NAME}>\"
208+
\"../../$<TARGET_FILE_NAME:${PROJECT_NAME}>\"
209+
\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/lib/qml/Ros2/$<TARGET_FILE_NAME:${PROJECT_NAME}>\"
198210
SYMBOLIC
211+
RESULT LINK_RESULT
199212
)
213+
if(NOT LINK_RESULT STREQUAL \"0\")
214+
message(FATAL_ERROR \"Failed to create symlink: \${LINK_RESULT}. Symlink is required to prevent singleton duplication. On Windows, enable Developer Mode or run as administrator.\")
215+
endif()
200216
")
217+
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/install_symlink_local_$<CONFIG>.cmake" CONTENT "${INSTALL_SYMLINK_CODE}")
218+
install(CODE "include(\"${CMAKE_CURRENT_BINARY_DIR}/install_symlink_local_\${CMAKE_INSTALL_CONFIG_NAME}.cmake\")")
219+
201220
# The export is only necessary if part of a ROS2 workspace
202221
ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET)
203222
ament_export_include_directories(include)

0 commit comments

Comments
 (0)