Files
runc/script/lib.sh
T
Kir Kolyshkin 78c3b60426 script: do not modify CFLAGS in set_cross_vars
When cross-building libseccomp, build_libseccomp resets CFLAGS to an
empty value (if it was unset) before each architecture, and
set_cross_vars exports it. Autoconf only applies its default CFLAGS
(-g -O2) when CFLAGS is unset, so the per-architecture libseccomp
builds (which are what release binaries link against, including for
the native architecture) end up unoptimized.

The only reason CFLAGS was touched at all is that set_cross_vars puts
-m32 -march=... there for 386, which is why callers had to save and
restore it. Pass these flags via CC instead (as autoconf documentation
suggests for multilib builds), so set_cross_vars no longer modifies
CFLAGS, and remove the now-unneeded save/restore dance from both
build-seccomp.sh and release_build.sh.

This way, a user-provided CFLAGS is passed as is, and if it is unset,
configure uses its defaults for all architectures, including 386.

Note that go build does not use CFLAGS (cgo uses CGO_CFLAGS), and it
handles CC with arguments just fine.

Reported-by: Ciprian Hacman <ciprian@hakman.dev>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-09-16 16:40:02 -07:00

91 lines
1.9 KiB
Bash

#!/bin/bash
# get_platform computes the platform section of target triples on this OS.
function get_platform() {
# Fedora doesn't have ID_LIKE and only has ID=fedora, so we need to
# construct a fake ID_LIKE to treat AlmaLinux and Fedora the same way.
local ID_LIKE
# shellcheck source=/etc/os-release disable=SC1091 # outside our sources
ID_LIKE="$(
source /etc/os-release
echo "${ID:-} ${ID_LIKE:-}"
)"
local PLATFORM
case "$ID_LIKE" in
*suse*)
PLATFORM=suse-linux
;;
*rhel* | *fedora* | *centos*)
PLATFORM=redhat-linux
;;
*)
PLATFORM=linux-gnu
;;
esac
echo "$PLATFORM"
}
# set_cross_vars sets a few environment variables used for cross-compiling,
# based on the architecture specified in $1.
function set_cross_vars() {
GOARCH="$1" # default, may be overridden below
local cc_flags=""
unset GOARM
PLATFORM="$(get_platform)"
[[ "$PLATFORM" == *suse* ]] && is_suse=1
case "$1" in
386)
# Always use the 64-bit compiler to build the 386 binary, which works
# for the more common cross-build method for x86 (namely, the
# equivalent of dpkg --add-architecture).
local cpu_type
if [ -v is_suse ]; then
cpu_type=i586
else
cpu_type=i686
fi
HOST=x86_64-${PLATFORM}
# Pass these via CC rather than CFLAGS, so that autoconf
# still uses its default CFLAGS (-g -O2) when CFLAGS is unset.
cc_flags=" -m32 -march=$cpu_type"
;;
amd64)
HOST=x86_64-${PLATFORM}
;;
arm64)
HOST=aarch64-${PLATFORM}
;;
armel)
HOST=arm-${PLATFORM}eabi
GOARCH=arm
GOARM=5
;;
armhf)
HOST=arm-${PLATFORM}eabihf
GOARCH=arm
GOARM=7
;;
ppc64le)
HOST=powerpc64le-${PLATFORM}
;;
riscv64)
HOST=riscv64-${PLATFORM}
;;
s390x)
HOST=s390x-${PLATFORM}
;;
*)
echo "set_cross_vars: unsupported architecture: $1" >&2
exit 1
;;
esac
CC="${HOST:+$HOST-}gcc${cc_flags}"
STRIP="${HOST:+$HOST-}strip"
export HOST GOARM GOARCH CC STRIP
}