Skip to content

Commit 8b53949

Browse files
committed
ci(init): add ci workflow
1 parent 5b72b73 commit 8b53949

5 files changed

Lines changed: 186 additions & 10 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
name: Build on ${{ matrix.os }}
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
matrix:
15+
os: [ubuntu-latest, windows-latest, macOS-latest]
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
with:
21+
submodules: recursive
22+
23+
- name: Install Qt
24+
uses: jurplel/install-qt-action@v3
25+
with:
26+
version: '5.15.2'
27+
host: ${{ matrix.os == 'windows-latest' && 'windows' || matrix.os == 'macOS-latest' && 'mac' || 'linux' }}
28+
target: 'desktop'
29+
arch: ${{ matrix.os == 'windows-latest' && 'win64_msvc2019_64' || 'gcc_64' }}
30+
modules: 'qtbase qttools qttranslations'
31+
install-deps: 'true'
32+
33+
- name: Install CMake and Ninja
34+
uses: lukka/get-cmake@v3.21.1
35+
36+
- name: Install dependencies on Ubuntu
37+
if: matrix.os == 'ubuntu-latest'
38+
run: |
39+
sudo apt update
40+
sudo apt install -y build-essential libgl1-mesa-dev libxcb1-dev libxkbcommon-x11-dev libxcb-cursor0
41+
42+
- name: Install dependencies on macOS
43+
if: matrix.os == 'macOS-latest'
44+
run: |
45+
brew install cmake
46+
47+
- name: Install dependencies on Windows
48+
if: matrix.os == 'windows-latest'
49+
run: |
50+
choco install ninja
51+
52+
- name: Configure CMake
53+
run: |
54+
cmake -B ${{github.workspace}}/build -G "Ninja" -DCMAKE_BUILD_TYPE=Release
55+
56+
- name: Build project
57+
run: |
58+
cmake --build ${{github.workspace}}/build --config Release
59+
60+
- name: Run tests
61+
run: |
62+
cd ${{github.workspace}}/build && ctest --output-on-failure -C Release
63+
env:
64+
CTEST_OUTPUT_ON_FAILURE: 1
65+
66+
- name: Upload build artifacts (Ubuntu)
67+
if: matrix.os == 'ubuntu-latest'
68+
uses: actions/upload-artifact@v3
69+
with:
70+
name: app-${{ matrix.os }}
71+
path: target/
72+
73+
clang-tidy:
74+
name: Run clang-tidy
75+
runs-on: ubuntu-latest
76+
steps:
77+
- name: Checkout code
78+
uses: actions/checkout@v4
79+
80+
- name: Install Qt
81+
uses: jurplel/install-qt-action@v3
82+
with:
83+
version: '5.15.2'
84+
host: 'linux'
85+
target: 'desktop'
86+
arch: 'gcc_64'
87+
modules: 'qtbase qttools qttranslations'
88+
install-deps: 'true'
89+
90+
- name: Install clang-tidy
91+
run: |
92+
sudo apt update
93+
sudo apt install -y clang-tidy
94+
95+
- name: Configure CMake with clang-tidy
96+
run: |
97+
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_CLANG_TIDY="clang-tidy"
98+
99+
- name: Build project with clang-tidy
100+
run: |
101+
cmake --build ${{github.workspace}}/build --config Release
102+
continue-on-error: true
103+
104+
- name: Run clang-tidy analysis
105+
run: |
106+
find src/ -name "*.cpp" -o -name "*.h" | xargs clang-tidy -p ${{github.workspace}}/build
107+
108+
memory-analysis:
109+
name: Memory analysis with Valgrind
110+
runs-on: ubuntu-latest
111+
steps:
112+
- name: Checkout code
113+
uses: actions/checkout@v4
114+
115+
- name: Install Qt
116+
uses: jurplel/install-qt-action@v3
117+
with:
118+
version: '5.15.2'
119+
host: 'linux'
120+
target: 'desktop'
121+
arch: 'gcc_64'
122+
modules: 'qtbase qttools qttranslations'
123+
install-deps: 'true'
124+
125+
- name: Install Valgrind
126+
run: |
127+
sudo apt update
128+
sudo apt install -y valgrind
129+
130+
- name: Configure CMake
131+
run: |
132+
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=Debug
133+
134+
- name: Build project
135+
run: |
136+
cmake --build ${{github.workspace}}/build --config Debug
137+
138+
- name: Run tests with Valgrind
139+
run: |
140+
cd ${{github.workspace}}/build && find . -name "*test*" -executable -type f -exec valgrind --leak-check=full --error-exitcode=1 {} \; || true

