[CMake] CMake: how to use 'if condition' in command add_custom_command(...)

Clark Wang dearvoid at gmail.com
Tue Apr 19 05:44:02 EDT 2016


On Tue, Apr 19, 2016 at 4:55 PM, Chaos Zhang <zcsd2012 at gmail.com> wrote:

> I have read the description of PRE_BUILD before in CMake doc, but when i
> use it, i found something confuesed.
> (1) In CMakeLists.txt, the content as below:
>
> ​(2)After i make the makefile generated by this CMakeLists.txt, i get
> result as below:
> "3_pre_build" is behind "1_pre_link", if POST_BUILD gets treated as
> PRE_LINK, why "3_pre_build" was printed before "1_pre_link"?
>

It's not the order of the "add_custom_command" commands in CMakeLists.txt
which determines in which order they will be invoked. According to the doc:

  PRE_BUILD − run before all other dependencies
  PRE_LINK − run after other dependencies
  POST_BUILD − run after the target has been built

So PRE_BUILD comes first and then PRE_LINK and at last POST_BUILD.

-clark

>
> Thanks for you reply,
> Chaos Zhang
>>
> 2016-04-19 15:50 GMT+08:00 Craig Scott-3 [via CMake] <[hidden email]
> <http:///user/SendEmail.jtp?type=node&node=7593283&i=0>>:
>
>> In case it matters, in Clark's suggested code, note that PRE_BUILD is
>> only fully honoured with Visual Studio generators. For everything else, it
>> gets treated as PRE_LINK, which occurs later (i.e. after compiling all the
>> sources rather than before). So it depends on what your "if" test is trying
>> to do whether or not this matters in your case.
>>
>> On Tue, Apr 19, 2016 at 5:24 PM, Clark Wang <[hidden email]
>> <http:///user/SendEmail.jtp?type=node&node=7593281&i=0>> wrote:
>>
>> On Tue, Apr 19, 2016 at 1:15 PM, Chaos Zhang <[hidden email]
>>> <http:///user/SendEmail.jtp?type=node&node=7593281&i=1>> wrote:
>>>
>>>> Hi,
>>>>
>>>> Is there a way to use Linux command 'if' like this :
>>>> <http://cmake.3232098.n2.nabble.com/file/n7593278/2016-04-19_113005.png
>>>> >
>>>> For i need to execute 'if condition' in make phase rather than cmake
>>>> phase.
>>>>
>>>
>>> Try like this:
>>>
>>> % cat CMakeLists.txt
>>> add_custom_target(foo ALL)
>>> add_custom_command(TARGET foo
>>>     PRE_BUILD
>>>     COMMAND if [ -f bar ]\; then echo yes\; else echo no\; fi)
>>> % cmake .
>>> % make
>>> no
>>> Built target foo
>>> % touch bar
>>> % make
>>> yes
>>> Built target foo
>>> %
>>>
>>>
>>>> Thanks a lot
>>>> Chaos Zhang
>>>>
>>>>
>>>>
>>>> --
>>>> View this message in context:
>>>> http://cmake.3232098.n2.nabble.com/CMake-how-to-use-if-condition-in-command-add-custom-command-tp7593278.html
>>>> Sent from the CMake mailing list archive at Nabble.com.
>>>> --
>>>>
>>>> Powered by www.kitware.com
>>>>
>>>> Please keep messages on-topic and check the CMake FAQ at:
>>>> http://www.cmake.org/Wiki/CMake_FAQ
>>>>
>>>> Kitware offers various services to support the CMake community. For
>>>> more information on each offering, please visit:
>>>>
>>>> CMake Support: http://cmake.org/cmake/help/support.html
>>>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>>>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>>>
>>>> Visit other Kitware open-source projects at
>>>> http://www.kitware.com/opensource/opensource.html
>>>>
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://public.kitware.com/mailman/listinfo/cmake
>>>>
>>>
>>>
>>> --
>>>
>>> Powered by www.kitware.com
>>>
>>> Please keep messages on-topic and check the CMake FAQ at:
>>> http://www.cmake.org/Wiki/CMake_FAQ
>>>
>>> Kitware offers various services to support the CMake community. For more
>>> information on each offering, please visit:
>>>
>>> CMake Support: http://cmake.org/cmake/help/support.html
>>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://public.kitware.com/mailman/listinfo/cmake
>>>
>>
>>
>>
>> --
>> Craig Scott
>> Melbourne, Australia
>> http://crascit.com
>>
>> --
>>
>> Powered by www.kitware.com
>>
>> Please keep messages on-topic and check the CMake FAQ at:
>> http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Kitware offers various services to support the CMake community. For more
>> information on each offering, please visit:
>>
>> CMake Support: http://cmake.org/cmake/help/support.html
>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/cmake
>>
>> ------------------------------
>> If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://cmake.3232098.n2.nabble.com/CMake-how-to-use-if-condition-in-command-add-custom-command-tp7593278p7593281.html
>> To unsubscribe from CMake: how to use 'if condition' in command
>> add_custom_command(...), click here.
>> NAML
>> <http://cmake.3232098.n2.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>
> ------------------------------
> View this message in context: Re: CMake: how to use 'if condition' in
> command add_custom_command(...)
> <http://cmake.3232098.n2.nabble.com/CMake-how-to-use-if-condition-in-command-add-custom-command-tp7593278p7593283.html>
>
> Sent from the CMake mailing list archive
> <http://cmake.3232098.n2.nabble.com/> at Nabble.com.
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160419/8ccbf207/attachment-0001.html>


More information about the CMake mailing list