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