[CMake] CMake macro problem

Marcel Loose loose at astron.nl
Fri Feb 13 11:47:44 EST 2009


Hi,

I have a problem with a self-written CMake macro, but I don't know
exactly what the problem is.

I have defined macro(SetIfElse var val def), which is supposed to assign
the contents of val to var, when val is defined; otherwise it should
assign the contents of def to var. This macro was inspired by the
macro(SET_IF_NOT_SET var val) from the CMake module CTest.cmake

When I feed the CMakeLists.txt file below to cmake, I would expect as
output: 'text=Hello World', but instead I get 'text=Sorry mate!'
Could you please explain what I'm doing wrong?

project(hallo)
cmake_minimum_required(VERSION 2.6)

# Set variable ${var} equal to ${val} if ${val} is defined; else to
${def}.
macro(SetIfElse var val def)
  if(DEFINED "${val}")
    set("${var}" "${val}")
  else(DEFINED "${val}")
    set("${var}" "${def}")
  endif(DEFINED "${val}")
endmacro(SetIfElse)

set(hello "Hello World")
set(mate  "Sorry mate!")

SetIfElse(text "${hello}" "${mate}")
message(STATUS "text=${text}")





More information about the CMake mailing list