[cmake-developers] Function overrides
Wojciech Knapik
wmknapik at gmail.com
Thu May 30 16:16:47 EDT 2013
On Thu, May 30, 2013 at 08:01:16PM +0200, Wojciech Knapik wrote:
> On Thu, May 30, 2013 at 11:39:01AM -0400, Brad King wrote:
>
> > On 05/30/2013 11:14 AM, Wojciech Knapik wrote:
>
> > > PS. I know you can achieve this by providing a wrapper for the user to
> > > use instead of the standard function, but that's not nearly as nice, is
> > > it ?
Since I'm likely not getting any further with the function overriding
issue, I'm thinking of other ways to do this, but they're all bad.
Let's say I'd like to write two unrelated modules, that provide some
functionality related to add_executable(). I could just provide a
function to call in addition to add_executable(). This would require
modifying all the CMakeLists calling that function and adding the call
to the one the module provides. That's not DRY, since I could just call
one function that calls the two. So, to remove this problem, I create a
wrapper in the module, that calls add_executable() and my function.
Let's call it add_foo_executable() (provided by module foo). So I edit
all the CMakeLists and substitute occurrences of add_executbale() with
add_foo_executable(). That could be hundreds of changes, but let's say
for now this is ok. Then I include the second module bar, that also
provides a function and a wrapper. I can't use the new wrapper, because
I already use the old one. I can't fuse the wrappers together, so the
only way out is to create a third wrapper outside both of these modules,
that calls add_executable() and the two functions provided by the two
modules. That sort of works, but now I have to edit all the CMakeLists
again and I've effectively coupled my code and the two modules together.
But let's say for now, that that's not wrong in any way.
A few months pass, a few more modules get written and it all turns out
to work quite nicely, so I decide to publish them as open source, for
everyone's benefit. How do I distribute these modules and allow people
to use any combination of them ? Do I create a separate wrapper for
every combination ? Do I create a series of if(COMMAND ...) in the one
wrapper ? Where do I define that wrapper ? If I put it in each of the
modules (wrapped in if(NOT COMMAND ...)), that's copy-pasting code, if I
create a third module with just that function, the modules are no longer
standalone.
There's no good way out of this.
Or I don't see one.
Which is it ?
> > As someone reading code I like knowing that a command does what the
> > CMake documentation says it does and nothing more.
>
> I agree, that you shouldn't do this if you're changing the signature
> of the function, or any of the functionality described in the
> documentation. But if you're only using the original function as a
> "hook" to perform some additional action it seems totally acceptable
> to me. It provides a nice way to create "zeroconf" modules. And if
> things go sideways, the stack trace will make this case
> distinguishable from an error in the original function.
>
> CMake does not provide any meta-programming facility, not even the
> simplest "eval" function, so it would be nice if at least this
> mechanism worked in a more generally useful way...
Ok, so is there a way to execute a piece of CMake code for every target,
or every CMakeList parsed ?
WK
More information about the cmake-developers
mailing list