Use get_target_property to determine whether the target exists

Source: Internet
Author: User

After you use add_custom_target of cmake to create a custom target, you must add this target to the all dependency. Otherwise, the generated makefile will not execute the target content. This may cause a problem. If you need to compile the following directory:

Exist, that is, there will be several add_custom_target (all. It may not matter if you enter a separate directory for compilation, but if we execute cmake to generate the makefile on the top or middle layer, the system will report the error of repeating the target. The duplicate target is the all 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 directorySee documentation  for policy CMP0002 for more details.

It can only be compiled on the leaves, but not on the upper layer. This is obviously not the case. A feasible solution is to determine whether the target exists before adding a target to all. If no target exists, use add_custom_target to add the dependency (The problem with add_custom_target is that, it does not determine whether the target exists. It is created every time.) If it exists, use add_dependencies to add dependencies (add_dependencies only adds dependencies, rather than creating targets ). Here, get_target_property is used to determine whether a target exists. 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, and the VaR is the value. This key-Value Pair belongs to the target. If this target does not have this key, vaR returns the OUTPUT_VALUE-NOTFOUND.

If get is available, set is required. The method for adding 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 many functions here. We only need to set target to the specified target name and property to the required key-value pair. In this way, you can write this statement to determine whether a target exists:

Get_target_property (output_value all status) if ($ {output_value} strequal OUTPUT_VALUE-NOTFOUND) # target all does not exist else () # target all has endif ()
Then, wrap a custom function append_dependencies to add dependencies to all. If all does not exist, use add_custom_target to create a target all and add dependencies, add a key-Value Pair message to target. If all exists, use add_dependencies to add dependencies. 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_DEPENDENCIES_DEPENDENCIES})    endif()    endfunction()<pre name="code" class="plain">

Use append_dependencies to add dependencies to all. This effectively resolves the target conflict in the upper-level 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.