CMake update, then: Built-in flow control command "add_custom_target" cannot be overridden.

Today I updated from 3.10 to 3.20. Then I get a lot of errors:

Built-in flow control command "install" cannot be overridden.
Built-in flow control command "get_target_property" cannot be overridden.
Built-in flow control command "add_custom_target" cannot be overridden.

I am overwriting these to test my own CMake functions that call these functions. This way I can check if it is called with the expected parameters.

Why are they now considered flow control commands?
How can I make CMake accept those overwrites? Or is there another way to do what I want?

PS: I could not find this change in Changelog or Policies page.

It kinda looks like unintended consequence of https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5409

Guess we need @brad.king or @kyle.edwards to confirm this.

Note that I overwrite more then the mentioned ones:

function(add_custom_command)
endfunction()
function(add_dependencies)
endfunction()

This works, for example.

Indeed, this was not supposed to happen. It was only supposed to protect a handful of commands, namely:

  • break
  • continue
  • foreach
  • function
  • if
  • macro
  • return
  • while

I will take a look.

I’ve opened https://gitlab.kitware.com/cmake/cmake/-/merge_requests/6232 to deal with this. You will once again be able to override add_custom_target and other project commands.

Note that you still won’t be able to override function, if, etc.

1 Like