[CMake] Return values from CMake functions?

Bartlett, Roscoe A rabartl at sandia.gov
Thu Nov 20 11:19:26 EST 2008


Hello,

Has anyone thought about the possibility of adding return values from CMake functions?  This would be a very useful language feature that the Trilinos CMake files would use everywhere.

Here is an example use case.  One problem with CMake is that it has very loose checking.  For example, if I write:

   IF (MYVARABLE)
     ...
   ENDIF()

Instead of what I meant:

   IF (MYVARIABLE)
     ...
   ENDIF()

then my CMakeLists.txt file will not work correctly.  What I have been doing is to instead write an ASSERT_DEFINED(...) macro and then use it as:

   ASSERT_DEFINED(MYVARIABLE)
   IF (MYVARIABLE)
     ...
   ENDIF()

Of course the problem with this is that I could misspell one of the uses as:

   ASSERT_DEFINED(MYVARIABLE)
   IF (MYVARABLE)
     ...
   ENDIF()

and I am back in the same situation.  The problem is that I am duplicating the variable name.  This is a fundamental software engineering issue that needs to be addressed in some way.

What I would like to be able to write is a function like:

  FUNCTION(ASSERTDEF VARNAME)
    IF(NOT DEFINED ${VARNAME})
      MESSAGE(SEND_ERROR "Error, the variable ${VARNAME} is not defined!")
    ENDIF()
    RETURN(${VARNAME})
  ENDFUNCTION()

You could then use this function like:

   IF (${ASSERTDEF(MYVARIABLE)$)
     ...
   ENDIF()

Then, if I misspelled the variable name as:

   IF (${ASSERTDEF(MYVARABLE)}$)
     ...
   ENDIF()

I would get an error message and processing would stop immediately.

Above, I assume that the syntax:

   ${SOMEFUNCTION(ARGS)}

Is needed to get CMake to expect a return value from the function and then return it, just like it would return a variables value.

How hard would it be to all return values from CMake functions in this way?

Thanks,

- Ross


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20081120/b527d10e/attachment.htm>


More information about the CMake mailing list