From 27e9285121002e1dcec1ca0d4325a6d144c3ee72 Mon Sep 17 00:00:00 2001 From: Aleksander Morgado Date: Mon, 21 Oct 2019 16:41:07 +0200 Subject: [PATCH] build: fix ADD_CXX_COMPILER_FLAG_IF_AVAILABLE usages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch solves two different issues at the same time when building with gcc/g++ 9.2.0: 1) The -fvisibility=hidden support check was done using the C++ compiler only (with check_cxx_compiler_flag), inside the block corresponding to the C compiler being GNU/clang, and the result of the check was applied to both C++ (CXX_FLAGS) and C (C_FLAGS) flags. Instead of this, there should be separate checks for the C and C++ compilers, each of them modifying a single set of _FLAGS. 2) -Wincompatible-pointer-types support check was done using the C++ compiler only, and the result of the check was applied to both C++ (CXX_FLAGS) and C (C_FLAGS) flags. But, this warning is not applicable to C++ and actually breaks compilation when using g++ 9.2: [ 0%] Building CXX object lib/src/asn1/CMakeFiles/rrc_asn1.dir/rrc_asn1.cc.o cc1plus: error: ‘-Werror=’ argument ‘-Werror=incompatible-pointer-types’ is not valid for C++ [-Werror] Instead of this, there should be a check for this warning only using the C compiler, and therefore only modifying the C flags (C_FLAGS). This patch splits the macro into one specific for C++ (which modifies CXX_FLAGS) and one specific for C (which modifies C_FLAGS). And so, the macro call to check for `-Werror=incompatible-pointer-types' is made C-only, and the one for `-fvisibility=hidden` is done for both C and C++ targets (each on the correct GNU/clang compiler block). Due to how the tests are done in cmake, the '-fvisibility=hidden' check wasn't even succeding before, so the compiler option wasn't being effectively used. The cmake flags.make file contents throughout the project are updated as follows now: Before this change, we had: C_FLAGS = -Werror=incompatible-pointer-types ... CXX_FLAGS = -Werror=incompatible-pointer-types ... And after this change, we have: C_FLAGS = -Werror=incompatible-pointer-types -fvisibility=hidden ... CXX_FLAGS = -fvisibility=hidden ... --- CMakeLists.txt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f38e8b807..a161cf092 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -275,10 +275,18 @@ macro(ADD_CXX_COMPILER_FLAG_IF_AVAILABLE flag have) include(CheckCXXCompilerFlag) check_cxx_compiler_flag(${flag} ${have}) if(${have}) - add_definitions(${flag}) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") endif(${have}) endmacro(ADD_CXX_COMPILER_FLAG_IF_AVAILABLE) +macro(ADD_C_COMPILER_FLAG_IF_AVAILABLE flag have) + include(CheckCCompilerFlag) + check_c_compiler_flag(${flag} ${have}) + if(${have}) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}") + endif(${have}) +endmacro(ADD_C_COMPILER_FLAG_IF_AVAILABLE) + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-comment -Wno-reorder -Wno-unused-but-set-variable -Wno-unused-variable -Wtype-limits -std=c++11") @@ -292,9 +300,13 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=${GCC_ARCH} -mfpmath=sse -msse4.1 -DLV_HAVE_SSE") endif(HAVE_AVX) endif (HAVE_AVX2) + + if(NOT WIN32) + ADD_CXX_COMPILER_FLAG_IF_AVAILABLE(-fvisibility=hidden HAVE_VISIBILITY_HIDDEN_CXX) + endif(NOT WIN32) endif(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") -ADD_CXX_COMPILER_FLAG_IF_AVAILABLE("-Werror=incompatible-pointer-types" HAVE_ERROR_INCOMPATIBLE) +ADD_C_COMPILER_FLAG_IF_AVAILABLE("-Werror=incompatible-pointer-types" HAVE_ERROR_INCOMPATIBLE) if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-comment -Wno-write-strings -Winline -Wno-unused-result -Wformat -Wmissing-field-initializers -Wtype-limits -std=c99 -D_GNU_SOURCE") @@ -363,7 +375,7 @@ if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang") endif(NOT HAVE_SSE AND NOT HAVE_NEON AND NOT DISABLE_SIMD) if(NOT WIN32) - ADD_CXX_COMPILER_FLAG_IF_AVAILABLE(-fvisibility=hidden HAVE_VISIBILITY_HIDDEN) + ADD_C_COMPILER_FLAG_IF_AVAILABLE(-fvisibility=hidden HAVE_VISIBILITY_HIDDEN_C) endif(NOT WIN32) if (ENABLE_ASAN AND ENABLE_MSAN)