-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplatform_profile
More file actions
399 lines (359 loc) · 11.6 KB
/
platform_profile
File metadata and controls
399 lines (359 loc) · 11.6 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/bin/bash
############################# Android 环境配置 #############################
if [[ -z $ANDROID_SDK_PATH ]]; then
export ANDROID_SDK_PATH="$HOME/Workspace/Android/sdk"
fi
export ANDROID_NDK_PATH="$ANDROID_SDK_PATH/ndk"
export ANDROID_NDK_VERSION_DEFAULT="22.1.7171670"
############################# HarmonyOS 环境配置 #############################
export HMOS_SDK_PATH="/Applications/DevEco-Studio.app/Contents/sdk/default"
# Android cmake
android_cmake() {
print_usage() {
echo "Usage:"
echo " android_cmake -h|--help"
echo " android_cmake [cmake-args]"
echo " android_cmake --ndk-version=21 --abi=arm64-v8a --api=21 --stl=c++_shared [other-cmake-args]"
echo " --ndk-version: ndk的版本号(可以只写主版本, 如22) 如果不指定,则默认为 \${ANDROID_NDK_VERSION_DEFAULT}, 如果指定的版本不存在,则返回错误"
echo " --abi: 指定abi,支持的值有 armeabi-v7a, arm64-v8a, x86-64, x86"
echo " --api: 指定api等级"
echo " --stl: 指定stl库,支持的值有 c++_shared, c++_static, system, none. 不指定默认为c++_shared"
}
local ndk_version abi api_level stl
local sum_args=$#
local other_args=()
for ((i=1; i<=sum_args; i++)); do
local arg=${!i}
case "$arg" in
-h|--help)
print_usage
return
;;
--ndk-version=*)
ndk_version=${arg#*=}
;;
--abi=*)
abi=${arg#*=}
;;
--api=*)
api_level=${arg#*=}
;;
--stl=*)
stl=${arg#*=}
;;
*)
other_args+=("$arg")
;;
esac
done
if [ -z "$abi" ] && [ -z "$api_level" ] && [ -z "$stl" ] && [ -z "$ndk_version" ]; then
cmake ${other_args[@]}
return $?
fi
if [ -z "$ndk_version" ]; then
ndk_version=$ANDROID_NDK_VERSION_DEFAULT
if [ -z "$ndk_version" ]; then
echo "ndk version is not specified!"
print_usage
return 1
fi
fi
case "$ndk_version" in
'26')
ndk_version="26.3.11579264"
;;
'25')
ndk_version="25.2.9519653"
;;
'24')
ndk_version="24.0.8215888"
;;
'23')
ndk_version="23.2.8568313"
;;
'22')
ndk_version="22.1.7171670"
;;
'21')
ndk_version="21.4.7075529"
;;
'20')
ndk_version="20.1.5948944"
;;
'19')
ndk_version="19.2.5345600"
;;
esac
case "$abi" in
'armeabi-v7a'|'arm64-v8a'|'x86-64'|'x86')
;;
*)
echo "unknown abi: $abi"
print_usage
return 1;
;;
esac
if [ -z "$api_level" ]; then
echo "api level is not specified!"
print_usage
return 1
fi
if [ -z "$stl" ]; then
stl="c++_shared"
fi
case "$stl" in
'c++_shared'|'c++_static'|'system'|'none')
;;
*)
echo "unknown stl: $stl"
print_usage
return 1;
;;
esac
local ndk_path="$ANDROID_NDK_PATH/$ndk_version"
if ! [ -d "$ndk_path" ]; then
echo "ndk path ($ndk_path) is not exits! pls check it!"
return
fi
local toolchain_file="$ndk_path/build/cmake/android.toolchain.cmake"
if ! [ -f "$toolchain_file" ]; then
echo "toolchain file ($toolchain_file) is not exits! pls check it!"
return 1
fi
echo "android cmake: version($ndk_version), abi($abi), api($api_level), stl($stl), other_args(${other_args[@]})"
cmake -DCMAKE_TOOLCHAIN_FILE=$toolchain_file \
-DANDROID_ABI=$abi \
-DANDROID_NDK=$ndk_path \
-DANDROID_NATIVE_API_LEVEL=android-$api_level \
-DANDROID_TOOLCHAIN=clang \
-DANDROID_STL=$stl ${other_args[@]} || return 1
return $?
}
# HarmonyOS cmake
hmos_cmake() {
print_usage() {
echo "Usage:"
echo " hmos_cmake --help|-h"
echo " hmos_cmake [cmake-args]"
echo " hmos_cmake --abi=armeabi-v7a|arm64-v8a|x86-64|x86 --stl=c++_shared|c++_static [cmake-args]"
echo " --abi: armeabi-v7a|arm64-v8a|x86-64|x86 必须指定"
echo " --stl: c++_shared, c++_static 默认为c++_shared"
}
if ! [ -d $HMOS_SDK_PATH ]; then
echo "HMOS_SDK_PATH not found: $HMOS_SDK_PATH"
return 1
fi
local abi stl build
local sum_args=$# other_args=()
for ((i=1; i<=$sum_args; i++)); do
eval "local arg=\${$i}"
case "$arg" in
-h|--help)
print_usage
return 1
;;
--abi=*)
abi=${arg#--abi=}
;;
--stl=*)
stl=${arg#--stl=}
;;
*)
other_args+=($arg)
;;
esac
done
# API 11 的路径
# local HMOS_NDK_PATH="$HMOS_SDK_PATH/base/native"
# API 12+ 路径
local HMOS_NDK_PATH="$HMOS_SDK_PATH/openharmony/native"
local HMOS_CMAKE_PATH="$HMOS_NDK_PATH/build-tools/cmake/bin"
local HMOS_CMAKE_TOOLCHAIN_FILE="$HMOS_NDK_PATH/build/cmake/ohos.toolchain.cmake"
# 使用 HarmonyOS 的 CMake
local mypath=${PATH/$HMOS_CMAKE_PATH:/}
if [[ "$mypath" = "$PATH" ]]; then
export PATH="$HMOS_CMAKE_PATH:$PATH"
fi
if [ -z "$abi" ] && [ -z "$stl" ]; then
cmake ${other_args[@]}
return $?
fi
if [ -z "$abi" ]; then
echo "Error: --abi=armeabi-v7a|arm64-v8a|x86-64|x86 必须指定"
print_usage
return 1
fi
if [ -z "$stl" ]; then
stl=c++_shared
fi
echo "hmos_cmake --abi=$abi --stl=$stl ${other_args[@]}"
#type cmake
#echo "toolchain file: $HMOS_CMAKE_TOOLCHAIN_FILE"
cmake -DCMAKE_TOOLCHAIN_FILE=$HMOS_CMAKE_TOOLCHAIN_FILE \
-DOHOS_STL=$stl \
-DOHOS_ARCH=$abi \
-DOHOS_PLATFORM=OHOS ${other_args[@]} || return 1
return $?
}
# build and install
native_build() {
print_usage() {
echo "Usage:"
echo " native_build -h|--help"
echo " native_build --os=android --abi=armeabi-v7a --api=21 --stl=c++_shared --source=/path/to/source [other-cmake-args]"
echo " native_build --os=android --abi=arm64-v8a --source=/path/to/source --clean"
echo " --os: android, harmonyos, 如果没有指定,则默认为localos,即本地系统"
echo " --abi: armeabi-v7a, arm64-v8a, x86, x86_64 必须指定"
echo " --api: android api level 只有在os为android时有效,如果没有指定,则默认为21"
echo " --ndk-version: android ndk version 只有在os为android时有效,只需要指定主版本号即可,如果没有指定,则默认为ANDROID_NDK_VERSION_DEFAULT"
echo " --stl: c++_shared, c++_static, 只有在os为android和harmonyos时有效,如果没有指定,则默认为c++_shared"
echo " --source: 源码地址,只可以是本地路径, 如果没有指定,则默认为当前路径"
echo " [other-cmake-args]: 其他的cmake参数"
echo " --clean: 清除编译目录"
echo "编译目录为 source/build/{os}/{abi}"
echo "安装目录为 source/build_install/{os}/{abi}"
}
local os='localos' abi api ndk_version stl source_dir
local sum_args=$#
local other_args=() clean=false
for (( i = 1; i <= sum_args; i++ )); do
local arg=${!i}
case "$arg" in
-h|--help)
print_usage
return
;;
--os=*)
os=${arg#--os=}
;;
--abi=*)
abi=${arg#--abi=}
;;
--api=*)
api=${arg#--api=}
;;
--source=*)
source_dir="${arg#--source=}"
;;
--ndk-version=*)
ndk_version="${arg#--ndk-version=}"
;;
--stl=*)
stl="${arg#--stl=}"
;;
--clean)
clean=true
;;
*)
other_args+=("$arg")
;;
esac
done
# check os
if [ "$os" != 'android' ] && [ "$os" != 'harmonyos' ] && [ "$os" != 'localos' ]; then
echo "OS($os) is not supported"
print_usage
return 1
fi
if [ "$os" = 'localos' ]; then
echo "build for local os"
fi
# check abi
if [ -z $abi ]; then
echo "--abi is required"
print_usage
return 1
fi
case $abi in
armeabi-v7a|arm64-v8a|x86|x86_64)
;;
*)
echo "unsupported abi: $abi"
print_usage
return 1
;;
esac
# check source
if [ -z $source_dir ]; then
source_dir=$(pwd)
fi
if ! [ -d $source_dir ]; then
echo "source($source) is not a directory"
print_usage
return 1
fi
# build dir
local build_dir="$source_dir/zzz_build/$os/$abi"
[ -d $build_dir ] || mkdir -p $build_dir
if [ "$clean" = true ]; then
rm -rf $build_dir
echo "clean $build_dir"
return
fi
local install_dir="$source_dir/zzz_build_install/$os/$abi"
cd $build_dir
local build_args=("--abi=$abi" "-DCMAKE_BUILD_TYPE=Release")
[ -z "$stl" ] || build_args+=("--stl=$stl")
if [ $os = 'android' ]; then
[ -z "$api" ] && api=21
build_args+=("--api=$api")
[ -z "$ndk_version" ] || build_args+=("--ndk-version=$ndk_version")
android_cmake ${build_args[@]} ${other_args[@]} "$source_dir" && \
android_cmake --build . && \
android_cmake --install . --prefix "$install_dir"
elif [ $os = 'harmonyos' ]; then
hmos_cmake ${build_args[@]} ${other_args[@]} "$source_dir" && \
hmos_cmake --build . && \
hmos_cmake --install . --prefix "$install_dir"
else
cmake ${other_args[@]} "$source_dir" && \
cmake --build . && \
cmake --install . --prefix "$install_dir"
fi
return $?
}
# 解析参数,用于自定义脚本时,可以判断系统,架构等等
# 解析后的参数都会存放在 ALL_BUILD_ARGS=() 中
# 单个的参数会以 BUILD_ARG_ 开头存放在对应的变量中, 具体有
export BUILD_ARG_SOURCE_DIR
export BUILD_ARG_ABI
export BUILD_ARG_API
export BUILD_ARG_OS
export BUILD_ARG_NDK_VERSION
export BUILD_ARG_STL
export ALL_BUILD_ARGS=()
parse_arguments() {
for arg in "$@"; do
case "$arg" in
--os=*)
BUILD_ARG_OS=${arg#--os=}
ALL_BUILD_ARGS+=("$arg")
;;
--abi=*)
BUILD_ARG_ABI=${arg#--abi=}
ALL_BUILD_ARGS+=("$arg")
;;
--api=*)
BUILD_ARG_API=${arg#--api=}
ALL_BUILD_ARGS+=("$arg")
;;
--ndk-version=*)
BUILD_ARG_NDK_VERSION="${arg#--ndk-version=}"
ALL_BUILD_ARGS+=("$arg")
;;
--stl=*)
BUILD_ARG_STL="${arg#--stl=}"
ALL_BUILD_ARGS+=("$arg")
;;
--source=*)
# 获取绝对路径
local rel_path="${arg#--source=}"
BUILD_ARG_SOURCE_DIR="$(cd $rel_path && pwd)"
ALL_BUILD_ARGS+=("--source=$USER_SOURCE_DIR")
;;
*)
ALL_BUILD_ARGS+=("$arg")
;;
esac
done
}