如何检查一个目标是否在CMake中是一个别名目标?

huangapple go评论66阅读模式
英文:

How to check whether a target is an aliased target in CMake?

问题

I understand that you want a translation of the code portion you provided. Here it is:

我正在编写一个 CMake 函数,该函数接受一个目标名称作为参数。我想要检测它是静态库类型还是共享库类型,如果是共享库,则将其复制到指定目录。

我可以正确处理通过 `add_executable()`、`add_library()` 直接提供源代码的目标。我可以使用以下方式处理它们:

if(TARGET ${targetName})
   message(STATUS "这是一个普通目标")
endif()

**还有一些导入和别名目标**,但我无法确定它们。我想使用以下方式:

if(ALIASES_TARGET ${targetName})
  message(STATUS "这是一个别名目标")
endif()

但这没有起作用。

注意:CMake 系统将创建一些名为 "garbage coding" 的目标,似乎表示分号字符 `;`,我将忽略它们。

如何确定给定的目标名称是否是别名目标?

Is there anything else you need help with?

英文:

I'm writing an CMake function which accepts a target name as an argument. I would like to detect whether it is static library type or shared library type, and if it is shared library, copy it to specified directory.

I can correctly handle targets that created via add_executable(), add_library() that directly gives source code. I can handle them with:

if(TARGET ${targetName})
   message(STATUS "this is an ordinary target")
endif()

There are imported-and-aliased-targets, but I can't determine them. I would like to use

if(ALIASES_TARGET ${targetName})
  message(STATUS "this is an aliases target")
endif()

But this didn't work.

N.B. CMake system will create some "garbage coding" named targets, seems like representing semicolon chars ;, which I will ignore.

How can I determine if an given target name is an aliased target?

function(my_fancy_function targetName dstDir)
  if(TARGET ${targetName})
     message(STATUS "this is an ordinary target")
  endif()

  if(ALIASES_TARGET ${targetName}) # how to change this line?
    message(STATUS "this is an aliases target")
  endif()
endfunction()

答案1

得分: 1

使用 ALIASED_TARGET 目标属性。根据文档:

如果这是一个别名目标,该属性包含别名的目标名称。

例如:

get_property(aliased_target TARGET "${targetName}" PROPERTY ALIASED_TARGET)
if("${aliased_target}" STREQUAL "")
  # 不是别名
else()
  # 是别名
endif()
英文:

Use the ALIASED_TARGET target property. From the docs:

> If this is an Alias Target, this property contains the name of the target aliased.

Ex.

get_property(aliased_target TARGET "${targetName}" PROPERTY ALIASED_TARGET)
if("${aliased_target}" STREQUAL "")
  # is not an alias
else()
  # is an alias
endif()

huangapple
  • 本文由 发表于 2023年6月26日 15:35:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定