Skip to content

Commit a3938bc

Browse files
committed
ci: 新增 C++/CMake 跨平台流水线并支持 Windows 打包发布、新增py训练脚本
1 parent bd9e739 commit a3938bc

63 files changed

Lines changed: 728 additions & 19 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/cpp-cmake-ci.yml

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
name: cpp-cmake-ci
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- main
8+
tags:
9+
- "v*"
10+
pull_request:
11+
branches:
12+
- master
13+
- main
14+
workflow_dispatch:
15+
16+
permissions:
17+
contents: write
18+
19+
env:
20+
APP_NAME: YOLOClassifier
21+
BUILD_TYPE: Release
22+
23+
jobs:
24+
build:
25+
name: ${{ matrix.name }}
26+
runs-on: ${{ matrix.os }}
27+
strategy:
28+
fail-fast: false
29+
matrix:
30+
include:
31+
- name: Windows / Qt 5.12.12
32+
os: windows-2022
33+
qt_host: windows
34+
qt_arch: win64_msvc2017_64
35+
vcpkg_triplet: x64-windows
36+
artifact_name: YOLOClassifier-windows-x64
37+
- name: Linux / Qt 5.12.12
38+
os: ubuntu-22.04
39+
qt_host: linux
40+
qt_arch: gcc_64
41+
vcpkg_triplet: x64-linux
42+
artifact_name: YOLOClassifier-linux-x64
43+
44+
steps:
45+
- name: Checkout
46+
uses: actions/checkout@v4
47+
48+
- name: Install Linux system packages
49+
if: runner.os == 'Linux'
50+
shell: bash
51+
run: |
52+
sudo apt-get update
53+
sudo apt-get install -y ninja-build
54+
55+
- name: Setup MSVC
56+
if: runner.os == 'Windows'
57+
uses: ilammy/msvc-dev-cmd@v1
58+
with:
59+
arch: x64
60+
61+
- name: Install Qt 5.12.12
62+
uses: jurplel/install-qt-action@v4
63+
with:
64+
version: 5.12.12
65+
host: ${{ matrix.qt_host }}
66+
target: desktop
67+
arch: ${{ matrix.qt_arch }}
68+
cache: true
69+
install-deps: true
70+
71+
- name: Bootstrap vcpkg on Windows
72+
if: runner.os == 'Windows'
73+
shell: pwsh
74+
run: |
75+
git clone https://github.com/microsoft/vcpkg.git "$env:RUNNER_TEMP\vcpkg"
76+
& "$env:RUNNER_TEMP\vcpkg\bootstrap-vcpkg.bat" -disableMetrics
77+
"VCPKG_ROOT=$env:RUNNER_TEMP\vcpkg" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
78+
79+
- name: Bootstrap vcpkg on Linux
80+
if: runner.os == 'Linux'
81+
shell: bash
82+
run: |
83+
git clone https://github.com/microsoft/vcpkg.git "$RUNNER_TEMP/vcpkg"
84+
"$RUNNER_TEMP/vcpkg/bootstrap-vcpkg.sh" -disableMetrics
85+
echo "VCPKG_ROOT=$RUNNER_TEMP/vcpkg" >> "$GITHUB_ENV"
86+
87+
- name: Install ONNX Runtime on Windows
88+
if: runner.os == 'Windows'
89+
shell: pwsh
90+
run: |
91+
& "$env:VCPKG_ROOT\vcpkg.exe" install "onnxruntime:${{ matrix.vcpkg_triplet }}"
92+
93+
- name: Install ONNX Runtime on Linux
94+
if: runner.os == 'Linux'
95+
shell: bash
96+
run: |
97+
"$VCPKG_ROOT/vcpkg" install "onnxruntime:${{ matrix.vcpkg_triplet }}"
98+
99+
- name: Configure on Windows
100+
if: runner.os == 'Windows'
101+
shell: pwsh
102+
run: |
103+
cmake -S . -B build -G Ninja `
104+
"-DCMAKE_BUILD_TYPE=${env:BUILD_TYPE}" `
105+
"-DCMAKE_TOOLCHAIN_FILE=$env:VCPKG_ROOT\scripts\buildsystems\vcpkg.cmake" `
106+
"-DVCPKG_TARGET_TRIPLET=${{ matrix.vcpkg_triplet }}" `
107+
"-DQt5_DIR=$env:Qt5_DIR"
108+
109+
- name: Configure on Linux
110+
if: runner.os == 'Linux'
111+
shell: bash
112+
run: |
113+
cmake -S . -B build -G Ninja \
114+
-DCMAKE_BUILD_TYPE="${BUILD_TYPE}" \
115+
-DCMAKE_TOOLCHAIN_FILE="$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake" \
116+
-DVCPKG_TARGET_TRIPLET="${{ matrix.vcpkg_triplet }}" \
117+
-DQt5_DIR="${Qt5_DIR}"
118+
119+
- name: Build on Windows
120+
if: runner.os == 'Windows'
121+
shell: pwsh
122+
run: cmake --build build --config $env:BUILD_TYPE --parallel
123+
124+
- name: Build on Linux
125+
if: runner.os == 'Linux'
126+
shell: bash
127+
run: cmake --build build --config "${BUILD_TYPE}" --parallel
128+
129+
- name: Package Windows bundle
130+
if: runner.os == 'Windows'
131+
shell: pwsh
132+
env:
133+
BUILD_DIR: ${{ github.workspace }}\build
134+
DIST_DIR: ${{ github.workspace }}\dist
135+
VCPKG_TRIPLET: ${{ matrix.vcpkg_triplet }}
136+
run: .\scripts\package-windows.ps1
137+
138+
- name: Package Linux artifact
139+
if: runner.os == 'Linux'
140+
shell: bash
141+
run: |
142+
package_dir="dist/${{ matrix.artifact_name }}"
143+
mkdir -p "${package_dir}"
144+
cp "build/bin/${APP_NAME}" "${package_dir}/"
145+
cp README.md "${package_dir}/"
146+
if [ -f "models/cat_vs_dog/best.onnx" ]; then
147+
mkdir -p "${package_dir}/models/cat_vs_dog"
148+
cp "models/cat_vs_dog/best.onnx" "${package_dir}/models/cat_vs_dog/"
149+
fi
150+
tar -C dist -czf "dist/${{ matrix.artifact_name }}.tar.gz" "${{ matrix.artifact_name }}"
151+
152+
- name: Upload Windows artifact
153+
if: runner.os == 'Windows'
154+
uses: actions/upload-artifact@v4
155+
with:
156+
name: ${{ matrix.artifact_name }}
157+
path: dist/${{ matrix.artifact_name }}.zip
158+
if-no-files-found: error
159+
160+
- name: Upload Linux artifact
161+
if: runner.os == 'Linux'
162+
uses: actions/upload-artifact@v4
163+
with:
164+
name: ${{ matrix.artifact_name }}
165+
path: dist/${{ matrix.artifact_name }}.tar.gz
166+
if-no-files-found: error
167+
168+
- name: Publish GitHub Release
169+
if: runner.os == 'Windows' && github.ref_type == 'tag'
170+
uses: softprops/action-gh-release@v2
171+
with:
172+
files: dist/${{ matrix.artifact_name }}.zip
173+
generate_release_notes: true

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
.cache/
2-
build/
2+
build/
3+
runs/
4+
*.pt
5+
*.cache
6+
*.torchscript

