Set vtable offset as contained
[platform/upstream/coreclr.git] / configurecompiler.cmake
1 # Set initial flags for each configuration
2
3 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
4 set(CMAKE_C_STANDARD 11)
5 set(CMAKE_C_STANDARD_REQUIRED ON)
6 set(CMAKE_CXX_STANDARD 11)
7 set(CMAKE_CXX_STANDARD_REQUIRED ON)
8
9 # All code we build should be compiled as position independent
10 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
11
12 set(CLR_DEFINES_DEBUG_INIT              DEBUG _DEBUG _DBG URTBLDENV_FRIENDLY=Checked BUILDENV_CHECKED=1)
13 set(CLR_DEFINES_CHECKED_INIT            DEBUG _DEBUG _DBG URTBLDENV_FRIENDLY=Checked BUILDENV_CHECKED=1)
14 set(CLR_DEFINES_RELEASE_INIT            NDEBUG URTBLDENV_FRIENDLY=Retail)
15 set(CLR_DEFINES_RELWITHDEBINFO_INIT     NDEBUG URTBLDENV_FRIENDLY=Retail)
16
17 include(${CMAKE_CURRENT_LIST_DIR}/configureoptimization.cmake)
18
19 #----------------------------------------
20 # Detect and set platform variable names
21 #     - for non-windows build platform & architecture is detected using inbuilt CMAKE variables and cross target component configure
22 #     - for windows we use the passed in parameter to CMAKE to determine build arch
23 #----------------------------------------
24 if(CMAKE_SYSTEM_NAME STREQUAL Linux)
25     set(CLR_CMAKE_PLATFORM_UNIX 1)
26     if(CLR_CROSS_COMPONENTS_BUILD)
27         # CMAKE_HOST_SYSTEM_PROCESSOR returns the value of `uname -p` on host.
28         if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL amd64)
29             if(CLR_CMAKE_TARGET_ARCH STREQUAL "arm" OR CLR_CMAKE_TARGET_ARCH STREQUAL "armel")
30                 if($ENV{CROSSCOMPILE} STREQUAL "1")
31                     set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
32                 else()
33                     set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
34                 endif()
35             else()
36                 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
37             endif()
38         elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL i686)
39             set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
40         else()
41             clr_unknown_arch()
42         endif()
43     else()
44         # CMAKE_SYSTEM_PROCESSOR returns the value of `uname -p` on target.
45         # For the AMD/Intel 64bit architecture two different strings are common.
46         # Linux and Darwin identify it as "x86_64" while FreeBSD and netbsd uses the
47         # "amd64" string. Accept either of the two here.
48         if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL amd64)
49             set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
50         elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l)
51             set(CLR_CMAKE_PLATFORM_UNIX_ARM 1)
52         elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
53             set(CLR_CMAKE_PLATFORM_UNIX_ARM 1)
54         elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
55             set(CLR_CMAKE_PLATFORM_UNIX_ARM64 1)
56         elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL i686)
57             set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
58         else()
59             clr_unknown_arch()
60         endif()
61     endif()
62     set(CLR_CMAKE_PLATFORM_LINUX 1)
63
64     # Detect Linux ID
65     if(DEFINED CLR_CMAKE_LINUX_ID)
66         if(CLR_CMAKE_LINUX_ID STREQUAL ubuntu)
67             set(CLR_CMAKE_TARGET_UBUNTU_LINUX 1)
68         elseif(CLR_CMAKE_LINUX_ID STREQUAL tizen)
69             set(CLR_CMAKE_TARGET_TIZEN_LINUX 1)
70         elseif(CLR_CMAKE_LINUX_ID STREQUAL alpine)
71             set(CLR_CMAKE_PLATFORM_ALPINE_LINUX 1)
72         endif()
73         if(CLR_CMAKE_LINUX_ID STREQUAL ubuntu)
74             set(CLR_CMAKE_PLATFORM_UBUNTU_LINUX 1)
75         endif()
76     endif(DEFINED CLR_CMAKE_LINUX_ID)
77 endif(CMAKE_SYSTEM_NAME STREQUAL Linux)
78
79 if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
80   set(CLR_CMAKE_PLATFORM_UNIX 1)
81   set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
82   set(CLR_CMAKE_PLATFORM_DARWIN 1)
83   set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> <INCLUDES> -o <OBJECT> -c <SOURCE>")
84 endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
85
86 if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
87   set(CLR_CMAKE_PLATFORM_UNIX 1)
88   set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
89   set(CLR_CMAKE_PLATFORM_FREEBSD 1)
90 endif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
91
92 if(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
93   set(CLR_CMAKE_PLATFORM_UNIX 1)
94   set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
95   set(CLR_CMAKE_PLATFORM_OPENBSD 1)
96 endif(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
97
98 if(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
99   set(CLR_CMAKE_PLATFORM_UNIX 1)
100   set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
101   set(CLR_CMAKE_PLATFORM_NETBSD 1)
102 endif(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
103
104 if(CMAKE_SYSTEM_NAME STREQUAL SunOS)
105   set(CLR_CMAKE_PLATFORM_UNIX 1)
106   EXECUTE_PROCESS(
107     COMMAND isainfo -n
108     OUTPUT_VARIABLE SUNOS_NATIVE_INSTRUCTION_SET
109     )
110   if(SUNOS_NATIVE_INSTRUCTION_SET MATCHES "amd64")
111     set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
112     set(CMAKE_SYSTEM_PROCESSOR "amd64")
113   else()
114     clr_unknown_arch()
115   endif()
116   set(CLR_CMAKE_PLATFORM_SUNOS 1)
117 endif(CMAKE_SYSTEM_NAME STREQUAL SunOS)
118
119 #--------------------------------------------
120 # This repo builds two set of binaries
121 # 1. binaries which execute on target arch machine
122 #        - for such binaries host architecture & target architecture are same
123 #        - eg. coreclr.dll
124 # 2. binaries which execute on host machine but target another architecture
125 #        - host architecture is different from target architecture
126 #        - eg. crossgen.exe - runs on x64 machine and generates nis targeting arm64
127 #        - for complete list of such binaries refer to file crosscomponents.cmake
128 #-------------------------------------------------------------
129 # Set HOST architecture variables
130 if(CLR_CMAKE_PLATFORM_UNIX_ARM)
131     set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
132     set(CLR_CMAKE_HOST_ARCH "arm")
133 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
134     set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
135     set(CLR_CMAKE_HOST_ARCH "arm64")
136 elseif(CLR_CMAKE_PLATFORM_UNIX_AMD64)
137     set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
138     set(CLR_CMAKE_HOST_ARCH "x64")
139 elseif(CLR_CMAKE_PLATFORM_UNIX_X86)
140     set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
141     set(CLR_CMAKE_HOST_ARCH "x86")
142 elseif(WIN32)
143     # CLR_CMAKE_HOST_ARCH is passed in as param to cmake
144     if (CLR_CMAKE_HOST_ARCH STREQUAL x64)
145         set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
146     elseif(CLR_CMAKE_HOST_ARCH STREQUAL x86)
147         set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
148     elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm)
149         set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
150     elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm64)
151         set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
152     else()
153         clr_unknown_arch()
154     endif()
155 endif()
156
157 # Set TARGET architecture variables
158 # Target arch will be a cmake param (optional) for both windows as well as non-windows build
159 # if target arch is not specified then host & target are same
160 if(NOT DEFINED CLR_CMAKE_TARGET_ARCH OR CLR_CMAKE_TARGET_ARCH STREQUAL "" )
161   set(CLR_CMAKE_TARGET_ARCH ${CLR_CMAKE_HOST_ARCH})
162 endif()
163
164 # Set target architecture variables
165 if (CLR_CMAKE_TARGET_ARCH STREQUAL x64)
166     set(CLR_CMAKE_TARGET_ARCH_AMD64 1)
167   elseif(CLR_CMAKE_TARGET_ARCH STREQUAL x86)
168     set(CLR_CMAKE_TARGET_ARCH_I386 1)
169   elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm64)
170     set(CLR_CMAKE_TARGET_ARCH_ARM64 1)
171   elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm)
172     set(CLR_CMAKE_TARGET_ARCH_ARM 1)
173   elseif(CLR_CMAKE_TARGET_ARCH STREQUAL armel)
174     set(CLR_CMAKE_TARGET_ARCH_ARM 1)
175     set(ARM_SOFTFP 1)
176   else()
177     clr_unknown_arch()
178 endif()
179
180 # check if host & target arch combination are valid
181 if(NOT(CLR_CMAKE_TARGET_ARCH STREQUAL CLR_CMAKE_HOST_ARCH))
182     if(NOT((CLR_CMAKE_PLATFORM_ARCH_AMD64 AND CLR_CMAKE_TARGET_ARCH_ARM64) OR (CLR_CMAKE_PLATFORM_ARCH_I386 AND CLR_CMAKE_TARGET_ARCH_ARM) OR (CLR_CMAKE_PLATFORM_ARCH_AMD64 AND CLR_CMAKE_TARGET_ARCH_ARM)))
183         message(FATAL_ERROR "Invalid host and target arch combination")
184     endif()
185 endif()
186
187 #-----------------------------------------------------
188 # Initialize Cmake compiler flags and other variables
189 #-----------------------------------------------------
190
191 if(WIN32)
192     add_compile_options(/Zi /FC /Zc:strictStrings)
193 elseif (CLR_CMAKE_PLATFORM_UNIX)
194     add_compile_options(-g)
195     add_compile_options(-Wall)
196     if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
197         add_compile_options(-Wno-null-conversion)
198     else()
199         add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Werror=conversion-null>)
200     endif()
201 endif()
202
203 if (CMAKE_CONFIGURATION_TYPES) # multi-configuration generator?
204     set(CMAKE_CONFIGURATION_TYPES "Debug;Checked;Release;RelWithDebInfo" CACHE STRING "" FORCE)
205 endif (CMAKE_CONFIGURATION_TYPES)
206
207 set(CMAKE_C_FLAGS_CHECKED "")
208 set(CMAKE_CXX_FLAGS_CHECKED "")
209 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "")
210 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "")
211
212 set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc.
213
214 if (WIN32)
215   # For multi-configuration toolset (as Visual Studio)
216   # set the different configuration defines.
217   foreach (Config DEBUG CHECKED RELEASE RELWITHDEBINFO)
218     foreach (Definition IN LISTS CLR_DEFINES_${Config}_INIT)
219       set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:${Config}>:${Definition}>)
220     endforeach (Definition)
221   endforeach (Config)
222
223   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /GUARD:CF")
224   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")
225
226   # Linker flags
227   #
228   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO") #Do not create Side-by-Side Assembly Manifest
229
230   if (CLR_CMAKE_PLATFORM_ARCH_ARM)
231     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.02") #windows subsystem - arm minimum is 6.02
232   elseif(CLR_CMAKE_PLATFORM_ARCH_ARM64)
233     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.03") #windows subsystem - arm64 minimum is 6.03
234   else ()
235     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.01") #windows subsystem
236   endif ()
237
238   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LARGEADDRESSAWARE") # can handle addresses larger than 2 gigabytes
239   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NXCOMPAT") #Compatible with Data Execution Prevention
240   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DYNAMICBASE") #Use address space layout randomization
241   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /PDBCOMPRESS") #shrink pdb size
242   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG")
243   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /IGNORE:4197,4013,4254,4070,4221")
244
245   set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221")
246
247   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /PDBCOMPRESS")
248   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:1572864")
249
250   # Temporarily disable incremental link due to incremental linking CFG bug crashing crossgen.
251   # See https://github.com/dotnet/coreclr/issues/12592
252   # This has been fixed in VS 2017 Update 5 but we're keeping this around until everyone is off
253   # the versions that have the bug. The bug manifests itself as a bad crash.
254   set(NO_INCREMENTAL_LINKER_FLAGS "/INCREMENTAL:NO")
255
256   # Debug build specific flags
257   set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
258   set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}")
259
260   # Checked build specific flags
261   set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF /NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
262   set(CMAKE_STATIC_LINKER_FLAGS_CHECKED "${CMAKE_STATIC_LINKER_FLAGS_CHECKED}")
263   set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF ${NO_INCREMENTAL_LINKER_FLAGS}")
264
265   # Release build specific flags
266   set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
267   set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
268   set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
269
270   # ReleaseWithDebugInfo build specific flags
271   set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
272   set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
273   set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
274
275   # Force uCRT to be dynamically linked for Release build
276   set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
277   set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
278   set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
279   set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
280
281 elseif (CLR_CMAKE_PLATFORM_UNIX)
282   # Set the values to display when interactively configuring CMAKE_BUILD_TYPE
283   set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "DEBUG;CHECKED;RELEASE;RELWITHDEBINFO")
284
285   # Use uppercase CMAKE_BUILD_TYPE for the string comparisons below
286   string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
287
288   # For single-configuration toolset
289   # set the different configuration defines.
290   if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
291     # First DEBUG
292     set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_DEBUG_INIT})
293   elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
294     # Then CHECKED
295     set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_CHECKED_INIT})
296   elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
297     # Then RELEASE
298     set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELEASE_INIT})
299   elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
300     # And then RELWITHDEBINFO
301     set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELWITHDEBINFO_INIT})
302   else ()
303     message(FATAL_ERROR "Unknown build type! Set CMAKE_BUILD_TYPE to DEBUG, CHECKED, RELEASE, or RELWITHDEBINFO!")
304   endif ()
305
306   if(DEFINED ENV{TIZEN_ASAN_ENVIRONMENT})
307     if (CLR_CMAKE_PLATFORM_UNIX_ARM OR CLR_CMAKE_PLATFORM_UNIX_AMD64)
308       message(STATUS "TIZEN_ASAN_ENVIRONMENT is enabled.")
309       # add definitions to enable ASan support for only external libraries (supported only Tizen)
310       add_definitions(-DTIZEN_ASAN_ENVIRONMENT -DHAS_ADDRESS_SANITIZER)
311       set(TIZEN_ASAN_ENVIRONMENT 1)
312     else()
313       message(STATUS "TIZEN_ASAN_ENVIRONMENT cannot be enabled. Current arch is not supported.")
314     endif()
315   endif()
316
317   # set the CLANG sanitizer flags for debug build
318   if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
319     # obtain settings from running enablesanitizers.sh
320     string(FIND "$ENV{DEBUG_SANITIZERS}" "asan" __ASAN_POS)
321     string(FIND "$ENV{DEBUG_SANITIZERS}" "ubsan" __UBSAN_POS)
322     if ((${__ASAN_POS} GREATER -1) OR (${__UBSAN_POS} GREATER -1))
323       set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS} -fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/sanitizerblacklist.txt -fsanitize=")
324       set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS} -fsanitize=")
325       if (${__ASAN_POS} GREATER -1)
326         set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS}address,")
327         set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}address,")
328         add_definitions(-DHAS_ASAN)
329         message("Address Sanitizer (asan) enabled")
330       endif ()
331       if (${__UBSAN_POS} GREATER -1)
332         # all sanitizier flags are enabled except alignment (due to heavy use of __unaligned modifier)
333         set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS}bool,bounds,enum,float-cast-overflow,float-divide-by-zero,function,integer,nonnull-attribute,null,object-size,return,returns-nonnull-attribute,shift,unreachable,vla-bound,vptr")
334         set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}undefined")
335         message("Undefined Behavior Sanitizer (ubsan) enabled")
336       endif ()
337
338       # -fdata-sections -ffunction-sections: each function has own section instead of one per .o file (needed for --gc-sections)
339       # -O1: optimization level used instead of -O0 to avoid compile error "invalid operand for inline asm constraint"
340       set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections  -O1")
341       set(CMAKE_CXX_FLAGS_CHECKED "${CMAKE_CXX_FLAGS_CHECKED} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections  -O1")
342
343       set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS}")
344       set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS}")
345
346       # -Wl and --gc-sections: drop unused sections\functions (similar to Windows /Gy function-level-linking)
347       set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
348       set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
349     endif ()
350   endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
351
352   # This linker option causes executables we build to be marked as containing position independent code.
353   # It is necessary to make ASLR work for executables.
354   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie" )
355
356 endif(WIN32)
357
358 # CLR_ADDITIONAL_LINKER_FLAGS - used for passing additional arguments to linker
359 # CLR_ADDITIONAL_COMPILER_OPTIONS - used for passing additional arguments to compiler
360 #
361 # For example:
362 #       ./build-native.sh cmakeargs "-DCLR_ADDITIONAL_COMPILER_OPTIONS=<...>" cmakeargs "-DCLR_ADDITIONAL_LINKER_FLAGS=<...>"
363 #
364 if(CLR_CMAKE_PLATFORM_UNIX)
365     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CLR_ADDITIONAL_LINKER_FLAGS}")
366     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CLR_ADDITIONAL_LINKER_FLAGS}" )
367 endif(CLR_CMAKE_PLATFORM_UNIX)
368
369 if(CLR_CMAKE_PLATFORM_LINUX)
370   set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
371   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--build-id=sha1")
372   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id=sha1")
373 endif(CLR_CMAKE_PLATFORM_LINUX)
374 if(CLR_CMAKE_PLATFORM_FREEBSD)
375   set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
376   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld -Xlinker --build-id=sha1")
377   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -Xlinker --build-id=sha1")
378 endif(CLR_CMAKE_PLATFORM_FREEBSD)
379
380 #------------------------------------
381 # Definitions (for platform)
382 #-----------------------------------
383 if (CLR_CMAKE_PLATFORM_ARCH_AMD64)
384   add_definitions(-D_AMD64_)
385   add_definitions(-D_WIN64)
386   add_definitions(-DAMD64)
387   add_definitions(-DBIT64)
388 elseif (CLR_CMAKE_PLATFORM_ARCH_I386)
389   add_definitions(-D_X86_)
390 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM)
391   add_definitions(-D_ARM_)
392   add_definitions(-DARM)
393 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM64)
394   add_definitions(-D_ARM64_)
395   add_definitions(-DARM64)
396   add_definitions(-D_WIN64)
397   add_definitions(-DBIT64)
398 else ()
399   clr_unknown_arch()
400 endif ()
401
402 if (CLR_CMAKE_PLATFORM_UNIX)
403   if(CLR_CMAKE_PLATFORM_LINUX)
404     if(CLR_CMAKE_PLATFORM_UNIX_AMD64)
405       message("Detected Linux x86_64")
406       add_definitions(-DLINUX64)
407     elseif(CLR_CMAKE_PLATFORM_UNIX_ARM)
408       message("Detected Linux ARM")
409       add_definitions(-DLINUX32)
410     elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
411       message("Detected Linux ARM64")
412       add_definitions(-DLINUX64)
413     elseif(CLR_CMAKE_PLATFORM_UNIX_X86)
414       message("Detected Linux i686")
415       add_definitions(-DLINUX32)
416     else()
417       clr_unknown_arch()
418     endif()
419   endif(CLR_CMAKE_PLATFORM_LINUX)
420 endif(CLR_CMAKE_PLATFORM_UNIX)
421
422 if (CLR_CMAKE_PLATFORM_UNIX)
423   add_definitions(-DPLATFORM_UNIX)
424
425   if(CLR_CMAKE_PLATFORM_DARWIN)
426     message("Detected OSX x86_64")
427   endif(CLR_CMAKE_PLATFORM_DARWIN)
428
429   if(CLR_CMAKE_PLATFORM_FREEBSD)
430     message("Detected FreeBSD amd64")
431   endif(CLR_CMAKE_PLATFORM_FREEBSD)
432
433   if(CLR_CMAKE_PLATFORM_NETBSD)
434     message("Detected NetBSD amd64")
435   endif(CLR_CMAKE_PLATFORM_NETBSD)
436 endif(CLR_CMAKE_PLATFORM_UNIX)
437
438 if (WIN32)
439   add_definitions(-DPLATFORM_WINDOWS)
440
441   # Define the CRT lib references that link into Desktop imports
442   set(STATIC_MT_CRT_LIB  "libcmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
443   set(STATIC_MT_VCRT_LIB  "libvcruntime$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
444   set(STATIC_MT_CPP_LIB  "libcpmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
445 endif(WIN32)
446
447 # Architecture specific files folder name
448 if (CLR_CMAKE_TARGET_ARCH_AMD64)
449     set(ARCH_SOURCES_DIR amd64)
450 elseif (CLR_CMAKE_TARGET_ARCH_ARM64)
451     set(ARCH_SOURCES_DIR arm64)
452 elseif (CLR_CMAKE_TARGET_ARCH_ARM)
453     set(ARCH_SOURCES_DIR arm)
454 elseif (CLR_CMAKE_TARGET_ARCH_I386)
455     set(ARCH_SOURCES_DIR i386)
456 else ()
457     clr_unknown_arch()
458 endif ()
459
460 #--------------------------------------
461 # Compile Options
462 #--------------------------------------
463 if (CLR_CMAKE_PLATFORM_UNIX)
464   # Disable frame pointer optimizations so profilers can get better call stacks
465   add_compile_options(-fno-omit-frame-pointer)
466
467   # The -fms-extensions enable the stuff like __if_exists, __declspec(uuid()), etc.
468   add_compile_options(-fms-extensions )
469   #-fms-compatibility      Enable full Microsoft Visual C++ compatibility
470   #-fms-extensions         Accept some non-standard constructs supported by the Microsoft compiler
471
472   # Make signed arithmetic overflow of addition, subtraction, and multiplication wrap around
473   # using twos-complement representation (this is normally undefined according to the C++ spec).
474   add_compile_options(-fwrapv)
475
476   if(CLR_CMAKE_PLATFORM_DARWIN)
477     # We cannot enable "stack-protector-strong" on OS X due to a bug in clang compiler (current version 7.0.2)
478     add_compile_options(-fstack-protector)
479   else()
480     add_compile_options(-fstack-protector-strong)
481   endif(CLR_CMAKE_PLATFORM_DARWIN)
482
483   # Contracts are disabled on UNIX.
484   add_definitions(-DDISABLE_CONTRACTS)
485
486   if (CLR_CMAKE_WARNINGS_ARE_ERRORS)
487     # All warnings that are not explicitly disabled are reported as errors
488     add_compile_options(-Werror)
489   endif(CLR_CMAKE_WARNINGS_ARE_ERRORS)
490
491   # Disabled common warnings
492   add_compile_options(-Wno-unused-variable)
493   add_compile_options(-Wno-unused-value)
494   add_compile_options(-Wno-unused-function)
495
496   #These seem to indicate real issues
497   add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Wno-invalid-offsetof>)
498
499   if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
500     # The -ferror-limit is helpful during the porting, it makes sure the compiler doesn't stop
501     # after hitting just about 20 errors.
502     add_compile_options(-ferror-limit=4096)
503
504     # Disabled warnings
505     add_compile_options(-Wno-unused-private-field)
506     # Explicit constructor calls are not supported by clang (this->ClassName::ClassName())
507     add_compile_options(-Wno-microsoft)
508     # This warning is caused by comparing 'this' to NULL
509     add_compile_options(-Wno-tautological-compare)
510     # There are constants of type BOOL used in a condition. But BOOL is defined as int
511     # and so the compiler thinks that there is a mistake.
512     add_compile_options(-Wno-constant-logical-operand)
513     # We use pshpack1/2/4/8.h and poppack.h headers to set and restore packing. However
514     # clang 6.0 complains when the packing change lifetime is not contained within
515     # a header file.
516     add_compile_options(-Wno-pragma-pack)
517
518     add_compile_options(-Wno-unknown-warning-option)
519
520     # The following warning indicates that an attribute __attribute__((__ms_struct__)) was applied
521     # to a struct or a class that has virtual members or a base class. In that case, clang
522     # may not generate the same object layout as MSVC.
523     add_compile_options(-Wno-incompatible-ms-struct)
524     # Do not convert a #warning into an #error
525     add_compile_options("-Wno-error=#warnings")
526   else()
527     add_compile_options(-Wno-unused-variable)
528     add_compile_options(-Wno-unused-but-set-variable)
529     add_compile_options(-fms-extensions)
530     add_compile_options(-Wno-unknown-pragmas)
531     # Do not convert a #warning into an #error
532     add_compile_options(-Wno-error=cpp)
533     if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
534       add_compile_options(-Wno-nonnull-compare)
535     endif()
536     if (COMPILER_SUPPORTS_F_ALIGNED_NEW)
537       add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-faligned-new>)
538     endif()
539   endif()
540
541   # Some architectures (e.g., ARM) assume char type is unsigned while CoreCLR assumes char is signed
542   # as x64 does. It has been causing issues in ARM (https://github.com/dotnet/coreclr/issues/4746)
543   add_compile_options(-fsigned-char)
544
545   # We mark the function which needs exporting with DLLEXPORT
546   add_compile_options(-fvisibility=hidden)
547
548   # Specify the minimum supported version of macOS
549   if(CLR_CMAKE_PLATFORM_DARWIN)
550     set(MACOS_VERSION_MIN_FLAGS "-mmacosx-version-min=10.12")
551     add_compile_options("${MACOS_VERSION_MIN_FLAGS}")
552     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MACOS_VERSION_MIN_FLAGS}")
553     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${MACOS_VERSION_MIN_FLAGS}")
554   endif(CLR_CMAKE_PLATFORM_DARWIN)
555 endif(CLR_CMAKE_PLATFORM_UNIX)
556
557 if(CLR_CMAKE_PLATFORM_UNIX_ARM)
558    # Because we don't use CMAKE_C_COMPILER/CMAKE_CXX_COMPILER to use clang
559    # we have to set the triple by adding a compiler argument
560    add_compile_options(-mthumb)
561    add_compile_options(-mfpu=vfpv3)
562    if(ARM_SOFTFP)
563      add_definitions(-DARM_SOFTFP)
564      add_compile_options(-mfloat-abi=softfp)
565      if("$ENV{__DistroRid}" MATCHES "tizen.*")
566       add_compile_options(-target armv7l-tizen-linux-gnueabi)
567      else()
568       add_compile_options(-march=armv7-a)
569      endif()
570    else()
571     add_compile_options(-march=armv7-a)
572    endif(ARM_SOFTFP)
573 endif(CLR_CMAKE_PLATFORM_UNIX_ARM)
574
575 if(CLR_CMAKE_PLATFORM_UNIX)
576   add_compile_options(${CLR_ADDITIONAL_COMPILER_OPTIONS})
577 endif(CLR_CMAKE_PLATFORM_UNIX)
578
579 if (WIN32)
580   # Compile options for targeting windows
581
582   # The following options are set by the razzle build
583   add_compile_options(/TP) # compile all files as C++
584   add_compile_options(/d2Zi+) # make optimized builds debugging easier
585   add_compile_options(/nologo) # Suppress Startup Banner
586   add_compile_options(/W3) # set warning level to 3
587   add_compile_options(/WX) # treat warnings as errors
588   add_compile_options(/Oi) # enable intrinsics
589   add_compile_options(/Oy-) # disable suppressing of the creation of frame pointers on the call stack for quicker function calls
590   add_compile_options(/U_MT) # undefine the predefined _MT macro
591   add_compile_options(/GF) # enable read-only string pooling
592   add_compile_options(/Gm-) # disable minimal rebuild
593   add_compile_options(/EHa) # enable C++ EH (w/ SEH exceptions)
594   add_compile_options(/Zp8) # pack structs on 8-byte boundary
595   add_compile_options(/Gy) # separate functions for linker
596   add_compile_options(/Zc:wchar_t-) # C++ language conformance: wchar_t is NOT the native type, but a typedef
597   add_compile_options(/Zc:forScope) # C++ language conformance: enforce Standard C++ for scoping rules
598   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-") # disable C++ RTTI
599   add_compile_options(/FC) # use full pathnames in diagnostics
600   add_compile_options(/MP) # Build with Multiple Processes (number of processes equal to the number of processors)
601   add_compile_options(/GS) # Buffer Security Check
602   add_compile_options(/Zm200) # Specify Precompiled Header Memory Allocation Limit of 150MB
603   add_compile_options(/wd4960 /wd4961 /wd4603 /wd4627 /wd4838 /wd4456 /wd4457 /wd4458 /wd4459 /wd4091 /we4640)
604   add_compile_options(/Zi) # enable debugging information
605   add_compile_options(/ZH:SHA_256) # use SHA256 for generating hashes of compiler processed source files.
606   add_compile_options(/source-charset:utf-8) # Force MSVC to compile source as UTF-8.
607
608   if (CLR_CMAKE_PLATFORM_ARCH_I386)
609     add_compile_options(/Gz)
610   endif (CLR_CMAKE_PLATFORM_ARCH_I386)
611
612   add_compile_options($<$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>:/GL>)
613   add_compile_options($<$<OR:$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>,$<CONFIG:Checked>>:/O1>)
614
615   if (CLR_CMAKE_PLATFORM_ARCH_AMD64)
616   # The generator expression in the following command means that the /homeparams option is added only for debug builds
617   add_compile_options($<$<CONFIG:Debug>:/homeparams>) # Force parameters passed in registers to be written to the stack
618   endif (CLR_CMAKE_PLATFORM_ARCH_AMD64)
619
620   # enable control-flow-guard support for native components for non-Arm64 builds
621   # Added using variables instead of add_compile_options to let individual projects override it
622   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /guard:cf")
623   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf")
624
625   # Statically linked CRT (libcmt[d].lib, libvcruntime[d].lib and libucrt[d].lib) by default. This is done to avoid
626   # linking in VCRUNTIME140.DLL for a simplified xcopy experience by reducing the dependency on VC REDIST.
627   #
628   # For Release builds, we shall dynamically link into uCRT [ucrtbase.dll] (which is pushed down as a Windows Update on downlevel OS) but
629   # wont do the same for debug/checked builds since ucrtbased.dll is not redistributable and Debug/Checked builds are not
630   # production-time scenarios.
631   add_compile_options($<$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>:/MT>)
632   add_compile_options($<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:/MTd>)
633
634   set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /ZH:SHA_256")
635
636   if (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64)
637     # Contracts work too slow on ARM/ARM64 DEBUG/CHECKED.
638     add_definitions(-DDISABLE_CONTRACTS)
639   endif (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64)
640
641 endif (WIN32)
642
643 if(CLR_CMAKE_ENABLE_CODE_COVERAGE)
644
645   if(CLR_CMAKE_PLATFORM_UNIX)
646     string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
647     if(NOT UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
648       message( WARNING "Code coverage results with an optimised (non-Debug) build may be misleading" )
649     endif(NOT UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
650
651     add_compile_options(-fprofile-arcs)
652     add_compile_options(-ftest-coverage)
653     set(CLANG_COVERAGE_LINK_FLAGS  "--coverage")
654     set(CMAKE_SHARED_LINKER_FLAGS  "${CMAKE_SHARED_LINKER_FLAGS} ${CLANG_COVERAGE_LINK_FLAGS}")
655     set(CMAKE_EXE_LINKER_FLAGS     "${CMAKE_EXE_LINKER_FLAGS} ${CLANG_COVERAGE_LINK_FLAGS}")
656   else()
657     message(FATAL_ERROR "Code coverage builds not supported on current platform")
658   endif(CLR_CMAKE_PLATFORM_UNIX)
659
660 endif(CLR_CMAKE_ENABLE_CODE_COVERAGE)