[CMake] DLL handling under CMake

Hendrik Sattler post at hendrik-sattler.de
Thu May 11 09:32:00 EDT 2017


Hi,

sorry for the delay. I see you already found a solution yourself.
Still, I want to share mine:
---------------------CopyPrerequisites.cmake--------------------
include ( GetPrerequisites )

if ( NOT DEFINED PREREQ_FILE )
   message ( FATAL_ERROR "PREREQ_FILE must be defined(${PREREQ_FILE})" )
endif ()

#
# Satisfy the find_file() search by common subdirs where .dll file can  
be found
#
foreach ( dir ${CMAKE_PREFIX_PATH} )
   list ( APPEND dirs ${dir}/bin )
endforeach ( dir )

get_filename_component ( COPY_DESTINATION "${PREREQ_FILE}" PATH )
get_prerequisites ( "${PREREQ_FILE}" COPY_FILES 1 1 "${dirs}"  
"${COPY_DESTINATION}" )

foreach ( COPY_FILE ${COPY_FILES} )
   if ( NOT IS_ABSOLUTE ${COPY_FILE} )
     set ( COPY_FILE_FULL )
     gp_resolve_item ( "${PREREQ_FILE}" "${COPY_FILE}" "${dirs}"  
"${COPY_DESTINATION}" COPY_FILE_FULL )
   else ()
     set ( COPY_FILE_FULL ${COPY_FILE} )
   endif ()
   if ( COPY_FILE_FULL )
     get_filename_component ( COPY_SOURCE_DIR "${COPY_FILE_FULL}" PATH )
     if ( NOT COPY_SOURCE_DIR MATCHES "^${COPY_DESTINATION}$" )
       message ( "Copying ${COPY_FILE_FULL} to output directory" )
       execute_process ( COMMAND "${CMAKE_COMMAND}" -E  
copy_if_different "${COPY_FILE_FULL}" "${COPY_DESTINATION}" )
     endif ()
   else ()
     message ( "Failed to find  ${COPY_FILE}" )
   endif ()
endforeach ( COPY_FILE )
----------------------------------------------------------

This can now be called from CMakeLists.txt:
if ( CMAKE_RUNTIME_OUTPUT_DIRECTORY )
   add_custom_command ( TARGET Gui POST_BUILD
     COMMAND ${CMAKE_COMMAND}
             "-DCMAKE_PREFIX_PATH:PATH=${SOME_SEARCH_DIRS}"
             "-DPREREQ_FILE=$<TARGET_FILE:MyTarget>"
             -P ${CMAKE_SOURCE_DIR}/CMakeModules/CopyPrerequisites.cmake
     VERBATIM
   )
endif ()

The variable SOME_SERACH_DIRS is filled with e.g. IMPORTED_LOCATION  
property values.
The same variable is used when calling INSTALL(CODE ".....   
fixup_bundle(...)" )

Regards,

HS

Zitat von lectem at gmail.com:

> I managed to get it working by using an intermediate script.
> One might want to generate the script instead of using the  
> « RUN_IT » variable trick.
> This was only tested on Windows, but seems to work fine.
> Put the following code in a xxxxxx.cmake file, include it from your  
> CMakeLists.txt and enjoy.
>
>
> # This is a helper script to run BundleUtilities fixup_bundle as postbuild
> # for a target. The primary use case is to copy .DLLs to the build  
> directory for
> # the Windows platform. It allows generator expressions to be used  
> to determine
> # the binary location
> #
> # Usage : run_fixup(TARGET LIBS DIRS)
> # - TARGET : A cmake target
> # - See fixup_bundle for LIBS and DIRS arguments
>
> if(RUN_IT)
> # Script ran by the add_custom_command
> 	include(BundleUtilities)
> 	fixup_bundle("${TO_FIXUP_FILE}" "${TO_FIXUP_LIBS}" "${TO_FIXUP_DIRS}")
> # End of script ran by the add_custom_command
> else()
>
> set(THIS_FILE ${CMAKE_CURRENT_LIST_FILE})
> message(${THIS_FILE})
> function(run_fixup _target _libs _dirs)
> 	message(${THIS_FILE})
> 	add_custom_command(
> 		TARGET ${_target} POST_BUILD
> 		COMMAND ${CMAKE_COMMAND} -DRUN_IT:BOOL=ON  
> -DTO_FIXUP_FILE=$<TARGET_FILE:${_target}> -DTO_FIXUP_LIBS=${_libs}  
> -DTO_FIXUP_DIRS=${_dirs}  -P ${THIS_FILE}
> 		COMMENT "Fixing up dependencies for ${_target}"
> 		VERBATIM
> 	)
>
> endfunction()
>
> endif()
>
>
> De : Clément Gregoire
> Envoyé le :jeudi 4 mai 2017 08:37
> À : Hendrik Sattler; Louis-Paul CORDIER; Cmake Mailing List
> Objet :Re: [CMake] DLL handling under CMake
>
> I'd also be interested in this. I saw an old mail in the ML about  
> this, but it seems fixup_bundle is old and cant use generator  
> expressions, making it hard to use (I don't want to hardcode the  
> executable path).
>
> Do you have a sample for this ?
> CMake would really benefit from having those features made more  
> accessible instead of everyone having to write its own script 
> Le sam. 29 avr. 2017 22:10, Hendrik Sattler  
> <post at hendrik-sattler.de> a écrit :
>
>
> Am 27. April 2017 10:43:50 MESZ schrieb Louis-Paul CORDIER  
> <lp.cordier at dynamixyz.com>:
>> This steps are tedious and I'm wondering if there is a mechanism that
>> exists or that have to be imagined to make the DLL nightmare end.
>
> I use BundleUtilities to achieve the copying of DLL files to the  
> installation directory. The main problem for this is to enumerate  
> the needed directories.
>
> I use the same for copying DLL files to the output directory to ease  
> debugging.
>
> The advantage is the inspection of the exe for really needed DLL  
> files. This AUTOMATICALLY handles the case debug vs. release.
>
> HS
>
> --
> Diese Nachricht wurde von meinem Android-Mobiltelefon mit K-9 Mail gesendet.
> --
>
> 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