naderman
naderman

Reputation: 1090

How to check if a macro exists in CMake

How do I correctly check if a macro is defined in CMake?

macro(foo)
    message("foo")
endmacro()

if(<what goes here?>)
    foo()
endif()

Upvotes: 19

Views: 8946

Answers (2)

sakra
sakra

Reputation: 65771

The if command supports a COMMAND clause for that purpose:

if(COMMAND foo)
    foo()
endif()

Upvotes: 29

julp
julp

Reputation: 4010

Use MACROS property for a given directory.

get_directory_property(DEFINED_MACROS DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} MACROS)
list(FIND DEFINED_MACROS "foo" MACRO_INDEX)
if(MACRO_INDEX EQUAL -1)
    # macro foo does not exist
else(MACRO_INDEX EQUAL -1)
    # macro foo exists
endif(MACRO_INDEX EQUAL -1)

Upvotes: 2

Related Questions