[CMake] Possible bug in cmake version 2.4-patch 6 with macroargument testing

Ken Martin ken.martin at kitware.com
Tue May 29 10:28:39 EDT 2007


A macro in cmake is a bit like a cpp macro. It is a string replacement
operation that replaces ${varname} with the actual argument to the macro.
Then it executes the commands. At one point we considered (and tried) making
the macro arguments be variables but there are existing projects out there
that rely on a macro being a macro, not a function. i.e. they rely on the
string replacement behavior as opposed to variable behavior. So what that
means is that in the example below:

 MACRO (MYMACRO boolarg)
 	MESSAGE ("MYMACRO(${boolarg})")
 	IF (boolarg)

After string replacement of ${boolarg} with TRUE looks like

MACRO (MYMACRO boolarg)
 	MESSAGE ("MYMACRO(TRUE)")
 	IF (boolarg)

The IF statement tests to see if there is a variable defined named boolarg
and there is not (there have been no SETS done on boolarg) and so the
conditional is false. So the trick is that macros need to be thought of as
only doing string replacements on ${something}.

Hope that helps
Ken

> # Possible bug in cmake version 2.4-patch 6 where macro arguments
> # don't test correctly. When run this produces:
> #
> # 	mytrue=TRUE [msg1]
> # 	MYMACRO(TRUE)
> # 	boolarg=TRUE [False path]
> # 	_var=TRUE [True path]
> #
> 
> MACRO (MYMACRO boolarg)
> 	MESSAGE ("MYMACRO(${boolarg})")
> 	IF (boolarg)
> 		MESSAGE ("boolarg=${boolarg} [True path]")
> 	ELSE (boolarg)
> 		MESSAGE ("boolarg=${boolarg} [False path]")
> 	ENDIF (boolarg)
> 
> 	SET (_var ${boolarg})
> 	IF (_var)
> 		MESSAGE ("_var=${_var} [True path]")
> 	ELSE (_var)
> 		MESSAGE ("_var=${_var} [False path]")
> 	ENDIF (_var)
> ENDMACRO (MYMACRO boolarg)
> 
> SET (mytrue TRUE)
> IF (mytrue)
> 	MESSAGE ("mytrue=${mytrue} [msg1]")
> ELSE (mytrue)
> 	MESSAGE ("mytrue=${mytrue} [msg2]")
> ENDIF (mytrue)
> 
> MYMACRO(${mytrue})




More information about the CMake mailing list