diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index ec7c653..00d1c69 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -62,6 +62,12 @@ jobs: vcpkg_libs: breakpad steps: + - name: Set macos deployment target + if: startsWith(matrix.os, 'macos') + shell: bash + run: | + echo "MACOSX_DEPLOYMENT_TARGET=11.0" >> $GITHUB_ENV + - name: Restore windows vcpkg if: startsWith(matrix.os, 'windows') uses: actions/cache/restore@v4 @@ -82,7 +88,7 @@ jobs: choco install ninja ninja --version cmake --version - vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-windows + vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-windows || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: Install dependencies on ubuntu if: startsWith(matrix.os, 'ubuntu') shell: bash @@ -92,7 +98,7 @@ jobs: ninja --version cmake --version gcc --version - vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-linux + vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-linux || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: Install dependencies on macos-x86_64 if: startsWith(matrix.os, 'macos') && matrix.arch == 'x86_64' shell: bash @@ -101,7 +107,7 @@ jobs: ninja --version cmake --version clang --version - vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-osx + vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-osx || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: Install dependencies on macos-arm64 if: startsWith(matrix.os, 'macos') && matrix.arch == 'arm64' shell: bash @@ -110,7 +116,7 @@ jobs: ninja --version cmake --version clang --version - vcpkg install ${{ matrix.vcpkg_libs }} --triplet arm64-osx + vcpkg install ${{ matrix.vcpkg_libs }} --triplet arm64-osx || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: Install Qt uses: jurplel/install-qt-action@v3 @@ -131,9 +137,9 @@ jobs: run: | rm vcpkg.json - - name: Enable Developer Command Prompt + - name: Configure msvc for amd64 if: startsWith(matrix.os, 'windows') - uses: ilammy/msvc-dev-cmd@v1.12.1 + uses: ilammy/msvc-dev-cmd@v1 with: arch: amd64 - name: Configure windows diff --git a/.github/workflows/qmake.yml b/.github/workflows/qmake.yml index 50e97e5..c2129da 100644 --- a/.github/workflows/qmake.yml +++ b/.github/workflows/qmake.yml @@ -46,6 +46,8 @@ jobs: - 6.7.0 qt_modules: - qt5compat qtserialport qtnetworkauth qtmultimedia qtimageformats + build_type: + - "release" arch: - x86_64 vcpkg_libs: @@ -54,10 +56,17 @@ jobs: - os: macos-latest qt_ver: 6.7.0 qt_modules: qt5compat qtserialport qtnetworkauth qtmultimedia qtimageformats + build_type: "release" arch: arm64 vcpkg_libs: breakpad steps: + - name: Set macos deployment target + if: startsWith(matrix.os, 'macos') + shell: bash + run: | + echo "MACOSX_DEPLOYMENT_TARGET=11.0" >> $GITHUB_ENV + - name: Restore windows vcpkg if: startsWith(matrix.os, 'windows') uses: actions/cache/restore@v4 @@ -78,7 +87,7 @@ jobs: choco install ninja ninja --version cmake --version - vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-windows + vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-windows || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: Install dependencies on ubuntu if: startsWith(matrix.os, 'ubuntu') shell: bash @@ -88,7 +97,7 @@ jobs: ninja --version cmake --version gcc --version - vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-linux + vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-linux || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: Install dependencies on macos-x86_64 if: startsWith(matrix.os, 'macos') && matrix.arch == 'x86_64' shell: bash @@ -97,7 +106,7 @@ jobs: ninja --version cmake --version clang --version - vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-osx + vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-osx || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: Install dependencies on macos-arm64 if: startsWith(matrix.os, 'macos') && matrix.arch == 'arm64' shell: bash @@ -106,7 +115,7 @@ jobs: ninja --version cmake --version clang --version - vcpkg install ${{ matrix.vcpkg_libs }} --triplet arm64-osx + vcpkg install ${{ matrix.vcpkg_libs }} --triplet arm64-osx || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: Install Qt uses: jurplel/install-qt-action@v3 @@ -120,16 +129,23 @@ jobs: with: fetch-depth: 1 + - name: mkdir build + shell: bash + run: | + mkdir build + - name: msvc-build if: startsWith(matrix.os, 'windows') shell: cmd run: | call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64 - qmake + qmake "CONFIG+=${{ matrix.build_type }}" ./../. nmake + working-directory: build - name: build macos or ubuntu if: startsWith(matrix.os, 'macos') || startsWith(matrix.os, 'ubuntu') shell: bash run: | - qmake QMAKE_APPLE_DEVICE_ARCHS="${{ matrix.arch }}" + qmake QMAKE_APPLE_DEVICE_ARCHS="${{ matrix.arch }}" "CONFIG+=${{ matrix.build_type }}" ./../. make -j4 + working-directory: build diff --git a/.github/workflows/toolchain.yml b/.github/workflows/toolchain.yml index 726e747..f0c7e6a 100644 --- a/.github/workflows/toolchain.yml +++ b/.github/workflows/toolchain.yml @@ -28,6 +28,12 @@ jobs: - breakpad steps: + - name: Set macos deployment target + if: startsWith(matrix.os, 'macos') + shell: bash + run: | + echo "MACOSX_DEPLOYMENT_TARGET=11.0" >> $GITHUB_ENV + - name: Install dependencies on windows if: startsWith(matrix.os, 'windows') shell: bash @@ -35,7 +41,7 @@ jobs: choco install ninja ninja --version cmake --version - vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-windows + vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-windows || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: Install dependencies on macos if: startsWith(matrix.os, 'macos') shell: bash @@ -44,8 +50,8 @@ jobs: ninja --version cmake --version clang --version - vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-osx - vcpkg install ${{ matrix.vcpkg_libs }} --triplet arm64-osx + vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-osx || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) + vcpkg install ${{ matrix.vcpkg_libs }} --triplet arm64-osx || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: Install dependencies on ubuntu if: startsWith(matrix.os, 'ubuntu') shell: bash @@ -55,7 +61,7 @@ jobs: ninja --version cmake --version gcc --version - vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-linux + vcpkg install ${{ matrix.vcpkg_libs }} --triplet x64-linux || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: cache windows vcpkg if: startsWith(matrix.os, 'windows') diff --git a/CMakeLists.txt b/CMakeLists.txt index 5599867..065d3ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,69 +1,20 @@ cmake_minimum_required(VERSION 3.25.1) -if(CMAKE_HOST_WIN32) - set(CMAKE_TOOLCHAIN_FILE - "C:/vcpkg/scripts/buildsystems/vcpkg.cmake" - CACHE STRING "Vcpkg toolchain file") -elseif(CMAKE_HOST_APPLE) - set(CMAKE_TOOLCHAIN_FILE - "/usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake" - CACHE STRING "Vcpkg toolchain file") -elseif(CMAKE_HOST_LINUX) - set(CMAKE_TOOLCHAIN_FILE - "/usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake" - CACHE STRING "Vcpkg toolchain file") -endif() +include(cmake/vcpkg.cmake) +include(cmake/qt.cmake) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") include(utils) - -if(CMAKE_HOST_WIN32) - list(APPEND CMAKE_PREFIX_PATH "C:\\Qt\\6.7.0\\msvc2019_64") -elseif(CMAKE_HOST_APPLE) - -elseif(CMAKE_HOST_LINUX) - list(APPEND CMAKE_PREFIX_PATH "/opt/Qt/6.7.0/gcc_64") -endif() +include(qt_utils) project( Qt-App VERSION 0.1.1 + DESCRIPTION "This is a qt Application template" + HOMEPAGE_URL "https://github.com/RealChuan/Qt-App" LANGUAGES CXX) -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) -set(CMAKE_DEBUG_POSTFIX d) - -if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) - message("Setting build type to 'RelWithDebInfo' as none was specified.") - set(CMAKE_BUILD_TYPE - RelWithDebInfo - CACHE STRING "Choose the type of build." FORCE) - # Set the possible values of build type for cmake-gui - set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" - "MinSizeRel" "RelWithDebInfo") -endif() - -if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(CURRENT_PLATFORM "-64") -else() - set(CURRENT_PLATFORM "-32") -endif() - -message(STATUS "Current Platform is ${CURRENT_PLATFORM}") - -# 设定可执行二进制文件的目录 -set(EXECUTABLE_OUTPUT_PATH - ${PROJECT_SOURCE_DIR}/bin${CURRENT_PLATFORM}/${CMAKE_BUILD_TYPE}) # 源文件目录 -# 设定存放编译出来的库文件的目录 -set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin${CURRENT_PLATFORM}/libs) -# 并且把该目录设为连接目录 -link_directories(${LIBRARY_OUTPUT_PATH}) - -include_directories(${PROJECT_SOURCE_DIR}) +include(cmake/common.cmake) find_package( Qt6 REQUIRED @@ -91,17 +42,6 @@ include_directories(src) add_subdirectory(src) add_subdirectory(examples) -string(TOLOWER ${PROJECT_NAME} Lower_PROJECT_NAME) - -qt_add_translations( - ${PROJECT_NAME} - TS_FILE_BASE - ${Lower_PROJECT_NAME} - TS_FILE_DIR - translations - INCLUDE_DIRECTORIES - directory - ${PROJECT_SOURCE_DIR}/src) +add_translations(${PROJECT_NAME}) -file(GLOB_RECURSE TS_FILES "${PROJECT_SOURCE_DIR}/translations/*.ts") -set_qt_translations_output_location(${TS_FILES}) +include(cmake/build_info.cmake) diff --git a/cmake/build_info.cmake b/cmake/build_info.cmake new file mode 100644 index 0000000..917befe --- /dev/null +++ b/cmake/build_info.cmake @@ -0,0 +1,22 @@ +# 输出 CMake 版本和构建系统类型 +message("CMake Version: ${CMAKE_VERSION}") +message("Generator: ${CMAKE_GENERATOR}") + +# 输出编译器信息 +message("C Compiler ID: ${CMAKE_C_COMPILER_ID}") +message("C++ Compiler ID: ${CMAKE_CXX_COMPILER_ID}") +message("C++ Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}") + +# 输出构建类型和编译选项 +message("Build Type: ${CMAKE_BUILD_TYPE}") +message("C++ Compiler Flags: ${CMAKE_CXX_FLAGS}") + +# 输出链接选项 +message("Executable Linker Flags: ${CMAKE_EXE_LINKER_FLAGS}") + +# 输出构建和源代码目录 +message("Build Directory: ${CMAKE_BINARY_DIR}") +message("Source Directory: ${CMAKE_SOURCE_DIR}") + +# 输出目标架构 +message("Target Processor: ${CMAKE_SYSTEM_PROCESSOR}") diff --git a/cmake/common.cmake b/cmake/common.cmake new file mode 100644 index 0000000..ccfb15a --- /dev/null +++ b/cmake/common.cmake @@ -0,0 +1,37 @@ +if(CMAKE_HOST_APPLE) + set(CMAKE_OSX_DEPLOYMENT_TARGET + "11.0" + CACHE STRING "Minimum OS X version") +endif() + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) +set(CMAKE_DEBUG_POSTFIX d) + +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message("Setting build type to 'RelWithDebInfo' as none was specified.") + set(CMAKE_BUILD_TYPE + RelWithDebInfo + CACHE STRING "Choose the type of build." FORCE) + # Set the possible values of build type for cmake-gui + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" + "MinSizeRel" "RelWithDebInfo") +endif() + +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(BITS "64") +else() + set(BITS "32") +endif() + +message(STATUS "Current Platform is ${BITS} bits.") + +set(EXECUTABLE_OUTPUT_PATH + ${PROJECT_SOURCE_DIR}/bin-${BITS}/${CMAKE_BUILD_TYPE}) +set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin-${BITS}/libs) +link_directories(${LIBRARY_OUTPUT_PATH}) + +include_directories(${PROJECT_SOURCE_DIR}) diff --git a/cmake/qt.cmake b/cmake/qt.cmake new file mode 100644 index 0000000..5a7f536 --- /dev/null +++ b/cmake/qt.cmake @@ -0,0 +1,7 @@ +if(CMAKE_HOST_WIN32) + list(APPEND CMAKE_PREFIX_PATH "C:\\Qt\\6.7.0\\msvc2019_64") +elseif(CMAKE_HOST_APPLE) + +elseif(CMAKE_HOST_LINUX) + list(APPEND CMAKE_PREFIX_PATH "/opt/Qt/6.7.0/gcc_64") +endif() diff --git a/cmake/qt_utils.cmake b/cmake/qt_utils.cmake new file mode 100644 index 0000000..ed9adf4 --- /dev/null +++ b/cmake/qt_utils.cmake @@ -0,0 +1,33 @@ +function(add_translations OUTPUT_NAME) + # 转换输出名称为小写 + string(TOLOWER ${OUTPUT_NAME} Lower_Output_Name) + + # 添加翻译文件 + qt_add_translations( + ${OUTPUT_NAME} + TS_FILE_BASE + ${Lower_Output_Name} + TS_FILE_DIR + translations + INCLUDE_DIRECTORIES + directory + ${PROJECT_SOURCE_DIR}/src) + + # 确定翻译文件的输出位置 + if(CMAKE_HOST_APPLE) + set(output_location + "${EXECUTABLE_OUTPUT_PATH}/${OUTPUT_NAME}.app/Contents/Resources/translations" + ) + else() + set(output_location "${EXECUTABLE_OUTPUT_PATH}/translations") + endif() + + # 查找所有的翻译文件(.ts) + file(GLOB_RECURSE TS_FILES "${PROJECT_SOURCE_DIR}/translations/*.ts") + + # 设置翻译文件的输出位置 + foreach(ts_file ${TS_FILES}) + set_source_files_properties("${ts_file}" PROPERTIES OUTPUT_LOCATION + "${output_location}") + endforeach() +endfunction() diff --git a/cmake/utils.cmake b/cmake/utils.cmake index 7836ab0..01357ef 100644 --- a/cmake/utils.cmake +++ b/cmake/utils.cmake @@ -43,21 +43,3 @@ function(add_rpath target_name) PROPERTIES LINK_FLAGS "-Wl,-rpath,./:./lib:./../lib") endif() endfunction() - -function(set_qt_translations_output_location) - set(options "") - set(oneValueArgs "") - set(multiValueArgs TS_FILES) - - if(CMAKE_HOST_APPLE) - set(output_location - "${EXECUTABLE_OUTPUT_PATH}/${PROJECT_NAME}.app/Contents/Resources/translations" - ) - else() - set(output_location "${EXECUTABLE_OUTPUT_PATH}/translations") - endif() - foreach(ts_file ${TS_FILES}) - set_source_files_properties("${ts_file}" PROPERTIES OUTPUT_LOCATION - "${output_location}") - endforeach(ts_file) -endfunction() diff --git a/cmake/vcpkg.cmake b/cmake/vcpkg.cmake new file mode 100644 index 0000000..328f00f --- /dev/null +++ b/cmake/vcpkg.cmake @@ -0,0 +1,13 @@ +if(CMAKE_HOST_WIN32) + set(CMAKE_TOOLCHAIN_FILE + "C:/vcpkg/scripts/buildsystems/vcpkg.cmake" + CACHE STRING "Vcpkg toolchain file") +elseif(CMAKE_HOST_APPLE) + set(CMAKE_TOOLCHAIN_FILE + "/usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake" + CACHE STRING "Vcpkg toolchain file") +elseif(CMAKE_HOST_LINUX) + set(CMAKE_TOOLCHAIN_FILE + "/usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake" + CACHE STRING "Vcpkg toolchain file") +endif()