From 88195f40b8f28f9a677a548bd34d9884e64c0a4c Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Sun, 24 Feb 2019 14:11:21 +0100 Subject: [PATCH 1/3] Leave CMAKE_CXX_FLAGS alone --- CMakeLists.txt | 67 ++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1272bc341..9d6be231f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -420,23 +420,23 @@ if(NOT (CMAKE_VERSION VERSION_LESS "3.1")) else() if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") AND NOT WIN32) if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.1.1") - set(MY_CXX_FLAGS "${MY_CXX_FLAGS} -std=c++11") + add_compile_options(-std=c++11) else() - set(MY_CXX_FLAGS "${MY_CXX_FLAGS} -std=c++14") + add_compile_options(-std=c++14) endif() endif() if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") if(OS_IS_MACOSX) if(CLANG_VERSION VERSION_LESS "600") - set(MY_CXX_FLAGS "${MY_CXX_FLAGS} -std=c++11") + add_compile_options(-std=c++11) else() - set(MY_CXX_FLAGS "${MY_CXX_FLAGS} -std=c++14") + add_compile_options(-std=c++14) endif() else() if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.5.0") - set(MY_CXX_FLAGS "${MY_CXX_FLAGS} -std=c++11") + add_compile_options(-std=c++11) else() - set(MY_CXX_FLAGS "${MY_CXX_FLAGS} -std=c++14") + add_compile_options(-std=c++14) endif() endif() endif() @@ -1834,10 +1834,10 @@ if(ENABLE_GPERFTOOLS) # See https://github.com/gperftools/gperftools/blob/master/README if(GPERFTOOLS_FOUND) if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") AND NOT WIN32) - set(MY_CXX_FLAGS "${MY_CXX_FLAGS} -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free") + add_compile_options(-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free) endif() if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(MY_CXX_FLAGS "${MY_CXX_FLAGS} -fno-builtin") + add_compile_options(-fno-builtin) endif() endif() endif() @@ -1848,7 +1848,7 @@ endif() # GNU gprof (OPTIONAL) - https://sourceware.org/binutils/docs/gprof/ ######################################################################## if(ENABLE_GPROF) - set(MY_CXX_FLAGS "${MY_CXX_FLAGS} -pg") + add_compile_options(-pg) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg") endif() @@ -1858,35 +1858,38 @@ endif() # Set compiler flags ######################################################################## if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") AND NOT WIN32) - set(MY_CXX_FLAGS "${MY_CXX_FLAGS} -Wall -Wextra") #Add warning flags: For "-Wall" see https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html + # Add warning flags + # For "-Wall" see https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html + add_compile_options(-Wall -Wextra) + if(CMAKE_CROSSCOMPILING) + add_compile_options(-Wno-psabi) + endif() endif() # Processor-architecture related flags # See https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html -if(NOT ARCH_COMPILER_FLAGS) - if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") AND NOT WIN32) - if(OS_IS_MACOSX) - set(ARCH_COMPILER_FLAGS "-march=corei7 -mfpmath=sse") - else() - if(NOT ${ENABLE_GENERIC_ARCH}) - if(IS_ARM) # ARM-specific options (https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html) - if(NOT CMAKE_CROSSCOMPILING) - if(ARM_VERSION STREQUAL "arm") - # Unknown arm version - try our best to detect - set(ARCH_COMPILER_FLAGS "-mcpu=native") - else() - set(ARCH_COMPILER_FLAGS "-march=${ARM_VERSION}") - endif() - endif() - else() - set(ARCH_COMPILER_FLAGS "-march=native -mfpmath=sse") - endif() - endif() - endif() +if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") AND NOT WIN32) + if(OS_IS_MACOSX) + add_compile_options(-march=corei7 -mfpmath=sse) + else() + if(NOT ENABLE_GENERIC_ARCH) + if(IS_ARM) + # ARM-specific options + # See https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html + if(NOT CMAKE_CROSSCOMPILING) + if(ARM_VERSION STREQUAL "arm") + # Unknown arm version - try our best to detect + add_compile_options(-mcpu=native) + else() + add_compile_options(-march=${ARM_VERSION}) + endif() + endif() + else() + add_compile_options(-march=native -mfpmath=sse) + endif() + endif() endif() endif() -set(MY_CXX_FLAGS "${MY_CXX_FLAGS} ${ARCH_COMPILER_FLAGS}") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MY_CXX_FLAGS}") From 46f985d7632f3599d5ae29f046f39dc3fb5b4dae Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Sun, 24 Feb 2019 14:35:18 +0100 Subject: [PATCH 2/3] Fix for CMake < 3.1 --- CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d6be231f..c988a8003 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -420,23 +420,23 @@ if(NOT (CMAKE_VERSION VERSION_LESS "3.1")) else() if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") AND NOT WIN32) if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.1.1") - add_compile_options(-std=c++11) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") else() - add_compile_options(-std=c++14) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") endif() endif() if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") if(OS_IS_MACOSX) if(CLANG_VERSION VERSION_LESS "600") - add_compile_options(-std=c++11) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") else() - add_compile_options(-std=c++14) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") endif() else() if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.5.0") - add_compile_options(-std=c++11) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") else() - add_compile_options(-std=c++14) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") endif() endif() endif() From 869aadc731e2f67304292d173313ca993cfe6bc0 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Sun, 24 Feb 2019 19:17:32 +0100 Subject: [PATCH 3/3] Pass warning flags only to C++ targets --- CMakeLists.txt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c988a8003..ccedec774 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -418,25 +418,26 @@ if(NOT (CMAKE_VERSION VERSION_LESS "3.1")) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_EXTENSIONS OFF) else() + add_compile_options("$<$,C>:-std=gnu11>") if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") AND NOT WIN32) if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.1.1") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + add_compile_options("$<$,CXX>:-std=c++11>") else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") + add_compile_options("$<$,CXX>:-std=c++14>") endif() endif() if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") if(OS_IS_MACOSX) if(CLANG_VERSION VERSION_LESS "600") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + add_compile_options("$<$,CXX>:-std=c++11>") else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") + add_compile_options("$<$,CXX>:-std=c++14>") endif() else() if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.5.0") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + add_compile_options("$<$,CXX>:-std=c++11>") else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") + add_compile_options("$<$,CXX>:-std=c++14>") endif() endif() endif() @@ -1860,7 +1861,8 @@ endif() if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") AND NOT WIN32) # Add warning flags # For "-Wall" see https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html - add_compile_options(-Wall -Wextra) + set(cxx_warning_flags -Wall -Wextra) + add_compile_options("$<$,CXX>:${cxx_warning_flags}>") if(CMAKE_CROSSCOMPILING) add_compile_options(-Wno-psabi) endif()