CMakeLists.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
cmake_minimum_required(VERSION 3.10)
22
project(ShoppingApp)
3-
set(target_name ShoppingApp)
43

54
# 设置C++标准
65
set(CMAKE_CXX_STANDARD 17)
76

7+
set(target_name ShoppingApp)
8+
89
# 设置目标输出目录
910
set(TARGET_DIR ${PROJECT_SOURCE_DIR}/target)
1011

@@ -36,12 +37,9 @@ pkg_check_modules(QT5 REQUIRED IMPORTED_TARGET Qt5Core Qt5Widgets)
3637
set(CMAKE_PREFIX_PATH "/opt/homebrew/Cellar/qt@5/5.15.16_2/lib/cmake") # hard coded for convenience
3738
find_package(Qt5 REQUIRED COMPONENTS Core Widgets) # I don't know why using pkg-config solely doesn't work for automoc etc
3839

39-
add_executable(${target_name})
40-
target_link_libraries(${target_name} PkgConfig::QT5)
41-
4240
# 添加子目录
4341
add_subdirectory(thirdparty)
4442

45-
add_subdirectory(test)
46-
4743
add_subdirectory(src)
44+
45+
add_subdirectory(test)

src/CMakeLists.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
# 使用GLOB自动搜索源文件、头文件和表单文件
2-
file(GLOB_RECURSE SOURCES "*.cpp")
3-
file(GLOB_RECURSE HEADERS "*.h")
1+
add_executable(${target_name})
2+
3+
# 添加子目录 - shop和ui
4+
add_subdirectory(shop)
5+
6+
# 使用GLOB自动搜索UI目录下的源文件、头文件和表单文件
7+
file(GLOB_RECURSE SOURCES "ui/*.cpp")
8+
file(GLOB_RECURSE HEADERS "ui/*.h")
49
file(GLOB_RECURSE FORMS "*.ui")
510

611
# 如果有特殊命名的源文件需要排除或额外添加,可以在这里处理
@@ -10,6 +15,14 @@ target_include_directories(${target_name} PUBLIC
1015
${CMAKE_CURRENT_SOURCE_DIR}
1116
)
1217

18+
target_link_libraries(${target_name} PkgConfig::QT5)
19+
1320
target_sources(${target_name} PRIVATE
21+
main.cpp # 主入口文件
1422
${SOURCES} ${HEADERS} ${FORMS}
23+
)
24+
25+
# 链接shop库到主程序
26+
target_link_libraries(${target_name}
27+
shop
1528
)

src/shop/CMakelists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 定义库名称
2+
set(SHOP_LIBRARY_NAME shop)
3+
4+
# 使用GLOB自动搜索shop目录下的源文件和头文件
5+
file(GLOB_RECURSE SHOP_SOURCES "*.cpp")
6+
file(GLOB_RECURSE SHOP_HEADERS "*.h")
7+
8+
# 创建静态库
9+
add_library(${SHOP_LIBRARY_NAME} STATIC
10+
${SHOP_SOURCES}
11+
${SHOP_HEADERS}
12+
)
13+
14+
# 设置库的包含目录
15+
target_include_directories(${SHOP_LIBRARY_NAME}
16+
PUBLIC
17+
${CMAKE_CURRENT_SOURCE_DIR}
18+
)
19+
20+
# 链接Qt5库到shop库
21+
target_link_libraries(${SHOP_LIBRARY_NAME}
22+
PRIVATE
23+
Qt5::Core
24+
Qt5::Widgets
25+
)

src/shop/UserRepository.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef USERREPOSITORY_H
22
#define USERREPOSITORY_H
33

4-
#include "shop/User.h"
4+
#include "User.h"
55
#include <QHash>
66
#include <QList>
77
#include <QString>

0 commit comments

Comments
 (0)