$<TARGET_BUNDLE_DIR:tgt> generator equivalent for cmake < 3.9

两盒软妹~` 提交于 2019-12-11 08:49:02

问题


Since cmake 3.9 the following generator expression has been introduced:

$<TARGET_BUNDLE_DIR:tgt>

For which the documentation states that:

Full path to the bundle directory (my.app, my.framework, or my.bundle) where tgt is the name of a target.

How can one obtain the same result (path to the bundle directory) if using cmake < 3.9?


I tried the following:

include(BundleUtilities)
get_dotapp_dir($<TARGET_FILE:my_target> DOTAPP_DIR)

Unfortunately it doesn't work. The documentation for get_dotapp_dir says:

Returns the nearest parent dir whose name ends with ”.app” given the full path to an executable. If there is no such parent dir, then simply return the dir containing the executable.

And the dir containing the executable is exactly what I'm getting out of it, even if a parent .app dir actually exists.


回答1:


Unfortunately, $<TARGET_FILE:my_target> is a generator expression. According to the documentation, it is evaluated at build time (not at CMake generation time). See the related doc (emphasis is mine):

Generator expressions are evaluated during build system generation to produce information specific to each build configuration.

Generator expressions are allowed in the context of many target properties, such as LINK_LIBRARIES, INCLUDE_DIRECTORIES, COMPILE_DEFINITIONS and others. They may also be used when using commands to populate those properties, such as target_link_libraries(), target_include_directories(), target_compile_definitions() and others.

In other words, you cannot use $<TARGET_FILE:my_target> as argument to get_dotapp_dir. You have to pass a variable containing the full path of your executable.

Since CMake 3, the full path of generated target is impossible to retrieve without this generator expression. See CMP0026 for more info.

So as long as you keep this Policy set to its default value, you will not be able to compute the full path to your executable or the parent bundle.

You are not the first trying to solve this issue. But depending on "what to do with this bundle path", you may try the following solutions:

  1. Set CMP0026 to OLD, and use LOCATION property to get the path to your executable, and the give this path to get_dotapp_dir to retrieve the corresponding bundle path. This solution is definitely not portable, may stop to work in the future, and is not advised...
  2. If you need to access to your bundle path from a custom command or a custom target (at build time), you may use a script (python, php, bash, perl, etc.) to compute the path to bundle from the path to executable.

Currently, we use something like this in some project:

add_custom_command(TARGET MyTarget POST_BUILD
    COMMAND ${PYTHON_EXECUTABLE} -u MakeRelease.py $<TARGET_FILE:MyTarget>
)

Unfortunately, there is no clean way to retrieve the bundle path at configuration time at the moment...



来源:https://stackoverflow.com/questions/45432489/target-bundle-dirtgt-generator-equivalent-for-cmake-3-9

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!