Skip to content

Commit

Permalink
Feature: Android arm64 support (#442)
Browse files Browse the repository at this point in the history
* Feature: Support Linux android-arm64(aarch64)

* Add dockcross script

* Add native lib

* Add --no-cache for CI

* Revert "Add --no-cache for CI"

This reverts commit 0f2f05b.

* Add docker clean process

* Update native libraries

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
xerial and github-actions[bot] committed May 23, 2023
1 parent ad2e2d7 commit fd50402
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 3 deletions.
18 changes: 16 additions & 2 deletions Makefile
Expand Up @@ -149,7 +149,17 @@ native: jni-header snappy-header $(NATIVE_DLL)
native-nocmake: jni-header $(NATIVE_DLL)
snappy: native $(TARGET)/$(snappy-jar-version).jar

native-all: native native-arm mac64 win32 win64 linux32 linux64 linux-ppc64le linux-riscv64 linux-s390x
native-all: native native-arm clean-docker mac64 win32 win64 linux32 linux64 linux-ppc64le linux-riscv64 linux-s390x

ifdef CI
# Clean docker images within CI to avoid no space left error
DOCKER_POST_PROCESS:=docker system prune --all --force --volumes
else
DOCKER_POST_PROCESS:=
endif

clean-docker:
$(DOCKER_POST_PROCESS)

$(NATIVE_DLL): $(SNAPPY_OUT)/$(LIBNAME)
@mkdir -p $(@D)
Expand All @@ -167,6 +177,7 @@ test: $(NATIVE_DLL)

DOCKER_RUN_OPTS:=--rm


win32: jni-header
./docker/dockcross-windows-x86 -a $(DOCKER_RUN_OPTS) bash -c 'make clean-native snappy-header native CROSS_PREFIX=i686-w64-mingw32.static- OS_NAME=Windows OS_ARCH=x86 SNAPPY_CMAKE_OPTS="-DHAVE_SYS_UIO_H=0"'

Expand All @@ -190,7 +201,7 @@ freebsd64:
$(MAKE) native OS_NAME=FreeBSD OS_ARCH=x86_64

# For ARM
native-arm: linux-arm64 linux-android-arm linux-arm linux-armv6 linux-armv7
native-arm: linux-arm64 linux-android-arm linux-android-aarch64 linux-arm linux-armv6 linux-armv7

linux-arm: jni-header
./docker/dockcross-armv5 -a $(DOCKER_RUN_OPTS) bash -c 'make clean-native native CROSS_PREFIX=/usr/xcc/armv5-unknown-linux-gnueabi/bin//armv5-unknown-linux-gnueabi- OS_NAME=Linux OS_ARCH=arm'
Expand All @@ -204,6 +215,9 @@ linux-armv7: jni-header
linux-android-arm: jni-header
./docker/dockcross-android-arm -a $(DOCKER_RUN_OPTS) bash -c 'make clean-native native CROSS_PREFIX=/usr/arm-linux-androideabi/bin/arm-linux-androideabi- OS_NAME=Linux OS_ARCH=android-arm'

linux-android-aarch64: jni-header
./docker/dockcross-android-arm64 -a $(DOCKER_RUN_OPTS) bash -c 'make clean-native native OS_NAME=Linux OS_ARCH=android-aarch64'

linux-ppc64le: jni-header
./docker/dockcross-ppc64le -a $(DOCKER_RUN_OPTS) bash -c 'make clean-native native CROSS_PREFIX=powerpc64le-unknown-linux-gnu- OS_NAME=Linux OS_ARCH=ppc64le'

Expand Down
9 changes: 8 additions & 1 deletion Makefile.common
Expand Up @@ -50,7 +50,7 @@ endif

# os=Default is meant to be generic unix/linux

known_os_archs := Linux-x86 Linux-x86_64 Linux-arm Linux-armv6 Linux-armv7 Linux-android-arm Linux-aarch64 Linux-ppc Linux-ppc64 Linux-ppc64le Linux-s390 Linux-s390x Mac-x86 Mac-x86_64 Mac-aarch64 FreeBSD-x86_64 Windows-x86 Windows-x86_64 SunOS-x86 SunOS-sparc SunOS-x86_64 AIX-ppc AIX-ppc64 Linux-riscv Linux-riscv64
known_os_archs := Linux-x86 Linux-x86_64 Linux-arm Linux-armv6 Linux-armv7 Linux-android-arm Linux-android-aarch64 Linux-aarch64 Linux-ppc Linux-ppc64 Linux-ppc64le Linux-s390 Linux-s390x Mac-x86 Mac-x86_64 Mac-aarch64 FreeBSD-x86_64 Windows-x86 Windows-x86_64 SunOS-x86 SunOS-sparc SunOS-x86_64 AIX-ppc AIX-ppc64 Linux-riscv Linux-riscv64
os_arch := $(OS_NAME)-$(OS_ARCH)
IBM_JDK_7 := $(findstring IBM, $(shell $(JAVA) -version 2>&1 | grep IBM | grep "JRE 1.7"))

Expand Down Expand Up @@ -234,6 +234,13 @@ Linux-android-arm_LINKFLAGS := -shared -static-libgcc
Linux-android-arm_LIBNAME := libsnappyjava.so
Linux-android-arm_SNAPPY_FLAGS:=

Linux-android-aarch64_CXX := ${CROSS_ROOT}/bin/${CROSS_TRIPLE}-g++
Linux-android-aarch64_STRIP := ${CROSS_ROOT}/bin/llvm-strip
Linux-android-aarch64_CXXFLAGS := -Ilib/inc_linux -I$(JAVA_HOME)/include -O2 -fPIC -fvisibility=hidden -std=c++11
Linux-android-aarch64_LINKFLAGS := -shared -static-libgcc
Linux-android-aarch64_LIBNAME := libsnappyjava.so
Linux-android-aarch64_SNAPPY_FLAGS:=

Linux-aarch64_CXX := $(CROSS_PREFIX)g++
Linux-aarch64_STRIP := $(CROSS_PREFIX)strip
Linux-aarch64_CXXFLAGS := -Ilib/inc_linux -I$(JAVA_HOME)/include -O2 -fPIC -fvisibility=hidden -std=c++11
Expand Down
278 changes: 278 additions & 0 deletions docker/dockcross-android-arm64
@@ -0,0 +1,278 @@
#!/usr/bin/env bash

DEFAULT_DOCKCROSS_IMAGE=dockcross/android-arm64:20230421-a0eaff4

#------------------------------------------------------------------------------
# Helpers
#
err() {
echo -e >&2 "ERROR: $*\n"
}

die() {
err "$*"
exit 1
}

has() {
# eg. has command update
local kind=$1
local name=$2

type -t $kind:$name | grep -q function
}

# If OCI_EXE is not already set, search for a container executor (OCI stands for "Open Container Initiative")
if [ -z "$OCI_EXE" ]; then
if which podman >/dev/null 2>/dev/null; then
OCI_EXE=podman
elif which docker >/dev/null 2>/dev/null; then
OCI_EXE=docker
else
die "Cannot find a container executor. Search for docker and podman."
fi
fi

#------------------------------------------------------------------------------
# Command handlers
#
command:update-image() {
$OCI_EXE pull $FINAL_IMAGE
}

help:update-image() {
echo "Pull the latest $FINAL_IMAGE ."
}

command:update-script() {
if cmp -s <( $OCI_EXE run --rm $FINAL_IMAGE ) $0; then
echo "$0 is up to date"
else
echo -n "Updating $0 ... "
$OCI_EXE run --rm $FINAL_IMAGE > $0 && echo ok
fi
}

help:update-script() {
echo "Update $0 from $FINAL_IMAGE ."
}

command:update() {
command:update-image
command:update-script
}

help:update() {
echo "Pull the latest $FINAL_IMAGE, and then update $0 from that."
}

command:help() {
if [[ $# != 0 ]]; then
if ! has command $1; then
err \"$1\" is not an dockcross command
command:help
elif ! has help $1; then
err No help found for \"$1\"
else
help:$1
fi
else
cat >&2 <<ENDHELP
Usage: dockcross [options] [--] command [args]
By default, run the given *command* in an dockcross Docker container.
The *options* can be one of:
--args|-a Extra args to the *docker run* command
--image|-i Docker cross-compiler image to use
--config|-c Bash script to source before running this script
Additionally, there are special update commands:
update-image
update-script
update
For update command help use: $0 help <command>
ENDHELP
exit 1
fi
}

#------------------------------------------------------------------------------
# Option processing
#
special_update_command=''
while [[ $# != 0 ]]; do
case $1 in

--)
shift
break
;;

--args|-a)
ARG_ARGS="$2"
shift 2
;;

--config|-c)
ARG_CONFIG="$2"
shift 2
;;

--image|-i)
ARG_IMAGE="$2"
shift 2
;;
update|update-image|update-script)
special_update_command=$1
break
;;
-*)
err Unknown option \"$1\"
command:help
exit
;;

