Can CMAKE_<LANG>_COMPILE_OBJECT call a script or a macro/function?

This is a follow-up to Need CMake M Plugin help: Single target, Single source, multiple objects, multiples libraries.

Briefly, I have one source file, and I need to make two object files.

Our original code looks like this:

set(CMAKE_M_COMPILE_OBJECT "LC_ALL=\"${LC_ALL}\" ydb_chset=\"${ydb_chset}\" ydb_icu_version=\"${icu_version}\" <CMAKE_M_COMPILER> -object=<OBJECT>")

Our current strategy involves setting these environment variables separately for two CMake invocations in order to produce two different object files. We don’t want to do that, but rather we would rather have a single CMake invocation.

I wrote what is desired as a macro, but the macro does not get called at the right times if I use it directly.

macro(M_COMPILE)
        foreach(source_file ${ARGN})
                message("--> " ${source_file})
                get_filename_component(output_file ${source_file} NAME_WE)
                add_custom_command(
                        OUTPUT ${output_file}
                        COMMAND ${CMAKE_M_COMPILER} LC_ALL=\"${LC_ALL}\" ydb_chset=\"M\" <CMAKE_M_COMPILER> -object=${output_file}.o ${M_FLAGS}
                        COMMAND ${CMAKE_M_COMPILER} LC_ALL=\"${LC_ALL}\" ydb_chset=\"UTF-8\" <CMAKE_M_COMPILER> -object={output_file}_utf8.o ${M_FLAGS}
                        DEPENDS ${source_file}
                )
        endforeach()
endmacro()

The *_COMPILE_* (and other similar variables) are a static template that is replaced in CMake’s C++ code. There’s no mechanism to use a command in their place (the scope of such a call is quite ambiguous and genex usage anywhere would be…weird to say the least).

Thank you Ben! I am thinking of another approach of doing what I want doing now. I will be posting another question.