# Possible to base custom command on generator?

**URL:** https://discourse.cmake.org/t/possible-to-base-custom-command-on-generator/11355
**Category:** Code
**Created:** [July 30, 2024, 5:51pm UTC](https://discourse.cmake.org/t/possible-to-base-custom-command-on-generator/11355 "2024-07-30T17:51:36Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [July 30, 2024, 5:51pm UTC](https://discourse.cmake.org/t/possible-to-base-custom-command-on-generator/11355/1 "2024-07-30T17:51:36Z")

</div>

All,

I’m not sure how to do what I want, so I’m hoping the gurus here can help. In some code where we build Wavewatch in the model I work on, we have CMake like this:

```cmake
set (WW3ESMF_F90)
foreach(src_file ${WW3ESMF_FTN})
    STRING(REGEX REPLACE ".ftn" ".F90" gen_src_file ${src_file})
    STRING(REGEX REPLACE "/" "_" gen_log_file ${gen_src_file})
    add_custom_command(
        OUTPUT ${gen_src_file}
        BYPRODUCTS ${gen_src_file}
        DEPENDS w3adc ${ftn_dir}/${src_file}
        COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/run_w3adc.sh ${ftn_dir} ${src_file} > ${gen_log_file}.w3adc.log 2>&1
        COMMENT "Running w3adc ${src_file}")
    list(APPEND WW3ESMF_F90 ${gen_src_file})
endforeach()

```

because the Wavewatch tag we currently use is the older style that had files like `constants.ftn` that you then used `w3adc` on to generate `constants.f90`. Weird, but works. (I think Wavewatch has gotten rid of this but we still have it until the next release.)

Recently I was reminded that Ninja no longer builds our model and I tracked it down to this block and the fact that things build just fine if the `BYPRODUCTS` line is not in that `add_custom_command`.

Now the fun part: apparently if I want to build this with GNU plus the Unix Makefiles generator, that `BYPRODUCTS` is needed!

With some testing I see that:

```cmake
    if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
      set(BYPRODUCTS ${gen_src_file})
    else()
      set(BYPRODUCTS "")
    endif()
    add_custom_command(
        OUTPUT ${gen_src_file}
        BYPRODUCTS ${BYPRODUCTS}
        DEPENDS w3adc ${ftn_dir}/${src_file}
        COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/run_w3adc.sh ${ftn_dir} ${src_file} > ${gen_log_file}.w3adc.log 2>&1
        COMMENT "Running w3adc ${src_file}")

```

works but it seems…wrong? Works, but wrong.

So, I was wondering if there is any clever generator expression I could use to say “If `CMAKE_GENERATOR` is Ninja, no `BYPRODUCTS`, else yes `BYPRODUCTS`?”

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [July 31, 2024, 7:02am UTC](https://discourse.cmake.org/t/possible-to-base-custom-command-on-generator/11355/2 "2024-07-31T07:02:35Z")

</div>

Having BYPRODUCT point to the output file is weird.

Anyway, use a variable that include BYPRODUCT like

> set ( args BYPRODUCT … )
