forked from soumith/torch-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·220 lines (179 loc) · 7.14 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
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
#!/bin/bash
# have ndk-build in your $PATH and the script figures out where your ANDROID_NDK is at.
# optionally, modify the variables below as needed.
NDKABI=21
# Default architecture is V8
ARCH=${ARCH:-"v8"}
# Default is to build with CUDA.
# Make sure you installed https://developer.nvidia.com/codeworks-android.
# Otherwise, set WITH_CUDA to OFF.
WITH_CUDA=${WITH_CUDA:-"OFF"}
if [[ "$ARCH" == "v8" ]]; then
APP_ABI=arm64-v8a
M_ARCH=-march=armv8-a
ABI_NAME=aarch64-linux-androideabi
COMPUTE_NAME="Maxwell+Tegra"
elif [[ "$ARCH" == "v7n" ]]; then
APP_ABI="armeabi-v7a with NEON"
M_ARCH="-march=armv7-a"
ABI_NAME=armv7-linux-androideabi
COMPUTE_NAME="Kepler+Tegra Maxwell+Tegra"
LOCAL_ARM_NEON=true
ARCH_ARM_HAVE_NEON=true
else
echo "Unsupported Architecture: $ARCH"
exit 1
fi
NVCC=`which nvcc`
export MAKE=make
export MAKEARGS=-j$(getconf _NPROCESSORS_ONLN)
####################################################
# You do not need to modify anything below this line
####################################################
# find system torch, if not found, install it
command -v th >/dev/null 2>&1
TORCHINSTALLCHECK=$?
if [ $TORCHINSTALLCHECK -ne 0 ]; then
echo "Torch-7 not found on system. Please install it using instructions from http://torch.ch"
exit -1
fi
# have ndk-build in your PATH and the script figures out where your ANDROID_NDK is at
unamestr=`uname`
ndkbuildloc=`which ndk-build`
if [[ "$?" == 1 ]]; then
echo "Error: Cannot find ndk-build in PATH. Please add it to PATH environment variable"
exit 1
fi
if [[ "$unamestr" == 'Linux' ]]; then
export ANDROID_NDK=`readlink -f $ndkbuildloc|sed 's/ndk-exec.sh//'|sed 's/ndk-build//'`
elif [[ "$unamestr" == 'Darwin' ]]; then
which greadlink >/dev/null 2>&1
if [[ "$?" == 1 ]]; then
brew install coreutils
fi
export ANDROID_NDK=`greadlink -f $ndkbuildloc|sed 's/ndk-exec.sh//'|sed 's/ndk-build//'`
fi
echo "Android NDK found at: $ANDROID_NDK"
cd "$(dirname "$0")" # switch to script directory
SCRIPT_ROOT_DIR=`pwd`
export INSTALL_DIR=$SCRIPT_ROOT_DIR/install
mkdir -p ${INSTALL_DIR}/man
echo "INSTALL_DIR=${INSTALL_DIR}"
set +e # hard errors
export CMAKE_INSTALL_SUBDIR="share/cmake/torch"
NDK=$ANDROID_NDK
TOOLCHAIN_VERSION=4.9
if [[ "$ARCH" == "v8" ]]; then
NDK_SYSROOT=$NDK/platforms/android-$NDKABI/arch-arm64
export HOST=aarch64-linux-android
HOST_CC="gcc"
else
export HOST=arm-linux-androideabi
NDK_SYSROOT=$NDK/platforms/android-$NDKABI/arch-arm
ANDROID_CFLAGS="-mfloat-abi=softfp -fprefetch-loop-arrays"
HOST_CC="gcc -m32"
fi
ANDROID_CFLAGS="${M_ARCH} --sysroot ${NDK_SYSROOT} ${ANDROID_CFLAGS}"
if [[ "$unamestr" == 'Linux' ]]; then
BUILD_PLATFORM=linux-x86_64
elif [[ "$unamestr" == 'Darwin' ]]; then
BUILD_PLATFORM=darwin-x86_64
fi
export TOOLCHAIN="$NDK/toolchains/${HOST}-${TOOLCHAIN_VERSION}/prebuilt/${BUILD_PLATFORM}"
export TORCH_CUDA_ARCH_LIST="${COMPUTE_NAME}"
#-DANDROID_STANDALONE_TOOLCHAIN="${SCRIPT_ROOT_DIR}/standalone-toochains" \
do_cmake_config() {
cmake $1 -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_TOOLCHAIN_FILE="$SCRIPT_ROOT_DIR/cmake/android.toolchain.cmake" \
-DANDROID_NDK="${ANDROID_NDK}" -DANDROID_ABI="${APP_ABI}" \
-DWITH_CUDA=${WITH_CUDA} -DWITH_LUAROCKS=OFF -DWITH_LUAJIT21=ON\
-DCUDA_USE_STATIC_CUDA_RUNTIME=OFF -DANDROID_STL_FORCE_FEATURES=OFF\
-DANDROID_NATIVE_API_LEVEL="${NDKABI}" -DANDROID_STL=gnustl_shared\
-DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" -DCMAKE_INSTALL_SUBDIR="${CMAKE_INSTALL_SUBDIR}" \
-DLIBRARY_OUTPUT_PATH_ROOT="${INSTALL_DIR}" \
-DLUAJIT_SYSTEM_MINILUA="$SCRIPT_ROOT_DIR/distro/exe/luajit-rocks/luajit-2.1/src/host/minilua" \
-DLUAJIT_SYSTEM_BUILDVM="$SCRIPT_ROOT_DIR/distro/exe/luajit-rocks/luajit-2.1/src/host/buildvm" \
-DCMAKE_C_FLAGS="-DDISABLE_POSIX_MEMALIGN"
echo " -------------- Configuring DONE ---------------"
}
if [[ "$WITH_CUDA" == "ON" ]]; then
echo "Found CUDA on your machine. Installing CMake 3.6 modules to get up-to-date FindCUDA"
cd ${SCRIPT_ROOT_DIR}/distro/cmake/3.6 && \
(cmake -E make_directory build && cd build && cmake .. -DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
&& make install) && echo "FindCuda bits of CMake 3.6 installed" || exit 1
fi
cd $SCRIPT_ROOT_DIR
(cmake -E make_directory build && cd build && do_cmake_config ..) || exit 1
echo "Start to compile OpenBLAS"
cd external/OpenBLAS
NDK_ROOT=$ANDROID_NDK
echo "NDK_ROOT=$NDK_ROOT, APP_ABI=$APP_ABI"
echo $PATH
PATHI=$PATH
target="ARMV8 BINARY=64"
arch="arch-arm64"
CCFolder="aarch64-linux-android-4.9"
#CC="aarch64-linux-android-gcc FC=aarch64-linux-android-gfortran"
CC="aarch64-linux-android-gcc"
ANDROID_ABI="android-21"
export PATH=${PATHI}:/home/huyaonan/arm64-toolchain/bin
echo $PATH
#command="make TARGET=${target} HOSTCC=gcc CC=${CC} USE_THREAD=1 USE_OPENMP=1 USE_LAPACK=1 CFLAGS=--sysroot=${NDK_ROOT}/platforms/${ANDROID_ABI}/${arch} libs netlib"
command="make TARGET=${target} HOSTCC=gcc CC=${CC} USE_THREAD=1 USE_OPENMP=1 NOFORTRAN=1 libs"
echo $command
$command
command="make PREFIX=${INSTALL_DIR} install"
$command
echo "End compile OpenBLAS"
cd $SCRIPT_ROOT_DIR
cd external/libpng && \
(cmake -E make_directory build && cd build && do_cmake_config .. && make install) \
&& echo "libpng installed" || exit 1
cd $SCRIPT_ROOT_DIR
#cd external/libjpeg-turbo && \
# (cmake -E make_directory build && cd build && . ../../build-libjpeg.sh) \
# && echo "libjpeg installed" || exit 1
#cd $SCRIPT_ROOT_DIR
# Build host luajit for minilua and buildvm
cd distro/exe/luajit-rocks/luajit-2.1
# make clean
$MAKE $MAKEARGS HOST_CC="$HOST_CC" CC="gcc" HOST_SYS=$unamestr TARGET_SYS=Linux CROSS="${TOOLCHAIN}/bin/${HOST}-" TARGET_FLAGS="$ANDROID_CFLAGS"
echo "Done installing Lua"
if [[ "$ARCH" == "v7" ]]; then
cd $SCRIPT_ROOT_DIR
(cmake -E make_directory build && cd build && do_cmake_config ..) || exit 1
echo "Start to compile OpenBLAS"
cd external/OpenBLAS
NDK_ROOT=$ANDROID_NDK
echo "NDK_ROOT=$NDK_ROOT, APP_ABI=$APP_ABI"
echo $PATH
PATHI=$PATH
target="ARMV8 BINARY=64"
arch="arch-arm64"
CCFolder="aarch64-linux-android-4.9"
CC="aarch64-linux-android-gcc FC=aarch64-linux-android-gfortran"
ANDROID_ABI="android-21"
export PATH=${PATHI}:/home/huyaonan/arm64-toolchain/bin
echo $PATH
#command="make TARGET=${target} HOSTCC=gcc CC=${CC} USE_THREAD=1 USE_OPENMP=1 USE_LAPACK=1 CFLAGS=--sysroot=${NDK_ROOT}/platforms/${ANDROID_ABI}/${arch} libs netlib"
command="make TARGET=${target} HOSTCC=gcc CC=${CC} USE_THREAD=1 USE_OPENMP=1 USE_LAPACK=1 libs netlib"
echo $command
$command
command="make PREFIX=${INSTALL_DIR} install"
$command
echo "End compile OpenBLAS"
fi
cd $SCRIPT_ROOT_DIR
(cmake -E make_directory build && cd build && do_cmake_config ..) || exit 1
cd build
(cd distro/exe && $MAKE $MAKEARGS install) || exit 1
# cwrap needs to be there first
(cd distro/pkg/cwrap && $MAKE $MAKEARGS install) || exit 1
(cd distro/pkg && $MAKE $MAKEARGS install) || exit 1
# Cutorch installs some headers/libs used by other modules in extra
if [[ "$WITH_CUDA" == "ON" ]]; then
(cd distro/extra/cutorch && $MAKE $MAKEARGS install) || exit 1
fi
(cd distro/extra && $MAKE $MAKEARGS install) || exit 1
(cd src && $MAKE $MAKEARGS install) || exit 1
cd ..
echo "done"