[CMake] List all binaries associated with a target.
Roman Bolshakov
roolebo at gmail.com
Sat Mar 14 16:51:15 EDT 2015
Here's an example function which addresses 1.
It creates a string of generator expressions for direct and indirect
dependencies of a target recursively.
When all recursive invocations are done, it spits <target-name>-deps
file in the CURRENT_BINARY_DIR which contains full paths of the
dependencies:
function(store_dependencies LIB)
if(NOT TARGET ${LIB})
message(FATAL_ERROR "You have to pass valid target")
endif()
# Do not add location for the top-most invocation
if (ARGV1)
set(ORIGINAL_LIB ${ARGV1})
set_property(TARGET ${ORIGINAL_LIB} APPEND_STRING PROPERTY
DEPENDENCY_LIST "$<TARGET_FILE:${LIB}> ")
else()
set(ORIGINAL_LIB ${LIB})
endif()
# init dependency list with direct dependencies
get_property(DEPENDENCIES TARGET ${LIB} PROPERTY LINK_LIBRARIES)
# We're not intersted in interface link libraries of the top-most target
if (ARGV1)
get_property(INTERFACE_LINK_LIBRARIES TARGET ${LIB} PROPERTY
INTERFACE_LINK_LIBRARIES)
list(APPEND DEPENDENCIES ${INTERFACE_LINK_LIBRARIES})
endif()
if (DEPENDENCIES)
list(REMOVE_DUPLICATES DEPENDENCIES)
# message (STATUS "${LIB} dependens on ${DEPENDENCIES}")
endif()
foreach(DEPENDENCY ${DEPENDENCIES})
store_dependencies(${DEPENDENCY} "${ORIGINAL_LIB}")
endforeach()
# Store list of gathered dependencies for the top-most call only
if (NOT ARGV1)
get_property(DEPENDENCY_LIST TARGET d PROPERTY DEPENDENCY_LIST)
file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${LIB}-deps
CONTENT "${DEPENDENCY_LIST}")
endif()
endfunction()
I'm not sure how to achieve 2. during CMake configuration phase though
as all generator expression are expanded later during build generation
phase. But you might copy the files during build phase. You can create
custom commands which use content of the generated file.
I don't get what exactly you're trying to achieve in 3. In general I
wouldn't encourage to copy binaries during build phase unless you have
a very solid reason. FWIW there's install(TARGET) command for
installation and packaging.
Roman
2015-03-14 4:24 GMT+03:00 Klaim - Joël Lamotte <mjklaim at gmail.com>:
> I am looking for a (hopefully simple) way to do the following:
> 1. gather a list of paths of all the binaries associated with a target,
> that is all the linked dll/so and executable the target link or depend
> on.
> 2. copy these somewhere else.
> 3. take into account the build modes so that the right binaries are copied
> for
> each of the target build mode.
>
> I can't find a way to do 1 so far without manually specifying the
> dependencies.
> It looks like the generators expressions can't gather these info.
> So is there a way to retrieve this list in current CMake features?
> Did I miss something?
>
> Thanks for your time.
>
>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
More information about the CMake
mailing list