Wrapping function()

Why does this not work?

macro(myfunction FUNCTION_NAME)
   function(${FUNCTION_NAME})
endmacro()
macro(endmyfunction)
   endfunction()
endmacro()

myfunction(new_function)
endmyfunction()

This results in:

A logical block opening on the line x.cmake:39 (function)  is not closed.

Why? A macro should not check if its own “scope” is complete since it will will be replaced into the code.

(Note: I am on 3.10)

1 Like

CMake’s top-level parser still needs to be able to determine scoping which means being able to see all function, macro, if, while, and foreach commands with their corresponding closing commands before actually executing anything. I don’t see the C preprocessor macro being able to make half-blocks as a benefit so I’m happy this doesn’t work :slight_smile: . But this is really a side-effect of macros being runtime-expansions.

One problem case I don’t know how to possibly handle:

foreach (func IN …)
  myfunction("${func}")
endforeach ()

Well, I wanted use the macros for creating Mocks of CMake default functions that are not scriptable.

mock(add_executable)
   assert(5 ARGC)
   assert_called(1)
   # more manual checks
endmock()

I clould try to make mock() have a load of arguments that track every case you could check. This may be possible, but well this is how i started.

I also dont agree with you that it has no benefit in C. Here is a very simple example, that could be useful.

I’d just use function here. What is mock() gaining you here?

I was more referring to the preprocessor not allowing you to make new #if statements that it actually sees. Sure, you can stamp out whatever C you want (because it doesn’t actually care about C at all beyond the constant folding specification), but you can’t stamp out C preprocessor blocks with macros. That is what you’re asking for here.

You are right, there are probably other ways to do this with normal overwrite. The time window where I had time for this passed anyways… :frowning:

Now I see what you mean :slight_smile: Thanks