[43998] How to implement different CMake functions with the same name?

You can’t, CMake only has one scope for functions, and no overloading.

If you need a customisation point for a shared piece of CMake code (as in your case), you must make it a variable, not a function. For a simple case like your example, this could work:

  • common.cmake:
    set(MACRO_EXTENSION ${INJECTED_MACRO_EXTENSION}) # or just use INJECTED_MACRO_EXTENSION directly
    
  • cpp/CMakeLists.txt:
    set(INJECTED_MACRO_EXTENSION C)
    include(common.cmake)
    

For something more complicated, you can inject the name of a command to call:

  • common.cmake:
    cmake_language(CALL ${INJECTED_COMMAND_NAME})
    
  • cpp/CMakeListstxt
    function(cpp_func)
      # whatever
    endfunction()
    set(INJECTED_COMMAND_NAME cpp_func)
    include(common.cmake)
    
  • python/CMakeListstxt
    function(py_func)
      # whatever
    endfunction()
    set(INJECTED_COMMAND_NAME py_func)
    include(common.cmake)
    
1 Like