From 90dd13ee1bd497d7724c2b1d0fd833d42f7001ad Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 5 Jun 2019 14:48:55 -0700 Subject: [PATCH] Use Modern CMake features instead of CMAKE_CXX_FLAGS (#24861) * Convert C++ standard settings and warning options from CMAKE__FLAGS to Modern CMake isms. * More $ generator expressions instead of CMAKE_CXX_FLAGS. * Use $ for all -fpermissive usage * Fix generator expression that generates multiple flags * Fix invalid use of CMAKE_CXX_FLAGS instead of CMAKE_C_FLAGS. * Treat AppleClang as though it is Clang (match pre-3.0 behavior). * Update our build system to understand that AppleClang is distinct from Clang and remove CMP0025 policy setting. * PR Feedback. --- CMakeLists.txt | 11 ++--------- configurecompiler.cmake | 27 ++++++++++++--------------- configureoptimization.cmake | 2 +- src/ToolBox/SOS/Strike/CMakeLists.txt | 4 ++-- src/ToolBox/SOS/lldbplugin/CMakeLists.txt | 2 +- src/dlls/mscorpe/CMakeLists.txt | 2 +- src/ilasm/CMakeLists.txt | 2 +- src/inc/CMakeLists.txt | 2 +- src/jit/CMakeLists.txt | 2 +- src/pal/src/libunwind/src/CMakeLists.txt | 6 +++--- src/pal/tests/CMakeLists.txt | 2 +- src/pal/tests/palsuite/CMakeLists.txt | 4 ++-- tests/CMakeLists.txt | 9 +++------ 13 files changed, 31 insertions(+), 44 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ca5a69b..ab5c441 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,7 @@ # Verify minimum required version -cmake_minimum_required(VERSION 2.8.12) +cmake_minimum_required(VERSION 3.5.1) -if(CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0) - cmake_policy(SET CMP0042 NEW) -endif() - -if (NOT WIN32) - set(CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT} -std=c++11" CACHE STRING "Flags used by the compiler during all build types.") - set(CMAKE_C_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} -std=c11" CACHE STRING "Flags used by the compiler during all build types.") -endif() +cmake_policy(SET CMP0042 NEW) # Set the project name project(CoreCLR) diff --git a/configurecompiler.cmake b/configurecompiler.cmake index a8a8ffc..a5e44bb 100644 --- a/configurecompiler.cmake +++ b/configurecompiler.cmake @@ -1,6 +1,10 @@ # Set initial flags for each configuration set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CLR_DEFINES_DEBUG_INIT DEBUG _DEBUG _DBG URTBLDENV_FRIENDLY=Checked BUILDENV_CHECKED=1) set(CLR_DEFINES_CHECKED_INIT DEBUG _DEBUG _DBG URTBLDENV_FRIENDLY=Checked BUILDENV_CHECKED=1) @@ -73,11 +77,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL Darwin) set(CLR_CMAKE_PLATFORM_UNIX 1) set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1) set(CLR_CMAKE_PLATFORM_DARWIN 1) - if(CMAKE_VERSION VERSION_LESS "3.4.0") - set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} -o -c ") - else() - set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} -o -c ") - endif(CMAKE_VERSION VERSION_LESS "3.4.0") + set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} -o -c ") endif(CMAKE_SYSTEM_NAME STREQUAL Darwin) if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD) @@ -189,14 +189,11 @@ if(WIN32) add_compile_options(/Zi /FC /Zc:strictStrings) elseif (CLR_CMAKE_PLATFORM_UNIX) add_compile_options(-g) - # We need to add -Wall to CMAKE__FLAGS since add_compile_options takes precedence - set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") - set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") - if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + add_compile_options(-Wall) + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wno-null-conversion) else() - # We need to add -Werror=conversion-null to CMAKE__FLAGS since add_compile_options takes precedence - set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=conversion-null") + add_compile_options($<$:-Werror=conversion-null>) endif() endif() @@ -479,9 +476,9 @@ if (CLR_CMAKE_PLATFORM_UNIX) add_compile_options(-Wno-unused-function) #These seem to indicate real issues - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-invalid-offsetof") + add_compile_options($<$:-Wno-invalid-offsetof>) - if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # The -ferror-limit is helpful during the porting, it makes sure the compiler doesn't stop # after hitting just about 20 errors. add_compile_options(-ferror-limit=4096) @@ -515,7 +512,7 @@ if (CLR_CMAKE_PLATFORM_UNIX) add_compile_options(-Wno-nonnull-compare) endif() if (COMPILER_SUPPORTS_F_ALIGNED_NEW) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -faligned-new") + add_compile_options($<$:-faligned-new>) endif() endif() @@ -595,7 +592,7 @@ if (WIN32) # enable control-flow-guard support for native components for non-Arm64 builds # Added using variables instead of add_compile_options to let individual projects override it set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /guard:cf") - set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} /guard:cf") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf") # Statically linked CRT (libcmt[d].lib, libvcruntime[d].lib and libucrt[d].lib) by default. This is done to avoid # linking in VCRUNTIME140.DLL for a simplified xcopy experience by reducing the dependency on VC REDIST. diff --git a/configureoptimization.cmake b/configureoptimization.cmake index ad9f4a4..dd26849 100644 --- a/configureoptimization.cmake +++ b/configureoptimization.cmake @@ -6,7 +6,7 @@ if(WIN32) elseif(CLR_CMAKE_PLATFORM_UNIX) set(CLR_CMAKE_ARM_OPTIMIZATION_FALLBACK OFF) if(CLR_CMAKE_TARGET_ARCH STREQUAL "arm" OR CLR_CMAKE_TARGET_ARCH STREQUAL "armel") - if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") AND (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.9)) + if((CMAKE_CXX_COMPILER_ID MATCHES "Clang") AND (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.9)) set(CLR_CMAKE_ARM_OPTIMIZATION_FALLBACK ON) endif() endif() diff --git a/src/ToolBox/SOS/Strike/CMakeLists.txt b/src/ToolBox/SOS/Strike/CMakeLists.txt index 8e0218a..7047e32 100644 --- a/src/ToolBox/SOS/Strike/CMakeLists.txt +++ b/src/ToolBox/SOS/Strike/CMakeLists.txt @@ -106,10 +106,10 @@ if(WIN32) ) else(WIN32) add_definitions(-DPAL_STDCPP_COMPAT=1) - if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wno-null-arithmetic) elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-conversion-null") + add_compile_options($<$:-Wno-conversion-null>) add_compile_options(-Wno-pointer-arith) endif() add_compile_options(-Wno-format) diff --git a/src/ToolBox/SOS/lldbplugin/CMakeLists.txt b/src/ToolBox/SOS/lldbplugin/CMakeLists.txt index 0e4d40a..2b65f52 100644 --- a/src/ToolBox/SOS/lldbplugin/CMakeLists.txt +++ b/src/ToolBox/SOS/lldbplugin/CMakeLists.txt @@ -124,7 +124,7 @@ endif() message(STATUS "LLDB_H: ${LLDB_H}") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-delete-non-virtual-dtor") +add_compile_options($<$:-Wno-delete-non-virtual-dtor>) include_directories(inc) include_directories("${LLDB_H}") diff --git a/src/dlls/mscorpe/CMakeLists.txt b/src/dlls/mscorpe/CMakeLists.txt index 43deb47..6a89193 100644 --- a/src/dlls/mscorpe/CMakeLists.txt +++ b/src/dlls/mscorpe/CMakeLists.txt @@ -10,7 +10,7 @@ set(MSCORPE_SOURCES ) if(NOT WIN32) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-delete-non-virtual-dtor") + add_compile_options($<$:-Wno-delete-non-virtual-dtor>) endif() add_library_clr(mscorpe STATIC diff --git a/src/ilasm/CMakeLists.txt b/src/ilasm/CMakeLists.txt index 3d1a74d..f780dca 100644 --- a/src/ilasm/CMakeLists.txt +++ b/src/ilasm/CMakeLists.txt @@ -51,7 +51,7 @@ if(CLR_CMAKE_PLATFORM_UNIX) # Need generate a right form of asmparse.cpp to avoid the following options. # Clang also produces a bad-codegen on this prebuilt file with optimization. # https://github.com/dotnet/coreclr/issues/2305 - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-delete-non-virtual-dtor -Wno-register") + add_compile_options("$<$:-Wno-delete-non-virtual-dtor;-Wno-register>") add_compile_options(-Wno-array-bounds) add_compile_options(-Wno-unused-label) set_source_files_properties( prebuilt/asmparse.cpp PROPERTIES COMPILE_FLAGS "-O0" ) diff --git a/src/inc/CMakeLists.txt b/src/inc/CMakeLists.txt index 517ea8e..c38d595 100644 --- a/src/inc/CMakeLists.txt +++ b/src/inc/CMakeLists.txt @@ -49,7 +49,7 @@ else() # The prebuilt files contain extra '!_MIDL_USE_GUIDDEF_' after the #endif, but not in the comment. # In order to not to have to modify these prebuilt files, we disable the extra tokens warning. -if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wno-extra-tokens) elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") add_compile_options(-Wno-endif-labels) diff --git a/src/jit/CMakeLists.txt b/src/jit/CMakeLists.txt index 5e7c9c5..8a34756 100644 --- a/src/jit/CMakeLists.txt +++ b/src/jit/CMakeLists.txt @@ -4,7 +4,7 @@ include_directories("./jitstd") include_directories("../inc") if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive") + add_compile_options($<$:-fpermissive>) add_compile_options(-Wno-error) endif() diff --git a/src/pal/src/libunwind/src/CMakeLists.txt b/src/pal/src/libunwind/src/CMakeLists.txt index b29e7ee..93b4daf 100644 --- a/src/pal/src/libunwind/src/CMakeLists.txt +++ b/src/pal/src/libunwind/src/CMakeLists.txt @@ -22,7 +22,7 @@ add_definitions("-Ddwarf_search_unwind_table_int=UNW_OBJ(dwarf_search_unwind_tab # Disable warning due to incorrect format specifier in debugging printf via the Debug macro add_compile_options(-Wno-format) -if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wno-header-guard) elseif() add_compile_options(-Wno-unused-value) @@ -33,7 +33,7 @@ if(CLR_CMAKE_PLATFORM_ARCH_ARM) add_definitions("-Darm_search_unwind_table=UNW_OBJ(arm_search_unwind_table)") # Disable warning in asm: use of SP or PC in the list is deprecated add_compile_options(-Wno-inline-asm) - if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # Disable warning due to labs function called on unsigned argument add_compile_options(-Wno-absolute-value) endif() @@ -48,7 +48,7 @@ if(CLR_CMAKE_PLATFORM_ARCH_ARM) # the include/tdep-arm to include directories include_directories(../include/tdep-arm) elseif(CLR_CMAKE_PLATFORM_ARCH_ARM64) - if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # Disable warning due to labs function called on unsigned argument add_compile_options(-Wno-absolute-value) endif() diff --git a/src/pal/tests/CMakeLists.txt b/src/pal/tests/CMakeLists.txt index 5597922..0ec630d 100644 --- a/src/pal/tests/CMakeLists.txt +++ b/src/pal/tests/CMakeLists.txt @@ -32,7 +32,7 @@ endif() # C++ emits errors and warnings for c-string literal fed into char* parameter # this is just to take care of the warnings -if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wno-writable-strings) elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") add_compile_options(-Wno-write-strings) diff --git a/src/pal/tests/palsuite/CMakeLists.txt b/src/pal/tests/palsuite/CMakeLists.txt index 6726165..00a4301 100644 --- a/src/pal/tests/palsuite/CMakeLists.txt +++ b/src/pal/tests/palsuite/CMakeLists.txt @@ -14,13 +14,13 @@ endif() list(APPEND COMMON_TEST_LIBRARIES coreclrpal) -if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wno-incompatible-pointer-types-discards-qualifiers) add_compile_options(-Wno-int-to-void-pointer-cast) elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") add_compile_options(-Wno-sign-compare) add_compile_options(-Wno-narrowing) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive" ) + add_compile_options($<$:-fpermissive>) add_compile_options(-Wno-int-to-pointer-cast) endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 54ea1ec..937ecd3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,10 +1,7 @@ -# Require at least version 2.8.12 of CMake -cmake_minimum_required(VERSION 2.8.12) +# Require at least version 3.5.1 of CMake +cmake_minimum_required(VERSION 3.5.1) -if (NOT WIN32) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11") -endif() +cmake_policy(SET CMP0042 NEW) set(INC_PLATFORM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/Common/Platform) if (WIN32) -- 2.7.4