Skip to content

Commit

Permalink
优化 编译、构建流程;
Browse files Browse the repository at this point in the history
  • Loading branch information
RealChuan committed Apr 23, 2024
1 parent 97a327f commit 782ac71
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 102 deletions.
18 changes: 12 additions & 6 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
28 changes: 22 additions & 6 deletions .github/workflows/qmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ jobs:
- 6.7.0
qt_modules:
- qt5compat qtserialport qtnetworkauth qtmultimedia qtimageformats
build_type:
- "release"
arch:
- x86_64
vcpkg_libs:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
14 changes: 10 additions & 4 deletions .github/workflows/toolchain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@ 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
run: |
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
Expand All @@ -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
Expand All @@ -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')
Expand Down
76 changes: 8 additions & 68 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
22 changes: 22 additions & 0 deletions cmake/build_info.cmake
Original file line number Diff line number Diff line change
@@ -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}")
37 changes: 37 additions & 0 deletions cmake/common.cmake
Original file line number Diff line number Diff line change
@@ -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})
7 changes: 7 additions & 0 deletions cmake/qt.cmake
Original file line number Diff line number Diff line change
@@ -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()
33 changes: 33 additions & 0 deletions cmake/qt_utils.cmake
Original file line number Diff line number Diff line change
@@ -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()
Loading

0 comments on commit 782ac71

Please sign in to comment.