Skip to content

Commit

Permalink
scripts: improving work with cgct (#21112)
Browse files Browse the repository at this point in the history
These changes are intended to make it easier to customize the compilation of glibc-based packages for other applications.
  • Loading branch information
Maxython authored Aug 15, 2024
1 parent 49fc92c commit b675b06
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
36 changes: 28 additions & 8 deletions clean.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
# clean.sh - clean everything.
set -e -u

Expand All @@ -7,6 +7,8 @@ TERMUX_SCRIPTDIR=$(cd "$(realpath "$(dirname "$0")")"; pwd)
# Store pid of current process in a file for docker__run_docker_exec_trap
. "$TERMUX_SCRIPTDIR/scripts/utils/docker/docker.sh"; docker__create_docker_exec_pid_file

# Get variable CGCT_DIR
. "$TERMUX_SCRIPTDIR/scripts/properties.sh"

# Checking if script is running on Android with 2 different methods.
# Needed for safety to prevent execution of potentially dangerous
Expand All @@ -17,7 +19,7 @@ else
TERMUX_ON_DEVICE_BUILD=false
fi

if [ "$(id -u)" = "0" ] && $TERMUX_ON_DEVICE_BUILD; then
if [ "$(id -u)" = "0" ] && [[ "$TERMUX_ON_DEVICE_BUILD" == "true" ]]; then
echo "On-device execution of this script as root is disabled."
exit 1
fi
Expand All @@ -44,11 +46,29 @@ fi
chmod +w -R "$TERMUX_TOPDIR" || true
fi

if $TERMUX_ON_DEVICE_BUILD; then
# For on-device build cleanup /data shouldn't be erased.
rm -Rf "$TERMUX_TOPDIR"
else
find /data -mindepth 1 ! -regex '^/data/data/com.termux/cgct\(/.*\)?' -delete 2> /dev/null || true
rm -Rf "$TERMUX_TOPDIR"
# For on-device build cleanup /data shouldn't be erased.
if [[ "$TERMUX_ON_DEVICE_BUILD" == "false" ]]; then
if [[ ! "$TERMUX_BASE_DIR" =~ ^(/[^/]+)+$ ]]; then
echo "TERMUX_BASE_DIR '$TERMUX_BASE_DIR' is not an absolute path under rootfs '/'." 1>&2
exit 1
fi
if [[ "$TERMUX_BASE_DIR" =~ ^/[^/]+$ ]]; then
# Use `/rootfs` as is.
rootfs_top_level_dir="$TERMUX_BASE_DIR"
else
# Get `/path/` from `/path/to/rootfs`.
rootfs_top_level_dir="${TERMUX_BASE_DIR%"${TERMUX_BASE_DIR#/*/}"}"
fi

if [[ ! "$CGCT_DIR" =~ ^(/[^/]+)+$ ]] || [[ "$CGCT_DIR" == "$TERMUX_BASE_DIR" ]]; then
echo "CGCT_DIR '$CGCT_DIR' is not an absolute path under rootfs '/' or equals TERMUX_BASE_DIR." 1>&2
exit 1
fi

# Escape '\$[](){}|^.?+*' with backslashes
cgct_dir_escaped="$(printf "%s" "$CGCT_DIR" | sed -zE -e 's/[][\.|$(){}?+*^]/\\&/g')"
find "$rootfs_top_level_dir" -mindepth 1 -regextype posix-extended ! -regex "^$cgct_dir_escaped(/.*)?" -delete 2>/dev/null || true
fi

rm -Rf "$TERMUX_TOPDIR"
} 5< "$TERMUX_BUILD_LOCK_FILE"
5 changes: 5 additions & 0 deletions scripts/build/termux_step_setup_cgct_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
termux_step_setup_cgct_environment() {
[ "$TERMUX_ON_DEVICE_BUILD" = "true" ] && return

if [ "$TERMUX_REPO_PACKAGE" != "$TERMUX_APP_PACKAGE" ]; then
echo "WARNING: It is not possible to install glibc core packages from the repo for operation of CGCT, you must install glibc packages for your application with the prefix '$TERMUX_PREFIX' yourself (core packages: glibc and linux-api-headers-glibc)."
return
fi

for PKG in gcc-libs-glibc glibc linux-api-headers-glibc; do
local PKG_DIR=$(ls ${TERMUX_SCRIPTDIR}/*/${PKG}/build.sh 2> /dev/null || \
ls ${TERMUX_SCRIPTDIR}/*/${PKG/-glibc/}/build.sh 2> /dev/null)
Expand Down
3 changes: 3 additions & 0 deletions scripts/build/termux_step_setup_variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ termux_step_setup_variables() {

if [ "$TERMUX_PACKAGE_LIBRARY" = "glibc" ]; then
export TERMUX_PREFIX="$TERMUX_PREFIX/glibc"
if [ "$TERMUX_ON_DEVICE_BUILD" = "false" ] && [ "$TERMUX_PREFIX" != "$CGCT_DEFAULT_PREFIX" ]; then
export CGCT_APP_PREFIX="$TERMUX_PREFIX"
fi
if ! package__is_package_name_have_glibc_prefix "$TERMUX_PKG_NAME"; then
TERMUX_PKG_NAME="$(package__add_prefix_glibc_to_package_name ${TERMUX_PKG_NAME})"
fi
Expand Down
21 changes: 16 additions & 5 deletions scripts/build/toolchain/termux_setup_toolchain_gnu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,30 @@ termux_setup_toolchain_gnu() {
export CXXFILT=$TERMUX_HOST_PLATFORM-c++filt
fi

export PATH_DYNAMIC_LINKER="$TERMUX_PREFIX/lib/"
if [ ! -d "$TERMUX_PREFIX/lib/" ]; then
termux_error_exit "glibc library directory was not found ('$TERMUX_PREFIX/lib/')"
fi
if [ ! -d "$TERMUX_PREFIX/include/" ]; then
termux_error_exit "glibc header directory was not found ('$TERMUX_PREFIX/include/')"
fi

if [ "$TERMUX_ARCH" = "aarch64" ]; then
CFLAGS+=" -march=armv8-a"
PATH_DYNAMIC_LINKER+="ld-linux-aarch64.so.1"
export DYNAMIC_LINKER="ld-linux-aarch64.so.1"
elif [ "$TERMUX_ARCH" = "arm" ]; then
CFLAGS+=" -march=armv7-a -mfloat-abi=hard -mfpu=neon"
PATH_DYNAMIC_LINKER+="ld-linux-armhf.so.3"
export DYNAMIC_LINKER="ld-linux-armhf.so.3"
elif [ "$TERMUX_ARCH" = "x86_64" ]; then
CFLAGS+=" -march=x86-64 -fPIC"
PATH_DYNAMIC_LINKER+="ld-linux-x86-64.so.2"
export DYNAMIC_LINKER="ld-linux-x86-64.so.2"
elif [ "$TERMUX_ARCH" = "i686" ]; then
CFLAGS+=" -march=i686"
PATH_DYNAMIC_LINKER+="ld-linux.so.2"
export DYNAMIC_LINKER="ld-linux.so.2"
fi
export PATH_DYNAMIC_LINKER="$TERMUX_PREFIX/lib/$DYNAMIC_LINKER"

if [ ! -f "$PATH_DYNAMIC_LINKER" ]; then
termux_error_exit "glibc dynamic linker was not found ('$PATH_DYNAMIC_LINKER')"
fi

case "$TERMUX_ARCH" in
Expand Down
3 changes: 2 additions & 1 deletion scripts/properties.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ TERMUX_CONFIG_PREFIX_DIR_PATH="${TERMUX_ETC_PREFIX_DIR_PATH}/termux"
TERMUX_BOOTSTRAP_CONFIG_DIR_PATH="${TERMUX_CONFIG_PREFIX_DIR_PATH}/bootstrap"

# Path to CGCT tools
export CGCT_DIR="/data/data/${TERMUX_APP_PACKAGE}/cgct"
CGCT_DEFAULT_PREFIX="/data/data/com.termux/files/usr/glibc"
export CGCT_DIR="/data/data/com.termux/cgct"

# Package name for the packages hosted on the repo.
# This must only equal TERMUX_APP_PACKAGE if using custom repo that
Expand Down

0 comments on commit b675b06

Please sign in to comment.