-
Notifications
You must be signed in to change notification settings - Fork 14
/
setup.ps1
182 lines (144 loc) · 5.76 KB
/
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
# Luca Anzalone
# -----------------------------------------------------------------------------
# -- DLIB FOR ANDROID: setup script
# -----------------------------------------------------------------------------
# Android-cmake path: REPLACE WITH YOUR CMAKE PATH!
$AndroidCmake = 'your-path-to\Android\sdk\cmake\3.10.2.4988404\bin\cmake.exe'
# Android-ndk path: REPLACE WITH YOUR NDK PATH!
if (Get-Variable 'ANDROID_NDK' -Scope Global -ErrorAction 'Ignore') {
$NDK = $ANDROID_NDK
} else {
$NDK = 'your-path-to\Android\sdk\ndk-bundle'
}
$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 = 16
# Android project path: REPLACE WITH YOUR PROJECT PATH!
$PROJECT_PATH = 'your-path-to\android\project'
# 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 = '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_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\dlib" "$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\dlib\libdlib.so" -Destination "$NATIVE_DIR\dlib\lib\$abi"
Write-Host " > Copied libdlib.so for $abi"
}
}
# COMMENT TO DISABLE COMPILATION
Compile-Dlib
# -----------------------------------------------------------------------------
# -- OpenCV
# -----------------------------------------------------------------------------
# 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."
# -----------------------------------------------------------------------------