1

I'm working with a CMake body of code that wants to set certain variables with generator expressions, e.g., the second entry in

target_sources( date-tz
  PUBLIC
    include/date/tz.h
    $<$<BOOL:${IOS}>:include/date/ios.h>
)

The idea here is: if ${IOS} is TRUE, then the second line expands to

include/date/ios.h

and otherwise to nothing (""). This however is not true: The output target files will contain something like

$<$<BOOL:TRUE>:include/date/ios.h>

I gather from here that generator expressions are for things that are not known at configure time, and hence are probably not what we actually want.

One alternative would be to set

if (${IOS})
   set(ios_h include/date/ios.h)
else()
   set(ios_h "")
endif()

target_sources( date-tz
  PUBLIC
    include/date/tz.h
    ${ios_h}
)

which sure enough is evaluated at configure time. Is this the correct approach? Is there a shorter (inline) form of the latter?

This is with CMake 3.16.3.

Kevin
  • 16,549
  • 8
  • 60
  • 74
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249

1 Answers1

2

Is this the correct approach?

Approach, yes. But don't expand IOS, if handles that. Just:

if (IOS)
   target_sources(date-tz PUBLIC include/date/ios.h)
endif()

Is there a shorter (inline) form of the latter?

You can write a macro.

I gather from here that generator expressions are for things that are not known at configure time, and hence are probably not what we actually want.

Personally, I would say: neee, use the way is most readable to you. I would say using a generator expression here is perfectly fine. (But I guess if someone uses a IDE that searches headers from cmake, it may have problems? I don't know, never used such IDEs).

The idea here is: if ${IOS} is TRUE

That sounds strange, IOS should be set to 1. So no need for $<BOOL, just:

$<${IOS}:include/date/ios.h>

There is also $<PLATFORM_ID that you can use, that allows for a comma separated list of platforms, I guess that would be along:

$<$<PLATFORM_ID:iOS>:include/date/ios.h>
KamilCuk
  • 120,984
  • 8
  • 59
  • 111