Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Use Modern CMake features instead of CMAKE_CXX_FLAGS #24861

Merged
merged 10 commits into from
Jun 5, 2019
Merged
11 changes: 2 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
27 changes: 12 additions & 15 deletions configurecompiler.cmake
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -71,11 +75,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)
Expand Down Expand Up @@ -187,14 +187,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()

Expand Down Expand Up @@ -477,9 +474,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)
Expand Down Expand Up @@ -513,7 +510,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()

Expand Down Expand Up @@ -593,7 +590,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")
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
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.
Expand Down
2 changes: 1 addition & 1 deletion configureoptimization.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/ToolBox/SOS/Strike/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/ToolBox/SOS/lldbplugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion src/dlls/mscorpe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ilasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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" )
Expand Down
2 changes: 1 addition & 1 deletion src/inc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/jit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions src/pal/src/libunwind/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/pal/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/pal/tests/palsuite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
9 changes: 3 additions & 6 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down