Search code examples
cmakepackagesoftware-distribution

CMake: build cross-platform distribution


On my MacOSX I've developed an application that makes use of Qt and VTK libraries. I generate the makefile using CMake.

Now I want to compile an end-user self-contained package on Windows, and it is supposed to work on end-user machine without needing to pre-install Qt or VTK libraries. I think is possible to do this by modifying the CMakeLists.txt file but a web search hasn't pointed me the right direction.

How to make a distributable package for Windows using CMake?


Solution

  • What I have done in one of my own projects is write a little script which will give me the .so files or .dll files from VTK's cmake-variables and QT_LIBRARIES variables.

    After that, I add those .dll or .so files to my install targets (example scripts below) and the install target will copy those .dll or .so files from the VTK_DIR or QTDIR into ${CMAKE_INSTALL_PREFIX}\bin. This is compatible with CPack, so you could write a little cpack-script too.

    Note, however, that you need a little more on windows to get an end-user self-contained package: You will also need the "system-libraries" (msvcrt.dll, msvcrp.dll or mingwm10.dll, libstdc++.dll). E.g. take a look at this question.

    On windows, the following scripts finds all Vtk dlls from the VTK_DIR.

    file( GLOB VTK_DLLS ${VTK_RUNTIME_LIBRARY_DIRS}/*.dll )
    if( VTK_DLLS )
        foreach( Vtk_library ${VTK_DLLS} )
            # Add it to the list of 'desired' vtk-libraries for later installation
            list( APPEND Vtk_Install_Libraries ${Vtk_library} )
        endforeach( Vtk_library ${VTK_DLLS} )
        list( REMOVE_DUPLICATES Vtk_Install_Libraries )
        install( FILES ${Vtk_Install_Libraries} DESTINATION bin COMPONENT ThirdParty  )
    endif( VTK_DLLS )
    

    And for Qt the script is a little longer, because I needed to find both debug- and release libraries. The up-side: It only searches for those components I requested with find_package( Qt4 ... )

    # If Qt-4 was used, add the 'found' Qt-libraries to the Install-target.
    if ( USE_QT )
        foreach( Qt_library ${QT_LIBRARIES} )
            # With QT_USE_IMPORTED_TARGETS, we should extract the dll info 
            # from the target properties
            get_target_property( Qt_lib_name ${Qt_library} IMPORTED_LOCATION )
            get_target_property( Qt_lib_name_debug ${Qt_library} IMPORTED_LOCATION_DEBUG )
            get_target_property( Qt_lib_name_release ${Qt_library} IMPORTED_LOCATION_RELEASE )
    
            # Initially assume the release dlls should be installed, but 
            # fall back to debug if necessary
            if ( Qt_lib_name_release AND EXISTS ${Qt_lib_name_release} )
                set( Qt_library_location ${Qt_lib_name_release} )
            elseif ( Qt_lib_name_debug AND EXISTS ${Qt_lib_name_debug} AND ENVIRONMENT_DEBUG )
                set( Qt_library_location ${Qt_lib_name_debug} )
            elseif ( Qt_lib_name AND EXISTS ${Qt_lib_name} )
                set( Qt_library_location ${Qt_lib_name} )
            endif ( Qt_lib_name_release AND EXISTS ${Qt_lib_name_release} )
    
            # Extract the filename part, without the lib-prefix or the .a or ..lib suffix
            get_filename_component( Qt_library_name ${Qt_library_location} NAME_WE )
            string( REGEX REPLACE "^lib(.*)" "\\1" Qt_library_name ${Qt_library_name} )
    
            set( Qt_shared_library ${QT_BINARY_DIR}/${Qt_library_name}.dll )
            if ( EXISTS ${Qt_shared_library} )
                # Add it to the list of 'desired' qt-libraries for later installation
                list( APPEND Qt_Install_Libraries ${Qt_shared_library} )
            else ( EXISTS ${Qt_shared_library} )
                message( WARNING "    could not find ${Qt_shared_library}" )
            endif ( EXISTS ${Qt_shared_library} )
        endforeach( Qt_library ${QT_LIBRARIES} )
        # When building against a static Qt, the list of Qt_Install_Libraries can be empty
        if ( Qt_Install_Libraries )
            list( REMOVE_DUPLICATES Qt_Install_Libraries )
            install( FILES ${Qt_Install_Libraries} DESTINATION bin COMPONENT ThirdParty )
        endif ( Qt_Install_Libraries )
    endif ( USE_QT )