Need CMake M Plugin help: Single target, Single source, multiple objects, multiples libraries

Hello everybody,

More than a year later, I am happy to report that I have a resolution.

@craig.scott I tried the ExternalProject paradigm, and it works well, but it was too complex for my taste. PS: I have your CMake book; you are a very good writer!

I contacted Brad, and a couple of email exchanges later, he guided to me to the solution:

  1. Use target_compile_options() to pass <FLAGS> to CMAKE_M_COMPILE_OBJECT.
  2. Use a macro or function for outside users to use this functionality which will create both objects and both install rules at the same time.

The new CMAKE_M_COMPILE_OBJECT looks like this: set(CMAKE_M_COMPILE_OBJECT "LC_ALL=C.utf-8 <FLAGS> <CMAKE_M_COMPILER> -object=<OBJECT>")

Here’s the function:

function(add_ydb_library library_name)
        set(flags)
        set(args)
        set(listArgs SOURCES)
        cmake_parse_arguments(arg "${flags}" "${args}" "${listArgs}" ${ARGN})

        if (NOT arg_SOURCES)
                message(FATAL_ERROR "[add_ydb_library]: SOURCES is a required argument")
        endif()
        if (SOURCES IN_LIST arg_KEYWORDS_MISSING_VALUES)
                message(FATAL_ERROR "[add_ydb_library]: SOURCES requires at least one value")
        endif()
        add_library(${library_name}M SHARED ${arg_SOURCES})
        target_compile_options(${library_name}M PRIVATE ydb_chset=M ydb_icu_version=)
        set_target_properties(${library_name}M PROPERTIES PREFIX "")
        set_target_properties(${library_name}M PROPERTIES LIBRARY_OUTPUT_NAME ${library_name})
        set_target_properties(${library_name}M PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
        if(ydb_icu_version)
                add_library(${library_name}utf8 SHARED ${arg_SOURCES})
                target_compile_options(${library_name}utf8 PRIVATE ydb_chset=utf-8 ydb_icu_version=${ydb_icu_version})
                set_target_properties(${library_name}utf8 PROPERTIES PREFIX "")
                set_target_properties(${library_name}utf8 PROPERTIES LIBRARY_OUTPUT_NAME ${library_name})
                set_target_properties(${library_name}utf8 PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/utf8)
        endif()
endfunction()