cmake: AddZstdCompilationFlags still depends on C++ after #4357
Since #4357 the cmake build enables C++ only for the tests, but AddZstdCompilationFlags.cmake still depends on it. For MinGW targets configure fails as soon as CMAKE_CXX_COMPILER is set, which is what cross toolchain files usually do.
Tested on 01b7154f1172432f8abe9b3bb9909e14a1176b7d. Works fine on the latest release (v1.5.7).
Steps to reproduce
cmake -S build/cmake -B out \
-DCMAKE_SYSTEM_NAME=Windows \
-DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc \
-DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++CMake Error at /usr/share/cmake-4.2/Modules/Internal/CheckFlagCommonConfig.cmake:72 (message):
check_compiler_flag: CXX: needs to be enabled before use.The same command works without -DCMAKE_CXX_COMPILER or with -DZSTD_BUILD_TESTS=ON. The first commit that fails is 769723aee2.
Cause
EnableCompilerFlag() checks C++ flags when CMAKE_CXX_COMPILER is set, which does not tell whether the language is enabled:
if (_CXX AND CMAKE_CXX_COMPILER)
CHECK_CXX_COMPILER_FLAG(${_flag} CXX_FLAG_${varname})_CXX is always true here. ADD_ZSTD_COMPILATION_FLAGS passes _C _CXX on without ${}, so the function gets the string _CXX and not the value of ZSTD_ENABLE_CXX.
Also affects native compilation
Default build silently loses all flags and the assembly. The block is guarded by CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang", which is empty without C++, and ZSTD_HAS_NOEXECSTACK needs CXX_FLAG_WA_NOEXECSTACK. On x86-64 Linux with gcc 15.2.0:
$ cmake -S build/cmake -B out
$ grep C_FLAGS out/lib/CMakeFiles/libzstd_static.dir/flags.make
C_FLAGS = -O3 -DNDEBUG -fPIC -DZSTD_DISABLE_ASM
$ cmake -S build/cmake -B out2 -DZSTD_BUILD_TESTS=ON
$ grep C_FLAGS out2/lib/CMakeFiles/libzstd_static.dir/flags.make
C_FLAGS = -Wall -Wextra -Wundef -Wshadow -Wcast-align -Wcast-qual -Wstrict-prototypes -Wa,--noexecstack -O3 -DNDEBUG -fPICPossible fix
Use CMAKE_C_COMPILER_ID for the guard, check C++ flags only if CMAKE_CXX_COMPILER_LOADED is set, and do not require CXX_FLAG_WA_NOEXECSTACK when C++ is not enabled.
Source: facebook/zstd