From d2c9a7aa7b4bafd4619daa58e71fcdf1f9f5ad0b Mon Sep 17 00:00:00 2001 From: Mobin Aydinfar Date: Thu, 25 Dec 2025 15:24:00 +0330 Subject: [PATCH] configure: improve cross-compilation detection Assume the build is native if the compiler for the host and the build machine are the same and both use the same flags. It is fairly common to set both variables to the same compiler with same flags in a native build. For example, cbuild (Chimera Linux package builder) defines both the host and the build compiler and their respective compiler flags, regardless of native or cross build. Also, that assumption in this case is consistent with GNU autotools behaviour. --- BUILD | 12 ++++++++++-- configure | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/BUILD b/BUILD index c99167bc..bae85684 100644 --- a/BUILD +++ b/BUILD @@ -168,8 +168,16 @@ Consult compiler documentation for further information on the above options. Cross-compilation with "configure" script =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -If cross-compiling (i.e. specifying the CXX_FOR_BUILD variable), there are some additional -considerations regarding use of the "configure" script. +If the CXX_FOR_BUILD variable is specified, the "configure" script will assume the build is a +cross-build, unless all of the following conditions are met: + +* Both the host compiler (CXX variable) and the build compiler (CXX_FOR_BUILD variable) are the +same. +* Their respective compiler flags (CXXFLAGS and CXXFLAGS_FOR_BUILD variables) are the same or both +unset. +* The CXXFLAGS_EXTRA variable is unset. + +If all of these conditions are met, the "configure" script will instead assume the build is native. Some build options are currently defaulted based on the detected platform (operating system). When doing a cross-build, the target platform is not automatically detected, and "configure" prints a diff --git a/configure b/configure index c3ea70b4..be25f5a1 100755 --- a/configure +++ b/configure @@ -450,6 +450,21 @@ for arg in "$@"; do esac done +## Verify the build is a cross-build when CXX_FOR_BUILD is specified +if [ -n "${CXX_FOR_BUILD:-}" ]; then + # The $CXX_FOR_BUILD is a variable set by the user to the compiler for the build machine. + # Later parts of this script assume the build is a cross-build when the $CXX_FOR_BUILD is set. + # The following check ensures that either the host compiler and the build compiler and their + # respective compiler flags are actually different or that $CXX_FOR_BUILD is unset. + if [ "$CXX_FOR_BUILD" = "$CXX" ]; then + if [ -z "${CXXFLAGS_EXTRA:-}" ]; then + if [ "${CXXFLAGS:-}" = "${CXXFLAGS_FOR_BUILD:-}" ]; then + unset CXX_FOR_BUILD + fi + fi + fi +fi + ## Check platform is specified for a cross-build if [ -n "${CXX_FOR_BUILD:-}" ]; then if [ -z "${PLATFORM:-}" ]; then