cherry-pick: scripts: imp mips compat

Updates #4269.

Squashed commit of the following:

commit f633e875f4f0ab767a0537d9bfe95734823f8a51
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Feb 11 17:33:53 2022 +0300

    scripts: imp mips compat
This commit is contained in:
Ainar Garipov 2022-02-11 17:41:36 +03:00 committed by Ainar Garipov
parent 6cfdbef1a5
commit 3e240741f1
1 changed files with 18 additions and 1 deletions

View File

@ -39,8 +39,25 @@ is_command() {
}
# Function is_little_endian checks if the CPU is little-endian.
#
# See https://serverfault.com/a/163493/267530.
is_little_endian() {
[ "$( head -c 6 /bin/sh | tail -c 1 )" = "$( printf '\001' )" ]
# The ASCII character "I" has the octal code of 111. In the two-byte octal
# display mode (-o), hexdump will print it either as "000111" on a little
# endian system or as a "111000" on a big endian one. Return the sixth
# character to compare it against the number '1'.
#
# Do not use echo -n, because its behavior in the presence of the -n flag is
# explicitly implementation-defined in POSIX. Use hexdump instead of od,
# because OpenWrt and its derivatives have the former but not the latter.
is_little_endian_result="$(
printf 'I'\
| hexdump -o\
| awk '{ print substr($2, 6, 1); exit; }'
)"
readonly is_little_endian_result
[ "$is_little_endian_result" -eq '1' ]
}
# Function check_required checks if the required software is available on the