-
Notifications
You must be signed in to change notification settings - Fork 127
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
surround variables with quotes inside if
?
#443
Comments
Thanks for this issue! |
We discussed this, and unfortunately this is a case-by-case basis kind of thing. Certainly there are times where we want to use quotes to be able to deal with things like semicolons (lists) in CMake, but there are other times (like macros) where we specifically do not want quotes. My suggestion here is to open PR (or PRs) that specifically fix the places that you see as issues, and we can review them one-by-one. |
I see, I have not played enough with cmake to find cases where unquoted if are preferrable. diff --git a/ament_cmake_auto/cmake/ament_auto_package.cmake b/ament_cmake_auto/cmake/ament_auto_package.cmake
index 3c6c7bb..a69ce90 100644
--- a/ament_cmake_auto/cmake/ament_auto_package.cmake
+++ b/ament_cmake_auto/cmake/ament_auto_package.cmake
@@ -66,14 +66,16 @@ macro(ament_auto_package)
endif()
# export and install all libraries
- if(NOT ${PROJECT_NAME}_LIBRARIES STREQUAL "")
+ if(NOT "${${PROJECT_NAME}_LIBRARIES}" STREQUAL "")
ament_export_libraries(${${PROJECT_NAME}_LIBRARIES})
install(
TARGETS ${${PROJECT_NAME}_LIBRARIES}
+ EXPORT ${PROJECT_NAME}Targets
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
+ ament_export_targets(${PROJECT_NAME}Targets)
endif()
# install all executables Where if the package does not define any library, we still get into the |
I guess I won't surprise anyone saying cmake
if()
is quite a funny lad.For example, let's consider this one:
ament_cmake/ament_cmake_auto/cmake/ament_auto_package.cmake
Line 69 in e78ed7e
${${PROJECT_NAME}_LIBRARIES}
is not an empty string, it evaluates toTRUE
${${PROJECT_NAME}_LIBRARIES}
is the empty string""
, it evaluates toFALSE
${PROJECT_NAME}_LIBRARIES
variable has not been defined, it evaluates to...TRUE
!This is because in cmake,
if(var ...)
first check if a variable namedvar
exists and if it is not the case, it considers var is the string"var"
(which is not""
!)A simple example:
So for example, if
ament_auto_package()
is called on a package that does not define any library, the${PROJECT_NAME}_LIBRARIES
variable is not defined but the command will still callament_export_libraries
andinstall
(with no target). Although it is mostly harmless, this is bug-prone at quite head-scratching.A minimal example:
ros2 pkg create --build-type ament_cmake test
.CMakeLists.txt
as such:ament_auto_package.cmake
if()
shown above. For example:You will get the following warning:
Note that many other
if
in the code base have this issue.The usual workaround is to always surround variables with quotes. For example:
The text was updated successfully, but these errors were encountered: