forked from seladb/PcapPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure-android.sh
executable file
·217 lines (164 loc) · 6.54 KB
/
configure-android.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
#!/bin/bash
set -e
SCRIPT_FILEPATH=$0
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
COLOR_RED='\033[0;31m' # Red
COLOR_GREEN='\033[0;32m' # Green
COLOR_PURPLE='\033[0;35m' # Purple
COLOR_OFF='\033[0m' # Reset
exit_with_error() {
printf '%b\n' "${COLOR_RED}$*${COLOR_OFF}" >&2
exit 1
}
is_integer () {
case "${1#[+-]}" in
(*[!0123456789]*) return 1 ;;
('') return 1 ;;
(*) return 0 ;;
esac
}
#########################################################################################
help() {
printf '%b\n' "
${COLOR_PURPLE}Usage:${COLOR_OFF}
${COLOR_GREEN}$SCRIPT_FILEPATH${COLOR_OFF}
${COLOR_GREEN}$SCRIPT_FILEPATH -h${COLOR_OFF}
${COLOR_GREEN}$SCRIPT_FILEPATH --help${COLOR_OFF}
show help of this script.
${COLOR_GREEN}$SCRIPT_FILEPATH <OPTION>...${COLOR_OFF}
${COLOR_GREEN}--ndk-path <ANDROID-NDK-ROOT>${COLOR_OFF}
specify the root path of Android NDK, for example: '/opt/Android/Sdk/ndk/23.1.7779620'
${COLOR_GREEN}--target <ANDROID-ABI>${COLOR_OFF}
specify the android abi, value must be one of 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'.
${COLOR_GREEN}--api <ANDROID-API-LEVEL>${COLOR_OFF}
specify the android api level. value must be between 21 and 30. If not provided, the default value is 29.
${COLOR_GREEN}--libpcap-include-dir <DIR>${COLOR_OFF}
specify the libpcap header files directory
${COLOR_GREEN}--libpcap-lib-dir <DIR>${COLOR_OFF}
specify the libpcap pre compiled lib directory. Please make sure libpcap was compiled with the same architecture and API level
${COLOR_PURPLE}Examples:${COLOR_OFF}
${COLOR_GREEN}./configure-android.sh${COLOR_OFF}
${COLOR_GREEN}./configure-android.sh -h${COLOR_OFF}
${COLOR_GREEN}./configure-android.sh --help${COLOR_OFF}
${COLOR_GREEN}./configure-android.sh --ndk-path /opt/Android/sdk/ndk/23.1.7779620 --target armeabi-v7a --api 21 --libpcap-include-dir ~/libpcap/armeabi-v7a/include --libpcap-lib-dir ~/libpcap/armeabi-v7a/lib${COLOR_OFF}
"
}
#########################################################################################
printf '%b\n' "${COLOR_PURPLE}
******************************************
PcapPlusPlus Android configuration script
******************************************
${COLOR_OFF}"
case $1 in
''|-h|--help) help; exit
esac
unset ANDROID_NDK_ROOT
unset ANDROID_API_LEVEL
unset TARGET_TRIPLE
unset LIBPCAP_INCLUDE_DIR
unset LIBPCAP_LIBRARY_DIR
while [ -n "$1" ]
do
case "$1" in
--ndk-path)
if [ -d "$2" ] ; then
ANDROID_NDK_ROOT=$2
shift 2
else
exit_with_error "--ndk-path PATH, PATH is not specified."
fi
;;
--target)
case $2 in
armeabi-v7a) TARGET_TRIPLE='armv7a-linux-androideabi' ;;
arm64-v8a) TARGET_TRIPLE='aarch64-linux-android' ;;
x86) TARGET_TRIPLE='i686-linux-android' ;;
x86_64) TARGET_TRIPLE='x86_64-linux-android' ;;
*) exit_with_error "--target TARGET, TARGET must be one of 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'"
esac
shift 2
;;
--api)
is_integer "$2" || exit_with_error "--api API, API must be a integer."
if [ "$2" -ge 21 ] && [ "$2" -le 30 ] ; then
ANDROID_API_LEVEL=$2
shift 2
else
exit_with_error "--api API, API must be between 21 and 30."
fi
;;
--libpcap-include-dir)
LIBPCAP_INCLUDE_DIR=$2
shift 2
;;
--libpcap-lib-dir)
LIBPCAP_LIBRARY_DIR=$2
shift 2
;;
*) exit_with_error "Unrecognized option: $1"
esac
done
if [ -z "$ANDROID_NDK_ROOT" ] ; then
exit_with_error "Please specify the Android NDK root path via --ndk-path PATH"
fi
if [ -z "$ANDROID_API_LEVEL" ] ; then
ANDROID_API_LEVEL=29
fi
if [ -z "$TARGET_TRIPLE" ] ; then
exit_with_error "Please specify the target via --target TARGET"
fi
if [ -z "$LIBPCAP_INCLUDE_DIR" ]; then
exit_with_error "Please specify the directory of libpcap header files via --libpcap-include-dir DIR"
fi
if [ -z "$LIBPCAP_LIBRARY_DIR" ]; then
exit_with_error "Please specify the directory of libpcap binary that matches the specified target via --libpcap-lib-dir DIR"
fi
#########################################################################################
unset BUILD_MACHINE_OS_TYPE
unset BUILD_MACHINE_OS_ARCH
BUILD_MACHINE_OS_TYPE=$(uname | tr A-Z a-z)
BUILD_MACHINE_OS_ARCH=$(uname -m)
ANDROID_TOOLCHAIN=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/$BUILD_MACHINE_OS_TYPE-$BUILD_MACHINE_OS_ARCH
#########################################################################################
cat <<EOF
Build machine OS type: $BUILD_MACHINE_OS_TYPE
Build machine OS arch: $BUILD_MACHINE_OS_ARCH
Android NDK root: $ANDROID_NDK_ROOT
Anrdoid API level: $ANDROID_API_LEVEL
Android toolchain path: $ANDROID_TOOLCHAIN
Target triple: $TARGET_TRIPLE
libpcap include dir: $LIBPCAP_INCLUDE_DIR
libpcap library dir: $LIBPCAP_LIBRARY_DIR
EOF
#########################################################################################
unset TEMP_DIR
unset TEMP_CONFIG_FILE
TEMP_DIR=$(mktemp -d)
TEMP_CONFIG_FILE="$TEMP_DIR/config.mk"
cat > "$TEMP_CONFIG_FILE" <<EOF
PCAPPLUSPLUS_HOME := $SCRIPT_DIR
ANDROID_TARGET := $TARGET_TRIPLE$ANDROID_API_LEVEL
ANDROID_TOOLCHAIN := $ANDROID_TOOLCHAIN
EOF
#########################################################################################
PCAPPLUSPLUS_MK="$SCRIPT_DIR/mk/PcapPlusPlus.mk"
cp -f "$TEMP_CONFIG_FILE" "$PCAPPLUSPLUS_MK"
cat "$SCRIPT_DIR/mk/PcapPlusPlus.mk.common" >> "$PCAPPLUSPLUS_MK"
cat "$SCRIPT_DIR/mk/PcapPlusPlus.mk.android" >> "$PCAPPLUSPLUS_MK"
cat >> "$PCAPPLUSPLUS_MK" <<EOF
LIBPCAP_INCLUDE_DIR := $LIBPCAP_INCLUDE_DIR
PCAPPP_INCLUDES += -I\$(LIBPCAP_INCLUDE_DIR)
LIBPCAP_LIB_DIR := $LIBPCAP_LIBRARY_DIR
PCAPPP_LIBS_DIR += -L\$(LIBPCAP_LIB_DIR)
EOF
#########################################################################################
PLATFORM_MK="$SCRIPT_DIR/mk/platform.mk"
cp -f "$TEMP_CONFIG_FILE" "$PLATFORM_MK"
cat "$SCRIPT_DIR/mk/platform.mk.android" >> "$PLATFORM_MK"
#########################################################################################
printf '%b\n' "
${COLOR_GREEN}PcapPlusPlus Android configuration is complete.${COLOR_OFF}
configurations is written to :
- $PLATFORM_MK
- $PCAPPLUSPLUS_MK
"