Generator expression question

Hi, I have a simple generator expression question. I want to add a source file conditionally based on a cache variable value. My code is:

set(ARM_PLATFORM "ModelSim" CACHE  STRING "Target platform on which this executable will run (ModelSim or MPS2)")


               # retarget.c is needed for ModelSim platform only

but if ARM_PLATFORM equals “ModelSim” retarget.c is not getting compiled (I intend it should).

How is my syntax wrong?

I’m not sure this is a job for generator expressions at all.
I’d rather do something along this:

    target_sources(${EXECUTABLE} PRIVATE retarget.c)

@fenrir Thank you for your suggestion. I have changed my code to use target_sources as you suggested and that fixed that problem. However, I have to use a similar generator expression in target_compile_definitions and it is not working there:

target_compile_definitions(${EXECUTABLE} PRIVATE
        # For ModelSim only: define __FILE_INCOMPLETE (used in stdio.h) so that __FILE can be redefined in retarget.c

So I would still appreciate guidance on the correct syntax please.

I’m not sure about the syntax but what I see is that you’re trying to use generator expressions where standard cmake-time constructs would suffice because they are not dependent on some properties known only at generation-time but on simple variables.
Therefore I’d again suggest:

    target_compile_definitions(${EXECUTABLE} PRIVATE __FILE_INCOMPLETE)

The only thing that I can think of regarding the syntax is whitespace. I strongly suspect that your generator expression actually gets split into 3 arguments waaaay before it has a chance to get evaluated because of the whitespace you’ve put there.

@fenrir Thanks again. I have done what you’ve suggested and it works fine. Much simpler!

Correct, that will have been the case. Also, the use of double-quotes around "ModelSim" inside the generator expression is also not likely to be what you want (I am pretty sure it makes the quotes part of the string to be matched). You may find the following article helpful in clarifying some of these behaviours:

1 Like