[CMake] Call function from name and list, including empty elements?

Craig Scott craig.scott at crascit.com
Sat Sep 29 19:00:34 EDT 2018


On Sun, Sep 30, 2018 at 2:28 AM Jan Wielemaker <jan at swi-prolog.org> wrote:

> Hi
>
> I'm converting a big project (SWI-Prolog) to cmake. Still a newbie wrt.
> cmake, but quite happy. There is one thing that I can't get done:
> install the system using symbolic links to the source tree. This is in
> this case really handy for developers.
>

This would be very unusual. An installed project should not generally
require that the sources it was built from remain around.



>
> I've come as far as understanding:
>
>    - The generator creates cmake_install.cmake
>    - make/ninja/... install calls cmake -P cmake_install.cmake
>    - This calls file(INSTALL ....)
>
> So, I thought I can
>
>    - Add a file cmake_ln_install.cmake that
>      - redefines file()
>      - includes cmake_install.cmake
>

You will likely find it more convenient to use install(CODE)
<https://cmake.org/cmake/help/latest/command/install.html#code> or
install(SCRIPT)
<https://cmake.org/cmake/help/latest/command/install.html#script> to define
commands to add to the install process rather than trying to work with
cmake_install.cmake directly. Those two forms of the install() command are
the recommended way to get your own code into the cmake_install.cmake file
that CMake generates. At the moment, you would need to use
execute_process() to invoke cmake -E create_symlink to create the actual
link within that code or script (see further below for state of the file()
command for this).



>
> To redefine file(), I tried this to see whether this works:
>
> function(file)
>    message("Calling file(${ARGN})")
>    _file(${ARGN})
> endfunction()
>

Overriding built-in functions is strongly discouraged. Apart from relying
on undocumented CMake behavior, the above has the potential to result in
infinite recursion. I recently wrote a blog article about this which
explains in more detail:

https://crascit.com/2018/09/14/do-not-redefine-cmake-commands/




>
> But ... some of the file() calls contain empty strings ("") in the
> argument vector.  All the rest works fine.
>
> On stack overflow [1], the suggestion was to use "${ARGN}", which
> indeed keeps the empty lists and works fine if the receiver is a
> function iterating over its argument vector.  After downloading
> the cmake source it turns out file() is builtin (C++) and complains
> to require at least 2 arguments.  I tried passing the first two
> as file(${arg1} ${arg2} "${ARGN}"), which would (I think) work for
> a user function, but doesn't for a builtin.
>
> [1]
> https://stackoverflow.com/questions/52480737/pass-empty-strings-in-cmake


> So, my question is
>
>    - Is there a way to redefine a builtin function that passes
>      empty strings correctly to the original?
>    - If not, should this be considered a bug ...
>

Builtin functions should not be redefined, so no, it is not considered a
bug. ;)

I have a follow-up "part 2" article to the above one I linked, but it is
still in preparation. It discusses other problems with trying to forward
arguments using ARGN or ARGV, but some of the observations you've made here
and in the linked stackoverflow article may also be relevant. If I get
time, I'll try to see if I can incorporate some of the observations and
behaviors you've raised.



> P.s.    I'd also like to see the possibility to create symlinks
>         in cmake, other than calling cmake -E ...
>

There is already a feature request with discussion for that here:

https://gitlab.kitware.com/cmake/cmake/issues/16926


-- 
Craig Scott
Melbourne, Australia
https://crascit.com

New book released: Professional CMake: A Practical Guide
<https://crascit.com/professional-cmake/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180930/136513ca/attachment.html>


More information about the CMake mailing list