How to set swift macros from cmake

I have a swift project which uses macros for conditional compilation.

How to set macro: Go to Target → BuildSettings → Swift Compiler - Custom Flags → Active Compilation Conditions and add your macro (Ex: sample macro - ELDIABLO in this case).

Then the following swift code,

#if ELDIABLO
NSLog("ELDIABLO macro detected!")
#else 
NSLog("ELDIABLO macro not detected!")
#endif

…produces the desired output.

ELDIABLO macro detected!

Now, how do I achieve this through cmake?

When I do the following,

set(CMAKE_Swift_FLAGS "${CMAKE_Swift_FLAGS} ELDIABLO")  

the macro gets added to Swift Compiler - Custom Flags → Other Swift Flags (shown in the above image besides Active Compilation Conditions).

But it must be added in Swift Compiler - Custom Flags → Active Compilation Conditions, else (here’s why), it will not get detected in swift files.

How can I set swift macros in Active Compilation Conditions through cmake?

Are you sure the flag doesn’t have some prefix like -D to tell it how to use your ELDIABLO name? As it is…is it a source file, a file to link, or something else?

-D is required when setting macro to Other Swift Flags
When setting to Active Compilation Condition, -D is not required.

We are trying to set these SWIFT flags in CMakePreset.json or cmake file. We could not find a way how to set a flag in CMake which gets reflected in xcode’s Active Compilation Conditions which Swift uses for macro.

In Xcode, sure. But CMake is not Xcode. How does CMake know that this should go into that place?

Thats what we are trying to understand that how we can pass Active Compilation Condition to xcode buildsistem via cmake.

If we set something like below in cmake build system.

set(CMAKE_Swift_FLAGS “${CMAKE_Swift_FLAGS} -DELDIABLO=0”)

flag ELDIABLO gets passed to Other Swift Flags in xcode. But we want to know if there is a way in cmake to pass Active Compilation Condition flags to Swift.

There might not be a way right now. That is, what is done now has been sufficient for building, but for actual Xcode users it might be non-optimal. I suggest filing a new issue detailing what behavior differences occur when the flag is recategorized.