Merge pull request #4760 from dotnet-bot/from-tfs
[platform/upstream/coreclr.git] / compileoptions.cmake
1 if (CLR_CMAKE_PLATFORM_UNIX)
2   # Disable frame pointer optimizations so profilers can get better call stacks
3   add_compile_options(-fno-omit-frame-pointer)
4
5   # The -fms-extensions enable the stuff like __if_exists, __declspec(uuid()), etc.
6   add_compile_options(-fms-extensions )
7   #-fms-compatibility      Enable full Microsoft Visual C++ compatibility
8   #-fms-extensions         Accept some non-standard constructs supported by the Microsoft compiler
9
10   if(CLR_CMAKE_PLATFORM_DARWIN)
11     # We cannot enable "stack-protector-strong" on OS X due to a bug in clang compiler (current version 7.0.2)
12     add_compile_options(-fstack-protector)
13   else()
14     add_compile_options(-fstack-protector-strong)
15   endif(CLR_CMAKE_PLATFORM_DARWIN)
16
17   add_definitions(-DDISABLE_CONTRACTS)
18   # The -ferror-limit is helpful during the porting, it makes sure the compiler doesn't stop
19   # after hitting just about 20 errors.
20   add_compile_options(-ferror-limit=4096)
21
22   # All warnings that are not explicitly disabled are reported as errors
23   add_compile_options(-Werror)
24
25   # Disabled warnings
26   add_compile_options(-Wno-unused-private-field)
27   add_compile_options(-Wno-unused-variable)
28   # Explicit constructor calls are not supported by clang (this->ClassName::ClassName())
29   add_compile_options(-Wno-microsoft)
30   # This warning is caused by comparing 'this' to NULL
31   add_compile_options(-Wno-tautological-compare)
32   # There are constants of type BOOL used in a condition. But BOOL is defined as int
33   # and so the compiler thinks that there is a mistake.
34   add_compile_options(-Wno-constant-logical-operand)
35
36   add_compile_options(-Wno-unknown-warning-option)
37
38   #These seem to indicate real issues
39   add_compile_options(-Wno-invalid-offsetof)
40   # The following warning indicates that an attribute __attribute__((__ms_struct__)) was applied
41   # to a struct or a class that has virtual members or a base class. In that case, clang
42   # may not generate the same object layout as MSVC.
43   add_compile_options(-Wno-incompatible-ms-struct)
44
45   # Some architectures (e.g., ARM) assume char type is unsigned while CoreCLR assumes char is signed
46   # as x64 does. It has been causing issues in ARM (https://github.com/dotnet/coreclr/issues/4746)
47   add_compile_options(-fsigned-char)
48
49 endif(CLR_CMAKE_PLATFORM_UNIX)
50
51 if(CLR_CMAKE_PLATFORM_UNIX_ARM)
52    # Because we don't use CMAKE_C_COMPILER/CMAKE_CXX_COMPILER to use clang
53    # we have to set the triple by adding a compiler argument
54    add_compile_options(-mthumb)
55    add_compile_options(-mfpu=vfpv3)
56    if(ARM_SOFTFP)
57      add_compile_options(-mfloat-abi=softfp)
58      add_compile_options(-target armv7-linux-gnueabi)
59    else()
60      add_compile_options(-target armv7-linux-gnueabihf)
61    endif(ARM_SOFTFP)
62 endif(CLR_CMAKE_PLATFORM_UNIX_ARM)
63
64 if (WIN32)
65   # Compile options for targeting windows
66
67   # The following options are set by the razzle build
68   add_compile_options(/TP) # compile all files as C++
69   add_compile_options(/d2Zi+) # make optimized builds debugging easier
70   add_compile_options(/nologo) # Suppress Startup Banner
71   add_compile_options(/W3) # set warning level to 3
72   add_compile_options(/WX) # treat warnings as errors
73   add_compile_options(/Oi) # enable intrinsics
74   add_compile_options(/Oy-) # disable suppressing of the creation of frame pointers on the call stack for quicker function calls
75   add_compile_options(/U_MT) # undefine the predefined _MT macro
76   add_compile_options(/GF) # enable read-only string pooling
77   add_compile_options(/Gm-) # disable minimal rebuild
78   add_compile_options(/EHa) # enable C++ EH (w/ SEH exceptions)
79   add_compile_options(/Zp8) # pack structs on 8-byte boundary
80   add_compile_options(/Gy) # separate functions for linker
81   add_compile_options(/Zc:wchar_t-) # C++ language conformance: wchar_t is NOT the native type, but a typedef
82   add_compile_options(/Zc:forScope) # C++ language conformance: enforce Standard C++ for scoping rules
83   add_compile_options(/GR-) # disable C++ RTTI
84   add_compile_options(/FC) # use full pathnames in diagnostics
85   add_compile_options(/MP) # Build with Multiple Processes (number of processes equal to the number of processors)
86   add_compile_options(/GS) # Buffer Security Check
87   add_compile_options(/Zm200) # Specify Precompiled Header Memory Allocation Limit of 150MB
88   add_compile_options(/wd4960 /wd4961 /wd4603 /wd4627 /wd4838 /wd4456 /wd4457 /wd4458 /wd4459 /wd4091 /we4640)
89   add_compile_options(/Zi) # enable debugging information
90
91   if (CLR_CMAKE_PLATFORM_ARCH_I386)
92     add_compile_options(/Gz)
93   endif (CLR_CMAKE_PLATFORM_ARCH_I386)
94
95   add_compile_options($<$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>:/GL>)
96   add_compile_options($<$<OR:$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>,$<CONFIG:Checked>>:/O1>)
97
98   if (CLR_CMAKE_PLATFORM_ARCH_AMD64)
99   # The generator expression in the following command means that the /homeparams option is added only for debug builds
100   add_compile_options($<$<CONFIG:Debug>:/homeparams>) # Force parameters passed in registers to be written to the stack
101   endif (CLR_CMAKE_PLATFORM_ARCH_AMD64)
102
103   # enable control-flow-guard support for native components for non-Arm64 builds
104   add_compile_options(/guard:cf) 
105
106   # Statically linked CRT (libcmt[d].lib, libvcruntime[d].lib and libucrt[d].lib) by default. This is done to avoid  
107   # linking in VCRUNTIME140.DLL for a simplified xcopy experience by reducing the dependency on VC REDIST.  
108   #  
109   # For Release builds, we shall dynamically link into uCRT [ucrtbase.dll] (which is pushed down as a Windows Update on downlevel OS) but  
110   # wont do the same for debug/checked builds since ucrtbased.dll is not redistributable and Debug/Checked builds are not  
111   # production-time scenarios.  
112   add_compile_options($<$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>:/MT>)  
113   add_compile_options($<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:/MTd>)  
114
115 endif (WIN32)
116
117 if(CMAKE_ENABLE_CODE_COVERAGE)
118
119   if(CLR_CMAKE_PLATFORM_UNIX)
120     string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
121     if(NOT UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
122       message( WARNING "Code coverage results with an optimised (non-Debug) build may be misleading" )
123     endif(NOT UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
124
125     add_compile_options(-fprofile-arcs)
126     add_compile_options(-ftest-coverage)
127     set(CLANG_COVERAGE_LINK_FLAGS  "--coverage")
128     set(CMAKE_SHARED_LINKER_FLAGS  "${CMAKE_SHARED_LINKER_FLAGS} ${CLANG_COVERAGE_LINK_FLAGS}")
129     set(CMAKE_EXE_LINKER_FLAGS     "${CMAKE_EXE_LINKER_FLAGS} ${CLANG_COVERAGE_LINK_FLAGS}")
130   else()
131     message(FATAL_ERROR "Code coverage builds not supported on current platform")
132   endif(CLR_CMAKE_PLATFORM_UNIX)
133
134 endif(CMAKE_ENABLE_CODE_COVERAGE)