robotology / ycm-cmake-modules

YCM (YCM CMake modules) is a collection of various useful CMake modules.
http://robotology.github.io/ycm-cmake-modules
Other
52 stars 23 forks source link

YCMEPHelper: Do not de-reference CMAKE_EXPORT_COMPILE_COMMANDS if we do not know if CMAKE_EXPORT_COMPILE_COMMANDS is defined #443

Closed traversaro closed 9 months ago

traversaro commented 9 months ago

https://github.com/robotology/ycm/pull/442 contained a critical regression, if CMAKE_EXPORT_COMPILE_COMMANDS was not defined, the superbuild failed with:

CMake Error at b/_deps/ycm-src/modules/YCMEPHelper.cmake:260 (if):
  if given arguments:

    "DEFINED" "CMAKE_EXPORT_COMPILE_COMMANDS" "AND"

  Unknown arguments specified
Call Stack (most recent call first):
  b/_deps/ycm-src/modules/YCMEPHelper.cmake:1468 (_ycm_setup)
  CMakeLists.txt:63 (include)

-- Configuring incomplete, errors occurred!
Error: Process completed with exit code 1.

The problem is that if CMAKE_EXPORT_COMPILE_COMMANDS is not defined, ${CMAKE_EXPORT_COMPILE_COMMANDS} evaluates to an empty string, and:

if(DEFINED CMAKE_EXPORT_COMPILE_COMMANDS AND)

is not syntactically valid cmake. The fix is to use CMAKE_EXPORT_COMPILE_COMMANDS instead of ${CMAKE_EXPORT_COMPILE_COMMANDS}, as in that way the if is always a valid cmake syntax:

if(DEFINED CMAKE_EXPORT_COMPILE_COMMANDS AND CMAKE_EXPORT_COMPILE_COMMANDS )

and the if of CMake automatically dereference a variable if it finds it in the if clause.

traversaro commented 9 months ago

Thanks @gabrielenava @davidetome @davidegorbani @nicktrem for reporting the issue! As I shown to the iRonCub team, if this happens in the future you can specify any (hopefully working) version of YCM to use to the superbuild by passing the -DYCM_TAG=v0.16.2 (in place of v0.16.2, use the tag that you want to use) CMake option to the superbuild.