forked from misakamm/xege
-
Notifications
You must be signed in to change notification settings - Fork 50
/
tasks.sh
executable file
·170 lines (147 loc) · 4.19 KB
/
tasks.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env bash
cd "$(dirname "$0")"
PROJECT_DIR=$(pwd)
CMAKE_VS_DIR="$PROJECT_DIR/build"
# 如果是在 vscode 里面调用, 那么不给错误码, 否则会导致 vscode tasks 无法正常执行任务并显示错误.
if [[ "$TERM_PROGRAM" =~ vscode ]]; then
export EXIT_WHEN_FAILED=false
else
export EXIT_WHEN_FAILED=true
fi
export BUILD_TARGET="xege" # 默认只构建 xege 静态库
# 默认开Release模式
export CMAKE_BUILD_TYPE="Release"
if [[ -z "$WIN_CMAKE_BUILD_DEFINE" ]]; then
export WIN_CMAKE_BUILD_DEFINE=""
fi
if [[ -z "$CMAKE_CONFIG_DEFINE" ]]; then
export CMAKE_CONFIG_DEFINE=""
fi
function MY_CMAKE_BUILD_DEFINE() {
echo "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} ${CMAKE_CONFIG_DEFINE}"
}
if ! command -v grealpath && command -v realpath; then
function grealpath() {
realpath $@
}
fi
function isWindows() {
# check mingw and cygwin
[[ -d "/c" ]] || [[ -d "/cygdrive/c" ]]
}
function loadCMakeProject() {
echo "Run cmake command: cmake "$(MY_CMAKE_BUILD_DEFINE)" .."
if mkdir -p "$CMAKE_VS_DIR" &&
cd "$CMAKE_VS_DIR" &&
cmake $(MY_CMAKE_BUILD_DEFINE) ..; then
echo "CMake Project Loaded: MY_CMAKE_BUILD_DEFINE=$(MY_CMAKE_BUILD_DEFINE)"
else
echo "CMake Project Load Failed!"
exit 1
fi
}
function cmakeCleanAll() {
pushd $PROJECT_DIR
git clean -ffdx build
popd
}
function reloadCMakeProject() {
cmakeCleanAll && loadCMakeProject
}
function cmakeBuildAll() {
pushd "$CMAKE_VS_DIR"
if isWindows; then
if [[ -n "$CMAKE_BUILD_TYPE" ]]; then
export WIN_CMAKE_BUILD_DEFINE="$WIN_CMAKE_BUILD_DEFINE --config $CMAKE_BUILD_TYPE"
fi
echo start: cmake.exe --build . --target "$BUILD_TARGET" $WIN_CMAKE_BUILD_DEFINE -- /m
# ref: https://stackoverflow.com/questions/11865085/out-of-a-git-console-how-do-i-execute-a-batch-file-and-then-return-to-git-conso
if ! cmd "/C cmake.exe --build . --target "$BUILD_TARGET" $WIN_CMAKE_BUILD_DEFINE -- /m" && $EXIT_WHEN_FAILED; then
exit 1
fi
echo end: cmake.exe --build . --target "$BUILD_TARGET" $WIN_CMAKE_BUILD_DEFINE -- /m
else
if ! cmake --build . --target "$BUILD_TARGET" $(test -n "$CMAKE_BUILD_TYPE" && echo --config $CMAKE_BUILD_TYPE) -- -j $(nproc) && $EXIT_WHEN_FAILED; then
exit 1
fi
fi
popd
}
if [[ $# -eq 0 ]]; then
echo "usage: [--load] [--reload] [--clean] [--build]"
fi
while [[ $# > 0 ]]; do
PARSE_KEY="$1"
case "$PARSE_KEY" in
--load)
echo "loadCMakeProject"
loadCMakeProject
shift # past argument
;;
--reload)
echo "reloadCMakeProject"
reloadCMakeProject
shift # past argument
;;
--clean)
echo "clean"
cmakeCleanAll
shift # past argument
;;
--build)
echo "build"
if [[ ! -f "$CMAKE_VS_DIR/CMakeCache.txt" ]]; then
loadCMakeProject
fi
cmakeBuildAll
shift # past argument
;;
--debug)
echo "enable debug mode"
export CMAKE_BUILD_TYPE="Debug"
shift
;;
--release)
echo "enable release mode"
export CMAKE_BUILD_TYPE="Release"
shift # past argument
;;
--target)
echo "set build target to $2"
export BUILD_TARGET="$2"
shift
shift
;;
--toolset)
echo "set toolset to $2"
export CMAKE_CONFIG_DEFINE="$CMAKE_CONFIG_DEFINE -T $2"
shift
shift
;;
--arch)
echo "set arch to $2"
export CMAKE_CONFIG_DEFINE="$CMAKE_CONFIG_DEFINE -A $2"
shift
shift
;;
--run)
if isWindows; then
echo "run $CMAKE_VS_DIR/$CMAKE_BUILD_TYPE/$2"
"$CMAKE_VS_DIR/$CMAKE_BUILD_TYPE/$2"
else
echo run "$CMAKE_VS_DIR/$2"
if command -v wine64 &>/dev/null; then
wine64 "$CMAKE_VS_DIR/$2"
else
echo "Command 'wine64' not found, please install wine first."
fi
fi
shift
shift
;;
*)
echo "unknown option $PARSE_KEY..."
exit 1
;;
esac
done