Search code examples
qtcmakeqmakeprojects-and-solutionsexternal-project

How to use existing QMake project (.pro project file) as "external project" in CMake?


Is there a concise doc or example for how to use an existing QMake project with .pro project file as an "external project" in CMake? This can somewhat be done in qtcreator by marking one project as dependency of another, but it would be nice to define it more explicitly with the ExternalProject() CMake syntax.

related question: CMake: How to build external projects and include their targets


Solution

  • Something like this works. You can then edit the files from either qtcreator in main CMake project tree, OR from opening the .pro file; great for iterating quickly on QT widgets in SomeGarbageApplication that is part of large cmake build tree.

    macro(DeclareProjectFiles  Tag  Filez)
    ######### Trick: use this syntax to make arbitrary files
    #########             appear in IDE project. #######################
    ### Note: pass in the raw name of a list variable,
    ###   since it will get expanded here in this macro.
      add_custom_target(${Tag}_files  ALL
                        pwd
                        COMMAND ls -ltrh
                        COMMENT  " ${Tag} files thunk... got list: [ ${${Filez}} ]"
                        VERBATIM
                        SOURCES  ${${Filez}}
                       )    
    endmacro()
    
        message(STATUS "QT_QMAKE_EXE is:  ${QT_QMAKE_EXECUTABLE}")
    
    set(Z  SomeGarbageApplication)
    file(GLOB  ${Z}_Files
                ./*.cpp
                ./*.h
                ./*.ui
                ./*.pro
                ./*.png
                ./*.jpg)
    
    DeclareProjectFiles( ${Z}_grbg  ${Z}_Files )
    
    add_custom_target(${Z}_pro  ALL)
    
    set(ExtraQMakeArgs  -r -spec linux-g++ CONFIG+=release)
    
    # note: use killall because this can/will fail if the exe is running
    #       But, need || true to not fail build when it's not running. 
    
    add_custom_command(TARGET  ${Z}_pro
                       COMMAND  killall
                       ARGS     -q -9 -v ${Z} || true
                       COMMAND  ${QT_QMAKE_EXECUTABLE}
                       ARGS     -query
                       COMMAND  ${QT_QMAKE_EXECUTABLE}
                       ARGS     ${CMAKE_CURRENT_SOURCE_DIR}/${Z}.pro ${ExtraQMakeArgs}
                       COMMAND  make  ${Z}
                       ARGS     -j4
                       COMMAND  cp
                       ARGS     ${Z} ${CMAKE_CURRENT_SOURCE_DIR}/${${Z}_config}  ${CMAKE_BINARY_DIR}/bin/
                       WORKING_DIRECTORY  ${CMAKE_CURRENT_BINARY_DIR}
                       VERBATIM
                       )    
    #################################################################