# Can CMAKE\_\<LANG\>\_COMPILE\_OBJECT call a script or a macro/function?

**URL:** https://discourse.cmake.org/t/can-cmake-lang-compile-object-call-a-script-or-a-macro-function/4448
**Category:** Code
**Tags:** os:linux, gen:makefiles
**Created:** [November 8, 2021, 3:56pm UTC](https://discourse.cmake.org/t/can-cmake-lang-compile-object-call-a-script-or-a-macro-function/4448 "2021-11-08T15:56:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![shabiel](https://discourse.cmake.org/user_avatar/discourse.cmake.org/shabiel/32/1285_2.png) [@shabiel](https://discourse.cmake.org/u/shabiel)
#### Post date: [November 8, 2021, 3:56pm UTC](https://discourse.cmake.org/t/can-cmake-lang-compile-object-call-a-script-or-a-macro-function/4448/1 "2021-11-08T15:56:41Z")

</div>

This is a follow-up to [Need CMake M Plugin help: Single target, Single source, multiple objects, multiples libraries](https://discourse.cmake.org/t/need-cmake-m-plugin-help-single-target-single-source-multiple-objects-multiples-libraries/2951).

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

Our original code looks like this:

```auto
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.

```auto
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()

```

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 13, 2021, 11:06pm UTC](https://discourse.cmake.org/t/can-cmake-lang-compile-object-call-a-script-or-a-macro-function/4448/2 "2021-11-13T23:06:42Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![shabiel](https://discourse.cmake.org/user_avatar/discourse.cmake.org/shabiel/32/1285_2.png) [@shabiel](https://discourse.cmake.org/u/shabiel)
#### Post date: [November 15, 2021, 3:27pm UTC](https://discourse.cmake.org/t/can-cmake-lang-compile-object-call-a-script-or-a-macro-function/4448/3 "2021-11-15T15:27:38Z")

</div>

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