[CMake] Return values from CMake functions?

Ken Martin ken.martin at kitware.com
Thu Nov 20 13:08:22 EST 2008


While not super sexy, you can do what you are looking for with a minor
twist. The following code illustrates it.

function(assertdef VARNAME RESULT_NAME)
    if(NOT DEFINED ${VARNAME})
      message(SEND_ERROR "Error, the variable ${VARNAME} is not defined!")
    endif()
    set (${RESULT_NAME} ${VARNAME} PARENT_SCOPE)
endfunction()

# comment the next line to see the two different results
set (myvar 1)

assertdef(myvar varname)
if (${varname})
  message("${varname} is true")
endif()


With respect to the general question of functions returning values it could
be done but it is a bit of a big change.  Functions and commands look the
same (and should act the same IMO) to the people using them. So really we
are talking about commands returning values. This is mostly just a syntax
issue. Right now we have

command(arg arg arg
) 

to support return values we need something that could handle 


command (arg command2(arg arg) arg arg 
) 

or in your case

if(assertdef(foo))

or in another case 

set(foo get_property(
)) 

etc. This hits the parser and the argument processing in CMake but I think
it could be done. I guess I’m not sure if we *should* do it. Open to
opinions here.

Ken

Ken Martin PhD
Kitware Inc.
28 Corporate Drive
Clifton Park NY 12065
ken.martin at kitware.com
518 371 3971 (w/f)

From: cmake-bounces at cmake.org [mailto:cmake-bounces at cmake.org] On Behalf Of
Bartlett, Roscoe A
Sent: Thursday, November 20, 2008 11:19 AM
To: cmake at cmake.org
Subject: [CMake] Return values from CMake functions?

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
 
 



More information about the CMake mailing list