-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·93 lines (83 loc) · 2.4 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
function help {
echo "Usage: ./build.sh [OPTION] [ARGUMENT]"
echo "Build the project, optionally run tests, or clean directories."
echo
echo "Options:"
echo " test Build the project and run tests."
echo " clean Clean the build directory."
echo " clean output Clean the package tests/output and build directories."
echo " help Display this help message."
}
function prepare_directories {
# 检查并清理或创建目录
for dir in "$@"; do
if [ -d "$dir" ]; then
echo "Cleaning $dir..."
rm -rf "$dir/*"
else
echo "Creating $dir..."
mkdir -p "$dir"
fi
done
}
function build_project {
# 创建一个包含目录名的数组
directories=(build package)
# 准备目录
prepare_directories "${directories[@]}"
cd build
# 检查是否有参数传入
if [ "$1" == "test" ]; then
prepare_directories tests/output
# 如果参数是 "test",则启用测试
cmake -DBUILD_TESTS=ON ..
else
# 否则,禁用测试
cmake ..
fi
# 构建项目
make
# 如果启用了测试,运行所有的测试可执行文件
if [ "$1" == "test" ]; then
echo "Running tests..."
if command -v valgrind &> /dev/null; then
for test in ../tests/output/*; do
echo "Running $test with valgrind..."
valgrind --leak-check=full $test
echo
done
else
for test in ../tests/output/*; do
echo "Running $test..."
$test
echo
done
fi
fi
}
function clean_directories {
if [ "$1" == "output" ]; then
# 如果第二个参数是 "output",则清理 package 目录和 tests/output 目录
echo "Cleaning package tests/output and build directories..."
rm -rf package tests/output build
else
# 否则,清理 build 目录
echo "Cleaning build directory..."
rm -rf build
fi
}
function main {
# 检查是否有参数传入
if [ "$1" == "help" ]; then
# 如果参数是 "help",则显示帮助信息
help
elif [ "$1" == "clean" ]; then
clean_directories $2
else
# 否则,调用 build_project 函数
build_project $1
fi
}
# 调用 main 函数
main "$@"