-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As of this commit, ksh supports the Termux environment on Android. There were several challenges to solve to make it build. Termux is based on the typical Linux environment but with important changes, and there is no attempt at POSIX compliance. This commit adds the necessary workarounds to deal with this system's quirks. This commit was tested on Android 14.0 arm64 with Termux 0.118.0 on the Android emulator provided by Android Studio on macOS. To build correctly, you need the clang, binutils, and getconf packages. At runtime, the getconf package is needed for AST getconf(1) fallbacks to function, and ncurses-utils packages (specifically, tput(1)) is needed to enable multiline editing. bin/package: - Since Termux does not have 'getconf PATH' (since it lacks POSIX confstr(3)): move the DEFPATH/PATH code and add two fallbacks for 'getconf PATH' that require a compiler: one that tries confstr(), currently expected to fail on Termux, and another one that tries the non-standard _PATH_DEFPATH macro, which exist on most (all?) Linux and BSD systems including Android. (re: 78b1a84) - Export DEFPATH to the environment so we can eliminate repetitive code to determine it in iffe and mamprobe. - hostinfo(): Use 'android' and not 'linux' as the host type base name on Android/Termux; this will allow us to implement necessary special-casing in make.probe (see below). src/cmd/INIT/iffe.sh, src/cmd/INIT/mamprobe.sh, src/lib/libast/comp/conf.sh, src/lib/libast/comp/conf.tab: - Require DEFPATH in the environment (see above). - Use it instead of the code to determine default system path. src/cmd/INIT/make.probe, **/Mamfile: - On Termux we need API version 26 for the system headers to declare a couple of functions we depend on, including catopen(3). This is done by adding --target=aarch64-linux-android26 to the compiler command line. Add a CC.TARGET variable (which becomes mam_cc_TARGET in the Mamfiles) to contain this flag. src/lib/libast/features/standards: - Add support for Android. After the make.probe change above we can treat it the same as GNU, as the Android headers also recognise _GNU_SOURCE for some things. src/lib/libast/comp/omitted.c: - On Android, bzero(3) is a macro that expands to __bionic_bzero(), so iffe doesn't detect bzero() as a library function. Check for both the iffe result _lib_bzero and the presence of bzero as a macro to avoid a build error. src/lib/libast/comp/strtold.c: - Avoid another build failure: work around Android system header incompatibilities with the AST headers by not including stdlib.h from ast_sys.h in this case. This is done by temporarily defining _STDLIB_H so the Android stdlib.h code gets skipped. src/lib/libast/sfio/sfcvt.c: - Work around another build failure by not relying on the _lib_isnan iffe test result on Android. src/lib/libast/vmalloc/vmhdr.h: - Include <sig.h> here to avoid yet another build failure on Android (undeclared function '_ast_signal'). src/lib/libcmd/fds.c: - Since Android declares ntohs(3) in sys/endian.h and not in the POSIX standard location arpa/inet.h, include the former on Android. src/cmd/ksh93/tests/*.sh: - Since Android does not have /tmp, avoid using it. For testing cd, /dev is a good alternative. - Android don't allow inheriting stdout in closed state. Disable the tests that rely on this on systems that don't allow this.
- Loading branch information
Showing
26 changed files
with
216 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,42 +82,6 @@ case $PWD in | |
exit 1 ;; | ||
esac || exit | ||
|
||
# Outputs sanitized system $PATH, eliminating duplicate and nonexistent dirs, '..', etc. | ||
sanitize_PATH() ( | ||
set -fu +e | ||
IFS=':' | ||
unset -v CDPATH | ||
sPATH='' | ||
for dir in $1; do | ||
# Sanitize this path, resolving symlinks, | ||
# with special-casing of ksh's virtual built-ins directory | ||
case $dir in | ||
/opt/ast/bin) | ||
test ! -d "$dir" && sdir=$dir ;; | ||
*/* | [!+-]* | [+-]*[!0123456789]*) | ||
sdir=$(cd -- $dir 2>/dev/null && pwd -P && echo X) ;; | ||
*) sdir=$(cd ./$dir 2>/dev/null && pwd -P && echo X) ;; | ||
esac || continue | ||
sdir=${sdir%?X} | ||
# Skip duplicates | ||
case :$sPATH: in | ||
*:"$sdir":*) | ||
continue ;; | ||
esac | ||
# Found one, add it | ||
sPATH=${sPATH:+$sPATH:}$sdir | ||
done | ||
printf '%s\n' "${sPATH#:}" | ||
) | ||
|
||
# Ensure a sane $PATH beginning with standard utilities. | ||
# Find preferred 'getconf' on NixOS and Solaris/illumos. | ||
DEFPATH=$( | ||
PATH=/run/current-system/sw/bin:/usr/xpg7/bin:/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin:$PATH | ||
getconf PATH 2>/dev/null | ||
) || DEFPATH=/bin:/usr/bin:/sbin:/usr/sbin | ||
PATH=$(sanitize_PATH "/opt/ast/bin:$DEFPATH:$PATH") | ||
|
||
# shell checks | ||
checksh() | ||
{ | ||
|
@@ -137,7 +101,7 @@ lib="" # need /usr/local/lib /usr/local/shlib | |
ccs="/usr/kvm /usr/ccs/bin" | ||
org="gnu GNU" | ||
makefiles="Mamfile" # ksh 93u+m no longer uses these: Nmakefile nmakefile Makefile makefile | ||
env="HOSTTYPE PACKAGEROOT INSTALLROOT PATH FPATH MANPATH" | ||
env="HOSTTYPE PACKAGEROOT INSTALLROOT PATH DEFPATH FPATH MANPATH" | ||
|
||
package_use='=$HOSTTYPE=$PACKAGEROOT=$INSTALLROOT=$EXECROOT=$CC=' | ||
|
||
|
@@ -151,7 +115,7 @@ command=${0##*/} | |
case $(getopts '[-][123:xyz]' opt --xyz 2>/dev/null; echo 0$opt) in | ||
0123) USAGE=$' | ||
[-? | ||
@(#)$Id: '$command$' (ksh 93u+m) 2024-03-07 $ | ||
@(#)$Id: '$command$' (ksh 93u+m) 2024-03-20 $ | ||
] | ||
[-author?Glenn Fowler <[email protected]>] | ||
[-author?Contributors to https://github.com/ksh93/ksh] | ||
|
@@ -579,7 +543,7 @@ SEE ALSO | |
pkgadd(1), pkgmk(1), rpm(1), sh(1), tar(1), optget(3) | ||
|
||
IMPLEMENTATION | ||
version package (ksh 93u+m) 2024-03-07 | ||
version package (ksh 93u+m) 2024-03-20 | ||
author Glenn Fowler <[email protected]> | ||
author Contributors to https://github.com/ksh93/ksh | ||
copyright (c) 1994-2012 AT&T Intellectual Property | ||
|
@@ -1069,6 +1033,9 @@ int main(void) | |
*-linux-gnu* | *-linux-musl*) | ||
# fix missing machine field, e.g. aarch64-linux-gnu => aarch64-unknown-linux-gnu | ||
canon=${canon%%-*}-unknown-${canon#*-} ;; | ||
*-linux-android*) | ||
# Android/Termux is very much its own thing, so identify it as 'android', not 'linux' | ||
canon=${canon%-linux-*}-android ;; | ||
esac | ||
case -${canon}- in | ||
--|*-powerpc-*) | ||
|
@@ -1674,6 +1641,115 @@ checkcc() | |
esac | ||
} | ||
|
||
# output sanitized system $PATH, eliminating duplicate and nonexistent dirs, '..', etc. | ||
|
||
sanitize_PATH() | ||
( | ||
set -fu +e | ||
IFS=':' | ||
unset -v CDPATH | ||
sPATH='' | ||
for dir in $1; do | ||
# Sanitize this path, resolving symlinks, | ||
# with special-casing of ksh's virtual built-ins directory | ||
case $dir in | ||
/opt/ast/bin) | ||
test ! -d "$dir" && sdir=$dir ;; | ||
*/* | [!+-]* | [+-]*[!0123456789]*) | ||
sdir=$(cd -- $dir 2>/dev/null && pwd -P && echo X) ;; | ||
*) sdir=$(cd ./$dir 2>/dev/null && pwd -P && echo X) ;; | ||
esac || continue | ||
sdir=${sdir%?X} | ||
# Skip duplicates | ||
case :$sPATH: in | ||
*:"$sdir":*) | ||
continue ;; | ||
esac | ||
# Found one, add it | ||
sPATH=${sPATH:+$sPATH:}$sdir | ||
done | ||
printf '%s\n' "${sPATH#:}" | ||
) | ||
|
||
# Ensure a sane $PATH beginning with standard utilities. | ||
# POSIXly, a simple 'getconf PATH' should do it, but reality is otherwise. | ||
# Find preferred getconf(1) on NixOS and Solaris/illumos. | ||
# Compile fallback programs on systems without getconf(1). | ||
|
||
DEFPATH=$( | ||
# support Android/Termux, NixOS, Solaris/illumos, generic /bin:/usr/bin | ||
PATH=/data/data/com.termux/files/usr/bin:/run/current-system/sw/bin:/usr/xpg7/bin:/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin:$PATH | ||
getconf PATH 2>/dev/null || { | ||
# no getconf(1) -- try confstr(3) | ||
checkcc | ||
t=${TMPDIR:-/tmp}/path_test.$$ | ||
trap 'set +f; rm -rf "$t."*' 0 | ||
cat > $t.c <<-EOF | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
int main(void) | ||
{ | ||
char buf[8192]; | ||
size_t len = confstr(_CS_PATH, &buf, sizeof(buf)); | ||
if (len > 0 && len < sizeof(buf)) | ||
{ | ||
printf("%s\n", buf); | ||
return 0; | ||
} | ||
return 1; | ||
} | ||
EOF | ||
$CC $CCFLAGS $LDFLAGS -o "$t.exe" "$t.c" 2>/dev/null && "$t.exe" | ||
} || { | ||
# no confstr(3) either -- try non-standard _PATH_DEFPATH | ||
cat > $t.c <<-EOF | ||
#include <stdio.h> | ||
#include <paths.h> | ||
int main(void) | ||
{ | ||
printf("%s\n", _PATH_DEFPATH); | ||
return 0; | ||
} | ||
EOF | ||
$CC $CCFLAGS $LDFLAGS -o "$t.exe" "$t.c" 2>/dev/null && "$t.exe" | ||
} | ||
) || DEFPATH=/bin:/usr/bin:/sbin:/usr/sbin | ||
# Fix for NixOS. Not all POSIX standard utilities come with the default system, | ||
# e.g. 'bc', 'file', 'vi'. The command that NixOS recommends to get missing | ||
# utilities, e.g. 'nix-env -iA nixos.bc', installs them in a default profile | ||
# directory that is not in $(getconf PATH). So add this path to the standard path. | ||
# See: https://github.com/NixOS/nixpkgs/issues/65512 | ||
if test -e /etc/NIXOS && | ||
nix_profile_dir=/nix/var/nix/profiles/default/bin && | ||
test -d "$nix_profile_dir" | ||
then case ":$DEFPATH:" in | ||
*:"$nix_profile_dir":* ) | ||
# nothing to do | ||
;; | ||
* ) # insert the default profile directory as the second entry | ||
DEFPATH=$( | ||
set -f | ||
IFS=: | ||
set -- $DEFPATH | ||
one=$1 | ||
shift | ||
echo "$one:$nix_profile_dir${1+:}$*" | ||
) ;; | ||
esac | ||
fi | ||
# Fix for AIX. At least as of version 7.1, the system default 'find', 'diff -u' and 'patch' utilities | ||
# are broken and/or non-compliant in ways that make them incompatible with POSIX 2018. However, GNU | ||
# utilities are commonly installed in /opt/freeware/bin, and under standard names (no g- prefix). | ||
if test -d /opt/freeware/bin | ||
then case $(uname) in | ||
AIX ) DEFPATH="/opt/freeware/bin:$DEFPATH" ;; | ||
esac | ||
fi | ||
|
||
export DEFPATH # for iffe, etc. | ||
|
||
PATH=$(sanitize_PATH "/opt/ast/bin:$DEFPATH:$PATH") | ||
|
||
# some actions have their own PACKAGEROOT or kick out early | ||
|
||
case $action in | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.