-
Notifications
You must be signed in to change notification settings - Fork 2
/
dlib-android-setup.ps1
187 lines (149 loc) · 6.12 KB
/
dlib-android-setup.ps1
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
# Author: Luca Anzalone
# Modified by Asif Ahmed to fix ninja and path error, opencv is not used here
# MUST CHANGE $CMAKE_BIN_PATH, $NDK, $PROJECT_PATH, $DLIB_PATH BASED ON NEEDS
# -----------------------------------------------------------------------------
# -- DLIB FOR ANDROID: setup script
# -----------------------------------------------------------------------------
# Android-cmake path: REPLACE WITH YOUR CMAKE PATH!
$CMAKE_BIN_PATH = 'C:\Users\computer\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin'
$AndroidCmake = "$CMAKE_BIN_PATH\cmake.exe"
# Android-ndk path: REPLACE WITH YOUR NDK PATH!
if (Get-Variable 'ANDROID_NDK' -Scope Global -ErrorAction 'Ignore') {
$NDK = $ANDROID_NDK
} else {
$NDK = 'C:\Users\computer\AppData\Local\Android\Sdk\ndk\21.1.6352462'
}
$TOOLCHAIN = "$NDK\build\cmake\android.toolchain.cmake"
# Supported Android ABI: TAKE ONLY WHAT YOU NEED!
$ABIs = 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
# path to stripper tool: REPLACE WITH YOURS, ACCORDING TO OS!!
$STRIPPER_PATH = "$NDK\toolchains\llvm\prebuilt\windows-x86_64\bin"
$STRIPPERS = @{
'armeabi-v7a' = "$STRIPPER_PATH\arm-linux-androideabi-strip.exe";
'arm64-v8a' = "$STRIPPER_PATH\aarch64-linux-android-strip.exe";
'x86' = "$STRIPPER_PATH\x86_64-linux-android-strip.exe" ;
'x86_64' = "$STRIPPER_PATH\x86_64-linux-android-strip.exe" ;
}
# Minimum supported sdk: SHOULD BE GREATER THAN 16
$MIN_SDK = 24
# Android project path: REPLACE WITH YOUR PROJECT PATH!
$PROJECT_PATH = 'C:\Users\computer\AndroidStudioProjects\YourAnroidProjectName'
# Directory for storing native libraries
$NATIVE_DIR = "$PROJECT_PATH\app\src\main\cppLibs"
# -----------------------------------------------------------------------------
# -- Utils
# -----------------------------------------------------------------------------
function Make-Dir {
if (-Not (Test-Path $args[0])) {
New-Item -ItemType directory -Path $args[0] > $null
}
}
function Copy-Directory ($from, $to, $tab) {
$Items = Get-ChildItem $from
ForEach ($item in $Items) {
if ((Get-Item $item.FullName) -is [System.IO.DirectoryInfo]) {
Write-Host "$tab > $item"
Make-Dir "$to\$item"
Copy-Directory "$from\$item" "$to\$item" "$tab "
} else {
Copy-Item -Path $item.FullName -Destination $to
Write-Host "$tab | $item copied."
}
}
}
# -----------------------------------------------------------------------------
# -- Dlib
# -----------------------------------------------------------------------------
# Dlib library path:
$DLIB_PATH = 'C:\dlib\dlib-master\dlib'
function Compile-Dlib {
Set-Location $DLIB_PATH
Make-Dir 'build'
Write-Host '=> Compiling Dlib...'
sleep 0.5
ForEach ($abi in $ABIs) {
Write-Host
Write-Host "=> Compiling Dlib for ABI: '$abi'..."
sleep 0.5
Make-Dir "build\$abi"
Set-Location "build\$abi"
$cmakeArguments = @(
"-DBUILD_SHARED_LIBS=1",
"-DANDROID_NDK=$NDK",
"-DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN",
"-GNinja"
"-DCMAKE_MAKE_PROGRAM=$CMAKE_BIN_PATH\ninja.exe",
"-DCMAKE_BUILD_TYPE=Release",
"-DCMAKE_CXX_FLAGS=-std=c++11 -frtti -fexceptions",
"-DCMAKE_C_FLAGS=-O3",
"-DANDROID_ABI=$abi",
"-DANDROID_PLATFORM=android-$MIN_SDK",
"-DANDROID_TOOLCHAIN=clang",
"-DANDROID_STL=c++_shared",
"-DANDROID_CPP_FEATURES=rtti exceptions",
"-DCMAKE_PREFIX_PATH=..\..\",
"..\..\"
)
$stripArguments = @(
"--strip-unneeded",
"dlib/libdlib.so"
)
& $AndroidCmake $cmakeArguments
sleep 0.5
Write-Host "=> Generating the 'dlib/libdlib.so' for ABI: '$abi'..."
& $AndroidCmake --build .
sleep 0.5
Write-Host "=> Stripping libdlib.so for ABI: '$abi'to reduce space..."
& $STRIPPERS[$abi] $stripArguments
sleep 0.5
Write-Host '=> done.'
Set-Location ..\..\
sleep 0.5
}
}
function Dlib-Setup {
Write-Host '=> Making directories for Dlib...'
Make-Dir "$NATIVE_DIR\dlib"
Write-Host "=> '$NATIVE_DIR\dlib' created."
Make-Dir "$NATIVE_DIR\dlib\lib"
Write-Host "=> '$NATIVE_DIR\dlib\lib' created."
Make-Dir "$NATIVE_DIR\dlib\include"
Write-Host "=> '$NATIVE_DIR\dlib\include' created."
Make-Dir "$NATIVE_DIR\dlib\include\dlib"
Write-Host "=> '$NATIVE_DIR\dlib\include\dlib' created."
Write-Host "=> Copying Dlib headers..."
Copy-Directory "$DLIB_PATH" "$NATIVE_DIR\dlib\include\dlib" ''
Write-Host "=> Copying 'libdlib.so' for each ABI..."
ForEach ($abi in $ABIs) {
Make-Dir "$NATIVE_DIR\dlib\lib\$abi"
Copy-Item -Path "$DLIB_PATH\build\$abi\libdlib.so" -Destination "$NATIVE_DIR\dlib\lib\$abi"
Write-Host " > Copied libdlib.so for $abi"
}
}
# COMMENT TO DISABLE COMPILATION
Compile-Dlib
# -----------------------------------------------------------------------------
# -- OpenCV: Not used in this
# -----------------------------------------------------------------------------
# OpenCV library path: REPLACE WITH YOUR OPENCV PATH!
#$OPENCV_PATH='path-to-your\opencv-4.0.1-android-sdk\sdk\native'
#function Opencv-Setup {
# Make-Dir "$NATIVE_DIR\opencv"
#
# Write-Host "=> Copying 'libopencv_java4.so' for each ABI..."
# ForEach ($abi in $ABIs) {
# Make-Dir "$NATIVE_DIR\opencv\$abi"
# Copy-Item -Path "$OPENCV_PATH\libs\$abi\libopencv_java4.so" -Destination "$NATIVE_DIR\opencv\$abi"
# Write-Host " > Copied libopencv_java4.so for $abi"
# }
#}
# -----------------------------------------------------------------------------
# -- Project setup
# -----------------------------------------------------------------------------
Make-Dir $NATIVE_DIR
# COMMENT TO NOT COPY DLIB '.so' FILES
Dlib-Setup
# COMMENT TO NOT COPY OPENCV '.so' FILES
#Opencv-Setup
Write-Host "=> Project configuration completed."
# -----------------------------------------------------------------------------