Use Modern CMake features instead of CMAKE_CXX_FLAGS (#24861)
authorJeremy Koritzinsky <jekoritz@microsoft.com>
Wed, 5 Jun 2019 21:48:55 +0000 (14:48 -0700)
committerGitHub <noreply@github.com>
Wed, 5 Jun 2019 21:48:55 +0000 (14:48 -0700)
* Convert C++ standard settings and warning options from CMAKE_<LANG>_FLAGS to Modern CMake isms.

* More $<COMPILE_LANGUAGE> generator expressions instead of CMAKE_CXX_FLAGS.

* Use $<COMPILE_LANGUAGE:CXX> 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.

13 files changed:
CMakeLists.txt
configurecompiler.cmake
configureoptimization.cmake
src/ToolBox/SOS/Strike/CMakeLists.txt
src/ToolBox/SOS/lldbplugin/CMakeLists.txt
src/dlls/mscorpe/CMakeLists.txt
src/ilasm/CMakeLists.txt
src/inc/CMakeLists.txt
src/jit/CMakeLists.txt
src/pal/src/libunwind/src/CMakeLists.txt
src/pal/tests/CMakeLists.txt
src/pal/tests/palsuite/CMakeLists.txt
tests/CMakeLists.txt

index ca5a69b..ab5c441 100644 (file)
@@ -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)
index a8a8ffc..a5e44bb 100644 (file)
@@ -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} <FLAGS> <DEFINES> -o <OBJECT> -c <SOURCE>")
-  else()
-    set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> <INCLUDES> -o <OBJECT> -c <SOURCE>")
-  endif(CMAKE_VERSION VERSION_LESS "3.4.0")
+  set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> <INCLUDES> -o <OBJECT> -c <SOURCE>")
 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_<LANG>_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_<LANG>_FLAGS since add_compile_options takes precedence
-        set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=conversion-null")
+        add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-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($<$<COMPILE_LANGUAGE:CXX>:-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($<$<COMPILE_LANGUAGE:CXX>:-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.
index ad9f4a4..dd26849 100644 (file)
@@ -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()
index 8e0218a..7047e32 100644 (file)
@@ -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($<$<COMPILE_LANGUAGE:CXX>:-Wno-conversion-null>)
     add_compile_options(-Wno-pointer-arith)
   endif()
   add_compile_options(-Wno-format)
index 0e4d40a..2b65f52 100644 (file)
@@ -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($<$<COMPILE_LANGUAGE:CXX>:-Wno-delete-non-virtual-dtor>)
 
 include_directories(inc)
 include_directories("${LLDB_H}")
index 43deb47..6a89193 100644 (file)
@@ -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($<$<COMPILE_LANGUAGE:CXX>:-Wno-delete-non-virtual-dtor>)
 endif()
 
 add_library_clr(mscorpe STATIC
index 3d1a74d..f780dca 100644 (file)
@@ -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("$<$<COMPILE_LANGUAGE:CXX>:-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" )
index 517ea8e..c38d595 100644 (file)
@@ -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)
index 5e7c9c5..8a34756 100644 (file)
@@ -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($<$<COMPILE_LANGUAGE:CXX>:-fpermissive>)
   add_compile_options(-Wno-error)
 endif()
 
index b29e7ee..93b4daf 100644 (file)
@@ -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()
index 5597922..0ec630d 100644 (file)
@@ -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)
index 6726165..00a4301 100644 (file)
@@ -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($<$<COMPILE_LANGUAGE:CXX>:-fpermissive>)
   add_compile_options(-Wno-int-to-pointer-cast)
 endif()
 
index 54ea1ec..937ecd3 100644 (file)
@@ -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)