如何使用CMake正确获取Qt?

62lalag4  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(194)

我面临以下问题:我尝试使用CMake函数FetchContent_Declare来获取Qt C++库,特别是版本6.4.2。我正在遵循这个网站的指南,其中解释了如何从源代码构建Qt,如果在没有CMake的情况下“手工”执行,它会工作得很好;但是如果我想将它与FetchContent_Declare合并,从编译之前开始就变得困难了,正如你可以从链接中看到的,我需要运行初始化命令:

perl init-repository

和配置一(构建前):

mkdir qt6-build
cd qt6-build
../qt6/configure

如何将这两个步骤合并到CMake的获取内容部分?
我现在的CMake脚本是:

include( FetchContent )

FetchContent_Declare(
  Qt6
  GIT_REPOSITORY "https://github.com/qt/qt5"
  GIT_TAG v6.4.2
  CONFIGURE_COMMAND "perl init-repository && ./configure"
)

FetchContent_MakeAvailable( Qt6 )

但它似乎不正确工作:

CMake Error at /usr/share/cmake-3.22/Modules/FetchContent.cmake:1087 (message):
  Build step for qt6 failed: 2
Call Stack (most recent call first):
  /usr/share/cmake-3.22/Modules/FetchContent.cmake:1216:EVAL:2 (__FetchContent_directPopulate)
  /usr/share/cmake-3.22/Modules/FetchContent.cmake:1216 (cmake_language)
  /usr/share/cmake-3.22/Modules/FetchContent.cmake:1259 (FetchContent_Populate)
  deps/qt6/CMakeLists.txt:19 (FetchContent_MakeAvailable)

我还需要一种方法,如果可能的话,在通过FetchContent_Declare配置和安装库时不询问GitHube用户名和密码。

dnph8jn4

dnph8jn41#

我终于找到解决办法了!这个CMakeLists.txt完美地用于这个目的:

# Project settings
cmake_minimum_required( VERSION 3.15 )

project( key-manager-dependencies-qt6
    VERSION 1.0
    DESCRIPTION "Build system for key-manager Qt6 dependency."
    LANGUAGES CXX
)

# Error if building out of a build directory
file( TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" LOC_PATH )
if( EXISTS "${LOC_PATH}" )
    message( FATAL_ERROR "You cannot build in a source directory (or any directory with "
                         "CMakeLists.txt file). Please make a build subdirectory. Feel free to "
                         "remove CMakeCache.txt and CMakeFiles." )
endif()

# Fetch the package
find_package( Qt6 6.4.2 QUIET )

if( NOT Qt6_FOUND )

    # Set Ninja for compilation
    set( CMAKE_GENERATOR "Ninja" CACHE STRING "CMake generator" FORCE )

    # Initial message
    message( STATUS "Qt 6.4.2 not found. Downloading and configuring Qt..." )

    # Download and extract archive of Qt 6.4.2
    set( QT_VERSION "6.4.2" )
    set( QT_ARCHIVE_URL "https://download.qt.io/official_releases/qt/6.4/${QT_VERSION}/submodules/qtbase-everywhere-src-${QT_VERSION}.tar.xz" )
    set( QT_ARCHIVE_FILE "${CMAKE_CURRENT_BINARY_DIR}/qtbase-everywhere-src-${QT_VERSION}.tar.xz" )
    set( QT_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/qtbase-everywhere-src-${QT_VERSION}" )
    message( STATUS "Downloading the package from the official repository..." )
    file( DOWNLOAD ${QT_ARCHIVE_URL} ${QT_ARCHIVE_FILE}
          SHOW_PROGRESS
          EXPECTED_HASH SHA256=a88bc6cedbb34878a49a622baa79cace78cfbad4f95fdbd3656ddb21c705525d )
    message( STATUS "Unpacking the release..." )
    execute_process( COMMAND ${CMAKE_COMMAND} -E tar xvf ${QT_ARCHIVE_FILE}
                     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} 
                     OUTPUT_QUIET
    )

    # Configure Qt (skip building of useless modules)
    set( QT_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/qt-build" )
    file( MAKE_DIRECTORY ${QT_BUILD_DIR} )
    message( STATUS "Configuring..." )
    execute_process( COMMAND ${QT_SOURCE_DIR}/configure -prefix ${QT_BUILD_DIR}
                     WORKING_DIRECTORY ${QT_BUILD_DIR} )

    # Check if system supports parallelization
    if( DEFINED ENV{NUMBER_OF_PROCESSORS} )
        set( JOBS_OPTION "-j$ENV{NUMBER_OF_PROCESSORS}" )
    elseif( DEFINED ENV{PROCESSOR_COUNT} )
        set( JOBS_OPTION "-j$ENV{PROCESSOR_COUNT}" )
    else()
        set( JOBS_OPTION "" )
    endif()

    # Use -j option to compile if possible (qt is very big)
    if( JOBS_OPTION )
        message( STATUS "Compiling with parallelization..." )
        execute_process( COMMAND ninja ${JOBS_OPTION}
                     WORKING_DIRECTORY ${QT_BUILD_DIR} )
    else()
        message( STATUS "Compiling without parallelization..." )
        execute_process( COMMAND ninja
                     WORKING_DIRECTORY ${QT_BUILD_DIR} )
    endif()

    # Set necessary environment variables to use Qt
    set( ENV{QTDIR} ${QT_BUILD_DIR} )
    set( ENV{PATH} ${QT_BUILD_DIR}/bin:$ENV{PATH})

else()
    message( STATUS "Qt 6.4.2 found in the system." )
endif()

这一切都是很好的评论,所以希望这可以对其他人有用。

    • 注意**:此脚本获取Qt 6.4.2,但您可以修改为任何需要的版本。
    • 注2**:如果你想加快这个过程,你可以通过修改这一行来要求跳过测试,示例和可选Qt模块的编译:
execute_process( COMMAND ${QT_SOURCE_DIR}/configure -prefix ${QT_BUILD_DIR} WORKING_DIRECTORY ${QT_BUILD_DIR} )

相关问题