-
Notifications
You must be signed in to change notification settings - Fork 13
/
build-onnxruntime-mac.sh
163 lines (143 loc) · 4.38 KB
/
build-onnxruntime-mac.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
#!/bin/bash
# build onnxruntime by benjaminwan
# CMakeFiles/onnxruntime.dir/link.txt/link/lib*.a
function is_cmd_exist() {
retval=""
if ! command -v $1 >/dev/null 2>&1; then
retval="false"
else
retval="true"
fi
echo "$retval"
}
function collect_shared_lib() {
if [ -d "install/bin" ]; then
rm -r -f install/bin
fi
if [ -d "install/include/onnxruntime" ]; then
mv install/include/onnxruntime/* install/include
rm -rf install/include/onnxruntime
fi
echo "set(OnnxRuntime_INCLUDE_DIRS \"\${CMAKE_CURRENT_LIST_DIR}/include\")" >install/OnnxRuntimeConfig.cmake
echo "include_directories(\${OnnxRuntime_INCLUDE_DIRS})" >>install/OnnxRuntimeConfig.cmake
echo "link_directories(\${CMAKE_CURRENT_LIST_DIR}/lib)" >>install/OnnxRuntimeConfig.cmake
echo "set(OnnxRuntime_LIBS onnxruntime)" >>install/OnnxRuntimeConfig.cmake
}
function copy_libs() {
all_link=$(cat CMakeFiles/onnxruntime.dir/link.txt)
link=${all_link#*onnxruntime.dir}
regex="lib.*.a$"
libs=""
for var in $link; do
if [[ ${var} =~ ${regex} ]]; then
#echo cp ${var} install-static/lib
cp ${var} install-static/lib
name=$(echo $var | grep -E ${regex} -o)
name=${name#lib}
name=${name%.a}
libs="${libs} ${name}"
fi
done
echo "$libs"
}
function combine_libs_darwin() {
all_link=$(cat CMakeFiles/onnxruntime.dir/link.txt)
link=${all_link#*onnxruntime.dir}
regex="lib.*.a$"
root_path="${PWD}"
lib_path="${PWD}/install-static/lib"
mkdir -p $lib_path
libs=""
for var in $link; do
if [[ ${var} =~ ${regex} ]]; then
libs="${libs} ${root_path}/${var}"
fi
done
libtool -static -o ${lib_path}/libonnxruntime.a ${libs}
}
function collect_static_libs() {
if [ -d "install-static" ]; then
rm -r -f install-static
fi
mkdir -p install-static/lib
if [ -d "install/include" ]; then
cp -r install/include install-static
fi
if [ ! -f "CMakeFiles/onnxruntime.dir/link.txt" ]; then
echo "link.txt is not exist, collect static libs error."
exit 0
fi
libtool_exist=$(is_cmd_exist libtool)
if [ "$libtool_exist" == "true" ]; then
echo "combine_libs_darwin"
combine_libs_darwin
libs="onnxruntime"
else
echo "copy_libs"
libs=$(copy_libs)
fi
echo "set(OnnxRuntime_INCLUDE_DIRS \"\${CMAKE_CURRENT_LIST_DIR}/include\")" >install-static/OnnxRuntimeConfig.cmake
echo "include_directories(\${OnnxRuntime_INCLUDE_DIRS})" >>install-static/OnnxRuntimeConfig.cmake
echo "link_directories(\${CMAKE_CURRENT_LIST_DIR}/lib)" >>install-static/OnnxRuntimeConfig.cmake
echo "set(OnnxRuntime_LIBS $libs)" >>install-static/OnnxRuntimeConfig.cmake
cp CMakeFiles/onnxruntime.dir/link.txt install-static/link.log
}
HOST_OS=$(uname -s)
NUM_THREADS=1
BUILD_TYPE=Release
if [ $HOST_OS == "Darwin" ]; then
NUM_THREADS=$(sysctl -n hw.ncpu)
else
echo "Unsupported OS: $HOST_OS"
exit 0
fi
JAVA_FLAG=""
while getopts "n:j" arg; do
case $arg in
n)
echo "n's arg:$OPTARG"
TARGET_ARCH=$OPTARG
;;
j)
echo "j's arg:$OPTARG"
JAVA_FLAG="--build_java"
;;
?)
echo -e "unknown argument."
echo -e "usage 1: ./build-onnxruntim-mac.bat -n x86_64"
echo -e "usage 2: ./build-onnxruntim-mac.bat -n arm64"
exit 1
;;
esac
done
if [ "$TARGET_ARCH" != "x86_64" ] && [ "$TARGET_ARCH" != "arm64" ] && [ "$TARGET_ARCH" != "arm64e" ]; then
echo "Unsupported TARGET_ARCH:$TARGET_ARCH"
exit 0
fi
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
python3 $DIR/tools/ci_build/build.py --build_dir $DIR/build-$HOST_OS-$TARGET_ARCH \
--allow_running_as_root \
--config $BUILD_TYPE \
--parallel "$NUM_THREADS" \
--skip_tests \
--build_shared_lib \
--compile_no_warning_as_error \
--osx_arch $TARGET_ARCH \
--apple_deploy_target "10.15" \
$JAVA_FLAG \
--cmake_extra_defines CMAKE_INSTALL_PREFIX=./install \
onnxruntime_BUILD_UNIT_TESTS=OFF \
CMAKE_OSX_DEPLOYMENT_TARGET="10.15"
if [ ! -d "build-$HOST_OS-$TARGET_ARCH/$BUILD_TYPE" ]; then
echo "Build error!"
exit 0
fi
pushd build-$HOST_OS-$TARGET_ARCH/$BUILD_TYPE
cmake --install .
if [ ! -d "install" ]; then
echo "Cmake install error!"
exit 0
fi
collect_shared_lib
collect_static_libs
popd