XCode config generator expression becomes NOCONFIG

I configure a file with generator expressions targeting single and multi configuration generators like so:

configure_file(StartServer.cpp.in StartServer.cpp.in @ONLY) 
file(GENERATE
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/StartServer_$<CONFIG>.cpp
  INPUT ${CMAKE_CURRENT_BINARY_DIR}/StartServer.cpp.in
)

then I add the generated file as a target source:

target_sources(StartServer
    PRIVATE
        ${CMAKE_CURRENT_BINARY_DIR}/StartServer_$<CONFIG>.cpp
)

This generates the various StartServer_Debug.cpp, StartServer_Release.cpp files and works on every generator (inluding multi config ones like Visual Studio) except XCode.

With XCode I get the error:

Cannot find source file:
    <...>/build-xcode-clang-debug/tests/e2e_tests/setup/StartServer_NOCONFIG.cpp

Why is $<CONFIG> expanded to NOCONFIG. I printed the generator expression to a file, it doesn’t have the value NOCONFIG.

What’s the proper way of doing this so it works for every available generator on every platform?

I could be wrong, but I think the Xcode generator doesn’t support per-config source files. @brad.king would be able to confirm this.

What can I then do to use generator expressions in source files for xcode?

What can I then do to use generator expressions in source files for xcode?

If they evaluate to per-config values, you can’t.

The Xcode generator does not support per-config sources. IIRC, Xcode does not provide a simple and robust way to express them.

In some cases one can work around this by using per-config COMPILE_DEFINITIONS along with a source that uses preprocessor conditions to #include the source of the matching configuration.

I want the value $<TARGET_FILE:BeansServer> to be available in my script. Currently I use configure_file and generate for this.
Can I pass this via target_compile_definitions as a macro instead?
Will that work for Xcode and all other generators`

Yes, target_compile_definitions should work.

The COMPILE_DEFINITIONS source file property might also work too, but IIRC Xcode might have trouble with per-config per-source flags too.