Platform/Generator specific properties in a condition

Often I see cmake code like this:

if(APPLE)
    set(CMAKE_XCODE_GENERATE_SCHEME TRUE)
else()
    set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECTlab)
endif()

Is it really necessary to do the if…else clause? Isn’t it clear already by the name of the property that it belongs to XCODE or VS? I would rather write:

set(CMAKE_XCODE_GENERATE_SCHEME TRUE)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECTlab)

Does it have any impact to leave out the if…else clause? What’s the best practice for code like this?

Yeah, the properties are only “active” when the generator looks at them, so you can make these unconditional since the other generators will just ignore them.

1 Like