在 GitHub 个操作中构建基于 Qt/cmake 的项目

Build Qt/cmake based project in GitHub Actions

我在我的项目中使用 Qt 5.12.11。因为我想在 linux 和 win 上都使用该软件,所以我选择了 gcc 编译器(我使用 MSYS2,所以 gcc 是 10.3.0)。本地构建对于 linux 和 win 都是可以的,但是通过 GitHub Actions 的构建在找到 qt 时失败(无论我是否设置 CMAKE_PREFIX_PATH 或 Qt5_DIR 或不) .

这就是我被困了一段时间的地方。有没有人遇到过这样的问题或者有什么想法?

github动作看起来像

name: Build

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

env:
  BUILD_TYPE: Release

jobs:
  build:
    runs-on: windows-latest
    defaults:
        run:
            shell: msys2 {0}

    steps:
      - uses: msys2/setup-msys2@v2
        with:
            install: mingw-w64-x86_64-toolchain
            msystem: mingw64
            release: false

      - name: Install Qt
        uses: jurplel/install-qt-action@v2
        with:
          version: '5.12.11'
          host: 'windows'
          target: 'desktop'
          arch: 'win64_mingw73'
          dir: '${{github.workspace}}/qt/'
          install-deps: 'true'

      - name: List files in Qt
        run: find ${{env.Qt5_Dir}}

      - uses: actions/checkout@v2

      - name: Configure CMake
        run: cmake -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_PREFIX_PATH="${{env.Qt5_Dir}}/lib/cmake/" -DQt5_DIR=${{env.Qt5_Dir}}/lib/cmake/Qt5/ -G "CodeBlocks - MinGW Makefiles" -B '${{github.workspace}}'/build

      - name: Build
        run: cmake --build '${{github.workspace}}'/build --target RePr

我也试过 -DCMAKE_PREFIX_PATH="${{env.Qt5_Dir}}/lib/cmake/Qt5/" 但没有用。 :( 如果我直接从 cmakelists 设置 CMAKE_PREFIX_PATH,我会观察到相同的行为。

它失败的 CMakeLists 非常简单和常用(它是一个使用 qt 的嵌套包,所以不要混淆不同的 cmakelists 项目和构建目标)

cmake_minimum_required(VERSION 3.14)
project(RePr_controller)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(QT_VERSION 5)
set(REQUIRED_LIBS Core Gui Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets)

add_library(${PROJECT_NAME} main_window.cpp main_window.h main_window.ui resources/resources.qrc)
add_dependencies(${PROJECT_NAME} FacadeLib)
target_link_libraries(${PROJECT_NAME} FacadeLib Utils)

find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})

直接从 cmakelists returns

向 CMAKE_PREFIX_PATH 发送消息
D:/a/RePr/RePr/qt/Qt/5.12.11/mingw73_64/lib/cmake/

qt5 来了

D:/a/RePr/RePr/qt/Qt/5.12.11/mingw73_64/lib/cmake/Qt5/Qt5Config.cmake

最后,我在配置步骤

中收到了这条消息
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="D:/a/RePr/RePr/qt/Qt/5.12.11/mingw73_64/lib/cmake/" -DQt5_DIR=D:/a/RePr/RePr/qt/Qt/5.12.11/mingw73_64/lib/cmake/Qt5/ -G "CodeBlocks - MinGW Makefiles" -B 'D:\a\RePr\RePr'/build
  shell: D:\a\_temp\msys\msys2.CMD {0}
  env:
    BUILD_TYPE: Release
    MSYSTEM: MINGW64
    pythonLocation: C:\hostedtoolcache\windows\Python.9.6\x64
    Qt5_Dir: D:/a/RePr/RePr/qt/Qt/5.12.11/mingw73_64
    QT_PLUGIN_PATH: D:/a/RePr/RePr/qt/Qt/5.12.11/mingw73_64/plugins
    QML2_IMPORT_PATH: D:/a/RePr/RePr/qt/Qt/5.12.11/mingw73_64/qml
-- The C compiler identification is GNU 10.3.0
-- The CXX compiler identification is GNU 10.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/msys64/mingw64/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/msys64/mingw64/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done

CMake Error at Controller/CMakeLists.txt:17 (find_package):
  By not providing "FindQt5.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Qt5", but
-- Configuring incomplete, errors occurred!
See also "D:/a/RePr/RePr/build/CMakeFiles/CMakeOutput.log".
  CMake did not find one.

  Could not find a package configuration file provided by "Qt5" with any of
  the following names:

    Qt5Config.cmake
    qt5-config.cmake

  Add the installation prefix of "Qt5" to CMAKE_PREFIX_PATH or set "Qt5_DIR"
  to a directory containing one of the above files.  If "Qt5" provides a
  separate development package or SDK, be sure it has been installed.


Error: Process completed with exit code 1.

将CMAKE_PREFIX_PATH设置为Qt5_Dir:

- name: Configure CMake
  env:
    CMAKE_PREFIX_PATH: ${{env.Qt5_Dir}}
  run: cmake -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -G "CodeBlocks - MinGW Makefiles" -B '${{github.workspace}}'/build