CMake use Get_target_property to determine if the target exists

Source: Internet
Author: User

After using CMake's add_custom_target to establish a custom target, the target must be added to all dependencies, otherwise the generated makefile will not execute the target content. This will have a problem if you need to compile the following directory:

There are n directories under the directory, these directories or directories, or files, then recursively, there will be a number of CMakeLists.txt, that is, there will be several add_custom_target (all ...) The existence of. It may not matter to go into a separate directory compilation, but if we execute cmake generation makefile on the top or middle tier, we will report duplicate target errors, and this duplicate target is the one that was added multiple times. The error is as follows:

Add_custom_target cannot create target "all" because
  another target with the same name already exists.  The existing target is a
  custom target created in source directory see
documentation for
policy CMP0002 for More details.

Can only be compiled on the leaf and not in the upper-middle, so obviously not. A viable solution is to determine if the target ' exists, if it does not exist, to add a dependency using add_custom_target before adding the target to all (the problem with Add_custom_target is that It does not itself determine whether the target exists, is new each time, and if so, adds a dependency using add_dependencies (add_dependencies only adds dependencies and does not create a new target). Here, use Get_target_property to determine whether the target exists or not, and the prototype is as follows:

Get_target_property (VAR target property)
The property and Var here are equivalent to a map key value pair, the property is the key, Var is the value, the key value pair from the target, if the target does not have this key, then Var will return Output_value-notfound.

A get must have set, and the method to add a key value to a target is Set_property, the prototype is as follows:

Set_property (<global                            |
              DIRECTORY [dir]                   |
              TARGET    [Target1 [Target2 ...] |
              SOURCE    [SRC1 [Src2 ...]       |
              TEST      [Test1 [test2 ...]     |
              CACHE     [Entry1 [Entry2 ...] >
             [APPEND] [append_string] Property
             <name> [value1 [value2 ...]]
There are more functions here, we only need to set target to the specified target name, the property is set to the desired key value pair. So, to determine whether a target exists, you can write this:

Get_target_property (Output_value all STATUS)
if (${output_value} strequal output_value-notfound)
	#Target All does not exist
else ()
	#Target all exists
endif ()
On this basis, wrap a custom function append_dependencies, which adds a dependency to all, uses Add_custom_target to create a new target all and increases dependency, and adds a key-value pair message to the target in case all does not exist , if all exists, use add_dependencies to add dependencies and the code is as follows:

function (append_dependencies)
set (Multivalueargs dependencies)
cmake_parse_arguments (append_ DEPENDENCIES "" "" "${multivalueargs}" ${argn})
get_target_property (output_value all STATUS)
if ($ {Output_value}        Strequal output_value-notfound)
add_custom_target (all DEPENDS ${append_dependencies_dependencies})
Set_property (TARGET all property STATUS AVAILABLE)
else ()
add_dependencies (All DEPENDS ${append_depen Dencies_dependencies})
endif ()
endfunction () <pre name= "code" class= "plain" >

Use Append_dependencies to add dependencies to all, which effectively resolves the target conflict in the pelagic directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.