Skip to content

Commit bcc495c

Browse files
authored
Merge pull request #2 from cubzh/libssl_android
libssl : add build system for Android version
2 parents 69c517f + 6f66723 commit bcc495c

8 files changed

Lines changed: 233 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
libssl/android/output
2+
libssl/openssl

libssl/android/build.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# ==================================================
5+
# FUNCTIONS
6+
# ==================================================
7+
arch_is_invalid() {
8+
local valid=( arm arm64 x86 x86_64 )
9+
local value=$1
10+
for elem in "${valid[@]}"; do
11+
[[ $value == $elem ]] && return 1
12+
done
13+
return 0
14+
}
15+
# ==================================================
16+
17+
if [ "$#" -ne 1 ]; then
18+
echo "❌ Illegal number of parameters ($#). Expected 1."
19+
fi
20+
21+
TARGET_ARCH=${1}
22+
23+
if arch_is_invalid "${TARGET_ARCH}"; then
24+
echo "❌ Architecture NOT supported: ${TARGET_ARCH}. Exiting."
25+
exit 1
26+
fi
27+
28+
echo "=================================================="
29+
echo "⚙️ Building OpenSSL for Android > ${TARGET_ARCH} <"
30+
echo "=================================================="
31+
32+
set -x
33+
34+
# Set directory
35+
SCRIPTPATH=`realpath .`
36+
37+
# If OPENSSL_DIR is not set, then fallback to a default value
38+
OPENSSL_DIR=${OPENSSL_DIR:-"../openssl"}
39+
40+
# Find the toolchain for your build machine
41+
toolchains_path=$(python3 toolchains_path.py --ndk ${ANDROID_NDK_HOME})
42+
43+
# Configure the OpenSSL environment, refer to NOTES.ANDROID in OPENSSL_DIR
44+
# Set compiler clang, instead of gcc by default
45+
CC=clang
46+
47+
# Add toolchains bin directory to PATH
48+
PATH=$toolchains_path/bin:$PATH
49+
50+
# Set the Android API levels
51+
# ANDROID_API=21
52+
53+
# Set the target architecture
54+
# Can be android-arm, android-arm64, android-x86, android-x86 etc
55+
architecture=android-${TARGET_ARCH}
56+
57+
# Create the make file
58+
cd ${OPENSSL_DIR}
59+
# ./Configure ${architecture} -D__ANDROID_API__=$ANDROID_API
60+
./Configure ${architecture}
61+
62+
# Build
63+
make
64+
65+
# Copy the outputs
66+
OUTPUT_INCLUDE=$SCRIPTPATH/output/include
67+
OUTPUT_LIB=$SCRIPTPATH/output/lib/${TARGET_ARCH}
68+
mkdir -p $OUTPUT_INCLUDE
69+
mkdir -p $OUTPUT_LIB
70+
cp -RL include/crypto $OUTPUT_INCLUDE
71+
cp -RL include/internal $OUTPUT_INCLUDE
72+
cp -RL include/openssl $OUTPUT_INCLUDE
73+
cp libcrypto.so $OUTPUT_LIB
74+
cp libcrypto.a $OUTPUT_LIB
75+
cp libssl.so $OUTPUT_LIB
76+
cp libssl.a $OUTPUT_LIB

libssl/android/build_all.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Set directory
5+
SCRIPTPATH=`realpath .`
6+
7+
${SCRIPTPATH}/clean.sh
8+
${SCRIPTPATH}/build.sh arm
9+
10+
${SCRIPTPATH}/clean.sh
11+
${SCRIPTPATH}/build.sh arm64
12+
13+
${SCRIPTPATH}/clean.sh
14+
${SCRIPTPATH}/build.sh x86
15+
16+
${SCRIPTPATH}/clean.sh
17+
${SCRIPTPATH}/build.sh x86_64