*)
break
;;

esac
done

# The precedence for options is:
# 1. command-line arguments
# 2. environment variables
# 3. defaults

# Source the config file if it exists
DEFAULT_DOCKCROSS_CONFIG=~/.dockcross
FINAL_CONFIG=${ARG_CONFIG-${DOCKCROSS_CONFIG-$DEFAULT_DOCKCROSS_CONFIG}}

[[ -f "$FINAL_CONFIG" ]] && source "$FINAL_CONFIG"

# Set the docker image
FINAL_IMAGE=${ARG_IMAGE-${DOCKCROSS_IMAGE-$DEFAULT_DOCKCROSS_IMAGE}}

# Handle special update command
if [ "$special_update_command" != "" ]; then
case $special_update_command in

update)
command:update
exit $?
;;

update-image)
command:update-image
exit $?
;;

update-script)
command:update-script
exit $?
;;

esac
fi

# Set the docker run extra args (if any)
FINAL_ARGS=${ARG_ARGS-${DOCKCROSS_ARGS}}

# Bash on Ubuntu on Windows
UBUNTU_ON_WINDOWS=$([ -e /proc/version ] && grep -l Microsoft /proc/version || echo "")
# MSYS, Git Bash, etc.
MSYS=$([ -e /proc/version ] && grep -l MINGW /proc/version || echo "")
# CYGWIN
CYGWIN=$([ -e /proc/version ] && grep -l CYGWIN /proc/version || echo "")

