Get runtime library of my target

Hi,

I’m creating a library with add_library(h5geo SHARED ${src_files_h5geo} ${include_files_h5geo}).
Then I need to get path to h5geo runtime library (for now I’m on Windows so I would like to get path to h5geo.dll)

I’m trying to do that with commands:

add_library(h5geo SHARED ${src_files_h5geo} ${include_files_h5geo})

get_target_property(rname h5geo RUNTIME_OUTPUT_NAME)
get_target_property(rdir h5geo RUNTIME_OUTPUT_DIRECTORY)
message("rname: ${rname}")  # rname: rname-NOTFOUND
message("rdir: ${rdir}")  # rdir: rdir-NOTFOUND

but this prints NOTFOUND.

What is the correct way to do this?

During configuration step, these properties are input properties. So only usable if you have already set some value.

The final information is only available during generation step. For that purpose, you should use generator expressions like $<TARGET_PROPERTY:h5geo,RUNTIME_OUTPUT_NAME>.

1 Like

Thank you for answer

Can’t understand how exactly should I use.

For example can I use it during install step? I have:

install(CODE [[
  file(GET_RUNTIME_DEPENDENCIES
    LIBRARIES $<TARGET_FILE:_h5geo>
    RESOLVED_DEPENDENCIES_VAR _r_deps
    UNRESOLVED_DEPENDENCIES_VAR _u_deps
    CONFLICTING_DEPENDENCIES_PREFIX conflict
    DIRECTORIES $<TARGET_PROPERTY:h5geo,RUNTIME_OUTPUT_DIRECTORY>
    PRE_INCLUDE_REGEXES "^.*([Hh][Dd][Ff][5]|[Hh][5][Gg][Ee][Oo]).*$"
    PRE_EXCLUDE_REGEXES "[^\n]+"
  )
  foreach(_file ${_r_deps})
    message(STATUS "Dependency R: ${_file}")
  endforeach()

  foreach(_file ${_u_deps})
    message(STATUS "Dependency U: ${_file}")
  endforeach()

  foreach(_file ${conflict_FILENAMES})
    message(STATUS "Conflict: ${_file}")
  endforeach()

  message("RUNTIME_OUTPUT_DIRECTORY: $<TARGET_PROPERTY:h5geo,RUNTIME_OUTPUT_DIRECTORY>")    # shows empty
]])

As you can see I’m trying to add $<TARGET_PROPERTY:h5geo,RUNTIME_OUTPUT_DIRECTORY> runtime dir to add directory to search path.

Generator expressions are not usable everywhere, only when explicitly specified in the documentation of the commands. For example, install(CODE) does not support generator expressions.

In your case, the most simple way to achieve what you want is to generate a script using file(GENERATE) command and execute it using install(SCRIPT) command.

1 Like

As of CMake 3.14, install(CODE) supports generator expressions. See policy CMP0087.

1 Like

Thank you for hint
Now I set cmake_policy(SET CMP0087 NEW) but I get empty when trying to cmake install the code below:

cmake_policy(SET CMP0087 NEW)

install(CODE "message(\"RUNTIME_OUTPUT_DIRECTORY: $<TARGET_PROPERTY:h5geo,RUNTIME_OUTPUT_DIRECTORY>\")")

Is it necessary to set RUNTIME_OUTPUT_DIRECTORY target property before reading it? Or there is a default path (MSVC stores binaries at Release/Config folder inside buinding tree for example) that I can get using generator expression?

@kyle.edwards You are right, I read the documentation too quickly.

1 Like

Cna’t understand. install(CODE "message(\"TARGET_FILE: $<TARGET_FILE:h5geo>\")") shows my library.
But if I put the same code to the script and call it install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/h5geopy-install.cmake) then it will literally print TARGET_FILE: $<TARGET_FILE:h5geo>

install(SCRIPT) does not evaluate generator expressions contained in the SCRIPT itself, only the filename passed to install(SCRIPT):

install(SCRIPT $<TARGET_FILE_DIR:tgt>/script.cmake)

If you want the contents of the script file to contain generator expressions, you have to use file(GENERATE) first as per @marc.chevrier’s suggestion.

1 Like

With your help I could solve my problem

  1. I get runtime with command $<TARGET_FILE:h5geo>
  2. generator expression is used inside file(GENERATE ...):
configure_file(${CMAKE_SOURCE_DIR}/cmake/h5geopy-install.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/h5geopy-install.cmake
  @ONLY
  )

# $<CONFIG> at the end of filename is necessary for multiconfig generators
# such as MSVC or Ninja multiconfig (gives error without it:
# "Evaluation file to be written multiple times with different content."
file(GENERATE
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/h5geopy-install-$<CONFIG>.cmake
  INPUT ${CMAKE_CURRENT_BINARY_DIR}/h5geopy-install.cmake
  )

So for the first time I simply configure file and set marked with @ variables and after that I use file(GENERATE ...) where my $<TARGET_FILE:h5geo> transforms to path.

Thank you a lot!