libssl/android/clean.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
# README
4+
# ------------------------------
5+
#
6+
# "OPENSSL_DIR" env var needs to be set
7+
# TODO: "ANDROID_NDK_HOME" env var needs to be set
8+
#
9+
10+
set -e
11+
set -x
12+
13+
# TODO: use bashsource ?
14+
15+
# If OPENSSL_DIR is not set, then fallback to a default value
16+
OPENSSL_DIR=${OPENSSL_DIR:-"../openssl"}
17+
18+
SCRIPT_PATH=`readlink -f "${BASH_SOURCE:-$0}"`
19+
SCRIPT_DIR=`dirname ${SCRIPT_PATH}`
20+
21+
# Find the toolchain for your build machine
22+
toolchains_path=$(python3 ${SCRIPT_DIR}/toolchains_path.py --ndk ${ANDROID_NDK_HOME})
23+
24+
# Configure the OpenSSL environment, refer to NOTES.ANDROID in OPENSSL_DIR
25+
# Set compiler clang, instead of gcc by default
26+
CC=clang
27+
28+
# Add toolchains bin directory to PATH
29+
PATH=$toolchains_path/bin:$PATH
30+
31+
# clean
32+
cd ${OPENSSL_DIR}
33+
34+
make clean

libssl/android/toolchains_path.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
"""
3+
Get the toolchains path
4+
"""
5+
import argparse
6+
import atexit
7+
import inspect
8+
import os
9+
import shutil
10+
import stat
11+
import sys
12+
import textwrap
13+
14+
def get_host_tag_or_die():
15+
"""Return the host tag for this platform. Die if not supported."""
16+
if sys.platform.startswith('linux'):
17+
return 'linux-x86_64'
18+
elif sys.platform == 'darwin':
19+
return 'darwin-x86_64'
20+
elif sys.platform == 'win32' or sys.platform == 'cygwin':
21+
host_tag = 'windows-x86_64'
22+
if not os.path.exists(os.path.join(NDK_DIR, 'prebuilt', host_tag)):
23+
host_tag = 'windows'
24+
return host_tag
25+
sys.exit('Unsupported platform: ' + sys.platform)
26+
27+
28+
def get_toolchain_path_or_die(ndk, host_tag):
29+
"""Return the toolchain path or die."""
30+
toolchain_path = os.path.join(ndk, 'toolchains/llvm/prebuilt',
31+
host_tag)
32+
if not os.path.exists(toolchain_path):
33+
sys.exit('Could not find toolchain: {}'.format(toolchain_path))
34+
return toolchain_path
35+
36+
def main():
37+
"""Program entry point."""
38+
parser = argparse.ArgumentParser(description='Optional app description')
39+
parser.add_argument('--ndk', required=True,
40+
help='The NDK Home directory')
41+
args = parser.parse_args()
42+
43+
host_tag = get_host_tag_or_die()
44+
toolchain_path = get_toolchain_path_or_die(args.ndk, host_tag)
45+
print(toolchain_path)
46+
47+
if __name__ == '__main__':
48+
main()

libssl/build_android.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Set directory
6+
SCRIPTPATH=`realpath .`
7+
8+
#
9+
export ANDROID_NDK_HOME=${ANDROID_NDK_HOME:-"/android/ndk/25.2.9519653"}
10+
11+
#
12+
LIBSSL_ANDROID_PATH=${SCRIPTPATH}/android
13+
export OPENSSL_DIR=${SCRIPTPATH}/openssl
14+
15+
cd ${LIBSSL_ANDROID_PATH}
16+
17+
./clean.sh
18+
./build.sh arm
19+
20+
./clean.sh
21+
./build.sh arm64
22+
23+
./clean.sh
24+
./build.sh x86
25+
26+
./clean.sh
27+
./build.sh x86_64

libssl/build_android_docker.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# ------------------------------
6+
# Variables
7+
# ------------------------------
8+
SCRIPT_PATH=`realpath .`
9+
10+
# ------------------------------
11+
#
12+
# ------------------------------
13+
docker run --rm -ti \
14+
-v ${SCRIPT_PATH}:/project \
15+
-w /project \
16+
voxowl/android-sdk ./build_android.sh

libssl/clone.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# Config
4+
OPENSSL_GIT_REPO="https://github.com/gdevillele/openssl.git"
5+
OPENSSL_GIT_BRANCH="OpenSSL_1_1_1-stable-cubzh-patch"
6+
7+
set -e
8+
9+
docker run \
10+
--rm \
11+
-v $(pwd)/openssl:/repo/openssl \
12+
ubuntu:22.04 \
13+
bash -c "set -e; apt update; apt install -y git; mkdir -p /repo/openssl; find /repo/openssl -mindepth 1 -delete; git clone --depth 1 --branch ${OPENSSL_GIT_BRANCH} ${OPENSSL_GIT_REPO} /repo/openssl; exit 0;"

0 commit comments

Comments
 (0)