if [ -z "$UBUNTU_ON_WINDOWS" -a -z "$MSYS" -a "$OCI_EXE" != "podman" ]; then
USER_IDS=(-e BUILDER_UID="$( id -u )" -e BUILDER_GID="$( id -g )" -e BUILDER_USER="$( id -un )" -e BUILDER_GROUP="$( id -gn )")
fi

# Change the PWD when working in Docker on Windows
if [ -n "$UBUNTU_ON_WINDOWS" ]; then
WSL_ROOT="/mnt/"
CFG_FILE=/etc/wsl.conf
if [ -f "$CFG_FILE" ]; then
CFG_CONTENT=$(cat $CFG_FILE | sed -r '/[^=]+=[^=]+/!d' | sed -r 's/\s+=\s/=/g')
eval "$CFG_CONTENT"
if [ -n "$root" ]; then
WSL_ROOT=$root
fi
fi
HOST_PWD=`pwd -P`
HOST_PWD=${HOST_PWD/$WSL_ROOT//}
elif [ -n "$MSYS" ]; then
HOST_PWD=$PWD
HOST_PWD=${HOST_PWD/\//}
HOST_PWD=${HOST_PWD/\//:\/}
elif [ -n "$CYGWIN" ]; then
for f in pwd readlink cygpath ; do
test -n "$(type "${f}" )" || { echo >&2 "Missing functionality (${f}) (in cygwin)." ; exit 1 ; } ;
done ;
HOST_PWD="$( cygpath -w "$( readlink -f "$( pwd ;)" ; )" ; )" ;
else
HOST_PWD=$PWD
[ -L $HOST_PWD ] && HOST_PWD=$(readlink $HOST_PWD)
fi

# Mount Additional Volumes
if [ -z "$SSH_DIR" ]; then
SSH_DIR="$HOME/.ssh"
fi

HOST_VOLUMES=
if [ -e "$SSH_DIR" -a -z "$MSYS" ]; then
if test -n "${CYGWIN}" ; then
HOST_VOLUMES+="-v $(cygpath -w ${SSH_DIR} ; ):/home/$(id -un)/.ssh" ;
else
HOST_VOLUMES+="-v $SSH_DIR:/home/$(id -un)/.ssh" ;
fi ;
fi

#------------------------------------------------------------------------------
# Now, finally, run the command in a container
#
TTY_ARGS=
tty -s && [ -z "$MSYS" ] && TTY_ARGS=-ti
CONTAINER_NAME=dockcross_$RANDOM
$OCI_EXE run $TTY_ARGS --name $CONTAINER_NAME \
-v "$HOST_PWD":/work \
$HOST_VOLUMES \
"${USER_IDS[@]}" \
$FINAL_ARGS \
$FINAL_IMAGE "$@"
run_exit_code=$?

# Attempt to delete container
rm_output=$($OCI_EXE rm -f $CONTAINER_NAME 2>&1)
rm_exit_code=$?
if [[ $rm_exit_code != 0 ]]; then
if [[ "$CIRCLECI" == "true" ]] && [[ $rm_output == *"Driver btrfs failed to remove"* ]]; then
: # Ignore error because of https://circleci.com/docs/docker-btrfs-error/
else
echo "$rm_output"
exit $rm_exit_code
fi
fi

exit $run_exit_code

################################################################################
#
# This image is not intended to be run manually.
#
# To create a dockcross helper script for the
# dockcross/android-arm64:20230421-a0eaff4 image, run:
#
# docker run --rm dockcross/android-arm64:20230421-a0eaff4 > dockcross-android-arm64-20230421-a0eaff4
# chmod +x dockcross-android-arm64-20230421-a0eaff4
#
# You may then wish to move the dockcross script to your PATH.
#
################################################################################
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit fd50402

Please sign in to comment.