CMake: cross builds silently use the host OpenSSL when the toolchain file names no target root
Reported in discussion #5431 (aarch64, crosstool-NG toolchain); #3829 was the same situation with the GNU make build.
Problem
A toolchain file that sets the CMAKE_FIND_ROOT_PATH_MODE_* ONLY modes but neither CMAKE_SYSROOT nor CMAKE_FIND_ROOT_PATH gives CMake nothing to re-root, so find_package(OpenSSL) finds the host's OpenSSL in /usr/include and /usr/lib64. Crypto, NetSSL and JWT are then enabled, and the build fails far from the cause, in Crypto, with openssl/opensslv.h: No such file or directory: CMake omits /usr/include from the compile line when the compiler's implicit include path already ends in usr/include, which holds for a cross toolchain's sysroot. Nothing at configure time indicates that the wrong OpenSSL was picked.
Proposed changes
- Configure-time check in the top-level
CMakeLists.txt: whenCMAKE_CROSSCOMPILINGis set andOPENSSL_INCLUDE_DIRis not belowCMAKE_SYSROOT,CMAKE_FIND_ROOT_PATHorCMAKE_STAGING_PREFIX, emitmessage(WARNING ...)naming the directory and pointing toCMAKE_FIND_ROOT_PATH/OPENSSL_ROOT_DIRand toENABLE_CRYPTO,ENABLE_NETSSLandENABLE_JWT. Written as a helper so that MySQL, PostgreSQL, ODBC and APR can get the same check. - Documentation (
README.md, section "Cross-Compiling", anddoc/00200-GettingStarted.page): the toolchain file must name the target root withCMAKE_SYSROOTorCMAKE_FIND_ROOT_PATH; external dependencies must be built for the target and installed below that root; theENABLE_*options turn them off; a minimal toolchain file example.
Acceptance
- Configuring with the toolchain file from #5431 prints the warning naming the host directory.
- Configuring with
CMAKE_FIND_ROOT_PATHset to a root that contains an OpenSSL for the target prints no warning. - Native builds are unchanged.
Source: pocoproject/poco