Need suggestions for implementation of new feature in cmake.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Need suggestions for implementation of new feature in cmake.

Carlo Wood
Hello!

I'm writing a patch for cmake to address
https://gitlab.kitware.com/cmake/cmake/issues/19703

but I need a little help; I'm only familiar with
GNU/Linux and single target generator Makefile.

In terms of a Makefile, if one uses ExternalProject_Add_Step, see

https://cmake.org/cmake/help/latest/module/ExternalProject.html#explicit-step-management

then something like this is generated:

    /path/to/stamp_dir/myname-mystep: /path/to/some/dependency
            @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) \
                --blue --bold \
                --progress-dir=/path/to/buildir/CMakeFiles \
                --progress-num=$(CMAKE_PROGRESS_9) "My Comment"
            cd /path/to/workingdir && \
                my_COMMAND my_arg1 my_arg2 ... \
            cd /path/to/workingdir && \
                cmake -E touch /path/to/stamp_dir/myname-mystep

Now, I'm not sure about the portability of the COMMAND that
is passed to ExternalProject_Add_Step. Are we assuming that
every operating system supports a POSIX (unix) shell, like bash?

Otherwise it seems that the only way to be portable is to run
cmake in script mode there.

Note how the last command does this: it runs cmake in order to portably
'touch' the target file (a stamp file).

What I need is way to generate a rule where depending on a
condition (to be decided by the COMMAND (my_COMMAND)) that
touch is or is not done.

In the single target target POSIX Makefile case this could look like
this (I'm leaving out the COMMENT here for brevity),

     /path/to/stamp_dir/myname-mystep: /path/to/some/dependency
            (cd /path/to/workingdir && \
                if my_COMMAND my_arg1 my_arg2 ...; then \
                  cmake -E touch /path/to/stamp_dir/myname-mystep; \
                fi)

which uses the return value of my_COMMAND to decide whether or
not to touch the stamp file.

However, this is pure shell coding: using a '(' to open a sub shell
and then using 'if ...; then ... fi'.

I'm looking for ideas on how to implement this in a portable way.

How can I extract a boolean from a user defined COMMAND and then
use that to conditionally execute the touch?

--
Carlo Wood <[hidden email]>
--

Powered by kitware.com/cmake

Kitware offers various services to support the CMake community. For more information on each offering, please visit https://cmake.org/services

Visit other Kitware open-source projects at https://www.kitware.com/platforms

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

This mailing list is deprecated in favor of https://discourse.cmake.org
Reply | Threaded
Open this post in threaded view
|

Re: Need suggestions for implementation of new feature in cmake.

Hendrik Sattler
Hi Carlo,

The answer is already right in front of you: "a && b" executes b only if a returns positive (zero exit code). This is already done for the mkdir.

And to answer the other question: no, you cannot assume a POSIX shell in a Makefile.

HS


Am 27. März 2020 05:53:39 MEZ schrieb Carlo Wood <[hidden email]>:

>Hello!
>
>I'm writing a patch for cmake to address
>https://gitlab.kitware.com/cmake/cmake/issues/19703
>
>but I need a little help; I'm only familiar with
>GNU/Linux and single target generator Makefile.
>
>In terms of a Makefile, if one uses ExternalProject_Add_Step, see
>
>https://cmake.org/cmake/help/latest/module/ExternalProject.html#explicit-step-management
>
>then something like this is generated:
>
>    /path/to/stamp_dir/myname-mystep: /path/to/some/dependency
>            @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) \
>                --blue --bold \
>                --progress-dir=/path/to/buildir/CMakeFiles \
>                --progress-num=$(CMAKE_PROGRESS_9) "My Comment"
>            cd /path/to/workingdir && \
>                my_COMMAND my_arg1 my_arg2 ... \
>            cd /path/to/workingdir && \
>                cmake -E touch /path/to/stamp_dir/myname-mystep
>
>Now, I'm not sure about the portability of the COMMAND that
>is passed to ExternalProject_Add_Step. Are we assuming that
>every operating system supports a POSIX (unix) shell, like bash?
>
>Otherwise it seems that the only way to be portable is to run
>cmake in script mode there.
>
>Note how the last command does this: it runs cmake in order to portably
>'touch' the target file (a stamp file).
>
>What I need is way to generate a rule where depending on a
>condition (to be decided by the COMMAND (my_COMMAND)) that
>touch is or is not done.
>
>In the single target target POSIX Makefile case this could look like
>this (I'm leaving out the COMMENT here for brevity),
>
>     /path/to/stamp_dir/myname-mystep: /path/to/some/dependency
>            (cd /path/to/workingdir && \
>                if my_COMMAND my_arg1 my_arg2 ...; then \
>                  cmake -E touch /path/to/stamp_dir/myname-mystep; \
>                fi)
>
>which uses the return value of my_COMMAND to decide whether or
>not to touch the stamp file.
>
>However, this is pure shell coding: using a '(' to open a sub shell
>and then using 'if ...; then ... fi'.
>
>I'm looking for ideas on how to implement this in a portable way.
>
>How can I extract a boolean from a user defined COMMAND and then
>use that to conditionally execute the touch?
--

Powered by kitware.com/cmake

Kitware offers various services to support the CMake community. For more information on each offering, please visit https://cmake.org/services

Visit other Kitware open-source projects at https://www.kitware.com/platforms

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

This mailing list is deprecated in favor of https://discourse.cmake.org
Reply | Threaded
Open this post in threaded view
|

Re: Need suggestions for implementation of new feature in cmake.

Carlo Wood
On Fri, 27 Mar 2020 07:18:11 +0100
Hendrik Sattler <[hidden email]> wrote:

> Hi Carlo,
>
> The answer is already right in front of you: "a && b" executes b only
> if a returns positive (zero exit code). This is already done for the
> mkdir.
>
> And to answer the other question: no, you cannot assume a POSIX shell
> in a Makefile.
>
> HS

Hi Hendrik,

thank you for your reply :). I kinda overlooked that I have to admit.
I think my brain saw the '&&' as shell scripting and the question
was if that is portable, or that the generated Makefile on my system
made use of that because of the platform that I am using.

So, for clarity, I can use:

    add_custom_command(
      ...
      COMMAND ${cmd} && ${CMAKE_COMMAND} -E touch ${stamp_file}
    )

Where ${cmd} is the user defined command,
and that will work portably on all supported platforms?

How should I do this for GENERATOR_IS_MULTI_CONFIG?

Carlo Wood

PS I assume that '||' also works then. Namely I also need the
   stamp file to be created when it doesn't exist.

   So really I need in addition

   COMMAND test -e ${stamp_file} || ${CMAKE_COMMAND} -E touch ${stamp_file}

Is 'test -e' also portable?

--

Powered by kitware.com/cmake

Kitware offers various services to support the CMake community. For more information on each offering, please visit https://cmake.org/services

Visit other Kitware open-source projects at https://www.kitware.com/platforms

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

This mailing list is deprecated in favor of https://discourse.cmake.org