CMakeLists.txt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
cmake_minimum_required(VERSION 3.16)
2-
project(YOLOClassifier LANGUAGES CXX)
2+
project(YOLOClassifier VERSION 0.1.0 LANGUAGES CXX)
33

44
set(CMAKE_CXX_STANDARD 17)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
set(CMAKE_CXX_EXTENSIONS OFF)
67

7-
set(CMAKE_PREFIX_PATH $ENV{VCPKG_LIB} ${CMAKE_PREFIX_PATH})
8-
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/build/${CMAKE_BUILD_TYPE}/bin)
8+
if(DEFINED ENV{VCPKG_LIB} AND NOT "$ENV{VCPKG_LIB}" STREQUAL "")
9+
list(PREPEND CMAKE_PREFIX_PATH "$ENV{VCPKG_LIB}")
10+
endif()
11+
12+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
913

1014
find_package(Qt5 REQUIRED COMPONENTS Core Gui Widgets)
1115
find_package(onnxruntime CONFIG REQUIRED)
@@ -15,11 +19,11 @@ set(CMAKE_AUTORCC ON)
1519
set(CMAKE_AUTOUIC ON)
1620

1721
add_executable(${PROJECT_NAME}
18-
main.cpp
19-
MainWindow.cpp
20-
MainWindow.h
21-
OnnxClassifier.cpp
22-
OnnxClassifier.h
22+
src/main.cpp
23+
src/MainWindow.h
24+
src/MainWindow.cpp
25+
src/OnnxClassifier.h
26+
src/OnnxClassifier.cpp
2327
)
2428

2529
target_link_libraries(${PROJECT_NAME} PRIVATE
@@ -28,3 +32,17 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
2832
Qt5::Widgets
2933
onnxruntime::onnxruntime
3034
)
35+
36+
install(TARGETS ${PROJECT_NAME}
37+
RUNTIME DESTINATION .
38+
)
39+
40+
install(FILES "${CMAKE_SOURCE_DIR}/README.md"
41+
DESTINATION .
42+
)
43+
44+
if(EXISTS "${CMAKE_SOURCE_DIR}/models/cat_vs_dog/best.onnx")
45+
install(FILES "${CMAKE_SOURCE_DIR}/models/cat_vs_dog/best.onnx"
46+
DESTINATION models/cat_vs_dog
47+
)
48+
endif()

README.md

Lines changed: 44 additions & 6 deletions

assets/train/cat/cat_0.jpg

41.7 KB

assets/train/cat/cat_1.jpg

135 KB

assets/train/cat/cat_10.jpg

103 KB

assets/train/cat/cat_11.jpg

53.8 KB

assets/train/cat/cat_12.jpg

69.6 KB

assets/train/cat/cat_13.jpg

77.7 KB

0 commit comments

Comments
 (0)