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